IntervalArithmetic.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013, 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_MATH_INTERVALARITHMETIC_H
#define SURGSIM_MATH_INTERVALARITHMETIC_H
#include <array>
#include <ostream>
namespace SurgSim
{
namespace Math
{
template <class T>
class Interval
{
template <class P>
friend void IntervalArithmetic_add(const Interval<P>& a, const Interval<P>& b, Interval<P>* res); // +
template <class P>
friend void IntervalArithmetic_addadd(const Interval<P>& a, const Interval<P>& b, Interval<P>* res); // +=( + )
template <class P>
friend void IntervalArithmetic_sub(const Interval<P>& a, const Interval<P>& b, Interval<P>* res); // -
template <class P>
friend void IntervalArithmetic_addsub(const Interval<P>& a, const Interval<P>& b, Interval<P>* res); // +=( - )
template <class P>
friend void IntervalArithmetic_mul(const Interval<P>& a, const Interval<P>& b, Interval<P>* res); // *
template <class P>
friend void IntervalArithmetic_addmul(const Interval<P>& a, const Interval<P>& b, Interval<P>* res); // += ( * )
template <class P>
friend void IntervalArithmetic_submul(const Interval<P>& a, const Interval<P>& b, Interval<P>* res); // -= ( * )
public:
Interval();
Interval(const T& min, const T& max);
Interval(const Interval<T>& i);
Interval(Interval<T>&& i);
Interval<T>& operator =(const Interval<T>& i);
Interval<T>& operator=(Interval<T>&& i);
static Interval<T> minToMax(const T& a1, const T& a2);
static Interval<T> minToMax(const T& a1, const T& a2, const T& a3);
static Interval<T> minToMax(const T& a1, const T& a2, const T& a3, const T& a4);
bool overlapsWith(const Interval<T>& i) const;
bool contains(const T& val) const;
bool containsZero() const;
bool isApprox(const Interval<T>& i, const T& epsilon) const;
bool operator ==(const Interval<T>& i) const;
bool operator !=(const Interval<T>& i) const;
Interval<T>& addThickness(const T& thickness);
Interval<T>& extendToInclude(const T& x);
Interval<T>& extendToInclude(const Interval<T>& i);
Interval<T> operator +(const Interval<T>& i) const;
Interval<T> operator +(const T& v) const;
Interval<T>& operator +=(const Interval<T>& i);
Interval<T>& operator +=(const T& v);
Interval<T> operator -() const;
Interval<T> operator -(const Interval<T>& i) const;
Interval<T> operator -(const T& v) const;
Interval<T>& operator -=(const Interval<T>& i);
Interval<T>& operator -=(const T& v);
Interval<T> operator *(const Interval<T>& i) const;
Interval<T> operator *(const T& v) const;
Interval<T>& operator *=(const Interval<T>& i);
Interval<T>& operator *=(const T& v);
Interval<T> inverse() const;
Interval<T> operator /(const Interval<T>& i) const;
Interval<T>& operator /=(const Interval<T>& i);
Interval<T> square() const;
T getMin() const;
T getMax() const;
Interval<T> lowerHalf() const;
Interval<T> upperHalf() const;
private:
T m_min, m_max;
};
template <class T, int N>
class IntervalND
{
public:
static_assert(N >= 1, "IntervalND<T, N> cannot be instantiated with N<=0.");
IntervalND();
explicit IntervalND(const std::array<Interval<T>, N>& x);
IntervalND(const IntervalND<T, N>& interval);
IntervalND(IntervalND<T, N>&& i);
IntervalND(const std::array<T, N>& a, const std::array<T, N>& b);
IntervalND<T, N>& operator =(const IntervalND<T, N>& interval);
IntervalND<T, N>& operator=(IntervalND<T, N>&& i);
bool overlapsWith(const IntervalND<T, N>& interval) const;
bool isApprox(const IntervalND<T, N>& interval, const T& epsilon) const;
bool operator ==(const IntervalND<T, N>& interval) const;
bool operator !=(const IntervalND<T, N>& interval) const;
IntervalND<T, N>& addThickness(const T& thickness);
IntervalND<T, N> operator +(const IntervalND<T, N>& interval) const;
IntervalND<T, N>& operator +=(const IntervalND<T, N>& interval);
IntervalND<T, N> operator -(const IntervalND<T, N>& interval) const;
IntervalND<T, N>& operator -=(const IntervalND<T, N>& interval);
IntervalND<T, N> operator *(const IntervalND<T, N>& interval) const;
IntervalND<T, N>& operator *=(const IntervalND<T, N>& interval);
IntervalND<T, N> inverse() const;
IntervalND<T, N> operator /(const IntervalND<T, N>& interval) const;
IntervalND<T, N>& operator /=(const IntervalND<T, N>& interval);
Interval<T> dotProduct(const IntervalND<T, N>& interval) const;
Interval<T> magnitudeSquared() const;
Interval<T> magnitude() const;
const Interval<T>& getAxis(size_t i) const;
private:
std::array<Interval<T>, N> m_interval;
};
template <class T>
class IntervalND<T, 3>
{
template <class P>
friend void IntervalArithmetic_add(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b, IntervalND<P, 3>* res);
template <class P>
friend void IntervalArithmetic_sub(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b, IntervalND<P, 3>* res);
template <class P>
friend void IntervalArithmetic_crossProduct(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b,
IntervalND<P, 3>* res);
template <class P>
friend void IntervalArithmetic_dotProduct(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b,
Interval<P>* res);
public:
IntervalND();
explicit IntervalND(const std::array<Interval<T>, 3>& x);
IntervalND(const Interval<T>& x, const Interval<T>& y, const Interval<T>& z);
IntervalND(const IntervalND<T, 3>& i);
IntervalND(IntervalND<T, 3>&& i);
IntervalND(const std::array<T, 3>& a, const std::array<T, 3>& b);
IntervalND<T, 3>& operator =(const IntervalND<T, 3>& i);
IntervalND<T, 3>& operator=(IntervalND<T, 3>&& i);
bool overlapsWith(const IntervalND<T, 3>& interval) const;
bool isApprox(const IntervalND<T, 3>& interval, const T& epsilon) const;
bool operator ==(const IntervalND<T, 3>& i) const;
bool operator !=(const IntervalND<T, 3>& i) const;
IntervalND<T, 3>& addThickness(const T& thickness);
IntervalND<T, 3> operator +(const IntervalND<T, 3>& i) const;
IntervalND<T, 3>& operator +=(const IntervalND<T, 3>& i);
IntervalND<T, 3> operator -(const IntervalND<T, 3>& i) const;
IntervalND<T, 3>& operator -=(const IntervalND<T, 3>& i);
IntervalND<T, 3> operator *(const IntervalND<T, 3>& i) const;
IntervalND<T, 3>& operator *=(const IntervalND<T, 3>& i);
IntervalND<T, 3> inverse() const;
IntervalND<T, 3> operator /(const IntervalND<T, 3>& i) const;
IntervalND<T, 3>& operator /=(const IntervalND<T, 3>& i);
Interval<T> dotProduct(const IntervalND<T, 3>& i) const;
IntervalND<T, 3> crossProduct(const IntervalND<T, 3>& i) const;
Interval<T> magnitudeSquared() const;
Interval<T> magnitude() const;
const Interval<T>& getAxis(size_t i) const;
private:
std::array<Interval<T>, 3> m_interval;
};
// Interval utilities
template <typename T>
Interval<T> operator+ (T v, const Interval<T>& i);
template <typename T>
Interval<T> operator* (T v, const Interval<T>& i);
template <typename T>
std::ostream& operator<< (std::ostream& o, const Interval<T>& interval);
template <class P>
void IntervalArithmetic_add(const Interval<P>& a, const Interval<P>& b,
Interval<P>* res); // +
template <class P>
void IntervalArithmetic_addadd(const Interval<P>& a, const Interval<P>& b,
Interval<P>* res); // +=( + )
template <class P>
void IntervalArithmetic_sub(const Interval<P>& a, const Interval<P>& b,
Interval<P>* res); // -
template <class P>
void IntervalArithmetic_addsub(const Interval<P>& a, const Interval<P>& b,
Interval<P>* res); // +=( - )
template <class P>
void IntervalArithmetic_mul(const Interval<P>& a, const Interval<P>& b,
Interval<P>* res); // *
template <class P>
void IntervalArithmetic_addmul(const Interval<P>& a, const Interval<P>& b,
Interval<P>* res); // += ( * )
template <class P>
void IntervalArithmetic_submul(const Interval<P>& a, const Interval<P>& b,
Interval<P>* res); // -= ( * )
// Interval ND utilities
template <typename T, int N>
std::ostream& operator<< (std::ostream& o, const IntervalND<T, N>& interval);
// Interval 3D utilities
template <class P>
void IntervalArithmetic_add(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b,
IntervalND<P, 3>* res);
template <class P>
void IntervalArithmetic_sub(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b,
IntervalND<P, 3>* res);
template <class P>
void IntervalArithmetic_dotProduct(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b,
Interval<P>* res);
template <class P>
void IntervalArithmetic_crossProduct(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b,
IntervalND<P, 3>* res);
}; // Math
}; // SurgSim
#include "SurgSim/Math/IntervalArithmetic-inl.h"
#endif // SURGSIM_MATH_INTERVALARITHMETIC_H
SurgSim::Math::Interval::operator=
Interval< T > & operator=(const Interval< T > &i)
Assignment operator.
Definition: IntervalArithmetic-inl.h:46
SurgSim::Math::IntervalArithmetic_dotProduct
void IntervalArithmetic_dotProduct(const IntervalND< T, 3 > &a, const IntervalND< T, 3 > &b, Interval< T > *res)
Definition: IntervalArithmetic-inl.h:889
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Math::Interval::operator-=
Interval< T > & operator-=(const Interval< T > &i)
Definition: IntervalArithmetic-inl.h:204
SurgSim::Math::Interval::getMin
T getMin() const
Definition: IntervalArithmetic-inl.h:283
SurgSim::Math::Interval::isApprox
bool isApprox(const Interval< T > &i, const T &epsilon) const
Definition: IntervalArithmetic-inl.h:104
SurgSim::Math::IntervalND< T, 3 >
IntervalND<T,3> defines the concept of a group of mathematical intervals specialized to 3 intervals a...
Definition: IntervalArithmetic.h:307
SurgSim::Math::Interval::upperHalf
Interval< T > upperHalf() const
Definition: IntervalArithmetic-inl.h:301
SurgSim::Math::Interval::getMax
T getMax() const
Definition: IntervalArithmetic-inl.h:289
SurgSim::Math::Interval::operator*
Interval< T > operator*(const Interval< T > &i) const
Definition: IntervalArithmetic-inl.h:220
SurgSim::Math::Interval::operator+=
Interval< T > & operator+=(const Interval< T > &i)
Definition: IntervalArithmetic-inl.h:170
SurgSim::Math::Interval::IntervalArithmetic_addsub
friend void IntervalArithmetic_addsub(const Interval< P > &a, const Interval< P > &b, Interval< P > *res)
Add the difference of two intervals to an existing value.
SurgSim::Math::Interval::inverse
Interval< T > inverse() const
Definition: IntervalArithmetic-inl.h:253
SurgSim::Math::IntervalND
IntervalND defines the concept of a group of mathematical intervals and provides operations on them i...
Definition: IntervalArithmetic.h:199
SurgSim::Math::Interval::addThickness
Interval< T > & addThickness(const T &thickness)
Widens the current interval by thickness on both sides.
Definition: IntervalArithmetic-inl.h:122
SurgSim::Math::Interval::IntervalArithmetic_addmul
friend void IntervalArithmetic_addmul(const Interval< P > &a, const Interval< P > &b, Interval< P > *res)
Add the product of two intervals to an existing value.
SurgSim::Math::Interval::IntervalArithmetic_sub
friend void IntervalArithmetic_sub(const Interval< P > &a, const Interval< P > &b, Interval< P > *res)
Calculate the difference of two intervals.
SurgSim::Math::IntervalND::m_interval
std::array< Interval< T >, N > m_interval
The N dimensional group of intervals.
Definition: IntervalArithmetic.h:297
SurgSim::Math::IntervalArithmetic_crossProduct
void IntervalArithmetic_crossProduct(const IntervalND< T, 3 > &a, const IntervalND< T, 3 > &b, IntervalND< T, 3 > *res)
Definition: IntervalArithmetic-inl.h:876
SurgSim::Math::Interval::operator*=
Interval< T > & operator*=(const Interval< T > &i)
Definition: IntervalArithmetic-inl.h:239
SurgSim::Math::Interval::IntervalArithmetic_add
friend void IntervalArithmetic_add(const Interval< P > &a, const Interval< P > &b, Interval< P > *res)
Calculate the sum of two intervals.
SurgSim::Math::Interval::operator/=
Interval< T > & operator/=(const Interval< T > &i)
Definition: IntervalArithmetic-inl.h:267
SurgSim::Math::Interval::operator-
Interval< T > operator-() const
Definition: IntervalArithmetic-inl.h:186
SurgSim::Math::Interval::m_max
T m_max
Definition: IntervalArithmetic.h:187
SurgSim::Math::Interval::m_min
T m_min
The lower (m_min) and upper (m_max) limits of the interval.
Definition: IntervalArithmetic.h:187
SurgSim::Math::Interval::operator!=
bool operator!=(const Interval< T > &i) const
Definition: IntervalArithmetic-inl.h:116
SurgSim::Math::Interval::IntervalArithmetic_submul
friend void IntervalArithmetic_submul(const Interval< P > &a, const Interval< P > &b, Interval< P > *res)
Subtract the product of two intervals from an existing value.
SurgSim::Math::IntervalND< T, 3 >::m_interval
std::array< Interval< T >, 3 > m_interval
The 3 dimensional group of intervals.
Definition: IntervalArithmetic.h:424
SurgSim::Math::Interval::IntervalArithmetic_mul
friend void IntervalArithmetic_mul(const Interval< P > &a, const Interval< P > &b, Interval< P > *res)
Calculate the product of two intervals.
SurgSim::Math::Interval::operator+
Interval< T > operator+(const Interval< T > &i) const
Definition: IntervalArithmetic-inl.h:158
SurgSim::Math::Interval::extendToInclude
Interval< T > & extendToInclude(const T &x)
Widens the current interval on one end to include x.
Definition: IntervalArithmetic-inl.h:130
SurgSim::Math::Interval::operator==
bool operator==(const Interval< T > &i) const
Definition: IntervalArithmetic-inl.h:110
SurgSim::Math::Interval::overlapsWith
bool overlapsWith(const Interval< T > &i) const
Definition: IntervalArithmetic-inl.h:86
SurgSim::Math::Interval
Interval defines the concept of a mathematical interval and provides operations on it including arith...
Definition: IntervalArithmetic.h:34
SurgSim::Math::Interval::IntervalArithmetic_addadd
friend void IntervalArithmetic_addadd(const Interval< P > &a, const Interval< P > &b, Interval< P > *res)
Calculate the sum of three intervals res + a + b.
SurgSim::Math::Interval::Interval
Interval()
Constructor.
Definition: IntervalArithmetic-inl.h:27
IntervalArithmetic-inl.h
SurgSim::Math::Interval::contains
bool contains(const T &val) const
Definition: IntervalArithmetic-inl.h:92
SurgSim::Math::Interval::operator/
Interval< T > operator/(const Interval< T > &i) const
Definition: IntervalArithmetic-inl.h:261
SurgSim::Math::Interval::square
Interval< T > square() const
Definition: IntervalArithmetic-inl.h:273
SurgSim::Math::Interval::containsZero
bool containsZero() const
Definition: IntervalArithmetic-inl.h:98
SurgSim::Math::Interval::minToMax
static Interval< T > minToMax(const T &a1, const T &a2)
Generate an interval from min to max based on the inputs.
Definition: IntervalArithmetic-inl.h:62
SurgSim::Math::Interval::lowerHalf
Interval< T > lowerHalf() const
Definition: IntervalArithmetic-inl.h:295