opensurgsim
SegmentMesh-inl.h
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.";
90  return TriangleMeshType::getTriangles();
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.";
98  return TriangleMeshType::getTriangles();
99 }
100 
101 template <class VertexData, class EdgeData>
104 {
105  SURGSIM_FAILURE() << "No triangles present in segment mesh.";
106  return TriangleMeshType::getTriangle(id);
107 }
108 
109 template <class VertexData, class EdgeData>
112 {
113  SURGSIM_FAILURE() << "No triangles present in segment mesh.";
114  return TriangleMeshType::getTriangle(id);
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 {
149  TriangleMeshType::doClearEdges();
150  TriangleMeshType::doClearVertices();
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
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
#define SURGSIM_LOG_WARNING(logger)
Logs a message to the specified logger at the WARNING level.
Definition: LogMacros.h:96
SegmentMesh()
Constructor. The mesh is initially empty (no vertices, no edges).
Definition: SegmentMesh-inl.h:28
Definition: MockObjects.h:47
#define SURGSIM_FAILURE()
Report that something very bad has happened and abort program execution.
Definition: Assert.h:95
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:57
The convenience header that provides the entirety of the logging API.
Class to hold the type of a SegmentMesh.
Definition: SegmentMesh.h:33
void createDefaultEdges()
Creates edges for all vertices in the mesh connecting all the points consecutively.
Definition: SegmentMesh-inl.h:154
Element structure for meshes.
Definition: MeshElement.h:44
static std::shared_ptr< Logger > getDefaultLogger()
Get default logger.
Definition: Logger.h:116
TriangleMeshType::TriangleType TriangleType
Triangle type for convenience (Ids of the 3 vertices)
Definition: SegmentMesh.h:41