VTK  9.7.20260802
vtkStaticEdgeLocatorTemplate.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
50
51#ifndef vtkStaticEdgeLocatorTemplate_h
52#define vtkStaticEdgeLocatorTemplate_h
53
54#include "vtkABINamespace.h"
55#include "vtkType.h" // For vtkIdType
56
57#include <algorithm>
58#include <vector>
59
66VTK_ABI_NAMESPACE_BEGIN
67template <typename TId, typename TED>
69{
70 TId V0;
71 TId V1;
72 TED Data;
73
74 // Default constructor - nothing needs to be done
75 EdgeTuple() = default;
76
77 // Construct an edge and ensure that the edge tuple (vo,v1) is
78 // specified such that (v0<v1).
79 EdgeTuple(TId v0, TId v1, TED data)
80 : V0(v0)
81 , V1(v1)
82 , Data(data)
83 {
84 if (this->V0 > this->V1)
85 {
86 std::swap(this->V0, this->V1);
87 }
88 }
89
90 void Define(TId v0, TId v1)
91 {
92 if (v0 < v1)
93 {
94 this->V0 = v0;
95 this->V1 = v1;
96 }
97 else
98 {
99 this->V0 = v1;
100 this->V1 = v0;
101 }
102 }
103
104 bool operator==(const EdgeTuple& et) const { return this->V0 == et.V0 && this->V1 == et.V1; }
105
106 bool operator!=(const EdgeTuple& et) const { return this->V0 != et.V0 || this->V1 != et.V1; }
107
108 bool IsEdge(TId v0, TId v1) const
109 {
110 if (v0 < v1) // ordered properly
111 {
112 return this->V0 == v0 && this->V1 == v1;
113 }
114 else // swap comparison required
115 {
116 return this->V0 == v1 && this->V1 == v0;
117 }
118 }
119 // Sort on v0 first, then v1.
120 bool operator<(const EdgeTuple& tup) const
121 {
122 if (this->V0 < tup.V0)
123 return true;
124 if (tup.V0 < this->V0)
125 return false;
126 if (this->V1 < tup.V1)
127 return true;
128 return false;
129 }
130};
131
136template <typename IDType, typename EdgeData>
138{
139public:
141
146
151 : NumEdges(0)
152 , NumEdgesPerBin(5)
153 , EdgeArray(nullptr)
154 , EdgeOffsets(nullptr)
155 , MinV0(-1)
156 , MaxV0(-1)
157 , V0Range(0)
158 , NDivs(0)
159 , MergeArray(nullptr)
160 {
161 }
162
168
172 IDType GetNumberOfEdges() { return this->NumEdges; }
173
186 const IDType* MergeEdges(vtkIdType numEdges, EdgeTupleType* edgeArray, vtkIdType& numUniqueEdges);
187
197
204 IDType IsInsertedEdge(IDType v0, IDType v1) const
205 {
206 // Ensure that BuildLocator has been called by checking MinV0, MaxV0
207 if (this->MinV0 < 0 || this->MaxV0 < 0)
208 {
209 return -1;
210 }
211 // Ensure that data is consistent with what is expected.
212 if (v0 > v1)
213 {
214 std::swap(v0, v1);
215 }
216 if (v0 < this->MinV0 || v0 > this->MaxV0)
217 {
218 return -1;
219 }
220
221 // Bin and search for matching edge
222 const IDType curBin = this->HashBin(v0);
223 const IDType num = this->GetNumberOfEdgesInBin(curBin);
224 // check if there are no edges
225 if (num < 1)
226 {
227 return -1;
228 }
229 IDType curId = this->EdgeOffsets[curBin];
230 IDType curV0 = this->EdgeArray[curId].V0;
231 while (curV0 < v0)
232 {
233 curId++;
234 curV0 = this->EdgeArray[curId].V0;
235 }
236 if (curV0 > v0)
237 {
238 return -1;
239 }
240 else // matched v0, now find v1
241 {
242 IDType curV1 = this->EdgeArray[curId].V1;
243 while (curV1 < v1)
244 {
245 curId++;
246 curV1 = this->EdgeArray[curId].V1;
247 }
248 if (curV1 > v1)
249 {
250 return -1;
251 }
252 else
253 {
254 return curId;
255 }
256 }
257 }
258
263 const EdgeTupleType& GetEdge(IDType i) const { return (*this->EdgeArray)[i]; }
264
265protected:
267
268 // Support BuildLocator usage pattern
271 IDType* EdgeOffsets;
272 IDType MinV0;
273 IDType MaxV0;
274 IDType V0Range;
275 int NDivs;
276
277 IDType HashBin(IDType v) const { return ((v - this->MinV0) / this->NumEdgesPerBin); }
278
279 IDType GetNumberOfEdgesInBin(IDType bin) const
280 {
281 return (this->EdgeOffsets[bin + 1] - this->EdgeOffsets[bin]);
282 }
283
284 // Support MergeEdges usage pattern
286 std::vector<IDType> MergeOffsets;
287
288private:
290 void operator=(const vtkStaticEdgeLocatorTemplate&) = delete;
291};
292
293VTK_ABI_NAMESPACE_END
294#include "vtkStaticEdgeLocatorTemplate.txx"
295
296#endif
297// VTK-HeaderTest-Exclude: vtkStaticEdgeLocatorTemplate.h
IDType V0Range
Some convenient typedefs.
IDType MinV0
Some convenient typedefs.
vtkIdType NumEdgesPerBin
Some convenient typedefs.
IDType * EdgeOffsets
Some convenient typedefs.
const IDType * MergeEdges(vtkIdType numEdges, EdgeTupleType *edgeArray, vtkIdType &numUniqueEdges)
This method sorts (in place) an array of EdgeTupleType (of length numEdges) into separate groups,...
IDType GetNumberOfEdgesInBin(IDType bin) const
Some convenient typedefs.
IDType MaxV0
Some convenient typedefs.
~vtkStaticEdgeLocatorTemplate()
Delete internal offset array.
IDType IsInsertedEdge(IDType v0, IDType v1) const
Return the id of the edge indicated.
std::vector< IDType > MergeOffsets
Some convenient typedefs.
IDType HashBin(IDType v) const
Some convenient typedefs.
const EdgeTupleType & GetEdge(IDType i) const
Return the ith edge in the edge array.
IDType GetNumberOfEdges()
Return the number of edges in the edge array.
vtkIdType BuildLocator(vtkIdType numEdges, EdgeTupleType *edgeArray)
This method constructs the edge locator to be used when searching for edges.
vtkIdType NumEdges
Some convenient typedefs.
EdgeTupleType * EdgeArray
Some convenient typedefs.
EdgeTuple< IDType, EdgeData > EdgeTupleType
Some convenient typedefs.
EdgeTupleType * MergeArray
Some convenient typedefs.
Definition of an edge tuple.
bool IsEdge(TId v0, TId v1) const
bool operator==(const EdgeTuple &et) const
EdgeTuple(TId v0, TId v1, TED data)
EdgeTuple()=default
bool operator<(const EdgeTuple &tup) const
bool operator!=(const EdgeTuple &et) const
void Define(TId v0, TId v1)
int vtkIdType
Definition vtkType.h:363