Add csv parsing
This commit is contained in:
parent
91cd60b155
commit
e1fa9734da
@ -1,6 +1,9 @@
|
|||||||
package assets
|
package assets
|
||||||
|
|
||||||
|
import "core:bytes"
|
||||||
import "core:c"
|
import "core:c"
|
||||||
|
import "core:encoding/csv"
|
||||||
|
import "core:io"
|
||||||
import "core:log"
|
import "core:log"
|
||||||
import "core:math"
|
import "core:math"
|
||||||
import lg "core:math/linalg"
|
import lg "core:math/linalg"
|
||||||
@ -40,6 +43,10 @@ Loaded_Convex :: struct {
|
|||||||
inertia_tensor: lg.Matrix3f32,
|
inertia_tensor: lg.Matrix3f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loaded_Curve_2D :: struct {
|
||||||
|
points: [][2]f32,
|
||||||
|
}
|
||||||
|
|
||||||
destroy_loaded_bvh :: proc(loaded_bvh: Loaded_BVH) {
|
destroy_loaded_bvh :: proc(loaded_bvh: Loaded_BVH) {
|
||||||
tracy.Zone()
|
tracy.Zone()
|
||||||
|
|
||||||
@ -53,6 +60,7 @@ Asset_Manager :: struct {
|
|||||||
textures: map[cstring]Loaded_Texture,
|
textures: map[cstring]Loaded_Texture,
|
||||||
models: map[cstring]Loaded_Model,
|
models: map[cstring]Loaded_Model,
|
||||||
bvhs: map[cstring]Loaded_BVH,
|
bvhs: map[cstring]Loaded_BVH,
|
||||||
|
curves: map[cstring]Loaded_Curve_2D,
|
||||||
}
|
}
|
||||||
|
|
||||||
get_texture :: proc(assetman: ^Asset_Manager, path: cstring) -> rl.Texture2D {
|
get_texture :: proc(assetman: ^Asset_Manager, path: cstring) -> rl.Texture2D {
|
||||||
@ -177,31 +185,69 @@ get_bvh :: proc(assetman: ^Asset_Manager, path: cstring) -> Loaded_BVH {
|
|||||||
return assetman.bvhs[path]
|
return assetman.bvhs[path]
|
||||||
}
|
}
|
||||||
|
|
||||||
Curve_2D :: struct {
|
// TODO: cache result
|
||||||
points: [][2]f32,
|
// Reads a two column comma separated csv file as a curve
|
||||||
|
get_curve_2d :: proc(assetman: ^Asset_Manager, path: string) -> (curve: Loaded_Curve_2D) {
|
||||||
|
data, err := os2.read_entire_file_from_path(path, context.temp_allocator)
|
||||||
|
if err != nil {
|
||||||
|
log.errorf("Failed to read curve: %s", path)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reads a two column comma separated csv file as a curve
|
bytes_reader: bytes.Reader
|
||||||
// get_curve_2d :: proc(assetman: ^Asset_Manager, path: string) -> (curve: Curve_2D) {
|
bytes.reader_init(&bytes_reader, data)
|
||||||
// data, err := os2.read_entire_file_from_path(path, context.allocator)
|
bytes_stream := bytes.reader_to_stream(&bytes_reader)
|
||||||
// if err != nil {
|
|
||||||
// log.errorf("Failed to read curve: %s", path)
|
csv_reader: csv.Reader
|
||||||
// return
|
csv.reader_init(&csv_reader, bytes_stream, context.temp_allocator)
|
||||||
// }
|
|
||||||
// defer delete(data)
|
tmp_result := make([dynamic][2]f32, context.temp_allocator)
|
||||||
//
|
|
||||||
// str := string(data)
|
skipped_header := false
|
||||||
// scan: scanner.Scanner
|
for {
|
||||||
// scanner.init(&scan, str, path)
|
row, err := csv.read(&csv_reader, context.temp_allocator)
|
||||||
// scan.whitespace = scanner.Whitespace{' ', '\t', '\r'}
|
if err != nil {
|
||||||
// scan.flags = scanner.Scan_Flags{.Scan_Ints, .Scan_Floats, .Scan_Idents}
|
if err != io.Error.EOF {
|
||||||
//
|
log.errorf("Failed to read curve %v", err)
|
||||||
// for tok := scanner.scan(&scan); tok != scanner.EOF; tok = scanner.scan(&scan) {
|
}
|
||||||
//
|
break
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// return
|
if len(row) != 2 {
|
||||||
// }
|
log.errorf("Curve expected 2 columns, got %v", len(row))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
ok: bool
|
||||||
|
key: f64
|
||||||
|
val: f64
|
||||||
|
key, ok = strconv.parse_f64(row[0])
|
||||||
|
if !ok {
|
||||||
|
if skipped_header {
|
||||||
|
log.errorf("Curve expected numbers, got %s", row[0])
|
||||||
|
break
|
||||||
|
}
|
||||||
|
skipped_header = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val, ok = strconv.parse_f64(row[1])
|
||||||
|
if !ok {
|
||||||
|
if skipped_header {
|
||||||
|
log.errorf("Curve expected numbers, got %s", row[1])
|
||||||
|
break
|
||||||
|
}
|
||||||
|
skipped_header = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
append(&tmp_result, [2]f32{f32(key), f32(val)})
|
||||||
|
}
|
||||||
|
|
||||||
|
curve.points = make([][2]f32, len(tmp_result), context.temp_allocator)
|
||||||
|
copy(curve.points, tmp_result[:])
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
get_convex :: proc(assetman: ^Asset_Manager, path: cstring) -> (result: Loaded_Convex) {
|
get_convex :: proc(assetman: ^Asset_Manager, path: cstring) -> (result: Loaded_Convex) {
|
||||||
bytes, err := os2.read_entire_file(string(path), context.temp_allocator)
|
bytes, err := os2.read_entire_file(string(path), context.temp_allocator)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user