Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_rect.h

raw
1 /*****************************************************************************/
2 // Copyright 2006-2019 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE: Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8
9 #ifndef __dng_rect__
10 #define __dng_rect__
11
12 /*****************************************************************************/
13
14 #include "dng_exceptions.h"
15 #include "dng_point.h"
16 #include "dng_safe_arithmetic.h"
17 #include "dng_types.h"
18 #include "dng_utils.h"
19
20 /*****************************************************************************/
21
22 #define DNG_RECT_FMT(x) (x).t, (x).l, (x).b, (x).r
23
24 /*****************************************************************************/
25
26 class dng_rect
27 {
28
29 public:
30
31 int32 t;
32 int32 l;
33 int32 b;
34 int32 r;
35
36 public:
37
38 dng_rect ()
39 : t (0)
40 , l (0)
41 , b (0)
42 , r (0)
43 {
44 }
45
46 // Constructs a dng_rect from the top-left and bottom-right corner.
47 // Throws an exception if the resulting height or width are too large
48 // to be represented as an int32. The intent of this is to protect
49 // code that may be computing the height or width directly from the
50 // member variables (instead of going through H() or W()).
51
52 dng_rect (int32 tt, int32 ll, int32 bb, int32 rr)
53 : t (tt)
54 , l (ll)
55 , b (bb)
56 , r (rr)
57 {
58
59 int32 dummy;
60
61 if (!SafeInt32Sub (r, l, &dummy) ||
62 !SafeInt32Sub (b, t, &dummy))
63 {
64 ThrowProgramError ("Overflow in dng_rect constructor");
65 }
66
67 }
68
69 dng_rect (uint32 h, uint32 w)
70 : t (0)
71 , l (0)
72 {
73
74 if (!ConvertUint32ToInt32 (h, &b) ||
75 !ConvertUint32ToInt32 (w, &r))
76 {
77 ThrowProgramError ("Overflow in dng_rect constructor");
78 }
79
80 }
81
82 dng_rect (const dng_point &size)
83 : t (0)
84 , l (0)
85 , b (size.v)
86 , r (size.h)
87 {
88 }
89
90 void Clear ()
91 {
92 *this = dng_rect ();
93 }
94
95 bool operator== (const dng_rect &rect) const;
96
97 bool operator!= (const dng_rect &rect) const
98 {
99 return !(*this == rect);
100 }
101
102 bool IsZero () const;
103
104 bool NotZero () const
105 {
106 return !IsZero ();
107 }
108
109 bool IsEmpty () const
110 {
111 return (t >= b) || (l >= r);
112 }
113
114 bool NotEmpty () const
115 {
116 return !IsEmpty ();
117 }
118
119 bool Contains (const dng_rect &rect) const
120 {
121 return (rect.IsEmpty () || (t <= rect.t && l <= rect.l &&
122 b >= rect.b && r >= rect.r));
123 }
124
125 bool DoesNotContain (const dng_rect &rect) const
126 {
127 return !Contains (rect);
128 }
129
130 bool Contains (const dng_point &pt) const
131 {
132 return (t <= pt.v && l <= pt.h &&
133 b > pt.v && r > pt.h);
134 }
135
136 bool DoesNotContain (const dng_point &pt) const
137 {
138 return !Contains (pt);
139 }
140
141 // Returns the width of the rectangle, or 0 if r is smaller than l.
142 // Throws an exception if the width is too large to be represented as
143 // a _signed_ int32 (even if it would fit in a uint32). This is
144 // consciously conservative -- there are existing uses of W() where
145 // the result is converted to an int32 without an overflow check, and
146 // we want to make sure no overflow can occur in such cases. We
147 // provide this check in addition to the check performed in the
148 // "two-corners" constructor to protect client code that produes a
149 // dng_rect with excessive size by initializing or modifying the
150 // member variables directly.
151
152 uint32 W () const
153 {
154
155 if (r >= l)
156 {
157
158 int32 width;
159
160 if (!SafeInt32Sub (r, l, &width))
161 {
162 ThrowProgramError ("Overflow computing rectangle width");
163 }
164
165 return static_cast<uint32> (width);
166
167 }
168
169 else
170 {
171 return 0;
172 }
173
174 }
175
176 // Returns the height of the rectangle, or 0 if b is smaller than t.
177 // Throws an exception if the height is too large to be represented as
178 // a _signed_ int32 (see W() for rationale).
179
180 uint32 H () const
181 {
182
183 if (b >= t)
184 {
185
186 int32 height;
187
188 if (!SafeInt32Sub (b, t, &height))
189 {
190 ThrowProgramError ("Overflow computing rectangle height");
191 }
192
193 return static_cast<uint32> (height);
194
195 }
196
197 else
198 {
199 return 0;
200 }
201
202 }
203
204 dng_point TL () const
205 {
206 return dng_point (t, l);
207 }
208
209 dng_point TR () const
210 {
211 return dng_point (t, r);
212 }
213
214 dng_point BL () const
215 {
216 return dng_point (b, l);
217 }
218
219 dng_point BR () const
220 {
221 return dng_point (b, r);
222 }
223
224 dng_point Size () const
225 {
226 return dng_point ((int32) H (), (int32) W ());
227 }
228
229 uint32 LongSide () const
230 {
231 return Max_uint32 (W (), H ());
232 }
233
234 uint32 ShortSide () const
235 {
236 return Min_uint32 (W (), H ());
237 }
238
239 real64 Diagonal () const
240 {
241 return hypot ((real64) W (),
242 (real64) H ());
243 }
244
245 };
246
247 /*****************************************************************************/
248
249 class dng_rect_real64
250 {
251
252 public:
253
254 real64 t;
255 real64 l;
256 real64 b;
257 real64 r;
258
259 public:
260
261 dng_rect_real64 ()
262 : t (0.0)
263 , l (0.0)
264 , b (0.0)
265 , r (0.0)
266 {
267 }
268
269 dng_rect_real64 (real64 tt, real64 ll, real64 bb, real64 rr)
270 : t (tt)
271 , l (ll)
272 , b (bb)
273 , r (rr)
274 {
275 }
276
277 dng_rect_real64 (real64 h, real64 w)
278 : t (0)
279 , l (0)
280 , b (h)
281 , r (w)
282 {
283 }
284
285 dng_rect_real64 (const dng_point_real64 &size)
286 : t (0)
287 , l (0)
288 , b (size.v)
289 , r (size.h)
290 {
291 }
292
293 dng_rect_real64 (const dng_point_real64 &pt1,
294 const dng_point_real64 &pt2)
295 : t (Min_real64 (pt1.v, pt2.v))
296 , l (Min_real64 (pt1.h, pt2.h))
297 , b (Max_real64 (pt1.v, pt2.v))
298 , r (Max_real64 (pt1.h, pt2.h))
299 {
300 }
301
302 dng_rect_real64 (const dng_rect &rect)
303 : t ((real64) rect.t)
304 , l ((real64) rect.l)
305 , b ((real64) rect.b)
306 , r ((real64) rect.r)
307 {
308 }
309
310 void Clear ()
311 {
312 *this = dng_point_real64 ();
313 }
314
315 bool operator== (const dng_rect_real64 &rect) const;
316
317 bool operator!= (const dng_rect_real64 &rect) const
318 {
319 return !(*this == rect);
320 }
321
322 bool IsZero () const;
323
324 bool NotZero () const
325 {
326 return !IsZero ();
327 }
328
329 bool IsEmpty () const
330 {
331 return (t >= b) || (l >= r);
332 }
333
334 bool NotEmpty () const
335 {
336 return !IsEmpty ();
337 }
338
339 bool Contains (const dng_rect_real64 &rect) const
340 {
341 return (rect.IsEmpty () || (t <= rect.t && l <= rect.l &&
342 b >= rect.b && r >= rect.r));
343 }
344
345 bool Contains (const dng_point_real64 &pt) const
346 {
347 return (t <= pt.v && l <= pt.h &&
348 b > pt.v && r > pt.h);
349 }
350
351 real64 W () const
352 {
353 return Max_real64 (r - l, 0.0);
354 }
355
356 real64 H () const
357 {
358 return Max_real64 (b - t, 0.0);
359 }
360
361 dng_point_real64 TL () const
362 {
363 return dng_point_real64 (t, l);
364 }
365
366 dng_point_real64 TR () const
367 {
368 return dng_point_real64 (t, r);
369 }
370
371 dng_point_real64 BL () const
372 {
373 return dng_point_real64 (b, l);
374 }
375
376 dng_point_real64 BR () const
377 {
378 return dng_point_real64 (b, r);
379 }
380
381 dng_point_real64 Size () const
382 {
383 return dng_point_real64 (H (), W ());
384 }
385
386 dng_rect Round () const
387 {
388 return dng_rect (Round_int32 (t),
389 Round_int32 (l),
390 Round_int32 (b),
391 Round_int32 (r));
392 }
393
394 real64 LongSide () const
395 {
396 return Max_real64 (W (), H ());
397 }
398
399 real64 ShortSide () const
400 {
401 return Min_real64 (W (), H ());
402 }
403
404 real64 Diagonal () const
405 {
406 return hypot (W (), H ());
407 }
408
409 dng_point_real64 Center () const
410 {
411 return dng_point_real64 ((t + b) * 0.5,
412 (l + r) * 0.5);
413 }
414
415 };
416
417 /*****************************************************************************/
418
419 dng_rect operator& (const dng_rect &a,
420 const dng_rect &b);
421
422 dng_rect operator| (const dng_rect &a,
423 const dng_rect &b);
424
425 /*****************************************************************************/
426
427 dng_rect_real64 operator& (const dng_rect_real64 &a,
428 const dng_rect_real64 &b);
429
430 dng_rect_real64 operator| (const dng_rect_real64 &a,
431 const dng_rect_real64 &b);
432
433 /*****************************************************************************/
434
435 inline dng_rect operator+ (const dng_rect &a,
436 const dng_point &b)
437 {
438
439 return dng_rect (a.t + b.v,
440 a.l + b.h,
441 a.b + b.v,
442 a.r + b.h);
443
444 }
445
446 /*****************************************************************************/
447
448 inline dng_rect_real64 operator+ (const dng_rect_real64 &a,
449 const dng_point_real64 &b)
450 {
451
452 return dng_rect_real64 (a.t + b.v,
453 a.l + b.h,
454 a.b + b.v,
455 a.r + b.h);
456
457 }
458
459 /*****************************************************************************/
460
461 inline dng_rect operator- (const dng_rect &a,
462 const dng_point &b)
463 {
464
465 return dng_rect (a.t - b.v,
466 a.l - b.h,
467 a.b - b.v,
468 a.r - b.h);
469
470 }
471
472 /*****************************************************************************/
473
474 inline dng_rect_real64 operator- (const dng_rect_real64 &a,
475 const dng_point_real64 &b)
476 {
477
478 return dng_rect_real64 (a.t - b.v,
479 a.l - b.h,
480 a.b - b.v,
481 a.r - b.h);
482
483 }
484
485 /*****************************************************************************/
486
487 inline dng_rect Transpose (const dng_rect &a)
488 {
489
490 return dng_rect (a.l, a.t, a.r, a.b);
491
492 }
493
494 /*****************************************************************************/
495
496 inline dng_rect_real64 Transpose (const dng_rect_real64 &a)
497 {
498
499 return dng_rect_real64 (a.l, a.t, a.r, a.b);
500
501 }
502
503 /*****************************************************************************/
504
505 inline void HalfRect (dng_rect &rect)
506 {
507
508 rect.r = rect.l + (int32) (rect.W () >> 1);
509 rect.b = rect.t + (int32) (rect.H () >> 1);
510
511 }
512
513 /*****************************************************************************/
514
515 inline void DoubleRect (dng_rect &rect)
516 {
517
518 rect.r = rect.l + (int32) (rect.W () << 1);
519 rect.b = rect.t + (int32) (rect.H () << 1);
520
521 }
522
523 /*****************************************************************************/
524
525 inline void InnerPadRect (dng_rect &rect,
526 int32 pad)
527 {
528
529 rect.l += pad;
530 rect.r -= pad;
531 rect.t += pad;
532 rect.b -= pad;
533
534 }
535
536 /*****************************************************************************/
537
538 inline void OuterPadRect (dng_rect &rect,
539 int32 pad)
540 {
541
542 InnerPadRect (rect, -pad);
543
544 }
545
546 /*****************************************************************************/
547
548 inline void InnerPadRectH (dng_rect &rect,
549 int32 pad)
550 {
551
552 rect.l += pad;
553 rect.r -= pad;
554
555 }
556
557 /*****************************************************************************/
558
559 inline void InnerPadRectV (dng_rect &rect,
560 int32 pad)
561 {
562
563 rect.t += pad;
564 rect.b -= pad;
565
566 }
567
568 /*****************************************************************************/
569
570 inline dng_rect MakeHalfRect (const dng_rect &rect)
571 {
572
573 dng_rect out = rect;
574
575 HalfRect (out);
576
577 return out;
578
579 }
580
581 /*****************************************************************************/
582
583 inline dng_rect MakeDoubleRect (const dng_rect &rect)
584 {
585
586 dng_rect out = rect;
587
588 DoubleRect (out);
589
590 return out;
591
592 }
593
594 /*****************************************************************************/
595
596 inline dng_rect MakeInnerPadRect (const dng_rect &rect,
597 int32 pad)
598 {
599
600 dng_rect out = rect;
601
602 InnerPadRect (out, pad);
603
604 return out;
605
606 }
607
608 /*****************************************************************************/
609
610 inline dng_rect_real64 MakeInnerPadRect (const dng_rect_real64 &rect,
611 const real64 pad)
612 {
613
614 dng_rect_real64 result = rect;
615
616 result.t += pad;
617 result.l += pad;
618 result.b -= pad;
619 result.r -= pad;
620
621 return result;
622
623 }
624
625 /*****************************************************************************/
626
627 inline dng_rect MakeOuterPadRect (const dng_rect &rect,
628 int32 pad)
629 {
630
631 dng_rect out = rect;
632
633 OuterPadRect (out, pad);
634
635 return out;
636
637 }
638
639 /*****************************************************************************/
640
641 inline dng_rect_real64 MakeOuterPadRect (const dng_rect_real64 &rect,
642 const real64 pad)
643 {
644
645 dng_rect_real64 result = rect;
646
647 result.t -= pad;
648 result.l -= pad;
649 result.b += pad;
650 result.r += pad;
651
652 return result;
653
654 }
655
656 /*****************************************************************************/
657
658 inline dng_rect_real64 Lerp (const dng_rect_real64 &a,
659 const dng_rect_real64 &b,
660 const real64 t)
661 {
662
663 return dng_rect_real64 (Lerp_real64 (a.t, b.t, t),
664 Lerp_real64 (a.l, b.l, t),
665 Lerp_real64 (a.b, b.b, t),
666 Lerp_real64 (a.r, b.r, t));
667
668 }
669
670 /*****************************************************************************/
671
672 dng_rect_real64 Bounds (const dng_point_real64 &a,
673 const dng_point_real64 &b,
674 const dng_point_real64 &c,
675 const dng_point_real64 &d);
676
677 /*****************************************************************************/
678
679 // An oriented bounding box (a dng_rect_real64 with rotation).
680
681 class dng_oriented_bounding_box
682 {
683
684 public:
685
686 // The center of the OBB.
687
688 dng_point_real64 fCenter;
689
690 // The two normal vectors. These vectors should be perpendicular to
691 // each other. The length of the vector is half the edge length.
692
693 dng_point_real64 fVec1;
694 dng_point_real64 fVec2;
695
696 public:
697
698 // Create an empty, invalid OBB.
699
700 dng_oriented_bounding_box ()
701 {
702 }
703
704 // Create an OBB from an axis-aligned bounding box (AABB).
705
706 dng_oriented_bounding_box (const dng_rect_real64 &rect)
707 {
708
709 fCenter = rect.Center ();
710
711 fVec1 = (0.5 * (rect.TR () + rect.BR ())) - fCenter;
712 fVec2 = (0.5 * (rect.BL () + rect.BR ())) - fCenter;
713
714 }
715
716 // Create an OBB from four corners.
717
718 dng_oriented_bounding_box (const dng_point_real64 &tl,
719 const dng_point_real64 &tr,
720 const dng_point_real64 &bl,
721 const dng_point_real64 &br)
722 {
723
724 fCenter = 0.25 * (tl + tr + bl + br);
725
726 fVec1 = (0.5 * (tr + br)) - fCenter;
727 fVec2 = (0.5 * (bl + br)) - fCenter;
728
729 }
730
731 // Calculate four corners from this OBB.
732
733 void CalcCorners (dng_point_real64 &tl,
734 dng_point_real64 &tr,
735 dng_point_real64 &bl,
736 dng_point_real64 &br) const
737 {
738
739 tl = fCenter - fVec1 - fVec2;
740 tr = fCenter + fVec1 - fVec2;
741
742 bl = fCenter - fVec1 + fVec2;
743 br = fCenter + fVec1 + fVec2;
744
745 }
746
747 };
748
749 /*****************************************************************************/
750
751 // Returns true iff the two oriented bounding boxes intersect.
752
753 bool Intersect (const dng_oriented_bounding_box &aBox,
754 const dng_oriented_bounding_box &bBox);
755
756 /*****************************************************************************/
757
758 #endif
759
760 /*****************************************************************************/