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

BKE_mesh_test_util.cc « blenkernel « gtests « tests - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f72ee7c74d08c33317d2c8a40c47fc3c5e04d601 (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
/* Apache License, Version 2.0 */

#include <iostream>
#include <iomanip>

#include "BKE_mesh_test_util.h"

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

#include "BLI_math.h"

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

#include "BKE_customdata.h"
#include "BKE_mesh.h"
}

static void mesh_update(Mesh *mesh, int calc_edges, int calc_tessface)
{
	bool tessface_input = false;

	if (mesh->totface > 0 && mesh->totpoly == 0) {
		BKE_mesh_convert_mfaces_to_mpolys(mesh);

		/* would only be converting back again, don't bother */
		tessface_input = true;
	}

	if (calc_edges || ((mesh->totpoly || mesh->totface) && mesh->totedge == 0))
		BKE_mesh_calc_edges(mesh, calc_edges, true);

	if (calc_tessface) {
		if (tessface_input == false) {
			BKE_mesh_tessface_calc(mesh);
		}
	}
	else {
		/* default state is not to have tessface's so make sure this is the case */
		BKE_mesh_tessface_clear(mesh);
	}

	BKE_mesh_calc_normals(mesh);
}

static void mesh_add_verts(Mesh *mesh, int len)
{
	CustomData vdata;

	if (len == 0)
		return;

	int totvert = mesh->totvert + len;
	CustomData_copy(&mesh->vdata, &vdata, CD_MASK_MESH, CD_DEFAULT, totvert);
	CustomData_copy_data(&mesh->vdata, &vdata, 0, 0, mesh->totvert);

	if (!CustomData_has_layer(&vdata, CD_MVERT))
		CustomData_add_layer(&vdata, CD_MVERT, CD_CALLOC, NULL, totvert);

	CustomData_free(&mesh->vdata, mesh->totvert);
	mesh->vdata = vdata;
	BKE_mesh_update_customdata_pointers(mesh, false);

	/* scan the input list and insert the new vertices */

	/* set final vertex list size */
	mesh->totvert = totvert;
}

static void mesh_add_edges(Mesh *mesh, int len)
{
	CustomData edata;
	MEdge *medge;
	int i, totedge;

	if (len == 0)
		return;

	totedge = mesh->totedge + len;

	/* update customdata  */
	CustomData_copy(&mesh->edata, &edata, CD_MASK_MESH, CD_DEFAULT, totedge);
	CustomData_copy_data(&mesh->edata, &edata, 0, 0, mesh->totedge);

	if (!CustomData_has_layer(&edata, CD_MEDGE))
		CustomData_add_layer(&edata, CD_MEDGE, CD_CALLOC, NULL, totedge);

	CustomData_free(&mesh->edata, mesh->totedge);
	mesh->edata = edata;
	BKE_mesh_update_customdata_pointers(mesh, false); /* new edges don't change tessellation */

	/* set default flags */
	medge = &mesh->medge[mesh->totedge];
	for (i = 0; i < len; i++, medge++)
		medge->flag = ME_EDGEDRAW | ME_EDGERENDER;

	mesh->totedge = totedge;
}

static void mesh_add_loops(Mesh *mesh, int len)
{
	CustomData ldata;
	int totloop;

	if (len == 0)
		return;

	totloop = mesh->totloop + len;   /* new face count */

	/* update customdata */
	CustomData_copy(&mesh->ldata, &ldata, CD_MASK_MESH, CD_DEFAULT, totloop);
	CustomData_copy_data(&mesh->ldata, &ldata, 0, 0, mesh->totloop);

	if (!CustomData_has_layer(&ldata, CD_MLOOP))
		CustomData_add_layer(&ldata, CD_MLOOP, CD_CALLOC, NULL, totloop);

	CustomData_free(&mesh->ldata, mesh->totloop);
	mesh->ldata = ldata;
	BKE_mesh_update_customdata_pointers(mesh, true);

	mesh->totloop = totloop;
}

