iMSTK
Interactive Medical Simulation Toolkit
imstkGeometryUtilities.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #pragma once
8 
9 #include "imstkMath.h"
10 #include "imstkTypes.h"
11 #include "imstkVecDataArray.h"
12 
13 #include <memory>
14 #include <numeric>
15 #include <queue>
16 #include <set>
17 #include <array>
18 #include <unordered_map>
19 #include <unordered_set>
20 #include <vtkSmartPointer.h>
21 #include <vtkType.h>
22 
23 class vtkDataArray;
24 class vtkCellArray;
25 class vtkPolyData;
26 class vtkImageData;
27 class vtkPointData;
28 class vtkPoints;
29 class vtkPointSet;
30 class vtkUnstructuredGrid;
31 
32 class vtkDataSetAttributes;
33 
34 namespace imstk
35 {
36 class AbstractCellMesh;
37 class AbstractDataArray;
38 class AnalyticalGeometry;
39 class ImageData;
40 class HexahedralMesh;
41 class LineMesh;
42 class PointSet;
43 class SurfaceMesh;
44 class TetrahedralMesh;
45 
46 class OrientedBox;
47 class Sphere;
48 class Plane;
49 
54 namespace GeometryUtils
55 {
56 static std::unordered_map<int, ScalarTypeId> vtkToImstkScalarType =
57 {
58  { VTK_VOID, IMSTK_VOID },
59  { VTK_CHAR, IMSTK_CHAR },
60  { VTK_UNSIGNED_CHAR, IMSTK_UNSIGNED_CHAR },
61  { VTK_SHORT, IMSTK_SHORT },
62  { VTK_UNSIGNED_SHORT, IMSTK_UNSIGNED_SHORT },
63  { VTK_INT, IMSTK_INT },
64  { VTK_UNSIGNED_INT, IMSTK_UNSIGNED_INT },
65  { VTK_LONG, IMSTK_LONG },
66  { VTK_UNSIGNED_LONG, IMSTK_UNSIGNED_LONG },
67  { VTK_FLOAT, IMSTK_FLOAT },
68  { VTK_DOUBLE, IMSTK_DOUBLE },
69  { VTK_LONG_LONG, IMSTK_LONG_LONG },
70  { VTK_UNSIGNED_LONG_LONG, IMSTK_UNSIGNED_LONG_LONG },
71  { VTK_ID_TYPE, IMSTK_UNSIGNED_LONG_LONG }
72 };
73 static std::unordered_map<ScalarTypeId, int> imstkToVtkScalarType =
74 {
75  { IMSTK_VOID, VTK_VOID },
76  { IMSTK_CHAR, VTK_CHAR },
77  { IMSTK_UNSIGNED_CHAR, VTK_UNSIGNED_CHAR },
78  { IMSTK_SHORT, VTK_SHORT },
79  { IMSTK_UNSIGNED_SHORT, VTK_UNSIGNED_SHORT },
80  { IMSTK_INT, VTK_INT },
81  { IMSTK_UNSIGNED_INT, VTK_UNSIGNED_INT },
82  { IMSTK_LONG, VTK_LONG },
83  { IMSTK_UNSIGNED_LONG, VTK_UNSIGNED_LONG },
84  { IMSTK_FLOAT, VTK_FLOAT },
85  { IMSTK_DOUBLE, VTK_DOUBLE },
86  { IMSTK_LONG_LONG, VTK_LONG_LONG },
87  { IMSTK_UNSIGNED_LONG_LONG, VTK_UNSIGNED_LONG_LONG }
88 };
89 
94 vtkSmartPointer<vtkDataArray> coupleVtkDataArray(std::shared_ptr<AbstractDataArray> imstkArray);
95 vtkSmartPointer<vtkImageData> coupleVtkImageData(std::shared_ptr<ImageData> imstkImageData);
97 
101 vtkSmartPointer<vtkDataArray> copyToVtkDataArray(std::shared_ptr<AbstractDataArray> imstkArray);
102 std::shared_ptr<AbstractDataArray> copyToDataArray(vtkSmartPointer<vtkDataArray> vtkArray);
103 
104 std::shared_ptr<ImageData> copyToImageData(vtkSmartPointer<vtkImageData> imageDataVtk);
105 vtkSmartPointer<vtkImageData> copyToVtkImageData(std::shared_ptr<ImageData> imageData);
107 
111 std::shared_ptr<PointSet> copyToPointSet(vtkSmartPointer<vtkPointSet> vtkMesh);
112 
116 std::shared_ptr<SurfaceMesh> copyToSurfaceMesh(vtkSmartPointer<vtkPolyData> vtkMesh);
117 
121 std::shared_ptr<LineMesh> copyToLineMesh(vtkSmartPointer<vtkPolyData> vtkMesh);
122 
127 std::shared_ptr<AbstractCellMesh> copyToCellMesh(vtkSmartPointer<vtkUnstructuredGrid> vtkMesh);
128 
132 vtkSmartPointer<vtkPointSet> copyToVtkPointSet(std::shared_ptr<PointSet> imstkMesh);
133 
137 vtkSmartPointer<vtkPolyData> copyToVtkPolyData(std::shared_ptr<LineMesh> imstkMesh);
138 
142 vtkSmartPointer<vtkPolyData> copyToVtkPolyData(std::shared_ptr<SurfaceMesh> imstkMesh);
143 
147 vtkSmartPointer<vtkUnstructuredGrid> copyToVtkUnstructuredGrid(std::shared_ptr<TetrahedralMesh> imstkMesh);
148 
152 vtkSmartPointer<vtkUnstructuredGrid> copyToVtkUnstructuredGrid(std::shared_ptr<HexahedralMesh> imstkMesh);
153 
157 std::shared_ptr<VecDataArray<double, 3>> copyToVecDataArray(vtkSmartPointer<vtkPoints> points);
158 
162 vtkSmartPointer<vtkPoints> copyToVtkPoints(std::shared_ptr<VecDataArray<double, 3>> vertices);
163 
167 template<size_t dim>
168 vtkSmartPointer<vtkCellArray> copyToVtkCellArray(std::shared_ptr<VecDataArray<int, dim>> cells);
169 
173 template<size_t dim>
174 std::shared_ptr<VecDataArray<int, dim>> copyToVecDataArray(vtkCellArray* vtkCells);
175 
179 void copyToDataMap(vtkDataSetAttributes* pointData, std::unordered_map<std::string, std::shared_ptr<AbstractDataArray>>& dataMap);
180 
185 void copyToVtkDataAttributes(vtkDataSetAttributes* pointData, const std::unordered_map<std::string, std::shared_ptr<AbstractDataArray>>& dataMap);
186 
190 std::shared_ptr<SurfaceMesh> toUVSphereSurfaceMesh(std::shared_ptr<Sphere> sphere,
191  const unsigned int phiDivisions, const unsigned int thetaDivisions);
192 
196 std::shared_ptr<SurfaceMesh> toSurfaceMesh(std::shared_ptr<AnalyticalGeometry> geom);
197 
205 std::shared_ptr<TetrahedralMesh> toTetGrid(
206  const Vec3d& center, const Vec3d& size, const Vec3i& divisions,
207  const Quatd orientation = Quatd::Identity());
208 
216 std::shared_ptr<SurfaceMesh> toTriangleGrid(
217  const Vec3d& center, const Vec2d& size, const Vec2i& dim,
218  const Quatd orientation = Quatd::Identity(),
219  const double uvScale = 1.0);
220 
228 std::shared_ptr<LineMesh> toLineGrid(const Vec3d& start, const Vec3d& dir,
229  const double length, const int dim);
230 
234 int getOpenEdgeCount(std::shared_ptr<SurfaceMesh> surfMesh);
235 
239 inline bool
240 isClosed(std::shared_ptr<SurfaceMesh> surfMesh)
241 {
242  return getOpenEdgeCount(surfMesh) == 0;
243 }
244 
248 double getVolume(std::shared_ptr<SurfaceMesh> surfMesh);
249 
260 std::shared_ptr<TetrahedralMesh> createUniformMesh(const Vec3d& aabbMin, const Vec3d& aabbMax, const int nx, const int ny, const int nz);
261 
265 std::shared_ptr<TetrahedralMesh> createTetrahedralMeshCover(std::shared_ptr<SurfaceMesh> surfMesh, const int nx, const int ny, int nz);
266 
271 {
272  ReverseCuthillMckee // Reverse Cuthill-Mckee
273 };
274 
283 template<typename NeighborContainer>
284 std::vector<size_t> reorderConnectivity(const std::vector<NeighborContainer>& neighbors, const MeshNodeRenumberingStrategy& method = MeshNodeRenumberingStrategy::ReverseCuthillMckee);
285 
295 template<typename ElemConn>
296 std::vector<size_t> reorderConnectivity(const std::vector<ElemConn>& conn, const size_t numVerts, const MeshNodeRenumberingStrategy& method = MeshNodeRenumberingStrategy::ReverseCuthillMckee);
297 } // namespace GeometryUtils
298 } // namespace imstk
vtkSmartPointer< vtkUnstructuredGrid > copyToVtkUnstructuredGrid(std::shared_ptr< TetrahedralMesh > imstkMesh)
Converts imstk tetrahedral mesh into a vtk unstructured grid.
std::shared_ptr< SurfaceMesh > toTriangleGrid(const Vec3d &center, const Vec2d &size, const Vec2i &dim, const Quatd orientation=Quatd::Identity(), const double uvScale=1.0)
Produces a triangle grid on a plane given the imstkPlane.
vtkSmartPointer< vtkDataArray > coupleVtkDataArray(std::shared_ptr< AbstractDataArray > imstkArray)
Coupling functions, these create vtk data objects that point to our data objects thus no copying is d...
bool isClosed(std::shared_ptr< SurfaceMesh > surfMesh)
Returns if the surface is closed or not.
std::shared_ptr< LineMesh > copyToLineMesh(vtkSmartPointer< vtkPolyData > vtkMesh)
Converts vtk polydata into a imstk surface mesh.
vtkSmartPointer< vtkPolyData > copyToVtkPolyData(std::shared_ptr< LineMesh > imstkMesh)
Converts imstk line mesh into a vtk polydata.
int getOpenEdgeCount(std::shared_ptr< SurfaceMesh > surfMesh)
Returns the number of open edges, use to tell if manifold (==0)
vtkSmartPointer< vtkPoints > copyToVtkPoints(std::shared_ptr< VecDataArray< double, 3 >> vertices)
Copies vertices from imstk structure to VTK one.
Compound Geometry.
std::shared_ptr< TetrahedralMesh > toTetGrid(const Vec3d &center, const Vec3d &size, const Vec3i &divisions, const Quatd orientation=Quatd::Identity())
Produces a tetrahedral grid given the OrientedBox with the given divisions.
std::shared_ptr< AbstractCellMesh > copyToCellMesh(vtkSmartPointer< vtkUnstructuredGrid > vtkMesh)
Get imstk cell mesh given vtkUnstructuredGrid as input iMSTK only supports homogenous cells...
std::shared_ptr< LineMesh > toLineGrid(const Vec3d &start, const Vec3d &dir, const double length, const int dim)
Creates a set of connected lines.
vtkSmartPointer< vtkPointSet > copyToVtkPointSet(std::shared_ptr< PointSet > imstkMesh)
Converts imstk point set into a vtk polydata.
void copyToDataMap(vtkDataSetAttributes *pointData, std::unordered_map< std::string, std::shared_ptr< AbstractDataArray >> &dataMap)
Copy vtkPointData to a data map.
std::shared_ptr< TetrahedralMesh > createUniformMesh(const Vec3d &aabbMin, const Vec3d &aabbMax, const int nx, const int ny, const int nz)
Create a tetrahedral mesh based on a uniform Cartesian mesh.
std::shared_ptr< SurfaceMesh > toUVSphereSurfaceMesh(std::shared_ptr< Sphere > sphere, const unsigned int phiDivisions, const unsigned int thetaDivisions)
UV sphere from imstkSphere.
std::shared_ptr< SurfaceMesh > copyToSurfaceMesh(vtkSmartPointer< vtkPolyData > vtkMesh)
Converts vtk polydata into a imstk surface mesh.
std::shared_ptr< TetrahedralMesh > createTetrahedralMeshCover(std::shared_ptr< SurfaceMesh > surfMesh, const int nx, const int ny, int nz)
Create a tetrahedral mesh cover.
double getVolume(std::shared_ptr< SurfaceMesh > surfMesh)
Returns volume estimate of closed SurfaceMesh.
vtkSmartPointer< vtkCellArray > copyToVtkCellArray(std::shared_ptr< VecDataArray< int, dim >> cells)
Copies cells of the given dimension from imstk structure to VTK one.
void copyToVtkDataAttributes(vtkDataSetAttributes *pointData, const std::unordered_map< std::string, std::shared_ptr< AbstractDataArray >> &dataMap)
Copy from a data map to vtkDataAttributes (used for vtkCellData and vtkPointData) warning: Components...
std::shared_ptr< PointSet > copyToPointSet(vtkSmartPointer< vtkPointSet > vtkMesh)
Converts vtk polydata into a imstk point set.
vtkSmartPointer< vtkDataArray > copyToVtkDataArray(std::shared_ptr< AbstractDataArray > imstkArray)
Copy functions, these copy to/from vtk data objects.
std::shared_ptr< SurfaceMesh > toSurfaceMesh(std::shared_ptr< AnalyticalGeometry > geom)
Produces SurfaceMesh from an analytical geometry.
MeshNodeRenumberingStrategy
Enumeration for reordering method.
std::shared_ptr< VecDataArray< double, 3 > > copyToVecDataArray(vtkSmartPointer< vtkPoints > points)
Copy from vtk points to a imstk vertices array.
std::vector< size_t > reorderConnectivity(const std::vector< NeighborContainer > &neighbors, const MeshNodeRenumberingStrategy &method=MeshNodeRenumberingStrategy::ReverseCuthillMckee)
Reorder indices in a connectivity to reduce bandwidth.