-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDimensions.hh
1605 lines (1267 loc) · 48 KB
/
Dimensions.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Dimensions
PIRL CVS ID: $Id: Dimensions.hh,v 1.27 2011/02/18 02:29:28 castalia Exp $
Copyright (C) 2010 Arizona Board of Regents on behalf of the
Planetary Image Research Laboratory, Lunar and Planetary Laboratory at
the University of Arizona.
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License, version 2.1,
as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#ifndef _Dimensions_
#define _Dimensions_
#include <iosfwd>
namespace PIRL
{
/*==============================================================================
Types
*/
#ifndef COORDINATE_TYPE
#define COORDINATE_TYPE int
#endif
#ifndef DIMENSIONS_TYPE
#define DIMENSIONS_TYPE unsigned COORDINATE_TYPE
#endif
//! The integer data type of a coordinate value.
typedef COORDINATE_TYPE Coordinate_Type;
//! The integer data type of a dimension value.
typedef DIMENSIONS_TYPE Dimensions_Type;
/** Rounds a floating point number to the nearest integer value.
The nearest integer value is found by adding 0.5 to the floating
point number and truncating the result to the Coordinate_Type integer
value. For negative numbers 0.5 is subtracted. Thus a floating point
number exactly halfway beteen two integer values is rounded to the
absolute larger integer with the sign preserved.
@param number The floating point (double) number to be rounded.
@return The integer value of the rounded number.
*/
inline Coordinate_Type Round (double number)
{return (Coordinate_Type)(number > 0 ? (number + 0.5) : (number - 0.5));}
//******************************************************************************
/** A <i>Point_2D</i> holds 2-dimensional position information.
@author Bradford Castalia, UA/PIRL
@version $Revision: 1.27 $
*/
struct Point_2D
{
//! Class identification name with source code version and date.
static const char* const
ID;
//! The horizontal (x-axis) position of the Point_2D.
Coordinate_Type
X;
//! The vertical (y-axis) position of the Point_2D.
Coordinate_Type
Y;
/*==============================================================================
Constructors:
*/
//! Constructs a Point_2D at position 0,0.
Point_2D ();
/** Constructs a Point_2D at position x,y.
@param x The horizontal (x-axis) position of the Point_2D.
@param y The vertical (y-axis) position of the Point_2D.
*/
Point_2D (const Coordinate_Type& x,const Coordinate_Type& y);
/** Constructs a Point_2D from another Point_2D.
@param point A Point_2D to be copied.
*/
Point_2D (const Point_2D& point);
/*==============================================================================
Accessors:
*/
/** Set the position of this Point_2D.
@param x The horizontal (x-axis) position of the Point_2D.
@param y The vertical (y-axis) position of the Point_2D.
@return This Point_2D.
*/
inline Point_2D& position (const Coordinate_Type& x,const Coordinate_Type& y)
{
X = x;
Y = y;
return *this;
}
/** Set the position of this Point_2D.
@param point A Point_2D whose coordinates are to be assigned to
this Point_2D.
@return This Point_2D.
*/
inline Point_2D& position (const Point_2D& point)
{
X = point.X;
Y = point.Y;
return *this;
}
/** Assign the position of another Point_2D to this Point_2D.
@param point A Point_2D whose coordinates are to be assigned to
this Point_2D.
*/
inline Point_2D& operator= (const Point_2D& point)
{
if (this != &point)
{
X = point.X;
Y = point.Y;
}
return *this;
}
/** Set the horizontal (x-axis) position.
@param x_position The horizontal (x-axis) position of the Point_2D.
@return This Point_2D.
*/
inline Point_2D& x (const Coordinate_Type& x_position)
{X = x_position; return *this;}
/** Get the horizontal (x-axis) position.
@return The horizontal (x-axis) position of the Point_2D.
*/
inline Coordinate_Type x () const
{return X;}
/** Set the vertical (y-axis) position.
@param y_position The vertical (y-axis) position of the Point_2D.
@return This Point_2D.
*/
inline Point_2D& y (const Coordinate_Type& y_position)
{Y = y_position; return *this;}
/** Get the vertical (y-axis) position.
@return The vertical (y-axis) position of the Point_2D.
*/
inline Coordinate_Type y () const
{return Y;}
/** Test if this Point_2D is equal to another Point_2D.
The two Point_2Ds are equal if both their X and Y coordinates are equal.
@param point The Point_2D to which this Point_2D is to be compared.
@return true if the two Point_2Ds are equal; false otherwise.
*/
inline bool operator== (const Point_2D& point) const
{return X == point.X && Y == point.Y;}
/** Test if this Point_2D is not equal to another Point_2D.
The two Point_2Ds are not equal if either their X or Y coordinates are
not equal.
@param point The Point_2D to which this Point_2D is to be compared.
@return true if the two Point_2Ds are not equal; false otherwise.
*/
inline bool operator!= (const Point_2D& point) const
{return X != point.X || Y != point.Y;}
/** Test for all zero coordinate values.
@return true if any coordinate value is non-zero; false otherwise.
@see is_null()
*/
inline operator bool ()
{return X != 0 || Y != 0;}
/** Test for all zero coordinate values.
@return true if both coordinates are zero; false otherwise.
@see operator bool()
*/
inline bool is_null ()
{return X == 0 && Y == 0;}
/*==============================================================================
Manipulators:
*/
/** Add an offset.
@param offset A Point_2D that provides the offset values.
@return This Point_2D with its values offset.
*/
inline Point_2D& operator+= (const Point_2D& offset)
{X += offset.X; Y += offset.Y; return *this;}
/** Subtract an offset.
@param offset A Point_2D that provides the offset values.
@return This Point_2D with its values offset.
*/
inline Point_2D& operator-= (const Point_2D& offset)
{X -= offset.X; Y -= offset.Y; return *this;}
/** Multiply by a factor.
The new coordinate values will be rounded to the nearest
Coordinate_Type values.
@param factor A factor by which to multiply the Point_2D coordinates.
@return This Point_2D with its values changed.
*/
inline Point_2D& operator*= (double factor)
{X = Round (X * factor); Y = Round (Y * factor); return *this;}
/** Divide by a factor.
The new coordinate values will be rounded to the nearest
Coordinate_Type values.
<b>N.B.</b>: Divide by zero is handled as a special case. If the
coordinate value was zero it will remain zero. If the Coordinate_Type
has an infinity value (determined by numeric_types) that is used, or
its negative if the coordinate value is negative. Otherwise, if the
Coordinate_Type is signed the type's max value is used, or the min
value if the coordinate value is negative; for an unsigned value the
max value is used.
@param factor A factor by which to divide the Point_2D dimensions.
@return This Point_2D with its values changed.
*/
Point_2D& operator/= (double factor);
}; // End of Point_2D class.
/** Add two points.
@param point_1 A Point_2D.
@param point_2 A Point_2D.
@return A Point_2D in which the dimensions are the sum of the
dimensions of the specified points.
*/
inline Point_2D operator+ (const Point_2D& point_1, const Point_2D& point_2)
{return Point_2D (point_1) += point_2;}
/** Subtract one point from another.
@param point_1 A Point_2D.
@param point_2 A Point_2D.
@return A Point_2D in which the dimensions are the difference of the
dimensions of the specified points.
*/
inline Point_2D operator- (const Point_2D& point_1, const Point_2D& point_2)
{return Point_2D (point_1) -= point_2;}
/** Get the negation of a point.
@param point A Point_2D.
@return A Point_2D in which the dimensions are the negation of the
dimensions of the specified points.
*/
inline Point_2D operator- (const Point_2D& point)
{return Point_2D (-point.X, -point.Y);}
/** Multiply a point by a factor.
@param point A Point_2D.
@param factor A floating point (double) number.
@return A Point_2D in which the dimensions are the dimensions of the
specified point multiplied by the factor and {@link Round(double)
rounded}.
*/
inline Point_2D operator* (const Point_2D& point, double factor)
{return Point_2D (point) *= factor;}
/** Multiply a point by a factor.
@param factor A floating point (double) number.
@param point A Point_2D.
@return A Point_2D in which the dimensions are the dimensions of the
specified point multiplied by the factor and {@link Round(double)
rounded}.
*/
inline Point_2D operator* (double factor, const Point_2D& point)
{return Point_2D (point) *= factor;}
/** Divide a point by a factor.
@param point A Point_2D.
@param factor A floating point (double) number.
@return A Point_2D in which the dimensions are the dimensions of the
specified point divided by the factor and {@link Round(double)
rounded}.
*/
inline Point_2D operator/ (const Point_2D& point, double factor)
{return Point_2D (point) /= factor;}
/** Print a Point_2D description to an output stream.
@param stream The ostream where the Point_2D will be printed.
@param point The Print_2D to be printed.
@return The stream that was written.
*/
std::ostream& operator<< (std::ostream& stream, const Point_2D& point);
//******************************************************************************
/** A <i>Size_2D</i> holds 2-dimensional size information.
@author Bradford Castalia, UA/PIRL
@version $Revision: 1.27 $
*/
struct Size_2D
{
//! The Width of the Size_2D.
Dimensions_Type
Width;
//! The Height of the Size_2D.
Dimensions_Type
Height;
/*==============================================================================
Constructors:
*/
/** Constructs an empty Size_2D.
Both Width and Height are zero.
*/
Size_2D ();
/** Constructs a Size_2D with width,height size.
@param width The Width of the Size_2D.
@param height The Height of the Size_2D.
*/
Size_2D (const Dimensions_Type& width, const Dimensions_Type& height);
/** Constructs a Size_2D of equal Width and Height.
@param side The length of each side. Both Width and Height will
be set to this value.
*/
Size_2D (const Dimensions_Type& side);
/** Constructs a Size_2D from another Size_2D.
@param size A Size_2D to be copied.
*/
Size_2D (const Size_2D& size);
/*==============================================================================
Accessors:
*/
/** Set the size of this Size_2D.
@param width The Width of the Size_2D.
@param height The Height of the Size_2D.
@return This Size_2D.
*/
inline Size_2D& size
(const Dimensions_Type& width, const Dimensions_Type& height)
{
Width = width;
Height = height;
return *this;
}
/** Set the size of this Size_2D.
@param size A Size_2D to have its dimensions assigned to this
Size_2D.
@return This Size_2D.
*/
inline Size_2D& size (const Size_2D& size)
{
Width = size.Width;
Height = size.Height;
return *this;
}
/** Assign the dimensions of another Size_2D to this Size_2D.
@param size A Size_2D to have its dimensions assigned to this
Size_2D.
*/
inline Size_2D& operator= (const Size_2D& size)
{
if (this != &size)
{
Width = size.Width;
Height = size.Height;
}
return *this;
}
/** Set the Width of the Size_2D.
@param width The Width of the Size_2D.
@return This Size_2D.
*/
inline Size_2D& width (const Dimensions_Type& width)
{Width = width; return *this;}
/** Get the Width of the Size_2D.
@return The Width of the Size_2D.
*/
inline Dimensions_Type width () const
{return Width;}
/** Set the Height of the Size_2D.
@param height The Height of the Size_2D.
@return This Size_2D.
*/
inline Size_2D& height (const Dimensions_Type& height)
{Height = height; return *this;}
/** Get the Height of the Size_2D.
@return The Height of the Size_2D.
*/
inline Dimensions_Type height () const
{return Height;}
/** Get the area of this Size_2D.
@return The area (Width * Height) of the Size_2D.
*/
inline unsigned long long area () const
{return (long long)Width * Height;}
/** Test if this Size_2D is equal to another Size_2D.
The two Size_2Ds are equal if both their Width and Height dimensions
are equal.
@param size The Size_2D to which this Size_2D is to be compared.
@return true if the two Size_2Ds are equal; false otherwise.
*/
inline bool operator== (const Size_2D& size) const
{return Width == size.Width && Height == size.Height;}
/** Test if this Size_2D is not equal to another Size_2D.
The two Size_2Ds are not equal if either their Width or Height
dimensions are not equal.
@param size The Size_2D to which this Size_2D is to be compared.
@return true if the two Size_2Ds are not equal; false otherwise.
*/
inline bool operator!= (const Size_2D& size) const
{return Width != size.Width || Height != size.Height;}
/** Test for all zero dimension values.
@return true if any dimension value is non-zero; false otherwise.
*/
inline operator bool ()
{return Width != 0 || Height != 0;}
/** Test for any zero dimension values.
@return true if any dimension is zero; false otherwise.
*/
inline bool is_empty ()
{return Width == 0 || Height == 0;}
/*==============================================================================
Manipulators:
*/
/** Add a size amount.
@param size A Size_2D that provides the amount values.
@return This Size_2D with the amount added to its values.
*/
inline Size_2D& operator+= (const Size_2D& size)
{Width += size.Width; Height += size.Height; return *this;}
/** Subtract a size amount.
<b>N.B.</b>: If the size Dimensions_Type values are an unsigned type
(as they are by default) and a size amount to be subtracted is greater
than the corresponding dimension value, the result will be zero.
@param size A Size_2D that provides the amount values.
@return This Size_2D with the amount subtracted to its values.
*/
Size_2D& operator-= (const Size_2D& size);
/** Multiply by a factor.
The new size values will be rounded to the nearest Dimensions_Types.
@param factor A factor by which to multiply the Size_2D dimensions.
@return This Size_2D with its values changed.
@throws invalid_argument If the factor is negative and the size values
Dimensions_Type are an unsigned type (as they are by default).
*/
Size_2D& operator*= (double factor);
/** Divide by a factor.
The new dimension values will be rounded to the nearest Dimensions_Type
values.
<b>N.B.</b>: Divide by zero is handled as a special case. If the
coordinate value was zero it will remain zero. If the Dimensions_Type
has an infinity value (determined by numeric_types) that is used, or
its negative if the coordinate value is negative. Otherwise, if the
Dimensions_Type is signed the type's max value is used, or the min
value if the coordinate value is negative; for an unsigned value the
max value is used.
@param factor A factor by which to divide the Size_2D dimensions.
@return This Size_2D with its values changed.
@throws invalid_argument If the factor is negative and the size
values Dimensions_Type are an unsigned type (as they are by
default).
*/
Size_2D& operator/= (double factor);
}; // End of Size_2D class.
/** Add two sizes.
@param size_1 A Size_2D.
@param size_2 A Size_2D.
@return A Size_2D in which the dimensions are the sum of the
dimensions of the specified sizes.
*/
inline Size_2D operator+ (const Size_2D& size_1, const Size_2D& size_2)
{return Size_2D (size_1) += size_2;}
/** Subtract one size from another.
<b>N.B.</b>: If the size Dimensions_Type values are an unsigned type
(as they are by default) and a size amount to be subtracted is greater
than the corresponding dimension value, the result will be zero.
@param size_1 A Size_2D.
@param size_2 A Size_2D.
@return A Size_2D in which the dimensions are the difference of the
dimensions of the specified sizes.
*/
inline Size_2D operator- (const Size_2D& size_1, const Size_2D& size_2)
{return Size_2D (size_1) -= size_2;}
/** Multiply a size by a factor.
@param size A Size_2D.
@param factor A floating point (double) number.
@return A Size_2D in which the dimensions are the dimensions of the
specified size multiplied by the factor and {@link Round(double)
rounded}.
@throws invalid_argument If the factor is negative and the size values
Dimensions_Type are an unsigned type (as they are by default).
*/
inline Size_2D operator* (const Size_2D& size, double factor)
{return Size_2D (size) *= factor;}
/** Multiply a size by a factor.
@param factor A floating point (double) number.
@param size A Size_2D.
@return A Size_2D in which the dimensions are the dimensions of the
specified size multiplied by the factor and {@link Round(double)
rounded}.
@throws invalid_argument If the factor is negative and the size values
Dimensions_Type are an unsigned type (as they are by default).
*/
inline Size_2D operator* (double factor, const Size_2D& size)
{return Size_2D (size) *= factor;}
/** Divide a size by a factor.
@param size A Size_2D.
@param factor A floating point (double) number.
@return A Size_2D in which the dimensions are the dimensions of the
specified size divided by the factor and {@link Round(double)
rounded}.
@throws invalid_argument If the factor is negative and the size values
Dimensions_Type are an unsigned type (as they are by default).
*/
inline Size_2D operator/ (const Size_2D& size, double factor)
{return Size_2D (size) /= factor;}
/** Print a Size_2D description to an output stream.
@param stream The ostream where the Size_2D will be printed.
@param size The Size_2D to be printed.
@return The stream that was written.
*/
std::ostream& operator<< (std::ostream& stream, const Size_2D& size);
//******************************************************************************
/** A <i>Rectangle</i> is a position with a size.
The Rectangle's position is based in a Point_2D being at the upper
left corner of the Rectangle, and its size is based in a Size_2D with
the X-axis Width increasing to the right and the Y-axis Height
increasing downward (raster order).
@author Bradford Castalia, UA/PIRL
@version $Revision: 1.27 $
@see Point_2D
@see Size_2D
*/
struct Rectangle
: public Point_2D,
public Size_2D
{
/*==============================================================================
Constructors:
*/
/** Constructs an empty Rectangle.
The position is 0,0 and the size is 0,0.
*/
Rectangle ();
/** Constructs a Rectangle from an x,y position and width,height size.
@param x The horizontal (x-axis) position.
@param y The vertical (y-axis) position.
@param width The Width of the Rectangle.
@param height The Height of the Rectangle.
*/
Rectangle
(
const Coordinate_Type x,
const Coordinate_Type y,
const Dimensions_Type width = 0,
const Dimensions_Type height = 0
);
/** Constructs a Rectangle from a position and a size.
@param position A Point_2D.
@param size A Size_2D.
*/
Rectangle (const Point_2D& position, const Size_2D& size);
/** Constructs a Rectangle from a size at position 0,0.
@param size A Size_2D.
*/
Rectangle (const Size_2D& size);
/** Constructs a Rectangle as a copy of another Rectangle.
@param rectangle A Rectangle to be copied.
*/
Rectangle (const Rectangle& rectangle);
/*==============================================================================
Accessors:
*/
/** Set the position of this Rectangle.
@param x The horizontal (x-axis) position of the Rectangle.
@param y The vertical (y-axis) position of the Rectangle.
@return This Rectangle.
*/
inline Rectangle& position (const Coordinate_Type& x,const Coordinate_Type& y)
{Point_2D::position (x, y); return *this;}
/** Set the position of this Rectangle.
@param point A Point_2D whose dimensions are to be assigned to
this Rectangle.
@return This Rectangle.
*/
inline Rectangle& position (const Point_2D& point)
{Point_2D::position (point); return *this;}
/** Assign the position of the Rectangle from a Point_2D.
@param point A Point_2D for the position of the Rectangle.
@return This Rectangle.
*/
inline Rectangle& operator= (const Point_2D& point)
{Point_2D::operator= (point); return *this;}
/** Get the Rectangle position.
@return A Point_2D with the Rectangle position. <b>N.B.</b>: Changing
this Point_2D will not change the position of the Rectangle.
*/
inline Point_2D position () const
{return Point_2D (X, Y);}
/** Convert the Rectangle to its corresponding Point_2D.
@return A Point_2D with the Rectangle position.
*/
inline operator Point_2D () const
{return position ();}
/** Set the size of this Rectangle.
@param width The Width of the Rectangle.
@param height The Height of the Rectangle.
@return This Rectangle.
*/
inline Rectangle& size
(const Dimensions_Type& width, const Dimensions_Type& height)
{Size_2D::size (width, height); return *this;}
/** Set the size of this Rectangle.
@param size A Size_2D to have its dimensions assigned to this
Rectangle.
@return This Rectangle.
*/
inline Rectangle& size (const Size_2D& size)
{Size_2D::size (size); return *this;}
/** Assign the size of the Rectangle from a Size_2D.
@param size A Size_2D for the size of the Rectangle.
@return This Rectangle.
*/
inline Rectangle& operator= (const Size_2D& size)
{Size_2D::operator= (size); return *this;}
/** Get the Rectangle size.
@return A Size_2D with the Rectangle size. <b>N.B.</b>: Changing
this size will not change the size of the Rectangle.
*/
inline Size_2D size () const
{return Size_2D (Width, Height);}
/** Convert the Rectangle to its corresponding Size_2D.
@return A Size_2D with the Rectangle size.
*/
inline operator Size_2D () const
{return size ();}
/** Assign the position and size of another Rectangle to this Rectangle.
@param rectangle A Rectangle whose position and size are to be
assigned to this Rectangle.
@return This Rectangle.
*/
inline Rectangle& operator= (const Rectangle& rectangle)
{
if (this != &rectangle)
{
X = rectangle.X;
Y = rectangle.Y;
Width = rectangle.Width;
Height = rectangle.Height;
}
return *this;
}
/** Test if this Rectangle is equal to another Rectangle.
The two Rectangles are equal if both their position and size
dimensions are equal.
@param rectangle The Rectangle to which this Rectangle is to be
compared.
@return true if the two Rectangles are equal; false otherwise.
*/
inline bool operator== (const Rectangle& rectangle) const
{return Point_2D::operator== ((const Point_2D)rectangle) &&
Size_2D::operator== ((const Size_2D)rectangle);}
/** Test if this Rectangle is not equal to another Rectangle.
The two Rectangles are not equal if either their position or size
dimensions are not equal.
@param rectangle The Rectangle to which this Rectangle is to be
compared.
@return true if the two Rectangles are not equal; false otherwise.
*/
inline bool operator!= (const Rectangle& rectangle) const
{return Point_2D::operator!= ((const Point_2D)rectangle) ||
Size_2D::operator!= ((const Size_2D)rectangle);}
/** Test for all zero dimension values.
@return true if any dimension value is non-zero; false otherwise.
*/
inline operator bool ()
{return Point_2D::operator bool () || Size_2D::operator bool ();}
/*==============================================================================
Manipulators:
*/
/** Add an offset.
@param offset A Point_2D that provides the offset values.
@return This Rectangle with its values offset.
*/
inline Rectangle& operator+= (const Point_2D& offset)
{Point_2D::operator+= (offset); return *this;}
/** Add a size amount.
@param size A Size_2D that provides the amount values.
@return This Rectangle with the amount added to its size values.
*/
inline Rectangle& operator+= (const Size_2D& size)
{Size_2D::operator+= (size); return *this;}
/** Add another Rectangle's point coordinate offset and size amount.
@param rectangle A Rectangle.
@return This Rectangle with its point coordinate offset by the
other Rectangle's point coordinate values, and the other Rectangle's
size amount added to its size dimensions.
*/
inline Rectangle& operator+= (const Rectangle& rectangle)
{
Point_2D::operator+= (static_cast<Point_2D>(rectangle));
Size_2D::operator+= (static_cast<Size_2D>(rectangle));
return *this;
}
/** Subtract an offset.
@param offset A Point_2D that provides the offset values.
@return This Rectangle with its values offset.
*/
inline Rectangle& operator-= (const Point_2D& offset)
{Point_2D::operator-= (offset); return *this;}
/** Subtract a size amount.
<b>N.B.</b>: If the size Dimensions_Type values are an unsigned type
(as they are by default) and a size amount to be subtracted is greater
than the corresponding dimension value, the result will be zero.
@param size A Size_2D that provides the amount values.
@return This Rectangle with the amount subtracted to its values.
*/
inline Rectangle& operator-= (const Size_2D& size)
{Size_2D::operator-= (size); return *this;}
/** Subtract another Rectangle's point coordinate offset and size amount.
@param rectangle A Rectangle.
@return This Rectangle with its point coordinate offset by the
other Rectangle's point coordinate values, and the other Rectangle's
size amount subtracted from its size dimensions.
*/
inline Rectangle& operator-= (const Rectangle& rectangle)
{
Point_2D::operator-= (static_cast<Point_2D>(rectangle));
Size_2D::operator-= (static_cast<Size_2D>(rectangle));
return *this;
}
/** Multiply by a factor.
The new size values will be rounded to the nearest Dimensions_Types.
@param factor A factor by which to multiply the Size_2D dimensions.
@return This Rectangle with its values changed.
@throws invalid_argument If the factor is negative and the size values
Dimensions_Type are an unsigned type (as they are by default).
*/
inline Rectangle& operator*= (double factor)
{Size_2D::operator*= (factor); return *this;}
/** Divide by a factor.
The new size dimensions will be rounded to the nearest Dimensions_Type
values.
<b>N.B.</b>: Divide by zero is handled as a special case. If the
coordinate value was zero it will remain zero. If the Dimensions_Type
has an infinity value (determined by numeric_types) that is used, or
its negative if the coordinate value is negative. Otherwise, if the
Dimensions_Type is signed the type's max value is used, or the min
value if the coordinate value is negative; for an unsigned value the
max value is used.
@param factor A factor by which to divide the Size_2D dimensions.
@return This Rectangle with its values changed.
@throws invalid_argument If the factor is negative and the size values
Dimensions_Type are an unsigned type (as they are by default).
*/
inline Rectangle& operator/= (double factor)
{Size_2D::operator/= (factor); return *this;}
/** Take the intersection with another Rectangle.
The intersection of two Rectangles is the overlapping area of both.
@param rectangle The Rectangle to intersect with this Rectangle.
@return This Rectangle with its position and size set to the
intersection with the other Rectangle. If the Rectangles do not
intersect this will result in a Rectangle with no area (Width
and Height both zero) but the position will be unchanged.
*/
Rectangle& operator&= (const Rectangle& rectangle);
/** Take the union with another Rectangle.
The union of two Rectangles is the bounding area of both.
@param rectangle The Rectangle to unite with this Rectangle.
@return This Rectangle with its position and size set to the
union - i.e. the bounding box - with the other Rectangle.
*/
Rectangle& operator|= (const Rectangle& rectangle);
}; // End of Rectangle class.
/** Add a Point_2D offset to a Rectangle.
@param rectangle A Rectangle.
@param point A Point_2D.
@return A Rectangle in which the coordinate point dimensions are the
Point_2D have been added to the coordinate point of the Rectangle.
*/
inline Rectangle operator+ (const Rectangle& rectangle, const Point_2D& point)
{return Rectangle (rectangle) += point;}
/** Add a Point_2D offset to a Rectangle.
@param point A Point_2D.
@param rectangle A Rectangle.
@return A Rectangle in which the coordinate point dimensions of the
Point_2D have been added to the coordinate point of the Rectangle.
*/
inline Rectangle operator+ (const Point_2D& point, const Rectangle& rectangle)
{return Rectangle (rectangle) += point;}
/** Add two Rectangles.
@param rectangle_1 A Rectangle.
@param rectangle_2 A Rectangle.
@return A Rectangle in which the coordinate point dimensions and the size
amounts of the two Rectangles have been added.
*/
inline Rectangle operator+
(const Rectangle& rectangle_1, const Rectangle& rectangle_2)
{return Rectangle (rectangle_1) += rectangle_2;}
/** Subtract a Point_2D offset from a Rectangle.
@param rectangle A Rectangle.
@param point A Point_2D.
@return A Rectangle in which the coordinate point dimensions of the
Point_2D have been subtracted from the coordinate point of the
Rectangle.
*/
inline Rectangle operator- (const Rectangle& rectangle, const Point_2D& point)
{return Rectangle (rectangle) -= point;}
/** Subtract two Rectangles.
@param rectangle_1 A Rectangle.
@param rectangle_2 A Rectangle.
@return A Rectangle in which the coordinate point dimensions and the size
amounts of the two Rectangles have been added.
*/
inline Rectangle operator-
(const Rectangle& rectangle_1, const Rectangle& rectangle_2)
{return Rectangle (rectangle_1) -= rectangle_2;}
/** Get the negation of a Rectangle.
@param rectangle A Rectangle.
@return A Rectangle in which the coordinate point dimensions are the
negation of the Rectangle's coordinate point; the size is the same.
*/
inline Rectangle operator- (const Rectangle& rectangle)
{return Rectangle (-rectangle.X, -rectangle.Y,