VTK  9.0.20200918
vtkIntersectionCounter.h
Go to the documentation of this file.
1 
19 #ifndef vtkIntersectionCounter_h
20 #define vtkIntersectionCounter_h
21 
22 #include "vtkCommonDataModelModule.h" // For export macro
23 #include "vtkSystemIncludes.h"
24 #include <algorithm> // for sorting
25 #include <vector> // for implementation
26 
27 // class VTKCOMMONDATAMODEL_EXPORT vtkIntersectionCounter
28 
30 {
31 public:
33 
38  : Tolerance(0.0001)
39  {
40  }
41  vtkIntersectionCounter(double tol, double length)
42  {
43  this->Tolerance = (length > 0.0 ? (tol / length) : 0.0);
44  }
46 
50  void SetTolerance(double tol) { this->Tolerance = (tol < 0.0 ? 0.0001 : tol); }
51  double GetTolerance() { return this->Tolerance; }
52 
56  void AddIntersection(double t) { IntsArray.push_back(t); }
57 
61  void Reset() { IntsArray.clear(); }
62 
69  {
70  int size = static_cast<int>(IntsArray.size());
71 
72  // Fast check for trivial cases
73  if (size <= 1)
74  {
75  return size; // 0 or 1
76  }
77 
78  // Need to work harder: sort and then count the intersections
79  std::sort(IntsArray.begin(), IntsArray.end());
80 
81  // If here, there is at least one intersection, and two inserted
82  // intersection points
83  int numInts = 1;
84  std::vector<double>::iterator i0 = IntsArray.begin();
85  std::vector<double>::iterator i1 = i0 + 1;
86 
87  // Now march through sorted array counting "separated" intersections
88  while (i1 != IntsArray.end())
89  {
90  if ((*i1 - *i0) > this->Tolerance)
91  {
92  numInts++;
93  i0 = i1;
94  }
95  i1++;
96  }
97 
98  return numInts;
99  }
100 
101 protected:
102  double Tolerance;
103  std::vector<double> IntsArray;
104 
105 }; // vtkIntersectionCounter
106 
107 #endif
108 // VTK-HeaderTest-Exclude: vtkIntersectionCounter.h
vtkIntersectionCounter::Tolerance
double Tolerance
Definition: vtkIntersectionCounter.h:102
vtkIntersectionCounter::vtkIntersectionCounter
vtkIntersectionCounter(double tol, double length)
Definition: vtkIntersectionCounter.h:41
vtkIntersectionCounter::AddIntersection
void AddIntersection(double t)
Add an intersection given by parametric coordinate t.
Definition: vtkIntersectionCounter.h:56
vtkX3D::length
Definition: vtkX3D.h:399
vtkIntersectionCounter::SetTolerance
void SetTolerance(double tol)
Set/Get the intersection tolerance.
Definition: vtkIntersectionCounter.h:50
vtkX3D::size
Definition: vtkX3D.h:259
vtkIntersectionCounter::Reset
void Reset()
Reset the intersection process.
Definition: vtkIntersectionCounter.h:61
vtkIntersectionCounter::GetTolerance
double GetTolerance()
Definition: vtkIntersectionCounter.h:51
vtkIntersectionCounter::IntsArray
std::vector< double > IntsArray
Definition: vtkIntersectionCounter.h:103
vtkIntersectionCounter::vtkIntersectionCounter
vtkIntersectionCounter()
This tolerance must be converted to parametric space.
Definition: vtkIntersectionCounter.h:37
vtkIntersectionCounter::CountIntersections
int CountIntersections()
Returns number of intersections (even number of intersections, outside or odd number of intersections...
Definition: vtkIntersectionCounter.h:68
vtkSystemIncludes.h
vtkIntersectionCounter
Fast simple class for dealing with ray intersections.
Definition: vtkIntersectionCounter.h:29