VTK  9.7.20261031
vtkStaticElementLocator.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
48
49#ifndef vtkStaticElementLocator_h
50#define vtkStaticElementLocator_h
51
52#include "vtkABINamespace.h"
53#include "vtkKeys.h" // For the scatter of the elements by key
54#include "vtkType.h" // For vtkIdType
55
56#include <type_traits> // For std::is_void_v
57#include <utility> // For std::forward, std::swap
58
59VTK_ABI_NAMESPACE_BEGIN
60
61template <typename TID, typename TElement>
63{
64public:
65 using IDType = TID;
66 using ElementType = TElement;
67 // The keys of the elements, whose number is not bounded by IDType.
69
74
80
87
94 vtkIdType BuildLocator(vtkIdType numElements, ElementType* elementArray);
95
102
107 template <typename Arg0, typename Arg1, typename... Args>
108 IDType IsInsertedElement(Arg0&& arg0, Arg1&& arg1, Args&&... args) const
109 {
110 return this->IsInsertedElement(
111 ElementType(std::forward<Arg0>(arg0), std::forward<Arg1>(arg1), std::forward<Args>(args)...));
112 }
113
117 IDType GetNumberOfElements() const { return this->NumElements; }
118
122 const ElementType& GetElement(IDType i) const { return this->ElementArray[i]; }
123
133 const GroupedElements& GroupElements(vtkIdType numElements, ElementType* elementArray);
134
135protected:
137 ElementType* ElementArray = nullptr; // Non-owning pointer to external memory
138 // The search index of BuildLocator(): the unique elements of key id k are
139 // [GetOffset(k), GetOffset(k + 1)) of ElementArray.
141 // The groups of equal elements of GroupElements().
143
144private:
146 void operator=(const vtkStaticElementLocator&) = delete;
147};
148
149// ============================================================================
150// Common Elements
151// ============================================================================
152
157template <typename TData>
159{
160 TData Data;
161};
162template <>
163struct vtkTupleData<void>
164{
165};
166
172template <typename TId, typename TED = void>
174{
175 TId V0;
176 TId V1;
177
178 EdgeTuple() = default;
179
180 // Construct an edge with its data.
181 template <typename D = TED, typename = std::enable_if_t<!std::is_void_v<D>>>
182 EdgeTuple(TId v0, TId v1, const std::common_type_t<D>& data)
183 : vtkTupleData<TED>{ data }
184 , V0(v0)
185 , V1(v1)
186 {
187 if (this->V0 > this->V1)
188 {
189 std::swap(this->V0, this->V1);
190 }
191 }
192
193 // Construct an edge without data, e.g., a query.
194 EdgeTuple(TId v0, TId v1)
195 : vtkTupleData<TED>()
196 , V0(v0)
197 , V1(v1)
198 {
199 if (this->V0 > this->V1)
200 {
201 std::swap(this->V0, this->V1);
202 }
203 }
204
205 // The key of the element: required by vtkStaticElementLocator, see the Element concept of
206 // vtkKeys.h.
207 TId GetKey() const { return this->V0; }
208
209 // Set the points of the edge, ordered.
210 void Define(TId v0, TId v1)
211 {
212 if (v0 < v1)
213 {
214 this->V0 = v0;
215 this->V1 = v1;
216 }
217 else
218 {
219 this->V0 = v1;
220 this->V1 = v0;
221 }
222 }
223
224 bool operator==(const EdgeTuple& et) const { return this->V0 == et.V0 && this->V1 == et.V1; }
225 bool operator!=(const EdgeTuple& et) const { return !(*this == et); }
226
227 // Whether the points of the edge are v0 and v1, in any order.
228 bool IsEdge(TId v0, TId v1) const
229 {
230 return v0 < v1 ? this->V0 == v0 && this->V1 == v1 : this->V0 == v1 && this->V1 == v0;
231 }
232
233 // Sort on V0 first, then V1.
234 bool operator<(const EdgeTuple& et) const
235 {
236 return this->V0 < et.V0 || (this->V0 == et.V0 && this->V1 < et.V1);
237 }
238};
239
245template <typename TId, typename TData = void>
247{
248 TId V0;
249 TId V1;
250 TId V2;
251
252 TriangleTuple() = default;
253
254 // Construct a triangle with its data.
255 template <typename D = TData, typename = std::enable_if_t<!std::is_void_v<D>>>
256 TriangleTuple(TId v0, TId v1, TId v2, const std::common_type_t<D>& data)
257 : vtkTupleData<TData>{ data }
258 , V0(v0)
259 , V1(v1)
260 , V2(v2)
261 {
262 this->Sort();
263 }
264
265 // Construct a triangle without data, e.g., a query.
266 TriangleTuple(TId v0, TId v1, TId v2)
267 : vtkTupleData<TData>()
268 , V0(v0)
269 , V1(v1)
270 , V2(v2)
271 {
272 this->Sort();
273 }
274
275 // The key of the element, see EdgeTuple::GetKey().
276 TId GetKey() const { return this->V0; }
277
278 bool operator==(const TriangleTuple& t) const
279 {
280 return this->V0 == t.V0 && this->V1 == t.V1 && this->V2 == t.V2;
281 }
282 bool operator!=(const TriangleTuple& t) const { return !(*this == t); }
283
284 // Sort on V0 first, then V1, then V2.
285 bool operator<(const TriangleTuple& t) const
286 {
287 if (this->V0 != t.V0)
288 {
289 return this->V0 < t.V0;
290 }
291 return this->V1 != t.V1 ? this->V1 < t.V1 : this->V2 < t.V2;
292 }
293
294private:
295 // A 3-element sorting network.
296 void Sort()
297 {
298 if (this->V0 > this->V1)
299 std::swap(this->V0, this->V1);
300 if (this->V1 > this->V2)
301 std::swap(this->V1, this->V2);
302 if (this->V0 > this->V1)
303 std::swap(this->V0, this->V1);
304 }
305};
306
312template <typename TId, typename TData = void>
313struct QuadTuple : vtkTupleData<TData>
314{
315 TId V0;
316 TId V1;
317 TId V2;
318 TId V3;
319
320 QuadTuple() = default;
321
322 // Construct a quad with its data.
323 template <typename D = TData, typename = std::enable_if_t<!std::is_void_v<D>>>
324 QuadTuple(TId v0, TId v1, TId v2, TId v3, const std::common_type_t<D>& data)
325 : vtkTupleData<TData>{ data }
326 , V0(v0)
327 , V1(v1)
328 , V2(v2)
329 , V3(v3)
330 {
331 this->Sort();
332 }
333
334 // Construct a quad without data, e.g., a query.
335 QuadTuple(TId v0, TId v1, TId v2, TId v3)
336 : vtkTupleData<TData>()
337 , V0(v0)
338 , V1(v1)
339 , V2(v2)
340 , V3(v3)
341 {
342 this->Sort();
343 }
344
345 // The key of the element, see EdgeTuple::GetKey().
346 TId GetKey() const { return this->V0; }
347
348 bool operator==(const QuadTuple& q) const
349 {
350 return this->V0 == q.V0 && this->V1 == q.V1 && this->V2 == q.V2 && this->V3 == q.V3;
351 }
352 bool operator!=(const QuadTuple& q) const { return !(*this == q); }
353
354 // Sort on V0 first, then V1, then V2, then V3.
355 bool operator<(const QuadTuple& q) const
356 {
357 if (this->V0 != q.V0)
358 {
359 return this->V0 < q.V0;
360 }
361 if (this->V1 != q.V1)
362 {
363 return this->V1 < q.V1;
364 }
365 return this->V2 != q.V2 ? this->V2 < q.V2 : this->V3 < q.V3;
366 }
367
368private:
369 // A 4-element sorting network.
370 void Sort()
371 {
372 if (this->V0 > this->V1)
373 std::swap(this->V0, this->V1);
374 if (this->V2 > this->V3)
375 std::swap(this->V2, this->V3);
376 if (this->V0 > this->V2)
377 std::swap(this->V0, this->V2);
378 if (this->V1 > this->V3)
379 std::swap(this->V1, this->V3);
380 if (this->V1 > this->V2)
381 std::swap(this->V1, this->V2);
382 }
383};
384
385// ============================================================================
386// Common Locators
387// ============================================================================
388
393template <typename TId = vtkIdType, typename TData = void>
395
400template <typename TId = vtkIdType, typename TData = void>
402
407template <typename TId = vtkIdType, typename TData = void>
409
410VTK_ABI_NAMESPACE_END
411#include "vtkStaticElementLocator.txx"
412
413#endif
414// VTK-HeaderTest-Exclude: vtkStaticElementLocator.h
templated locator of topological elements, with data on the elements
IDType IsInsertedElement(Arg0 &&arg0, Arg1 &&arg1, Args &&... args) const
Same as above, with the query constructed in place from its arguments, as emplace_back(),...
vtkIdType BuildLocator(vtkIdType numElements, ElementType *elementArray)
Build the locator from the numElements elements of elementArray, which may contain duplicates: the un...
vtk::keys::GroupedElements< KeysType > GroupedElements
vtk::keys::DenseArrayKeys< ElementType, vtkIdType > KeysType
const GroupedElements & GroupElements(vtkIdType numElements, ElementType *elementArray)
Sort the numElements elements of elementArray in place into groups of equal elements,...
vtkStaticElementLocator(vtkStaticElementLocator &&)=default
Move a locator.
const ElementType & GetElement(IDType i) const
Return the element of id i, i.e., the ith unique element, 0 <= i < GetNumberOfElements().
IDType IsInsertedElement(const ElementType &query) const
Return the id of an element, or a negative value if it was not inserted.
~vtkStaticElementLocator()=default
IDType GetNumberOfElements() const
Return the number of unique elements, after BuildLocator().
vtkStaticElementLocator()=default
Construct an empty element locator.
vtkStaticElementLocator & operator=(vtkStaticElementLocator &&)=default
The groups of equal elements of Group(), ordered by key: the elements of group g are at [GetGroupOffs...
Definition vtkKeys.h:427
The keys of the surviving elements of Reduce(), or of the groups of Group(): the keys of the scattere...
Definition vtkKeys.h:382
DenseKeys< detail::ElementKeyOf< Element >, ElementIdType > DenseArrayKeys
Definition vtkKeys.h:305
bool IsEdge(TId v0, TId v1) const
bool operator<(const EdgeTuple &et) const
bool operator==(const EdgeTuple &et) const
EdgeTuple(TId v0, TId v1, const std::common_type_t< D > &data)
EdgeTuple()=default
bool operator!=(const EdgeTuple &et) const
EdgeTuple(TId v0, TId v1)
void Define(TId v0, TId v1)
QuadTuple(TId v0, TId v1, TId v2, TId v3, const std::common_type_t< D > &data)
bool operator!=(const QuadTuple &q) const
QuadTuple(TId v0, TId v1, TId v2, TId v3)
bool operator<(const QuadTuple &q) const
QuadTuple()=default
bool operator==(const QuadTuple &q) const
TriangleTuple(TId v0, TId v1, TId v2, const std::common_type_t< D > &data)
bool operator<(const TriangleTuple &t) const
bool operator!=(const TriangleTuple &t) const
TriangleTuple(TId v0, TId v1, TId v2)
bool operator==(const TriangleTuple &t) const
TriangleTuple()=default
The data associated with an element tuple, TData, as its member Data.
parallel scatter of elements by key, and operations on the elements of every key
vtkStaticElementLocator< TId, TriangleTuple< TId, TData > > vtkStaticTriangleLocator
Static locator specialized for TriangleTuple (3 vertices).
vtkStaticElementLocator< TId, EdgeTuple< TId, TData > > vtkStaticEdgeLocator
Static locator specialized for EdgeTuple (2 vertices).
vtkStaticElementLocator< TId, QuadTuple< TId, TData > > vtkStaticQuadLocator
Static locator specialized for QuadTuple (4 vertices).
int vtkIdType
Definition vtkType.h:363