|
| 1 | +// The installed ACL wrapper: a chain of five joints turning and moving over two seconds at 30 samples a second, |
| 2 | +// compressed with ACL's default settings twice to the same bytes, its error measured against the raw clip, and |
| 3 | +// played back at five times. The compressed bytes and the poses are pinned, as the wrapper's CI pins them on every |
| 4 | +// platform; the adapted constants. |
| 5 | +import std; |
| 6 | +import mcpplibs.mwrap.acl; |
| 7 | + |
| 8 | +namespace { |
| 9 | + |
| 10 | +int failures = 0; |
| 11 | +void check(bool held) { failures += held ? 0 : 1; } |
| 12 | + |
| 13 | +constexpr std::uint32_t joints = 5, samples = 61; |
| 14 | +constexpr float rate = 30.0f; |
| 15 | + |
| 16 | +std::uint64_t fnv(const void* data, std::size_t size, std::uint64_t hash = 0xcbf29ce484222325ull) { |
| 17 | + const auto* bytes = static_cast<const unsigned char*>(data); |
| 18 | + for (std::size_t index = 0; index < size; ++index) hash = (hash ^ bytes[index]) * 0x100000001b3ull; |
| 19 | + return hash; |
| 20 | +} |
| 21 | + |
| 22 | +// Each joint's rotation a normalised quaternion of polynomials in time, its translation along its parent's x. |
| 23 | +acl::track_array_qvvf raw(acl::iallocator& allocator) { |
| 24 | + acl::track_array_qvvf tracks(allocator, joints); |
| 25 | + for (std::uint32_t joint = 0; joint < joints; ++joint) { |
| 26 | + acl::track_desc_transformf desc; |
| 27 | + desc.output_index = joint; |
| 28 | + desc.parent_index = joint == 0 ? mcpplibs::mwrap::acl::k_invalid_track_index : joint - 1; |
| 29 | + desc.precision = 0.0001f; |
| 30 | + desc.shell_distance = 0.03f; |
| 31 | + acl::track_qvvf track = acl::track_qvvf::make_reserve(desc, allocator, samples, rate); |
| 32 | + for (std::uint32_t sample = 0; sample < samples; ++sample) { |
| 33 | + const float time = static_cast<float>(sample) / rate, turn = 0.3f * time - 0.1f * time * time + 0.05f * static_cast<float>(joint); |
| 34 | + const rtm::quatf rotation = rtm::quat_normalize(rtm::quat_set(turn, 0.2f * turn * turn, 0.1f * static_cast<float>(joint), 1.0f)); |
| 35 | + const rtm::vector4f translation = rtm::vector_set(joint == 0 ? 0.5f * time : 0.25f, joint == 0 ? 0.1f * time * time : 0.0f, 0.0f); |
| 36 | + track[sample] = rtm::qvv_set(rotation, translation, rtm::vector_set(1.0f)); |
| 37 | + } |
| 38 | + tracks[joint] = std::move(track); |
| 39 | + } |
| 40 | + return tracks; |
| 41 | +} |
| 42 | + |
| 43 | +struct Pose : acl::track_writer { |
| 44 | + std::array<float, joints * 10> values{}; |
| 45 | + void write_rotation(std::uint32_t joint, rtm::quatf_arg0 rotation) { rtm::quat_store(rotation, &values[joint * 10]); } |
| 46 | + void write_translation(std::uint32_t joint, rtm::vector4f_arg0 translation) { rtm::vector_store3(translation, &values[joint * 10 + 4]); } |
| 47 | + void write_scale(std::uint32_t joint, rtm::vector4f_arg0 scale) { rtm::vector_store3(scale, &values[joint * 10 + 7]); } |
| 48 | +}; |
| 49 | + |
| 50 | +} // namespace |
| 51 | + |
| 52 | +int main() { |
| 53 | + acl::ansi_allocator allocator; |
| 54 | + const acl::track_array_qvvf tracks = raw(allocator); |
| 55 | + acl::compression_settings settings = acl::get_default_compression_settings(); |
| 56 | + acl::qvvf_transform_error_metric metric; |
| 57 | + settings.error_metric = &metric; |
| 58 | + const auto compress = [&] { |
| 59 | + acl::output_stats stats; |
| 60 | + acl::compressed_tracks* compressed = nullptr; |
| 61 | + check(acl::compress_track_list(allocator, tracks, settings, compressed, stats).empty() && compressed); |
| 62 | + return compressed; |
| 63 | + }; |
| 64 | + acl::compressed_tracks* compressed = compress(); |
| 65 | + acl::compressed_tracks* again = compress(); |
| 66 | + if (!compressed || !again) return 1; |
| 67 | + check(compressed->is_valid(true).empty()); |
| 68 | + check(compressed->get_size() == again->get_size() && std::memcmp(compressed, again, compressed->get_size()) == 0); |
| 69 | + |
| 70 | + acl::decompression_context<acl::default_transform_decompression_settings> context; |
| 71 | + check(context.initialize(*compressed)); |
| 72 | + const acl::track_error error = acl::calculate_compression_error(allocator, tracks, context, metric); |
| 73 | + check(error.error < 0.00015f); |
| 74 | + std::uint64_t poses = 0xcbf29ce484222325ull; |
| 75 | + for (const float time : {0.0f, 0.37f, 1.0f, 1.5f, 2.0f}) { |
| 76 | + Pose pose; |
| 77 | + context.seek(time, acl::sample_rounding_policy::none); |
| 78 | + context.decompress_tracks(pose); |
| 79 | + poses = fnv(pose.values.data(), sizeof(pose.values), poses); |
| 80 | + } |
| 81 | + const std::uint64_t bytes = fnv(compressed, compressed->get_size()); |
| 82 | + check(bytes == 0x7bbb0daa9b0e5fa6ull && poses == 0x0c6e1a02a38a29a5ull); |
| 83 | + check(mcpplibs::mwrap::acl::k_invalid_track_index == 0xFFFFFFFFu && mcpplibs::mwrap::acl::k_num_quality_tiers == 3u); |
| 84 | + std::println("ACL 2.1.0 through import mcpplibs.mwrap.acl: {} raw bytes compressed to {} within {:.3f} mm; the bytes {:#018x}, the poses {:#018x}{}", |
| 85 | + joints * samples * 10 * sizeof(float), compressed->get_size(), error.error * 1000.0f, bytes, poses, |
| 86 | + failures ? std::format(" — {} checks failed", failures) : ""); |
| 87 | + allocator.deallocate(compressed, compressed->get_size()); |
| 88 | + allocator.deallocate(again, again->get_size()); |
| 89 | + return failures; |
| 90 | +} |
0 commit comments