cisst-saw
Loading...
Searching...
No Matches
msh3Mesh.h
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3// ****************************************************************************
4//
5// Copyright (c) 2015, Seth Billings, Russell Taylor, Johns Hopkins University
6// All rights reserved.
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12// 1. Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14//
15// 2. Redistributions in binary form must reproduce the above copyright
16// notice, this list of conditions and the following disclaimer in the
17// documentation and/or other materials provided with the distribution.
18//
19// 3. Neither the name of the copyright holder nor the names of its
20// contributors may be used to endorse or promote products derived from
21// this software without specific prior written permission.
22//
23// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34//
35// ****************************************************************************
36
37#ifndef _msh3Mesh_h_
38#define _msh3Mesh_h_
39
40#include <cisstMesh/mshConfig.h>
41
46
47#if CISST_MSH_HAS_RPLY
48 #include <ply_io.h>
49#endif
50
51#include <unordered_map>
52
53// Always include last!
54#include <cisstMesh/mshExport.h>
55
57{
58
59public:
60
61 //--- Variables ---//
62 vctDynamicVector<vct3> vertices; // the coordinates for each vertex in the mesh
63 vctDynamicVector<vctInt3> faces; // the vertex indices for each triangle in the mesh
64 vctDynamicVector<vct3> faceNormals; // the face normal for each triangle in the mesh
65
66 // optional mesh properties
67 vctDynamicVector<vct3> vertexNormals; // a normal orientation associated with each vertex
68 vctDynamicVector<vctInt3> faceNeighbors; // the face indices for the neighbors of each triangle
69
70 // for mesh constraint
71 vctDynamicVector<vct3> closestPoint; // the Eculidean location of the closest point
72 vctDynamicVector<int> cpLocation; // the location of the closest point defined by enum (edge, vertex or inside)
73 vctDynamicVector<vct3> activeNormal; // the active normal of the mesh after merging
75
76 // mesh noise model
77 // NOTE: if used, this must be set manually by the user AFTER loading the mesh file
78 // (defaults to all zeroes, i.e. zero measurement noise on the mesh)
79 vctDynamicVector<vct3x3> TriangleCov; // triangle covariances
80 vctDynamicVector<vct3> TriangleCovEig; // triangle covariance eigenvalues (in decreasing size)
81
82
83 //--- Methods ---//
84
85 // constructor
86 msh3Mesh(double robotResolution, bool convertMMtoM); //robot resolution in mm
87 msh3Mesh(bool convertMMtoM);
88 msh3Mesh(double robotResolution);
90
91 // destructor
93
94 // initializes all mesh properties to empty (default initializer);
95 // this is a useful routine to use while building a mesh,
96 // since some mesh properties are optional and may not be
97 // initialized by the data used to build the mesh; calling this
98 // ensures that unused properties are emptied rather than left with
99 // possibly invalid values
100 void ResetMesh();
101
102 inline size_t NumVertices(void) const { return vertices.size(); }
103 inline size_t NumTriangles(void) const { return faces.size(); }
104
105 // initializes triangle noise models to zero (default initializer)
107
108 // computes noise model covariances for each triangle in the mesh
109 // such that the in-plane and perpendicular-plane directions have
110 // the specified variance
111 void InitializeNoiseModel(double noiseInPlaneVar, double noisePerpPlaneVar);
112
113 void SaveTriangleCovariances(std::string &filePath);
114
115 // get coordinates of all three vertices for a given face index
116 inline void FaceCoords(int ti, vct3 &v0, vct3 &v1, vct3 &v2) const
117 {
118 v0 = vertices[faces[ti][0]];
119 v1 = vertices[faces[ti][1]];
120 v2 = vertices[faces[ti][2]];
121 }
122 // get vertex coordinate for a given face/vertex index
123 inline vct3& FaceCoord(int ti, int vi)
124 {
125 return vertices[faces[ti][vi]];
126 }
127
128 // Mesh I/O
129public:
130
131 // Build mesh from data arrays
134 const vctDynamicVector<vct3> *face_normals = NULL,
135 const vctDynamicVector<vctInt3> *face_neighbors = NULL,
136 const vctDynamicVector<vct3> *vertex_normals = NULL
137 );
138
139 // Load mesh from PLY file
140 void LoadPLY(const std::string &input_file);
141
142 // Save mesh to PLY fle
143 void SavePLY(const std::string &output_file);
144
145 // Build mesh from an array of vertices, faces, and face normals
148 const vctDynamicVector<vct3> &N);
149
150 // Build new mesh from a single .mesh file
151 int LoadMeshFile(const std::string &meshFilePath);
152
153 // Build new mesh from multiple .mesh files
154 int LoadMeshFileMultiple(const std::vector<std::string> &meshFilePaths);
155
156 // Save mesh to .mesh file
157 int SaveMeshFile(const std::string &filePath);
158
159 // Build new mesh from .stl file
160 // Assumes model have unit of mm
161 int LoadMeshFromSTLFile(const std::string &stlFilePath);
162
163 enum VertexIndex { V1 = 1, V2 = 2, V3 = 3, V1V2 = 4, V1V3 = 5, V2V3 = 6, IN = -1, VOID=0}; // avoid 0
164 int edgeOffset = 4;
166 // merge edge points to approximate local geometry
167 void MergeEdgePoint(std::vector<int>& faceIdx,
168 vct3& target // the target location that used to test against mesh model
169 );
170 // reset mesh constraint related values
172 void TransformTriangle(const vctFrm4x4 & transformation);
173private:
174
175 // Load .mesh file, adding it to the current mesh while preserving all
176 // data currently existing in the mesh
177 int AddMeshFile(const std::string &meshFilePath);
178
179 // helper function for finding neighboring triangle when loading STL file
180 void FindFaceNeighbor(int faceIdx, std::unordered_multimap<std::string, int>& map);
181 // helper function for vertex index to string conversion, e.g. triangle of vertex [12,3,4],
182 // input (0,1,triangle) will return edge01 (vertex 12 and 3) of key "3-12"
183 // vertex1 and 2 are enums V1,V2,V3 from VertexIndex
184 std::string VertexToKey(const int& vertex1, const int& vertex2, const vctInt3& triangle);
185
186 // check for convexity
187 // if the dot product of
188 // 1. vector pointing from the common edge to the other vertex
189 // 2. normal of neighbor plane
190 // is positive, then local shape is convex
191 inline bool CheckConvexity(int idx, int idxNeighbor) {
192 vct3 vec1 = {0.0, 0.0, 0.0}, vec2 = {0.0, 0.0, 0.0};
193 switch (cpLocation.at(idx)){
194 case V1V2:
195 vec1 = vertices.at(faces.at(idx)[V3-vertexOffset])-vertices.at(faces.at(idx)[V1-vertexOffset]);
196 vec2 = vertices.at(faces.at(idx)[V3-vertexOffset])-vertices.at(faces.at(idx)[V2-vertexOffset]);
197 break;
198 case V1V3:
199 vec1 = vertices.at(faces.at(idx)[V2-vertexOffset])-vertices.at(faces.at(idx)[V1-vertexOffset]);
200 vec2 = vertices.at(faces.at(idx)[V2-vertexOffset])-vertices.at(faces.at(idx)[V3-vertexOffset]);
201 break;
202 case V2V3:
203 vec1 = vertices.at(faces.at(idx)[V1-vertexOffset])-vertices.at(faces.at(idx)[V2-vertexOffset]);
204 vec2 = vertices.at(faces.at(idx)[V1-vertexOffset])-vertices.at(faces.at(idx)[V3-vertexOffset]);
205 break;
206 case V1:
207 vec1 = vertices.at(faces.at(idx)[V2-vertexOffset])-vertices.at(faces.at(idx)[V1-vertexOffset]);
208 vec2 = vertices.at(faces.at(idx)[V3-vertexOffset])-vertices.at(faces.at(idx)[V1-vertexOffset]);
209 break;
210 case V2:
211 vec1 = vertices.at(faces.at(idx)[V1-vertexOffset])-vertices.at(faces.at(idx)[V2-vertexOffset]);
212 vec2 = vertices.at(faces.at(idx)[V3-vertexOffset])-vertices.at(faces.at(idx)[V2-vertexOffset]);
213 break;
214 case V3:
215 vec1 = vertices.at(faces.at(idx)[V1-vertexOffset])-vertices.at(faces.at(idx)[V3-vertexOffset]);
216 vec2 = vertices.at(faces.at(idx)[V2-vertexOffset])-vertices.at(faces.at(idx)[V3-vertexOffset]);
217 break;
218 }
219 if (vec1.DotProduct(faceNormals.at(idxNeighbor)) > 0.0 || vec2.DotProduct(faceNormals.at(idxNeighbor)) > 0.0){ // in case of perpendicularity
220 return true;
221 }
222 if (vec1.DotProduct(faceNormals.at(idxNeighbor)) < 0.0 || vec2.DotProduct(faceNormals.at(idxNeighbor)) < 0.0){
223 return false;
224 }
225 return false;
226 }
227
228 inline bool CheckConcavity(int idx, int idxNeighbor) {
229 return !CheckConvexity(idx, idxNeighbor);
230 }
231 // check side of normal
232 inline bool CheckNormal(const vct3 & target, const int idx, double tolerance) {
233 if ((target-closestPoint.at(idx)).DotProduct(faceNormals.at(idx)) >= -tolerance){
234 return true;
235 }
236 else {
237 return false;
238 }
239 }
240
241#if CISST_MSH_HAS_RPLY
242 ply_io ply_obj;
243#endif
244 double mMeshResolution;
245 double mRobotResolution;
246 bool mConvertMMtoM;
247
248public:
249
250 // Legacy Mesh I/O
251 void ReadMeshFile(const char *fn);
252 void ReadMeshMeshFile( const std::string &meshFilePath );
253 void ReadSURMeshFile(const char *fn);
254 void ReadSFCMeshFile(const char *fn);
255 void WriteMeshFile(const char *fn);
256 void WriteSURMeshFile(const char *fn);
257};
258
259#endif // _cisstMesh_h_
size_t NumTriangles(void) const
Definition msh3Mesh.h:103
void TransformTriangle(const vctFrm4x4 &transformation)
msh3Mesh(double robotResolution, bool convertMMtoM)
void ResetMeshConstraintValues()
int LoadMeshFile(const std::string &meshFilePath)
void ReadMeshMeshFile(const std::string &meshFilePath)
void FaceCoords(int ti, vct3 &v0, vct3 &v1, vct3 &v2) const
Definition msh3Mesh.h:116
vctDynamicVector< vct3 > faceNormals
Definition msh3Mesh.h:64
void InitializeNoiseModel()
void ReadSURMeshFile(const char *fn)
VertexIndex
Definition msh3Mesh.h:163
@ V1V2
Definition msh3Mesh.h:163
@ IN
Definition msh3Mesh.h:163
@ VOID
Definition msh3Mesh.h:163
@ V2
Definition msh3Mesh.h:163
@ V2V3
Definition msh3Mesh.h:163
@ V3
Definition msh3Mesh.h:163
@ V1V3
Definition msh3Mesh.h:163
@ V1
Definition msh3Mesh.h:163
void SaveTriangleCovariances(std::string &filePath)
int SaveMeshFile(const std::string &filePath)
int LoadMesh(const vctDynamicVector< vct3 > &V, const vctDynamicVector< vctInt3 > &T, const vctDynamicVector< vct3 > &N)
vctDynamicVector< vct3 > closestPoint
Definition msh3Mesh.h:71
size_t NumVertices(void) const
Definition msh3Mesh.h:102
vct3 & FaceCoord(int ti, int vi)
Definition msh3Mesh.h:123
vctDynamicVector< vctInt3 > faces
Definition msh3Mesh.h:63
void InitializeNoiseModel(double noiseInPlaneVar, double noisePerpPlaneVar)
int LoadMeshFromSTLFile(const std::string &stlFilePath)
void LoadPLY(const std::string &input_file)
int edgeOffset
Definition msh3Mesh.h:164
vctDynamicVector< vct3x3 > TriangleCov
Definition msh3Mesh.h:79
msh3Mesh(bool convertMMtoM)
void MergeEdgePoint(std::vector< int > &faceIdx, vct3 &target)
vctDynamicVector< vct3 > vertices
Definition msh3Mesh.h:62
void ResetMesh()
vctDynamicVector< int > cpLocation
Definition msh3Mesh.h:72
vctDynamicVector< vct3 > activeNormal
Definition msh3Mesh.h:73
void ReadMeshFile(const char *fn)
vctDynamicVector< vct3 > TriangleCovEig
Definition msh3Mesh.h:80
vctDynamicVector< vctInt3 > faceNeighbors
Definition msh3Mesh.h:68
int LoadMeshFileMultiple(const std::vector< std::string > &meshFilePaths)
void WriteSURMeshFile(const char *fn)
int LoadMesh(const vctDynamicVector< vct3 > *vertices, const vctDynamicVector< vctInt3 > *faces, const vctDynamicVector< vct3 > *face_normals=NULL, const vctDynamicVector< vctInt3 > *face_neighbors=NULL, const vctDynamicVector< vct3 > *vertex_normals=NULL)
void SavePLY(const std::string &output_file)
void WriteMeshFile(const char *fn)
vctDynamicVector< double > distance
Definition msh3Mesh.h:74
~msh3Mesh()
Definition msh3Mesh.h:92
vctDynamicVector< vct3 > vertexNormals
Definition msh3Mesh.h:67
void ReadSFCMeshFile(const char *fn)
int vertexOffset
Definition msh3Mesh.h:165
msh3Mesh(double robotResolution)
Definition vctForwardDeclarations.h:131
value_type DotProduct(const vctFixedSizeConstVectorBase< _size, __stride, value_type, __dataPtrType > &otherVector) const
Definition vctFixedSizeConstVectorBase.h:660
#define CISST_EXPORT
Definition cmnExportMacros.h:50
Macros to export the symbols of cisstMesh (in a Dll).
Typedef for dynamic vectors.
Typedef for fixed size matrices.
Typedef for fixed size vectors.
vctFixedSizeVector< double, 3 > vct3
Definition vctFixedSizeVectorTypes.h:46
vctFixedSizeVector< int, 3 > vctInt3
Definition vctFixedSizeVectorTypes.h:145
Typedef for different transformations.
vctFrame4x4< double, VCT_ROW_MAJOR > vctFrm4x4
Definition vctTransformationTypes.h:153