[Bf-blender-cvs] [9e8a41aadf2] strand_editmode: Include other sample generators in tests.

Lukas Tönne noreply at git.blender.org
Wed Aug 23 10:08:36 CEST 2017


Commit: 9e8a41aadf2df9bdfd6d7847974ac36eb9fd5113
Author: Lukas Tönne
Date:   Wed Aug 23 09:07:45 2017 +0100
Branches: strand_editmode
https://developer.blender.org/rB9e8a41aadf2df9bdfd6d7847974ac36eb9fd5113

Include other sample generators in tests.

Volume bbray method is currently excluded because the implementation
is not yet thread-safe.

===================================================================

M	source/blender/blenkernel/intern/mesh_sample.c
M	tests/gtests/blenkernel/BKE_mesh_sample_test.cc

===================================================================

diff --git a/source/blender/blenkernel/intern/mesh_sample.c b/source/blender/blenkernel/intern/mesh_sample.c
index 09b3adcbc84..4f403ecbf0c 100644
--- a/source/blender/blenkernel/intern/mesh_sample.c
+++ b/source/blender/blenkernel/intern/mesh_sample.c
@@ -236,36 +236,40 @@ static void generator_vertices_thread_context_free(const MSurfaceSampleGenerator
 	MEM_freeN(thread_ctx);
 }
 
