forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RectI.h
477 lines (407 loc) · 11.5 KB
/
RectI.h
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
/* ***** BEGIN LICENSE BLOCK *****
* This file is part of Natron <https://natrongithub.github.io/>,
* (C) 2018-2021 The Natron developers
* (C) 2013-2018 INRIA and Alexandre Gauthier-Foichat
*
* Natron is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Natron 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Natron. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
* ***** END LICENSE BLOCK ***** */
#ifndef Engine_RectI_h
#define Engine_RectI_h
// ***** BEGIN PYTHON BLOCK *****
// from <https://docs.python.org/3/c-api/intro.html#include-files>:
// "Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included."
#include <Python.h>
// ***** END PYTHON BLOCK *****
#include "Global/Macros.h"
#include <cassert>
#include <iostream>
#include <vector>
#include <utility>
#include <cmath>
#include <algorithm> // min, max
#include <limits>
#include "Global/GlobalDefines.h"
#include "Engine/EngineFwd.h"
#if !defined(Q_MOC_RUN) && !defined(SBK_RUN)
//Shiboken fails if defined at the start of a header
GCC_DIAG_OFF(strict-overflow)
#endif
NATRON_NAMESPACE_ENTER;
/**
* @brief A rectangle where x1 < x2 and y1 < y2 such as width() == (x2 - x1) && height() == (y2 - y1)
**/
class RectI
{
public:
////public so the fields can be access exactly like the OfxRect struct !
int x1; // left
int y1; // bottom
int x2; // right
int y2; // top
template<class Archive>
void serialize(Archive & ar,
const unsigned int version);
RectI()
: x1(0), y1(0), x2(0), y2(0)
{
}
RectI(int x1_,
int y1_,
int x2_,
int y2_)
: x1(x1_), y1(y1_), x2(x2_), y2(y2_)
{
assert( (x2 >= x1) && (y2 >= y1) );
}
RectI(const RectI &b)
: x1(b.x1), y1(b.y1), x2(b.x2), y2(b.y2)
{
assert( (x2 >= x1) && (y2 >= y1) );
}
virtual ~RectI()
{
}
int left() const
{
return x1;
}
void set_left(int v)
{
x1 = v;
}
int bottom() const
{
return y1;
}
void set_bottom(int v)
{
y1 = v;
}
int right() const
{
return x2;
}
void set_right(int v)
{
x2 = v;
}
int top() const
{
return y2;
}
void set_top(int v)
{
y2 = v;
}
int width() const
{
return x2 - x1;
}
int height() const
{
return y2 - y1;
}
void set(int x1_,
int y1_,
int x2_,
int y2_)
{
x1 = x1_;
y1 = y1_;
x2 = x2_;
y2 = y2_;
/*assert( (x2 >= x1) && (y2 >= y1) );*/
}
void set(const RectI & b)
{
*this = b;
}
/**
* @brief Upscales the bounds assuming this rectangle is the Nth level of mipmap
**/
RectI upscalePowerOfTwo(unsigned int thisLevel) const
{
if (thisLevel == 0) {
return *this;
}
RectI ret;
ret.x1 = x1 << thisLevel;
ret.x2 = x2 << thisLevel;
ret.y1 = y1 << thisLevel;
ret.y2 = y2 << thisLevel;
return ret;
}
void toCanonical(unsigned int thisLevel, double par, const RectD & rod, RectD *rect) const;
void toCanonical_noClipping(unsigned int thisLevel, double par, RectD *rect) const;
// the following should never be used: only canonical coordinates may be downscaled
/**
* @brief Scales down the rectangle by the given power of 2
**/
RectI downscalePowerOfTwo(unsigned int thisLevel) const
{
if (thisLevel == 0) {
return *this;
}
RectI ret;
assert(x1 % (1 << thisLevel) == 0 && x2 % (1 << thisLevel) == 0 && y1 % (1 << thisLevel) == 0 && y2 % (1 << thisLevel) == 0);
ret.x1 = x1 >> thisLevel;
ret.x2 = x2 >> thisLevel;
ret.y1 = y1 >> thisLevel;
ret.y2 = y2 >> thisLevel;
return ret;
}
/*
test program for rounding integer to the next/previous pot:
#include <stdio.h>
int main()
{
int i;
int pot = 3;
int scale = 1 << pot;
int scalem1 = scale - 1;
for(i=-100; i < 100; ++i)
{
printf("%d => %d,%d %d,%d\n", i, i & ~scalem1, i+scalem1 & ~scalem1, (i >> pot) << pot, ((i+scalem1)>>pot) << pot);
}
}
*/
/**
* @brief round the rectangle by the given power of 2, and return the largest *enclosed* (inside) rectangle
**/
RectI roundPowerOfTwoLargestEnclosed(unsigned int thisLevel) const
{
if (thisLevel == 0) {
return *this;
}
RectI ret;
int pot = (1 << thisLevel);
int pot_minus1 = pot - 1;
ret.x1 = (x1 + pot_minus1) & ~pot_minus1;
ret.x2 = x2 & ~pot_minus1;
ret.y1 = (y1 + pot_minus1) & ~pot_minus1;
ret.y2 = y2 & ~pot_minus1;
// check that it's enclosed
assert(ret.x1 >= x1 && ret.x2 <= x2 && ret.y1 >= y1 && ret.y2 <= y2);
return ret;
}
/**
* @brief round the rectangle by the given power of 2, and return the smallest *enclosing* rectangle
**/
RectI roundPowerOfTwoSmallestEnclosing(unsigned int thisLevel) const
{
if (thisLevel == 0) {
return *this;
}
RectI ret;
int pot = (1 << thisLevel);
int pot_minus1 = pot - 1;
ret.x1 = x1 & ~pot_minus1;
ret.x2 = (x2 + pot_minus1) & ~pot_minus1;
ret.y1 = y1 & ~pot_minus1;
ret.y2 = (y2 + pot_minus1) & ~pot_minus1;
// check that it's enclosing
assert(ret.x1 <= x1 && ret.x2 >= x2 && ret.y1 <= y1 && ret.y2 >= y2);
return ret;
}
/**
* @brief Scales down the rectangle by the given power of 2, and return the largest *enclosed* (inside) rectangle
**/
RectI downscalePowerOfTwoLargestEnclosed(unsigned int thisLevel) const
{
if (thisLevel == 0) {
return *this;
}
RectI ret;
int pot = (1 << thisLevel);
int pot_minus1 = pot - 1;
ret.x1 = (x1 + pot_minus1) >> thisLevel;
ret.x2 = x2 >> thisLevel;
ret.y1 = (y1 + pot_minus1) >> thisLevel;
ret.y2 = y2 >> thisLevel;
// check that it's enclosed
assert(ret.x1 * pot >= x1 && ret.x2 * pot <= x2 && ret.y1 * pot >= y1 && ret.y2 * pot <= y2);
return ret;
}
/**
* @brief Scales down the rectangle in pixel coordinates by the given power of 2, and return the smallest *enclosing* rectangle in pixel coordinates
**/
RectI downscalePowerOfTwoSmallestEnclosing(unsigned int thisLevel) const
{
if (thisLevel == 0) {
return *this;
}
RectI ret;
int pot = (1 << thisLevel);
int pot_minus1 = pot - 1;
ret.x1 = x1 >> thisLevel;
ret.x2 = (x2 + pot_minus1) >> thisLevel;
ret.y1 = y1 >> thisLevel;
ret.y2 = (y2 + pot_minus1) >> thisLevel;
// check that it's enclosing
assert(ret.x1 * pot <= x1 && ret.x2 * pot >= x2 && ret.y1 * pot <= y1 && ret.y2 * pot >= y2);
return ret;
}
bool isNull() const
{
return (x2 <= x1) || (y2 <= y1);
}
bool isInfinite() const
{
return x1 <= kOfxFlagInfiniteMin || x2 >= kOfxFlagInfiniteMax || y1 <= kOfxFlagInfiniteMin || y2 >= kOfxFlagInfiniteMax;
}
void clear()
{
x1 = 0;
y1 = 0;
x2 = 0;
y2 = 0;
}
/*merge the current box with another integerBox.
* The current box is the smallest box enclosing the two boxes
(not the union, which is not a box).*/
void merge(const RectI & box)
{
merge(box.x1, box.y1, box.x2, box.y2);
}
void merge(int x1_,
int y1_,
int x2_,
int y2_)
{
if ( isNull() ) {
x1 = x1_;
y1 = y1_;
x2 = x2_;
y2 = y2_;
} else {
x1 = std::min(x1, x1_);
y1 = std::min(y1, y1_);
x2 = std::max(x2, x2_);
y2 = std::max(y2, y2_);
}
}
/*intersection of two boxes*/
bool intersect(const RectI & r,
RectI* intersection) const
{
if ( !intersects(r) ) {
return false;
}
intersection->x1 = std::max(x1, r.x1);
intersection->y1 = std::max(y1, r.y1);
// the region must be *at least* empty, thus the maximin.
intersection->x2 = std::max( intersection->x1, std::min(x2, r.x2) );
// the region must be *at least* empty, thus the maximin.
intersection->y2 = std::max( intersection->y1, std::min(y2, r.y2) );
assert( !intersection->isNull() );
return true;
}
bool intersect(int x1_,
int y1_,
int x2_,
int y2_,
RectI* intersection) const
{
return intersect(RectI(x1_, y1_, x2_, y2_), intersection);
}
/// returns true if the rect passed as parameter intersects this one
bool intersects(const RectI & r) const
{
if ( isNull() || r.isNull() ) {
return false;
}
if ( (r.x2 <= x1) || (x2 <= r.x1) || (r.y2 <= y1) || (y2 <= r.y1) ) {
return false;
}
return true;
}
bool intersects(int x1_,
int y1_,
int x2_,
int y2_) const
{
return intersects( RectI(x1_, y1_, x2_, y2_) );
}
/*the area : w*h*/
U64 area() const
{
return (U64)width() * height();
}
RectI & operator=(const RectI & other)
{
x1 = other.x1;
y1 = other.y1;
x2 = other.x2;
y2 = other.y2;
return *this;
}
bool contains(const RectI & other) const
{
return other.x1 >= x1 &&
other.y1 >= y1 &&
other.x2 <= x2 &&
other.y2 <= y2;
}
bool contains(int x,
int y) const
{
return x >= x1 && x < x2 && y >= y1 && y < y2;
}
bool contains(double x,
double y) const
{
return x >= x1 && x < x2 && y >= y1 && y < y2;
}
void translate(int dx,
int dy)
{
x1 += dx;
y1 += dy;
x2 += dx;
y2 += dy;
}
#ifdef DEBUG
void debug() const
{
std::cout << "x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 << std::endl;
}
#endif
std::vector<RectI> splitIntoSmallerRects(int splitsCount) const;
static RectI fromOfxRectI(const OfxRectI & r)
{
RectI ret(r.x1, r.y1, r.x2, r.y2);
return ret;
}
};
GCC_DIAG_ON(strict-overflow)
/// equality of boxes
inline bool
operator==(const RectI & b1,
const RectI & b2)
{
return (b1.x1 == b2.x1 &&
b1.y1 == b2.y1 &&
b1.x2 == b2.x2 &&
b1.y2 == b2.y2);
}
/// inequality of boxes
inline bool
operator!=(const RectI & b1,
const RectI & b2)
{
return !(b1 == b2);
}
NATRON_NAMESPACE_EXIT;
Q_DECLARE_METATYPE(NATRON_NAMESPACE::RectI)
#endif // Engine_RectI_h