File: vtkImplicitSelectionLoop.cxx

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 205,984 kB
  • sloc: cpp: 2,336,570; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 181; javascript: 165; objc: 153; tcl: 59
file content (200 lines) | stat: -rw-r--r-- 6,022 bytes parent folder | download
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
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkImplicitSelectionLoop.h"

#include "vtkDoubleArray.h"
#include "vtkLine.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPlane.h"
#include "vtkPoints.h"
#include "vtkPolygon.h"

VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkImplicitSelectionLoop);
vtkCxxSetObjectMacro(vtkImplicitSelectionLoop, Loop, vtkPoints);

//------------------------------------------------------------------------------
// Instantiate object with no initial loop.
vtkImplicitSelectionLoop::vtkImplicitSelectionLoop()
{
  this->Loop = nullptr;
  this->AutomaticNormalGeneration = 1;
  this->Normal[0] = 0.0;
  this->Normal[1] = 0.0;
  this->Normal[2] = 1.0;
  this->Polygon = vtkPolygon::New();
}

//------------------------------------------------------------------------------
vtkImplicitSelectionLoop::~vtkImplicitSelectionLoop()
{
  if (this->Loop)
  {
    this->Loop->Delete();
  }
  this->Polygon->Delete();
  this->Polygon = nullptr;
}

//------------------------------------------------------------------------------
#define VTK_DELTA 0.0001
// Generate plane equations only once to avoid a lot of extra work
void vtkImplicitSelectionLoop::Initialize()
{
  int i, numPts;
  double x[3], xProj[3];

  numPts = this->Loop->GetNumberOfPoints();
  this->Polygon->Points->SetDataTypeToDouble();
  this->Polygon->Points->SetNumberOfPoints(numPts);

  if (this->AutomaticNormalGeneration)
  {
    // Make sure points define a loop with a normal
    vtkPolygon::ComputeNormal(this->Loop, this->Normal);
    if (this->Normal[0] == 0.0 && this->Normal[1] == 0.0 && this->Normal[2] == 0.0)
    {
      vtkErrorMacro(<< "Cannot determine inside/outside of loop");
    }
  }

  // Determine origin point by taking average
  this->Origin[0] = this->Origin[1] = this->Origin[2] = 0.0;
  for (i = 0; i < numPts; i++)
  {
    this->Loop->GetPoint(i, x);
    this->Origin[0] += x[0];
    this->Origin[1] += x[1];
    this->Origin[2] += x[2];
  }
  this->Origin[0] /= numPts;
  this->Origin[1] /= numPts;
  this->Origin[2] /= numPts;

  // Project points onto plane generating new coordinates
  for (i = 0; i < numPts; i++)
  {
    this->Loop->GetPoint(i, x);
    vtkPlane::ProjectPoint(x, this->Origin, this->Normal, xProj);
    this->Polygon->Points->SetPoint(i, xProj);
  }

  this->Polygon->GetBounds(this->Bounds);
  this->DeltaX = VTK_DELTA * (this->Bounds[1] - this->Bounds[0]);
  this->DeltaY = VTK_DELTA * (this->Bounds[3] - this->Bounds[2]);
  this->DeltaZ = VTK_DELTA * (this->Bounds[5] - this->Bounds[4]);
  this->InitializationTime.Modified();
}

//------------------------------------------------------------------------------
// Evaluate plane equations. Return smallest absolute value.
double vtkImplicitSelectionLoop::EvaluateFunction(double x[3])
{
  int i, numPts;
  double xProj[3];
  double t, dist2, minDist2, closest[3];
  int inside = 0;

  if (this->InitializationTime < this->GetMTime())
  {
    this->Initialize();
  }
  // Initialize may change the number of points
  numPts = this->Polygon->Points->GetNumberOfPoints();

  // project point onto plane
  vtkPlane::ProjectPoint(x, this->Origin, this->Normal, xProj);

  // determine whether it's in the selection loop and then evaluate point
  // in polygon only if absolutely necessary.
  if (xProj[0] >= this->Bounds[0] && xProj[0] <= this->Bounds[1] && xProj[1] >= this->Bounds[2] &&
    xProj[1] <= this->Bounds[3] && xProj[2] >= this->Bounds[4] && xProj[2] <= this->Bounds[5] &&
    vtkPolygon::PointInPolygon(xProj, numPts,
      vtkArrayDownCast<vtkDoubleArray>(this->Polygon->Points->GetData())->GetPointer(0),
      this->Bounds, this->Normal) == 1)
  {
    inside = 1;
  }

  // determine distance to boundary
  for (minDist2 = VTK_DOUBLE_MAX, i = 0; i < numPts; i++)
  {
    double p1[3], p2[3];
    this->Polygon->Points->GetPoint(i, p1);
    this->Polygon->Points->GetPoint((i + 1) % numPts, p2);
    dist2 = vtkLine::DistanceToLine(xProj, p1, p2, t, closest);
    if (dist2 < minDist2)
    {
      minDist2 = dist2;
    }
  }

  minDist2 = sqrt(minDist2);
  return (inside ? -minDist2 : minDist2);
}

//------------------------------------------------------------------------------
// Evaluate gradient of the implicit function. Use a numerical scheme: evaluate
// the function at four points (O,O+dx,O+dy,O+dz) and approximate the gradient.
// It's damn slow.
void vtkImplicitSelectionLoop::EvaluateGradient(double x[3], double n[3])
{
  double xp[3], yp[3], zp[3], g0, gx, gy, gz;
  int i;

  g0 = this->EvaluateFunction(x); // side-effect is to compute DeltaX, Y, and Z

  for (i = 0; i < 3; i++)
  {
    xp[i] = yp[i] = zp[i] = x[i];
  }
  xp[0] += this->DeltaX;
  yp[1] += this->DeltaY;
  zp[2] += this->DeltaZ;

  gx = this->EvaluateFunction(xp);
  gy = this->EvaluateFunction(yp);
  gz = this->EvaluateFunction(zp);

  n[0] = (gx - g0) / this->DeltaX;
  n[1] = (gy - g0) / this->DeltaY;
  n[2] = (gz - g0) / this->DeltaZ;
}

//------------------------------------------------------------------------------
vtkMTimeType vtkImplicitSelectionLoop::GetMTime()
{
  vtkMTimeType mTime = this->vtkImplicitFunction::GetMTime();
  vtkMTimeType time;

  if (this->Loop != nullptr)
  {
    time = this->Loop->GetMTime();
    mTime = (time > mTime ? time : mTime);
  }

  return mTime;
}

//------------------------------------------------------------------------------
void vtkImplicitSelectionLoop::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);

  if (this->Loop)
  {
    os << indent << "Loop of " << this->Loop->GetNumberOfPoints() << " points defined\n";
  }
  else
  {
    os << indent << "Loop not defined\n";
  }

  os << indent
     << "Automatic Normal Generation: " << (this->AutomaticNormalGeneration ? "On\n" : "Off\n");

  os << indent << "Normal: (" << this->Normal[0] << ", " << this->Normal[1] << ", "
     << this->Normal[2] << ")\n";
}
VTK_ABI_NAMESPACE_END