00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __UT_BoundingRect_H__
00023 #define __UT_BoundingRect_H__
00024
00025 #include "UT_API.h"
00026 #include <iostream.h>
00027 #include <SYS/SYS_Types.h>
00028 #include "UT_Assert.h"
00029
00030 class UT_Vector2;
00031
00032 class UT_API UT_BoundingRect {
00033 public:
00034 UT_BoundingRect() {}
00035 UT_BoundingRect(fpreal xmin,fpreal ymin, fpreal xmax,fpreal ymax)
00036 {
00037 vals[0][0] = xmin; vals[1][0] = ymin;
00038 vals[0][1] = xmax; vals[1][1] = ymax;
00039 }
00040
00041 UT_BoundingRect(const UT_Vector2 &lowerbound,
00042 const UT_Vector2 &upperbound);
00043
00044 float operator()(unsigned m, unsigned n) const
00045 {
00046 UT_ASSERT_P( m < 2 && n < 2 );
00047 return vals[m][n];
00048 }
00049 float &operator()(unsigned m, unsigned n)
00050 {
00051 UT_ASSERT_P( m < 2 && n < 2 );
00052 return vals[m][n];
00053 }
00054
00055 int operator==(const UT_BoundingRect &brect) const
00056 {
00057 return (vals[0][0] == brect.vals[0][0] &&
00058 vals[0][1] == brect.vals[0][1] &&
00059 vals[1][0] == brect.vals[1][0] &&
00060 vals[1][1] == brect.vals[1][1] );
00061 }
00062
00063 int contains(const UT_Vector2 &pt) const;
00064 int contains(const UT_Vector2 &pt, fpreal tol) const;
00065 int contains(fpreal x, fpreal y) const
00066 {
00067 return (x >= vals[0][0] && x <= vals[0][1] &&
00068 y >= vals[1][0] && y <= vals[1][1] );
00069 }
00070
00071 int isInside(const UT_BoundingRect &brect) const
00072 {
00073
00074 return (vals[0][0] >= brect.vals[0][0] &&
00075 vals[0][1] <= brect.vals[0][1] &&
00076 vals[1][0] >= brect.vals[1][0] &&
00077 vals[1][1] <= brect.vals[1][1] );
00078 }
00079
00080
00081 int intersects(const UT_Vector2 &v0, const UT_Vector2 &v1) const;
00082 int intersects(const UT_BoundingRect &rect) const;
00083 int intersects(const UT_BoundingRect &rect, fpreal tol) const;
00084
00085 void initBounds(const UT_Vector2 &pt);
00086 void initBounds(fpreal x, fpreal y)
00087 {
00088 vals[0][0] = vals[0][1] = x;
00089 vals[1][0] = vals[1][1] = y;
00090 }
00091 void initBounds(fpreal xmin, fpreal xmax, fpreal ymin, fpreal ymax)
00092 {
00093 vals[0][0] = xmin; vals[0][1] = xmax;
00094 vals[1][0] = ymin; vals[1][1] = ymax;
00095 }
00096 void initBounds(const UT_BoundingRect &rect)
00097 {
00098 vals[0][0] = rect.vals[0][0];
00099 vals[0][1] = rect.vals[0][1];
00100 vals[1][0] = rect.vals[1][0];
00101 vals[1][1] = rect.vals[1][1];
00102 }
00103
00104 void enlargeBounds(const UT_Vector2 &pt);
00105 void enlargeBounds(fpreal x, fpreal y)
00106 {
00107 if (x < vals[0][0]) vals[0][0] = x;
00108 else if (x > vals[0][1]) vals[0][1] = x;
00109 if (y < vals[1][0]) vals[1][0] = y;
00110 else if (y > vals[1][1]) vals[1][1] = y;
00111 }
00112 void enlargeBounds(fpreal xmin, fpreal xmax, fpreal ymin, fpreal ymax)
00113 {
00114 if (xmin < vals[0][0]) vals[0][0] = xmin;
00115 if (xmax > vals[0][1]) vals[0][1] = xmax;
00116 if (ymin < vals[1][0]) vals[1][0] = ymin;
00117 if (ymax > vals[1][1]) vals[1][1] = ymax;
00118 }
00119 void enlargeBounds(const UT_BoundingRect &rect)
00120 {
00121 if (rect(0, 0) < vals[0][0]) vals[0][0] = rect(0, 0);
00122 if (rect(0, 1) > vals[0][1]) vals[0][1] = rect(0, 1);
00123 if (rect(1, 0) < vals[1][0]) vals[1][0] = rect(1, 0);
00124 if (rect(1, 1) > vals[1][1]) vals[1][1] = rect(1, 1);
00125 }
00126 void stretch(fpreal percent = 0.001F, fpreal min = 0.001F)
00127 {
00128 fpreal d;
00129 d = min + (vals[0][1] - vals[0][0])*percent;
00130 vals[0][0] -= d; vals[0][1] += d;
00131 d = min + (vals[1][1] - vals[1][0])*percent;
00132 vals[1][0] -= d; vals[1][1] += d;
00133 }
00134 void translate(fpreal x, fpreal y)
00135 {
00136 vals[0][0] += x;
00137 vals[0][1] += x;
00138 vals[1][0] += y;
00139 vals[1][1] += y;
00140 }
00141
00142 fpreal sizeX() const { return vals[0][1] - vals[0][0]; }
00143 fpreal sizeY() const { return vals[1][1] - vals[1][0]; }
00144
00145 fpreal centerX() const { return (vals[0][0] + vals[0][1]) * .5f; }
00146 fpreal centerY() const { return (vals[1][0] + vals[1][1]) * .5f; }
00147
00148
00149
00150
00151
00152 void project(float &x, float &y, int *touchx=0, int *touchy=0) const;
00153
00154 fpreal LX() const { return vals[0][0]; }
00155 fpreal LY() const { return vals[1][0]; }
00156 fpreal UX() const { return vals[0][1]; }
00157 fpreal UY() const { return vals[1][1]; }
00158
00159 float &LX() { return vals[0][0]; }
00160 float &LY() { return vals[1][0]; }
00161 float &UX() { return vals[0][1]; }
00162 float &UY() { return vals[1][1]; }
00163
00164
00165
00166
00167
00168 int intersectRay(const UT_Vector2 &orig, const UT_Vector2 &dir,
00169 fpreal tmax = 1E17F,
00170 float *distance = 0,
00171 UT_Vector2 *xsect = 0) const;
00172
00173
00174 void save(ostream &os, int binary = 0) const;
00175
00176
00177 friend ostream &operator<<(ostream &os, const UT_BoundingRect &brect)
00178 {
00179 brect.save(os);
00180 return os;
00181 }
00182 float vals[2][2];
00183 private:
00184 };
00185
00186 #endif