14a49301eSmrg/************************************************************************** 24a49301eSmrg * 3af69d88dSmrg * Copyright 2008 VMware, Inc. 44a49301eSmrg * All Rights Reserved. 54a49301eSmrg * 64a49301eSmrg * Permission is hereby granted, free of charge, to any person obtaining a 74a49301eSmrg * copy of this software and associated documentation files (the 84a49301eSmrg * "Software"), to deal in the Software without restriction, including 94a49301eSmrg * without limitation the rights to use, copy, modify, merge, publish, 104a49301eSmrg * distribute, sub license, and/or sell copies of the Software, and to 114a49301eSmrg * permit persons to whom the Software is furnished to do so, subject to 124a49301eSmrg * the following conditions: 134a49301eSmrg * 144a49301eSmrg * The above copyright notice and this permission notice (including the 154a49301eSmrg * next paragraph) shall be included in all copies or substantial portions 164a49301eSmrg * of the Software. 174a49301eSmrg * 184a49301eSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 194a49301eSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 204a49301eSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21af69d88dSmrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 224a49301eSmrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 234a49301eSmrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 244a49301eSmrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 254a49301eSmrg * 264a49301eSmrg **************************************************************************/ 274a49301eSmrg 284a49301eSmrg 293464ebd5Sriastradh#ifndef U_RECT_H 303464ebd5Sriastradh#define U_RECT_H 313464ebd5Sriastradh 323464ebd5Sriastradh#include "pipe/p_compiler.h" 3301e04c3fSmrg#include "util/u_math.h" 343464ebd5Sriastradh 35af69d88dSmrg#ifdef __cplusplus 36af69d88dSmrgextern "C" { 37af69d88dSmrg#endif 38af69d88dSmrg 393464ebd5Sriastradhstruct u_rect { 403464ebd5Sriastradh int x0, x1; 413464ebd5Sriastradh int y0, y1; 423464ebd5Sriastradh}; 433464ebd5Sriastradh 443464ebd5Sriastradh/* Do two rectangles intersect? 4501e04c3fSmrg * Note: empty rectangles are valid as inputs (and never intersect). 464a49301eSmrg */ 4701e04c3fSmrgstatic inline boolean 483464ebd5Sriastradhu_rect_test_intersection(const struct u_rect *a, 493464ebd5Sriastradh const struct u_rect *b) 503464ebd5Sriastradh{ 513464ebd5Sriastradh return (!(a->x1 < b->x0 || 523464ebd5Sriastradh b->x1 < a->x0 || 533464ebd5Sriastradh a->y1 < b->y0 || 5401e04c3fSmrg b->y1 < a->y0 || 5501e04c3fSmrg a->x1 < a->x0 || 5601e04c3fSmrg a->y1 < a->y0 || 5701e04c3fSmrg b->x1 < b->x0 || 5801e04c3fSmrg b->y1 < b->y0)); 593464ebd5Sriastradh} 604a49301eSmrg 613464ebd5Sriastradh/* Find the intersection of two rectangles known to intersect. 623464ebd5Sriastradh */ 6301e04c3fSmrgstatic inline void 643464ebd5Sriastradhu_rect_find_intersection(const struct u_rect *a, 653464ebd5Sriastradh struct u_rect *b) 663464ebd5Sriastradh{ 673464ebd5Sriastradh /* Caller should verify intersection exists before calling. 683464ebd5Sriastradh */ 693464ebd5Sriastradh if (b->x0 < a->x0) b->x0 = a->x0; 703464ebd5Sriastradh if (b->x1 > a->x1) b->x1 = a->x1; 713464ebd5Sriastradh if (b->y0 < a->y0) b->y0 = a->y0; 723464ebd5Sriastradh if (b->y1 > a->y1) b->y1 = a->y1; 733464ebd5Sriastradh} 744a49301eSmrg 754a49301eSmrg 7601e04c3fSmrgstatic inline int 7701e04c3fSmrgu_rect_area(const struct u_rect *r) 7801e04c3fSmrg{ 7901e04c3fSmrg return (r->x1 - r->x0) * (r->y1 - r->y0); 8001e04c3fSmrg} 8101e04c3fSmrg 8201e04c3fSmrgstatic inline void 833464ebd5Sriastradhu_rect_possible_intersection(const struct u_rect *a, 843464ebd5Sriastradh struct u_rect *b) 853464ebd5Sriastradh{ 863464ebd5Sriastradh if (u_rect_test_intersection(a,b)) { 873464ebd5Sriastradh u_rect_find_intersection(a,b); 883464ebd5Sriastradh } 893464ebd5Sriastradh else { 9001e04c3fSmrg /* 9101e04c3fSmrg * Note the u_rect_xx tests deal with inclusive coordinates 9201e04c3fSmrg * hence all-zero would not be an empty box. 9301e04c3fSmrg */ 9401e04c3fSmrg b->x0 = b->y0 = 0; 9501e04c3fSmrg b->x1 = b->y1 = -1; 963464ebd5Sriastradh } 973464ebd5Sriastradh} 984a49301eSmrg 9901e04c3fSmrg/* Set @d to a rectangle that covers both @a and @b. 10001e04c3fSmrg */ 10101e04c3fSmrgstatic inline void 10201e04c3fSmrgu_rect_union(struct u_rect *d, const struct u_rect *a, const struct u_rect *b) 10301e04c3fSmrg{ 10401e04c3fSmrg d->x0 = MIN2(a->x0, b->x0); 10501e04c3fSmrg d->y0 = MIN2(a->y0, b->y0); 10601e04c3fSmrg d->x1 = MAX2(a->x1, b->x1); 10701e04c3fSmrg d->y1 = MAX2(a->y1, b->y1); 10801e04c3fSmrg} 10901e04c3fSmrg 110af69d88dSmrg#ifdef __cplusplus 111af69d88dSmrg} 112af69d88dSmrg#endif 1134a49301eSmrg 1144a49301eSmrg#endif /* U_RECT_H */ 115