opensurgsim
Vertices-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_VERTICES_INL_H
17 #define SURGSIM_DATASTRUCTURES_VERTICES_INL_H
18 
19 #include <typeinfo>
20 
22 
23 
24 namespace SurgSim
25 {
26 namespace DataStructures
27 {
28 
29 template <class VertexData>
31 {
32 }
33 
34 template <class VertexData>
35 template <class V>
37 {
38  m_vertices.reserve(other.getVertices().size());
39  for (auto& otherVertex : other.getVertices())
40  {
41  addVertex(VertexType(otherVertex));
42  }
43 }
44 
45 template <class VertexData>
46 template <class V>
48 {
49  auto& otherVertices = other.getVertices();
50 
51  if (otherVertices.size() < m_vertices.size())
52  {
53  m_vertices.resize(otherVertices.size());
54  }
55  else
56  {
57  m_vertices.reserve(otherVertices.size());
58  }
59 
60  auto vertex = m_vertices.begin();
61  auto otherVertex = otherVertices.begin();
62  for (; vertex != m_vertices.end(); ++vertex, ++otherVertex)
63  {
64  *vertex = *otherVertex;
65  }
66  for (; otherVertex != otherVertices.end(); ++otherVertex)
67  {
68  addVertex(VertexType(*otherVertex));
69  }
70 
71  return *this;
72 }
73 
74 template <class VertexData>
76 {
77 }
78 
79 template <class VertexData>
81 {
82  doClear();
83 }
84 
85 template <class VertexData>
87 {
88  return doUpdate();
89 }
90 
91 template <class VertexData>
93 {
94  m_vertices.push_back(vertex);
95  return m_vertices.size() - 1;
96 }
97 
98 template <class VertexData>
100 {
101  return m_vertices.size();
102 }
103 
104 template <class VertexData>
106 {
107  return m_vertices[id];
108 }
109 
110 template <class VertexData>
112 {
113  return m_vertices[id];
114 }
115 
116 template <class VertexData>
117 const std::vector<typename Vertices<VertexData>::VertexType>& Vertices<VertexData>::getVertices() const
118 {
119  return m_vertices;
120 }
121 
122 template <class VertexData>
123 std::vector<typename Vertices<VertexData>::VertexType>& Vertices<VertexData>::getVertices()
124 {
125  return m_vertices;
126 }
127 
128 template <class VertexData>
130 {
131  m_vertices[id].position = position;
132 }
133 
134 template <class VertexData>
136 {
137  return m_vertices[id].position;
138 }
139 
140 template <class VertexData>
141 void Vertices<VertexData>::setVertexPositions(const std::vector<SurgSim::Math::Vector3d>& positions, bool doUpdate)
142 {
143  SURGSIM_ASSERT(m_vertices.size() == positions.size()) << "Number of positions must match number of vertices.";
144 
145  for (size_t i = 0; i < m_vertices.size(); ++i)
146  {
147  m_vertices[i].position = positions[i];
148  }
149 
150  if (doUpdate)
151  {
152  update();
153  }
154 }
155 
156 template <class VertexData>
158 {
159  for (auto& vertex : m_vertices)
160  {
161  vertex.position = pose * vertex.position;
162  }
163 }
164 
165 template <class VertexData>
167 {
168  return (typeid(*this) == typeid(mesh)) && isEqual(mesh);
169 }
170 
171 template <class VertexData>
173 {
174  return (typeid(*this) != typeid(mesh)) || ! isEqual(mesh);
175 }
176 
177 template <class VertexData>
179 {
180  m_vertices.clear();
181 }
182 
183 template <class VertexData>
185 {
186  return m_vertices == mesh.m_vertices;
187 }
188 
189 template <class VertexData>
191 {
192  doClearVertices();
193 }
194 
195 template <class VertexData>
197 {
198  return true;
199 }
200 
201 };
202 };
203 
204 #endif //SURGSIM_DATASTRUCTURES_VERTICES_INL_H
205 
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
void transform(const Math::RigidTransform3d &pose)
Apply a rigid transform to each vertex.
Definition: Vertices-inl.h:157
size_t getNumVertices() const
Returns the number of vertices in this mesh.
Definition: Vertices-inl.h:99
Eigen::Transform< double, 3, Eigen::Isometry > RigidTransform3d
A 3D rigid (isometric) transform, represented as doubles.
Definition: RigidTransform.h:46
virtual ~Vertices()
Destructor.
Definition: Vertices-inl.h:75
Vertices< VertexData > & operator=(const Vertices< V > &other)
Assignment when the template data is a different type In this case, no data will be copied...
Definition: Vertices-inl.h:47
bool operator==(const Vertices &mesh) const
Compares the mesh with another one (equality)
Definition: Vertices-inl.h:166
Vertices()
Constructor.
Definition: Vertices-inl.h:30
bool update()
Performs any updates that are required when the vertices are modified.
Definition: Vertices-inl.h:86
const VertexType & getVertex(size_t id) const
Returns the specified vertex.
Definition: Vertices-inl.h:105
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:57
void setVertexPositions(const std::vector< SurgSim::Math::Vector3d > &positions, bool doUpdate=true)
Sets the position of each vertex.
Definition: Vertices-inl.h:141
Vertex structure for meshes.
Definition: Vertex.h:44
const std::vector< VertexType > & getVertices() const
Returns a vector containing the position of each vertex.
Definition: Vertices-inl.h:117
void setVertexPosition(size_t id, const SurgSim::Math::Vector3d &position)
Sets the position of a vertex.
Definition: Vertices-inl.h:129
The header that provides the assertion API.
size_t addVertex(const VertexType &vertex)
Adds a vertex to the mesh.
Definition: Vertices-inl.h:92
void clear()
Clear mesh to return to an empty state (no vertices).
Definition: Vertices-inl.h:80
virtual bool isEqual(const Vertices &mesh) const
Internal comparison of meshes of the same type: returns true if equal, false if not equal...
Definition: Vertices-inl.h:184
virtual void doClearVertices()
Remove all vertices from the mesh.
Definition: Vertices-inl.h:178
const SurgSim::Math::Vector3d & getVertexPosition(size_t id) const
Returns the position of a vertex.
Definition: Vertices-inl.h:135
Base class for mesh structures, handling basic vertex functionality.
Definition: Vertices.h:51
bool operator!=(const Vertices &mesh) const
Compares the mesh with another one (inequality)
Definition: Vertices-inl.h:172