Skip to content

Commit a7b0287

Browse files
committed
Inline frequently called code
1 parent b757109 commit a7b0287

File tree

2 files changed

+144
-153
lines changed

2 files changed

+144
-153
lines changed

src/xrCore/Collision/Frustum.cpp

Lines changed: 1 addition & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void CFrustum::_add (Fvector& P1, Fvector& P2, Fvector&P3)
4444
#define My 4
4545
#define Mz 5
4646

47-
u32 frustum_aabb_remap [8][6] =
47+
u32 XRCORE_API frustum_aabb_remap[8][6]
4848
{
4949
{ Mx,My,Mz,mx,my,mz},
5050
{ Mx,My,mz,mx,my,Mz},
@@ -56,89 +56,6 @@ u32 frustum_aabb_remap [8][6] =
5656
{ mx,my,mz,Mx,My,Mz}
5757
};
5858

59-
//////////////////////////////////////////////////////////////////////
60-
EFC_Visible CFrustum::testSphere (Fvector& c, float r, u32& test_mask) const
61-
{
62-
u32 bit = 1;
63-
for (int i=0; i<p_count; i++, bit<<=1)
64-
{
65-
if (test_mask&bit) {
66-
float cls = planes[i].classify (c);
67-
if (cls>r) { test_mask=0; return fcvNone;} // none - return
68-
if (_abs(cls)>=r) test_mask&=~bit; // fully - no need to test this plane
69-
}
70-
}
71-
return test_mask ? fcvPartial:fcvFully;
72-
}
73-
74-
BOOL CFrustum::testSphere_dirty(const Fvector& c, float r) const
75-
{
76-
switch (p_count) {
77-
case 12:if (planes[11].classify(c)>r) return FALSE;
78-
case 11:if (planes[10].classify(c)>r) return FALSE;
79-
case 10:if (planes[9].classify(c)>r) return FALSE;
80-
case 9: if (planes[8].classify(c)>r) return FALSE;
81-
case 8: if (planes[7].classify(c)>r) return FALSE;
82-
case 7: if (planes[6].classify(c)>r) return FALSE;
83-
case 6: if (planes[5].classify(c)>r) return FALSE;
84-
case 5: if (planes[4].classify(c)>r) return FALSE;
85-
case 4: if (planes[3].classify(c)>r) return FALSE;
86-
case 3: if (planes[2].classify(c)>r) return FALSE;
87-
case 2: if (planes[1].classify(c)>r) return FALSE;
88-
case 1: if (planes[0].classify(c)>r) return FALSE;
89-
case 0: break;
90-
default: NODEFAULT;
91-
}
92-
return TRUE;
93-
}
94-
95-
EFC_Visible CFrustum::testAABB (const float* mM, u32& test_mask) const
96-
{
97-
// go for trivial rejection or acceptance using "faster overlap test"
98-
u32 bit = 1;
99-
100-
for (int i=0; i<p_count; i++, bit<<=1)
101-
{
102-
if (test_mask&bit) {
103-
EFC_Visible r = AABB_OverlapPlane(planes[i],mM);
104-
if (fcvFully==r) test_mask&=~bit; // fully - no need to test this plane
105-
else if (fcvNone==r){ test_mask=0; return fcvNone; } // none - return
106-
}
107-
}
108-
return test_mask ? fcvPartial:fcvFully;
109-
}
110-
111-
EFC_Visible CFrustum::testSAABB (Fvector& c, float r, const float* mM, u32& test_mask) const
112-
{
113-
u32 bit = 1;
114-
for (int i=0; i<p_count; i++, bit<<=1)
115-
{
116-
if (test_mask&bit) {
117-
float cls = planes[i].classify(c);
118-
if (cls>r) { test_mask=0; return fcvNone;} // none - return
119-
if (_abs(cls)>=r) test_mask&=~bit; // fully - no need to test this plane
120-
else {
121-
EFC_Visible r_ = AABB_OverlapPlane(planes[i],mM);
122-
if (fcvFully==r_) test_mask&=~bit; // fully - no need to test this plane
123-
else if (fcvNone==r_){ test_mask=0; return fcvNone; } // none - return
124-
}
125-
}
126-
}
127-
return test_mask ? fcvPartial:fcvFully;
128-
}
129-
130-
BOOL CFrustum::testPolyInside_dirty(Fvector* p, int count) const
131-
{
132-
Fvector* e = p+count;
133-
for (int i=0; i<p_count; i++)
134-
{
135-
const fplane &P = planes[i];
136-
for (Fvector* I=p; I!=e; I++)
137-
if (P.classify(*I)>0) return false;
138-
}
139-
return true;
140-
}
141-
14259
//////////////////////////////////////////////////////////////////////
14360
void CFrustum::CreateFromPoints(Fvector* p, int count, Fvector& COP)
14461
{
@@ -280,66 +197,6 @@ void CFrustum::CreateOccluder(Fvector* p, int count, Fvector& vBase, CFrustum& c
280197
}
281198
}
282199

