00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef _Dimensions_
00024 #define _Dimensions_
00025
00026 #include <iosfwd>
00027
00028
00029 namespace PIRL
00030 {
00031
00032
00033
00034 #ifndef COORDINATE_TYPE
00035 #define COORDINATE_TYPE int
00036 #endif
00037 #ifndef DIMENSIONS_TYPE
00038 #define DIMENSIONS_TYPE unsigned COORDINATE_TYPE
00039 #endif
00040
00041 typedef COORDINATE_TYPE Coordinate_Type;
00043 typedef DIMENSIONS_TYPE Dimensions_Type;
00044
00045
00057 inline Coordinate_Type Round (double number)
00058 {return (Coordinate_Type)(number > 0 ? (number + 0.5) : (number - 0.5));}
00059
00060
00066 struct Point_2D
00067 {
00069 static const char* const
00070 ID;
00071
00072
00074 Coordinate_Type
00075 X;
00077 Coordinate_Type
00078 Y;
00079
00080
00081
00082
00084 Point_2D ();
00085
00091 Point_2D (const Coordinate_Type& x,const Coordinate_Type& y);
00092
00097 Point_2D (const Point_2D& point);
00098
00099
00100
00101
00108 inline Point_2D& position (const Coordinate_Type& x,const Coordinate_Type& y)
00109 {
00110 X = x;
00111 Y = y;
00112 return *this;
00113 }
00114
00121 inline Point_2D& position (const Point_2D& point)
00122 {
00123 X = point.X;
00124 Y = point.Y;
00125 return *this;
00126 }
00127
00133 inline Point_2D& operator= (const Point_2D& point)
00134 {
00135 if (this != &point)
00136 {
00137 X = point.X;
00138 Y = point.Y;
00139 }
00140 return *this;
00141 }
00142
00148 inline Point_2D& x (const Coordinate_Type& x_position)
00149 {X = x_position; return *this;}
00150
00155 inline Coordinate_Type x () const
00156 {return X;}
00157
00163 inline Point_2D& y (const Coordinate_Type& y_position)
00164 {Y = y_position; return *this;}
00165
00170 inline Coordinate_Type y () const
00171 {return Y;}
00172
00180 inline bool operator== (const Point_2D& point) const
00181 {return X == point.X && Y == point.Y;}
00182
00191 inline bool operator!= (const Point_2D& point) const
00192 {return X != point.X || Y != point.Y;}
00193
00198 inline operator bool ()
00199 {return X != 0 || Y != 0;}
00200
00201
00202
00203
00209 inline Point_2D& operator+= (const Point_2D& offset)
00210 {X += offset.X; Y += offset.Y; return *this;}
00211
00217 inline Point_2D& operator-= (const Point_2D& offset)
00218 {X -= offset.X; Y -= offset.Y; return *this;}
00219
00228 inline Point_2D& operator*= (double factor)
00229 {X = Round (X * factor); Y = Round (Y * factor); return *this;}
00230
00243 Point_2D& operator/= (double factor);
00244
00245 };
00246
00254 inline Point_2D operator+ (const Point_2D& point_1, const Point_2D& point_2)
00255 {return Point_2D (point_1) += point_2;}
00256
00264 inline Point_2D operator- (const Point_2D& point_1, const Point_2D& point_2)
00265 {return Point_2D (point_1) -= point_2;}
00266
00273 inline Point_2D operator- (const Point_2D& point)
00274 {return Point_2D (-point.X, -point.Y);}
00275
00284 inline Point_2D operator* (const Point_2D& point, double factor)
00285 {return Point_2D (point) *= factor;}
00286
00295 inline Point_2D operator* (double factor, const Point_2D& point)
00296 {return Point_2D (point) *= factor;}
00297
00306 inline Point_2D operator/ (const Point_2D& point, double factor)
00307 {return Point_2D (point) /= factor;}
00308
00315 std::ostream& operator<< (std::ostream& stream, const Point_2D& point);
00316
00317
00323 struct Size_2D
00324 {
00326 Dimensions_Type
00327 Width;
00329 Dimensions_Type
00330 Height;
00331
00332
00333
00334
00339 Size_2D ();
00340
00346 Size_2D (const Dimensions_Type& width, const Dimensions_Type& height);
00347
00353 Size_2D (const Dimensions_Type& side);
00354
00359 Size_2D (const Size_2D& size);
00360
00361
00362
00363
00370 inline Size_2D& size
00371 (const Dimensions_Type& width, const Dimensions_Type& height)
00372 {
00373 Width = width;
00374 Height = height;
00375 return *this;
00376 }
00377
00384 inline Size_2D& size (const Size_2D& size)
00385 {
00386 Width = size.Width;
00387 Height = size.Height;
00388 return *this;
00389 }
00390
00396 inline Size_2D& operator= (const Size_2D& size)
00397 {
00398 if (this != &size)
00399 {
00400 Width = size.Width;
00401 Height = size.Height;
00402 }
00403 return *this;
00404 }
00405
00411 inline Size_2D& width (const Dimensions_Type& width)
00412 {Width = width; return *this;}
00413
00418 inline Dimensions_Type width () const
00419 {return Width;}
00420
00426 inline Size_2D& height (const Dimensions_Type& height)
00427 {Height = height; return *this;}
00428
00433 inline Dimensions_Type height () const
00434 {return Height;}
00435
00440 inline unsigned long long area () const
00441 {return Width * Height;}
00442
00451 inline bool operator== (const Size_2D& size) const
00452 {return Width == size.Width && Height == size.Height;}
00453
00462 inline bool operator!= (const Size_2D& size) const
00463 {return Width != size.Width || Height != size.Height;}
00464
00469 inline operator bool ()
00470 {return Width != 0 || Height != 0;}
00471
00472
00473
00474
00480 inline Size_2D& operator+= (const Size_2D& size)
00481 {Width += size.Width; Height += size.Height; return *this;}
00482
00492 Size_2D& operator-= (const Size_2D& size);
00493
00503 Size_2D& operator*= (double factor);
00504
00519 Size_2D& operator/= (double factor);
00520
00521 };
00522
00530 inline Size_2D operator+ (const Size_2D& size_1, const Size_2D& size_2)
00531 {return Size_2D (size_1) += size_2;}
00532
00544 inline Size_2D operator- (const Size_2D& size_1, const Size_2D& size_2)
00545 {return Size_2D (size_1) -= size_2;}
00546
00557 inline Size_2D operator* (const Size_2D& size, double factor)
00558 {return Size_2D (size) *= factor;}
00559
00570 inline Size_2D operator* (double factor, const Size_2D& size)
00571 {return Size_2D (size) *= factor;}
00572
00583 inline Size_2D operator/ (const Size_2D& size, double factor)
00584 {return Size_2D (size) /= factor;}
00585
00592 std::ostream& operator<< (std::ostream& stream, const Size_2D& size);
00593
00594
00605 struct Rectangle
00606 : public Point_2D,
00607 public Size_2D
00608 {
00609
00610
00611
00616 Rectangle ();
00617
00625 Rectangle
00626 (
00627 const Coordinate_Type x,
00628 const Coordinate_Type y,
00629 const Dimensions_Type width = 0,
00630 const Dimensions_Type height = 0
00631 );
00632
00638 Rectangle (const Point_2D& position, const Size_2D& size);
00639
00644 Rectangle (const Size_2D& size);
00645
00650 Rectangle (const Rectangle& rectangle);
00651
00652
00653
00654
00661 inline Rectangle& position (const Coordinate_Type& x,const Coordinate_Type& y)
00662 {Point_2D::position (x, y); return *this;}
00663
00670 inline Rectangle& position (const Point_2D& point)
00671 {Point_2D::position (point); return *this;}
00672
00678 inline Rectangle& operator= (const Point_2D& point)
00679 {Point_2D::operator= (point); return *this;}
00680
00686 inline Point_2D position () const
00687 {return Point_2D (X, Y);}
00688
00693 inline operator Point_2D () const
00694 {return position ();}
00695
00702 inline Rectangle& size
00703 (const Dimensions_Type& width, const Dimensions_Type& height)
00704 {Size_2D::size (width, height); return *this;}
00705
00712 inline Rectangle& size (const Size_2D& size)
00713 {Size_2D::size (size); return *this;}
00714
00720 inline Rectangle& operator= (const Size_2D& size)
00721 {Size_2D::operator= (size); return *this;}
00722
00728 inline Size_2D size () const
00729 {return Size_2D (Width, Height);}
00730
00735 inline operator Size_2D () const
00736 {return size ();}
00737
00744 inline Rectangle& operator= (const Rectangle& rectangle)
00745 {
00746 if (this != &rectangle)
00747 {
00748 X = rectangle.X;
00749 Y = rectangle.Y;
00750 Width = rectangle.Width;
00751 Height = rectangle.Height;
00752 }
00753 return *this;
00754 }
00755
00765 inline bool operator== (const Rectangle& rectangle) const
00766 {return Point_2D::operator== ((const Point_2D)rectangle) &&
00767 Size_2D::operator== ((const Size_2D)rectangle);}
00768
00778 inline bool operator!= (const Rectangle& rectangle) const
00779 {return Point_2D::operator!= ((const Point_2D)rectangle) ||
00780 Size_2D::operator!= ((const Size_2D)rectangle);}
00781
00786 inline operator bool ()
00787 {return Point_2D::operator bool () || Size_2D::operator bool ();}
00788
00789
00790
00791
00797 inline Rectangle& operator+= (const Point_2D& offset)
00798 {Point_2D::operator+= (offset); return *this;}
00799
00805 inline Rectangle& operator+= (const Size_2D& size)
00806 {Size_2D::operator+= (size); return *this;}
00807
00815 inline Rectangle& operator+= (const Rectangle& rectangle)
00816 {
00817 Point_2D::operator+= (static_cast<Point_2D>(rectangle));
00818 Size_2D::operator+= (static_cast<Size_2D>(rectangle));
00819 return *this;
00820 }
00821
00827 inline Rectangle& operator-= (const Point_2D& offset)
00828 {Point_2D::operator-= (offset); return *this;}
00829
00839 inline Rectangle& operator-= (const Size_2D& size)
00840 {Size_2D::operator-= (size); return *this;}
00841
00849 inline Rectangle& operator-= (const Rectangle& rectangle)
00850 {
00851 Point_2D::operator-= (static_cast<Point_2D>(rectangle));
00852 Size_2D::operator-= (static_cast<Size_2D>(rectangle));
00853 return *this;
00854 }
00855
00865 inline Rectangle& operator*= (double factor)
00866 {Size_2D::operator*= (factor); return *this;}
00867
00882 inline Rectangle& operator/= (double factor)
00883 {Size_2D::operator/= (factor); return *this;}
00884
00893 Rectangle& operator&= (const Rectangle& rectangle);
00894
00895 };
00896
00904 inline Rectangle operator+ (const Rectangle& rectangle, const Point_2D& point)
00905 {return Rectangle (rectangle) += point;}
00906
00914 inline Rectangle operator+ (const Point_2D& point, const Rectangle& rectangle)
00915 {return Rectangle (rectangle) += point;}
00916
00924 inline Rectangle operator+
00925 (const Rectangle& rectangle_1, const Rectangle& rectangle_2)
00926 {return Rectangle (rectangle_1) += rectangle_2;}
00927
00936 inline Rectangle operator- (const Rectangle& rectangle, const Point_2D& point)
00937 {return Rectangle (rectangle) -= point;}
00938
00946 inline Rectangle operator-
00947 (const Rectangle& rectangle_1, const Rectangle& rectangle_2)
00948 {return Rectangle (rectangle_1) -= rectangle_2;}
00949
00956 inline Rectangle operator- (const Rectangle& rectangle)
00957 {return Rectangle (-rectangle.X, -rectangle.Y,
00958 rectangle.Width, rectangle.Height);}
00959
00967 inline Rectangle operator+ (const Rectangle& rectangle, const Size_2D& size)
00968 {return Rectangle (rectangle) += size;}
00969
00978 inline Rectangle operator+ (const Size_2D& size, const Rectangle& rectangle)
00979 {return Rectangle (rectangle) += size;}
00980
00992 inline Rectangle operator- (const Rectangle& rectangle, const Size_2D& size)
00993 {return Rectangle (rectangle) -= size;}
00994
01005 inline Rectangle operator* (const Rectangle& rectangle, double factor)
01006 {return Rectangle (rectangle) *= factor;}
01007
01018 inline Rectangle operator* (double factor, const Rectangle& rectangle)
01019 {return Rectangle (rectangle) *= factor;}
01020
01031 inline Rectangle operator/ (const Rectangle& rectangle, double factor)
01032 {return Rectangle (rectangle) /= factor;}
01033
01044 inline Rectangle operator&
01045 (const Rectangle& rectangle_1, const Rectangle& rectangle_2)
01046 {return Rectangle (rectangle_1) &= rectangle_2;}
01047
01054 std::ostream& operator<< (std::ostream& stream, const Rectangle& rectangle);
01055
01056
01063 struct Cube
01064 : public Rectangle
01065 {
01067 Dimensions_Type
01068 Depth;
01069
01070
01071
01072
01077 Cube ();
01078
01087 Cube
01088 (
01089 const Coordinate_Type x,
01090 const Coordinate_Type y,
01091 const Dimensions_Type width = 0,
01092 const Dimensions_Type height = 0,
01093 const Dimensions_Type depth = 0
01094 );
01095
01103 Cube (const Point_2D& position, const Size_2D& size);
01104
01109 Cube (const Size_2D& size);
01110
01115 Cube (const Rectangle& rectangle);
01116
01121 Cube (const Cube& cube);
01122
01123
01124
01125
01132 inline Cube& position (const Coordinate_Type& x,const Coordinate_Type& y)
01133 {Point_2D::position (x, y); return *this;}
01134
01141 inline Cube& position (const Point_2D& point)
01142 {Point_2D::position (point); return *this;}
01143
01149 inline Cube& operator= (const Point_2D& point)
01150 {Point_2D::operator= (point); return *this;}
01151
01158 inline Cube& size
01159 (const Dimensions_Type& width, const Dimensions_Type& height)
01160 {Size_2D::size (width, height); return *this;}
01161
01168 inline Cube& size (const Size_2D& size)
01169 {Size_2D::size (size); return *this;}
01170
01176 inline Cube& depth (const Dimensions_Type& depth)
01177 {Depth = depth; return *this;}
01178
01183 inline Dimensions_Type depth () const
01184 {return Depth;}
01185
01191 inline Cube& operator= (const Size_2D& size)
01192 {Size_2D::operator= (size); return *this;}
01193
01200 inline Cube& dimensions (const Rectangle& rectangle)
01201 {Rectangle::operator= (rectangle); return *this;}
01202
01209 inline Cube& operator= (const Rectangle& rectangle)
01210 {Rectangle::operator= (rectangle); return *this;}
01211
01217 inline Cube& depth (Dimensions_Type depth)
01218 {Depth = depth; return *this;}
01219
01226 inline Cube& operator= (const Cube& cube)
01227 {
01228 if (this != &cube)
01229 {
01230 X = cube.X;
01231 Y = cube.Y;
01232 Width = cube.Width;
01233 Height = cube.Height;
01234 Depth = cube.Depth;
01235 }
01236 return *this;
01237 }
01238
01243 inline unsigned long long volume () const
01244 {return (long long)area () * Depth;}
01245
01254 inline bool operator== (const Cube& cube) const
01255 {return Depth == cube.Depth &&
01256 Rectangle::operator== ((const Rectangle)cube);}
01257
01266 inline bool operator!= (const Cube& cube) const
01267 {return Depth != cube.Depth ||
01268 Rectangle::operator!= ((const Rectangle)cube);}
01269
01274 inline operator bool ()
01275 {return Rectangle::operator bool () || Depth != 0;}
01276
01277 };
01278
01285 std::ostream& operator<< (std::ostream& stream, const Cube& cube);
01286
01287
01288 }
01289 #endif