Chipmunk2D Pro API Reference  7.0.0
cpBB.h
1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
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 
22 #ifndef CHIPMUNK_BB_H
23 #define CHIPMUNK_BB_H
24 
25 #include "chipmunk_types.h"
26 #include "cpVect.h"
27 
31 
33 typedef struct cpBB{
34  cpFloat l, b, r ,t;
35 } cpBB;
36 
38 static inline cpBB cpBBNew(const cpFloat l, const cpFloat b, const cpFloat r, const cpFloat t)
39 {
40  cpBB bb = {l, b, r, t};
41  return bb;
42 }
43 
45 static inline cpBB
46 cpBBNewForExtents(const cpVect c, const cpFloat hw, const cpFloat hh)
47 {
48  return cpBBNew(c.x - hw, c.y - hh, c.x + hw, c.y + hh);
49 }
50 
52 static inline cpBB cpBBNewForCircle(const cpVect p, const cpFloat r)
53 {
54  return cpBBNewForExtents(p, r, r);
55 }
56 
58 static inline cpBool cpBBIntersects(const cpBB a, const cpBB b)
59 {
60  return (a.l <= b.r && b.l <= a.r && a.b <= b.t && b.b <= a.t);
61 }
62 
64 static inline cpBool cpBBContainsBB(const cpBB bb, const cpBB other)
65 {
66  return (bb.l <= other.l && bb.r >= other.r && bb.b <= other.b && bb.t >= other.t);
67 }
68 
70 static inline cpBool cpBBContainsVect(const cpBB bb, const cpVect v)
71 {
72  return (bb.l <= v.x && bb.r >= v.x && bb.b <= v.y && bb.t >= v.y);
73 }
74 
76 static inline cpBB cpBBMerge(const cpBB a, const cpBB b){
77  return cpBBNew(
78  cpfmin(a.l, b.l),
79  cpfmin(a.b, b.b),
80  cpfmax(a.r, b.r),
81  cpfmax(a.t, b.t)
82  );
83 }
84 
86 static inline cpBB cpBBExpand(const cpBB bb, const cpVect v){
87  return cpBBNew(
88  cpfmin(bb.l, v.x),
89  cpfmin(bb.b, v.y),
90  cpfmax(bb.r, v.x),
91  cpfmax(bb.t, v.y)
92  );
93 }
94 
96 static inline cpVect
98 {
99  return cpvlerp(cpv(bb.l, bb.b), cpv(bb.r, bb.t), 0.5f);
100 }
101 
103 static inline cpFloat cpBBArea(cpBB bb)
104 {
105  return (bb.r - bb.l)*(bb.t - bb.b);
106 }
107 
109 static inline cpFloat cpBBMergedArea(cpBB a, cpBB b)
110 {
111  return (cpfmax(a.r, b.r) - cpfmin(a.l, b.l))*(cpfmax(a.t, b.t) - cpfmin(a.b, b.b));
112 }
113 
115 static inline cpFloat cpBBSegmentQuery(cpBB bb, cpVect a, cpVect b)
116 {
117  cpFloat idx = 1.0f/(b.x - a.x);
118 #ifdef _MSC_VER
119 #pragma warning(disable: 4056)
120 #endif
121  cpFloat tx1 = (bb.l == a.x ? -INFINITY : (bb.l - a.x)*idx);
122  cpFloat tx2 = (bb.r == a.x ? INFINITY : (bb.r - a.x)*idx);
123  cpFloat txmin = cpfmin(tx1, tx2);
124  cpFloat txmax = cpfmax(tx1, tx2);
125 
126  cpFloat idy = 1.0f/(b.y - a.y);
127  cpFloat ty1 = (bb.b == a.y ? -INFINITY : (bb.b - a.y)*idy);
128  cpFloat ty2 = (bb.t == a.y ? INFINITY : (bb.t - a.y)*idy);
129 #ifdef _MSC_VER
130 #pragma warning(default: 4056)
131 #endif
132  cpFloat tymin = cpfmin(ty1, ty2);
133  cpFloat tymax = cpfmax(ty1, ty2);
134 
135  if(tymin <= txmax && txmin <= tymax){
136  cpFloat min = cpfmax(txmin, tymin);
137  cpFloat max = cpfmin(txmax, tymax);
138 
139  if(0.0 <= max && min <= 1.0) return cpfmax(min, 0.0);
140  }
141 
142  return INFINITY;
143 }
144 
147 {
148  return (cpBBSegmentQuery(bb, a, b) != INFINITY);
149 }
150 
152 static inline cpVect
153 cpBBClampVect(const cpBB bb, const cpVect v)
154 {
155  return cpv(cpfclamp(v.x, bb.l, bb.r), cpfclamp(v.y, bb.b, bb.t));
156 }
157 
159 static inline cpVect
160 cpBBWrapVect(const cpBB bb, const cpVect v)
161 {
162  cpFloat dx = cpfabs(bb.r - bb.l);
163  cpFloat modx = cpfmod(v.x - bb.l, dx);
164  cpFloat x = (modx > 0.0f) ? modx : modx + dx;
165 
166  cpFloat dy = cpfabs(bb.t - bb.b);
167  cpFloat mody = cpfmod(v.y - bb.b, dy);
168  cpFloat y = (mody > 0.0f) ? mody : mody + dy;
169 
170  return cpv(x + bb.l, y + bb.b);
171 }
172 
174 static inline cpBB
175 cpBBOffset(const cpBB bb, const cpVect v)
176 {
177  return cpBBNew(
178  bb.l + v.x,
179  bb.b + v.y,
180  bb.r + v.x,
181  bb.t + v.y
182  );
183 }
184 
186 
187 #endif
static cpBB cpBBMerge(const cpBB a, const cpBB b)
Returns a bounding box that holds both bounding boxes.
Definition: cpBB.h:76
static cpBB cpBBOffset(const cpBB bb, const cpVect v)
Returns a bounding box offseted by v.
Definition: cpBB.h:175
static cpBB cpBBNew(const cpFloat l, const cpFloat b, const cpFloat r, const cpFloat t)
Convenience constructor for cpBB structs.
Definition: cpBB.h:38
static cpBool cpBBIntersects(const cpBB a, const cpBB b)
Returns true if a and b intersect.
Definition: cpBB.h:58
static cpFloat cpfabs(cpFloat f)
Return the absolute value of a cpFloat.
Definition: chipmunk_types.h:152
unsigned char cpBool
Chipmunk&#39;s boolean type.
Definition: chipmunk_types.h:199
static cpVect cpBBWrapVect(const cpBB bb, const cpVect v)
Wrap a vector to a bounding box.
Definition: cpBB.h:160
static cpFloat cpfmin(cpFloat a, cpFloat b)
Return the min of two cpFloats.
Definition: chipmunk_types.h:146
static cpBool cpBBContainsBB(const cpBB bb, const cpBB other)
Returns true if other lies completely within bb.
Definition: cpBB.h:64
static cpVect cpBBCenter(cpBB bb)
Returns the center of a bounding box.
Definition: cpBB.h:97
static cpBB cpBBNewForCircle(const cpVect p, const cpFloat r)
Constructs a cpBB for a circle with the given position and radius.
Definition: cpBB.h:52
struct cpBB cpBB
Chipmunk&#39;s axis-aligned 2D bounding box type. (left, bottom, right, top)
static cpVect cpBBClampVect(const cpBB bb, const cpVect v)
Clamp a vector to a bounding box.
Definition: cpBB.h:153
static cpFloat cpfclamp(cpFloat f, cpFloat min, cpFloat max)
Clamp f to be between min and max.
Definition: chipmunk_types.h:158
double cpFloat
Chipmunk&#39;s floating point type.
Definition: chipmunk_types.h:83
static cpBB cpBBNewForExtents(const cpVect c, const cpFloat hw, const cpFloat hh)
Constructs a cpBB centered on a point with the given extents (half sizes).
Definition: cpBB.h:46
Definition: chipmunk_types.h:271
static cpVect cpvlerp(const cpVect v1, const cpVect v2, const cpFloat t)
Linearly interpolate between v1 and v2.
Definition: cpVect.h:140
static cpFloat cpfmax(cpFloat a, cpFloat b)
Return the max of two cpFloats.
Definition: chipmunk_types.h:140
static cpBB cpBBExpand(const cpBB bb, const cpVect v)
Returns a bounding box that holds both bb and v.
Definition: cpBB.h:86
static cpVect cpv(const cpFloat x, const cpFloat y)
Convenience constructor for cpVect structs.
Definition: cpVect.h:35
static cpBool cpBBContainsVect(const cpBB bb, const cpVect v)
Returns true if bb contains v.
Definition: cpBB.h:70
static cpFloat cpBBSegmentQuery(cpBB bb, cpVect a, cpVect b)
Returns the fraction along the segment query the cpBB is hit. Returns INFINITY if it doesn&#39;t hit...
Definition: cpBB.h:115
static cpFloat cpBBMergedArea(cpBB a, cpBB b)
Merges a and b and returns the area of the merged bounding box.
Definition: cpBB.h:109
Chipmunk&#39;s axis-aligned 2D bounding box type. (left, bottom, right, top)
Definition: cpBB.h:33
static cpFloat cpBBArea(cpBB bb)
Returns the area of the bounding box.
Definition: cpBB.h:103
static cpBool cpBBIntersectsSegment(cpBB bb, cpVect a, cpVect b)
Return true if the bounding box intersects the line segment with ends a and b.
Definition: cpBB.h:146