Cleanup: use stubs for eigen gtest
[blender-staging.git] / tests / gtests / blenlib / BLI_kdopbvh_test.cc
1 /* Apache License, Version 2.0 */
2
3 #include "testing/testing.h"
4
5 /* TODO: ray intersection, overlap ... etc.*/
6
7 extern "C" {
8 #include "BLI_compiler_attrs.h"
9 #include "BLI_kdopbvh.h"
10 #include "BLI_rand.h"
11 #include "BLI_math_vector.h"
12 #include "MEM_guardedalloc.h"
13 }
14
15 #include "stubs/bf_intern_eigen_stubs.h"
16
17 /* -------------------------------------------------------------------- */
18 /* Helper Functions */
19
20 static void rng_v3_round(
21         float *coords, int coords_len,
22         struct RNG *rng, int round, float scale)
23 {
24         for (int i = 0; i < coords_len; i++) {
25                 float f = BLI_rng_get_float(rng) * 2.0f - 1.0f;
26                 coords[i] = ((float)((int)(f * round)) / (float)round) * scale;
27         }
28 }
29
30 /* -------------------------------------------------------------------- */
31 /* Tests */
32
33 TEST(kdopbvh, Empty)
34 {
35         BVHTree *tree = BLI_bvhtree_new(0, 0.0, 8, 8);
36         BLI_bvhtree_balance(tree);
37         EXPECT_EQ(0, BLI_bvhtree_get_size(tree));
38         BLI_bvhtree_free(tree);
39 }
40
41 TEST(kdopbvh, Single)
42 {
43         BVHTree *tree = BLI_bvhtree_new(1, 0.0, 8, 8);
44         {
45                 float co[3] = {0};
46                 BLI_bvhtree_insert(tree, 0, co, 1);
47         }
48
49         EXPECT_EQ(BLI_bvhtree_get_size(tree), 1);
50
51         BLI_bvhtree_balance(tree);
52         BLI_bvhtree_free(tree);
53 }
54
55 /**
56  * Note that a small epsilon is added to the BVH nodes bounds, even if we pass in zero.
57  * Use rounding to ensure very close nodes don't cause the wrong node to be found as nearest.
58  */
59 static void find_nearest_points_test(int points_len, float scale, int round, int random_seed)
60 {
61         struct RNG *rng = BLI_rng_new(random_seed);
62         BVHTree *tree = BLI_bvhtree_new(points_len, 0.0, 8, 8);
63
64         void *mem = MEM_mallocN(sizeof(float[3]) * points_len, __func__);
65         float (*points)[3] = (float (*)[3])mem;
66
67         for (int i = 0; i < points_len; i++) {
68                 rng_v3_round(points[i], 3, rng, round, scale);
69                 BLI_bvhtree_insert(tree, i, points[i], 1);
70         }
71         BLI_bvhtree_balance(tree);
72         /* first find each point */
73         for (int i = 0; i < points_len; i++) {
74                 const int j = BLI_bvhtree_find_nearest(tree, points[i], NULL, NULL, NULL);
75                 if (j != i) {
76 #if 0
77                         const float dist = len_v3v3(points[i], points[j]);
78                         if (dist > (1.0f / (float)round)) {
79                                 printf("%.15f (%d %d)\n", dist, i, j);
80                                 print_v3_id(points[i]);
81                                 print_v3_id(points[j]);
82                                 fflush(stdout);
83                         }
84 #endif
85                         EXPECT_GE(j, 0);
86                         EXPECT_LT(j, points_len);
87                         EXPECT_EQ_ARRAY(points[i], points[j], 3);
88                 }
89         }
90         BLI_bvhtree_free(tree);
91         BLI_rng_free(rng);
92         MEM_freeN(points);
93 }
94
95 TEST(kdopbvh, FindNearest_1)            { find_nearest_points_test(1, 1.0, 1000, 1234); }
96 TEST(kdopbvh, FindNearest_2)            { find_nearest_points_test(2, 1.0, 1000, 123); }
97 TEST(kdopbvh, FindNearest_500)          { find_nearest_points_test(500, 1.0, 1000, 12); }