Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BKE_mesh_sample_test.cc « blenkernel « gtests « tests - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 34c0b82342fdc401c531a58029d85358c80e6b12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* Apache License, Version 2.0 */

#include <iostream>
#include <fstream>
#include <sstream>

#include "testing/testing.h"

#include "BKE_mesh_test_util.h"

extern "C" {
#include "MEM_guardedalloc.h"

#include "BLI_math.h"
#include "BLI_path_util.h"
#include "BLI_rand.h"

#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"

#include "BKE_appdir.h"
#include "BKE_cdderivedmesh.h"
#include "BKE_DerivedMesh.h"
#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_mesh.h"
#include "BKE_mesh_sample.h"
}

#define TEST_MESH_OUTPUT_FILE "mesh_dump_"

static const float verts[][3] = { {-1, -1, -1}, {1, -1, -1}, {-1, 1, -1}, {1, 1, -1},
                                  {-1, -1, 1},  {1, -1, 1},  {-1, 1, 1},  {1, 1, 1} };
static const int faces[] = { 0, 1, 3, 2,
                             4, 5, 7, 6,
                             0, 1, 5, 4,
                             2, 3, 7, 6,
                             0, 2, 6, 4,
                             1, 3, 7, 5,
                           };
static const int face_lengths[] = { 4, 4, 4, 4, 4, 4 };

class MeshSampleTest : public ::testing::Test
{
public:
	static const unsigned int m_seed;
	static const int m_numsamples;
	
	std::string get_testname() const;
	
	void load_mesh(const float (*verts)[3], int numverts,
	               const int (*edges)[2], int numedges,
	               const int *faces, const int *face_lengths, int numfaces);
	void load_mesh(const char *filename);
	void unload_mesh();
	
	void generate_samples_simple(struct MeshSampleGenerator *gen);
	void generate_samples_batch(struct MeshSampleGenerator *gen);
	void generate_samples_batch_threaded(struct MeshSampleGenerator *gen);
	void compare_samples(const struct MeshSample *ground_truth);
	void test_samples(struct MeshSampleGenerator *gen, const struct MeshSample *ground_truth, int count);
	
	void dump_samples();
	
protected:
	void SetUp() override;
	void TearDown() override;
	
protected:
	Mesh *m_mesh;
	DerivedMesh *m_dm;
	
	MeshSample *m_samples;
};

const unsigned int MeshSampleTest::m_seed = 8343;
const int MeshSampleTest::m_numsamples = 100000;

std::string MeshSampleTest::get_testname() const
{
	std::stringstream testname;
	testname << ::testing::UnitTest::GetInstance()->current_test_info()->name();
	return testname.str();
}

void MeshSampleTest::load_mesh(const float (*verts)[3], int numverts,
                               const int (*edges)[2], int numedges,
                               const int *faces, const int *face_lengths, int numfaces)
{
	m_mesh = BKE_mesh_test_from_data(verts, numverts, edges, numedges, faces, face_lengths, numfaces);
	m_dm = CDDM_from_mesh(m_mesh);
}

void MeshSampleTest::load_mesh(const char *filename)
{
	const char *folder = BKE_appdir_folder_id(BLENDER_DATAFILES, "tests");
	char path[FILE_MAX];

	BLI_make_file_string(G.main->name, path, folder, filename);

	if (path[0]) {
		m_mesh = BKE_mesh_test_from_csv(path);
		m_dm = CDDM_from_mesh(m_mesh);
	}
}

void MeshSampleTest::unload_mesh()
{
	if (m_dm) {
		m_dm->release(m_dm);
		m_dm = NULL;
	}
	if (m_mesh) {
		BKE_mesh_free(m_mesh);
		MEM_freeN(m_mesh);
		m_mesh = NULL;
	}
}

void MeshSampleTest::SetUp()
{
	load_mesh("suzanne.csv");
	if (!m_dm) {
		int numverts = ARRAY_SIZE(verts);
		int numfaces = ARRAY_SIZE(face_lengths);
		load_mesh(verts, numverts, NULL, 0, faces, face_lengths, numfaces);
	}
	
	m_samples = (MeshSample *)MEM_mallocN(sizeof(MeshSample) * m_numsamples, "mesh samples");
}

