VTK  9.3.20240418
vtkIntersectionCounter.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
21 #ifndef vtkIntersectionCounter_h
22 #define vtkIntersectionCounter_h
23 
24 #include "vtkCommonDataModelModule.h" // For export macro
25 #include "vtkSystemIncludes.h"
26 #include <algorithm> // for sorting
27 #include <vector> // for implementation
28 
29 // class VTKCOMMONDATAMODEL_EXPORT vtkIntersectionCounter
30 
31 VTK_ABI_NAMESPACE_BEGIN
33 {
34 public:
36 
41  : Tolerance(0.0001)
42  {
43  }
44  vtkIntersectionCounter(double tol, double length)
45  {
46  this->Tolerance = (length > 0.0 ? (tol / length) : 0.0);
47  }
49 
53  void SetTolerance(double tol) { this->Tolerance = (tol < 0.0 ? 0.0001 : tol); }
54  double GetTolerance() { return this->Tolerance; }
55 
59  void AddIntersection(double t) { IntsArray.push_back(t); }
60 
64  void Reset() { IntsArray.clear(); }
65 
72  {
73  int size = static_cast<int>(IntsArray.size());
74 
75  // Fast check for trivial cases
76  if (size <= 1)
77  {
78  return size; // 0 or 1
79  }
80 
81  // Need to work harder: sort and then count the intersections
82  std::sort(IntsArray.begin(), IntsArray.end());
83 
84  // If here, there is at least one intersection, and two inserted
85  // intersection points
86  int numInts = 1;
87  std::vector<double>::iterator i0 = IntsArray.begin();
88  std::vector<double>::iterator i1 = i0 + 1;
89 
90  // Now march through sorted array counting "separated" intersections
91  while (i1 != IntsArray.end())
92  {
93  if ((*i1 - *i0) > this->Tolerance)
94  {
95  numInts++;
96  i0 = i1;
97  }
98  i1++;
99  }
100 
101  return numInts;
102  }
103 
104 protected:
105  double Tolerance;
106  std::vector<double> IntsArray;
107 
108 }; // vtkIntersectionCounter
109 
110 VTK_ABI_NAMESPACE_END
111 #endif
112 // VTK-HeaderTest-Exclude: vtkIntersectionCounter.h
Fast simple class for dealing with ray intersections.
void AddIntersection(double t)
Add an intersection given by parametric coordinate t.
void Reset()
Reset the intersection process.
vtkIntersectionCounter(double tol, double length)
This tolerance must be converted to parametric space.
std::vector< double > IntsArray
vtkIntersectionCounter()
This tolerance must be converted to parametric space.
int CountIntersections()
Returns number of intersections (even number of intersections, outside or odd number of intersections...
void SetTolerance(double tol)
Set/Get the intersection tolerance.
@ length
Definition: vtkX3D.h:393
@ size
Definition: vtkX3D.h:253