forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkPolygon.cxx
2004 lines (1750 loc) · 55.4 KB
/
vtkPolygon.cxx
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPolygon.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkPolygon.h"
#include "vtkCellArray.h"
#include "vtkDataSet.h"
#include "vtkDoubleArray.h"
#include "vtkLine.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPlane.h"
#include "vtkPoints.h"
#include "vtkPriorityQueue.h"
#include "vtkQuad.h"
#include "vtkTriangle.h"
#include "vtkBox.h"
#include "vtkMergePoints.h"
#include "vtkIncrementalPointLocator.h"
#include "vtkSmartPointer.h"
vtkStandardNewMacro(vtkPolygon);
//----------------------------------------------------------------------------
// Instantiate polygon.
vtkPolygon::vtkPolygon()
{
this->Tris = vtkIdList::New();
this->Tris->Allocate(VTK_CELL_SIZE);
this->Triangle = vtkTriangle::New();
this->Quad = vtkQuad::New();
this->TriScalars = vtkDoubleArray::New();
this->TriScalars->Allocate(3);
this->Line = vtkLine::New();
this->Tolerance = 0.0;
this->SuccessfulTriangulation = 0;
this->Normal[0] = this->Normal[1] = this->Normal[2] = 0.0;
this->UseMVCInterpolation = false;
}
//----------------------------------------------------------------------------
vtkPolygon::~vtkPolygon()
{
this->Tris->Delete();
this->Triangle->Delete();
this->Quad->Delete();
this->TriScalars->Delete();
this->Line->Delete();
}
//----------------------------------------------------------------------------
double vtkPolygon::ComputeArea()
{
double normal[3]; //not used, but required for the
//following ComputeArea call
return vtkPolygon::ComputeArea(this->GetPoints(),
this->GetNumberOfPoints(),
this->GetPointIds()->GetPointer(0),
normal);
}
#define VTK_POLYGON_FAILURE -1
#define VTK_POLYGON_OUTSIDE 0
#define VTK_POLYGON_INSIDE 1
#define VTK_POLYGON_INTERSECTION 2
#define VTK_POLYGON_ON_LINE 3
//----------------------------------------------------------------------------
//
// In many of the functions that follow, the Points and PointIds members
// of the Cell are assumed initialized. This is usually done indirectly
// through the GetCell(id) method in the DataSet objects.
//
// Compute the polygon normal from a points list, and a list of point ids
// that index into the points list. This version will handle non-convex
// polygons.
void vtkPolygon::ComputeNormal(vtkPoints *p, int numPts, vtkIdType *pts,
double *n)
{
int i;
double v0[3], v1[3], v2[3];
double ax, ay, az, bx, by, bz;
//
// Check for special triangle case. Saves extra work.
//
n[0] = n[1] = n[2] = 0.0;
if ( numPts == 2 || numPts == 1 )
{
return;
}
if ( numPts == 3 )
{
p->GetPoint(pts[0],v0);
p->GetPoint(pts[1],v1);
p->GetPoint(pts[2],v2);
vtkTriangle::ComputeNormal(v0, v1, v2, n);
return;
}
// Because polygon may be concave, need to accumulate cross products to
// determine true normal.
//
p->GetPoint(pts[0],v1); //set things up for loop
p->GetPoint(pts[1],v2);
for (i=0; i < numPts; i++)
{
v0[0] = v1[0]; v0[1] = v1[1]; v0[2] = v1[2];
v1[0] = v2[0]; v1[1] = v2[1]; v1[2] = v2[2];
p->GetPoint(pts[(i+2)%numPts],v2);
// order is important!!! to maintain consistency with polygon vertex order
ax = v2[0] - v1[0]; ay = v2[1] - v1[1]; az = v2[2] - v1[2];
bx = v0[0] - v1[0]; by = v0[1] - v1[1]; bz = v0[2] - v1[2];
n[0] += (ay * bz - az * by);
n[1] += (az * bx - ax * bz);
n[2] += (ax * by - ay * bx);
}
vtkMath::Normalize(n);
}
//----------------------------------------------------------------------------
// Compute the polygon normal from a points list, and a list of point ids
// that index into the points list. This version will handle non-convex
// polygons.
void vtkPolygon::ComputeNormal(vtkIdTypeArray *ids, vtkPoints *p, double n[3])
{
vtkIdType i, numPts = ids->GetNumberOfTuples();
double v0[3], v1[3], v2[3];
double ax, ay, az, bx, by, bz;
//
// Check for special triangle case. Saves extra work.
//
n[0] = n[1] = n[2] = 0.0;
if ( numPts == 2 || numPts == 1 )
{
return;
}
if ( numPts == 3 )
{
p->GetPoint(ids->GetValue(0),v0);
p->GetPoint(ids->GetValue(1),v1);
p->GetPoint(ids->GetValue(2),v2);
vtkTriangle::ComputeNormal(v0, v1, v2, n);
return;
}
// Because polygon may be concave, need to accumulate cross products to
// determine true normal.
//
p->GetPoint(ids->GetValue(0),v1); //set things up for loop
p->GetPoint(ids->GetValue(1),v2);
for (i=0; i < numPts; i++)
{
v0[0] = v1[0]; v0[1] = v1[1]; v0[2] = v1[2];
v1[0] = v2[0]; v1[1] = v2[1]; v1[2] = v2[2];
p->GetPoint(ids->GetValue((i+2)%numPts),v2);
// order is important!!! to maintain consistency with polygon vertex order
ax = v2[0] - v1[0]; ay = v2[1] - v1[1]; az = v2[2] - v1[2];
bx = v0[0] - v1[0]; by = v0[1] - v1[1]; bz = v0[2] - v1[2];
n[0] += (ay * bz - az * by);
n[1] += (az * bx - ax * bz);
n[2] += (ax * by - ay * bx);
}
vtkMath::Normalize(n);
}
//----------------------------------------------------------------------------
// Compute the polygon normal from a list of doubleing points. This version
// will handle non-convex polygons.
void vtkPolygon::ComputeNormal(vtkPoints *p, double *n)
{
int i, numPts;
double v0[3], v1[3], v2[3];
double ax, ay, az, bx, by, bz;
// Polygon is assumed non-convex -> need to accumulate cross products to
// find correct normal.
//
numPts = p->GetNumberOfPoints();
p->GetPoint(0, v1); //set things up for loop
p->GetPoint(1, v2);
n[0] = n[1] = n[2] = 0.0;
for (i=0; i < numPts; i++)
{
memcpy(v0, v1, sizeof(v0));
memcpy(v1, v2, sizeof(v1));
p->GetPoint((i+2)%numPts, v2);
// order is important!!! to maintain consistency with polygon vertex order
ax = v2[0] - v1[0]; ay = v2[1] - v1[1]; az = v2[2] - v1[2];
bx = v0[0] - v1[0]; by = v0[1] - v1[1]; bz = v0[2] - v1[2];
n[0] += (ay * bz - az * by);
n[1] += (az * bx - ax * bz);
n[2] += (ax * by - ay * bx);
}//over all points
vtkMath::Normalize(n);
}
//----------------------------------------------------------------------------
// Compute the polygon normal from an array of points. This version assumes
// that the polygon is convex, and looks for the first valid normal.
void vtkPolygon::ComputeNormal (int numPts, double *pts, double n[3])
{
int i;
double *v1, *v2, *v3;
double length;
double ax, ay, az;
double bx, by, bz;
// Because some polygon vertices are colinear, need to make sure
// first non-zero normal is found.
//
v1 = pts;
v2 = pts + 3;
v3 = pts + 6;
for (i=0; i<numPts-2; i++)
{
ax = v2[0] - v1[0]; ay = v2[1] - v1[1]; az = v2[2] - v1[2];
bx = v3[0] - v1[0]; by = v3[1] - v1[1]; bz = v3[2] - v1[2];
n[0] = (ay * bz - az * by);
n[1] = (az * bx - ax * bz);
n[2] = (ax * by - ay * bx);
length = sqrt (n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
if (length != 0.0)
{
n[0] /= length;
n[1] /= length;
n[2] /= length;
return;
}
else
{
v1 = v2;
v2 = v3;
v3 += 3;
}
} //over all points
}
//----------------------------------------------------------------------------
int vtkPolygon::EvaluatePosition(double x[3], double* closestPoint,
int& vtkNotUsed(subId), double pcoords[3],
double& minDist2, double *weights)
{
int i;
double p0[3], p10[3], l10, p20[3], l20, n[3], cp[3];
double ray[3];
this->ParameterizePolygon(p0, p10, l10, p20, l20, n);
this->InterpolateFunctions(x,weights);
vtkPlane::ProjectPoint(x,p0,n,cp);
for (i=0; i<3; i++)
{
ray[i] = cp[i] - p0[i];
}
pcoords[0] = vtkMath::Dot(ray,p10) / (l10*l10);
pcoords[1] = vtkMath::Dot(ray,p20) / (l20*l20);
if ( pcoords[0] >= 0.0 && pcoords[0] <= 1.0 &&
pcoords[1] >= 0.0 && pcoords[1] <= 1.0 &&
(this->PointInPolygon(cp, this->Points->GetNumberOfPoints(),
static_cast<vtkDoubleArray *>(
this->Points->GetData())->GetPointer(0),
this->GetBounds(),n) == VTK_POLYGON_INSIDE) )
{
if (closestPoint)
{
closestPoint[0] = cp[0];
closestPoint[1] = cp[1];
closestPoint[2] = cp[2];
minDist2 = vtkMath::Distance2BetweenPoints(x,closestPoint);
}
return 1;
}
// If here, point is outside of polygon, so need to find distance to boundary
//
else
{
double t, dist2;
int numPts;
double closest[3];
double pt1[3], pt2[3];
if (closestPoint)
{
numPts = this->Points->GetNumberOfPoints();
for (minDist2=VTK_DOUBLE_MAX,i=0; i<numPts; i++)
{
this->Points->GetPoint(i, pt1);
this->Points->GetPoint((i+1)%numPts, pt2);
dist2 = vtkLine::DistanceToLine(x, pt1, pt2, t, closest);
if ( dist2 < minDist2 )
{
closestPoint[0] = closest[0];
closestPoint[1] = closest[1];
closestPoint[2] = closest[2];
minDist2 = dist2;
}
}
}
return 0;
}
}
//----------------------------------------------------------------------------
void vtkPolygon::EvaluateLocation(int& vtkNotUsed(subId), double pcoords[3],
double x[3], double *weights)
{
int i;
double p0[3], p10[3], l10, p20[3], l20, n[3];
this->ParameterizePolygon(p0, p10, l10, p20, l20, n);
for (i=0; i<3; i++)
{
x[i] = p0[i] + pcoords[0]*p10[i] + pcoords[1]*p20[i];
}
this->InterpolateFunctions(x,weights);
}
//----------------------------------------------------------------------------
// Compute interpolation weights using 1/r**2 normalized sum or mean value
// coordinate.
void vtkPolygon::InterpolateFunctions(double x[3], double *weights)
{
// Compute interpolation weights using mean value coordinate.
if (this->UseMVCInterpolation)
{
this->InterpolateFunctionsUsingMVC(x, weights);
return;
}
// Compute interpolation weights using 1/r**2 normalized sum.
int i;
int numPts=this->Points->GetNumberOfPoints();
double sum, pt[3];
for (sum=0.0, i=0; i<numPts; i++)
{
this->Points->GetPoint(i, pt);
weights[i] = vtkMath::Distance2BetweenPoints(x,pt);
if ( weights[i] == 0.0 ) //exact hit
{
for (int j=0; j<numPts; j++)
{
weights[j] = 0.0;
}
weights[i] = 1.0;
return;
}
else
{
weights[i] = 1.0 / (weights[i]*weights[i]);
sum += weights[i];
}
}
for (i=0; i<numPts; i++)
{
weights[i] /= sum;
}
}
//----------------------------------------------------------------------------
// Compute interpolation weights using mean value coordinate.
void vtkPolygon::InterpolateFunctionsUsingMVC(double x[3], double *weights)
{
int numPts=this->Points->GetNumberOfPoints();
// Begin by initializing weights.
for (int i=0; i < numPts; i++)
{
weights[i] = static_cast<double>(0.0);
}
// create local array for storing point-to-vertex vectors and distances
double *dist = new double [numPts];
double *uVec = new double [3*numPts];
static const double eps = 0.00000001;
for (int i=0; i<numPts; i++)
{
double pt[3];
this->Points->GetPoint(i, pt);
// point-to-vertex vector
uVec[3*i] = pt[0] - x[0];
uVec[3*i+1] = pt[1] - x[1];
uVec[3*i+2] = pt[2] - x[2];
// distance
dist[i] = vtkMath::Norm(uVec+3*i);
// handle special case when the point is really close to a vertex
if (dist[i] < eps)
{
weights[i] = 1.0;
delete [] dist;
delete [] uVec;
return;
}
uVec[3*i] /= dist[i];
uVec[3*i+1] /= dist[i];
uVec[3*i+2] /= dist[i];
}
// Now loop over all vertices to compute weight
// w_i = ( tan(theta_i/2) + tan(theta_(i+1)/2) ) / dist_i
// To do consider the simplification of
// tan(alpha/2) = (1-cos(alpha))/sin(alpha)
// = (d0*d1 - cross(u0, u1))/(2*dot(u0,u1))
double *tanHalfTheta = new double [numPts];
for (int i = 0; i < numPts; i++)
{
int i1 = i+1;
if ( i1 == numPts )
{
i1 = 0;
}
double *u0 = uVec + 3*i;
double *u1 = uVec + 3*i1;
double l = sqrt(vtkMath::Distance2BetweenPoints(u0, u1));
double theta = 2.0*asin(l/2.0);
// special case where x lies on an edge
if (vtkMath::Pi() - theta < 0.001)
{
weights[i] = dist[i1] / (dist[i] + dist[i1]);
weights[i1] = 1 - weights[i];
delete [] dist;
delete [] uVec;
delete [] tanHalfTheta;
return;
}
tanHalfTheta[i] = tan(theta/2.0);
}
// Normal case
for (int i = 0; i < numPts; i++)
{
int i1 = i-1;
if ( i1 == -1 )
{
i1 = numPts-1;
}
weights[i] = (tanHalfTheta[i] + tanHalfTheta[i1]) / dist[i];
}
// clear memory
delete [] dist;
delete [] uVec;
delete [] tanHalfTheta;
// normalize weight
double sum = 0.0;
for (int i=0; i < numPts; i++)
{
sum += weights[i];
}
if (fabs(sum) < eps)
{
return;
}
for (int i=0; i < numPts; i++)
{
weights[i] /= sum;
}
return;
}
//----------------------------------------------------------------------------
void vtkPolygon::InterpolateDerivs(double pcoords[3], double *derivs)
{
(void)pcoords;
(void)derivs;
}
//----------------------------------------------------------------------------
// Create a local s-t coordinate system for a polygon. The point p0 is
// the origin of the local system, p10 is s-axis vector, and p20 is the
// t-axis vector. (These are expressed in the modelling coordinate system and
// are vectors of dimension [3].) The values l20 and l20 are the lengths of
// the vectors p10 and p20, and n is the polygon normal.
int vtkPolygon::ParameterizePolygon(double *p0, double *p10, double& l10,
double *p20,double &l20, double *n)
{
int i, j;
double s, t, p[3], p1[3], p2[3], sbounds[2], tbounds[2];
int numPts=this->Points->GetNumberOfPoints();
double x1[3], x2[3];
// This is a two pass process: first create a p' coordinate system
// that is then adjusted to insure that the polygon points are all in
// the range 0<=s,t<=1. The p' system is defined by the polygon normal,
// first vertex and the first edge.
//
this->ComputeNormal (this->Points,n);
this->Points->GetPoint(0, x1);
this->Points->GetPoint(1, x2);
for (i=0; i<3; i++)
{
p0[i] = x1[i];
p10[i] = x2[i] - x1[i];
}
vtkMath::Cross (n,p10,p20);
// Determine lengths of edges
//
if ( (l10=vtkMath::Dot(p10,p10)) == 0.0
|| (l20=vtkMath::Dot(p20,p20)) == 0.0 )
{
return 0;
}
// Now evalute all polygon points to determine min/max parametric
// coordinate values.
//
// first vertex has (s,t) = (0,0)
sbounds[0] = 0.0; sbounds[1] = 0.0;
tbounds[0] = 0.0; tbounds[1] = 0.0;
for(i=1; i<numPts; i++)
{
this->Points->GetPoint(i, x1);
for(j=0; j<3; j++)
{
p[j] = x1[j] - p0[j];
}
#ifdef BAD_WITH_NODEBUG
s = vtkMath::Dot(p,p10) / l10;
t = vtkMath::Dot(p,p20) / l20;
#endif
s = (p[0]*p10[0] + p[1]*p10[1] + p[2]*p10[2]) / l10;
t = (p[0]*p20[0] + p[1]*p20[1] + p[2]*p20[2]) / l20;
sbounds[0] = (s<sbounds[0]?s:sbounds[0]);
sbounds[1] = (s>sbounds[1]?s:sbounds[1]);
tbounds[0] = (t<tbounds[0]?t:tbounds[0]);
tbounds[1] = (t>tbounds[1]?t:tbounds[1]);
}
// Re-evaluate coordinate system
//
for (i=0; i<3; i++)
{
p1[i] = p0[i] + sbounds[1]*p10[i] + tbounds[0]*p20[i];
p2[i] = p0[i] + sbounds[0]*p10[i] + tbounds[1]*p20[i];
p0[i] = p0[i] + sbounds[0]*p10[i] + tbounds[0]*p20[i];
p10[i] = p1[i] - p0[i];
p20[i] = p2[i] - p0[i];
}
l10 = vtkMath::Norm(p10);
l20 = vtkMath::Norm(p20);
return 1;
}
#define VTK_POLYGON_CERTAIN 1
#define VTK_POLYGON_UNCERTAIN 0
#define VTK_POLYGON_RAY_TOL 1.e-03 //Tolerance for ray firing
#define VTK_POLYGON_MAX_ITER 10 //Maximum iterations for ray-firing
#define VTK_POLYGON_VOTE_THRESHOLD 2
#ifndef TRUE
#define FALSE 0
#define TRUE 1
#endif
//----------------------------------------------------------------------------
// Determine whether point is inside polygon. Function uses ray-casting
// to determine if point is inside polygon. Works for arbitrary polygon shape
// (e.g., non-convex). Returns 0 if point is not in polygon; 1 if it is.
// Can also return -1 to indicate degenerate polygon. Note: a point in
// bounding box check is NOT performed prior to in/out check. You may want
// to do this to improve performance.
int vtkPolygon::PointInPolygon (double x[3], int numPts, double *pts,
double bounds[6], double *n)
{
double *x1, *x2, xray[3], u, v;
double rayMag, mag=1, ray[3];
int testResult, rayOK, status, numInts, i;
int iterNumber;
int maxComp, comps[2];
int deltaVotes;
// do a quick bounds check
if ( x[0] < bounds[0] || x[0] > bounds[1] ||
x[1] < bounds[2] || x[1] > bounds[3] ||
x[2] < bounds[4] || x[2] > bounds[5])
{
return VTK_POLYGON_OUTSIDE;
}
//
// Define a ray to fire. The ray is a random ray normal to the
// normal of the face. The length of the ray is a function of the
// size of the face bounding box.
//
for (i=0; i<3; i++)
{
ray[i] = ( bounds[2*i+1] - bounds[2*i] )*1.1 +
fabs((bounds[2*i+1] + bounds[2*i])/2.0 - x[i]);
}
if ( (rayMag = vtkMath::Norm(ray)) == 0.0 )
{
return VTK_POLYGON_OUTSIDE;
}
// Get the maximum component of the normal.
//
if ( fabs(n[0]) > fabs(n[1]) )
{
if ( fabs(n[0]) > fabs(n[2]) )
{
maxComp = 0;
comps[0] = 1;
comps[1] = 2;
}
else
{
maxComp = 2;
comps[0] = 0;
comps[1] = 1;
}
}
else
{
if ( fabs(n[1]) > fabs(n[2]) )
{
maxComp = 1;
comps[0] = 0;
comps[1] = 2;
}
else
{
maxComp = 2;
comps[0] = 0;
comps[1] = 1;
}
}
// Check that max component is non-zero
//
if ( n[maxComp] == 0.0 )
{
return VTK_POLYGON_FAILURE;
}
// Enough information has been acquired to determine the random ray.
// Random rays are generated until one is satisfactory (i.e.,
// produces a ray of non-zero magnitude). Also, since more than one
// ray may need to be fired, the ray-firing occurs in a large loop.
//
// The variable iterNumber counts the number of iterations and is
// limited by the defined variable VTK_POLYGON_MAX_ITER.
//
// The variable deltaVotes keeps track of the number of votes for
// "in" versus "out" of the face. When delta_vote > 0, more votes
// have counted for "in" than "out". When delta_vote < 0, more votes
// have counted for "out" than "in". When the delta_vote exceeds or
// equals the defined variable VTK_POLYGON_VOTE_THRESHOLD, than the
// appropriate "in" or "out" status is returned.
//
for (deltaVotes = 0, iterNumber = 1;
(iterNumber < VTK_POLYGON_MAX_ITER)
&& (abs(deltaVotes) < VTK_POLYGON_VOTE_THRESHOLD);
iterNumber++)
{
//
// Generate ray
//
for (rayOK = FALSE; rayOK == FALSE; )
{
ray[comps[0]] = vtkMath::Random(-rayMag, rayMag);
ray[comps[1]] = vtkMath::Random(-rayMag, rayMag);
ray[maxComp] = -(n[comps[0]]*ray[comps[0]] +
n[comps[1]]*ray[comps[1]]) / n[maxComp];
if ( (mag = vtkMath::Norm(ray)) > rayMag*VTK_TOL )
{
rayOK = TRUE;
}
}
// The ray must be appropriately sized.
//
for (i=0; i<3; i++)
{
xray[i] = x[i] + (rayMag/mag)*ray[i];
}
// The ray may now be fired against all the edges
//
for (numInts=0, testResult=VTK_POLYGON_CERTAIN, i=0; i<numPts; i++)
{
x1 = pts + 3*i;
x2 = pts + 3*((i+1)%numPts);
// Fire the ray and compute the number of intersections. Be careful
// of degenerate cases (e.g., ray intersects at vertex).
//
if ((status=vtkLine::Intersection(x,xray,x1,x2,u,v)) == VTK_POLYGON_INTERSECTION)
{
if ( (VTK_POLYGON_RAY_TOL < v) && (v < 1.0-VTK_POLYGON_RAY_TOL) )
{
numInts++;
}
else
{
testResult = VTK_POLYGON_UNCERTAIN;
}
}
else if ( status == VTK_POLYGON_ON_LINE )
{
testResult = VTK_POLYGON_UNCERTAIN;
}
}
if ( testResult == VTK_POLYGON_CERTAIN )
{
if ( (numInts % 2) == 0)
{
--deltaVotes;
}
else
{
++deltaVotes;
}
}
} //try another ray
// If the number of intersections is odd, the point is in the polygon.
//
if ( deltaVotes < 0 )
{
return VTK_POLYGON_OUTSIDE;
}
else
{
return VTK_POLYGON_INSIDE;
}
}
#define VTK_POLYGON_TOLERANCE 1.0e-06
//----------------------------------------------------------------------------
// Triangulate polygon.
//
int vtkPolygon::Triangulate(vtkIdList *outTris)
{
int success;
double *bounds, d;
bounds = this->GetBounds();
d = sqrt((bounds[1]-bounds[0])*(bounds[1]-bounds[0]) +
(bounds[3]-bounds[2])*(bounds[3]-bounds[2]) +
(bounds[5]-bounds[4])*(bounds[5]-bounds[4]));
this->Tolerance = VTK_POLYGON_TOLERANCE * d;
this->SuccessfulTriangulation = 1;
this->Tris->Reset();
success = this->EarCutTriangulation();
if ( !success ) //degenerate triangle encountered
{
vtkDebugMacro(<< "Degenerate polygon encountered during triangulation");
}
outTris->DeepCopy(this->Tris);
return success;
}
//----------------------------------------------------------------------------
// Split into non-degenerate polygons prior to triangulation
//
int vtkPolygon::NonDegenerateTriangulate(vtkIdList *outTris)
{
double pt[3], bounds[6];
vtkIdType ptId, numPts;
// ComputeBounds does not give the correct bounds
// So we do it manually
bounds[0]= VTK_DOUBLE_MAX;
bounds[1]=-VTK_DOUBLE_MAX;
bounds[2]= VTK_DOUBLE_MAX;
bounds[3]=-VTK_DOUBLE_MAX;
bounds[4]= VTK_DOUBLE_MAX;
bounds[5]=-VTK_DOUBLE_MAX;
numPts = this->GetNumberOfPoints();
for(int i=0; i<numPts; i++)
{
this->Points->GetPoint(i,pt);
if(pt[0] < bounds[0]) { bounds[0] = pt[0]; }
if(pt[1] < bounds[2]) { bounds[2] = pt[1]; }
if(pt[2] < bounds[4]) { bounds[4] = pt[2]; }
if(pt[0] > bounds[1]) { bounds[1] = pt[0]; }
if(pt[1] > bounds[3]) { bounds[3] = pt[1]; }
if(pt[2] > bounds[5]) { bounds[5] = pt[2]; }
}
outTris->Reset();
outTris->Allocate(3*(2*numPts-4));
vtkPoints *newPts = vtkPoints::New();
newPts->Allocate(numPts);
vtkMergePoints *mergePoints = vtkMergePoints::New();
mergePoints->InitPointInsertion(newPts,bounds);
mergePoints->SetDivisions(10,10,10);
vtkIdTypeArray *matchingIds = vtkIdTypeArray::New();
matchingIds->SetNumberOfTuples(numPts);
int numDuplicatePts = 0;
for(int i=0; i<numPts; i++)
{
this->Points->GetPoint(i,pt);
if(mergePoints->InsertUniquePoint(pt,ptId))
{
matchingIds->SetValue(i,ptId+numDuplicatePts);
}
else
{
matchingIds->SetValue(i,ptId+numDuplicatePts);
numDuplicatePts++;
}
}
mergePoints->Delete();
newPts->Delete();
int numPtsRemoved = 0;
vtkIdType tri[3];
while(numPtsRemoved < numPts)
{
vtkIdType start = 0;
vtkIdType end = numPts-1;
for(;start<numPts;start++)
{
if(matchingIds->GetValue(start) >= 0)
{
break;
}
}
if(start >= end)
{
vtkErrorMacro("ERROR: start >= end");
break;
}
for(int i=start; i<numPts; i++)
{
if(matchingIds->GetValue(i) < 0)
{
continue;
}
if(matchingIds->GetValue(i) != i)
{
start = (matchingIds->GetValue(i) + 1) % numPts;
end = i;
while(matchingIds->GetValue(start) < 0)
{
start++;
}
break;
}
}
vtkPolygon *polygon = vtkPolygon::New();
polygon->Points->SetDataTypeToDouble();
int numPolygonPts = start<end ? end-start+1 : end-start+numPts+1;
for(int i=0; i<numPolygonPts; i++)
{
ptId = start+i;
if(matchingIds->GetValue(ptId) >= 0)
{
numPtsRemoved++;
matchingIds->SetValue(ptId,-1);
polygon->PointIds->InsertNextId(ptId);
polygon->Points->InsertNextPoint(this->Points->GetPoint(ptId));
}
}
vtkIdList *outTriangles = vtkIdList::New();
outTriangles->Allocate(3*(2*polygon->GetNumberOfPoints()-4));
polygon->Triangulate(outTriangles);
int outNumTris = outTriangles->GetNumberOfIds();
for(int i=0; i<outNumTris; i+=3)
{
tri[0] = outTriangles->GetId(i);
tri[1] = outTriangles->GetId(i+1);
tri[2] = outTriangles->GetId(i+2);
tri[0] = polygon->PointIds->GetId(tri[0]);
tri[1] = polygon->PointIds->GetId(tri[1]);
tri[2] = polygon->PointIds->GetId(tri[2]);
outTris->InsertNextId(tri[0]);
outTris->InsertNextId(tri[1]);
outTris->InsertNextId(tri[2]);
}
polygon->Delete();
outTriangles->Delete();
}
matchingIds->Delete();
return 1;
}
//----------------------------------------------------------------------------
// Special structures for building loops. This is a double-linked list.
typedef struct _vtkPolyVertex
{
int id;
double x[3];
double measure;
_vtkPolyVertex* next;
_vtkPolyVertex* previous;
} vtkLocalPolyVertex;
class vtkPolyVertexList { //structure to support triangulation
public:
vtkPolyVertexList(vtkIdList *ptIds, vtkPoints *pts, double tol2);
~vtkPolyVertexList();
int ComputeNormal();
double ComputeMeasure(vtkLocalPolyVertex *vtx);
void RemoveVertex(int i, vtkIdList *, vtkPriorityQueue *);
int CanRemoveVertex(int id, double tol);
int NumberOfVerts;
vtkLocalPolyVertex *Array;
vtkLocalPolyVertex *Head;
double Normal[3];
};
//----------------------------------------------------------------------------
// tolerance is squared
vtkPolyVertexList::vtkPolyVertexList(vtkIdList *ptIds, vtkPoints *pts,
double tol2)
{
int numVerts = ptIds->GetNumberOfIds();
this->NumberOfVerts = numVerts;
this->Array = new vtkLocalPolyVertex [numVerts];
int i;