void MeshSampleTest::TearDown()
{
	if (m_samples) {
		MEM_freeN(m_samples);
		m_samples = NULL;
	}
	
	unload_mesh();
}


void MeshSampleTest::dump_samples()
{
#ifdef TEST_MESH_OUTPUT_FILE
	int numverts = m_mesh->totvert;
	
	int dbg_numverts = numverts + m_numsamples;
	float (*dbg_verts)[3] = (float (*)[3])MEM_mallocN(sizeof(float[3]) * dbg_numverts, "vertices");
	for (int i = 0; i < numverts; ++i) {
		copy_v3_v3(dbg_verts[i], m_mesh->mvert[i].co);
	}
	for (int i = 0; i < m_numsamples; ++i) {
		float nor[3], tang[3];
		BKE_mesh_sample_eval(m_dm, &m_samples[i], dbg_verts[numverts + i], nor, tang);
	}
	int *dbg_faces = (int *)MEM_mallocN(sizeof(int) * m_mesh->totloop, "faces");
	int *dbg_face_lengths = (int *)MEM_mallocN(sizeof(int) * m_mesh->totpoly, "face_lengths");
	int loopstart = 0;
	for (int i = 0; i < m_mesh->totpoly; ++i) {
		const MPoly *mp = &m_mesh->mpoly[i];
		dbg_face_lengths[i] = mp->totloop;
		for (int k = 0; k < mp->totloop; ++k) {
			dbg_faces[loopstart + k] = m_mesh->mloop[mp->loopstart + k].v;
		}
		loopstart += mp->totloop;
	}
	Mesh *dbg_mesh = BKE_mesh_test_from_data(dbg_verts, dbg_numverts, NULL, 0, dbg_faces, dbg_face_lengths, m_mesh->totpoly);
	MEM_freeN(dbg_verts);
	MEM_freeN(dbg_faces);
	MEM_freeN(dbg_face_lengths);
	
	std::stringstream filename;
	filename << TEST_MESH_OUTPUT_FILE << get_testname() << ".py";
	std::fstream s(filename.str(), s.trunc | s.out);
	
	BKE_mesh_test_dump_mesh(dbg_mesh, get_testname().c_str(), s);
	
	BKE_mesh_free(dbg_mesh);
	MEM_freeN(dbg_mesh);
#endif
}

void MeshSampleTest::compare_samples(const MeshSample *ground_truth)
{
	for (int i = 0; i < m_numsamples; ++i) {
		EXPECT_EQ(ground_truth[i].orig_verts[0], m_samples[i].orig_verts[0]);
		EXPECT_EQ(ground_truth[i].orig_verts[1], m_samples[i].orig_verts[1]);
		EXPECT_EQ(ground_truth[i].orig_verts[2], m_samples[i].orig_verts[2]);
		
		EXPECT_EQ(ground_truth[i].orig_weights[0], m_samples[i].orig_weights[0]);
		EXPECT_EQ(ground_truth[i].orig_weights[1], m_samples[i].orig_weights[1]);
		EXPECT_EQ(ground_truth[i].orig_weights[2], m_samples[i].orig_weights[2]);
	}
}

void MeshSampleTest::generate_samples_simple(MeshSampleGenerator *gen)
{
	for (int i = 0; i < m_numsamples; ++i) {
		BKE_mesh_sample_generate(gen, &m_samples[i]);
	}
}

void MeshSampleTest::generate_samples_batch(MeshSampleGenerator *gen)
{
	BKE_mesh_sample_generate_batch_ex(gen, m_samples, sizeof(MeshSample), m_numsamples, false);
}

void MeshSampleTest::generate_samples_batch_threaded(MeshSampleGenerator *gen)
{
	BKE_mesh_sample_generate_batch_ex(gen, m_samples, sizeof(MeshSample), m_numsamples, true);
}

