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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
/**
* $Id$
*
* ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version. The Blender
* Foundation also sells licenses for use in proprietary software under
* the Blender License. See http://www.blender.org/BL/ for information
* about this.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL/BL DUAL LICENSE BLOCK *****
* CSG operations.
*/
#include <string.h>
#include "MEM_guardedalloc.h"
#include "BLI_ghash.h"
#include "DNA_material_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "CSG_BooleanOps.h"
#include "BKE_booleanops.h"
#include "BKE_cdderivedmesh.h"
#include "BKE_depsgraph.h"
#include "BKE_DerivedMesh.h"
#include "BKE_global.h"
#include "BKE_library.h"
#include "BKE_material.h"
#include "BKE_mesh.h"
#include "BKE_object.h"
#include "BKE_utildefines.h"
#include "BIF_toolbox.h"
#include "BDR_editface.h"
#include <math.h>
// TODO check to see how many of these includes are necessary
#include "BLI_blenlib.h"
#include "BLI_arithb.h"
#include "BLI_linklist.h"
#include "BLI_memarena.h"
/**
* Here's the vertex iterator structure used to walk through
* the blender vertex structure.
*/
typedef struct {
Mesh *mesh;
Object *ob;
int pos;
} VertexIt;
/**
* Implementations of local vertex iterator functions.
* These describe a blender mesh to the CSG module.
*/
static void VertexIt_Destruct(CSG_VertexIteratorDescriptor * iterator)
{
if (iterator->it) {
// deallocate memory for iterator
MEM_freeN(iterator->it);
iterator->it = 0;
}
iterator->Done = NULL;
iterator->Fill = NULL;
iterator->Reset = NULL;
iterator->Step = NULL;
iterator->num_elements = 0;
}
static int VertexIt_Done(CSG_IteratorPtr it)
{
VertexIt * iterator = (VertexIt *)it;
return(iterator->pos >= iterator->mesh->totvert);
}
static void VertexIt_Fill(CSG_IteratorPtr it, CSG_IVertex *vert)
{
VertexIt * iterator = (VertexIt *)it;
MVert *verts = iterator->mesh->mvert;
float global_pos[3];
/* boolean happens in global space, transform both with obmat */
VecMat4MulVecfl(
global_pos,
iterator->ob->obmat,
verts[iterator->pos].co
);
vert->position[0] = global_pos[0];
vert->position[1] = global_pos[1];
vert->position[2] = global_pos[2];
}
static void VertexIt_Step(CSG_IteratorPtr it)
{
VertexIt * iterator = (VertexIt *)it;
iterator->pos ++;
}
static void VertexIt_Reset(CSG_IteratorPtr it)
{
VertexIt * iterator = (VertexIt *)it;
iterator->pos = 0;
}
static void VertexIt_Construct(CSG_VertexIteratorDescriptor *output, Object *ob)
{
VertexIt *it;
if (output == 0) return;
// allocate some memory for blender iterator
it = (VertexIt *)(MEM_mallocN(sizeof(VertexIt),"Boolean_VIt"));
if (it == 0) {
return;
}
// assign blender specific variables
it->ob = ob;
it->mesh = ob->data;
it->pos = 0;
// assign iterator function pointers.
output->Step = VertexIt_Step;
output->Fill = VertexIt_Fill;
output->Done = VertexIt_Done;
output->Reset = VertexIt_Reset;
output->num_elements = it->mesh->totvert;
output->it = it;
}
/**
* Blender Face iterator
*/
typedef struct {
Mesh *mesh;
int pos;
int offset;
} FaceIt;
static void FaceIt_Destruct(CSG_FaceIteratorDescriptor * iterator)
{
MEM_freeN(iterator->it);
iterator->Done = NULL;
iterator->Fill = NULL;
iterator->Reset = NULL;
iterator->Step = NULL;
iterator->num_elements = 0;
}
static int FaceIt_Done(CSG_IteratorPtr it)
{
// assume CSG_IteratorPtr is of the correct type.
FaceIt * iterator = (FaceIt *)it;
return(iterator->pos >= iterator->mesh->totface);
}
static void FaceIt_Fill(CSG_IteratorPtr it, CSG_IFace *face)
{
// assume CSG_IteratorPtr is of the correct type.
FaceIt *face_it = (FaceIt *)it;
MFace *mfaces = face_it->mesh->mface;
MFace *mface = &mfaces[face_it->pos];
face->vertex_index[0] = mface->v1;
face->vertex_index[1] = mface->v2;
face->vertex_index[2] = mface->v3;
if (mface->v4) {
face->vertex_index[3] = mface->v4;
face->vertex_number = 4;
} else {
face->vertex_number = 3;
}
face->orig_face = face_it->offset + face_it->pos;
}
static void FaceIt_Step(CSG_IteratorPtr it)
{
FaceIt * face_it = (FaceIt *)it;
face_it->pos ++;
}
static void FaceIt_Reset(CSG_IteratorPtr it)
{
FaceIt * face_it = (FaceIt *)it;
face_it->pos = 0;
}
static void FaceIt_Construct(
CSG_FaceIteratorDescriptor *output, Object *ob, int offset)
{
FaceIt *it;
if (output == 0) return;
// allocate some memory for blender iterator
it = (FaceIt *)(MEM_mallocN(sizeof(FaceIt),"Boolean_FIt"));
if (it == 0) {
return ;
}
// assign blender specific variables
it->mesh = ob->data;
it->offset = offset;
it->pos = 0;
// assign iterator function pointers.
output->Step = FaceIt_Step;
output->Fill = FaceIt_Fill;
output->Done = FaceIt_Done;
output->Reset = FaceIt_Reset;
output->num_elements = it->mesh->totface;
output->it = it;
}
static Object *AddNewBlenderMesh(Base *base)
{
// This little function adds a new mesh object to the blender object list
// It uses ob to duplicate data as this seems to be easier than creating
// a new one. This new oject contains no faces nor vertices.
Mesh *old_me;
Base *basen;
Object *ob_new;
// now create a new blender object.
// duplicating all the settings from the previous object
// to the new one.
ob_new= copy_object(base->object);
// Ok we don't want to use the actual data from the
// last object, the above function incremented the
// number of users, so decrement it here.
old_me= ob_new->data;
old_me->id.us--;
// Now create a new base to add into the linked list of
// vase objects.
basen= MEM_mallocN(sizeof(Base), "duplibase");
*basen= *base;
BLI_addhead(&G.scene->base, basen); /* addhead: anders oneindige lus */
basen->object= ob_new;
basen->flag &= ~SELECT;
// Initialize the mesh data associated with this object.
ob_new->data= add_mesh();
G.totmesh++;
// Finally assign the object type.
ob_new->type= OB_MESH;
return ob_new;
}
/* editmode function, will be replaced soon once custom face data arrives */
void interp_uv_vcol(float *v1, float *v2, float *v3, float *v4, float *co,
TFace *tf, TFace *outtf, int j);
static void ConvertCSGTFace(
DerivedMesh *dm, Mesh *orig_me, int index, int orig_index, int nr,
TFace *tface, TFace *orig_tface, float mapmat[][4])
{
float obco[3], *co[4], *orig_co[4];
MFace *mface, *orig_mface;
int j;
mface = CDDM_get_face(dm, index);
orig_mface = orig_me->mface + orig_index;
*tface = *orig_tface;
// get the vertex coordinates from the original mesh
orig_co[0] = (orig_me->mvert + orig_mface->v1)->co;
orig_co[1] = (orig_me->mvert + orig_mface->v2)->co;
orig_co[2] = (orig_me->mvert + orig_mface->v3)->co;
orig_co[3] = (orig_mface->v4)? (orig_me->mvert + orig_mface->v4)->co: NULL;
// get the vertex coordinates from the new derivedmesh
co[0] = CDDM_get_vert(dm, mface->v1)->co;
co[1] = CDDM_get_vert(dm, mface->v2)->co;
co[2] = CDDM_get_vert(dm, mface->v3)->co;
co[3] = (nr == 4)? CDDM_get_vert(dm, mface->v4)->co: NULL;
for (j = 0; j < nr; j++) {
// get coordinate into the space of the original mesh
if (mapmat)
VecMat4MulVecfl(obco, mapmat, co[j]);
else
VecCopyf(obco, co[j]);
interp_uv_vcol(orig_co[0], orig_co[1], orig_co[2], orig_co[3], obco,
orig_tface, tface, j);
}
}
/* Iterate over the CSG Output Descriptors and create a new DerivedMesh
from them */
static DerivedMesh *ConvertCSGDescriptorsToDerivedMesh(
CSG_FaceIteratorDescriptor *face_it,
CSG_VertexIteratorDescriptor *vertex_it,
float parinv[][4],
float mapmat[][4],
Material **mat,
int *totmat,
Object *ob1,
Object *ob2)
{
DerivedMesh *dm;
GHash *material_hash = NULL;
Mesh *me1= (Mesh*)ob1->data;
Mesh *me2= (Mesh*)ob2->data;
int i;
// create a new DerivedMesh
dm = CDDM_new(vertex_it->num_elements, 0, face_it->num_elements);
// add tface layers
if (me1->mcol)
mcol_to_tface(me1, 1);
if (me2->mcol)
mcol_to_tface(me2, 1);
if (me1->tface || me2->tface)
DM_add_face_layer(dm, LAYERTYPE_TFACE, 0, NULL);
// step through the vertex iterators:
for (i = 0; !vertex_it->Done(vertex_it->it); i++) {
CSG_IVertex csgvert;
MVert *mvert = CDDM_get_vert(dm, i);
// retrieve a csg vertex from the boolean module
vertex_it->Fill(vertex_it->it, &csgvert);
vertex_it->Step(vertex_it->it);
// we have to map the vertex coordinates back in the coordinate frame
// of the resulting object, since it was computed in world space
VecMat4MulVecfl(mvert->co, parinv, csgvert.position);
}
// a hash table to remap materials to indices
if (mat) {
material_hash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
*totmat = 0;
}
// step through the face iterators
for(i = 0; !face_it->Done(face_it->it); i++) {
Mesh *orig_me;
Object *orig_ob;
Material *orig_mat;
CSG_IFace csgface;
MFace *mface, *orig_mface;
TFace *tface, *orig_tface;
int orig_index, mat_nr;
// retrieve a csg face from the boolean module
face_it->Fill(face_it->it, &csgface);
face_it->Step(face_it->it);
// find the original mesh and data
orig_me = (csgface.orig_face < me1->totface)? me1: me2;
orig_ob = (orig_me == me1)? ob1: ob2;
orig_index = (orig_me == me1)? csgface.orig_face: csgface.orig_face - me1->totface;
orig_mface = orig_me->mface + orig_index;
orig_mat= give_current_material(orig_ob, orig_mface->mat_nr+1);
// set mface
mface = CDDM_get_face(dm, i);
mface->v1 = csgface.vertex_index[0];
mface->v2 = csgface.vertex_index[1];
mface->v3 = csgface.vertex_index[2];
mface->v4 = (csgface.vertex_number == 4)? csgface.vertex_index[3]: 0;
mface->flag = orig_mface->flag;
// set material, based on lookup in hash table
if (mat && orig_mat) {
if (!BLI_ghash_haskey(material_hash, orig_mat)) {
mat[*totmat] = orig_mat;
mat_nr = mface->mat_nr = (*totmat)++;
BLI_ghash_insert(material_hash, orig_mat, (void*)mat_nr);
}
else
mface->mat_nr = (int)BLI_ghash_lookup(material_hash, orig_mat);
}
else
mface->mat_nr = 0;
// set tface
if (me1->tface || me2->tface) {
tface = DM_get_face_data(dm, i, LAYERTYPE_TFACE);;
if (orig_me->tface) {
orig_tface = orig_me->tface + orig_index;
ConvertCSGTFace(dm, orig_me, i, orig_index,
csgface.vertex_number, tface, orig_tface,
(orig_me == me2)? mapmat: NULL);
}
else
default_tface(tface);
}
else
tface = NULL;
test_index_face(mface, NULL, tface, csgface.vertex_number);
}
if (material_hash)
BLI_ghash_free(material_hash, NULL, NULL);
CDDM_calc_edges(dm);
CDDM_calc_normals(dm);
return dm;
}
static void BuildMeshDescriptors(
struct Object *ob,
int face_offset,
struct CSG_FaceIteratorDescriptor * face_it,
struct CSG_VertexIteratorDescriptor * vertex_it)
{
VertexIt_Construct(vertex_it,ob);
FaceIt_Construct(face_it,ob,face_offset);
}
static void FreeMeshDescriptors(
struct CSG_FaceIteratorDescriptor *face_it,
struct CSG_VertexIteratorDescriptor *vertex_it)
{
VertexIt_Destruct(vertex_it);
FaceIt_Destruct(face_it);
}
DerivedMesh *NewBooleanDerivedMesh_intern(
struct Object *ob, struct Object *ob_select,
int int_op_type, Material **mat, int *totmat)
{
float inv_mat[4][4];
float map_mat[4][4];
DerivedMesh *dm = NULL;
Mesh *me1 = get_mesh(ob);
Mesh *me2 = get_mesh(ob_select);
if (me1 == NULL || me2 == NULL) return 0;
if (!me1->totface || !me2->totface) return 0;
// we map the final object back into ob's local coordinate space. For this
// we need to compute the inverse transform from global to ob (inv_mat),
// and the transform from ob to ob_select for use in interpolation (map_mat)
Mat4Invert(inv_mat, ob_select->obmat);
Mat4MulMat4(map_mat, ob->obmat, inv_mat);
Mat4Invert(inv_mat, ob->obmat);
{
// interface with the boolean module:
//
// the idea is, we pass the boolean module verts and faces using the
// provided descriptors. once the boolean operation is performed, we
// get back output descriptors, from which we then build a DerivedMesh
CSG_VertexIteratorDescriptor vd_1, vd_2;
CSG_FaceIteratorDescriptor fd_1, fd_2;
CSG_OperationType op_type;
CSG_BooleanOperation *bool_op;
// work out the operation they chose and pick the appropriate
// enum from the csg module.
switch (int_op_type) {
case 1 : op_type = e_csg_intersection; break;
case 2 : op_type = e_csg_union; break;
case 3 : op_type = e_csg_difference; break;
case 4 : op_type = e_csg_classify; break;
default : op_type = e_csg_intersection;
}
BuildMeshDescriptors(ob, 0, &fd_1, &vd_1);
BuildMeshDescriptors(ob_select, me1->totface, &fd_2, &vd_2);
bool_op = CSG_NewBooleanFunction();
// perform the operation
if (CSG_PerformBooleanOperation(bool_op, op_type, fd_1, vd_1, fd_2, vd_2)) {
CSG_VertexIteratorDescriptor vd_o;
CSG_FaceIteratorDescriptor fd_o;
CSG_OutputFaceDescriptor(bool_op, &fd_o);
CSG_OutputVertexDescriptor(bool_op, &vd_o);
// iterate through results of operation and insert
// into new object
dm = ConvertCSGDescriptorsToDerivedMesh(
&fd_o, &vd_o, inv_mat, map_mat, mat, totmat, ob, ob_select);
// free up the memory
CSG_FreeVertexDescriptor(&vd_o);
CSG_FreeFaceDescriptor(&fd_o);
}
else
error("Unknown internal error in boolean");
CSG_FreeBooleanOperation(bool_op);
FreeMeshDescriptors(&fd_1, &vd_1);
FreeMeshDescriptors(&fd_2, &vd_2);
}
return dm;
}
int NewBooleanMesh(Base *base, Base *base_select, int int_op_type)
{
Mesh *me_new;
int a, maxmat, totmat= 0;
Object *ob_new, *ob, *ob_select;
Material **mat;
DerivedMesh *dm;
ob= base->object;
ob_select= base_select->object;
maxmat= ob->totcol + ob_select->totcol;
mat= (Material**)MEM_mallocN(sizeof(Material*)*maxmat, "NewBooleanMeshMat");
dm= NewBooleanDerivedMesh_intern(ob, ob_select, int_op_type, mat, &totmat);
if (dm == NULL) {
MEM_freeN(mat);
return 0;
}
/* create a new blender mesh object - using 'base' as a template */
ob_new= AddNewBlenderMesh(base);
me_new= ob_new->data;
DM_to_mesh(dm, me_new);
dm->release(dm);
/* add materials to object */
for (a = 0; a < totmat; a++)
assign_material(ob_new, mat[a], a+1);
MEM_freeN(mat);
/* update dag */
DAG_object_flush_update(G.scene, ob_new, OB_RECALC_DATA);
return 1;
}
DerivedMesh *NewBooleanDerivedMesh(struct Object *ob, struct Object *ob_select,
int int_op_type)
{
return NewBooleanDerivedMesh_intern(ob, ob_select, int_op_type, NULL, NULL);
}
|