1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * Copyright 2008 VMware, Inc.
4848b8605Smrg * All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the
8848b8605Smrg * "Software"), to deal in the Software without restriction, including
9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to
11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
12848b8605Smrg * the following conditions:
13848b8605Smrg *
14848b8605Smrg * The above copyright notice and this permission notice (including the
15848b8605Smrg * next paragraph) shall be included in all copies or substantial portions
16848b8605Smrg * of the Software.
17848b8605Smrg *
18848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21848b8605Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22848b8605Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23848b8605Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24848b8605Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25848b8605Smrg *
26848b8605Smrg **************************************************************************/
27848b8605Smrg
28848b8605Smrg
29848b8605Smrg#ifndef U_RECT_H
30848b8605Smrg#define U_RECT_H
31848b8605Smrg
32848b8605Smrg#include "pipe/p_compiler.h"
33b8e80941Smrg#include "util/u_math.h"
34848b8605Smrg
35848b8605Smrg#ifdef __cplusplus
36848b8605Smrgextern "C" {
37848b8605Smrg#endif
38848b8605Smrg
39848b8605Smrgstruct u_rect {
40848b8605Smrg   int x0, x1;
41848b8605Smrg   int y0, y1;
42848b8605Smrg};
43848b8605Smrg
44848b8605Smrg/* Do two rectangles intersect?
45b8e80941Smrg * Note: empty rectangles are valid as inputs (and never intersect).
46848b8605Smrg */
47b8e80941Smrgstatic inline boolean
48848b8605Smrgu_rect_test_intersection(const struct u_rect *a,
49848b8605Smrg                         const struct u_rect *b)
50848b8605Smrg{
51848b8605Smrg   return (!(a->x1 < b->x0 ||
52848b8605Smrg             b->x1 < a->x0 ||
53848b8605Smrg             a->y1 < b->y0 ||
54b8e80941Smrg             b->y1 < a->y0 ||
55b8e80941Smrg             a->x1 < a->x0 ||
56b8e80941Smrg             a->y1 < a->y0 ||
57b8e80941Smrg             b->x1 < b->x0 ||
58b8e80941Smrg             b->y1 < b->y0));
59848b8605Smrg}
60848b8605Smrg
61848b8605Smrg/* Find the intersection of two rectangles known to intersect.
62848b8605Smrg */
63b8e80941Smrgstatic inline void
64848b8605Smrgu_rect_find_intersection(const struct u_rect *a,
65848b8605Smrg                         struct u_rect *b)
66848b8605Smrg{
67848b8605Smrg   /* Caller should verify intersection exists before calling.
68848b8605Smrg    */
69848b8605Smrg   if (b->x0 < a->x0) b->x0 = a->x0;
70848b8605Smrg   if (b->x1 > a->x1) b->x1 = a->x1;
71848b8605Smrg   if (b->y0 < a->y0) b->y0 = a->y0;
72848b8605Smrg   if (b->y1 > a->y1) b->y1 = a->y1;
73848b8605Smrg}
74848b8605Smrg
75848b8605Smrg
76b8e80941Smrgstatic inline int
77b8e80941Smrgu_rect_area(const struct u_rect *r)
78b8e80941Smrg{
79b8e80941Smrg   return (r->x1 - r->x0) * (r->y1 - r->y0);
80b8e80941Smrg}
81b8e80941Smrg
82b8e80941Smrgstatic inline void
83848b8605Smrgu_rect_possible_intersection(const struct u_rect *a,
84848b8605Smrg                             struct u_rect *b)
85848b8605Smrg{
86848b8605Smrg   if (u_rect_test_intersection(a,b)) {
87848b8605Smrg      u_rect_find_intersection(a,b);
88848b8605Smrg   }
89848b8605Smrg   else {
90b8e80941Smrg      /*
91b8e80941Smrg       * Note the u_rect_xx tests deal with inclusive coordinates
92b8e80941Smrg       * hence all-zero would not be an empty box.
93b8e80941Smrg       */
94b8e80941Smrg      b->x0 = b->y0 = 0;
95b8e80941Smrg      b->x1 = b->y1 = -1;
96848b8605Smrg   }
97848b8605Smrg}
98848b8605Smrg
99b8e80941Smrg/* Set @d to a rectangle that covers both @a and @b.
100b8e80941Smrg */
101b8e80941Smrgstatic inline void
102b8e80941Smrgu_rect_union(struct u_rect *d, const struct u_rect *a, const struct u_rect *b)
103b8e80941Smrg{
104b8e80941Smrg   d->x0 = MIN2(a->x0, b->x0);
105b8e80941Smrg   d->y0 = MIN2(a->y0, b->y0);
106b8e80941Smrg   d->x1 = MAX2(a->x1, b->x1);
107b8e80941Smrg   d->y1 = MAX2(a->y1, b->y1);
108b8e80941Smrg}
109b8e80941Smrg
110848b8605Smrg#ifdef __cplusplus
111848b8605Smrg}
112848b8605Smrg#endif
113848b8605Smrg
114848b8605Smrg#endif /* U_RECT_H */
115