Location.h
Go to the documentation of this file.
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 
24 #include "SurgSim/Math/Vector.h"
25 
26 namespace SurgSim
27 {
28 namespace DataStructures
29 {
30 
39 struct Location
40 {
41 public:
43 
46  {
47  }
48 
51  Location(const Location& other)
54  index(other.index),
57  {}
58 
61  explicit Location(const SurgSim::Math::Vector3d& localPosition)
62  {
63  rigidLocalPosition.setValue(localPosition);
64  }
65 
69  {
70  octreeNodePath.setValue(nodePath);
71  }
72 
75  explicit Location(const size_t val)
76  {
77  index.setValue(val);
78  }
79 
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:
110  break;
111  case ELEMENT:
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 
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  }
141  {
143  }
145  {
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
Vector.h
Definitions of small fixed-size vector types.
SurgSim::DataStructures::OptionalValue< SurgSim::DataStructures::IndexedLocalCoordinate >
SurgSim::DataStructures::Location::ELEMENT
@ ELEMENT
Definition: Location.h:42
SurgSim::DataStructures::Location::rigidLocalPosition
SurgSim::DataStructures::OptionalValue< SurgSim::Math::Vector3d > rigidLocalPosition
Definition: Location.h:156
IndexedLocalCoordinate.h
SurgSim::DataStructures::OptionalValue::setValue
void setValue(const T &val)
Set the value of this object, and mark it as valid.
Definition: OptionalValue.h:69
OptionalValue.h
SurgSim::Math::Vector3d
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:57
SURGSIM_FAILURE
#define SURGSIM_FAILURE()
Report that something very bad has happened and abort program execution.
Definition: Assert.h:95
SurgSim
Definition: CompoundShapeToGraphics.cpp:30
SurgSim::DataStructures::Location::triangleMeshLocalCoordinate
SurgSim::DataStructures::OptionalValue< SurgSim::DataStructures::IndexedLocalCoordinate > triangleMeshLocalCoordinate
Definition: Location.h:159
SurgSim::DataStructures::OctreePath
std::vector< size_t > OctreePath
Typedef of octree path The path is a vector of children indexes (each within 0 to 7) that lead to the...
Definition: OctreeNode.h:43
SurgSim::DataStructures::Location::isApprox
bool isApprox(const Location &other, double precision=std::numeric_limits< double >::epsilon()) const
Definition: Location.h:121
SurgSim::DataStructures::IndexedLocalCoordinate
A generic (size_t index, Vector coordinate) pair.
Definition: IndexedLocalCoordinate.h:30
SurgSim::DataStructures::Location::TRIANGLE
@ TRIANGLE
Definition: Location.h:42
SurgSim::DataStructures::OptionalValue::hasValue
bool hasValue() const
Query if this object has been assigned a value.
Definition: OptionalValue.h:56
SurgSim::DataStructures::Location::Location
Location(const SurgSim::Math::Vector3d &localPosition)
Constructor for rigid local position.
Definition: Location.h:61
SurgSim::DataStructures::Location::Location
Location()
Default constructor.
Definition: Location.h:45
SurgSim::DataStructures::Location::Location
Location(const SurgSim::DataStructures::OctreePath &nodePath)
Constructor for octree node path.
Definition: Location.h:68
SurgSim::DataStructures::Location::Location
Location(const Location &other)
Copy constructor.
Definition: Location.h:51
SurgSim::DataStructures::OptionalValue::getValue
const T & getValue() const
Gets the value.
Definition: OptionalValue.h:78
SurgSim::DataStructures::Location
A Location defines a local position w.r.t.
Definition: Location.h:40
OctreeNode.h
SurgSim::DataStructures::IndexedLocalCoordinate::index
size_t index
Numeric index to indicate the entity w.r.t which the barycentricCoordinate is defined.
Definition: IndexedLocalCoordinate.h:41
SurgSim::DataStructures::operator<<
std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &out, const SurgSim::DataStructures::OptionalValue< T > &val)
Definition: Location.h:165
SurgSim::DataStructures::Location::Location
Location(const SurgSim::DataStructures::IndexedLocalCoordinate &localCoordinate, Type meshType)
Constructor for mesh-based location.
Definition: Location.h:83
SurgSim::DataStructures::Location::index
SurgSim::DataStructures::OptionalValue< size_t > index
Definition: Location.h:158
SurgSim::DataStructures::Location::Location
Location(const size_t val)
Constructor for an index.
Definition: Location.h:75
SurgSim::DataStructures::IndexedLocalCoordinate::coordinate
SurgSim::Math::Vector coordinate
Coordinates with respect to the entity identified by the index.
Definition: IndexedLocalCoordinate.h:44
SurgSim::DataStructures::Location::octreeNodePath
SurgSim::DataStructures::OptionalValue< SurgSim::DataStructures::OctreePath > octreeNodePath
Definition: Location.h:157
SurgSim::DataStructures::Location::get
const SurgSim::DataStructures::OptionalValue< SurgSim::DataStructures::IndexedLocalCoordinate > & get(Type meshType)
Gives access to the coordinates via enum rather than '.
Definition: Location.h:103
SurgSim::DataStructures::IndexedLocalCoordinate::isApprox
bool isApprox(const IndexedLocalCoordinate &other, double precision=std::numeric_limits< double >::epsilon()) const
Comparison method 'isApprox'.
Definition: IndexedLocalCoordinate.cpp:33
SurgSim::DataStructures::Location::Type
Type
Definition: Location.h:42
SurgSim::DataStructures::Location::elementMeshLocalCoordinate
SurgSim::DataStructures::OptionalValue< SurgSim::DataStructures::IndexedLocalCoordinate > elementMeshLocalCoordinate
Definition: Location.h:160