static void mesh_add_polys(Mesh *mesh, int len)
{
	CustomData pdata;
	MPoly *mpoly;
	int i, totpoly;

	if (len == 0)
		return;

	totpoly = mesh->totpoly + len;   /* new face count */

	/* update customdata */
	CustomData_copy(&mesh->pdata, &pdata, CD_MASK_MESH, CD_DEFAULT, totpoly);
	CustomData_copy_data(&mesh->pdata, &pdata, 0, 0, mesh->totpoly);

	if (!CustomData_has_layer(&pdata, CD_MPOLY))
		CustomData_add_layer(&pdata, CD_MPOLY, CD_CALLOC, NULL, totpoly);

	CustomData_free(&mesh->pdata, mesh->totpoly);
	mesh->pdata = pdata;
	BKE_mesh_update_customdata_pointers(mesh, true);

	/* set default flags */
	mpoly = &mesh->mpoly[mesh->totpoly];
	for (i = 0; i < len; i++, mpoly++)
		mpoly->flag = ME_FACE_SEL;

	mesh->totpoly = totpoly;
}

Mesh* BKE_mesh_test_from_data(
        const float (*verts)[3], int numverts,
        const int (*edges)[2], int numedges,
        const int *loops, const int *face_lengths, int numfaces)
{
	Mesh *me = (Mesh *)MEM_callocN(sizeof(Mesh), "Mesh");
	
	BKE_mesh_init(me);
	
	int numloops = 0;
	for (int i = 0; i < numfaces; ++i) {
		numloops += face_lengths[i];
	}
	
	mesh_add_verts(me, numverts);
	mesh_add_edges(me, numedges);
	mesh_add_loops(me, numloops);
	mesh_add_polys(me, numfaces);
	
	{
		MVert *v = me->mvert;
		for (int i = 0; i < numverts; ++i, ++v) {
			copy_v3_v3(v->co, verts[i]);
		}
	}
	
	{
		MEdge *e = me->medge;
		for (int i = 0; i < numedges; ++i, ++e) {
			e->v1 = edges[i][0];
			e->v2 = edges[i][1];
		}
	}
	
	{
		MLoop *l = me->mloop;
		for (int i = 0; i < numloops; ++i, ++l) {
			l->v = loops[i];
		}
	}
	
	{
		MPoly *p = me->mpoly;
		int loopstart = 0;
		for (int i = 0; i < numfaces; ++i, ++p) {
			int totloop = face_lengths[i];
			p->loopstart = loopstart;
			p->totloop = totloop;
			
			loopstart += totloop;
		}
	}
	
	if (numfaces > 0 && numedges == 0) {
		mesh_update(me, true, false);
	}
	
	return me;
}

void BKE_mesh_test_dump_verts(Mesh *me, std::ostream &str)
{
	int numverts = me->totvert;
	
	str << std::setprecision(5);
	
	MVert *v = me->mvert;
	str << "[";
	for (int i = 0; i < numverts; ++i, ++v) {
		str << "(" << v->co[0] << ", " << v->co[1] << ", " << v->co[2] << "), ";
	}
	str << "]";
}

void BKE_mesh_test_dump_edges(Mesh *me, std::ostream &str)
{
	int numedges = me->totedge;
	
	MEdge *e = me->medge;
	str << "[";
	for (int i = 0; i < numedges; ++i, ++e) {
		str << "(" << e->v1 << ", " << e->v2 << "), ";
	}
	str << "]";
}

void BKE_mesh_test_dump_faces(Mesh *me, std::ostream &str)
{
	int numpolys = me->totpoly;
	
	MPoly *p = me->mpoly;
	str << "[";
	for (int i = 0; i < numpolys; ++i, ++p) {
		int totloop = p->totloop;
		MLoop *l = me->mloop + p->loopstart;
		
		str << "(";
		for (int k = 0; k < totloop; ++k, ++l) {
			str << l->v << ", ";
		}
		str << "), ";
	}
	str << "]";
}

void BKE_mesh_test_dump_mesh(Mesh *me, const char *name, std::ostream &str)
{
	str << "import bpy\n";
	str << "from bpy_extras.object_utils import object_data_add\n";
	str << "mesh = bpy.data.meshes.new(name=\"" << name << "\")\n";
	
	str << "mesh.from_pydata(";
	str << "vertices=";
	BKE_mesh_test_dump_verts(me, str);
	str << ", edges=";
	BKE_mesh_test_dump_edges(me, str);
	str << ", faces=";
	BKE_mesh_test_dump_faces(me, str);
	str << ")\n";
	
	str << "object_data_add(bpy.context, mesh)\n";
}