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

BOP_BSPNode.cpp « intern « boolop « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68a20d7a5a42edc5ed7925e7baac04bca72fe9b6 (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
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/**
 * ***** 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 *****
 */
 
#include "BOP_MathUtils.h"
#include "BOP_BSPNode.h"
#include "MT_assert.h"
#include "MT_MinMax.h"
#include <iostream>
using namespace std;

/**
 * Constructs a new BSP node.
 * @param plane split plane.
 */
BOP_BSPNode::BOP_BSPNode(const MT_Plane3& plane)
{
	m_plane      = plane;
	m_inChild    = NULL;
	m_outChild   = NULL;
	m_deep       = 1;
}

/**
 * Destroys a BSP tree.
 */
BOP_BSPNode::~BOP_BSPNode()
{
	if (m_inChild!=NULL) delete m_inChild;
	if (m_outChild!=NULL) delete m_outChild;
}

/**
 * Adds a new face to this BSP tree.
 * @param pts vector containing face points
 * @param plane face plane.
 */

unsigned int BOP_BSPNode::addFace(BOP_BSPPoints pts,
								  const MT_Plane3& plane )
{
	unsigned int newDeep = 0;
	BOP_TAG tag = ON;

	// find out if any points on the "face" lie in either half-space
	BOP_IT_BSPPoints ptsEnd = pts.end();
	for(BOP_IT_BSPPoints itp=pts.begin();itp!=ptsEnd;itp++){
		tag = (BOP_TAG) ((int) tag | (int)testPoint(*itp));
	}
 
	if (tag == ON) { }		// face lies on hyperplane: do nothing
	else if ((tag & IN) != 0 && (tag & OUT) == 0) {	// face is entirely on inside
		if (m_inChild != NULL)
			newDeep = m_inChild->addFace(pts, plane) + 1;
		else {
			m_inChild = new BOP_BSPNode(plane);
			newDeep = 2;
		}    
	} else if ((tag & OUT) != 0 && (tag & IN) == 0) { // face is entirely on outside
		if (m_outChild != NULL)
			newDeep = m_outChild->addFace(pts, plane) + 1;
		else {
			m_outChild = new BOP_BSPNode(plane);
			newDeep = 2;
		}      
	} else { // face lies in both half-spaces: split it
		BOP_BSPPoints inside, outside;
  		MT_Point3 lpoint= pts[pts.size()-1];
		BOP_TAG ltag = testPoint(lpoint);
		BOP_TAG tstate = ltag;

		// classify each line segment, looking for endpoints which lie on different
		// sides of the hyperplane.

		ptsEnd = pts.end();
		for(BOP_IT_BSPPoints itp=pts.begin();itp!=ptsEnd;itp++){
			MT_Point3 npoint= *itp;
			BOP_TAG ntag = testPoint(npoint);

			if(ltag != ON) {	// last point not on hyperplane
				if(tstate == IN) {
					if (m_inChild != NULL) inside.push_back(lpoint);
				} else {
					if (m_outChild != NULL) outside.push_back(lpoint);
				}
				if(ntag != ON && ntag != tstate) {	// last, self in different half-spaces 
					MT_Point3 mpoint = BOP_intersectPlane( m_plane, lpoint, npoint );
					if (m_inChild != NULL) inside.push_back(mpoint);
					if (m_outChild != NULL) outside.push_back(mpoint);
					tstate = ntag;
				}
			} else {			// last point on hyperplane, so we're switching
								// half-spaces
								// boundary point belong to both faces
				if (m_inChild != NULL) inside.push_back(lpoint);	
				if (m_outChild != NULL) outside.push_back(lpoint);
				tstate = ntag;	// state changes to new point tag
			}
			lpoint = npoint;	// save point, tag for next iteration
			ltag = ntag;
		}

		if (m_inChild != NULL)
			newDeep = m_inChild->addFace(inside, plane) + 1;
		else {
			m_inChild = new BOP_BSPNode(plane);
			newDeep = 2;
		}    
		if (m_outChild != NULL)
			newDeep = MT_max(newDeep, m_outChild->addFace(outside, plane) + 1);
		else {
			m_outChild = new BOP_BSPNode(plane);
			newDeep = MT_max(newDeep,(unsigned int)2);
		}      
	}
	
	// update the deep attribute
	m_deep = MT_max(m_deep,newDeep);
	
	return m_deep;
}

/**
 * Tests the point situation respect the node plane.
 * @param p point to test.
 * @return TAG result: IN, OUT or ON.
 */
BOP_TAG BOP_BSPNode::testPoint(const MT_Point3& p) const
{
  return BOP_createTAG(BOP_classify(p,m_plane));

}

/**
 * Classifies a face using its coordinates and plane.
 * @param p1 first point.
 * @param p2 second point.
 * @param p3 third point.
 * @param plane face plane.
 * @return TAG result: IN, OUT or IN&OUT.
 */
BOP_TAG BOP_BSPNode::classifyFace(const MT_Point3& p1, 
								  const MT_Point3& p2, 
								  const MT_Point3& p3, 
								  const MT_Plane3& plane) const
{
	// local variables
	MT_Point3 auxp1, auxp2;
	BOP_TAG auxtag1, auxtag2, auxtag3;

	switch(BOP_createTAG(testPoint(p1),testPoint(p2),testPoint(p3))) {
		// Classify the face on the IN side
		case IN_IN_IN : 
			return classifyFaceIN(p1, p2, p3, plane);
		case IN_IN_ON :
		case IN_ON_IN :
		case ON_IN_IN :
		case IN_ON_ON :
		case ON_IN_ON :
		case ON_ON_IN :
			return BOP_addON(classifyFaceIN(p1, p2, p3, plane));
	
		// Classify the face on the OUT side
		case OUT_OUT_OUT :
			return classifyFaceOUT(p1, p2, p3, plane);
		case OUT_OUT_ON :
		case OUT_ON_OUT :
		case ON_OUT_OUT :
		case ON_ON_OUT :
		case ON_OUT_ON :
		case OUT_ON_ON :
			return BOP_addON(classifyFaceOUT(p1, p2, p3, plane));
	
		// Classify the ON face depending on it plane normal
		case ON_ON_ON :
			if (hasSameOrientation(plane))
				return BOP_addON(classifyFaceIN(p1, p2, p3, plane));
			else
				return BOP_addON(classifyFaceOUT(p1, p2, p3, plane));

		// Classify the face IN/OUT and one vertex ON
		// becouse only one ON, only one way to subdivide the face
		case IN_OUT_ON :
			auxp1 = BOP_intersectPlane(m_plane, p1, p2);
			auxtag1 = classifyFaceIN( p1,    auxp1 , p3, plane);
			auxtag2 = classifyFaceOUT(auxp1, p2,     p3, plane);
			return (BOP_compTAG(auxtag1,auxtag2)?BOP_addON(auxtag1):INOUT);

		case OUT_IN_ON :
			auxp1 = BOP_intersectPlane(m_plane, p1, p2);
			auxtag1 = classifyFaceOUT(p1,    auxp1, p3, plane);
			auxtag2 = classifyFaceIN( auxp1, p2,    p3, plane);
			return (BOP_compTAG(auxtag1,auxtag2)?BOP_addON(auxtag1):INOUT);

		case IN_ON_OUT :
			auxp1 = BOP_intersectPlane(m_plane, p1, p3);
			auxtag1 = classifyFaceIN( p1, p2, auxp1, plane);
			auxtag2 = classifyFaceOUT(p2, p3, auxp1, plane);
			return (BOP_compTAG(auxtag1,auxtag2)?BOP_addON(auxtag1):INOUT);

		case OUT_ON_IN :
			auxp1 = BOP_intersectPlane(m_plane, p1, p3);
			auxtag1 = classifyFaceOUT(p1, p2, auxp1, plane);
			auxtag2 = classifyFaceIN( p2, p3, auxp1, plane);
			return (BOP_compTAG(auxtag1,auxtag2)?BOP_addON(auxtag1):INOUT);

		case ON_IN_OUT :
			auxp1 = BOP_intersectPlane(m_plane, p2, p3);
			auxtag1 = classifyFaceIN( p1,    p2, auxp1, plane);
			auxtag2 = classifyFaceOUT(auxp1, p3, p1,    plane);
			return (BOP_compTAG(auxtag1,auxtag2)?BOP_addON(auxtag1):INOUT);

		case ON_OUT_IN :
			auxp1 = BOP_intersectPlane(m_plane, p2, p3);
			auxtag1 = classifyFaceOUT(p1,    p2, auxp1, plane);
			auxtag2 = classifyFaceIN( auxp1, p3, p1,    plane);
			return (BOP_compTAG(auxtag1,auxtag2)?BOP_addON(auxtag1):INOUT);

		// Classify IN/OUT face without ON vertices.
		// Two ways to divide the triangle, 
		// will chose the least degenerated sub-triangles.
		case IN_OUT_OUT :
			auxp1 = BOP_intersectPlane(m_plane, p1, p2);
			auxp2 = BOP_intersectPlane(m_plane, p1, p3);
	
			// f1: p1 auxp1 , auxp1 auxp2
			auxtag1 = classifyFaceIN(p1, auxp1, auxp2, plane);
	
			// f2: auxp1 p2 , p2 auxp2;  f3: p2 p3 , p3 auxp2  ||  
			// f2: auxp1 p3, p3 auxp2;   f3: p2 p3 , p3 auxp1
			if (BOP_isInsideCircle(p2, p3, auxp1, auxp2)) {
				auxtag2 = classifyFaceOUT(auxp1, p2, auxp2, plane);
				auxtag3 = classifyFaceOUT(p2,    p3, auxp2, plane);
			}
			else {
				auxtag2 = classifyFaceOUT(auxp1, p3, auxp2, plane);
				auxtag3 = classifyFaceOUT(p2,    p3, auxp1, plane);
			}
			return (BOP_compTAG(auxtag1,auxtag2)&&BOP_compTAG(auxtag2,auxtag3)?auxtag1:INOUT);

		case OUT_IN_IN :
			auxp1 = BOP_intersectPlane(m_plane, p1, p2);
			auxp2 = BOP_intersectPlane(m_plane, p1, p3);
	
			// f1: p1 auxp1 , auxp1 auxp2
			auxtag1 = classifyFaceOUT(p1, auxp1, auxp2, plane);
	
			// f2: auxp1 p2 , p2 auxp2;  f3: p2 p3 , p3 auxp2  ||
			// f2: auxp1 p3, p3 auxp2;  f3: p2 p3 , p3 auxp1
			if (BOP_isInsideCircle(p2, p3, auxp1, auxp2)) {
				auxtag2 = classifyFaceIN(auxp1, p2, auxp2, plane);
				auxtag3 = classifyFaceIN(p2, p3, auxp2, plane);
			}
			else {
				auxtag2 = classifyFaceIN(auxp1, p3, auxp2, plane);
				auxtag3 = classifyFaceIN(p2,    p3, auxp1, plane);
			}
			return (BOP_compTAG(auxtag1,auxtag2)&&BOP_compTAG(auxtag2,auxtag3)?auxtag1:INOUT);

		case OUT_IN_OUT :
			auxp1 = BOP_intersectPlane(m_plane, p2, p1);
			auxp2 = BOP_intersectPlane(m_plane, p2, p3);
	
			// f1: auxp1 p2 , p2 auxp2
			auxtag1 = classifyFaceIN(auxp1, p2, auxp2, plane);
	
			// f2: p1 auxp1 , auxp1 auxp2;  f3: p1 auxp2 , auxp2 p3  ||  
			// f2: p3 auxp1, auxp1 auxp2  f3:p1 auxp1, auxp1 p3
			if (BOP_isInsideCircle(p1, p3, auxp1, auxp2)) {
				auxtag2 = classifyFaceOUT(p1, auxp1, auxp2, plane);
				auxtag3 = classifyFaceOUT(p1, auxp2, p3,    plane);
			}
			else {
				auxtag2 = classifyFaceOUT(p3, auxp1, auxp2, plane);
				auxtag3 = classifyFaceOUT(p1, auxp1, p3,    plane);
			}
			return (BOP_compTAG(auxtag1,auxtag2)&&BOP_compTAG(auxtag2,auxtag3)?auxtag1:INOUT);
    
		case IN_OUT_IN :
			auxp1 = BOP_intersectPlane(m_plane, p2, p1);
			auxp2 = BOP_intersectPlane(m_plane, p2, p3);
	
			// f1: auxp1 p2 , p2 auxp2
			auxtag1 = classifyFaceOUT(auxp1, p2, auxp2, plane);
	
			// f2: p1 auxp1 , auxp1 auxp2;  f3: p1 auxp2 , auxp2 p3  ||  
			// f2: p3 auxp1, auxp1 auxp2  f3:p1 auxp1, auxp1 p3
			if (BOP_isInsideCircle(p1, p3, auxp1, auxp2)) {
				auxtag2 = classifyFaceIN(p1, auxp1, auxp2, plane);
				auxtag3 = classifyFaceIN(p1, auxp2, p3,    plane);
			}
			else {
				auxtag2 = classifyFaceIN(p3, auxp1, auxp2, plane);
				auxtag3 = classifyFaceIN(p1, auxp1, p3,    plane);
			}
			return (BOP_compTAG(auxtag1,auxtag2)&&BOP_compTAG(auxtag2,auxtag3)?auxtag1:INOUT);
	
		case OUT_OUT_IN :
			auxp1 = BOP_intersectPlane(m_plane, p3, p1);
			auxp2 = BOP_intersectPlane(m_plane, p3, p2);
	
			// f1: auxp1 auxp2 , auxp2 p3
			auxtag1 = classifyFaceIN(auxp1, auxp2, p3, plane);
	
			// f2: p1 p2 , p2 auxp2;   f3:p1 auxp2 , auxp2 auxp1  ||
			// f2: p1 p2, p2 auxp1;  f3:p2 auxp2, auxp2 auxp1
			if (BOP_isInsideCircle(p1, p2, auxp1, auxp2)) {
				auxtag2 = classifyFaceOUT(p1, p2,    auxp2, plane);
				auxtag3 = classifyFaceOUT(p1, auxp2, auxp1, plane);
			}
			else {
				auxtag2 = classifyFaceOUT(p1, p2,    auxp1, plane);
				auxtag3 = classifyFaceOUT(p2, auxp2, auxp1, plane);
			}
			return (BOP_compTAG(auxtag1,auxtag2)&&BOP_compTAG(auxtag2,auxtag3)?auxtag1:INOUT);

		case IN_IN_OUT :
			auxp1 = BOP_intersectPlane(m_plane, p3, p1);
			auxp2 = BOP_intersectPlane(m_plane, p3, p2);
	
			// f1: auxp1 auxp2 , auxp2 p3
			auxtag1 = classifyFaceOUT(auxp1, auxp2, p3, plane);
	
			// f2: p1 p2 , p2 auxp2;   f3:p1 auxp2 , auxp2 auxp1  ||
			// f2: p1 p2, p2 auxp1;  f3:p2 auxp2, auxp2 auxp1
			if (BOP_isInsideCircle(p1, p2, auxp1, auxp2)) {
				auxtag2 = classifyFaceIN(p1, p2,    auxp2, plane);
				auxtag3 = classifyFaceIN(p1, auxp2, auxp1, plane);
			}
			else {
				auxtag2 = classifyFaceIN(p1, p2,    auxp1, plane);
				auxtag3 = classifyFaceIN(p2, auxp2, auxp1, plane);
			}
			return (BOP_compTAG(auxtag1,auxtag2)&&BOP_compTAG(auxtag2,auxtag3)?auxtag1:INOUT);

		default:
			return UNCLASSIFIED;
	}
}

/**
 * Classifies a face through IN subtree.
 * @param p1 firts face vertex.
 * @param p2 second face vertex.
 * @param p3 third face vertex.
 * @param plane face plane.
 */
BOP_TAG BOP_BSPNode::classifyFaceIN(const MT_Point3& p1, 
									const MT_Point3& p2, 
									const MT_Point3& p3, 
									const MT_Plane3& plane) const
{
	if (m_inChild != NULL)
		return m_inChild->classifyFace(p1, p2, p3, plane);
	else
		return IN;
}

/**
 * Classifies a face through OUT subtree.
 * @param p1 firts face vertex.
 * @param p2 second face vertex.
 * @param p3 third face vertex.
 * @param plane face plane.
 */
BOP_TAG BOP_BSPNode::classifyFaceOUT(const MT_Point3& p1, 
									 const MT_Point3& p2, 
									 const MT_Point3& p3, 
									 const MT_Plane3& plane) const
{
	if (m_outChild != NULL)
		return m_outChild->classifyFace(p1, p2, p3, plane);
	else
		return OUT;
}

/**
 * Simplified classification (optimized but requires that the face is not 
 * INOUT; only works correctly with faces completely IN or OUT).
 * @param p1 firts face vertex.
 * @param p2 second face vertex.
 * @param p3 third face vertex.
 * @param plane face plane.
 * @return TAG result: IN or OUT.
 */
BOP_TAG BOP_BSPNode::simplifiedClassifyFace(const MT_Point3& p1, 
											const MT_Point3& p2, 
											const MT_Point3& p3, 
											const MT_Plane3& plane) const
{
	MT_Point3 ret[3];

	BOP_TAG tag = BOP_createTAG(testPoint(p1),testPoint(p2),testPoint(p3));

	if ((tag & IN_IN_IN) != 0) {
		if ((tag & OUT_OUT_OUT) != 0) {	    
		  if (splitTriangle(ret,m_plane,p1,p2,p3,tag)<0)
		    return simplifiedClassifyFaceIN(ret[0],ret[1],ret[2],plane);
		  else
		    return simplifiedClassifyFaceOUT(ret[0],ret[1],ret[2],plane);
		}
		else {
			return simplifiedClassifyFaceIN(p1,p2,p3,plane);
		}
	}
	else {
		if ((tag & OUT_OUT_OUT) != 0) {
			return simplifiedClassifyFaceOUT(p1,p2,p3,plane);
		}
		else {
			if (hasSameOrientation(plane)) {
				return simplifiedClassifyFaceIN(p1,p2,p3,plane);
			}
			else {
				return simplifiedClassifyFaceOUT(p1,p2,p3,plane);
			}
		}
	}
	
	return IN;
}

/**
 * Simplified classify through IN subtree.
 * @param p1 firts face vertex.
 * @param p2 second face vertex.
 * @param p3 third face vertex.
 * @param plane face plane.
 */
BOP_TAG BOP_BSPNode::simplifiedClassifyFaceIN(const MT_Point3& p1, 
											  const MT_Point3& p2, 
											  const MT_Point3& p3, 
											  const MT_Plane3& plane) const
{
	if (m_inChild != NULL)
		return m_inChild->simplifiedClassifyFace(p1, p2, p3, plane);
	else
		return IN;
}

/**
 * Simplified classify through OUT subtree.
 * @param p1 firts face vertex.
 * @param p2 second face vertex.
 * @param p3 third face vertex.
 * @param plane face plane.
 */
BOP_TAG BOP_BSPNode::simplifiedClassifyFaceOUT(const MT_Point3& p1, 
											   const MT_Point3& p2, 
											   const MT_Point3& p3, 
											   const MT_Plane3& plane) const
{
	if (m_outChild != NULL)
		return m_outChild->simplifiedClassifyFace(p1, p2, p3, plane);
	else
		return OUT;
}

/**
 * Determine if the input plane have the same orientation of the node plane.
 * @param plane plane to test.
 * @return TRUE if have the same orientation, FALSE otherwise.
 */
bool BOP_BSPNode::hasSameOrientation(const MT_Plane3& plane) const
{
	return (BOP_orientation(m_plane,plane)>0);
}

/**
 * Comparation between both childrens.
 * @return 0 equal deep, 1 inChild more deep than outChild and -1 otherwise.
 */
int BOP_BSPNode::compChildren() const
{
	unsigned int deep1 = (m_inChild == NULL?0:m_inChild->getDeep());
	unsigned int deep2 = (m_outChild == NULL?0:m_outChild->getDeep());
	
	if (deep1 == deep2)
		return 0;
	else if (deep1 < deep2)
		return -1;
	else
		return 1;
}

/**
 * Extract a subtriangle from input triangle, is used for simplified classification.
 * The subtriangle is obtained spliting the input triangle by input plane.
 * @param res output subtriangle result.
 * @param plane spliter plane.
 * @param p1 first triangle point.
 * @param p2 second triangle point.
 * @param p3 third triangle point.
 * @param tag triangle orientation respect the plane.
 */
int BOP_BSPNode::splitTriangle(MT_Point3* res, 
							   const MT_Plane3& plane, 
							   const MT_Point3& p1, 
							   const MT_Point3& p2, 
							   const MT_Point3& p3, 
							   const BOP_TAG tag) const
{
	switch (tag) {
		case IN_OUT_ON :
		  if (compChildren()<0) {
		    // f1: p1 new p3 || new = splitedge(p1,p2)
		    res[0] = p1;
		    res[1] = BOP_intersectPlane( plane, p1, p2 );
		    res[2] = p3;
		    return -1;
		  }else{
		    // f1: p2 new p3 || new = splitedge(p1,p2)
		    res[0] = p2;
		    res[1] = p3;
		    res[2] = BOP_intersectPlane( plane, p1, p2 );
		    return 1;
		  }
		case OUT_IN_ON :
		  if (compChildren()<0) {
		    // f1: p2 new p3 || new = splitedge(p1,p2)
		    res[0] = p2;
		    res[1] = p3;
		    res[2] = BOP_intersectPlane( plane, p1, p2 );
		    return -1;
		  }else{
		    // f1: p1 new p3 || new = splitedge(p1,p2)
		    res[0] = p1;
		    res[1] = BOP_intersectPlane( plane, p1, p2 );
		    res[2] = p3;
		    return 1;
		  }
		case IN_ON_OUT :
		  if (compChildren()<0) {
		    // f1: p1 p2 new || new = splitedge(p1,p3)
		    res[0] = p1;
		    res[1] = p2;
		    res[2] = BOP_intersectPlane( plane, p1, p3 );
		    return -1;
		  }else{
		    // f1: p2 p3 new || new = splitedge(p1,p3)
		    res[0] = p2;
		    res[1] = p3;
		    res[2] = BOP_intersectPlane( plane, p1, p3 );
		    return 1;
		  }
		case OUT_ON_IN :
		  if (compChildren()<0) {
		    // f1: p2 p3 new || new = splitedge(p1,p3)
		    res[0] = p2;
		    res[1] = p3;
		    res[2] = BOP_intersectPlane( plane, p1, p3 );
		    return -1;
		  }else{
		    // f1: p1 p2 new || new = splitedge(p1,p3)
		    res[0] = p1;
		    res[1] = p2;
		    res[2] = BOP_intersectPlane( plane, p1, p3 );
		    return 1;
		  }
		case ON_IN_OUT :
		  if (compChildren()<0) {
		    // f1: p1 p2 new || new = splitedge(p2,p3)
		    res[0] = p1;
		    res[1] = p2;
		    res[2] = BOP_intersectPlane( plane, p2, p3 );
		    return -1;
		  }else{
		    // f1: p1 p3 new || new = splitedge(p2,p3)
		    res[0] = p1;
		    res[1] = BOP_intersectPlane( plane, p2, p3 );
		    res[2] = p3;
		    return 1;
		  }
		case ON_OUT_IN :
		  if (compChildren()<0) {
		    // f1: p1 p2 new || new = splitedge(p2,p3)
		    res[0] = p1;
		    res[1] = BOP_intersectPlane( plane, p2, p3 );
		    res[2] = p3;
		    return -1;
		  }else{
		    // f1: p1 p2 new || new = splitedge(p2,p3)
		    res[0] = p1;
		    res[1] = p2;
		    res[2] = BOP_intersectPlane( plane, p2, p3 );
		    return 1;
		  }
		case IN_OUT_OUT :
		  if (compChildren()<=0) {
		    // f1: p1 new1 new2 || new1 = splitedge(p1,p2) new2 = splitedge(p1,p3)
		    res[0] = p1;
		    res[1] = BOP_intersectPlane( plane, p1, p2 );
		    res[2] = BOP_intersectPlane( plane, p1, p3 );
		    return -1;
		  }else{
		    // f1: p1 new1 new2 || new1 = splitedge(p1,p2) new2 = splitedge(p1,p3)
		    res[0] = BOP_intersectPlane( plane, p1, p2 );
		    res[1] = p2;
		    res[2] = p3;
		    return 1;
		  }
		case OUT_IN_IN :
		  if (compChildren()<0) {
		    // f1: p1 new1 new2 || new1 = splitedge(p1,p2) new2 = splitedge(p1,p3)
		    res[0] = BOP_intersectPlane( plane, p1, p2 );
		    res[1] = p2;
		    res[2] = p3;
		    return -1;
		  }else {
		    // f1: p1 new1 new2 || new1 = splitedge(p1,p2) new2 = splitedge(p1,p3)
		    res[0] = p1;
		    res[1] = BOP_intersectPlane( plane, p1, p2 );
		    res[2] = BOP_intersectPlane( plane, p1, p3 );
		    return 1;
		  }
		case OUT_IN_OUT :
		  if (compChildren()<=0) {
		    // f1: new1 p2 new2 || new1 = splitedge(p2,p1) new2 = splitedge(p2,p3)
		    res[0] = BOP_intersectPlane( plane, p2, p1 );
		    res[1] = p2;
		    res[2] = BOP_intersectPlane( plane, p2, p3 );
		    return -1;
		  }else {
		    // f1: new1 p2 new2 || new1 = splitedge(p2,p1) new2 = splitedge(p2,p3)
		    res[0] = p1;
		    res[1] = BOP_intersectPlane( plane, p2, p1 );
		    res[2] = BOP_intersectPlane( plane, p2, p3 );
		    return 1;
		  }
		case IN_OUT_IN :
		  if (compChildren()<0) {
		    // f1: new1 p2 new2 || new1 = splitedge(p2,p1) new2 = splitedge(p2,p3)
		    res[0] = p1;
		    res[1] = BOP_intersectPlane( plane, p2, p1 );
		    res[2] = BOP_intersectPlane( plane, p2, p3 );
		    return -1;
		  }else{
		    // f1: new1 p2 new2 || new1 = splitedge(p2,p1) new2 = splitedge(p2,p3)
		    res[0] = BOP_intersectPlane( plane, p2, p1 );
		    res[1] = p2;
		    res[2] = BOP_intersectPlane( plane, p2, p3 );
		    return 1;
		  }
		case OUT_OUT_IN :
		  if (compChildren()<=0) {
		    // f1: new1 new2 p2 || new1 = splitedge(p3,p1) new2 = splitedge(p3,p2)
		    res[0] = BOP_intersectPlane( plane, p3, p1 );
		    res[1] = BOP_intersectPlane( plane, p3, p2 );
		    res[2] = p3;
		    return -1;
		  }else{
		    // f1: new1 new2 p2 || new1 = splitedge(p3,p1) new2 = splitedge(p3,p2)
		    res[0] = BOP_intersectPlane( plane, p3, p1 );
		    res[1] = p1;
		    res[2] = p2;
		    return 1;
		  }
		case IN_IN_OUT :
		  if (compChildren()<0) {
		    // f1: new1 new2 p2 || new1 = splitedge(p3,p1) new2 = splitedge(p3,p2)
		    res[0] = BOP_intersectPlane( plane, p3, p1 );
		    res[1] = p1;
		    res[2] = p2;
		    return -1;
		  }else{
		    // f1: new1 new2 p2 || new1 = splitedge(p3,p1) new2 = splitedge(p3,p2)
		    res[0] = BOP_intersectPlane( plane, p3, p1 );
		    res[1] = BOP_intersectPlane( plane, p3, p2 );
		    res[2] = p3;
		    return 1;
		  }
	default:
	  return 0;
	}
}

/**
 * Debug info.
 */
void BOP_BSPNode::print(unsigned int deep)
{
	cout << "(" << deep << "," << m_plane << ")," << endl;
	if (m_inChild != NULL)
		m_inChild->print(deep + 1);
	else
		cout << "(" << deep+1 << ",None)," << endl;
	if (m_outChild != NULL)
		m_outChild->print(deep + 1);
	else
		cout << "(" << deep+1 << ",None)," << endl;
}