forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkOctreePointLocatorNode.cxx
604 lines (540 loc) · 16.4 KB
/
vtkOctreePointLocatorNode.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkOctreePointLocatorNode.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.
=========================================================================*/
/*----------------------------------------------------------------------------
Copyright (c) Sandia Corporation
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/
#include "vtkOctreePointLocatorNode.h"
#include "vtkBox.h"
#include "vtkCell.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPlanesIntersection.h"
#include "vtkPoints.h"
vtkStandardNewMacro(vtkOctreePointLocatorNode);
//----------------------------------------------------------------------------
vtkOctreePointLocatorNode::vtkOctreePointLocatorNode() :
NumberOfPoints(0), Children(NULL), ID(-1), MinID(-1)
{
// set the min and max data value since we won't know it for a while
for(int i=0;i<3;i++)
{
this->MinDataBounds[i] = VTK_DOUBLE_MAX;
this->MaxDataBounds[i] = VTK_DOUBLE_MIN;
}
}
//----------------------------------------------------------------------------
vtkOctreePointLocatorNode::~vtkOctreePointLocatorNode()
{
this->DeleteChildNodes();
}
//----------------------------------------------------------------------------
void vtkOctreePointLocatorNode::CreateChildNodes()
{
if(!this->Children)
{
int i;
double midpoint[3];
for(i=0;i<3;i++)
{
midpoint[i] = (this->MinBounds[i]+this->MaxBounds[i])*.5;
}
this->Children = new vtkOctreePointLocatorNode*[8];
for(i=0;i<8;i++)
{
this->Children[i] = vtkOctreePointLocatorNode::New();
double NewMin[3], NewMax[3];
for(int j=0;j<3;j++)
{
if(!((i>>j) & 1))
{
NewMin[j] = this->MinBounds[j];
NewMax[j] = midpoint[j];
}
else
{
NewMin[j] = midpoint[j];
NewMax[j] = this->MaxBounds[j];
}
}
this->Children[i]->SetMinBounds(NewMin);
this->Children[i]->SetMaxBounds(NewMax);
}
}
}
//----------------------------------------------------------------------------
void vtkOctreePointLocatorNode::DeleteChildNodes()
{
if(this->Children)
{
for(int i=0;i<8;i++)
{
this->Children[i]->Delete();
}
delete []this->Children;
this->Children = 0;
}
}
//----------------------------------------------------------------------------
vtkOctreePointLocatorNode* vtkOctreePointLocatorNode::GetChild(int i)
{
if(this->Children)
{
return this->Children[i];
}
return 0;
}
//----------------------------------------------------------------------------
int vtkOctreePointLocatorNode::GetSubOctantIndex(
double* point, int CheckContainment)
{
int i, index = 0;
if(CheckContainment)
{
for(i=0;i<3;i++)
{
if(point[i] <= this->MinBounds[i] || point[i] > this->MaxBounds[i])
{
return -1;
}
}
}
for(i=0;i<3;i++)
{
if(point[i] > (this->MinBounds[i]+this->MaxBounds[i])*.5)
{
index += 1 << i;
}
}
return index;
}
//----------------------------------------------------------------------------
void vtkOctreePointLocatorNode::ComputeOctreeNodeInformation(
vtkOctreePointLocatorNode* Parent, int & NextLeafId, int & NextMinId,
float* coordinates)
{
this->MinID = NextMinId;
if(this->Children)
{
int i;
for(i=0;i<8;i++)
{
this->Children[i]->ComputeOctreeNodeInformation(
this, NextLeafId, NextMinId, coordinates);
}
// a non-leaf region can get its data bounds from its children...
this->SetMinDataBounds(this->Children[0]->GetMinDataBounds());
this->SetMaxDataBounds(this->Children[0]->GetMaxDataBounds());
for(i=1;i<8;i++)
{
double* min = this->Children[i]->GetMinDataBounds();
double* max = this->Children[i]->GetMaxDataBounds();
for(int j=0;j<3;j++)
{
if(min[j] < this->MinDataBounds[j])
{
this->MinDataBounds[j] = min[j];
}
if(max[j] > this->MaxDataBounds[j])
{
this->MaxDataBounds[j] = max[j];
}
}
}
}
else
{
this->ID = NextLeafId;
NextLeafId++;
NextMinId = this->MinID+this->NumberOfPoints;
if(this->NumberOfPoints == 0)
{
// since there are no points in this, set the data bounds
// such that they won't affect anything else
this->SetMinDataBounds(Parent->GetMaxBounds());
this->SetMaxDataBounds(Parent->GetMinBounds());
}
else
{
int i;
float* coordptr = coordinates+3*this->MinID;
for(i=0;i<3;i++)
{
this->MinDataBounds[i] = this->MaxDataBounds[i] = coordptr[i];
}
for(i=1;i<this->NumberOfPoints;i++)
{
coordptr += 3;
for(int j=0;j<3;j++)
{
if(coordptr[j] < this->MinDataBounds[j])
{
this->MinDataBounds[j] = coordptr[j];
}
else if(coordptr[j] > this->MaxDataBounds[j])
{
this->MaxDataBounds[j] = coordptr[j];
}
}
}
}
}
}
//----------------------------------------------------------------------------
void vtkOctreePointLocatorNode::SetBounds(
double xMin,double xMax,double yMin,double yMax,double zMin,double zMax)
{
this->MinBounds[0] = xMin; this->MaxBounds[0] = xMax;
this->MinBounds[1] = yMin; this->MaxBounds[1] = yMax;
this->MinBounds[2] = zMin; this->MaxBounds[2] = zMax;
}
//----------------------------------------------------------------------------
void vtkOctreePointLocatorNode::GetBounds(double *b) const
{
b[0] = this->MinBounds[0]; b[1] = this->MaxBounds[0];
b[2] = this->MinBounds[1]; b[3] = this->MaxBounds[1];
b[4] = this->MinBounds[2]; b[5] = this->MaxBounds[2];
}
//----------------------------------------------------------------------------
void vtkOctreePointLocatorNode::SetDataBounds(
double xMin,double xMax,double yMin,double yMax,double zMin,double zMax)
{
this->MinDataBounds[0] = xMin; this->MaxDataBounds[0] = xMax;
this->MinDataBounds[1] = yMin; this->MaxDataBounds[1] = yMax;
this->MinDataBounds[2] = zMin; this->MaxDataBounds[2] = zMax;
}
//----------------------------------------------------------------------------
void vtkOctreePointLocatorNode::GetDataBounds(double *b) const
{
b[0] = this->MinDataBounds[0]; b[1] = this->MaxDataBounds[0];
b[2] = this->MinDataBounds[1]; b[3] = this->MaxDataBounds[1];
b[4] = this->MinDataBounds[2]; b[5] = this->MaxDataBounds[2];
}
//----------------------------------------------------------------------------
// Squared distance from any point anywhere to the boundary of spatial region
//
double vtkOctreePointLocatorNode::GetDistance2ToBoundary(
double x, double y, double z, vtkOctreePointLocatorNode* top,
int useDataBounds=0)
{
return this->_GetDistance2ToBoundary(x, y, z, NULL, 0, top, useDataBounds);
}
//----------------------------------------------------------------------------
// Squared distance from any point anywhere to the boundary of spatial region,
// and give me the point on the boundary closest to this point.
//
double vtkOctreePointLocatorNode::GetDistance2ToBoundary(
double x, double y, double z, double *p,
vtkOctreePointLocatorNode* top, int useDataBounds=0)
{
return this->_GetDistance2ToBoundary(x, y, z, p, 0, top, useDataBounds);
}
//----------------------------------------------------------------------------
// The point is inside the region, and I want the distance (squared)
// to the closest "interior" wall, one that is not an outer boundary of
// the entire space.
//
double vtkOctreePointLocatorNode::GetDistance2ToInnerBoundary(
double x, double y, double z, vtkOctreePointLocatorNode* top)
{
return this->_GetDistance2ToBoundary(x, y, z, NULL, 1, top, 0);
}
//----------------------------------------------------------------------------
double vtkOctreePointLocatorNode::_GetDistance2ToBoundary(
double x, double y, double z, // from this point
double *p, // set to point on boundary that is closest
int innerBoundaryOnly, // ignore boundaries on "outside"
vtkOctreePointLocatorNode* top, // the top of the octree
int useDataBounds=0) // use bounds of data within region instead
{
double minDistance, dist;
double edgePt[3];
double cornerPt[3];
double pt3[3];
double *min, *max;
if (useDataBounds)
{
min = this->MinDataBounds; max = this->MaxDataBounds; // data inside region
}
else
{
min = this->MinBounds; max = this->MaxBounds; // region itself
}
double *outerBoundaryMin=NULL;
double *outerBoundaryMax=NULL;
if (innerBoundaryOnly)
{
// We want the distance to the nearest inner boundary, because we
// are only interested in boundaries such that there may be points on
// the other side. This option only makes sense when the point supplied
// is inside this node (region).
outerBoundaryMin = (useDataBounds? top->MinDataBounds : top->MinBounds);
outerBoundaryMax = (useDataBounds? top->MaxDataBounds : top->MaxBounds);
}
double xmax = max[0]; double ymax = max[1]; double zmax = max[2];
double xmin = min[0]; double ymin = min[1]; double zmin = min[2];
int xless = (x < xmin);
int xmore = (x > xmax);
int yless = (y < ymin);
int ymore = (y > ymax);
int zless = (z < zmin);
int zmore = (z > zmax);
int withinX = !xless && !xmore;
int withinY = !yless && !ymore;
int withinZ = !zless && !zmore;
int mindim=0;
if (withinX && withinY && withinZ) // point is inside the box
{
if (!innerBoundaryOnly)
{
minDistance = x - xmin;
mindim = 0;
if ((dist = xmax - x) < minDistance)
{
mindim = 1;
minDistance = dist;
}
if ((dist = y - ymin) < minDistance)
{
mindim = 2;
minDistance = dist;
}
if ((dist = ymax - y) < minDistance)
{
mindim = 3;
minDistance = dist;
}
if ((dist = z - zmin) < minDistance)
{
mindim = 4;
minDistance = dist;
}
if ((dist = zmax - z) < minDistance)
{
mindim = 5;
minDistance = dist;
}
}
else
{
int first = 1;
minDistance = VTK_LARGE_FLOAT; // Suppresses warning message.
if ((xmin != outerBoundaryMin[0]) &&
(((dist = x - xmin) < minDistance) || first))
{
mindim = 0;
minDistance = dist;
first = 0;
}
if ((xmax != outerBoundaryMax[0]) &&
(((dist = xmax - x) < minDistance) || first))
{
mindim = 1;
minDistance = dist;
first = 0;
}
if ((ymin != outerBoundaryMin[1]) &&
(((dist = y - ymin) < minDistance) || first))
{
mindim = 2;
minDistance = dist;
first = 0;
}
if ((ymax != outerBoundaryMax[1]) &&
(((dist = ymax - y) < minDistance) || first))
{
mindim = 3;
minDistance = dist;
first = 0;
}
if ((zmin != outerBoundaryMin[2]) &&
(((dist = z - zmin) < minDistance) || first))
{
mindim = 4;
minDistance = dist;
first = 0;
}
if ((zmax != outerBoundaryMax[2]) &&
(((dist = zmax - z) < minDistance) || first))
{
mindim = 5;
minDistance = dist;
}
}
// if there are no inner boundaries we dont want to square.
if(minDistance != VTK_LARGE_FLOAT)
{
minDistance *= minDistance;
}
if (p)
{
p[0] = x; p[1] = y; p[2] = z;
if (mindim == 0)
{
p[0] = xmin;
}
else if (mindim == 1)
{
p[0] = xmax;
}
else if (mindim == 2)
{
p[1] = ymin;
}
else if (mindim == 3)
{
p[1] = ymax;
}
else if (mindim == 4)
{
p[2] = zmin;
}
else if (mindim == 5)
{
p[2] = zmax;
}
}
}
else if (withinX && withinY) // point projects orthogonally to a face
{
minDistance = (zless ? zmin - z : z - zmax);
minDistance *= minDistance;
if (p)
{
p[0] = x; p[1] = y;
p[2] = (zless ? zmin : zmax);
}
}
else if (withinX && withinZ)
{
minDistance = (yless ? ymin - y : y - ymax);
minDistance *= minDistance;
if (p)
{
p[0] = x; p[2] = z;
p[1] = (yless ? ymin : ymax);
}
}
else if (withinY && withinZ)
{
minDistance = (xless ? xmin - x : x - xmax);
minDistance *= minDistance;
if (p)
{
p[1] = y; p[2] = z;
p[0] = (xless ? xmin: xmax);
}
}
else if (withinX || withinY || withinZ) // point is closest to an edge
{
edgePt[0] = (withinX ? x : (xless ? xmin : xmax));
edgePt[1] = (withinY ? y : (yless ? ymin : ymax));
edgePt[2] = (withinZ ? z : (zless ? zmin : zmax));
pt3[0] = x; pt3[1] = y; pt3[2] = z;
minDistance = vtkMath::Distance2BetweenPoints(pt3, edgePt);
if (p)
{
p[0] = edgePt[0]; p[1] = edgePt[1]; p[2] = edgePt[2];
}
}
else // point is closest to a corner
{
cornerPt[0] = (xless ? xmin : xmax);
cornerPt[1] = (yless ? ymin : ymax);
cornerPt[2] = (zless ? zmin : zmax);
pt3[0] = x; pt3[1] = y; pt3[2] = z;
minDistance = vtkMath::Distance2BetweenPoints(pt3, cornerPt);
if (p)
{
p[0] = cornerPt[0]; p[1] = cornerPt[1]; p[2] = cornerPt[2];
}
}
return minDistance;
}
//----------------------------------------------------------------------------
int vtkOctreePointLocatorNode::ContainsPoint(double x, double y, double z,
int useDataBounds=0)
{
double *min, *max;
if (useDataBounds)
{
min = this->MinDataBounds;
max = this->MaxDataBounds;
}
else
{
min = this->MinBounds;
max = this->MaxBounds;
}
if( (min[0] >= x) ||
(max[0] < x) ||
(min[1] >= y) ||
(max[1] < y) ||
(min[2] >= z) ||
(max[2] < z))
{
return 0;
}
else
{
return 1;
}
}
//----------------------------------------------------------------------------
int vtkOctreePointLocatorNode::IntersectsRegion(
vtkPlanesIntersection *pi, int useDataBounds)
{
double xMin, xMax, yMin, yMax, zMin, zMax;
vtkPoints *box = vtkPoints::New();
box->SetNumberOfPoints(8);
double *min, *max;
if (useDataBounds)
{
min = this->MinDataBounds;
max = this->MaxDataBounds;
}
else
{
min = this->MinBounds;
max = this->MaxBounds;
}
xMin = min[0]; xMax = max[0];
yMin = min[1]; yMax = max[1];
zMin = min[2]; zMax = max[2];
box->SetPoint(0, xMax, yMin, zMax);
box->SetPoint(1, xMax, yMin, zMin);
box->SetPoint(2, xMax, yMax, zMin);
box->SetPoint(3, xMax, yMax, zMax);
box->SetPoint(4, xMin, yMin, zMax);
box->SetPoint(5, xMin, yMin, zMin);
box->SetPoint(6, xMin, yMax, zMin);
box->SetPoint(7, xMin, yMax, zMax);
int intersects = pi->IntersectsRegion(box);
box->Delete();
return intersects;
}
//----------------------------------------------------------------------------
void vtkOctreePointLocatorNode::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "NumberOfPoints: " << this->NumberOfPoints << endl;
os << indent << "Children: " << this->Children << endl;
os << indent << "ID: " << this->ID << endl;
os << indent << "MinID: " << this->MinID << endl;
os << indent << "MinBounds: " << this->MinBounds[0] << " "
<< this->MinBounds[1] << " " << this->MinBounds[2] << endl;
os << indent << "MaxBounds: " << this->MaxBounds[0] << " "
<< this->MaxBounds[1] << " " << this->MaxBounds[2] << endl;
os << indent << "MinDataBounds: " << this->MinDataBounds[0] << " "
<< this->MinDataBounds[1] << " " << this->MinDataBounds[2] << endl;
os << indent << "MaxDataBounds: " << this->MaxDataBounds[0] << " "
<< this->MaxDataBounds[1] << " " << this->MaxDataBounds[2] << endl;
}