Location.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013-2016, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_DATASTRUCTURES_LOCATION_H
#define SURGSIM_DATASTRUCTURES_LOCATION_H
#include <vector>
#include "SurgSim/DataStructures/OptionalValue.h"
#include "SurgSim/DataStructures/IndexedLocalCoordinate.h"
#include "SurgSim/DataStructures/OctreeNode.h"
#include "SurgSim/Math/Vector.h"
namespace SurgSim
{
namespace DataStructures
{
struct Location
{
public:
enum Type {TRIANGLE, ELEMENT};
Location()
{
}
Location(const Location& other)
: rigidLocalPosition(other.rigidLocalPosition),
octreeNodePath(other.octreeNodePath),
index(other.index),
triangleMeshLocalCoordinate(other.triangleMeshLocalCoordinate),
elementMeshLocalCoordinate(other.elementMeshLocalCoordinate)
{}
explicit Location(const SurgSim::Math::Vector3d& localPosition)
{
rigidLocalPosition.setValue(localPosition);
}
explicit Location(const SurgSim::DataStructures::OctreePath& nodePath)
{
octreeNodePath.setValue(nodePath);
}
explicit Location(const size_t val)
{
index.setValue(val);
}
Location(const SurgSim::DataStructures::IndexedLocalCoordinate& localCoordinate, Type meshType)
{
switch (meshType)
{
case TRIANGLE:
triangleMeshLocalCoordinate.setValue(localCoordinate);
break;
case ELEMENT:
elementMeshLocalCoordinate.setValue(localCoordinate);
break;
default:
SURGSIM_FAILURE() << "Unknown location";
break;
}
}
const SurgSim::DataStructures::OptionalValue<SurgSim::DataStructures::IndexedLocalCoordinate>& get(Type meshType)
{
static SurgSim::DataStructures::OptionalValue<SurgSim::DataStructures::IndexedLocalCoordinate> null;
switch (meshType)
{
case TRIANGLE:
return triangleMeshLocalCoordinate;
break;
case ELEMENT:
return elementMeshLocalCoordinate;
break;
default:
SURGSIM_FAILURE() << "Unknown location";
break;
}
return null;
}
bool isApprox(const Location& other, double precision = std::numeric_limits<double>::epsilon()) const
{
bool result = false;
if (rigidLocalPosition.hasValue() && other.rigidLocalPosition.hasValue())
{
auto const& vector1 = rigidLocalPosition.getValue();
auto const& vector2 = other.rigidLocalPosition.getValue();
result = (vector1.isZero(precision) && vector2.isZero(precision)) || vector1.isApprox(vector2, precision);
}
else if (octreeNodePath.hasValue() && other.octreeNodePath.hasValue())
{
result = (octreeNodePath.getValue() == other.octreeNodePath.getValue());
}
else if (index.hasValue() && other.index.hasValue())
{
result = (index.getValue() == other.index.getValue());
}
else if (triangleMeshLocalCoordinate.hasValue() && other.triangleMeshLocalCoordinate.hasValue())
{
result = triangleMeshLocalCoordinate.getValue().isApprox(other.triangleMeshLocalCoordinate.getValue());
}
else if (elementMeshLocalCoordinate.hasValue() && other.elementMeshLocalCoordinate.hasValue())
{
result = elementMeshLocalCoordinate.getValue().isApprox(other.elementMeshLocalCoordinate.getValue());
}
else
{
SURGSIM_FAILURE() << "Invalid location type";
}
return result;
}
SurgSim::DataStructures::OptionalValue<SurgSim::Math::Vector3d> rigidLocalPosition;
SurgSim::DataStructures::OptionalValue<SurgSim::DataStructures::OctreePath> octreeNodePath;
SurgSim::DataStructures::OptionalValue<size_t> index;
SurgSim::DataStructures::OptionalValue<SurgSim::DataStructures::IndexedLocalCoordinate> triangleMeshLocalCoordinate;
SurgSim::DataStructures::OptionalValue<SurgSim::DataStructures::IndexedLocalCoordinate> elementMeshLocalCoordinate;
};
template <typename charT, typename traits, typename T>
std::basic_ostream<charT, traits>& operator << (std::basic_ostream<charT, traits>& out,
const SurgSim::DataStructures::OptionalValue<T>& val)
{
if (val.hasValue())
{
out << val.getValue();
}
else
{
out << "<empty>";
}
return out;
}
template <typename charT, typename traits>
std::basic_ostream<charT, traits>& operator << (std::basic_ostream<charT, traits>& out,
const SurgSim::DataStructures::OptionalValue<SurgSim::Math::Vector3d>& val)
{
if (val.hasValue())
{
out << val.getValue().transpose();
}
else
{
out << "<empty>";
}
return out;
}
template <typename charT, typename traits>
std::basic_ostream<charT, traits>& operator << (std::basic_ostream<charT, traits>& out,
const SurgSim::DataStructures::IndexedLocalCoordinate& val)
{
out << "[ " << val.index << " : " << val.coordinate.transpose() << " ]";
return out;
}
template <typename charT, typename traits>
std::basic_ostream<charT, traits>& operator << (std::basic_ostream<charT, traits>& out,
const Location& loc)
{
out << "ElementMesh: " << loc.elementMeshLocalCoordinate << std::endl;
out << "Index: " << loc.index << std::endl;
out << "RigidLocal: " << loc.rigidLocalPosition << std::endl;
out << "TriangleMeshLocal: " << loc.triangleMeshLocalCoordinate << std::endl;
return out;
}
}; // namespace DataStructures
}; // namespace SurgSim
#endif // SURGSIM_DATASTRUCTURES_LOCATION_H
SurgSim::DataStructures::Location::elementMeshLocalCoordinate
SurgSim::DataStructures::OptionalValue< SurgSim::DataStructures::IndexedLocalCoordinate > elementMeshLocalCoordinate
Definition: Location.h:160
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::DataStructures::IndexedLocalCoordinate
A generic (size_t index, Vector coordinate) pair.
Definition: IndexedLocalCoordinate.h:29
SurgSim::DataStructures::Location::ELEMENT
Definition: Location.h:42
SurgSim::DataStructures::Location::Location
Location(const size_t val)
Constructor for an index.
Definition: Location.h:75
SurgSim::DataStructures::Location::Location
Location(const SurgSim::DataStructures::IndexedLocalCoordinate &localCoordinate, Type meshType)
Constructor for mesh-based location.
Definition: Location.h:83
SurgSim::DataStructures::Location::Location
Location(const SurgSim::Math::Vector3d &localPosition)
Constructor for rigid local position.
Definition: Location.h:61
SurgSim::DataStructures::Location
A Location defines a local position w.r.t.
Definition: Location.h:39
SurgSim::DataStructures::Location::triangleMeshLocalCoordinate
SurgSim::DataStructures::OptionalValue< SurgSim::DataStructures::IndexedLocalCoordinate > triangleMeshLocalCoordinate
Definition: Location.h:159
SurgSim::DataStructures::OptionalValue< SurgSim::DataStructures::IndexedLocalCoordinate >
SurgSim::DataStructures::Location::octreeNodePath
SurgSim::DataStructures::OptionalValue< SurgSim::DataStructures::OctreePath > octreeNodePath
Definition: Location.h:157
SURGSIM_FAILURE
#define SURGSIM_FAILURE()
Report that something very bad has happened and abort program execution.
Definition: Assert.h:95
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
IndexedLocalCoordinate.h
SurgSim::DataStructures::Location::Type
Type
Definition: Location.h:42
SurgSim::DataStructures::Location::index
SurgSim::DataStructures::OptionalValue< size_t > index
Definition: Location.h:158
SurgSim::DataStructures::Location::Location
Location()
Default constructor.
Definition: Location.h:45
SurgSim::DataStructures::Location::TRIANGLE
Definition: Location.h:42
Vector.h
Definitions of small fixed-size vector types.
OctreeNode.h
SurgSim::DataStructures::Location::Location
Location(const SurgSim::DataStructures::OctreePath &nodePath)
Constructor for octree node path.
Definition: Location.h:68
SurgSim::DataStructures::IndexedLocalCoordinate::isApprox
bool isApprox(const IndexedLocalCoordinate &other, double precision=std::numeric_limits< double >::epsilon()) const
Comparison method &#39;isApprox&#39;.
Definition: IndexedLocalCoordinate.cpp:33
OptionalValue.h
SurgSim::DataStructures::OptionalValue::hasValue
bool hasValue() const
Query if this object has been assigned a value.
Definition: OptionalValue.h:56
SurgSim::DataStructures::OptionalValue::setValue
void setValue(const T &val)
Set the value of this object, and mark it as valid.
Definition: OptionalValue.h:69
SurgSim::Math::Vector3d
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:57
SurgSim::DataStructures::OptionalValue::getValue
const T & getValue() const
Gets the value.
Definition: OptionalValue.h:78
SurgSim::DataStructures::Location::Location
Location(const Location &other)
Copy constructor.
Definition: Location.h:51
SurgSim::DataStructures::Location::rigidLocalPosition
SurgSim::DataStructures::OptionalValue< SurgSim::Math::Vector3d > rigidLocalPosition
Definition: Location.h:156