00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __RECTANGLE_H__
00012 #define __RECTANGLE_H__
00013
00014 BEGIN_FASTDB_NAMESPACE
00015
00016 #ifndef RECTANGLE_DIMENSION
00017 #define RECTANGLE_DIMENSION 2
00018 #endif
00019
00020 #ifndef RECTANGLE_COORDINATE_TYPE
00021 #define RECTANGLE_COORDINATE_TYPE int4
00022 #define RECTANGLE_AREA_TYPE int8
00023 #endif
00024
00025
00026 typedef RECTANGLE_COORDINATE_TYPE coord_t;
00027 typedef RECTANGLE_AREA_TYPE area_t;
00028
00032 class FASTDB_DLL_ENTRY rectangle
00033 {
00034 public:
00035 enum { dim = RECTANGLE_DIMENSION };
00042 coord_t boundary[dim*2];
00043
00047 friend coord_t FASTDB_DLL_ENTRY distance(rectangle const& r, rectangle const& q);
00048
00052 friend area_t area(rectangle const& r) {
00053 area_t area = 1;
00054 for (int i = dim; --i >= 0; area *= r.boundary[i+dim] - r.boundary[i]);
00055 return area;
00056 }
00057
00061 void operator +=(rectangle const& r) {
00062 int i = dim;
00063 while (--i >= 0) {
00064 boundary[i] = (boundary[i] <= r.boundary[i])
00065 ? boundary[i] : r.boundary[i];
00066 boundary[i+dim] = (boundary[i+dim] >= r.boundary[i+dim])
00067 ? boundary[i+dim] : r.boundary[i+dim];
00068 }
00069 }
00073 rectangle operator + (rectangle const& r) const {
00074 rectangle res;
00075 int i = dim;
00076 while (--i >= 0) {
00077 res.boundary[i] = (boundary[i] <= r.boundary[i])
00078 ? boundary[i] : r.boundary[i];
00079 res.boundary[i+dim] = (boundary[i+dim] >= r.boundary[i+dim])
00080 ? boundary[i+dim] : r.boundary[i+dim];
00081 }
00082 return res;
00083 }
00087 bool operator & (rectangle const& r) const {
00088 int i = dim;
00089 while (--i >= 0) {
00090 if (boundary[i] > r.boundary[i+dim] ||
00091 r.boundary[i] > boundary[i+dim])
00092 {
00093 return false;
00094 }
00095 }
00096 return true;
00097 }
00102 bool operator <= (rectangle const& r) const {
00103 int i = dim;
00104 while (--i >= 0) {
00105 if (boundary[i] < r.boundary[i] ||
00106 boundary[i+dim] > r.boundary[i+dim])
00107 {
00108 return false;
00109 }
00110 }
00111 return true;
00112 }
00117 bool operator >= (rectangle const& r) const {
00118 int i = dim;
00119 while (--i >= 0) {
00120 if (r.boundary[i] < boundary[i] ||
00121 r.boundary[i+dim] > boundary[i+dim])
00122 {
00123 return false;
00124 }
00125 }
00126 return true;
00127 }
00128
00133 bool operator < (rectangle const& r) const {
00134 return *this <= r && *this != r;
00135 }
00140 bool operator > (rectangle const& r) const {
00141 return *this >= r && *this != r;
00142 }
00146 bool operator == (rectangle const& r) const {
00147 int i = dim*2;
00148 while (--i >= 0) {
00149 if (boundary[i] != r.boundary[i]) {
00150 return false;
00151 }
00152 }
00153 return true;
00154 }
00158 bool operator != (rectangle const& r) const {
00159 int i = dim*2;
00160 while (--i >= 0) {
00161 if (boundary[i] != r.boundary[i]) {
00162 return true;
00163 }
00164 }
00165 return false;
00166 }
00167
00168 typedef bool (rectangle::*comparator)(rectangle const& r) const;
00169 };
00170
00171 END_FASTDB_NAMESPACE
00172
00173 #endif
00174
00175