opensurgsim
MockObjects.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2012-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_UNITTESTS_MOCKOBJECTS_H
17 #define SURGSIM_DATASTRUCTURES_UNITTESTS_MOCKOBJECTS_H
18 
19 #include <array>
20 #include <limits>
21 
22 #include "SurgSim/DataStructures/Grid.h"
23 #include "SurgSim/DataStructures/TetrahedronMesh.h"
24 #include "SurgSim/DataStructures/TriangleMesh.h"
25 #include "SurgSim/DataStructures/Vertices.h"
26 #include "SurgSim/Math/Vector.h"
27 
29 {
30 public:
31  int m_number;
32  std::string m_string;
33 
34  ElementTest() : m_number(-1), m_string("Empty"){}
35 
36  explicit ElementTest(int i) : m_number(i) { std::stringstream s; s << i; m_string = s.str(); }
37 
38  ElementTest(const ElementTest& e) : m_number(e.m_number), m_string(e.m_string){}
39 
40  bool operator ==(const ElementTest& e) const
41  {
42  return m_number == e.m_number && m_string.compare(e.m_string) == 0;
43  }
44 };
45 
46 // Add a hash function for this class
47 namespace std
48 {
49 template <>
50 struct hash<ElementTest>
51 {
52  std::size_t operator()(const ElementTest& k) const
53  {
54  using std::size_t;
55  using std::hash;
56  using std::string;
57 
58  // Compute individual hash values for first,
59  // second and third and combine them using XOR
60  // and bit shifting:
61  return (hash<string>()(k.m_string) ^ (hash<int>()(k.m_number) << 1));
62  }
63 };
64 }; // namespace std
65 
66 template<typename T>
68 {
69 public:
70  Mock3DData() :
71  m_height(0),
72  m_width(0),
73  m_depth(0),
74  m_buffer(0)
75  {
76  }
77 
78  Mock3DData(int height, int width, int depth) :
79  m_height(height),
80  m_width(width),
81  m_depth(depth),
82  m_buffer(height*depth*width)
83  {
84  }
85 
86  T get(int i, int j, int k)
87  {
88  return m_buffer[getIndex(i,j,k)];
89  }
90 
91  void set(int i, int j, int k, T value)
92  {
93  m_buffer[getIndex(i,j,k)] = value;
94  }
95 
96 private:
97  int getIndex(int i, int j, int k)
98  {
99  return i + j*m_width + k*m_width*m_height;
100  }
101 
102  int m_height;
103  int m_width;
104  int m_depth;
105  std::vector<T> m_buffer;
106 };
107 
110 {
111 public:
115  MockVertexData(size_t id, const SurgSim::Math::Vector3d& normal) :
116  m_id(id),
117  m_normal(normal)
118  {
119  }
120 
123  {
124  }
125 
127  virtual ~MockVertexData()
128  {
129  }
130 
132  size_t getId() const
133  {
134  return m_id;
135  }
136 
139  {
140  return m_normal;
141  }
142 
146  bool operator==(const MockVertexData& data) const
147  {
148  return m_id == data.m_id && (m_normal - data.m_normal).norm() < 1.0e-10;
149  }
150 private:
152  size_t m_id;
154  SurgSim::Math::Vector3d m_normal;
155 };
156 
159 {
160 public:
163  explicit MockEdgeData(size_t id) :
164  m_id(id)
165  {
166  }
167 
170  {
171  }
172 
174  virtual ~MockEdgeData()
175  {
176  }
177 
179  size_t getId() const
180  {
181  return m_id;
182  }
183 
187  bool operator==(const MockEdgeData& data) const
188  {
189  return m_id == data.m_id;
190  }
191 private:
193  size_t m_id;
194 };
195 
198 {
199 public:
203  MockTriangleData(size_t id, const std::array<size_t, 3>& edges) :
204  m_id(id),
205  m_edges(edges)
206  {
207  }
208 
211  {
212  }
213 
216  {
217  }
218 
220  size_t getId() const
221  {
222  return m_id;
223  }
224 
226  const std::array<size_t, 3>& getEdges() const
227  {
228  return m_edges;
229  }
230 
234  bool operator==(const MockTriangleData& data) const
235  {
236  return m_id == data.m_id && m_edges == data.m_edges;
237  }
238 private:
240  size_t m_id = 0;
242  std::array<size_t, 3> m_edges = {};
243 };
244 
247 {
248 public:
254  const std::array<size_t, 6>& edges,
255  const std::array<size_t, 4>& triangles) :
256  m_id(id), m_edges(edges), m_triangles(triangles)
257  {
258  }
259 
262  {
263  }
264 
266  size_t getId() const
267  {
268  return m_id;
269  }
270 
272  const std::array<size_t, 6>& getEdges() const
273  {
274  return m_edges;
275  }
276 
278  const std::array<size_t, 4>& getTriangles() const
279  {
280  return m_triangles;
281  }
282 
286  bool operator==(const MockTetrahedronData& data) const
287  {
288  return m_id == data.m_id && m_edges == data.m_edges && m_triangles == data.m_triangles;
289  }
290 private:
292  size_t m_id;
293 
296  std::array<size_t, 6> m_edges;
297 
299  std::array<size_t, 4> m_triangles;
300 };
301 
303 class MockMesh : public SurgSim::DataStructures::Vertices<MockVertexData>
304 {
305 public:
308 
310  MockMesh() : SurgSim::DataStructures::Vertices<MockVertexData>(),
311  m_numUpdates(0)
312  {
313  }
315  virtual ~MockMesh()
316  {
317  }
318 
323  size_t createVertex(const SurgSim::Math::Vector3d& position, const SurgSim::Math::Vector3d& normal)
324  {
325  VertexType vertex(position, MockVertexData(getNumVertices(), normal));
326 
327  return addVertex(vertex);
328  }
329 
331  const SurgSim::Math::Vector3d& getVertexNormal(size_t id) const
332  {
333  return getVertex(id).data.getNormal();
334  }
335 
337  int getNumUpdates() const
338  {
339  return m_numUpdates;
340  }
341 
342 private:
344  bool doUpdate() override
345  {
346  ++m_numUpdates;
347  return true;
348  }
349 
351  int m_numUpdates;
352 };
353 
355 class MockTetrahedronMesh : public SurgSim::DataStructures::TetrahedronMesh<MockVertexData, MockEdgeData,
356  MockTriangleData, MockTetrahedronData>
357 {
358 public:
371 
374  SurgSim::DataStructures::TetrahedronMesh<MockVertexData, MockEdgeData, MockTriangleData, MockTetrahedronData>(),
375  m_numUpdates(0)
376  {
377  }
380  {
381  }
382 
387  size_t createVertex(const SurgSim::Math::Vector3d& position, const SurgSim::Math::Vector3d& normal)
388  {
389  VertexType vertex(position, MockVertexData(getNumVertices(), normal));
390 
391  return addVertex(vertex);
392  }
393 
397  size_t createEdge(const std::array<size_t, 2>& vertices)
398  {
399  EdgeType edge(vertices, MockEdgeData(getNumEdges()));
400 
401  return addEdge(edge);
402  }
403 
408  size_t createTriangle(const std::array<size_t, 3>& vertices, const std::array<size_t, 3>& edges)
409  {
410  TriangleType triangle(vertices, MockTriangleData(getNumTriangles(), edges));
411 
412  return addTriangle(triangle);
413  }
414 
420  size_t createTetrahedron(const std::array<size_t, 4>& vertices,
421  const std::array<size_t, 6>& edges,
422  const std::array<size_t, 4>& triangles)
423  {
424  TetrahedronType tetrahedron(vertices, MockTetrahedronData(getNumTetrahedrons(), edges, triangles));
425 
426  return addTetrahedron(tetrahedron);
427  }
428 
430  const SurgSim::Math::Vector3d& getVertexNormal(size_t id) const
431  {
432  return getVertex(id).data.getNormal();
433  }
434 
436  int getNumUpdates() const
437  {
438  return m_numUpdates;
439  }
440 
441 private:
443  bool doUpdate() override
444  {
445  ++m_numUpdates;
446  return true;
447  }
448 
450  int m_numUpdates;
451 };
452 
453 template <typename T, size_t N>
455 {
456 public:
460 
461  MockGrid(const Eigen::Matrix<double, N, 1>& cellSize, const Eigen::AlignedBox<double, N>& bounds) :
462  SurgSim::DataStructures::Grid<T,N>(cellSize, bounds)
463  {}
464 
465  std::unordered_map<NDId, CellContent, NDIdHash>& getActiveCells() { return this->m_activeCells; }
466 
467  std::unordered_map<T, NDId>& getCellIds() { return this->m_cellIds; }
468 
469  std::vector<T>& getNonConstNeighbors(const T& element)
470  {
471  return const_cast<std::vector<T>&>(this->getNeighbors(element));
472  }
473 
474  Eigen::Matrix<double, N, 1> getSize() const { return this->m_size; }
475 
476  Eigen::AlignedBox<double, N> getAABB() const { return this->m_aabb; }
477 };
478 
479 namespace wrappers
480 {
481 template <typename IteratorType>
483 {
484 public:
485  formatIterator(IteratorType begin, IteratorType end, const std::string& separator)
486  : m_begin(begin), m_end(end), m_separator(separator)
487  {
488  }
489 
490  friend std::ostream& operator<<(std::ostream& stream, const formatIterator& formatIterator)
491  {
492  if (formatIterator.m_begin == formatIterator.m_end)
493  {
494  return stream;
495  }
496 
497  IteratorType start = formatIterator.m_begin;
498  IteratorType penultimate = formatIterator.m_end - 1;
499  while (start != penultimate)
500  {
501  stream << *start++ << formatIterator.m_separator;
502  }
503  stream << *start;
504 
505  return stream;
506  }
507 
508 private:
509  IteratorType m_begin;
510  IteratorType m_end;
511  const std::string& m_separator;
512 };
513 }
514 
515 template <typename IterableType>
517  const std::string& separator)
518 {
520  iterable.cbegin(), iterable.cend(), separator);
521 }
522 
523 template <typename IteratorType>
525  IteratorType end,
526  const std::string& separator)
527 {
528  return wrappers::formatIterator<IteratorType>(begin, end, separator);
529 }
530 
531 #endif // SURGSIM_DATASTRUCTURES_UNITTESTS_MOCKOBJECTS_H
bool operator==(const MockTriangleData &data) const
Compare the triangle data to another one (equality)
Definition: MockObjects.h:234
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
Definition: MockObjects.h:67
int getNumUpdates() const
Returns the number of updates performed on the mesh.
Definition: MockObjects.h:436
MockTriangleData(size_t id, const std::array< size_t, 3 > &edges)
Constructor.
Definition: MockObjects.h:203
bool operator==(const MockVertexData &data) const
Compare the vertex data to another one (equality)
Definition: MockObjects.h:146
MeshElement< 4, MockTetrahedronData > TetrahedronType
Tetrahedron type for convenience (Ids of the 4 vertices)
Definition: TetrahedronMesh.h:68
size_t createVertex(const SurgSim::Math::Vector3d &position, const SurgSim::Math::Vector3d &normal)
Create a new vertex in the mesh.
Definition: MockObjects.h:323
Definition: MockObjects.h:479
Basic class for storing Tetrahedron Meshes, handling basic vertex, edge, triangle and tetrahedron fun...
Definition: TetrahedronMesh.h:60
MockVertexData(size_t id, const SurgSim::Math::Vector3d &normal)
Constructor.
Definition: MockObjects.h:115
TetrahedronMesh< MockVertexData, MockEdgeData, MockTriangleData, MockTetrahedronData >::VertexType VertexType
Vertex type for convenience.
Definition: MockObjects.h:361
MockVertexData()
Constructor.
Definition: MockObjects.h:122
Mesh for testing using MockVertexData.
Definition: MockObjects.h:303
Definition: MockObjects.h:47
const SurgSim::Math::Vector3d & getVertexNormal(size_t id) const
Returns the normal of a vertex.
Definition: MockObjects.h:430
MockTriangleData()
Constructor.
Definition: MockObjects.h:210
const SurgSim::Math::Vector3d & getNormal() const
Gets the vertex surface normal.
Definition: MockObjects.h:138
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:57
virtual ~MockTetrahedronMesh()
Destructor.
Definition: MockObjects.h:379
const SurgSim::Math::Vector3d & getVertexNormal(size_t id) const
Returns the normal of a vertex.
Definition: MockObjects.h:331
Definition: MockObjects.h:28
size_t createEdge(const std::array< size_t, 2 > &vertices)
Create a new edge in the mesh.
Definition: MockObjects.h:397
Vertex data for testing, storing ID and surface normal.
Definition: MockObjects.h:109
MockTetrahedronMesh()
Constructor. Start out with no vertices and 0 updates.
Definition: MockObjects.h:373
const std::array< size_t, 4 > & getTriangles() const
Gets the IDs of the tetrahedron&#39;s triangles in its mesh.
Definition: MockObjects.h:278
Data structure for a cell&#39;s content (the list of elements and the list of all the neighbors) ...
Definition: Grid.h:81
Definition: MockObjects.h:482
const std::array< size_t, 6 > & getEdges() const
Gets the IDs of the tetrahedron&#39;s edges in its mesh.
Definition: MockObjects.h:272
const std::array< size_t, 3 > & getEdges() const
Gets the IDs of the triangle&#39;s edges in its mesh.
Definition: MockObjects.h:226
Tetrahedron data for testing, storing ID and edge IDs, triangle IDs.
Definition: MockObjects.h:246
Tetrahedron Mesh for testing using MockVertexData, MockEdgeData, MockTriangleData and MockTetrahedron...
Definition: MockObjects.h:355
size_t createTriangle(const std::array< size_t, 3 > &vertices, const std::array< size_t, 3 > &edges)
Create a new triangle in the mesh.
Definition: MockObjects.h:408
MockMesh()
Constructor. Start out with no vertices and 0 updates.
Definition: MockObjects.h:310
size_t createVertex(const SurgSim::Math::Vector3d &position, const SurgSim::Math::Vector3d &normal)
Create a new vertex in the mesh.
Definition: MockObjects.h:387
virtual ~MockMesh()
Destructor.
Definition: MockObjects.h:315
MockEdgeData()
Constructor.
Definition: MockObjects.h:169
size_t getId() const
Gets the triangle&#39;s unique ID in its mesh.
Definition: MockObjects.h:220
MeshElement< 2, MockEdgeData > EdgeType
Edge type for convenience (Ids of the 2 vertices)
Definition: TetrahedronMesh.h:64
size_t getId() const
Gets the tetrahedron&#39;s unique ID in its mesh.
Definition: MockObjects.h:266
Enable the NDId to be used as a key in an unordered map.
Definition: Grid.h:91
TetrahedronMesh< MockVertexData, MockEdgeData, MockTriangleData, MockTetrahedronData >::EdgeType EdgeType
Edge type for convenience.
Definition: MockObjects.h:364
Edge data for testing, storing ID.
Definition: MockObjects.h:158
virtual ~MockEdgeData()
Destructor.
Definition: MockObjects.h:174
bool operator==(const MockEdgeData &data) const
Compare the edge data to another one (equality)
Definition: MockObjects.h:187
TetrahedronMesh< MockVertexData, MockEdgeData, MockTriangleData, MockTetrahedronData >::TriangleType TriangleType
Triangle type for convenience.
Definition: MockObjects.h:367
Definitions of small fixed-size vector types.
virtual ~MockVertexData()
Destructor.
Definition: MockObjects.h:127
virtual ~MockTriangleData()
Destructor.
Definition: MockObjects.h:215
size_t getId() const
Gets the edge&#39;s unique ID in its mesh.
Definition: MockObjects.h:179
Vertices< MockVertexData >::VertexType VertexType
Vertex type for convenience.
Definition: MockObjects.h:307
size_t getId() const
Gets the vertex&#39;s unique ID in its mesh.
Definition: MockObjects.h:132
Definition: MockObjects.h:454
Vertex< MockVertexData > VertexType
Vertex type for convenience.
Definition: Vertices.h:55
n-dimensional grid structure with uniform non-cubic cells This data structure is useful to search for...
Definition: Grid.h:47
int getNumUpdates() const
Returns the number of updates performed on the mesh.
Definition: MockObjects.h:337
Eigen::Matrix< int, N, 1 > NDId
The type of the n-dimensional cell Id.
Definition: Grid.h:88
MeshElement< 3, MockTriangleData > TriangleType
Triangle type for convenience (Ids of the 3 vertices)
Definition: TetrahedronMesh.h:66
TetrahedronMesh< MockVertexData, MockEdgeData, MockTriangleData, MockTetrahedronData >::TetrahedronType TetrahedronType
Tetrahedron type for convenience.
Definition: MockObjects.h:370
bool operator==(const MockTetrahedronData &data) const
Compare the tetrahedron data (equality)
Definition: MockObjects.h:286
virtual ~MockTetrahedronData()
Destructor.
Definition: MockObjects.h:261
Triangle data for testing, storing ID and edge IDs.
Definition: MockObjects.h:197
MockEdgeData(size_t id)
Constructor.
Definition: MockObjects.h:163
Base class for mesh structures, handling basic vertex functionality.
Definition: Vertices.h:51
MockTetrahedronData(size_t id, const std::array< size_t, 6 > &edges, const std::array< size_t, 4 > &triangles)
Constructor.
Definition: MockObjects.h:253
size_t createTetrahedron(const std::array< size_t, 4 > &vertices, const std::array< size_t, 6 > &edges, const std::array< size_t, 4 > &triangles)
Create a new tetrahedron in the mesh.
Definition: MockObjects.h:420