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
00199 inline operator bool ()
00200 {return X != 0 || Y != 0;}
00201
00207 inline bool is_null ()
00208 {return X == 0 && Y == 0;}
00209
00210
00211
00212
00218 inline Point_2D& operator+= (const Point_2D& offset)
00219 {X += offset.X; Y += offset.Y; return *this;}
00220
00226 inline Point_2D& operator-= (const Point_2D& offset)
00227 {X -= offset.X; Y -= offset.Y; return *this;}
00228
00237 inline Point_2D& operator*= (double factor)
00238 {X = Round (X * factor); Y = Round (Y * factor); return *this;}
00239
00256 Point_2D& operator/= (double factor);
00257
00258 };
00259
00267 inline Point_2D operator+ (const Point_2D& point_1, const Point_2D& point_2)
00268 {return Point_2D (point_1) += point_2;}
00269
00277 inline Point_2D operator- (const Point_2D& point_1, const Point_2D& point_2)
00278 {return Point_2D (point_1) -= point_2;}
00279
00286 inline Point_2D operator- (const Point_2D& point)
00287 {return Point_2D (-point.X, -point.Y);}
00288
00297 inline Point_2D operator* (const Point_2D& point, double factor)
00298 {return Point_2D (point) *= factor;}
00299
00308 inline Point_2D operator* (double factor, const Point_2D& point)
00309 {return Point_2D (point) *= factor;}
00310
00319 inline Point_2D operator/ (const Point_2D& point, double factor)
00320 {return Point_2D (point) /= factor;}
00321
00328 std::ostream& operator<< (std::ostream& stream, const Point_2D& point);
00329
00330
00336 struct Size_2D
00337 {
00339 Dimensions_Type
00340 Width;
00342 Dimensions_Type
00343 Height;
00344
00345
00346
00347
00352 Size_2D ();
00353
00359 Size_2D (const Dimensions_Type& width, const Dimensions_Type& height);
00360
00366 Size_2D (const Dimensions_Type& side);
00367
00372 Size_2D (const Size_2D& size);
00373
00374
00375
00376
00383 inline Size_2D& size
00384 (const Dimensions_Type& width, const Dimensions_Type& height)
00385 {
00386 Width = width;
00387 Height = height;
00388 return *this;
00389 }
00390
00397 inline Size_2D& size (const Size_2D& size)
00398 {
00399 Width = size.Width;
00400 Height = size.Height;
00401 return *this;
00402 }
00403
00409 inline Size_2D& operator= (const Size_2D& size)
00410 {
00411 if (this != &size)
00412 {
00413 Width = size.Width;
00414 Height = size.Height;
00415 }
00416 return *this;
00417 }
00418
00424 inline Size_2D& width (const Dimensions_Type& width)
00425 {Width = width; return *this;}
00426
00431 inline Dimensions_Type width () const
00432 {return Width;}
00433
00439 inline Size_2D& height (const Dimensions_Type& height)
00440 {Height = height; return *this;}
00441
00446 inline Dimensions_Type height () const
00447 {return Height;}
00448
00453 inline unsigned long long area () const
00454 {return (long long)Width * Height;}
00455
00464 inline bool operator== (const Size_2D& size) const
00465 {return Width == size.Width && Height == size.Height;}
00466
00475 inline bool operator!= (const Size_2D& size) const
00476 {return Width != size.Width || Height != size.Height;}
00477
00482 inline operator bool ()
00483 {return Width != 0 || Height != 0;}
00484
00489 inline bool is_empty ()
00490 {return Width == 0 || Height == 0;}
00491
00492
00493
00494
00500 inline Size_2D& operator+= (const Size_2D& size)
00501 {Width += size.Width; Height += size.Height; return *this;}
00502
00512 Size_2D& operator-= (const Size_2D& size);
00513
00523 Size_2D& operator*= (double factor);
00524
00544 Size_2D& operator/= (double factor);
00545
00546 };
00547
00555 inline Size_2D operator+ (const Size_2D& size_1, const Size_2D& size_2)
00556 {return Size_2D (size_1) += size_2;}
00557
00569 inline Size_2D operator- (const Size_2D& size_1, const Size_2D& size_2)
00570 {return Size_2D (size_1) -= size_2;}
00571
00582 inline Size_2D operator* (const Size_2D& size, double factor)
00583 {return Size_2D (size) *= factor;}
00584
00595 inline Size_2D operator* (double factor, const Size_2D& size)
00596 {return Size_2D (size) *= factor;}
00597
00608 inline Size_2D operator/ (const Size_2D& size, double factor)
00609 {return Size_2D (size) /= factor;}
00610
00617 std::ostream& operator<< (std::ostream& stream, const Size_2D& size);
00618
00619
00632 struct Rectangle
00633 : public Point_2D,
00634 public Size_2D
00635 {
00636
00637
00638
00643 Rectangle ();
00644
00652 Rectangle
00653 (
00654 const Coordinate_Type x,
00655 const Coordinate_Type y,
00656 const Dimensions_Type width = 0,
00657 const Dimensions_Type height = 0
00658 );
00659
00665 Rectangle (const Point_2D& position, const Size_2D& size);
00666
00671 Rectangle (const Size_2D& size);
00672
00677 Rectangle (const Rectangle& rectangle);
00678
00679
00680
00681
00688 inline Rectangle& position (const Coordinate_Type& x,const Coordinate_Type& y)
00689 {Point_2D::position (x, y); return *this;}
00690
00697 inline Rectangle& position (const Point_2D& point)
00698 {Point_2D::position (point); return *this;}
00699
00705 inline Rectangle& operator= (const Point_2D& point)
00706 {Point_2D::operator= (point); return *this;}
00707
00713 inline Point_2D position () const
00714 {return Point_2D (X, Y);}
00715
00720 inline operator Point_2D () const
00721 {return position ();}
00722
00729 inline Rectangle& size
00730 (const Dimensions_Type& width, const Dimensions_Type& height)
00731 {Size_2D::size (width, height); return *this;}
00732
00739 inline Rectangle& size (const Size_2D& size)
00740 {Size_2D::size (size); return *this;}
00741
00747 inline Rectangle& operator= (const Size_2D& size)
00748 {Size_2D::operator= (size); return *this;}
00749
00755 inline Size_2D size () const
00756 {return Size_2D (Width, Height);}
00757
00762 inline operator Size_2D () const
00763 {return size ();}
00764
00771 inline Rectangle& operator= (const Rectangle& rectangle)
00772 {
00773 if (this != &rectangle)
00774 {
00775 X = rectangle.X;
00776 Y = rectangle.Y;
00777 Width = rectangle.Width;
00778 Height = rectangle.Height;
00779 }
00780 return *this;
00781 }
00782
00792 inline bool operator== (const Rectangle& rectangle) const
00793 {return Point_2D::operator== ((const Point_2D)rectangle) &&
00794 Size_2D::operator== ((const Size_2D)rectangle);}
00795
00805 inline bool operator!= (const Rectangle& rectangle) const
00806 {return Point_2D::operator!= ((const Point_2D)rectangle) ||
00807 Size_2D::operator!= ((const Size_2D)rectangle);}
00808
00813 inline operator bool ()
00814 {return Point_2D::operator bool () || Size_2D::operator bool ();}
00815
00816
00817
00818
00824 inline Rectangle& operator+= (const Point_2D& offset)
00825 {Point_2D::operator+= (offset); return *this;}
00826
00832 inline Rectangle& operator+= (const Size_2D& size)
00833 {Size_2D::operator+= (size); return *this;}
00834
00842 inline Rectangle& operator+= (const Rectangle& rectangle)
00843 {
00844 Point_2D::operator+= (static_cast<Point_2D>(rectangle));
00845 Size_2D::operator+= (static_cast<Size_2D>(rectangle));
00846 return *this;
00847 }
00848
00854 inline Rectangle& operator-= (const Point_2D& offset)
00855 {Point_2D::operator-= (offset); return *this;}
00856
00866 inline Rectangle& operator-= (const Size_2D& size)
00867 {Size_2D::operator-= (size); return *this;}
00868
00876 inline Rectangle& operator-= (const Rectangle& rectangle)
00877 {
00878 Point_2D::operator-= (static_cast<Point_2D>(rectangle));
00879 Size_2D::operator-= (static_cast<Size_2D>(rectangle));
00880 return *this;
00881 }
00882
00892 inline Rectangle& operator*= (double factor)
00893 {Size_2D::operator*= (factor); return *this;}
00894
00913 inline Rectangle& operator/= (double factor)
00914 {Size_2D::operator/= (factor); return *this;}
00915
00926 Rectangle& operator&= (const Rectangle& rectangle);
00927
00936 Rectangle& operator|= (const Rectangle& rectangle);
00937
00938 };
00939
00947 inline Rectangle operator+ (const Rectangle& rectangle, const Point_2D& point)
00948 {return Rectangle (rectangle) += point;}
00949
00957 inline Rectangle operator+ (const Point_2D& point, const Rectangle& rectangle)
00958 {return Rectangle (rectangle) += point;}
00959
00967 inline Rectangle operator+
00968 (const Rectangle& rectangle_1, const Rectangle& rectangle_2)
00969 {return Rectangle (rectangle_1) += rectangle_2;}
00970
00979 inline Rectangle operator- (const Rectangle& rectangle, const Point_2D& point)
00980 {return Rectangle (rectangle) -= point;}
00981
00989 inline Rectangle operator-
00990 (const Rectangle& rectangle_1, const Rectangle& rectangle_2)
00991 {return Rectangle (rectangle_1) -= rectangle_2;}
00992
00999 inline Rectangle operator- (const Rectangle& rectangle)
01000 {return Rectangle (-rectangle.X, -rectangle.Y,
01001 rectangle.Width, rectangle.Height);}
01002
01010 inline Rectangle operator+ (const Rectangle& rectangle, const Size_2D& size)
01011 {return Rectangle (rectangle) += size;}
01012
01021 inline Rectangle operator+ (const Size_2D& size, const Rectangle& rectangle)
01022 {return Rectangle (rectangle) += size;}
01023
01035 inline Rectangle operator- (const Rectangle& rectangle, const Size_2D& size)
01036 {return Rectangle (rectangle) -= size;}
01037
01048 inline Rectangle operator* (const Rectangle& rectangle, double factor)
01049 {return Rectangle (rectangle) *= factor;}
01050
01061 inline Rectangle operator* (double factor, const Rectangle& rectangle)
01062 {return Rectangle (rectangle) *= factor;}
01063
01085 inline Rectangle operator/ (const Rectangle& rectangle, double factor)
01086 {return Rectangle (rectangle) /= factor;}
01087
01097 inline Rectangle operator&
01098 (const Rectangle& rectangle_1, const Rectangle& rectangle_2)
01099 {return Rectangle (rectangle_1) &= rectangle_2;}
01100
01108 inline Rectangle& operator|
01109 (const Rectangle& rectangle_1, const Rectangle& rectangle_2)
01110 {return Rectangle (rectangle_1) |= rectangle_2;}
01111
01118 std::ostream& operator<< (std::ostream& stream, const Rectangle& rectangle);
01119
01120
01133 struct Cube
01134 : public Rectangle
01135 {
01137 Dimensions_Type
01138 Depth;
01139
01140
01141
01142
01147 Cube ();
01148
01157 Cube
01158 (
01159 const Coordinate_Type x,
01160 const Coordinate_Type y,
01161 const Dimensions_Type width = 0,
01162 const Dimensions_Type height = 0,
01163 const Dimensions_Type depth = 0
01164 );
01165
01173 Cube (const Point_2D& position, const Size_2D& size);
01174
01181 Cube (const Size_2D& size);
01182
01189 Cube (const Rectangle& rectangle);
01190
01195 Cube (const Cube& cube);
01196
01197
01198
01199
01206 inline Cube& position (const Coordinate_Type& x,const Coordinate_Type& y)
01207 {Point_2D::position (x, y); return *this;}
01208
01215 inline Cube& position (const Point_2D& point)
01216 {Point_2D::position (point); return *this;}
01217
01223 inline Cube& operator= (const Point_2D& point)
01224 {Point_2D::operator= (point); return *this;}
01225
01232 inline Cube& size
01233 (const Dimensions_Type& width, const Dimensions_Type& height)
01234 {Size_2D::size (width, height); return *this;}
01235
01242 inline Cube& size (const Size_2D& size)
01243 {Size_2D::size (size); return *this;}
01244
01250 inline Cube& depth (Dimensions_Type depth)
01251 {Depth = depth; return *this;}
01252
01257 inline Dimensions_Type depth () const
01258 {return Depth;}
01259
01265 inline Cube& operator= (const Size_2D& size)
01266 {Size_2D::operator= (size); return *this;}
01267
01274 inline Cube& dimensions (const Rectangle& rectangle)
01275 {Rectangle::operator= (rectangle); return *this;}
01276
01283 inline Cube& operator= (const Rectangle& rectangle)
01284 {Rectangle::operator= (rectangle); return *this;}
01285
01292 inline Cube& operator= (const Cube& cube)
01293 {
01294 if (this != &cube)
01295 {
01296 X = cube.X;
01297 Y = cube.Y;
01298 Width = cube.Width;
01299 Height = cube.Height;
01300 Depth = cube.Depth;
01301 }
01302 return *this;
01303 }
01304
01309 inline unsigned long long volume () const
01310 {return area () * Depth;}
01311
01320 inline bool operator== (const Cube& cube) const
01321 {return Depth == cube.Depth &&
01322 Rectangle::operator== ((const Rectangle)cube);}
01323
01332 inline bool operator!= (const Cube& cube) const
01333 {return Depth != cube.Depth ||
01334 Rectangle::operator!= ((const Rectangle)cube);}
01335
01340 inline operator bool ()
01341 {return Depth != 0 || Rectangle::operator bool ();}
01342
01347 inline bool is_empty ()
01348 {return Depth == 0 || Size_2D::is_empty ();}
01349
01350
01351
01352
01362 Cube& operator+= (int amount);
01363
01372 inline Cube& operator+= (const Cube& cube)
01373 {
01374 Rectangle::operator+= (static_cast<Rectangle>(cube));
01375 operator+= (cube.Depth);
01376 return *this;
01377 }
01378
01388 inline Cube& operator-= (int amount)
01389 {operator+= (-amount); return *this;}
01390
01399 inline Cube& operator-= (const Cube& cube)
01400 {
01401 Rectangle::operator-= (static_cast<Rectangle>(cube));
01402 operator-= (cube.Depth);
01403 return *this;
01404 }
01405
01415 inline Cube& operator*= (double factor)
01416 {
01417 Rectangle::operator*= (factor);
01418 Depth = Round (Depth * factor);
01419 return *this;
01420 }
01421
01440 Cube& operator/= (double factor);
01441
01453 Cube& operator&= (const Cube& cube);
01454
01464 Cube& operator|= (const Cube& cube);
01465
01466 };
01467
01476 inline Cube operator+ (const Cube& cube, int amount)
01477 {return Cube (cube) += amount;}
01478
01488 inline Cube operator- (const Cube& cube, int amount)
01489 {return Cube (cube) -= amount;}
01490
01499 inline Cube operator+
01500 (const Cube& cube_1, const Cube& cube_2)
01501 {return Cube (cube_1) += cube_2;}
01502
01511 inline Cube operator-
01512 (const Cube& cube_1, const Cube& cube_2)
01513 {return Cube (cube_1) -= cube_2;}
01514
01525 inline Cube operator* (const Cube& cube, double factor)
01526 {return Cube (cube) *= factor;}
01527
01538 inline Cube operator* (double factor, const Cube& cube)
01539 {return Cube (cube) *= factor;}
01540
01562 inline Cube operator/ (const Cube& cube, double factor)
01563 {return Cube (cube) /= factor;}
01564
01577 inline Cube operator&
01578 (const Cube& cube_1, const Cube& cube_2)
01579 {return Cube (cube_1) &= cube_2;}
01580
01591 inline Cube& operator|
01592 (const Cube& cube_1, const Cube& cube_2)
01593 {return Cube (cube_1) |= cube_2;}
01594
01601 std::ostream& operator<< (std::ostream& stream, const Cube& cube);
01602
01603
01604 }
01605 #endif