opensurgsim
Location.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013-2016, 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_LOCATION_H
17 #define SURGSIM_DATASTRUCTURES_LOCATION_H
18 
19 #include <vector>
20 
21 #include "SurgSim/DataStructures/OptionalValue.h"
22 #include "SurgSim/DataStructures/IndexedLocalCoordinate.h"
23 #include "SurgSim/DataStructures/OctreeNode.h"
24 #include "SurgSim/Math/Vector.h"
25 
26 namespace SurgSim
27 {
28 namespace DataStructures
29 {
30 
39 struct Location
40 {
41 public:
42  enum Type {TRIANGLE, ELEMENT};
43 
46  {
47  }
48 
51  Location(const Location& other)
52  : rigidLocalPosition(other.rigidLocalPosition),
53  octreeNodePath(other.octreeNodePath),
54  index(other.index),
55  triangleMeshLocalCoordinate(other.triangleMeshLocalCoordinate),
56  elementMeshLocalCoordinate(other.elementMeshLocalCoordinate)
57  {}
58 
61  explicit Location(const SurgSim::Math::Vector3d& localPosition)
62  {
63  rigidLocalPosition.setValue(localPosition);
64  }
65 
68  explicit Location(const SurgSim::DataStructures::OctreePath& nodePath)
69  {
70  octreeNodePath.setValue(nodePath);
71  }
72 
75  explicit Location(const size_t val)
76  {
77  index.setValue(val);
78  }
79 
83  Location(const SurgSim::DataStructures::IndexedLocalCoordinate& localCoordinate, Type meshType)
84  {
85  switch (meshType)
86  {
87  case TRIANGLE:
88  triangleMeshLocalCoordinate.setValue(localCoordinate);
89  break;
90  case ELEMENT:
91  elementMeshLocalCoordinate.setValue(localCoordinate);
92  break;
93  default:
94  SURGSIM_FAILURE() << "Unknown location";
95  break;
96  }
97  }
98 
104  {
106  switch (meshType)
107  {
108  case TRIANGLE:
109  return triangleMeshLocalCoordinate;
110  break;
111  case ELEMENT:
112  return elementMeshLocalCoordinate;
113  break;
114  default:
115  SURGSIM_FAILURE() << "Unknown location";
116  break;
117  }
118  return null;
119  }
120 
121  bool isApprox(const Location& other, double precision = std::numeric_limits<double>::epsilon()) const
122  {
123  bool result = false;
124 
125  if (rigidLocalPosition.hasValue() && other.rigidLocalPosition.hasValue())
126  {
127  auto const& vector1 = rigidLocalPosition.getValue();
128  auto const& vector2 = other.rigidLocalPosition.getValue();
129 
130  result = (vector1.isZero(precision) && vector2.isZero(precision)) || vector1.isApprox(vector2, precision);
131  }
132  else if (octreeNodePath.hasValue() && other.octreeNodePath.hasValue())
133  {
134  result = (octreeNodePath.getValue() == other.octreeNodePath.getValue());
135  }
136  else if (index.hasValue() && other.index.hasValue())
137  {
138  result = (index.getValue() == other.index.getValue());
139  }
140  else if (triangleMeshLocalCoordinate.hasValue() && other.triangleMeshLocalCoordinate.hasValue())
141  {
142  result = triangleMeshLocalCoordinate.getValue().isApprox(other.triangleMeshLocalCoordinate.getValue());
143  }
144  else if (elementMeshLocalCoordinate.hasValue() && other.elementMeshLocalCoordinate.hasValue())
145  {
146  result = elementMeshLocalCoordinate.getValue().isApprox(other.elementMeshLocalCoordinate.getValue());
147  }
148  else
149  {
150  SURGSIM_FAILURE() << "Invalid location type";
151  }
152 
153  return result;
154  }
155 
161 };
162 
163 
164 template <typename charT, typename traits, typename T>
165 std::basic_ostream<charT, traits>& operator << (std::basic_ostream<charT, traits>& out,
167 {
168  if (val.hasValue())
169  {
170  out << val.getValue();
171  }
172  else
173  {
174  out << "<empty>";
175  }
176  return out;
177 }
178 
179 template <typename charT, typename traits>
180 std::basic_ostream<charT, traits>& operator << (std::basic_ostream<charT, traits>& out,
182 {
183  if (val.hasValue())
184  {
185  out << val.getValue().transpose();
186  }
187  else
188  {
189  out << "<empty>";
190  }
191  return out;
192 }
193 
194 
195 template <typename charT, typename traits>
196 std::basic_ostream<charT, traits>& operator << (std::basic_ostream<charT, traits>& out,
198 {
199  out << "[ " << val.index << " : " << val.coordinate.transpose() << " ]";
200  return out;
201 }
202 
203 
204 template <typename charT, typename traits>
205 std::basic_ostream<charT, traits>& operator << (std::basic_ostream<charT, traits>& out,
206  const Location& loc)
207 {
208  out << "ElementMesh: " << loc.elementMeshLocalCoordinate << std::endl;
209  out << "Index: " << loc.index << std::endl;
210  out << "RigidLocal: " << loc.rigidLocalPosition << std::endl;
211  out << "TriangleMeshLocal: " << loc.triangleMeshLocalCoordinate << std::endl;
212  return out;
213 }
214 
215 }; // namespace DataStructures
216 }; // namespace SurgSim
217 
218 #endif // SURGSIM_DATASTRUCTURES_LOCATION_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
A generic (size_t index, Vector coordinate) pair.
Definition: IndexedLocalCoordinate.h:29
Location(const size_t val)
Constructor for an index.
Definition: Location.h:75
Location(const SurgSim::DataStructures::IndexedLocalCoordinate &localCoordinate, Type meshType)
Constructor for mesh-based location.
Definition: Location.h:83
Location(const SurgSim::Math::Vector3d &localPosition)
Constructor for rigid local position.
Definition: Location.h:61
A Location defines a local position w.r.t.
Definition: Location.h:39
Container class that can indicate whether the object has been assigned a value.
Definition: OptionalValue.h:29
#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
size_t index
Numeric index to indicate the entity w.r.t which the barycentricCoordinate is defined.
Definition: IndexedLocalCoordinate.h:41
Location()
Default constructor.
Definition: Location.h:45
Definitions of small fixed-size vector types.
Location(const SurgSim::DataStructures::OctreePath &nodePath)
Constructor for octree node path.
Definition: Location.h:68
bool hasValue() const
Query if this object has been assigned a value.
Definition: OptionalValue.h:56
void setValue(const T &val)
Set the value of this object, and mark it as valid.
Definition: OptionalValue.h:69
const T & getValue() const
Gets the value.
Definition: OptionalValue.h:78
Location(const Location &other)
Copy constructor.
Definition: Location.h:51