aboutsummaryrefslogtreecommitdiffhomepage
path: root/gpu/include/GrRect.h
blob: 67e366c3bffaadc6194a1449a5ae5bb334db2b7b (plain)
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
/*
    Copyright 2010 Google Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */


#ifndef GrRect_DEFINED
#define GrRect_DEFINED

#include "GrPoint.h"

struct GrIRect {
    int32_t fLeft, fTop, fRight, fBottom;

    GrIRect() {}
    GrIRect(int32_t left, int32_t top, int32_t right, int32_t bottom) {
        fLeft = left;
        fTop = top;
        fRight = right;
        fBottom = bottom;
    }

    int32_t x() const { return fLeft; }
    int32_t y() const { return fTop; }
    int32_t width() const { return fRight - fLeft; }
    int32_t height() const { return fBottom - fTop; }

    bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
    bool isInverted() const { return fLeft > fRight || fTop > fBottom; }

    void setEmpty() { fLeft = fTop = fRight = fBottom = 0; }

    void setXYWH(int32_t x, int32_t y, int32_t w, int32_t h) {
        fLeft = x;
        fTop = y;
        fRight = x + w;
        fBottom = y + h;
    }

    void setLTRB(int32_t l, int32_t t, int32_t r, int32_t b) {
        fLeft = l;
        fTop = t;
        fRight = r;
        fBottom = b;
    }

    /**
     *  Make the largest representable rectangle
     */
    void setLargest() {
        fLeft = fTop = GR_Int32Min;
        fRight = fBottom = GR_Int32Max;
    }

    bool quickReject(int l, int t, int r, int b) const {
        return l >= fRight || fLeft >= r || t >= fBottom || fTop >= b;
    }

    void unionWith(const GrIRect& r) {
        if (fLeft > r.fLeft) fLeft = r.fLeft;
        if (fTop > r.fTop) fTop = r.fTop;
        if (fRight < r.fRight) fRight = r.fRight;
        if (fBottom < r.fBottom) fBottom = r.fBottom;
    }

    /**
     * Sets this rect to the intersection with a clip rect. If there is no
     * intersection then this rect will be made empty.
     */
    void intersectWith(const GrIRect& clipRect) {
        if (fRight < clipRect.fLeft ||
            fLeft > clipRect.fRight ||
            fBottom < clipRect.fTop ||
            fTop > clipRect.fBottom) {
            this->setEmpty();
        } else {
            fLeft = GrMax(fLeft, clipRect.fLeft);
            fRight = GrMin(fRight, clipRect.fRight);
            fTop = GrMax(fTop, clipRect.fTop);
            fBottom = GrMin(fBottom, clipRect.fBottom);
        }
    }

    friend bool operator==(const GrIRect& a, const GrIRect& b) {
        return 0 == memcmp(&a, &b, sizeof(a));
    }

    friend bool operator!=(const GrIRect& a, const GrIRect& b) {
        return 0 != memcmp(&a, &b, sizeof(a));
    }

    bool equalsLTRB(int l, int t, int r, int b) const {
        return fLeft == l && fTop == t &&
               fRight == r && fBottom == b;
    }
    bool equalsXYWH(int x, int y, int w, int h) const {
        return fLeft == x && fTop == y &&
               this->width() == w && this->height() == h;
    }

    bool contains(const GrIRect& r) const {
        return fLeft   <= r.fLeft &&
               fRight  >= r.fRight &&
               fTop    <= r.fTop &&
               fBottom >= r.fBottom;
    }
};

struct GrIRect16 {
    int16_t fLeft, fTop, fRight, fBottom;

    int width() const { return fRight - fLeft; }
    int height() const { return fBottom - fTop; }
    int area() const { return this->width() * this->height(); }
    bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }

    void set(const GrIRect& r) {
        fLeft   = GrToS16(r.fLeft);
        fTop    = GrToS16(r.fTop);
        fRight  = GrToS16(r.fRight);
        fBottom = GrToS16(r.fBottom);
    }
};

/**
 *  2D Rect struct
 */
struct GrRect {
    GrScalar fLeft, fTop, fRight, fBottom;

    /**
     *  Uninitialized rectangle.
     */
    GrRect() {}

    /**
     *  Initialize a rectangle to a point.
     *  @param pt the point used to initialize the rectanglee.
     */
    explicit GrRect(const GrPoint& pt) {
        setToPoint(pt);
    }

    GrRect(GrScalar left, GrScalar top, GrScalar right, GrScalar bottom) {
        fLeft = left;
        fTop = top;
        fRight = right;
        fBottom = bottom;
    }

    explicit GrRect(const GrIRect& src) {
        fLeft = GrIntToScalar(src.fLeft);
        fTop = GrIntToScalar(src.fTop);
        fRight = GrIntToScalar(src.fRight);
        fBottom = GrIntToScalar(src.fBottom);
    }

    GrScalar x() const { return fLeft; }
    GrScalar y() const { return fTop; }
    GrScalar width() const { return fRight - fLeft; }
    GrScalar height() const { return fBottom - fTop; }

    GrScalar left() const { return fLeft; }
    GrScalar top() const { return fTop; }
    GrScalar right() const { return fRight; }
    GrScalar bottom() const { return fBottom; }

    GrScalar diagonalLengthSqd() const {
        GrScalar w = width();
        GrScalar h = height();
        return GrMul(w, w) + GrMul(h, h);
    }

    GrScalar diagonalLength() const {
        // TODO: fixed point sqrt
        return GrFloatToScalar(sqrtf(GrScalarToFloat(diagonalLengthSqd())));
    }

