#! Contains the tests of `fbx`: an ASCII scene of every part read, written as #! binary and read again; a document built by hand, written as binary of 7.4 #! and 7.5 and read back; an array compressed by zlib; models placed by FBX's #! chain of pivots and rotations, and a scene's axes turned; a curve's values; #! and what's refused. import std::math; import std::testing; import malunal::linear; import malunal::fbx; # Whether two floats are within a ten-thousandth. def close_to(a: f32, b: f32): bool { return math::abs(a - b) < 1e-4; } # A scene of every part FBX's scenes hold, as ASCII: its settings, Z up and # in meters; templates; a quad of two triangles, its normals by point, its # places on a texture through indices, and a material for each triangle; a # model placed with a pre-rotation, and a bone beneath it, holding a light; # two materials, the first with a texture, whose video embeds four bytes; a # skin of one cluster; and an animation turning the bone. const scene_text: strview = """ ; FBX 7.4.0 project file FBXHeaderExtension: { FBXHeaderVersion: 1003 FBXVersion: 7400 } GlobalSettings: { Version: 1000 Properties70: { P: "UpAxis", "int", "Integer", "",2 P: "UpAxisSign", "int", "Integer", "",1 P: "FrontAxis", "int", "Integer", "",1 P: "FrontAxisSign", "int", "Integer", "",-1 P: "CoordAxis", "int", "Integer", "",0 P: "CoordAxisSign", "int", "Integer", "",1 P: "UnitScaleFactor", "double", "Number", "",100 P: "TimeMode", "enum", "", "",11 } } Definitions: { ObjectType: "Model" { Count: 2 PropertyTemplate: "FbxNode" { Properties70: { P: "Lcl Scaling", "Lcl Scaling", "", "A",1,1,1 P: "Visibility", "Visibility", "", "A",1 } } } ObjectType: "Material" { PropertyTemplate: "FbxSurfacePhong" { Properties70: { P: "ShininessExponent", "Number", "", "A",42 } } } } Objects: { Geometry: 100, "Geometry::Quad", "Mesh" { Vertices: *12 { a: -1,-1,0,1,-1,0,1,1,0,-1,1,0 } PolygonVertexIndex: *6 { a: 0,1,-3,0,2,-4 } LayerElementNormal: 0 { Version: 101 MappingInformationType: "ByVertice" ReferenceInformationType: "Direct" Normals: *12 { a: 0,0,1,0,0,1,0,0,1,0,0,1 } } LayerElementUV: 0 { Name: "UVMap" MappingInformationType: "ByPolygonVertex" ReferenceInformationType: "IndexToDirect" UV: *8 { a: 0,0,1,0,1,1,0,1 } UVIndex: *6 { a: 0,1,2,0,2,3 } } LayerElementMaterial: 0 { MappingInformationType: "ByPolygon" ReferenceInformationType: "IndexToDirect" Materials: *2 { a: 0,1 } } } Model: 200, "Model::Quad", "Mesh" { Properties70: { P: "Lcl Translation", "Lcl Translation", "", "A",0,0,5 P: "RotationActive", "bool", "", "",1 P: "PreRotation", "Vector3D", "Vector", "",-90,0,0 } Shading: T Culling: "CullingOff" } Model: 201, "Model::Bone", "LimbNode" { Properties70: { P: "Lcl Translation", "Lcl Translation", "", "A",0,2,0 P: "Lcl Rotation", "Lcl Rotation", "", "A",0,0,90 } } Material: 300, "Material::Red", "" { ShadingModel: "phong" Properties70: { P: "DiffuseColor", "Color", "", "A",1,0,0 } } Material: 301, "Material::Blue", "" { ShadingModel: "lambert" } Texture: 400, "Texture::Bricks", "" { FileName: "C:/textures/bricks.png" RelativeFilename: "textures/bricks.png" Properties70: { P: "UVSet", "KString", "", "", "UVMap" } } Video: 500, "Video::Bricks", "Clip" { Content: , "AAEC/w==" RelativeFilename: "textures/bricks.png" } NodeAttribute: 600, "NodeAttribute::Sun", "Light" { Properties70: { P: "LightType", "enum", "", "",1 P: "Intensity", "Number", "", "A",150 } } Deformer: 700, "Deformer::Skin", "Skin" { } Deformer: 701, "SubDeformer::Bone", "Cluster" { Indexes: *2 { a: 0,1 } Weights: *2 { a: 1,0.5 } Transform: *16 { a: 1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1 } TransformLink: *16 { a: 1,0,0,0,0,1,0,0,0,0,1,0,0,2,0,1 } } AnimationStack: 800, "AnimStack::Wave", "" { Properties70: { P: "LocalStop", "KTime", "Time", "",46186158000 } } AnimationLayer: 801, "AnimLayer::Base", "" { } AnimationCurveNode: 802, "AnimCurveNode::R", "" { Properties70: { P: "d|X", "Number", "", "A",0 P: "d|Y", "Number", "", "A",0 P: "d|Z", "Number", "", "A",90 } } AnimationCurve: 803, "AnimCurve::", "" { Default: 90 KeyVer: 4008 KeyTime: *2 { a: 0,46186158000 } KeyValueFloat: *2 { a: 90,180 } KeyAttrFlags: *1 { a: 4 } KeyAttrDataFloat: *4 { a: 0,0,0,0 } KeyAttrRefCount: *1 { a: 2 } } } Connections: { ;Model::Quad, Model::RootNode C: "OO",200,0 C: "OO",201,200 C: "OO",100,200 C: "OO",300,200 C: "OO",301,200 C: "OP",400,300, "DiffuseColor" C: "OO",500,400 C: "OO",600,201 C: "OO",700,100 C: "OO",701,700 C: "OO",201,701 C: "OO",801,800 C: "OO",802,801 C: "OP",802,201, "Lcl Rotation" C: "OP",803,802, "d|Z" } """; # Checks the scene read of `scene_text`, whichever way it was written. def expect_scene(t: &mut tester, s: &fbx::scene): void { t.expects(s.settings.up_axis == 2 && s.settings.front_sign == -1 && close_to(s.settings.meters_per_unit(), 1) && s.settings.frame_rate == 24); t.expects(s.meshes.length == 1 && s.models.length == 2 && s.materials.length == 2 && s.textures.length == 1 && s.videos.length == 1); val quad := &s.meshes[0]; t.expects(quad.name.span == "Quad" && quad.positions.length == 4 && quad.polygons.length == 2 && quad.polygons[1].first == 3 && quad.polygons[1].count == 3); t.expects(quad.corners.length == 6 && quad.corners[2] == 2 && quad.corners[5] == 3); t.expects(quad.normals.length == 6 && close_to(quad.normals[5].z, 1)); t.expects(quad.uv_sets.length == 1 && quad.uv_sets[0].name.span == "UVMap" && close_to(quad.uv_sets[0].uvs[4].x, 1) && close_to(quad.uv_sets[0].uvs[5].y, 1) && close_to(quad.uv_sets[0].uvs[5].x, 0)); t.expects(quad.materials.length == 2 && quad.materials[0] == 0 && quad.materials[1] == 1); # The tree, and what's whose. val square := &s.models[0]; t.expects(square.name.span == "Quad" && square.kind.span == "Mesh" && square.parent == fbx::absent && square.mesh == 0 && square.materials.length == 2 && square.materials[1] == 1); t.expects(s.roots.length == 1 && s.roots[0] == 0 && square.children.length == 1 && square.children[0] == 1); t.expects(close_to(square.translation.z, 5) && square.rotation_active && close_to(square.pre_rotation.x, -90) && close_to(square.scale.y, 1) && square.visible); val bone := &s.models[1]; t.expects(bone.kind.span == "LimbNode" && bone.parent == 0 && bone.light == 0 && close_to(bone.rotation.z, 90)); # The bone's place: 2 up, turned by its parent's pre-rotation to 2 back, from 5 forward. val placed := s.world_matrix(1); t.expects(close_to(placed.m[12], 0) && close_to(placed.m[13], 0) && close_to(placed.m[14], 3)); # Materials, their template's values, and their textures. val red := &s.materials[0]; t.expects(red.name.span == "Red" && red.shading.span == "phong" && close_to(red.diffuse.x, 1) && close_to(red.diffuse.y, 0) && close_to(red.shininess, 42)); t.expects(red.map("DiffuseColor") is some(k) && k == 0 && red.map("NormalMap") is none); t.expects(close_to(s.materials[1].diffuse.x, 0.8) && close_to(s.materials[1].shininess, 42) && s.materials[1].shading.span == "lambert"); val bricks := &s.textures[0]; t.expects(bricks.file.span == "C:/textures/bricks.png" && bricks.relative.span == "textures/bricks.png" && bricks.uv_set.span == "UVMap" && bricks.video == 0); val held := &s.videos[0].content; t.expects(held.length == 4 && held[0] == 0 && held[2] == 2 && held[3] == 255); t.expects(s.lights.length == 1 && s.lights[0].kind == fbx::light_kind::directional && close_to(s.lights[0].intensity, 150)); # The skin, its cluster, and its bone. t.expects(s.skins.length == 1 && s.skins[0].mesh == 0 && quad.skin == 0 && s.skins[0].clusters.length == 1); val bound := &s.clusters[0]; t.expects(bound.bone == 1 && bound.points.length == 2 && bound.points[1] == 1 && close_to(bound.weights[1], 0.5) && close_to(bound.transform_link.m[13], 2)); # The animation, what it turns, and its curve. t.expects(s.stacks.length == 1 && s.stacks[0].stop == 1 && s.stacks[0].layers.length == 1 && s.layers[0].curve_nodes.length == 1); val turning := &s.curve_nodes[0]; t.expects(turning.target_kind == fbx::part_kind::model && turning.target == 1 && turning.property.span == "Lcl Rotation" && turning.channels.length == 3); if turning.channel("Z") is some(&z) { t.expects(z.curve == 0 && close_to(z.value, 90)); } else { t.expects(false); } t.expects(turning.channel("X") is some(&x) && x.curve == fbx::absent); val wave := &s.curves[0]; t.expects(wave.times.length == 2 && wave.times[1] == 1 && wave.interpolations[0] == fbx::key_interpolation::linear && close_to(wave.at(0.5), 135) && close_to(wave.at(2), 180)); } @test def reads_an_ascii_scene_of_every_part(t: &mut tester): void { var doc := fbx::decode(scene_text).expect("The scene is read."); t.expects(doc.version == 7400 && !doc.binary); if doc.child(fbx::absent, "Objects") is some(objects) { t.expects(doc.nodes[objects as u64].children.length == 14); } else { t.expects(false); } val s := fbx::scene::of(move(doc)).expect("The scene's objects are read."); expect_scene(t, &s); } @test def writes_a_scene_as_binary_and_reads_it_again(t: &mut tester): void { val doc := fbx::decode(scene_text).expect("The scene is read."); val bytes := fbx::encode(&doc).expect("It's written."); # The header, and Autodesk's footer. t.expects(bytes.length > 200 && bytes[0] == 'K' && bytes[21] == 0x1A && bytes[bytes.length - 1] == 0x0B && bytes[bytes.length - 16] == 0xF8); var again := fbx::decode(bytes.span).expect("It's read again."); t.expects(again.binary && again.version == 7400 && again.nodes.length == doc.nodes.length); val s := fbx::scene::of(move(again)).expect("The scene's objects are read."); expect_scene(t, &s); } # A name as binary FBX writes it: the name, two bytes 0 and 1, and its class. def binary_name(name: strview, class_name: strview): string { var made := string::from(name, nothing{}); made.push(0); made.push(1); made.append(class_name); return made; } # A document built by hand: a header, a quad's geometry, its model, and the # connections between them. def built_document(version: u32): fbx::document { var doc := fbx::document{ version = version }; val header := doc.add(fbx::absent, "FBXHeaderExtension"); val stated := doc.add(header, "FBXVersion"); doc.nodes[stated as u64].properties.append(fbx::property::of_int32(version as i32)); val objects := doc.add(fbx::absent, "Objects"); val geometry := doc.add(objects, "Geometry"); val geometry_name := binary_name("Quad", "Geometry"); doc.nodes[geometry as u64].properties.append(fbx::property::of_int64(100)); doc.nodes[geometry as u64].properties.append(fbx::property::of_text(geometry_name.span)); doc.nodes[geometry as u64].properties.append(fbx::property::of_text("Mesh")); val places: f64[12] = [-1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0]; val vertices := doc.add(geometry, "Vertices"); doc.nodes[vertices as u64].properties.append(fbx::property::of_reals(fbx::property_kind::float64s, places[:])); val corners: i64[6] = [0, 1, -3, 0, 2, -4]; val polygons := doc.add(geometry, "PolygonVertexIndex"); doc.nodes[polygons as u64].properties.append(fbx::property::of_integers(fbx::property_kind::int32s, corners[:])); val model := doc.add(objects, "Model"); val model_name := binary_name("Quad", "Model"); doc.nodes[model as u64].properties.append(fbx::property::of_int64(200)); doc.nodes[model as u64].properties.append(fbx::property::of_text(model_name.span)); doc.nodes[model as u64].properties.append(fbx::property::of_text("Mesh")); val properties := doc.add(model, "Properties70"); val moved := doc.add(properties, "P"); doc.nodes[moved as u64].properties.append(fbx::property::of_text("Lcl Translation")); doc.nodes[moved as u64].properties.append(fbx::property::of_text("Lcl Translation")); doc.nodes[moved as u64].properties.append(fbx::property::of_text("")); doc.nodes[moved as u64].properties.append(fbx::property::of_text("A")); doc.nodes[moved as u64].properties.append(fbx::property::of_float64(0)); doc.nodes[moved as u64].properties.append(fbx::property::of_float64(0)); doc.nodes[moved as u64].properties.append(fbx::property::of_float64(5)); val shown := doc.add(model, "Shading"); doc.nodes[shown as u64].properties.append(fbx::property::of_bool(true)); val flags := doc.add(model, "Flags"); doc.nodes[flags as u64].properties.append(fbx::property::of_float32(1.5)); val connections := doc.add(fbx::absent, "Connections"); val pairs: i64[4] = [200, 0, 100, 200]; for k in 0..2u { val link := doc.add(connections, "C"); doc.nodes[link as u64].properties.append(fbx::property::of_text("OO")); doc.nodes[link as u64].properties.append(fbx::property::of_int64(pairs[k * 2])); doc.nodes[link as u64].properties.append(fbx::property::of_int64(pairs[k * 2 + 1])); } return doc; } @test def writes_a_built_document_as_binary_of_both_widths(t: &mut tester): void { for k in 0..2u { val version: u32 = k == 0 ? 7400 : 7500; val doc := built_document(version); val bytes := fbx::encode(&doc).expect("It's written."); var read := fbx::decode(bytes.span).expect("It's read back."); t.expects(read.version == version && read.nodes.length == doc.nodes.length && read.roots.length == 3); for i in 0..doc.nodes.length { val a := &doc.nodes[i]; val b := &read.nodes[i]; t.expects(a.name.span == b.name.span && a.properties.length == b.properties.length && a.children.length == b.children.length && a.parent == b.parent); for p in 0..a.properties.length { t.expects(a.properties[p].kind == b.properties[p].kind && a.properties[p].integer == b.properties[p].integer && a.properties[p].real == b.properties[p].real); t.expects(a.properties[p].text() == b.properties[p].text() && a.properties[p].count() == b.properties[p].count()); } } if read.child(fbx::absent, "Objects") is some(objects) && read.child(objects, "Geometry") is some(geometry) && read.child(geometry, "PolygonVertexIndex") is some(indices) { val values := read.integers(indices); t.expects(values.length == 6 && values[2] == -3 && values[5] == -4); } else { t.expects(false); } val s := fbx::scene::of(move(read)).expect("Its scene is read."); t.expects(s.models.length == 1 && s.models[0].name.span == "Quad" && s.models[0].mesh == 0 && close_to(s.models[0].translation.z, 5)); t.expects(s.meshes.length == 1 && s.meshes[0].name.span == "Quad" && s.meshes[0].polygons.length == 2 && close_to(s.meshes[0].positions[2].y, 1)); } } # The Adler-32 of bytes. def adler_of(data: span): u32 { var low: u32 = 1; var high: u32 = 0; for byte in data { low = (low + byte as u32) % 65521; high = (high + low) % 65521; } return (high << 16) | low; } def put_word(out: &mut vector, value: u32): void { for i in 0..4u { out.append(((value >> (8 * i)) & 0xFF) as u8); } } @test def reads_an_array_compressed_by_zlib(t: &mut tester): void { # Two doubles, 0.5 and -2, compressed as a stored block within zlib. var raw := vector::::new(); for value in [0.5, -2.0] { val bits := math::bits_of(value as f64); for i in 0..8u { raw.append(((bits >> (8 * i)) & 0xFF) as u8); } } var zlib := vector::::new(); zlib.append(0x78); zlib.append(0x01); zlib.append(0x01); zlib.append(16); zlib.append(0); zlib.append(0xEF); zlib.append(0xFF); zlib.extend(raw.span); val sum := adler_of(raw.span); for i in 0..4u { zlib.append(((sum >> (8 * (3 - i))) & 0xFF) as u8); } # A node `A` of that array, then the record of zeros ending the root. var file := vector::::new(); file.extend("Kaydara FBX Binary "); file.append(0); file.append(0x1A); file.append(0); put_word(&mut file, 7400); val properties := 1 + 12 + zlib.length; val end := file.length + 13 + 1 + properties; put_word(&mut file, end as u32); put_word(&mut file, 1); put_word(&mut file, properties as u32); file.append(1); file.append('A'); file.append('d'); put_word(&mut file, 2); put_word(&mut file, 1); put_word(&mut file, zlib.length as u32); file.extend(zlib.span); for _ in 0..13u { file.append(0); } val doc := fbx::decode(file.span).expect("The compressed array is read."); t.expects(doc.nodes.length == 1 && doc.nodes[0].name.span == "A" && doc.nodes[0].properties[0].kind == fbx::property_kind::float64s); val values := doc.reals(0); t.expects(values.length == 2 && values[0] == 0.5 && values[1] == -2); # Its checksum broken, it won't inflate. file[file.length - 14] = file[file.length - 14] ^ 0xFF; t.expects(fbx::decode(file.span) is error(e) && e.kind == fbx::failure::malformed); } @test def places_models_by_fbx_chain_of_pivots_and_rotations(t: &mut tester): void { # Turned a quarter about z, about a pivot at x = 1. var pivoted := fbx::model{}; pivoted.rotation = vec3{ 0, 0, 90 }; pivoted.rotation_pivot = vec3{ 1, 0, 0 }; val turned := pivoted.local_matrix().transform_point(vec3{ 2, 0, 0 }); t.expects(close_to(turned.x, 1) && close_to(turned.y, 1) && close_to(turned.z, 0)); # Scaled twice about a pivot at x = 1. var scaled := fbx::model{}; scaled.scale = vec3{ 2, 2, 2 }; scaled.scaling_pivot = vec3{ 1, 0, 0 }; val grown := scaled.local_matrix().transform_point(vec3{ 2, 0, 0 }); t.expects(close_to(grown.x, 3) && close_to(grown.y, 0)); # The order of a rotation: x first, or z first; and an order not active is # taken as x first. var ordered := fbx::model{}; ordered.rotation = vec3{ 90, 90, 0 }; val first_x := ordered.local_matrix().transform_point(vec3{ 0, 1, 0 }); t.expects(close_to(first_x.x, 1) && close_to(first_x.z, 0)); ordered.order = fbx::rotation_order::zyx; ordered.rotation_active = true; val first_z := ordered.local_matrix().transform_point(vec3{ 0, 1, 0 }); t.expects(close_to(first_z.z, 1) && close_to(first_z.x, 0)); ordered.rotation_active = false; val inactive := ordered.local_matrix().transform_point(vec3{ 0, 1, 0 }); t.expects(close_to(inactive.x, 1)); # A post-rotation is turned back. var posted := fbx::model{ rotation_active = true }; posted.post_rotation = vec3{ 0, 0, 90 }; val back := posted.local_matrix().transform_point(vec3{ 1, 0, 0 }); t.expects(close_to(back.y, -1)); # Z up, -y to the front, in centimeters, turned to y up and meters. val zup := fbx::settings{ up_axis = 2, front_axis = 1, front_sign = -1 }; val up := zup.axes().transform_point(vec3{ 0, 0, 1 }); val front := zup.conversion().transform_point(vec3{ 0, -100, 0 }); t.expects(close_to(up.y, 1) && close_to(front.z, 1) && close_to(front.y, 0)); } @test def gives_a_curve_s_values_between_its_keys(t: &mut tester): void { var held := fbx::curve{ value = 7 }; t.expects(held.at(1) == 7); held.times.append(0); held.times.append(1); held.times.append(2); held.values.append(0); held.values.append(10); held.values.append(10); held.interpolations.append(fbx::key_interpolation::constant); held.interpolations.append(fbx::key_interpolation::cubic); held.interpolations.append(fbx::key_interpolation::linear); held.right_slopes.append(0); held.right_slopes.append(0); held.right_slopes.append(0); held.next_left_slopes.append(0); held.next_left_slopes.append(0); held.next_left_slopes.append(0); t.expects(held.at(-1) == 0 && held.at(0.5) == 0 && held.at(3) == 10); # A cubic of flat slopes, between equal values, stays. t.expects(close_to(held.at(1.5), 10)); } # Whether bytes are refused as a kind, saying what's given. def refused(bytes: span, kind: fbx::failure, says: strview): bool { if fbx::decode(bytes) is error(e) { val why := e.describe(); return e.kind == kind && why.span.find(says) is some(_); } return false; } @test def refuses_what_breaks_fbx(t: &mut tester): void { val doc := built_document(7400); var bytes := fbx::encode(&doc).expect("It's written."); # Cut short, and a property of a type FBX hasn't. var cut := vector::::new(); cut.extend(bytes.span[0:60]); t.expects(refused(cut.span, fbx::failure::malformed, "past the file's end")); var wrong := vector::::new(); wrong.extend(bytes.span); # The first property of the first node, `FBXHeaderExtension`'s child, is # `FBXVersion`'s, after its header and name. val first: u64 = 27 + 13 + 18 + 13 + 10; t.expects(wrong[first] == 'I'); wrong[first] = 'Z'; t.expects(refused(wrong.span, fbx::failure::malformed, "isn't FBX's")); # Before 7.0. var old := vector::::new(); old.extend(bytes.span); old[23] = 0xD4; old[24] = 0x17; t.expects(refused(old.span, fbx::failure::unsupported, "6100")); # ASCII unclosed, of an array short, and of version 6. t.expects(refused("Objects: {\n Model: 1, \"Model::A\", \"Null\" {\n", fbx::failure::malformed, "isn't closed")); t.expects(refused("Objects: {\n Geometry: 1, \"Geometry::A\", \"Mesh\" {\n Vertices: *3 {\n a: 0,0\n }\n }\n}\n", fbx::failure::malformed, "holds 2")); t.expects(refused("FBXHeaderExtension: {\n FBXVersion: 6100\n}\n", fbx::failure::unsupported, "6100")); # A polygon naming a point the mesh hasn't. var broken := fbx::decode("Objects: {\n Geometry: 1, \"Geometry::A\", \"Mesh\" {\n Vertices: *3 {\n a: 0,0,0\n }\n PolygonVertexIndex: *3 {\n a: 0,0,-3\n }\n }\n}\n").expect("The text is read."); t.expects(fbx::scene::of(move(broken)) is error(e) && e.kind == fbx::failure::malformed); }