/usr/include/SurgSim/Math/IntervalArithmetic.h is in libopensurgsim-dev 0.7.0-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | // 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
{
/// Interval defines the concept of a mathematical interval and provides operations on it
/// including arithmetic operations, construction, and IO.
///
/// \tparam T underlying data type over which the interval is defined.
///
/// \sa IntervalND<T, N> and IntervalND<T, 3>
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:
/// Constructor
Interval();
/// Constructor
/// \param min Lower bound of the constructed interval
/// \param max Upper bound of the constructed interval
/// \exception if max is less than min
Interval(const T& min, const T& max);
/// Copy constructor
/// \param i Interval to be copied
Interval(const Interval<T>& i);
/// Move constructor
/// \param i Interval to be copied
Interval(Interval<T>&& i);
/// Assignment operator
/// \param i Interval to be copied
Interval<T>& operator =(const Interval<T>& i);
/// Move assignment operator
/// \param i Interval to be moved
Interval<T>& operator=(Interval<T>&& i);
/// Generate an interval from min to max based on the inputs
/// \param a1 first input value
/// \param a2 second input value
/// \return an interval spanning the minimum input to the maximum input.
static Interval<T> minToMax(const T& a1, const T& a2);
/// Generate an interval from min to max based on the inputs
/// \param a1 first input value
/// \param a2 second input value
/// \param a3 third input value
/// \return an interval spanning the minimum input to the maximum input.
static Interval<T> minToMax(const T& a1, const T& a2, const T& a3);
/// Generate an interval from min to max based on the inputs
/// \param a1 first input value
/// \param a2 second input value
/// \param a3 third input value
/// \param a4 fourth input value
/// \return an interval spanning the minimum input to the maximum input.
static Interval<T> minToMax(const T& a1, const T& a2, const T& a3, const T& a4);
/// \param i the interval the current interval will be tested against
/// \return true if the input interval overlaps the current interval
bool overlapsWith(const Interval<T>& i) const;
/// \param val the value to test for inclusion in the interval
/// \return true if the current interval contains val
bool contains(const T& val) const;
/// \return true if the current interval contains 0
bool containsZero() const;
/// \param i the interval to be tested
/// \param epsilon the nearness parameter
/// \return true if the current interval is within epsilon of the input interval
bool isApprox(const Interval<T>& i, const T& epsilon) const;
/// \param i the interval to be tested
/// \return true if the current interval is identical to the input interval
bool operator ==(const Interval<T>& i) const;
/// \param i the interval to be tested
/// \return true if the current interval is not identical to the input interval
bool operator !=(const Interval<T>& i) const;
/// Widens the current interval by thickness on both sides
/// \param thickness the amount to widen the current interval on both sides
/// \return the current interval after modification
Interval<T>& addThickness(const T& thickness);
/// Widens the current interval on one end to include x
/// \param x the value to be included in the interval
/// \return the current interval extended to include x
Interval<T>& extendToInclude(const T& x);
/// Widens the current interval on both ends to include i
/// \param i the interval to be wholly contained in the current interval
/// \return the current interval extended to include the entirety of i
Interval<T>& extendToInclude(const Interval<T>& i);
/// @{
/// Standard arithmetic operators extended to intervals
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);
/// @}
/// \return the inverse of the current interval
/// \exception if the interval includes 0
Interval<T> inverse() const;
/// \param i the interval to be divided by
/// \return the current interval multiplied by the inverse of i
/// \exception if i includes 0
Interval<T> operator /(const Interval<T>& i) const;
/// \param i the interval to be divided by
/// \return the current interval multiplied by the inverse of i
/// \exception if i includes 0
/// \note the current interval is modified by this operation
Interval<T>& operator /=(const Interval<T>& i);
/// \return the square of the current interval
/// \note if the original interval contains 0, then the result will have the minimum identically set to 0
Interval<T> square() const;
/// \return the lower limit of the interval
T getMin() const;
/// \return the upper limit of the interval
T getMax() const;
/// \return the interval from the lower limit to the midpoint
Interval<T> lowerHalf() const;
/// \return the interval from the midpoint to the upper limit
Interval<T> upperHalf() const;
private:
/// The lower (m_min) and upper (m_max) limits of the interval
T m_min, m_max;
};
/// IntervalND defines the concept of a group of mathematical intervals and provides operations on them
/// including arithmetic operations, construction, and IO.
///
/// \tparam T underlying data type over which the interval is defined.
/// \tparam N number of intervals in the group.
///
/// \sa Interval<T> and IntervalND<T, 3>
template <class T, int N>
class IntervalND
{
public:
static_assert(N >= 1, "IntervalND<T, N> cannot be instantiated with N<=0.");
/// Constructor
IntervalND();
/// Constructor
/// \param x array of N intervals to be copied into the group
explicit IntervalND(const std::array<Interval<T>, N>& x);
/// Copy constructor
/// \param interval interval group to copied
IntervalND(const IntervalND<T, N>& interval);
/// Move constructor
/// \param i Interval to be copied
IntervalND(IntervalND<T, N>&& i);
/// Constructor
/// \param a array of N values to be used as the respective minimums for the interval entries.
/// \param b array of N values to be used as the respective maximums for the interval entries.
IntervalND(const std::array<T, N>& a, const std::array<T, N>& b);
/// Assignment operator
/// \param interval Interval group to be copied
IntervalND<T, N>& operator =(const IntervalND<T, N>& interval);
/// Move assignment operator
/// \param i Interval to be moved
IntervalND<T, N>& operator=(IntervalND<T, N>&& i);
/// \param interval the interval group the current group will be tested against
/// \return true if the input group interval overlaps the current group
bool overlapsWith(const IntervalND<T, N>& interval) const;
/// \param interval the interval group to be tested
/// \param epsilon the nearness parameter
/// \return true if each interval in the input group is approximately equal to its correspondent
/// element in interval.
bool isApprox(const IntervalND<T, N>& interval, const T& epsilon) const;
/// \param interval the interval group to be tested
/// \return true if the current interval group is identical to the input group
bool operator ==(const IntervalND<T, N>& interval) const;
/// \param interval the interval group to be tested
/// \return true if the current interval group is not identical to the input group
bool operator !=(const IntervalND<T, N>& interval) const;
/// Widens every interval in the current interval group by thickness on both sides
/// \param thickness the amount to widen on both sides
/// \return the current interval group after modification
IntervalND<T, N>& addThickness(const T& thickness);
/// @{
/// Standard arithmetic operators extended to interval groups
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);
/// @}
/// \return the inverse of each interval in the interval group
/// \exception if any interval includes 0
IntervalND<T, N> inverse() const;
/// \param interval the interval to be divided by
/// \return the product of each interval in the group multiplied by the inverse of
/// its correspondent in interval
/// \exception if any component of interval includes 0
IntervalND<T, N> operator /(const IntervalND<T, N>& interval) const;
/// \param interval the interval to be divided by
/// \return the product of each interval in the group multiplied by the inverse of
/// its correspondent in interval
/// \note the current interval is modified by this operation
IntervalND<T, N>& operator /=(const IntervalND<T, N>& interval);
/// \param interval the input interval group
/// \return the interval dot product of the current group and interval
Interval<T> dotProduct(const IntervalND<T, N>& interval) const;
/// \return the square of the interval magnitude for the current group
Interval<T> magnitudeSquared() const;
/// \return the interval magnitude for the current group
Interval<T> magnitude() const;
/// \param i the selector for the interval to be returned
/// \return the ith interval in the current group
const Interval<T>& getAxis(size_t i) const;
private:
/// The N dimensional group of intervals
std::array<Interval<T>, N> m_interval;
};
/// IntervalND<T,3> defines the concept of a group of mathematical intervals specialized to 3 intervals and provides
/// operations on them including arithmetic operations, construction, and IO.
///
/// \tparam T underlying data type over which the interval is defined.
///
/// \sa Interval<T> and IntervalND<T, N>
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:
/// Constructor
IntervalND();
/// Constructor
/// \param x array of 3 intervals to be copied into the group
explicit IntervalND(const std::array<Interval<T>, 3>& x);
/// Constructor
/// \param x first interval to be added to the 3 group
/// \param y second interval to be added to the 3 group
/// \param z third interval to be added to the 3 group
IntervalND(const Interval<T>& x, const Interval<T>& y, const Interval<T>& z);
/// Copy constructor
/// \param i interval 3 group to copied
IntervalND(const IntervalND<T, 3>& i);
/// Move constructor
/// \param i Interval to be copied
IntervalND(IntervalND<T, 3>&& i);
/// Constructor
/// \param a array of 3 values to be used as the respective minimums for the interval entries.
/// \param b array of 3 values to be used as the respective maximums for the interval entries.
IntervalND(const std::array<T, 3>& a, const std::array<T, 3>& b);
/// Assignment operator
/// \param i Interval 3 group to be copied
IntervalND<T, 3>& operator =(const IntervalND<T, 3>& i);
/// Move assignment operator
/// \param i Interval to be moved
IntervalND<T, 3>& operator=(IntervalND<T, 3>&& i);
/// \param interval the interval group the current group will be tested against
/// \return true if the input 3 group interval overlaps the current 3 group
bool overlapsWith(const IntervalND<T, 3>& interval) const;
/// \param interval the interval group to be tested
/// \param epsilon the nearness parameter
/// \return true if each interval in the input group is approximately equal to its correspondent
/// element in interval.
bool isApprox(const IntervalND<T, 3>& interval, const T& epsilon) const;
/// \param i the interval group to be tested
/// \return true if the current interval 3 group is identical to the input 3 group i
bool operator ==(const IntervalND<T, 3>& i) const;
/// \param i the interval group to be tested
/// \return true if the current interval 3 group is not identical to the input 3 group i
bool operator !=(const IntervalND<T, 3>& i) const;
/// Widens every interval in the current interval group by thickness on both sides
/// \param thickness the amount to widen on both sides
/// \return the current interval group after modification
IntervalND<T, 3>& addThickness(const T& thickness);
/// @{
/// Standard arithmetic operators extended to 3 interval groups
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);
/// @}
/// \return the inverse of each interval in the 3 interval group
/// \exception if any interval includes 0
IntervalND<T, 3> inverse() const;
/// \param i the interval to be divided by
/// \return the product of each interval in the 3 group multiplied by the inverse of
/// its correspondent in i
/// \exception if any component of interval includes 0
IntervalND<T, 3> operator /(const IntervalND<T, 3>& i) const;
/// \param i the interval to be divided by
/// \return the product of each interval in the 3 group multiplied by the inverse of
/// its correspondent in i
/// \note the current interval is modified by this operation
IntervalND<T, 3>& operator /=(const IntervalND<T, 3>& i);
/// \param i the input interval group
/// \return the interval dot product of the current 3 group and interval
Interval<T> dotProduct(const IntervalND<T, 3>& i) const;
/// \param i the input interval group
/// \return the interval cross product of the current 3 group and interval
IntervalND<T, 3> crossProduct(const IntervalND<T, 3>& i) const;
/// \return the square of the interval magnitude for the current 3 group
Interval<T> magnitudeSquared() const;
/// \return the interval magnitude for the current 3 group
Interval<T> magnitude() const;
/// \param i the selector for the interval to be returned
/// \return the ith interval in the current 3 group
const Interval<T>& getAxis(size_t i) const;
private:
/// The 3 dimensional group of intervals
std::array<Interval<T>, 3> m_interval;
};
// Interval utilities
/// \tparam T underlying type of the interval
/// \param v the scalar
/// \param i the interval
/// \return the sum of the scalar v and the interval i
template <typename T>
Interval<T> operator+ (T v, const Interval<T>& i);
/// \tparam T underlying type of the interval
/// \param v the scalar
/// \param i the interval
/// \return the product of the scalar v and the interval i
template <typename T>
Interval<T> operator* (T v, const Interval<T>& i);
/// Write a textual version of the interval to an output stream
/// \tparam T underlying type of the interval
/// \param o the ostream to be written to
/// \param interval the interval to write
/// \return the active ostream
template <typename T>
std::ostream& operator<< (std::ostream& o, const Interval<T>& interval);
/// Calculate the sum of two intervals
/// \tparam P underlying type of the interval
/// \param a the first interval
/// \param b the second interval
/// \param res [out] the result of a + b
template <class P>
void IntervalArithmetic_add(const Interval<P>& a, const Interval<P>& b,
Interval<P>* res); // +
/// Calculate the sum of three intervals res + a + b
/// \tparam P underlying type of the interval
/// \param a the first interval
/// \param b the second interval
/// \param res [in/out] the result of res + a + b
template <class P>
void IntervalArithmetic_addadd(const Interval<P>& a, const Interval<P>& b,
Interval<P>* res); // +=( + )
/// Calculate the difference of two intervals
/// \tparam P underlying type of the interval
/// \param a the first interval
/// \param b the second interval
/// \param res [out] the result of a - b
template <class P>
void IntervalArithmetic_sub(const Interval<P>& a, const Interval<P>& b,
Interval<P>* res); // -
/// Add the difference of two intervals to an existing value
/// \tparam P underlying type of the interval
/// \param a the first interval
/// \param b the second interval
/// \param res [in/out] the result of res + (a - b)
template <class P>
void IntervalArithmetic_addsub(const Interval<P>& a, const Interval<P>& b,
Interval<P>* res); // +=( - )
/// Calculate the product of two intervals
/// \tparam P underlying type of the interval
/// \param a the first interval
/// \param b the second interval
/// \param res [out] the result of a * b
template <class P>
void IntervalArithmetic_mul(const Interval<P>& a, const Interval<P>& b,
Interval<P>* res); // *
/// Add the product of two intervals to an existing value
/// \tparam P underlying type of the interval
/// \param a the first interval
/// \param b the second interval
/// \param res [in/out] the result of res + (a * b)
template <class P>
void IntervalArithmetic_addmul(const Interval<P>& a, const Interval<P>& b,
Interval<P>* res); // += ( * )
/// Subtract the product of two intervals from an existing value
/// \tparam P underlying type of the interval
/// \param a the first interval
/// \param b the second interval
/// \param res [in/out] the result of res - (a * b)
template <class P>
void IntervalArithmetic_submul(const Interval<P>& a, const Interval<P>& b,
Interval<P>* res); // -= ( * )
// Interval ND utilities
/// Write a textual version of an interval group to an output stream
/// \tparam T underlying type of the interval
/// \tparam N number of intervals in the group
/// \param o the ostream to be written to
/// \param interval the interval group to write
/// \return the active ostream
template <typename T, int N>
std::ostream& operator<< (std::ostream& o, const IntervalND<T, N>& interval);
// Interval 3D utilities
/// Calculate the sum of two interval groups
/// \tparam P underlying type of the interval
/// \param a the first interval group
/// \param b the second interval group
/// \param res [out] the result of a + b
template <class P>
void IntervalArithmetic_add(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b,
IntervalND<P, 3>* res);
/// Calculate the difference of two interval groups
/// \tparam P underlying type of the interval
/// \param a the first interval group
/// \param b the second interval group
/// \param res [out] the result of a - b
template <class P>
void IntervalArithmetic_sub(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b,
IntervalND<P, 3>* res);
/// Calculate the dot product of two interval groups
/// \tparam P underlying type of the interval
/// \param a the first interval group
/// \param b the second interval group
/// \param res [out] the dot product of a and b
template <class P>
void IntervalArithmetic_dotProduct(const IntervalND<P, 3>& a, const IntervalND<P, 3>& b,
Interval<P>* res);
/// Calculate the cross product of two interval groups
/// \tparam P underlying type of the interval
/// \param a the first interval group
/// \param b the second interval group
/// \param res [out] the cross product of a and b
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
|