opensurgsim
IntervalArithmetic.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 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_MATH_INTERVALARITHMETIC_H
17 #define SURGSIM_MATH_INTERVALARITHMETIC_H
18 
19 #include <array>
20 #include <ostream>
21 
22 namespace SurgSim
23 {
24 namespace Math
25 {
26 
33 template <class T>
34 class Interval
35 {
36  template <class P>
37  friend void IntervalArithmetic_add(const Interval<P>& a, const Interval<P>& b, Interval<P>* res); // +
38  template <class P>
39  friend void IntervalArithmetic_addadd(const Interval<P>& a, const Interval<P>& b, Interval<P>* res); // +=( + )
40  template <class P>
41  friend void IntervalArithmetic_sub(const Interval<P>& a, const Interval<P>& b, Interval<P>* res); // -
42  template <class P>
43  friend void IntervalArithmetic_addsub(const Interval<P>& a, const Interval<P>& b, Interval<P>* res); // +=( - )
44  template <class P>
45  friend void IntervalArithmetic_mul(const Interval<P>& a, const Interval<P>& b, Interval<P>* res); // *
46  template <class P>
47  friend void IntervalArithmetic_addmul(const Interval<P>& a, const Interval<P>& b, Interval<P>* res); // += ( * )
48  template <class P>
49  friend void IntervalArithmetic_submul(const Interval<P>& a, const Interval<P>& b, Interval<P>* res); // -= ( * )
50 
51 public:
53  Interval();
54 
59  Interval(const T& min, const T& max);
60 
63  Interval(const Interval<T>& i);
64 
67  Interval(Interval<T>&& i);
68 
72 
76 
81  static Interval<T> minToMax(const T& a1, const T& a2);
82 
88  static Interval<T> minToMax(const T& a1, const T& a2, const T& a3);
89 
96  static Interval<T> minToMax(const T& a1, const T& a2, const T& a3, const T& a4);
97 
100  bool overlapsWith(const Interval<T>& i) const;
101 
104  bool contains(const T& val) const;
105 
107  bool containsZero() const;
108 
112  bool isApprox(const Interval<T>& i, const T& epsilon) const;
113 
116  bool operator ==(const Interval<T>& i) const;
117 
120  bool operator !=(const Interval<T>& i) const;
121 
125  Interval<T>& addThickness(const T& thickness);
126 
130  Interval<T>& extendToInclude(const T& x);
131 
136 
139  Interval<T> operator +(const Interval<T>& i) const;
140  Interval<T> operator +(const T& v) const;
141  Interval<T>& operator +=(const Interval<T>& i);
142  Interval<T>& operator +=(const T& v);
143  Interval<T> operator -() const;
144  Interval<T> operator -(const Interval<T>& i) const;
145  Interval<T> operator -(const T& v) const;
146  Interval<T>& operator -=(const Interval<T>& i);
147  Interval<T>& operator -=(const T& v);
148  Interval<T> operator *(const Interval<T>& i) const;
149  Interval<T> operator *(const T& v) const;
150  Interval<T>& operator *=(const Interval<T>& i);
151  Interval<T>& operator *=(const T& v);
153 
156  Interval<T> inverse() const;
157 
161  Interval<T> operator /(const Interval<T>& i) const;
162 
168 
171  Interval<T> square() const;
172 
174  T getMin() const;
175 
177  T getMax() const;
178 
180  Interval<T> lowerHalf() const;
181 
183  Interval<T> upperHalf() const;
184 
185 private:
187  T m_min, m_max;
188 };
189 
190 
198 template <class T, int N>
200 {
201 public:
202  static_assert(N >= 1, "IntervalND<T, N> cannot be instantiated with N<=0.");
203 
205  IntervalND();
206 
209  explicit IntervalND(const std::array<Interval<T>, N>& x);
210 
213  IntervalND(const IntervalND<T, N>& interval);
214 
218 
222  IntervalND(const std::array<T, N>& a, const std::array<T, N>& b);
223 
226  IntervalND<T, N>& operator =(const IntervalND<T, N>& interval);
227 
231 
234  bool overlapsWith(const IntervalND<T, N>& interval) const;
235 
240  bool isApprox(const IntervalND<T, N>& interval, const T& epsilon) const;
241 
244  bool operator ==(const IntervalND<T, N>& interval) const;
245 
248  bool operator !=(const IntervalND<T, N>& interval) const;
249 
253  IntervalND<T, N>& addThickness(const T& thickness);
254 
257  IntervalND<T, N> operator +(const IntervalND<T, N>& interval) const;
258  IntervalND<T, N>& operator +=(const IntervalND<T, N>& interval);
259  IntervalND<T, N> operator -(const IntervalND<T, N>& interval) const;
260  IntervalND<T, N>& operator -=(const IntervalND<T, N>& interval);
261  IntervalND<T, N> operator *(const IntervalND<T, N>& interval) const;
262  IntervalND<T, N>& operator *=(const IntervalND<T, N>& interval);
264 
267  IntervalND<T, N> inverse() const;
268 
273  IntervalND<T, N> operator /(const IntervalND<T, N>& interval) const;
274 
280 
283  Interval<T> dotProduct(const IntervalND<T, N>& interval) const;
284 
286  Interval<T> magnitudeSquared() const;
287 
289  Interval<T> magnitude() const;
290 
293  const Interval<T>& getAxis(size_t i) const;
294 
295 private:
297  std::array<Interval<T>, N> m_interval;
298 };
299 
306 template <class T>
307 class IntervalND<T, 3>
308 {
309  template <class P>
310  friend void IntervalArithmetic_add(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b, IntervalND<P, 3>* res);
311  template <class P>
312  friend void IntervalArithmetic_sub(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b, IntervalND<P, 3>* res);
313  template <class P>
314  friend void IntervalArithmetic_crossProduct(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b,
315  IntervalND<P, 3>* res);
316  template <class P>
317  friend void IntervalArithmetic_dotProduct(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b,
318  Interval<P>* res);
319 
320 public:
322  IntervalND();
323 
326  explicit IntervalND(const std::array<Interval<T>, 3>& x);
327 
332  IntervalND(const Interval<T>& x, const Interval<T>& y, const Interval<T>& z);
333 
336  IntervalND(const IntervalND<T, 3>& i);
337 
341 
345  IntervalND(const std::array<T, 3>& a, const std::array<T, 3>& b);
346 
350 
354 
357  bool overlapsWith(const IntervalND<T, 3>& interval) const;
358 
363  bool isApprox(const IntervalND<T, 3>& interval, const T& epsilon) const;
364 
367  bool operator ==(const IntervalND<T, 3>& i) const;
368 
371  bool operator !=(const IntervalND<T, 3>& i) const;
372 
376  IntervalND<T, 3>& addThickness(const T& thickness);
377 
381  IntervalND<T, 3>& operator +=(const IntervalND<T, 3>& i);
382  IntervalND<T, 3> operator -(const IntervalND<T, 3>& i) const;
383  IntervalND<T, 3>& operator -=(const IntervalND<T, 3>& i);
384  IntervalND<T, 3> operator *(const IntervalND<T, 3>& i) const;
385  IntervalND<T, 3>& operator *=(const IntervalND<T, 3>& i);
387 
390  IntervalND<T, 3> inverse() const;
391 
397 
403 
406  Interval<T> dotProduct(const IntervalND<T, 3>& i) const;
407 
410  IntervalND<T, 3> crossProduct(const IntervalND<T, 3>& i) const;
411 
413  Interval<T> magnitudeSquared() const;
414 
416  Interval<T> magnitude() const;
417 
420  const Interval<T>& getAxis(size_t i) const;
421 
422 private:
424  std::array<Interval<T>, 3> m_interval;
425 };
426 
427 // Interval utilities
428 
433 template <typename T>
434 Interval<T> operator+ (T v, const Interval<T>& i);
435 
440 template <typename T>
441 Interval<T> operator* (T v, const Interval<T>& i);
442 
448 template <typename T>
449 std::ostream& operator<< (std::ostream& o, const Interval<T>& interval);
450 
456 template <class P>
457 void IntervalArithmetic_add(const Interval<P>& a, const Interval<P>& b,
458  Interval<P>* res); // +
459 
465 template <class P>
466 void IntervalArithmetic_addadd(const Interval<P>& a, const Interval<P>& b,
467  Interval<P>* res); // +=( + )
468 
474 template <class P>
475 void IntervalArithmetic_sub(const Interval<P>& a, const Interval<P>& b,
476  Interval<P>* res); // -
477 
483 template <class P>
484 void IntervalArithmetic_addsub(const Interval<P>& a, const Interval<P>& b,
485  Interval<P>* res); // +=( - )
486 
492 template <class P>
493 void IntervalArithmetic_mul(const Interval<P>& a, const Interval<P>& b,
494  Interval<P>* res); // *
495 
501 template <class P>
502 void IntervalArithmetic_addmul(const Interval<P>& a, const Interval<P>& b,
503  Interval<P>* res); // += ( * )
504 
510 template <class P>
511 void IntervalArithmetic_submul(const Interval<P>& a, const Interval<P>& b,
512  Interval<P>* res); // -= ( * )
513 
514 // Interval ND utilities
515 
522 template <typename T, int N>
523 std::ostream& operator<< (std::ostream& o, const IntervalND<T, N>& interval);
524 
525 // Interval 3D utilities
526 
532 template <class P>
534  IntervalND<P, 3>* res);
535 
541 template <class P>
543  IntervalND<P, 3>* res);
544 
550 template <class P>
551 void IntervalArithmetic_dotProduct(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b,
552  Interval<P>* res);
553 
559 template <class P>
560 void IntervalArithmetic_crossProduct(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b,
561  IntervalND<P, 3>* res);
562 
563 }; // Math
564 }; // SurgSim
565 
566 #include "SurgSim/Math/IntervalArithmetic-inl.h"
567 
568 #endif // SURGSIM_MATH_INTERVALARITHMETIC_H
Interval< T > & operator=(const Interval< T > &i)
Assignment operator.
Definition: IntervalArithmetic-inl.h:46
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
T getMin() const
Definition: IntervalArithmetic-inl.h:283
bool isApprox(const Interval< T > &i, const T &epsilon) const
Definition: IntervalArithmetic-inl.h:104
IntervalND<T,3> defines the concept of a group of mathematical intervals specialized to 3 intervals a...
Definition: IntervalArithmetic.h:307
Interval< T > upperHalf() const
Definition: IntervalArithmetic-inl.h:301
T getMax() const
Definition: IntervalArithmetic-inl.h:289
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.
Interval< T > inverse() const
Definition: IntervalArithmetic-inl.h:253
IntervalND defines the concept of a group of mathematical intervals and provides operations on them i...
Definition: IntervalArithmetic.h:199
Interval< T > & addThickness(const T &thickness)
Widens the current interval by thickness on both sides.
Definition: IntervalArithmetic-inl.h:122
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.
friend void IntervalArithmetic_sub(const Interval< P > &a, const Interval< P > &b, Interval< P > *res)
Calculate the difference of two intervals.
friend void IntervalArithmetic_add(const Interval< P > &a, const Interval< P > &b, Interval< P > *res)
Calculate the sum of two intervals.
Interval< T > & operator/=(const Interval< T > &i)
Definition: IntervalArithmetic-inl.h:267
bool operator!=(const Interval< T > &i) const
Definition: IntervalArithmetic-inl.h:116
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.
friend void IntervalArithmetic_mul(const Interval< P > &a, const Interval< P > &b, Interval< P > *res)
Calculate the product of two intervals.
Interval< T > operator+(const Interval< T > &i) const
Definition: IntervalArithmetic-inl.h:158
Interval< T > & extendToInclude(const T &x)
Widens the current interval on one end to include x.
Definition: IntervalArithmetic-inl.h:130
bool operator==(const Interval< T > &i) const
Definition: IntervalArithmetic-inl.h:110
bool overlapsWith(const Interval< T > &i) const
Definition: IntervalArithmetic-inl.h:86
Interval defines the concept of a mathematical interval and provides operations on it including arith...
Definition: IntervalArithmetic.h:34
friend void IntervalArithmetic_addadd(const Interval< P > &a, const Interval< P > &b, Interval< P > *res)
Calculate the sum of three intervals res + a + b.
Interval()
Constructor.
Definition: IntervalArithmetic-inl.h:27
bool contains(const T &val) const
Definition: IntervalArithmetic-inl.h:92
Interval< T > operator/(const Interval< T > &i) const
Definition: IntervalArithmetic-inl.h:261
Interval< T > square() const
Definition: IntervalArithmetic-inl.h:273
bool containsZero() const
Definition: IntervalArithmetic-inl.h:98
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
Interval< T > lowerHalf() const
Definition: IntervalArithmetic-inl.h:295