Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/tests/BLI_length_parameterize_test.cc
- This file was added.
| /* SPDX-License-Identifier: Apache-2.0 */ | |||||
| #include "BLI_array.hh" | |||||
| #include "BLI_length_parameterize.hh" | |||||
| #include "BLI_vector.hh" | |||||
| #include "testing/testing.h" | |||||
| namespace blender::length_parameterize::tests { | |||||
| template<typename T> Array<float> calculate_lengths(const Span<T> values, const bool cyclic) | |||||
| { | |||||
| Array<float> lengths(lengths_num(values.size(), cyclic)); | |||||
| accumulate_lengths<T>(values, cyclic, lengths); | |||||
| return lengths; | |||||
| } | |||||
| TEST(length_parameterize, FloatSimple) | |||||
| { | |||||
| Array<float> values{{0, 1, 4}}; | |||||
| Array<float> lengths = calculate_lengths(values.as_span(), false); | |||||
| ResultSamples samples; | |||||
| create_uniform_samples(lengths, false, 4, samples); | |||||
| Array<float> results(4); | |||||
| samples.interpolate_linear<float>(values, results); | |||||
| Array<float> expected({ | |||||
| 0.0f, | |||||
| 1.33333f, | |||||
| 2.66667f, | |||||
| 4.0f, | |||||
| }); | |||||
| for (const int i : results.index_range()) { | |||||
| EXPECT_NEAR(results[i], expected[i], 1e-5); | |||||
| } | |||||
| } | |||||
| TEST(length_parameterize, Float) | |||||
| { | |||||
| Array<float> values{{1, 2, 3, 5, 10}}; | |||||
| Array<float> lengths = calculate_lengths(values.as_span(), false); | |||||
| ResultSamples samples; | |||||
| create_uniform_samples(lengths, false, 20, samples); | |||||
| Array<float> results(20); | |||||
| samples.interpolate_linear<float>(values, results); | |||||
| Array<float> expected({ | |||||
| 1.0f, 1.47368f, 1.94737f, 2.42105f, 2.89474f, 3.36842f, 3.84211f, | |||||
| 4.31579f, 4.78947f, 5.26316f, 5.73684f, 6.21053f, 6.68421f, 7.1579f, | |||||
| 7.63158f, 8.10526f, 8.57895f, 9.05263f, 9.52632f, 10.0f, | |||||
| }); | |||||
| for (const int i : results.index_range()) { | |||||
| EXPECT_NEAR(results[i], expected[i], 1e-5); | |||||
| } | |||||
| } | |||||
| TEST(length_parameterize, Float2) | |||||
| { | |||||
| Array<float2> values{{{0, 0}, {1, 0}, {1, 1}, {0, 1}}}; | |||||
| Array<float> lengths = calculate_lengths(values.as_span(), false); | |||||
JacquesLucke: Would maybe be good to have an example where all segments have the same length. This doesn't… | |||||
Done Inline ActionsGood idea! It's implicitly tested in the "expected" values, but I added an explicit check for uniform lengths to a couple of them too. (the distances between consecutive sampled points in Cartesian space aren't necessarily uniform in the general case). HooglyBoogly: Good idea! It's implicitly tested in the "expected" values, but I added an explicit check for… | |||||
| ResultSamples samples; | |||||
| create_uniform_samples(lengths, false, 12, samples); | |||||
| Array<float2> results(12); | |||||
| samples.interpolate_linear<float2>(values, results); | |||||
| Array<float2> expected({ | |||||
| {0.0f, 0.0f}, | |||||
| {0.272727f, 0.0f}, | |||||
| {0.545455f, 0.0f}, | |||||
| {0.818182f, 0.0f}, | |||||
| {1.0f, 0.0909091f}, | |||||
| {1.0f, 0.363636f}, | |||||
| {1.0f, 0.636364f}, | |||||
| {1.0f, 0.909091f}, | |||||
| {0.818182f, 1.0f}, | |||||
| {0.545455f, 1.0f}, | |||||
| {0.272727f, 1.0f}, | |||||
| {0.0f, 1.0f}, | |||||
| }); | |||||
| for (const int i : results.index_range()) { | |||||
| EXPECT_NEAR(results[i].x, expected[i].x, 1e-5); | |||||
| EXPECT_NEAR(results[i].y, expected[i].y, 1e-5); | |||||
| } | |||||
| } | |||||
| TEST(length_parameterize, Float2Cyclic) | |||||
| { | |||||
| Array<float2> values{{{0, 0}, {1, 0}, {1, 1}, {0, 1}}}; | |||||
| Array<float> lengths = calculate_lengths(values.as_span(), true); | |||||
| ResultSamples samples; | |||||
| create_uniform_samples(lengths, true, 12, samples); | |||||
| Array<float2> results(12); | |||||
| samples.interpolate_linear<float2>(values, results); | |||||
| Array<float2> expected({ | |||||
| {0.0f, 0.0f}, | |||||
| {0.333333f, 0.0f}, | |||||
| {0.666667f, 0.0f}, | |||||
| {1.0f, 0.0f}, | |||||
| {1.0f, 0.333333f}, | |||||
| {1.0f, 0.666667f}, | |||||
| {1.0f, 1.0f}, | |||||
| {0.666667f, 1.0f}, | |||||
| {0.333333f, 1.0f}, | |||||
| {0.0f, 1.0f}, | |||||
| {0.0f, 0.666667f}, | |||||
| {0.0f, 0.333333f}, | |||||
| }); | |||||
| for (const int i : results.index_range()) { | |||||
| EXPECT_NEAR(results[i].x, expected[i].x, 1e-5); | |||||
| EXPECT_NEAR(results[i].y, expected[i].y, 1e-5); | |||||
| } | |||||
| } | |||||
| TEST(length_parameterize, LineMany) | |||||
| { | |||||
| Array<float> values{{1, 2}}; | |||||
| Array<float> lengths = calculate_lengths(values.as_span(), false); | |||||
| ResultSamples samples; | |||||
| create_uniform_samples(lengths, false, 5007, samples); | |||||
| Array<float> results(5007); | |||||
| samples.interpolate_linear<float>(values, results); | |||||
| Array<float> expected({ | |||||
| 1.9962f, 1.9964f, 1.9966f, 1.9968f, 1.997f, 1.9972f, 1.9974f, 1.9976f, 1.9978f, 1.998f, | |||||
| 1.9982f, 1.9984f, 1.9986f, 1.9988f, 1.999f, 1.9992f, 1.9994f, 1.9996f, 1.9998f, 2.0f, | |||||
| }); | |||||
| for (const int i : expected.index_range()) { | |||||
| EXPECT_NEAR(results.as_span().take_back(20)[i], expected[i], 1e-5); | |||||
| } | |||||
| } | |||||
| TEST(length_parameterize, CyclicMany) | |||||
| { | |||||
| Array<float2> values{{{0, 0}, {1, 0}, {1, 1}, {0, 1}}}; | |||||
| Array<float> lengths = calculate_lengths(values.as_span(), true); | |||||
| ResultSamples samples; | |||||
| create_uniform_samples(lengths, true, 5007, samples); | |||||
| Array<float2> results(5007); | |||||
| samples.interpolate_linear<float2>(values, results); | |||||
| Array<float2> expected({ | |||||
| {0, 0.0159776}, {0, 0.0151787}, {0, 0.0143797}, {0, 0.013581}, {0, 0.0127821}, | |||||
| {0, 0.0119832}, {0, 0.0111842}, {0, 0.0103855}, {0, 0.00958657}, {0, 0.00878763}, | |||||
| {0, 0.00798869}, {0, 0.00718999}, {0, 0.00639105}, {0, 0.00559211}, {0, 0.00479317}, | |||||
| {0, 0.00399446}, {0, 0.00319552}, {0, 0.00239658}, {0, 0.00159764}, {0, 0.000798941}, | |||||
| }); | |||||
| for (const int i : expected.index_range()) { | |||||
| EXPECT_NEAR(results.as_span().take_back(20)[i].x, expected[i].x, 1e-5); | |||||
| EXPECT_NEAR(results.as_span().take_back(20)[i].y, expected[i].y, 1e-5); | |||||
| } | |||||
| } | |||||
| TEST(length_parameterize, InterpolateColor) | |||||
| { | |||||
| Array<float2> values{{{0, 0}, {1, 0}, {1, 1}, {0, 1}}}; | |||||
| Array<float> lengths = calculate_lengths(values.as_span(), true); | |||||
| Array<ColorGeometry4f> colors{{{0, 0, 0, 1}, {1, 0, 0, 1}, {1, 1, 0, 1}, {0, 1, 0, 1}}}; | |||||
| ResultSamples samples; | |||||
| create_uniform_samples(lengths, true, 20, samples); | |||||
| Array<ColorGeometry4f> results(20); | |||||
| samples.interpolate_linear<ColorGeometry4f>(colors, results); | |||||
| } | |||||
Done Inline ActionsMaybe test if the computed result is actually correct? JacquesLucke: Maybe test if the computed result is actually correct? | |||||
Done Inline ActionsI added that as a "does this compile" test, but I might as well! :) HooglyBoogly: I added that as a "does this compile" test, but I might as well! :) | |||||
| } // namespace blender::length_parameterize::tests | |||||
Would maybe be good to have an example where all segments have the same length. This doesn't really test if the code does actually creates uniform samples.