VTK  9.6.20260510
vtkPolyhedronContour.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
26
27#ifndef vtkPolyhedronContour_h
28#define vtkPolyhedronContour_h
29
30#include "vtkCommonDataModelModule.h" // For export macro
31#include "vtkType.h" // For vtkIdType
32
33#include "vtkStaticEdgeLocatorTemplate.h" // For vtkStaticEdgeLocatorTemplate
34
35#include <array> // For array
36#include <cstdint> // For int64_t
37#include <unordered_map> // For unordered_map
38#include <unordered_set> // For unordered_set
39#include <vector> // For vector
40
41VTK_ABI_NAMESPACE_BEGIN
42
43class vtkCellArray;
44class vtkCellData;
45class vtkDataArray;
47class vtkPointData;
48class vtkPolyhedron;
49
53class VTKCOMMONDATAMODEL_EXPORT vtkPolyhedronContour
54{
55public:
58
65
77 CellClassification Execute(vtkPolyhedron* cell, vtkDataArray* scalars, double isoValue,
79
83 int GetNumberOfIsoPolygons() const { return static_cast<int>(this->Result.IsoPolygons.size()); }
84
88 int GetIsoPolygonSize(int polyIdx) const
89 {
90 return static_cast<int>(this->Result.IsoPolygons[polyIdx].size());
91 }
92
96 vtkIdType GetIsoPolygonVertex(int polyIdx, int vertIdx) const
97 {
98 return this->Result.IsoVertices[this->Result.IsoPolygons[polyIdx][vertIdx]].OutputPointId;
99 }
100
104 void OutputContours(vtkCellArray* polys, vtkCellData* inCd, vtkIdType cellId, vtkCellData* outCd);
105
113 void OutputClip(vtkPolyhedron* cell, vtkCellArray* connectivity,
115 vtkIdType cellId, vtkCellData* outCd, int insideOut, vtkCellArray* outFaces = nullptr,
116 vtkCellArray* outFaceLocs = nullptr);
117
118 //
119 // Bulk API for integration with threaded filters (vtkContour3DLinearGrid,
120 // vtkTableBasedClipDataSet). Operates on raw point coordinates and face
121 // streams without instantiating vtkPolyhedron objects.
122 //
123 // - Contour: single-call ContourCell helper below.
124 // - Clip: two-pass CountClip / EmitClip, where CountClip produces the edge
125 // intersection list fed into a shared vtkStaticEdgeLocatorTemplate before
126 // EmitClip writes final geometry with deduplicated iso-vertex IDs.
127 //============================================================================
128
143 static void ContourCell(vtkIdType numPointIds, const vtkIdType* pointIds,
144 vtkCellArray* polyhedronFaces, vtkDataArray* scalars, double isoValue, bool generateTriangles,
145 std::vector<vtkIdType>& polygonsSize,
146 std::vector<EdgeTuple<vtkIdType, double>>& intersectedEdges);
147
164 static void CountClip(vtkIdType numPointIds, const vtkIdType* pointIds,
165 vtkCellArray* polyhedronFaces, vtkDataArray* scalars, double isoValue, bool insideOut,
166 vtkIdType& numOutputCells, vtkIdType& numOutputCellConnectivity, vtkIdType& numOutputFaces,
167 vtkIdType& numOutputFacesConnectivity,
168 std::vector<EdgeTuple<vtkIdType, double>>& intersectedEdges);
169
190 static void EmitClip(vtkIdType numPointIds, const vtkIdType* pointIds,
191 vtkCellArray* polyhedronFaces, vtkDataArray* scalars, double isoValue, bool insideOut,
192 vtkDataArray* pointMap, vtkIdType numberOfKeptPoints,
193 const vtkStaticEdgeLocatorTemplate<vtkIdType, double>& edgeLocator, vtkCellArray* outputCells,
194 vtkCellArray* outputFaces);
195
196private:
197 //============================================================================
198 // Data Structures
199 //============================================================================
200
205 struct IsoVertex
206 {
207 vtkIdType OutsideVertex;
208 vtkIdType InsideVertex;
209 int KeyFace;
210 int OtherFace;
211 double Weight;
212 std::array<double, 3> Position;
213 vtkIdType OutputPointId;
214 };
215
216 struct TraceResult
217 {
219 std::vector<int> VertexTags;
220
222 std::vector<IsoVertex> IsoVertices;
223
225 std::vector<std::vector<int>> FaceIsoVertices;
226
229 std::vector<std::vector<int>> IsoVertexPositionInFace;
230
233 std::vector<std::vector<int>> IsoPolygons;
234
236 std::unordered_map<int64_t, int> EdgeToIsoVert;
237
238 // Assigne vertices
239 std::vector<bool> Assigned;
240
241 void Clear(vtkIdType nFaces)
242 {
243 this->VertexTags.clear();
244 this->IsoVertices.clear();
245 this->FaceIsoVertices.resize(nFaces);
246 for (vtkIdType i = 0; i < nFaces; ++i)
247 {
248 this->FaceIsoVertices[i].clear();
249 }
250 this->IsoVertexPositionInFace.resize(nFaces);
251 for (vtkIdType i = 0; i < nFaces; ++i)
252 {
253 this->IsoVertexPositionInFace[i].clear();
254 }
255 this->IsoPolygons.clear();
256 this->EdgeToIsoVert.clear();
257 this->Assigned.clear();
258 }
259 };
260
261 TraceResult Result;
262
263 //============================================================================
264 // Shared algorithm core (static — used by both the instance path and the
265 // bulk static API).
266 //============================================================================
267
279 static void ClassifyVertices(TraceResult& result, const std::vector<double>& localScalars,
280 double isoValue, int& numInside, int& numOutside);
281
289 static void FindIsoVertices(TraceResult& result, vtkIdType nFaces,
290 const vtkIdType* localFaceStream, const std::vector<double>& localScalars, double isoValue);
291
297 static void TraceIsoPolygons(TraceResult& result);
298
316 static void RunLopezTraceInto(TraceResult& result, vtkIdType nFaces,
317 const vtkIdType* localFaceStream, const std::vector<double>& localScalars, double isoValue);
318
319 //============================================================================
320 // Instance-only helpers
321 //============================================================================
322
328 void InterpolatePositionsAndInsertPoints(vtkPolyhedron* cell, vtkIncrementalPointLocator* locator,
329 vtkPointData* inPd, vtkPointData* outPd);
330
331 //============================================================================
332 // Internal Data structured for ContourCell/CountClip/EmitClip
333 //============================================================================
334
335 // Per-cell scratch for the bulk Contour/clip API. Held in a
336 // thread_local so allocations amortize across cells processed by the
337 // same worker thread. Capacities grow to the largest cell seen and are
338 // retained thereafter.
339 struct PolyhedronWorkspace
340 {
341 std::vector<double> LocalScalars;
342 std::vector<vtkIdType> LocalFaceStream; // face-stream remap: global ID -> local 0..N-1
343 std::unordered_map<vtkIdType, int> GlobalToLocal;
344 TraceResult Trace; // RunLopezTrace output reused
345 std::unordered_set<int64_t> LiveIsoEdgeKeys; // CountClip: edges with non-degenerate iso-verts
346 std::vector<vtkIdType> PieceSizes; // CountClip face-walker scratch
347 std::vector<vtkIdType> LocalToOutputId; // EmitClip: local vertex -> output point ID
348 std::vector<vtkIdType> IsoToOutputId; // EmitClip: iso-vertex index -> output point ID
349 std::unordered_map<int64_t, vtkIdType> EdgeToOutputId; // EmitClip face-walker lookup
350 std::vector<vtkIdType> CellPts; // EmitClip cell-connectivity scratch
351 std::vector<std::vector<vtkIdType>> Pieces; // EmitClip face-walker output pieces
352 std::vector<vtkIdType> CurrentPiece; // EmitClip face-walker active piece
353 std::vector<vtkIdType> CapFace; // EmitClip cap-face scratch
354 };
355};
356
357VTK_ABI_NAMESPACE_END
358#endif // vtkPolyhedronContour_h
object to represent cell connectivity
represent and manipulate cell attribute data
Abstract class in support of both point location and point insertion.
represent and manipulate point attribute data
vtkIdType GetIsoPolygonVertex(int polyIdx, int vertIdx) const
Get iso-polygon vertex (output point ID)
static void ContourCell(vtkIdType numPointIds, const vtkIdType *pointIds, vtkCellArray *polyhedronFaces, vtkDataArray *scalars, double isoValue, bool generateTriangles, std::vector< vtkIdType > &polygonsSize, std::vector< EdgeTuple< vtkIdType, double > > &intersectedEdges)
ContourCell for a single polyhedron.
int GetNumberOfIsoPolygons() const
Get the number of resulting iso-polygons.
static void EmitClip(vtkIdType numPointIds, const vtkIdType *pointIds, vtkCellArray *polyhedronFaces, vtkDataArray *scalars, double isoValue, bool insideOut, vtkDataArray *pointMap, vtkIdType numberOfKeptPoints, const vtkStaticEdgeLocatorTemplate< vtkIdType, double > &edgeLocator, vtkCellArray *outputCells, vtkCellArray *outputFaces)
Pass 2: Emit clipped polyhedron for a single cell.
~vtkPolyhedronContour()=default
void OutputClip(vtkPolyhedron *cell, vtkCellArray *connectivity, vtkIncrementalPointLocator *locator, vtkPointData *inPd, vtkPointData *outPd, vtkCellData *inCd, vtkIdType cellId, vtkCellData *outCd, int insideOut, vtkCellArray *outFaces=nullptr, vtkCellArray *outFaceLocs=nullptr)
Output clipped polyhedron faces to connectivity array.
CellClassification Execute(vtkPolyhedron *cell, vtkDataArray *scalars, double isoValue, vtkIncrementalPointLocator *locator, vtkPointData *inPd, vtkPointData *outPd)
Execute isosurface extraction on a polyhedron cell.
void OutputContours(vtkCellArray *polys, vtkCellData *inCd, vtkIdType cellId, vtkCellData *outCd)
Output contour polygons to cell array (triangulates if needed)
vtkPolyhedronContour()=default
static void CountClip(vtkIdType numPointIds, const vtkIdType *pointIds, vtkCellArray *polyhedronFaces, vtkDataArray *scalars, double isoValue, bool insideOut, vtkIdType &numOutputCells, vtkIdType &numOutputCellConnectivity, vtkIdType &numOutputFaces, vtkIdType &numOutputFacesConnectivity, std::vector< EdgeTuple< vtkIdType, double > > &intersectedEdges)
Pass 1: Count clip output sizes for a single polyhedron.
int GetIsoPolygonSize(int polyIdx) const
Get iso-polygon vertex count.
A 3D cell defined by a set of polygonal faces.
Templated on types of ids defining an edge, and any data associated with the edge.
Definition of an edge tuple.
#define vtkDataArray
int vtkIdType
Definition vtkType.h:363