-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplane.cpp
187 lines (164 loc) · 5.16 KB
/
plane.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/***********************************************
* $author: javery
* $date : 09 Dec 01
* $descp : routines/data stuctures for plane manip.
* $path : C:\Program Files\Microsoft Visual Studio\MyProjects\KaosDemoEngine\plane.h
* $ver : 0.0.0
***********************************************/
#include "plane.h"
#include "polygon.h"
#include <stdio.h>
void MTPl_Build( pPoly_t pPoly , pPlane_t pPlane )
{
pPlane->Normal = MTP_MakeNormal( pPoly );
if( !PLY_FLG_TYPE(pPoly->PolyFlag) )
{
pPlane->vVerticies[0] = *pPoly->pVerticies[0];
pPlane->vVerticies[1] = *pPoly->pVerticies[1];
pPlane->vVerticies[2] = *pPoly->pVerticies[2];
}
else
{
pPlane->vVerticies[0] = *pPoly->pVerticies[0];
pPlane->vVerticies[1] = *pPoly->pVerticies[1];
pPlane->vVerticies[2] = *pPoly->pVerticies[2];
pPlane->vVerticies[3] = *pPoly->pVerticies[3];
}
pPlane->distance =
-((pPlane->Normal.xComponent*pPlane->vVerticies[0].xComponent)+
(pPlane->Normal.yComponent*pPlane->vVerticies[0].yComponent)+
(pPlane->Normal.zComponent*pPlane->vVerticies[0].zComponent));
}
/*
****************************************************
* MTPl_Paramertize
* returns the coefficents of std form of plane eqn;
* with p.D == v->w ( Ax + Bx + Cx + D )
****************************************************
*/
Vect3_t MTPl_Paramatize( pPlane_t pPlane )
{
Vect3_t params;
params.xComponent = pPlane->Normal.xComponent;
params.yComponent = pPlane->Normal.yComponent;
params.zComponent = pPlane->Normal.zComponent;
params.wComponent = pPlane->distance;
return params;
}
/*
*****************************************************
* MTPl_Evaluate
* evalues the value of the planes eqn for the passed
* vector;
*****************************************************
*/
float MTPl_Evaluate( pPlane_t p , pVect3_t v3d )
{
return (p->Normal.xComponent*v3d->xComponent+
p->Normal.yComponent*v3d->yComponent+
p->Normal.zComponent*v3d->zComponent+
p->distance);
}
/*
******************************************************
* MTPl_Classify
* spatialy classifies the passed polygon in relation to
* the passed plane.
*******************************************************
*/
Polygon_PlaneRel MTPl_Classify( pPlane_t p , pPoly_t poly )
{
if( !PLY_FLG_TYPE(poly->PolyFlag) )
{
if( (MTPl_Evaluate( p , poly->pVerticies[0] ) > 0 ) &&
(MTPl_Evaluate( p , poly->pVerticies[1] ) > 0 ) &&
(MTPl_Evaluate( p , poly->pVerticies[2] ) > 0 ) )
{
return PPR_FRONT;
}
else if
( (MTPl_Evaluate( p , poly->pVerticies[0] ) < 0 ) &&
(MTPl_Evaluate( p , poly->pVerticies[1] ) < 0 ) &&
(MTPl_Evaluate( p , poly->pVerticies[2] ) < 0 ) )
{
return PPR_BEHIEND;
}
else if
( (!MTPl_Evaluate( p , poly->pVerticies[0] )) &&
(!MTPl_Evaluate( p , poly->pVerticies[1] )) &&
(!MTPl_Evaluate( p , poly->pVerticies[2] )) )
{
return PPR_CONSISTENT;
}
else
{
return PPR_SPLIT;
}
}
else
{
if( (MTPl_Evaluate( p , poly->pVerticies[0] ) > 0 ) &&
(MTPl_Evaluate( p , poly->pVerticies[1] ) > 0 ) &&
(MTPl_Evaluate( p , poly->pVerticies[2] ) > 0 ) &&
(MTPl_Evaluate( p , poly->pVerticies[3] ) > 0 ) )
{
return PPR_FRONT;
}
else if
( (MTPl_Evaluate( p , poly->pVerticies[0] ) < 0 ) &&
(MTPl_Evaluate( p , poly->pVerticies[1] ) < 0 ) &&
(MTPl_Evaluate( p , poly->pVerticies[2] ) < 0 ) &&
(MTPl_Evaluate( p , poly->pVerticies[3] ) < 0 ) )
{
return PPR_BEHIEND;
}
else if
( (!MTPl_Evaluate( p , poly->pVerticies[0] )) &&
(!MTPl_Evaluate( p , poly->pVerticies[1] )) &&
(!MTPl_Evaluate( p , poly->pVerticies[2] )) &&
(!MTPl_Evaluate( p , poly->pVerticies[3] )) )
{
return PPR_CONSISTENT;
}
else
{
return PPR_SPLIT;
}
}
}
/*
****************************************************
* MTPl_Intersect
* Returns the point of intersection ( if it exsists )
* between the passed plane and polygon.
****************************************************
*/
Vect3_t MTPl_Intersect( pPlane_t pPlane , pPoly_t pPoly , pVect3_t pInt_1 , pVect3_t pInt_2 )
{
float t=0,numerator=0,denomenator=0,
normX=pPlane->Normal.xComponent,
normY=pPlane->Normal.yComponent,
normZ=pPlane->Normal.zComponent,
deltaX=pInt_2->xComponent - pInt_1->xComponent,
deltaY=pInt_2->yComponent - pInt_1->yComponent,
deltaZ=pInt_2->zComponent - pInt_1->zComponent,
tempX=pPlane->vVerticies[0].xComponent-pInt_1->xComponent,
tempY=pPlane->vVerticies[0].yComponent-pInt_1->yComponent,
tempZ=pPlane->vVerticies[0].zComponent-pInt_1->zComponent;
Vect3_t vIntersection;
//
// plane polygon intersection eqn
// Ax + By + Cz + D
// t = ------------------------------------
// A(delta x) + B(delta y) + C(delta z)
//
// vector_component_int = vector_component_0 + t(delta_vector_exponent)
// numerator = MTPl_Evaluate( pPlane , pInt_1 );
numerator = (normX*tempX)+(normY*tempY)+(normZ*tempZ);
denomenator = (normX*deltaX)+(normY*deltaY)+(normZ*deltaZ);
if(denomenator){t = numerator/denomenator;} else{t=0.0f;}
MTV_Init( vIntersection , (((t*(deltaX)) + (pInt_1->xComponent))) ,
(((t*(deltaY)) + (pInt_1->yComponent))) ,
(((t*(deltaZ)) + (pInt_1->zComponent))) );
return vIntersection;
}