All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Vertices.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_VERTICES_H
17 #define SURGSIM_DATASTRUCTURES_VERTICES_H
18 
21 
22 #include <array>
23 #include <typeinfo>
24 #include <vector>
25 
26 namespace SurgSim
27 {
28 
29 namespace DataStructures
30 {
31 
49 template <class VertexData>
50 class Vertices
51 {
52 public:
55 
58  {
59  }
61  virtual ~Vertices()
62  {
63  }
64 
66  void clear()
67  {
68  doClear();
69  }
70 
73  void update()
74  {
75  doUpdate();
76  }
77 
84  size_t addVertex(const VertexType& vertex)
85  {
86  m_vertices.push_back(vertex);
87  return m_vertices.size() - 1;
88  }
89 
91  size_t getNumVertices() const
92  {
93  return m_vertices.size();
94  }
95 
97  const VertexType& getVertex(size_t id) const
98  {
99  return m_vertices[id];
100  }
102  VertexType& getVertex(size_t id)
103  {
104  return m_vertices[id];
105  }
106 
108  const std::vector<VertexType>& getVertices() const
109  {
110  return m_vertices;
111  }
113  std::vector<VertexType>& getVertices()
114  {
115  return m_vertices;
116  }
117 
121  void setVertexPosition(size_t id, const SurgSim::Math::Vector3d& position)
122  {
123  m_vertices[id].position = position;
124  }
129  {
130  return m_vertices[id].position;
131  }
132 
136  void setVertexPositions(const std::vector<SurgSim::Math::Vector3d>& positions, bool doUpdate = true)
137  {
138  SURGSIM_ASSERT(m_vertices.size() == positions.size()) << "Number of positions must match number of vertices.";
139 
140  for (size_t i = 0; i < m_vertices.size(); ++i)
141  {
142  m_vertices[i].position = positions[i];
143  }
144 
145  if (doUpdate)
146  {
147  update();
148  }
149  }
150 
154  bool operator==(const Vertices& mesh) const
155  {
156  return (typeid(*this) == typeid(mesh)) && isEqual(mesh);
157  }
158 
162  bool operator!=(const Vertices& mesh) const
163  {
164  return (typeid(*this) != typeid(mesh)) || ! isEqual(mesh);
165  }
166 
167 protected:
169  virtual void doClearVertices()
170  {
171  m_vertices.clear();
172  }
173 
178  virtual bool isEqual(const Vertices& mesh) const
179  {
180  return m_vertices == mesh.m_vertices;
181  }
182 
183 private:
185  virtual void doClear()
186  {
187  doClearVertices();
188  }
189 
193  virtual void doUpdate()
194  {
195  }
196 
198  std::vector<VertexType> m_vertices;
199 };
200 
201 }; // namespace DataStructures
202 
203 }; // namespace SurgSim
204 
205 #endif // SURGSIM_DATASTRUCTURES_VERTICES_H
std::vector< VertexType > m_vertices
Vertices.
Definition: Vertices.h:198
size_t getNumVertices() const
Returns the number of vertices in this mesh.
Definition: Vertices.h:91
Vertices()
Constructor. The mesh is initially empty (no vertices).
Definition: Vertices.h:57
const std::vector< VertexType > & getVertices() const
Returns a vector containing the position of each vertex.
Definition: Vertices.h:108
const VertexType & getVertex(size_t id) const
Returns the specified vertex.
Definition: Vertices.h:97
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
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.h:178
void setVertexPositions(const std::vector< SurgSim::Math::Vector3d > &positions, bool doUpdate=true)
Sets the position of each vertex.
Definition: Vertices.h:136
virtual void doClearVertices()
Remove all vertices from the mesh.
Definition: Vertices.h:169
Vertex structure for meshes.
Definition: Vertex.h:44
void update()
Performs any updates that are required when the vertices are modified.
Definition: Vertices.h:73
void setVertexPosition(size_t id, const SurgSim::Math::Vector3d &position)
Sets the position of a vertex.
Definition: Vertices.h:121
bool operator!=(const Vertices &mesh) const
Compares the mesh with another one (inequality)
Definition: Vertices.h:162
The header that provides the assertion API.
size_t addVertex(const VertexType &vertex)
Adds a vertex to the mesh.
Definition: Vertices.h:84
void clear()
Clear mesh to return to an empty state (no vertices).
Definition: Vertices.h:66
virtual void doUpdate()
Performs any updates that are required when the vertices are modified.
Definition: Vertices.h:193
Vertex< VertexData > VertexType
Vertex type for convenience.
Definition: Vertices.h:54
std::vector< VertexType > & getVertices()
Returns a vector containing the position of each vertex (non const version).
Definition: Vertices.h:113
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:56
VertexType & getVertex(size_t id)
Returns the specified vertex (non const version).
Definition: Vertices.h:102
const SurgSim::Math::Vector3d & getVertexPosition(size_t id) const
Returns the position of a vertex.
Definition: Vertices.h:128
virtual ~Vertices()
Destructor.
Definition: Vertices.h:61
Base class for mesh structures, handling basic vertex functionality.
Definition: Vertices.h:50
bool operator==(const Vertices &mesh) const
Compares the mesh with another one (equality)
Definition: Vertices.h:154
virtual void doClear()
Clear mesh to return to an empty state (no vertices).
Definition: Vertices.h:185