Chipmunk2D Pro API Reference  6.2.1
 All Classes Functions Variables Typedefs Properties Groups Pages
cpBB.h
1 /* Copyright (c) 2007 Scott Lembcke
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19  * SOFTWARE.
20  */
21 
25 
27 typedef struct cpBB{
28  cpFloat l, b, r ,t;
29 } cpBB;
30 
32 static inline cpBB cpBBNew(const cpFloat l, const cpFloat b, const cpFloat r, const cpFloat t)
33 {
34  cpBB bb = {l, b, r, t};
35  return bb;
36 }
37 
39 static inline cpBB cpBBNewForCircle(const cpVect p, const cpFloat r)
40 {
41  return cpBBNew(p.x - r, p.y - r, p.x + r, p.y + r);
42 }
43 
45 static inline cpBool cpBBIntersects(const cpBB a, const cpBB b)
46 {
47  return (a.l <= b.r && b.l <= a.r && a.b <= b.t && b.b <= a.t);
48 }
49 
51 static inline cpBool cpBBContainsBB(const cpBB bb, const cpBB other)
52 {
53  return (bb.l <= other.l && bb.r >= other.r && bb.b <= other.b && bb.t >= other.t);
54 }
55 
57 static inline cpBool cpBBContainsVect(const cpBB bb, const cpVect v)
58 {
59  return (bb.l <= v.x && bb.r >= v.x && bb.b <= v.y && bb.t >= v.y);
60 }
61 
63 static inline cpBB cpBBMerge(const cpBB a, const cpBB b){
64  return cpBBNew(
65  cpfmin(a.l, b.l),
66  cpfmin(a.b, b.b),
67  cpfmax(a.r, b.r),
68  cpfmax(a.t, b.t)
69  );
70 }
71 
73 static inline cpBB cpBBExpand(const cpBB bb, const cpVect v){
74  return cpBBNew(
75  cpfmin(bb.l, v.x),
76  cpfmin(bb.b, v.y),
77  cpfmax(bb.r, v.x),
78  cpfmax(bb.t, v.y)
79  );
80 }
81 
83 static inline cpVect
85 {
86  return cpvlerp(cpv(bb.l, bb.b), cpv(bb.r, bb.t), 0.5f);
87 }
88 
90 static inline cpFloat cpBBArea(cpBB bb)
91 {
92  return (bb.r - bb.l)*(bb.t - bb.b);
93 }
94 
96 static inline cpFloat cpBBMergedArea(cpBB a, cpBB b)
97 {
98  return (cpfmax(a.r, b.r) - cpfmin(a.l, b.l))*(cpfmax(a.t, b.t) - cpfmin(a.b, b.b));
99 }
100 
102 static inline cpFloat cpBBSegmentQuery(cpBB bb, cpVect a, cpVect b)
103 {
104  cpFloat idx = 1.0f/(b.x - a.x);
105  cpFloat tx1 = (bb.l == a.x ? -INFINITY : (bb.l - a.x)*idx);
106  cpFloat tx2 = (bb.r == a.x ? INFINITY : (bb.r - a.x)*idx);
107  cpFloat txmin = cpfmin(tx1, tx2);
108  cpFloat txmax = cpfmax(tx1, tx2);
109 
110  cpFloat idy = 1.0f/(b.y - a.y);
111  cpFloat ty1 = (bb.b == a.y ? -INFINITY : (bb.b - a.y)*idy);
112  cpFloat ty2 = (bb.t == a.y ? INFINITY : (bb.t - a.y)*idy);
113  cpFloat tymin = cpfmin(ty1, ty2);
114  cpFloat tymax = cpfmax(ty1, ty2);
115 
116  if(tymin <= txmax && txmin <= tymax){
117  cpFloat min = cpfmax(txmin, tymin);
118  cpFloat max = cpfmin(txmax, tymax);
119 
120  if(0.0 <= max && min <= 1.0) return cpfmax(min, 0.0);
121  }
122 
123  return INFINITY;
124 }
125 
128 {
129  return (cpBBSegmentQuery(bb, a, b) != INFINITY);
130 }
131 
133 static inline cpVect
134 cpBBClampVect(const cpBB bb, const cpVect v)
135 {
136  return cpv(cpfclamp(v.x, bb.l, bb.r), cpfclamp(v.y, bb.b, bb.t));
137 }
138 
139 // TODO edge case issue
141 cpVect cpBBWrapVect(const cpBB bb, const cpVect v); // wrap a vector to a bbox
142