1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2020 by Blender Foundation.
17  */
18 #include "testing/testing.h"
19 
20 #include "BKE_idtype.h"
21 #include "BKE_lattice.h"
22 
23 #include "MEM_guardedalloc.h"
24 
25 #include "DNA_lattice_types.h"
26 #include "DNA_mesh_types.h"
27 #include "DNA_object_types.h"
28 
29 #include "BLI_rand.hh"
30 
31 namespace blender::bke::tests {
32 
33 struct LatticeDeformTestContext {
34   Lattice lattice;
35   Object ob_lattice;
36   Mesh mesh;
37   Object ob_mesh;
38   float (*coords)[3];
39   LatticeDeformData *ldd;
40 };
41 
test_lattice_deform_init(LatticeDeformTestContext * ctx,RandomNumberGenerator * rng,int32_t num_items)42 static void test_lattice_deform_init(LatticeDeformTestContext *ctx,
43                                      RandomNumberGenerator *rng,
44                                      int32_t num_items)
45 {
46   /* Generate random input data between -5 and 5. */
47   ctx->coords = (float(*)[3])MEM_malloc_arrayN(sizeof(float[3]), num_items, __func__);
48   for (uint32_t index = 0; index < num_items; index++) {
49     ctx->coords[index][0] = (rng->get_float() - 0.5f) * 10;
50     ctx->coords[index][1] = (rng->get_float() - 0.5f) * 10;
51     ctx->coords[index][2] = (rng->get_float() - 0.5f) * 10;
52   }
53   IDType_ID_LT.init_data(&ctx->lattice.id);
54   IDType_ID_OB.init_data(&ctx->ob_lattice.id);
55   ctx->ob_lattice.type = OB_LATTICE;
56   ctx->ob_lattice.data = &ctx->lattice;
57   IDType_ID_OB.init_data(&ctx->ob_mesh.id);
58   IDType_ID_ME.init_data(&ctx->mesh.id);
59   ctx->ob_mesh.type = OB_MESH;
60   ctx->ob_mesh.data = &ctx->mesh;
61 
62   ctx->ldd = BKE_lattice_deform_data_create(&ctx->ob_lattice, &ctx->ob_mesh);
63 }
64 
test_lattice_deform(LatticeDeformTestContext * ctx,int32_t num_items)65 static void test_lattice_deform(LatticeDeformTestContext *ctx, int32_t num_items)
66 {
67   for (int i = 0; i < num_items; i++) {
68     float *co = &ctx->coords[i][0];
69     BKE_lattice_deform_data_eval_co(ctx->ldd, co, 1.0f);
70   }
71 }
72 
test_lattice_deform_free(LatticeDeformTestContext * ctx)73 static void test_lattice_deform_free(LatticeDeformTestContext *ctx)
74 {
75   BKE_lattice_deform_data_destroy(ctx->ldd);
76   MEM_freeN(ctx->coords);
77   IDType_ID_LT.free_data(&ctx->lattice.id);
78   IDType_ID_OB.free_data(&ctx->ob_lattice.id);
79   IDType_ID_OB.free_data(&ctx->ob_mesh.id);
80   IDType_ID_ME.free_data(&ctx->mesh.id);
81 }
82 
TEST(lattice_deform_performance,performance_no_dvert_1)83 TEST(lattice_deform_performance, performance_no_dvert_1)
84 {
85   const int32_t num_items = 1;
86   LatticeDeformTestContext ctx = {{{0}}};
87   RandomNumberGenerator rng;
88   test_lattice_deform_init(&ctx, &rng, num_items);
89   test_lattice_deform(&ctx, num_items);
90   test_lattice_deform_free(&ctx);
91 }
TEST(lattice_deform_performance,performance_no_dvert_1000)92 TEST(lattice_deform_performance, performance_no_dvert_1000)
93 {
94   const int32_t num_items = 1000;
95   LatticeDeformTestContext ctx = {{{0}}};
96   RandomNumberGenerator rng;
97   test_lattice_deform_init(&ctx, &rng, num_items);
98   test_lattice_deform(&ctx, num_items);
99   test_lattice_deform_free(&ctx);
100 }
TEST(lattice_deform_performance,performance_no_dvert_10000)101 TEST(lattice_deform_performance, performance_no_dvert_10000)
102 {
103   const int32_t num_items = 10000;
104   LatticeDeformTestContext ctx = {{{0}}};
105   RandomNumberGenerator rng;
106   test_lattice_deform_init(&ctx, &rng, num_items);
107   test_lattice_deform(&ctx, num_items);
108   test_lattice_deform_free(&ctx);
109 }
TEST(lattice_deform_performance,performance_no_dvert_100000)110 TEST(lattice_deform_performance, performance_no_dvert_100000)
111 {
112   const int32_t num_items = 100000;
113   LatticeDeformTestContext ctx = {{{0}}};
114   RandomNumberGenerator rng;
115   test_lattice_deform_init(&ctx, &rng, num_items);
116   test_lattice_deform(&ctx, num_items);
117   test_lattice_deform_free(&ctx);
118 }
TEST(lattice_deform_performance,performance_no_dvert_1000000)119 TEST(lattice_deform_performance, performance_no_dvert_1000000)
120 {
121   const int32_t num_items = 1000000;
122   LatticeDeformTestContext ctx = {{{0}}};
123   RandomNumberGenerator rng;
124   test_lattice_deform_init(&ctx, &rng, num_items);
125   test_lattice_deform(&ctx, num_items);
126   test_lattice_deform_free(&ctx);
127 }
TEST(lattice_deform_performance,performance_no_dvert_10000000)128 TEST(lattice_deform_performance, performance_no_dvert_10000000)
129 {
130   const int32_t num_items = 10000000;
131   LatticeDeformTestContext ctx = {{{0}}};
132   RandomNumberGenerator rng;
133   test_lattice_deform_init(&ctx, &rng, num_items);
134   test_lattice_deform(&ctx, num_items);
135   test_lattice_deform_free(&ctx);
136 }
137 
138 }  // namespace blender::bke::tests
139