+static bool generator_vertices_make_loop_sample(DerivedMesh *dm, const int *loops, MeshSample *sample)
+{
+	const MLoop *mloops = dm->getLoopArray(dm);
+	
+	if (loops[0] < 0) {
+		return false;
+	}
+	
+	sample->orig_poly = -1;
+	
+	sample->orig_loops[0] = (unsigned int)loops[0];
+	sample->orig_loops[1] = (unsigned int)loops[1];
+	sample->orig_loops[2] = (unsigned int)loops[2];
+	
+	sample->orig_verts[0] = mloops[loops[0]].v;
+	sample->orig_verts[1] = mloops[loops[1]].v;
+	sample->orig_verts[2] = mloops[loops[2]].v;
+	
+	sample->orig_weights[0] = 1.0f;
+	sample->orig_weights[1] = 0.0f;
+	sample->orig_weights[2] = 0.0f;
+	
+	return true;
+}
+
 static bool generator_vertices_make_sample(const MSurfaceSampleGenerator_Vertices *gen, void *thread_ctx, MeshSample *sample)
 {
 	DerivedMesh *dm = gen->dm;
 	const int num_verts = dm->getNumVerts(dm);
-	const MLoop *mloops = dm->getLoopArray(dm);
 	
 	int cur_vert = *(int *)thread_ctx;
 	bool found_vert = false;
-	while (cur_vert < num_verts) {
-		++cur_vert;
-		
-		const int *loops = gen->vert_loop_map[cur_vert];
-		if (loops[0] >= 0) {
-			sample->orig_poly = -1;
-			
-			sample->orig_loops[0] = (unsigned int)loops[0];
-			sample->orig_loops[1] = (unsigned int)loops[1];
-			sample->orig_loops[2] = (unsigned int)loops[2];
-			
-			sample->orig_verts[0] = mloops[loops[0]].v;
-			sample->orig_verts[1] = mloops[loops[1]].v;
-			sample->orig_verts[2] = mloops[loops[2]].v;
-			
-			sample->orig_weights[0] = 1.0f;
-			sample->orig_weights[1] = 0.0f;
-			sample->orig_weights[2] = 0.0f;
-
-			found_vert = true;
-			break;
-		}
+	for (; cur_vert < num_verts && !found_vert; ++cur_vert) {
+		found_vert |= generator_vertices_make_loop_sample(dm, gen->vert_loop_map[cur_vert], sample);
 	}
 	
 	*(int *)thread_ctx = cur_vert;
@@ -720,6 +724,7 @@ static void generator_volume_random_cast_ray(MVolumeSampleGenerator_Random *gen,
 	madd_v3_v3v3v3(wray.end, gen->min, ray->end, gen->extent);
 	
 	sub_v3_v3v3(dir, wray.end, wray.start);
+	normalize_v3(dir);
 	
 	gen->tothits = 0;
 	BLI_bvhtree_ray_cast_all(gen->bvhdata.tree, wray.start, dir, 0.0f, BVH_RAYCAST_DIST_MAX,
diff --git a/tests/gtests/blenkernel/BKE_mesh_sample_test.cc b/tests/gtests/blenkernel/BKE_mesh_sample_test.cc
index 786e1badde3..51998473a40 100644
--- a/tests/gtests/blenkernel/BKE_mesh_sample_test.cc
+++ b/tests/gtests/blenkernel/BKE_mesh_sample_test.cc
@@ -11,6 +11,9 @@
 extern "C" {
 #include "MEM_guardedalloc.h"
 
+#include "BLI_math.h"
+#include "BLI_rand.h"
+
 #include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
 
@@ -31,113 +34,275 @@ static const int faces[] = { 0, 1, 3, 2,
                              0, 2, 6, 4,
                              1, 3, 7, 5,
                            };
-static const int face_sizes[] = { 4, 4, 4, 4, 4, 4 };
+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 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::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()
+{
+	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();
+}
+
 
-static void dump_samples(const char *testname, const Mesh *mesh, const MeshSample *samples, int numsamples)
+void MeshSampleTest::dump_samples()
 {
 #ifdef TEST_MESH_OUTPUT_FILE
-	int numverts = mesh->totvert;
+	int numverts = m_mesh->totvert;
 	
-	int dbg_numverts = numverts + numsamples;
+	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], mesh->mvert[i].co);
+		copy_v3_v3(dbg_verts[i], m_mesh->mvert[i].co);
 	}
-	for (int i = 0; i < numsamples; ++i) {
+	for (int i = 0; i < m_numsamples; ++i) {
 		float nor[3], tang[3];
-		BKE_mesh_sample_eval(dm, &samples[i], dbg_verts[numverts + i], nor, tang);
+		BKE_mesh_sample_eval(m_dm, &m_samples[i], dbg_verts[numverts + i], nor, tang);
 	}
-	Mesh *dbg_mesh = BKE_mesh_test_from_data(dbg_verts, dbg_numverts, NULL, 0, faces, face_sizes, numfaces);
+	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 << testname << ".py";
+	filename << TEST_MESH_OUTPUT_FILE << get_testname() << ".py";
 	std::fstream s(filename.str(), s.trunc | s.out);
 	
-	BKE_mesh_test_dump_mesh(dbg_mesh, testname, s);
+	BKE_mesh_test_dump_mesh(dbg_mesh, get_testname().c_str(), s);
 	
 	BKE_mesh_free(dbg_mesh);
 	MEM_freeN(dbg_mesh);
-#else
-	UNUSED_VARS(testname, mesh, samples, numsamples);
 #endif
 }
 
-static void compare_samples(const MeshSample *ground_truth, const MeshSample *samples, int count)
+void MeshSampleTest::compare_samples(const MeshSample *ground_truth)
 {
-	for (int i = 0; i < count; ++i) {
-		EXPECT_EQ(ground_truth[i].orig_verts[0], samples[i].orig_verts[0]);
-		EXPECT_EQ(ground_truth[i].orig_verts[1], samples[i].orig_verts[1]);
-		EXPECT_EQ(ground_truth[i].orig_verts[2], samples[i].orig_verts[2]);
+	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], samples[i].orig_weights[0]);
-		EXPECT_EQ(ground_truth[i].orig_weights[1], samples[i].orig_weights[1]);
-		EXPECT_EQ(ground_truth[i].orig_weights[2], samples[i].orig_weights[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]);
 	}
 }
 
-static void generate_samples_simple(MeshSampleGenerator *gen, MeshSample *samples, int count)
+void MeshSampleTest::generate_samples_simple(MeshSampleGenerator *gen)
 {
-	for (int i = 0; i < count; ++i) {
-		BKE_mesh_sample_generate(gen, &samples[i]);
+	for (int i = 0; i < m_numsamples; ++i) {
+		BKE_mesh_sample_generate(gen, &m_samples[i]);
 	}
 }
 
-static void generate_samples_batch(MeshSampleGenerator *gen, MeshSample *samples, int count)
+void MeshSampleTest::generate_samples_batch(MeshSampleGenerator *gen)
 {
-	BKE_mesh_sample_generate_batch_ex(gen, samples, sizeof(MeshSample), count, false);
+	BKE_mesh_sample_generate_batch_ex(gen, m_samples, sizeof(MeshSample), m_numsamples, false);
 }
 
-static void generate_samples_batch_threaded(MeshSampleGenerator *gen, MeshSample *samples, int count)
+void MeshSampleTest::generate_samples_batch_threaded(MeshSampleGenerator *gen)
 {
-	BKE_mesh_sample_generate_batch_ex(gen, samples, sizeof(MeshSample), count, true);
+	BKE_mesh_sample_generate_batch_ex(gen, m_samples, sizeof(MeshSample), m_numsamples, true);
 }
 
-static void test_samples(MeshSampleGenerator *gen, const MeshSample *ground_truth, MeshSample *samples, int count)
+void MeshSampleTest::test_samples(MeshSampleGenerator *gen, const MeshSample *ground_truth, int count)
 {
-	generate_samples_simple(gen, samples, count);
 	if (ground_truth) {
-		compare_samples(ground_truth, samples, count);
+		EXPECT_EQ(count, m_numsamples) << "Ground truth size does not match number of samples";
+		if (count != m_numsamples) {
+			return;
+		}
+		
+		generate_samples

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list