void MeshSampleTest::test_samples(MeshSampleGenerator *gen, const MeshSample *ground_truth, int count)
{
	BKE_mesh_sample_generator_bind(gen, m_dm);
	
	if (ground_truth) {
		EXPECT_EQ(count, m_numsamples) << "Ground truth size does not match number of samples";
		if (count != m_numsamples) {
			return;
		}
		
		generate_samples_simple(gen);
		compare_samples(ground_truth);
	}
	else {
		generate_samples_simple(gen);
		// Use simple sample generation as ground truth if not provided explicitly
		ground_truth = m_samples;
	}
	
	generate_samples_batch(gen);
	compare_samples(ground_truth);
	
	generate_samples_batch_threaded(gen);
	compare_samples(ground_truth);
	
	BKE_mesh_sample_generator_unbind(gen);
}

TEST_F(MeshSampleTest, SurfaceVertices)
{
	MeshSampleGenerator *gen = BKE_mesh_sample_gen_surface_vertices();
	ASSERT_TRUE(gen != NULL) << "No generator created";
	
	test_samples(gen, NULL, 0);
	dump_samples();
	
	BKE_mesh_sample_free_generator(gen);
}

TEST_F(MeshSampleTest, SurfaceRandom)
{
	MeshSampleGenerator *gen = BKE_mesh_sample_gen_surface_random(m_seed, true, NULL, NULL);
	ASSERT_TRUE(gen != NULL) << "No generator created";
	
	test_samples(gen, NULL, 0);
	dump_samples();
	
	BKE_mesh_sample_free_generator(gen);
}

const float poisson_disk_mindist = 0.01f;

TEST_F(MeshSampleTest, SurfacePoissonDisk)
{
	MeshSampleGenerator *gen = BKE_mesh_sample_gen_surface_poissondisk(m_seed, poisson_disk_mindist, 10000000, NULL, NULL);
	ASSERT_TRUE(gen != NULL) << "No generator created";
	
	test_samples(gen, NULL, 0);
	dump_samples();
	
	BKE_mesh_sample_free_generator(gen);
}

extern "C" {

static const unsigned int raycast_seed = 85344;
static const float raycast_radius = 100.0f;

static void* raycast_thread_context_create(void *UNUSED(userdata), int start)
{
	RNG *rng = BLI_rng_new(raycast_seed);
	BLI_rng_skip(rng, start * 2);
	return rng;
}

static void raycast_thread_context_free(void *UNUSED(userdata), void *thread_ctx)
{
	BLI_rng_free((RNG *)thread_ctx);
}

static bool raycast_ray(void *UNUSED(userdata), void *thread_ctx, float ray_start[3], float ray_end[3])
{
	RNG *rng = (RNG *)thread_ctx;
	float v[3];
	{
		float r;
		v[2] = (2.0f * BLI_rng_get_float(rng)) - 1.0f;
		float a = (float)(M_PI * 2.0) * BLI_rng_get_float(rng);
		if ((r = 1.0f - (v[2] * v[2])) > 0.0f) {
			r = sqrtf(r);
			v[0] = r * cosf(a);
			v[1] = r * sinf(a);
		}
		else {
			v[2] = 1.0f;
		}
	}
	
	mul_v3_fl(v, raycast_radius);
	copy_v3_v3(ray_start, v);
	negate_v3_v3(ray_end, v);
	return true;
}

} /*extern "C"*/

TEST_F(MeshSampleTest, SurfaceRaycast)
{
	MeshSampleGenerator *gen = BKE_mesh_sample_gen_surface_raycast(
	                               raycast_thread_context_create, raycast_thread_context_free, raycast_ray, NULL);
	ASSERT_TRUE(gen != NULL) << "No generator created";
	
	test_samples(gen, NULL, 0);
	dump_samples();
	
	BKE_mesh_sample_free_generator(gen);
}

static const float volume_bbray_density = 0.1f;

TEST_F(MeshSampleTest, VolumeBBRay)
{
	MeshSampleGenerator *gen = BKE_mesh_sample_gen_volume_random_bbray(m_seed, volume_bbray_density);
	ASSERT_TRUE(gen != NULL) << "No generator created";
	
	test_samples(gen, NULL, 0);
	dump_samples();
	
	BKE_mesh_sample_free_generator(gen);
}