| 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 |
#include "dng_rect.h" |
| 10 |
|
| 11 |
#include "dng_utils.h" |
| 12 |
|
| 13 |
/*****************************************************************************/ |
| 14 |
|
| 15 |
bool dng_rect::operator== (const dng_rect &rect) const |
| 16 |
{ |
| 17 |
|
| 18 |
return (rect.t == t) && |
| 19 |
(rect.l == l) && |
| 20 |
(rect.b == b) && |
| 21 |
(rect.r == r); |
| 22 |
|
| 23 |
} |
| 24 |
|
| 25 |
/*****************************************************************************/ |
| 26 |
|
| 27 |
bool dng_rect::IsZero () const |
| 28 |
{ |
| 29 |
|
| 30 |
return (t == 0) && (l == 0) && (b == 0) && (r == 0); |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
/*****************************************************************************/ |
| 35 |
|
| 36 |
bool dng_rect_real64::operator== (const dng_rect_real64 &rect) const |
| 37 |
{ |
| 38 |
|
| 39 |
return (rect.t == t) && |
| 40 |
(rect.l == l) && |
| 41 |
(rect.b == b) && |
| 42 |
(rect.r == r); |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
/*****************************************************************************/ |
| 47 |
|
| 48 |
bool dng_rect_real64::IsZero () const |
| 49 |
{ |
| 50 |
|
| 51 |
return (t == 0.0) && (l == 0.0) && (b == 0.0) && (r == 0.0); |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
/*****************************************************************************/ |
| 56 |
|
| 57 |
dng_rect operator& (const dng_rect &a, |
| 58 |
const dng_rect &b) |
| 59 |
{ |
| 60 |
|
| 61 |
dng_rect c; |
| 62 |
|
| 63 |
c.t = Max_int32 (a.t, b.t); |
| 64 |
c.l = Max_int32 (a.l, b.l); |
| 65 |
|
| 66 |
c.b = Min_int32 (a.b, b.b); |
| 67 |
c.r = Min_int32 (a.r, b.r); |
| 68 |
|
| 69 |
if (c.IsEmpty ()) |
| 70 |
{ |
| 71 |
|
| 72 |
c = dng_rect (); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
return c; |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
/*****************************************************************************/ |
| 81 |
|
| 82 |
dng_rect operator| (const dng_rect &a, |
| 83 |
const dng_rect &b) |
| 84 |
{ |
| 85 |
|
| 86 |
if (a.IsEmpty ()) |
| 87 |
{ |
| 88 |
return b; |
| 89 |
} |
| 90 |
|
| 91 |
if (b.IsEmpty ()) |
| 92 |
{ |
| 93 |
return a; |
| 94 |
} |
| 95 |
|
| 96 |
dng_rect c; |
| 97 |
|
| 98 |
c.t = Min_int32 (a.t, b.t); |
| 99 |
c.l = Min_int32 (a.l, b.l); |
| 100 |
|
| 101 |
c.b = Max_int32 (a.b, b.b); |
| 102 |
c.r = Max_int32 (a.r, b.r); |
| 103 |
|
| 104 |
return c; |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
/*****************************************************************************/ |
| 109 |
|
| 110 |
dng_rect_real64 operator& (const dng_rect_real64 &a, |
| 111 |
const dng_rect_real64 &b) |
| 112 |
{ |
| 113 |
|
| 114 |
dng_rect_real64 c; |
| 115 |
|
| 116 |
c.t = Max_real64 (a.t, b.t); |
| 117 |
c.l = Max_real64 (a.l, b.l); |
| 118 |
|
| 119 |
c.b = Min_real64 (a.b, b.b); |
| 120 |
c.r = Min_real64 (a.r, b.r); |
| 121 |
|
| 122 |
if (c.IsEmpty ()) |
| 123 |
{ |
| 124 |
|
| 125 |
c = dng_rect_real64 (); |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
return c; |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
/*****************************************************************************/ |
| 134 |
|
| 135 |
dng_rect_real64 operator| (const dng_rect_real64 &a, |
| 136 |
const dng_rect_real64 &b) |
| 137 |
{ |
| 138 |
|
| 139 |
if (a.IsEmpty ()) |
| 140 |
{ |
| 141 |
return b; |
| 142 |
} |
| 143 |
|
| 144 |
if (b.IsEmpty ()) |
| 145 |
{ |
| 146 |
return a; |
| 147 |
} |
| 148 |
|
| 149 |
dng_rect_real64 c; |
| 150 |
|
| 151 |
c.t = Min_real64 (a.t, b.t); |
| 152 |
c.l = Min_real64 (a.l, b.l); |
| 153 |
|
| 154 |
c.b = Max_real64 (a.b, b.b); |
| 155 |
c.r = Max_real64 (a.r, b.r); |
| 156 |
|
| 157 |
return c; |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
/*****************************************************************************/ |
| 162 |
|
| 163 |
dng_rect_real64 Bounds (const dng_point_real64 &a, |
| 164 |
const dng_point_real64 &b, |
| 165 |
const dng_point_real64 &c, |
| 166 |
const dng_point_real64 &d) |
| 167 |
{ |
| 168 |
|
| 169 |
real64 xMin = Min_real64 (a.h, Min_real64 (b.h, Min_real64 (c.h, d.h))); |
| 170 |
real64 xMax = Max_real64 (a.h, Max_real64 (b.h, Max_real64 (c.h, d.h))); |
| 171 |
|
| 172 |
real64 yMin = Min_real64 (a.v, Min_real64 (b.v, Min_real64 (c.v, d.v))); |
| 173 |
real64 yMax = Max_real64 (a.v, Max_real64 (b.v, Max_real64 (c.v, d.v))); |
| 174 |
|
| 175 |
return dng_rect_real64 (yMin, |
| 176 |
xMin, |
| 177 |
yMax, |
| 178 |
xMax); |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
/*****************************************************************************/ |
| 183 |
|
| 184 |
bool Intersect (const dng_oriented_bounding_box &aBox, |
| 185 |
const dng_oriented_bounding_box &bBox) |
| 186 |
{ |
| 187 |
|
| 188 |
// Use separating axis theorem. Only need to check 4 axes in 2D. |
| 189 |
|
| 190 |
const dng_point_real64 &a = aBox.fCenter; |
| 191 |
const dng_point_real64 &b = bBox.fCenter; |
| 192 |
|
| 193 |
const dng_point_real64 &a1 = aBox.fVec1; |
| 194 |
const dng_point_real64 &a2 = aBox.fVec2; |
| 195 |
|
| 196 |
const dng_point_real64 &b1 = bBox.fVec1; |
| 197 |
const dng_point_real64 &b2 = bBox.fVec2; |
| 198 |
|
| 199 |
// Case 1: Project to a1. |
| 200 |
|
| 201 |
{ |
| 202 |
|
| 203 |
// Instead of normalizing a1_radius_b and a1_dist by dividing by |
| 204 |
// a1.Length (), just multiply a1_radius_a by a1.Length (), which is |
| 205 |
// equivalent to squaring itself. This is perfectly valid for the |
| 206 |
// intended comparison below. |
| 207 |
|
| 208 |
// Reference: Normalization step. |
| 209 |
|
| 210 |
// real64 a1_radius_a = a1.Length (); |
| 211 |
|
| 212 |
// a1_radius_b /= a1_radius_a; |
| 213 |
// a1_dist /= a1_radius_a; |
| 214 |
|
| 215 |
// Optimized: Just square a1_radius_a, which is equivalent computing |
| 216 |
// a1_radius_a as Dot (a1, a1) instead of a1.Length (). |
| 217 |
|
| 218 |
real64 a1_radius_a = Dot (a1, a1); |
| 219 |
|
| 220 |
real64 a1_radius_b = (Abs_real64 (Dot (b1, a1)) + |
| 221 |
Abs_real64 (Dot (b2, a1))); |
| 222 |
|
| 223 |
real64 a1_dist = Abs_real64 (Dot (b - a, a1)); |
| 224 |
|
| 225 |
if (a1_dist > a1_radius_a + a1_radius_b) |
| 226 |
{ |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
// Case 2: Project to a2. |
| 233 |
|
| 234 |
{ |
| 235 |
|
| 236 |
// See comment in Case 1, above. |
| 237 |
|
| 238 |
real64 a2_radius_a = Dot (a2, a2); |
| 239 |
|
| 240 |
real64 a2_radius_b = (Abs_real64 (Dot (b1, a2)) + |
| 241 |
Abs_real64 (Dot (b2, a2))); |
| 242 |
|
| 243 |
real64 a2_dist = Abs_real64 (Dot (b - a, a2)); |
| 244 |
|
| 245 |
if (a2_dist > a2_radius_a + a2_radius_b) |
| 246 |
{ |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
// Case 3: Project to b1. |
| 253 |
|
| 254 |
{ |
| 255 |
|
| 256 |
// See comment in Case 1, above. |
| 257 |
|
| 258 |
real64 b1_radius_b = Dot (b1, b1); |
| 259 |
|
| 260 |
real64 b1_radius_a = (Abs_real64 (Dot (a1, b1)) + |
| 261 |
Abs_real64 (Dot (a2, b1))); |
| 262 |
|
| 263 |
real64 b1_dist = Abs_real64 (Dot (b - a, b1)); |
| 264 |
|
| 265 |
if (b1_dist > b1_radius_a + b1_radius_b) |
| 266 |
{ |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
// Case 4: Project to b2. |
| 273 |
|
| 274 |
{ |
| 275 |
|
| 276 |
// See comment in Case 1, above. |
| 277 |
|
| 278 |
real64 b2_radius_b = Dot (b2, b2); |
| 279 |
|
| 280 |
real64 b2_radius_a = (Abs_real64 (Dot (a1, b2)) + |
| 281 |
Abs_real64 (Dot (a2, b2))); |
| 282 |
|
| 283 |
real64 b2_dist = Abs_real64 (Dot (b - a, b2)); |
| 284 |
|
| 285 |
if (b2_dist > b2_radius_a + b2_radius_b) |
| 286 |
{ |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
} |
| 291 |
|
| 292 |
// Projections to all four axes overlap: the two OBBs intersect. |
| 293 |
|
| 294 |
return true; |
| 295 |
|
| 296 |
} |
| 297 |
|
| 298 |
/*****************************************************************************/ |