#include "stdafx.h" #include "doctest.h" #include "model/clip.h" #include "model/eu07m/eu07m_build.h" #include "model/eu07m/eu07m_io.h" #include #include #include namespace { bool same_rotation(glm::quat const &A, glm::quat const &B, float const Tolerance = 1e-3f) { return std::abs(std::abs(glm::dot(A, B)) - 1.f) <= Tolerance; } glm::quat about_x(float const Degrees) { return glm::angleAxis(glm::radians(Degrees), glm::vec3{1.f, 0.f, 0.f}); } animation::track vector_track(animation::curve const Curve, std::vector Times, std::vector Values) { animation::track track; track.interpolation = Curve; track.times = std::move(Times); track.vectors = std::move(Values); return track; } // a model of one node and a clip turning it, for the checks of the file eu07m::model clip_model() { eu07m::model model; eu07m::node_record node; node.name = model.intern("arm"); node.subtree_end = 1; model.nodes.push_back(node); model.meshes.push_back({0, 0, -1, 0}); model.positions.push_back({0.f, 0.f, 0.f}); model.vertex_attributes.push_back({}); model.materials.push_back({}); eu07m::clip_record clip; clip.name = model.intern("swing"); clip.flags = eu07m::clip_flags::loop | eu07m::clip_flags::autoplay; clip.track_count = 1; clip.duration = 2.f; model.clips.push_back(clip); eu07m::track_record track; track.path = eu07m::track_path::rotation; track.key_count = 3; model.clip_tracks.push_back(track); model.key_times = {0.f, 1.f, 2.f}; for (auto const degrees : {0.f, 60.f, 0.f}) { auto const rotation{about_x(degrees)}; model.key_rotations.push_back(eu07m::encode_rotation({rotation.x, rotation.y, rotation.z, rotation.w})); } return model; } } // namespace TEST_CASE("clip: a rotation packed in 8 bytes comes back") { std::vector const rotations{glm::quat{1.f, 0.f, 0.f, 0.f}, about_x(60.f), about_x(-170.f), glm::angleAxis(2.f, glm::normalize(glm::vec3{1.f, -2.f, 0.5f})), glm::quat{-0.1f, 0.7f, -0.7f, 0.1f}}; for (auto const &source : rotations) { auto const rotation{glm::normalize(source)}; auto const packed{eu07m::encode_rotation({rotation.x, rotation.y, rotation.z, rotation.w})}; CHECK(packed.largest <= 3); auto const value{eu07m::decode_rotation(packed)}; CHECK(same_rotation(rotation, glm::quat{value[3], value[0], value[1], value[2]}, 1e-4f)); } // a quaternion of no length packs as the identity auto const none{eu07m::decode_rotation(eu07m::encode_rotation({0.f, 0.f, 0.f, 0.f}))}; CHECK(none[3] == doctest::Approx(1.f)); } TEST_CASE("clip: vectors by step, linear and cubic keys") { auto const linear{vector_track(animation::curve::linear, {1.f, 3.f}, {glm::vec3{0.f}, glm::vec3{2.f, 4.f, 0.f}})}; CHECK(animation::sample_vector(linear, 2.f).x == doctest::Approx(1.f)); CHECK(animation::sample_vector(linear, 2.f).y == doctest::Approx(2.f)); // held at the first and the last key outside of them CHECK(animation::sample_vector(linear, 0.f).x == doctest::Approx(0.f)); CHECK(animation::sample_vector(linear, 9.f).y == doctest::Approx(4.f)); auto step{linear}; step.interpolation = animation::curve::step; CHECK(animation::sample_vector(step, 2.9f).x == doctest::Approx(0.f)); CHECK(animation::sample_vector(step, 3.f).x == doctest::Approx(2.f)); // cubic with flat tangents eases in and out: half way at the middle, a quarter of the way gives 0.15625 auto const cubic{vector_track(animation::curve::cubic, {0.f, 1.f}, {glm::vec3{0.f}, glm::vec3{0.f}, glm::vec3{0.f}, glm::vec3{0.f}, glm::vec3{1.f}, glm::vec3{0.f}})}; CHECK(animation::sample_vector(cubic, 0.5f).x == doctest::Approx(0.5f)); CHECK(animation::sample_vector(cubic, 0.25f).x == doctest::Approx(0.15625f)); CHECK(animation::sample_vector(cubic, 1.f).x == doctest::Approx(1.f)); // a tangent of 1 a second over a second from 0 to 1 is the straight line auto const straight{vector_track(animation::curve::cubic, {0.f, 1.f}, {glm::vec3{1.f}, glm::vec3{0.f}, glm::vec3{1.f}, glm::vec3{1.f}, glm::vec3{1.f}, glm::vec3{1.f}})}; CHECK(animation::sample_vector(straight, 0.3f).x == doctest::Approx(0.3f)); } TEST_CASE("clip: rotations by slerp, the time looped or held") { animation::track track; track.target = animation::part::rotation; track.times = {0.f, 1.f}; track.rotations = {about_x(0.f), about_x(90.f)}; CHECK(same_rotation(animation::sample_rotation(track, 0.5f), about_x(45.f))); CHECK(same_rotation(animation::sample_rotation(track, 2.f), about_x(90.f))); animation::clip clip; clip.duration = 2.f; clip.loop = true; CHECK(animation::clip_time(clip, 5.5) == doctest::Approx(1.5f)); clip.loop = false; CHECK(animation::clip_time(clip, 5.5) == doctest::Approx(2.f)); CHECK(animation::clip_time(clip, -1.0) == doctest::Approx(0.f)); } TEST_CASE("clip: the parts of a node without a track keep its own TRS") { animation::clip clip; clip.tracks.push_back(vector_track(animation::curve::linear, {0.f, 1.f}, {glm::vec3{0.f}, glm::vec3{0.f, 0.f, 4.f}})); animation::node_tracks node; node.node = 0; node.translation = 0; node.rest_translation = {9.f, 9.f, 9.f}; node.rest_rotation = about_x(90.f); auto const local{animation::local_transform(clip, node, 0.5f)}; // the translation from the track, the rotation of the node CHECK(local[3].z == doctest::Approx(2.f)); CHECK(local[3].x == doctest::Approx(0.f)); auto const up{glm::vec3{local * glm::vec4{0.f, 1.f, 0.f, 0.f}}}; CHECK(up.z == doctest::Approx(1.f)); } TEST_CASE("clip: the clips go through the file and its checks") { auto const model{clip_model()}; CHECK(eu07m::validate(model, 16).empty()); eu07m::model read; REQUIRE(eu07m::read(eu07m::write(model, {true, 9}), read, {true, true}).ok); REQUIRE(read.clips.size() == 1); CHECK(read.clips[0].flags == (eu07m::clip_flags::loop | eu07m::clip_flags::autoplay)); CHECK(read.clip_tracks.size() == 1); CHECK(read.key_times.size() == 3); CHECK(read.key_rotations.size() == 3); auto broken{model}; broken.clip_tracks[0].interpolation = eu07m::track_interpolation::cubic; CHECK_FALSE(eu07m::validate(broken, 16).empty()); broken = model; broken.key_times = {0.f, 2.f, 1.f}; CHECK_FALSE(eu07m::validate(broken, 16).empty()); broken = model; broken.clip_tracks[0].node = 5; CHECK_FALSE(eu07m::validate(broken, 16).empty()); broken = model; broken.clip_tracks[0].key_count = 4; CHECK_FALSE(eu07m::validate(broken, 16).empty()); broken = model; broken.clips[0].track_count = 2; CHECK_FALSE(eu07m::validate(broken, 16).empty()); } TEST_CASE("clip: a joint turns or moves its node from the rest, within its limits") { animation::joint_binding hinge; hinge.type = animation::joint_type::hinge; hinge.axis = {2.f, 0.f, 0.f}; // made a unit vector hinge.rest = glm::translate(glm::mat4{1.f}, glm::vec3{0.f, 0.f, 2.f}); auto const turned{animation::joint_transform(hinge, std::numbers::pi_v / 2.f)}; // the rest first: the node stays at z = 2, its +z turned to -y CHECK(turned[3].z == doctest::Approx(2.f)); CHECK(glm::vec3{turned * glm::vec4{0.f, 0.f, 1.f, 0.f}}.y == doctest::Approx(-1.f)); hinge.limited = true; hinge.minimum = -0.5f; hinge.maximum = 0.5f; auto const limited{animation::joint_transform(hinge, 3.f)}; CHECK(glm::vec3{limited * glm::vec4{0.f, 0.f, 1.f, 0.f}}.y == doctest::Approx(-std::sin(0.5f))); animation::joint_binding slider; slider.type = animation::joint_type::slider; slider.axis = {0.f, 1.f, 0.f}; CHECK(animation::joint_transform(slider, 0.25f)[3].y == doctest::Approx(0.25f)); // a screw of 0.012 m a turn moves 0.006 m in half a turn animation::joint_binding screw; screw.type = animation::joint_type::screw; screw.axis = {0.f, 0.f, 1.f}; screw.pitch = 0.012f; CHECK(animation::joint_transform(screw, std::numbers::pi_v)[3].z == doctest::Approx(0.006f)); animation::joint_binding fixed; fixed.type = animation::joint_type::fixed; fixed.rest = hinge.rest; CHECK(animation::joint_transform(fixed, 1.f)[3].z == doctest::Approx(2.f)); } TEST_CASE("clip: a channel keeps its value within its range") { animation::channel channel; channel.minimum = 1.f; channel.maximum = -1.f; // the other way round, still the range CHECK(animation::channel_value(channel, 5.f) == doctest::Approx(1.f)); CHECK(animation::channel_value(channel, -5.f) == doctest::Approx(-1.f)); CHECK(animation::channel_value(channel, 0.25f) == doctest::Approx(0.25f)); } TEST_CASE("clip: a channel names a clip by its index and a joint by its component") { auto model{clip_model()}; auto const joint{model.add_component(eu07m::component::joint, eu07m::target_kind::node, 0, eu07m::payload_writer{}.put(eu07m::payload::joint{}).data())}; eu07m::payload::channel channel; channel.name = model.intern("swing"); channel.binding_count = 2; eu07m::payload::channel_binding toclip; toclip.kind = eu07m::payload::binding_kind::clip; toclip.target = 0; eu07m::payload::channel_binding tojoint; tojoint.target = joint; auto const component{model.add_component(eu07m::component::channel, eu07m::target_kind::model, 0, eu07m::payload_writer{}.put(channel).put(toclip).put(tojoint).data())}; CHECK(eu07m::validate(model, 16).empty()); // a clip past the clips of the model, a joint which isn't a JOIN auto broken{model}; toclip.target = 7; broken.component_data.clear(); broken.components.clear(); broken.add_component(eu07m::component::joint, eu07m::target_kind::node, 0, eu07m::payload_writer{}.put(eu07m::payload::joint{}).data()); broken.add_component(eu07m::component::channel, eu07m::target_kind::model, 0, eu07m::payload_writer{}.put(channel).put(toclip).put(tojoint).data()); CHECK_FALSE(eu07m::validate(broken, 16).empty()); broken = model; toclip.target = 0; tojoint.target = component; // the channel itself broken.component_data.clear(); broken.components.clear(); broken.add_component(eu07m::component::joint, eu07m::target_kind::node, 0, eu07m::payload_writer{}.put(eu07m::payload::joint{}).data()); broken.add_component(eu07m::component::channel, eu07m::target_kind::model, 0, eu07m::payload_writer{}.put(channel).put(toclip).put(tojoint).data()); CHECK_FALSE(eu07m::validate(broken, 16).empty()); }