283-
sPoly* CFrustum::ClipPoly(sPoly& S, sPoly& D) const
284-
{
285-
sPoly* src = &D;
286-
sPoly* dest = &S;
287-
for (int i=0; i<p_count; i++)
288-
{
289-
// cache plane and swap lists
290-
const fplane &P = planes[i];
291-
std::swap (src,dest);
292-
dest->clear ();
293-
294-
// classify all points relative to plane #i
295-
float cls [FRUSTUM_SAFE];
296-
for (u32 j=0; j<src->size(); j++) cls[j]=P.classify((*src)[j]);
297-
298-
// clip everything to this plane
299-
cls[src->size()] = cls[0];
300-
src->push_back((*src)[0]);
301-
Fvector D_; float denum,t;
302-
for (u32 j=0; j<src->size()-1; j++)
303-
{
304-
if ((*src)[j].similar((*src)[j+1],EPS_S)) continue;
305-
306-
if (negative(cls[j]))
307-
{
308-
dest->push_back((*src)[j]);
309-
if (positive(cls[j+1]))
310-
{
311-
// segment intersects plane
312-
D_.sub((*src)[j+1],(*src)[j]);
313-
denum = P.n.dotproduct(D_);
314-
if (denum!=0) {
315-
t = -cls[j]/denum; //VERIFY(t<=1.f && t>=0);
316-
dest->last().mad((*src)[j],D_,t);
317-
dest->inc();
318-
}
319-
}
320-
} else {
321-
// J - outside
322-
if (negative(cls[j+1]))
323-
{
324-
// J+1 - inside
325-
// segment intersects plane
326-
D_.sub((*src)[j+1],(*src)[j]);
327-
denum = P.n.dotproduct(D_);
328-
if (denum!=0) {
329-
t = -cls[j]/denum; //VERIFY(t<=1.f && t>=0);
330-
dest->last().mad((*src)[j],D_,t);
331-
dest->inc();
332-
}
333-
}
334-
}
335-
}
336-
337-
// here we end up with complete polygon in 'dest' which is inside plane #i
338-
if (dest->size()<3) return 0;
339-
}
340-
return dest;
341-
}
342-
343200
BOOL CFrustum::CreateFromClipPoly(Fvector* p, int count, Fvector& vBase, CFrustum& clip)
344201
{
345202
VERIFY(count<FRUSTUM_MAXPLANES);

src/xrCore/Collision/Frustum.h

Lines changed: 143 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ enum EFC_Visible
2828

2929
#define FRUSTUM_SAFE (FRUSTUM_MAXPLANES*4)
3030
typedef svector<Fvector,FRUSTUM_SAFE> sPoly;
31-
extern u32 frustum_aabb_remap[8][6];
31+
extern XRCORE_API u32 frustum_aabb_remap[8][6];
3232

3333
class XRCORE_API CFrustum
3434
{
@@ -71,22 +71,156 @@ class XRCORE_API CFrustum
7171
void CreateFromPortal (sPoly* P, Fvector& vPN, Fvector& vBase, Fmatrix& mFullXFORM);
7272
void CreateFromPlanes (Fplane* p, int count);
7373

74-
sPoly* ClipPoly (sPoly& src, sPoly& dest) const;
74+
ICF sPoly* ClipPoly(sPoly& S, sPoly& D) const
75+
{
76+
sPoly* src = &D;
77+
sPoly* dest = &S;
78+
for (int i = 0; i < p_count; i++)
79+
{
80+
// cache plane and swap lists
81+
const fplane& P = planes[i];
82+
std::swap(src, dest);
83+
dest->clear();
84+
85+
// classify all points relative to plane #i
86+
float cls[FRUSTUM_SAFE];
87+
for (u32 j = 0; j < src->size(); j++) cls[j] = P.classify((*src)[j]);
88+
89+
// clip everything to this plane
90+
cls[src->size()] = cls[0];
91+
src->push_back((*src)[0]);
92+
Fvector D_; float denum, t;
93+
for (u32 j = 0; j < src->size() - 1; j++)
94+
{
95+
if ((*src)[j].similar((*src)[j + 1], EPS_S)) continue;
96+
97+
if (negative(cls[j]))
98+
{
99+
dest->push_back((*src)[j]);
100+
if (positive(cls[j + 1]))
101+
{
102+
// segment intersects plane
103+
D_.sub((*src)[j + 1], (*src)[j]);
104+
denum = P.n.dotproduct(D_);
105+
if (denum != 0) {
106+
t = -cls[j] / denum; //VERIFY(t<=1.f && t>=0);
107+
dest->last().mad((*src)[j], D_, t);
108+
dest->inc();
109+
}
110+
}
111+
}
112+
else {
113+
// J - outside
114+
if (negative(cls[j + 1]))
115+
{
116+
// J+1 - inside
117+
// segment intersects plane
118+
D_.sub((*src)[j + 1], (*src)[j]);
119+
denum = P.n.dotproduct(D_);
120+
if (denum != 0) {
121+
t = -cls[j] / denum; //VERIFY(t<=1.f && t>=0);
122+
dest->last().mad((*src)[j], D_, t);
123+
dest->inc();
124+
}
125+
}
126+
}
127+
}
128+
129+
// here we end up with complete polygon in 'dest' which is inside plane #i
130+
if (dest->size() < 3) return 0;
131+
}
132+
return dest;
133+
}
75134

76135
u32 getMask () const { return (1<<p_count)-1; }
77136

78-
EFC_Visible testSphere (Fvector& c, float r, u32& test_mask) const;
79-
BOOL testSphere_dirty (const Fvector& c, float r) const;
80-
EFC_Visible testAABB (const float* mM, u32& test_mask) const;
81-
EFC_Visible testSAABB (Fvector& c, float r, const float* mM, u32& test_mask) const;
82-
BOOL testPolyInside_dirty(Fvector* p, int count) const;
137+
ICF EFC_Visible testSphere(Fvector& c, float r, u32& test_mask) const
138+
{
139+
u32 bit = 1;
140+
for (int i = 0; i < p_count; i++, bit <<= 1)
141+
{
142+
if (test_mask & bit) {
143+
float cls = planes[i].classify(c);
144+
if (cls > r) { test_mask = 0; return fcvNone; } // none - return
145+
if (_abs(cls) >= r) test_mask &= ~bit; // fully - no need to test this plane
146+
}
147+
}
148+
return test_mask ? fcvPartial : fcvFully;
149+
}
150+
151+
ICF BOOL testSphere_dirty(const Fvector& c, float r) const
152+
{
153+
switch (p_count) {
154+
case 12:if (planes[11].classify(c) > r) return FALSE;
155+
case 11:if (planes[10].classify(c) > r) return FALSE;
156+
case 10:if (planes[9].classify(c) > r) return FALSE;
157+
case 9: if (planes[8].classify(c) > r) return FALSE;
158+
case 8: if (planes[7].classify(c) > r) return FALSE;
159+
case 7: if (planes[6].classify(c) > r) return FALSE;
160+
case 6: if (planes[5].classify(c) > r) return FALSE;
161+
case 5: if (planes[4].classify(c) > r) return FALSE;
162+
case 4: if (planes[3].classify(c) > r) return FALSE;
163+
case 3: if (planes[2].classify(c) > r) return FALSE;
164+
case 2: if (planes[1].classify(c) > r) return FALSE;
165+
case 1: if (planes[0].classify(c) > r) return FALSE;
166+
case 0: break;
167+
default: NODEFAULT;
168+
}
169+
return TRUE;
170+
}
171+
ICF EFC_Visible testAABB(const float* mM, u32& test_mask) const
172+
{
173+
// go for trivial rejection or acceptance using "faster overlap test"
174+
u32 bit = 1;
175+
176+
for (int i = 0; i < p_count; i++, bit <<= 1)
177+
{
178+
if (test_mask & bit) {
179+
EFC_Visible r = AABB_OverlapPlane(planes[i], mM);
180+
if (fcvFully == r) test_mask &= ~bit; // fully - no need to test this plane
181+
else if (fcvNone == r) { test_mask = 0; return fcvNone; } // none - return
182+
}
183+
}
184+
return test_mask ? fcvPartial : fcvFully;
185+
}
186+
187+
ICF EFC_Visible testSAABB(Fvector& c, float r, const float* mM, u32& test_mask) const
188+
{
189+
u32 bit = 1;
190+
for (int i = 0; i < p_count; i++, bit <<= 1)
191+
{
192+
if (test_mask & bit) {
193+
float cls = planes[i].classify(c);
194+
if (cls > r) { test_mask = 0; return fcvNone; } // none - return
195+
if (_abs(cls) >= r) test_mask &= ~bit; // fully - no need to test this plane
196+
else {
197+
EFC_Visible r_ = AABB_OverlapPlane(planes[i], mM);
198+
if (fcvFully == r_) test_mask &= ~bit; // fully - no need to test this plane
199+
else if (fcvNone == r_) { test_mask = 0; return fcvNone; } // none - return
200+
}
201+
}
202+
}
203+
return test_mask ? fcvPartial : fcvFully;
204+
}
205+
206+
ICF BOOL testPolyInside_dirty(Fvector* p, int count) const
207+
{
208+
Fvector* e = p + count;
209+
for (int i = 0; i < p_count; i++)
210+
{
211+
const fplane& P = planes[i];
212+
for (Fvector* I = p; I != e; I++)
213+
if (P.classify(*I) > 0) return false;
214+
}
215+
return true;
216+
}
83217

84-
IC BOOL testPolyInside (sPoly& src) const
218+
ICF BOOL testPolyInside(sPoly& src) const
85219
{
86220
sPoly d;
87221
return !!ClipPoly(src,d);
88222
}
89-
IC BOOL testPolyInside (Fvector* p, int count) const
223+
ICF BOOL testPolyInside(Fvector* p, int count) const
90224
{
91225
sPoly src(p,count);
92226
return testPolyInside(src);

0 commit comments

Comments
 (0)