forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RectD.h
353 lines (294 loc) · 7.7 KB
/
RectD.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
/* ***** 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_RectD_h
#define Engine_RectD_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;
class RectD
{
public:
double x1; // left
double y1; // bottom
double x2; // right
double y2; // top
template<class Archive>
void serialize(Archive & ar,
const unsigned int version);
RectD()
: x1(0), y1(0), x2(0), y2(0)
{
}
RectD(double l,
double b,
double r,
double t)
: x1(l), y1(b), x2(r), y2(t) /*assert((x2>= x1) && (y2>=y1));*/
{
}
RectD(const RectD &b)
: x1(b.x1), y1(b.y1), x2(b.x2), y2(b.y2)
{
assert( (x2 >= x1) && (y2 >= y1) );
}
virtual ~RectD()
{
}
double left() const
{
return x1;
}
void set_left(double v)
{
x1 = v;
}
double bottom() const
{
return y1;
}
void set_bottom(double v)
{
y1 = v;
}
double right() const
{
return x2;
}
void set_right(double v)
{
x2 = v;
}
double top() const
{
return y2;
}
void set_top(double v)
{
y2 = v;
}
double width() const
{
return x2 - x1;
}
double height() const
{
return y2 - y1;
}
void set(double l,
double b,
double r,
double t)
{
x1 = l;
y1 = b;
x2 = r;
y2 = t;
/*assert((x2>= x1) && (y2>=y1));*/
}
//Useful for bbox computations
void setupInfinity()
{
x1 = std::numeric_limits<double>::infinity();
x2 = -std::numeric_limits<double>::infinity();
y1 = std::numeric_limits<double>::infinity();
y2 = -std::numeric_limits<double>::infinity();
}
void set(const RectD & b)
{
*this = b;
}
bool isInfinite() const
{
return x1 <= kOfxFlagInfiniteMin || x2 >= kOfxFlagInfiniteMax || y1 <= kOfxFlagInfiniteMin || y2 >= kOfxFlagInfiniteMax;
}
/*
// RectD are in canonical coordinates and should never be scaled!
RectD scaled(double sx,double sy) const {
RectD ret;
ret.x1 = x1;
ret.y1 = y1;
ret.x2 = (double)x2 * sx;
ret.y2 = (double)y2 * sy;
return ret;
}
*/
bool isNull() const
{
return (x2 <= x1) || (y2 <= y1);
}
void clear()
{
x1 = 0;
y1 = 0;
x2 = 0;
y2 = 0;
}
void translate(int dx,
int dy)
{
x1 += dx;
y1 += dy;
x2 += dx;
y2 += dy;
}
/*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 RectD & box)
{
merge( box.left(), box.bottom(), box.right(), box.top() );
}
void merge(double l,
double b,
double r,
double t)
{
x1 = std::min(x1, l);
x2 = std::max(x2, r);
y1 = std::min(y1, b);
y2 = std::max(y2, t);
}
/*intersection of two boxes*/
bool intersect(const RectD & r,
RectD* intersection) const
{
if ( isNull() || r.isNull() ) {
return false;
}
if ( (x1 > r.x2) || (r.x1 > x2) || (y1 > r.y2) || (r.y1 > y2) ) {
return false;
}
intersection->x1 = std::max(x1, r.x1);
intersection->x2 = std::min(x2, r.x2);
intersection->y1 = std::max(y1, r.y1);
intersection->y2 = std::min(y2, r.y2);
return true;
}
bool intersect(double l,
double b,
double r,
double t,
RectD* intersection) const
{
return intersect(RectD(l, b, r, t), intersection);
}
/// returns true if the rect passed as parameter is intersects this one
bool intersects(const RectD & r) const
{
if ( isNull() || r.isNull() ) {
return false;
}
if ( (x1 > r.x2) || (r.x1 > x2) || (y1 > r.y2) || (r.y1 > y2) ) {
return false;
}
return true;
}
bool intersects(double l,
double b,
double r,
double t) const
{
return intersects( RectD(l, b, r, t) );
}
/*the area : w*h*/
double area() const
{
return (double)width() * height();
}
RectD & operator=(const RectD & other)
{
x1 = other.left();
y1 = other.bottom();
x2 = other.right();
y2 = other.top();
return *this;
}
bool contains(const RectD & other) const
{
return other.x1 >= x1 &&
other.y1 >= y1 &&
other.x2 <= x2 &&
other.y2 <= y2;
}
bool contains(double x,
double y) const
{
return x >= x1 && x < x2 && y >= y1 && y < y2;
}
#ifdef DEBUG
void debug() const
{
std::cout << "x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 << std::endl;
}
#endif
void toPixelEnclosing(const RenderScale & scale,
double par,
RectI *rect) const;
void toPixelEnclosing(unsigned int mipMapLevel,
double par,
RectI *rect) const;
static void ofxRectDToRectD(const OfxRectD & r,
RectD *ret)
{
ret->x1 = r.x1;
ret->x2 = r.x2;
ret->y1 = r.y1;
ret->y2 = r.y2;
}
};
/// equality of boxes
inline bool
operator==(const RectD & b1,
const RectD & b2)
{
return b1.left() == b2.left() &&
b1.bottom() == b2.bottom() &&
b1.right() == b2.right() &&
b1.top() == b2.top();
}
/// inequality of boxes
inline bool
operator!=(const RectD & b1,
const RectD & b2)
{
return b1.left() != b2.left() ||
b1.bottom() != b2.bottom() ||
b1.right() != b2.right() ||
b1.top() != b2.top();
}
NATRON_NAMESPACE_EXIT;
Q_DECLARE_METATYPE(NATRON_NAMESPACE::RectD)
#endif // Engine_RectD_h