    /**
     *  Returns true if the width or height is <= 0
     */
    bool isEmpty() const {
        return fLeft >= fRight || fTop >= fBottom;
    }

    void setEmpty() {
        fLeft = fTop = fRight = fBottom = 0;
    }

    /**
     *  returns true if the rectangle is inverted either in x or y
     */
    bool isInverted() const {
        return (fLeft > fRight) || (fTop > fBottom);
    }

    /**
     * Does this rect contain a point.
     */
    bool contains(const GrPoint& point) const {
        return point.fX >= fLeft && point.fX < fRight &&
               point.fY >= fTop && point.fY < fBottom;
    }

    /**
     * Does this rect fully contain another rect.
     */
    bool contains(const GrRect& r) const {
        return fLeft   <= r.fLeft &&
               fRight  >= r.fRight &&
               fTop    <= r.fTop &&
               fBottom >= r.fBottom;
    }

    /**
     *  Offset the rectangle by (tx, ty), adding tx to the horizontal position
     *  and adds ty to the vertical position.
     */
    void offset(GrScalar tx, GrScalar ty) {
        fLeft  += tx;   fTop    += ty;
        fRight += tx;   fBottom += ty;
    }

    /**
     *  Inset the rectangle by dx,dy. If dx > 0 the rect becomes narrower,
     *  if dx < 0 the rect becomes wider.
     */
    void inset(GrScalar dx, GrScalar dy) {
        fLeft += dx;    fTop    += dy;
        fRight -= dx;   fBottom -= dy;
    }

    /**
     *  Initialize a rectangle to a point.
     *  @param pt the point used to initialize the rectangle.
     */
    void setToPoint(const GrPoint& pt) {
        fLeft = pt.fX;
        fTop = pt.fY;
        fRight = pt.fX;
        fBottom = pt.fY;
    }

    void set(const GrIRect& r) {
        fLeft = GrIntToScalar(r.fLeft);
        fTop = GrIntToScalar(r.fTop);
        fRight = GrIntToScalar(r.fRight);
        fBottom = GrIntToScalar(r.fBottom);
    }

    void roundOut(GrIRect* r) const {
        r->setLTRB(GrScalarFloorToInt(fLeft),
                   GrScalarFloorToInt(fTop),
                   GrScalarCeilToInt(fRight),
                   GrScalarCeilToInt(fBottom));
    }

    /**
     *  Set the rect to the union of the array of points. If the array is empty
     *  the rect will be empty [0,0,0,0]
     */
    void setBounds(const GrPoint pts[], int count);

    /**
     *  Make the largest representable rectangle
     *  Set the rect to fLeft = fTop = GR_ScalarMin and
     *  fRight = fBottom = GR_ScalarMax.
     */
    void setLargest() {
        fLeft = fTop = GR_ScalarMin;
        fRight = fBottom = GR_ScalarMax;
    }

    /**
     Set the rect to fLeft = fTop = GR_ScalarMax and
     fRight = fBottom = GR_ScalarMin.
     Useful for initializing a bounding rectangle.
     */
    void setLargestInverted() {
        fLeft = fTop = GR_ScalarMax;
        fRight = fBottom = GR_ScalarMin;
    }

    void setLTRB(GrScalar left,
                 GrScalar top,
                 GrScalar right,
                 GrScalar bottom) {
        fLeft = left;
        fTop = top;
        fRight = right;
        fBottom = bottom;
    }

    void setXYWH(GrScalar x, GrScalar y, GrScalar width, GrScalar height) {
        fLeft = x;
        fTop = y;
        fRight = x + width;
        fBottom = y + height;
    }

    /**
     Expand the edges of the rectangle to include a point.
     Useful for constructing a bounding rectangle.
     @param pt  the point used to grow the rectangle.
     */
    void growToInclude(const GrPoint& pt) {
        fLeft  = GrMin(pt.fX, fLeft);
        fRight = GrMax(pt.fX, fRight);

        fTop    = GrMin(pt.fY, fTop);
        fBottom = GrMax(pt.fY, fBottom);
    }

    /**
     * Grows a rect to include another rect.
     * @param rect the rect to include
     */
    void growToInclude(const GrRect& rect) {
        GrAssert(!rect.isEmpty());
        fLeft  = GrMin(rect.fLeft, fLeft);
        fRight = GrMax(rect.fRight, fRight);

        fTop    = GrMin(rect.fTop, fTop);
        fBottom = GrMax(rect.fBottom, fBottom);
    }

    /**
     * Sets this rect to the intersection with a clip rect. If there is no
     * intersection then this rect will be made empty.
     */
    void intersectWith(const GrRect& clipRect) {
        if (fRight < clipRect.fLeft ||
            fLeft > clipRect.fRight ||
            fBottom < clipRect.fTop ||
            fTop > clipRect.fBottom) {
            this->setEmpty();
        } else {
            fLeft = GrMax(fLeft, clipRect.fLeft);
            fRight = GrMin(fRight, clipRect.fRight);
            fTop = GrMax(fTop, clipRect.fTop);
            fBottom = GrMin(fBottom, clipRect.fBottom);
        }
    }

    /**
     *  Assigns 4 sequential points in order to construct a counter-clockwise
     *  triangle fan, given the corners of this rect. Returns the address of
     *  the next point, treating pts as an array.
     */
    GrPoint* setRectFan(GrPoint pts[4]) const {
        pts->setRectFan(fLeft, fTop, fRight, fBottom);
        return pts + 4;
    }

    bool operator ==(const GrRect& r) const {
        return fLeft == r.fLeft     &&
               fTop == r.fTop       &&
               fRight == r.fRight   &&
               fBottom == r.fBottom;
    }
};

#endif