00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef INTERVAL_H
00018 #define INTERVAL_H
00019
00020 #include <camvox/GMath.h>
00021
00022 namespace camvox {
00023
00024
00025
00026 template <class T>
00027 class TInterval {
00028 public:
00029 T low, high;
00030
00031 TInterval(T _low, T _high) {
00032 low = _low;
00033 high = _high;
00034 }
00035
00036 TInterval(T value) {
00037 low = high = value;
00038 }
00039
00040 ~TInterval() {}
00041
00042 TInterval operator+(const TInterval &other) const {
00043 T low = this->low + other.low;
00044 T high = (-this->high) + (-other.high);
00045
00046 return TInterval(low, -high);
00047 }
00048
00049 TInterval operator-(const TInterval &other) const {
00050 T low = this->low + (-other.high);
00051 T high = (-this->high) + other.low;
00052
00053 return TInterval(low, -high);
00054 }
00055
00056 TInterval operator*(const TInterval &other) const {
00057 T low = gmin(
00058 gmin(this->low * other.low, this->low * other.high),
00059 gmin(this->high * other.low, this->high * other.high)
00060 );
00061 T high = gmax(
00062 gmax(this->low * other.low, this->low * other.high),
00063 gmax(this->high * other.low, this->high * other.high)
00064 );
00065
00066 return TInterval(low, high);
00067 }
00068
00069 TInterval operator/(const TInterval &other) const {
00070 T low = gmin(
00071 gmin(this->low / other.low, this->low / other.high),
00072 gmin(this->high / other.low, this->high / other.high)
00073 );
00074 T high = gmax(
00075 gmax(this->low / other.low, this->low / other.high),
00076 gmax(this->high / other.low, this->high / other.high)
00077 );
00078
00079 return TInterval(low, high);
00080 }
00081
00082 TInterval abs(void) const {
00083 T low = gmax(gmax(this->low, -this->high), 0.0);
00084 T high = gmax(-this->low, this->high);
00085 return TInterval(low, high);
00086 }
00087
00088 TInterval square(void) const {
00089 TInterval tmp = this->abs();
00090
00091 T low = gmin(tmp.low * tmp.low, tmp.high * tmp.low);
00092 T high = gmax(tmp.high * tmp.high, tmp.low * tmp.high);
00093
00094 return TInterval(low, high);
00095 }
00096
00097 TInterval squareroot(void) const {
00098 TInterval tmp = this->abs();
00099
00100 T low = sqrt(tmp.low);
00101 T high = sqrt(tmp.high);
00102
00103 return TInterval(low, high);
00104 }
00105 };
00106
00107 typedef TInterval<double> Interval;
00108
00109 static inline Interval gsquare(Interval a) {
00110 return a.square();
00111 }
00112
00113 }
00114 #endif