SegmentMesh-inl.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_DATASTRUCTURES_SEGMENTMESH_INL_H
17 #define SURGSIM_DATASTRUCTURES_SEGMENTMESH_INL_H
18 
19 #include "SurgSim/Framework/Log.h"
20 
21 
22 namespace SurgSim
23 {
24 namespace DataStructures
25 {
26 
27 template <class VertexData, class EdgeData>
29 {
30 }
31 
32 template <class VertexData, class EdgeData>
34  const SegmentMesh<VertexData, EdgeData>& other) :
35  TriangleMeshType(other)
36 {
37 }
38 
39 template <class VertexData, class EdgeData>
40 template <class V, class E>
42  TriangleMeshType(other)
43 {
44 }
45 
46 template <class VertexData, class EdgeData>
48 {
49 }
50 
51 template <class VertexData, class EdgeData>
53  TriangleMeshType(std::move(other))
54 {
55 }
56 
57 template <class VertexData, class EdgeData>
60 {
61  return TriangleMeshType::operator=(other);
62 }
63 
64 template <class VertexData, class EdgeData>
67 {
68  return TriangleMeshType::operator=(std::move(other));
69 }
70 
71 template <class VertexData, class EdgeData>
73 {
74  SURGSIM_FAILURE() << "Cannot insert triangle into segment mesh.";
75  return static_cast<size_t>(0);
76 }
77 
78 template <class VertexData, class EdgeData>
80 {
81  SURGSIM_FAILURE() << "No triangles present in segment mesh.";
82  return static_cast<size_t>(0);
83 }
84 
85 template <class VertexData, class EdgeData>
86 const std::vector<typename SegmentMesh<VertexData, EdgeData>::TriangleType>&
88 {
89  SURGSIM_FAILURE() << "No triangles present in segment mesh.";
91 }
92 
93 template <class VertexData, class EdgeData>
94 std::vector<typename SegmentMesh<VertexData, EdgeData>::TriangleType>&
96 {
97  SURGSIM_FAILURE() << "No triangles present in segment mesh.";
99 }
100 
101 template <class VertexData, class EdgeData>
104 {
105  SURGSIM_FAILURE() << "No triangles present in segment mesh.";
107 }
108 
109 template <class VertexData, class EdgeData>
112 {
113  SURGSIM_FAILURE() << "No triangles present in segment mesh.";
115 }
116 
117 template <class VertexData, class EdgeData>
119 {
120  SURGSIM_FAILURE() << "No triangles present in segment mesh.";
121 }
122 
123 template <class VertexData, class EdgeData>
124 std::array<SurgSim::Math::Vector3d, 3>
126 {
128  SURGSIM_FAILURE() << "No triangles present in segment mesh.";
129  std::array<Vector3d, 3> result =
130  {
131  {
132  Vector3d::Zero(),
133  Vector3d::Zero(),
134  Vector3d::Zero()
135  }
136  };
137  return result;
138 }
139 
140 template <class VertexData, class EdgeData>
142 {
143  SURGSIM_FAILURE() << "No triangles present in segment mesh.";
144 }
145 
146 template <class VertexData, class EdgeData>
148 {
151 }
152 
153 template <class VertexData, class EdgeData>
155 {
156  doClearEdges();
157  for (size_t i = 0; i < getNumVertices() - 1; ++i)
158  {
159  std::array<size_t, 2> vertices = { i, i + 1 };
160  EdgeType edge(vertices);
161  addEdge(edge);
162  }
163 }
164 
165 
166 template <class VertexData, class EdgeData>
167 bool SegmentMesh<VertexData, EdgeData>::save(const std::string& fileName,
168  bool asPhysics,
169  double radius,
170  double massDensity,
171  double poissonRatio,
172  double youngsModulus)
173 {
174  std::fstream out(fileName, std::ios::out);
175 
176  if (out.is_open())
177  {
178  out << "ply" << std::endl;
179  out << "format ascii 1.0" << std::endl;
180  out << "comment Created by OpenSurgSim, www.opensurgsim.org" << std::endl;
181  out << "element vertex " << getNumVertices() << std::endl;
182  out << "property float x\nproperty float y\nproperty float z" << std::endl;
183  if (asPhysics)
184  {
185  out << "element 1d_element " << getNumEdges() << std::endl;
186  out << "property list uint uint vertex_indices" << std::endl;
187  out << "element radius 1" << std::endl;
188  out << "property double value" << std::endl;
189  out << "element material 1" << std::endl;
190  out << "property double mass_density" << std::endl;
191  out << "property double poisson_ratio" << std::endl;
192  out << "property double young_modulus" << std::endl;
193  out << "element boundary_condition 0" << std::endl;
194  out << "property uint vertex_index" << std::endl;
195  }
196  else
197  {
198  out << "element edge " << getNumEdges() << std::endl;
199  out << "property uint vertex1" << std::endl;
200  out << "property uint vertex2" << std::endl;
201  }
202  out << "end_header" << std::endl;
203  for (const auto& vertex : getVertices())
204  {
205  out << vertex.position[0] << " " << vertex.position[1] << " " << vertex.position[2] << std::endl;
206  }
207 
208  for (const auto& edge : getEdges())
209  {
210  if (asPhysics)
211  {
212  out << "2 ";
213  }
214  out << edge.verticesId[0] << " " << edge.verticesId[1] << std::endl;
215  }
216  if (asPhysics)
217  {
218  out << radius << std::endl;
219  out << massDensity << " " << poissonRatio << " " << youngsModulus << std::endl;
220  }
221 
222  if (out.bad())
223  {
225  << "There was a problem writing " << fileName;
226  }
227 
228  out.close();
229  }
230  else
231  {
233  << "Could not open " << fileName << " for writing.";
234  return false;
235  }
236  return true;
237 }
238 
239 
240 } // namespace DataStructures
241 } // namespace SurgSim
242 
243 #endif // SURGSIM_DATASTRUCTURES_SEGMENTMESH_INL_H
Definition: CompoundShapeToGraphics.cpp:29
const TriangleType & getTriangle(size_t id) const
Retrieve a specific triangle.
Definition: TriangleMesh-inl.h:179
size_t addTriangle(const TriangleType &triangle)
Definition: SegmentMesh-inl.h:72
size_t getNumVertices() const
Returns the number of vertices in this mesh.
Definition: Vertices-inl.h:99
void doClearTriangles() override
Definition: SegmentMesh-inl.h:141
#define SURGSIM_LOG_WARNING(logger)
Logs a message to the specified logger at the WARNING level.
Definition: LogMacros.h:96
virtual ~SegmentMesh()
Destructor.
Definition: SegmentMesh-inl.h:47
SegmentMesh()
Constructor. The mesh is initially empty (no vertices, no edges).
Definition: SegmentMesh-inl.h:28
STL namespace.
#define SURGSIM_FAILURE()
Report that something very bad has happened and abort program execution.
Definition: Assert.h:95
The convenience header that provides the entirety of the logging API.
const std::vector< VertexType > & getVertices() const
Returns a vector containing the position of each vertex.
Definition: Vertices-inl.h:117
void removeTriangle(size_t id)
Definition: SegmentMesh-inl.h:118
const std::vector< TriangleType > & getTriangles() const
Retrieve all triangles.
Definition: TriangleMesh-inl.h:137
Class to hold the type of a SegmentMesh.
Definition: SegmentMesh.h:33
virtual void doClearEdges()
Remove all edges from the mesh.
Definition: TriangleMesh-inl.h:259
void doClear() override
Clear mesh to return to an empty state (no vertices, no edges).
Definition: SegmentMesh-inl.h:147
size_t getNumTriangles() const
Definition: SegmentMesh-inl.h:79
void createDefaultEdges()
Creates edges for all vertices in the mesh connecting all the points consecutively.
Definition: SegmentMesh-inl.h:154
TriangleMesh< VertexData, EdgeData, SegmentEmptyData > & operator=(const TriangleMesh< VertexData, EdgeData, SegmentEmptyData > &other)
Copy Assignment.
Definition: TriangleMesh-inl.h:322
size_t addEdge(const EdgeType &edge)
Adds an edge to the mesh.
Definition: TriangleMesh-inl.h:81
Element structure for meshes.
Definition: MeshElement.h:44
std::array< SurgSim::Math::Vector3d, 3 > getTrianglePositions(size_t id) const
Definition: SegmentMesh-inl.h:125
static std::shared_ptr< Logger > getDefaultLogger()
Get default logger.
Definition: Logger.h:116
virtual void doClearVertices()
Remove all vertices from the mesh.
Definition: Vertices-inl.h:178
const TriangleType & getTriangle(size_t id) const
Definition: SegmentMesh-inl.h:103
const std::vector< EdgeType > & getEdges() const
Retrieve all edges.
Definition: TriangleMesh-inl.h:123
SegmentMesh< VertexData, EdgeData > & operator=(const SegmentMesh< VertexData, EdgeData > &other)
Copy Assignment.
Definition: SegmentMesh-inl.h:58
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:57
TriangleMeshType::TriangleType TriangleType
Triangle type for convenience (Ids of the 3 vertices)
Definition: SegmentMesh.h:41
bool save(const std::string &fileName, bool asPhysics=true, double radius=0.0001, double massDensity=900, double poissonRatio=0.45, double youngsModulus=1.75e9)
Save the current structure to a ply file.
Definition: SegmentMesh-inl.h:167
const std::vector< TriangleType > & getTriangles() const
Definition: SegmentMesh-inl.h:87
size_t getNumEdges() const
Get the number of edges.
Definition: TriangleMesh-inl.h:110