-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPOLYGON.CPP
More file actions
166 lines (149 loc) · 6.28 KB
/
Copy pathPOLYGON.CPP
File metadata and controls
166 lines (149 loc) · 6.28 KB
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
#include "Polygon.h"
namespace Affine{
/*
Polygon::Polygon( const unsigned int num, )
:
mNumVerticies( num )
{
return;
}
*/
/*
Polygon::Polygon( const Polygon& inSource )
:
{
return;
}
int Polygon::classify(Polygon *poly)
{
int result = CF_UNCLASSIFYED; // stores the result
float distance; // holds the distance results when we test the verts to this faces plane
for(int i=0; i<numVerts; i++) // loop through all the vertex data in the (to_classify) face
{
distance = this->distance(*poly->verts[i]); // get the distance to this point from our plane.
if(distance > -SMALL && distance < SMALL) // if the distance is zero (with float error) then this poin is on the plane
{
if(result == CF_UNCLASSIFYED || result == CF_ONPLANE) // if this is the first point tested or all other points have been on the plane
{
result = CF_ONPLANE; // classify the result so far as onplane
continue; // continue setting points
}
else continue; // otherwise leave result set as back or front and keep testing points
}
else if(distance > 0.0f) // if the distance is positive then the point is in fron of the plane.
{
if(result == CF_UNCLASSIFYED || result == CF_FRONT || result == CF_ONPLANE) // if the point is unclassifyed all front or all onplane so far then
{
result = CF_FRONT; // classify the result as front so far
continue; // continue checking points
}
else return CF_SPANNING; // otherwise we know that it is spannign and we can return for now.
}
else if(distance < 0.0f) // if the distance is negative then we know that the point is behind the plane
{
if(result == CF_UNCLASSIFYED || result == CF_BACK || result == CF_ONPLANE) // if the point is unclassifyed all back or all onplane so far then
{
result = CF_BACK; // classify the result as back so far
continue; // continue checking points
}
else return CF_SPANNING; // otherwise we know that it is spannign and we can return for now.
}
}
return result;
}
int Polygon::classify(Plane *plane)
{
int result = CF_UNCLASSIFYED; // stores the result
float distance; // holds the distance results when we test the verts to this faces plane
for(int i=0; i<numVerts; i++) // loop through all the vertex data in the (to_classify) face
{
distance = plane->distance(*this->verts[i]); // get the distance to this point from our plane.
if(distance > -SMALL && distance < SMALL) // if the distance is zero (with float error) then this poin is on the plane
{
if(result == CF_UNCLASSIFYED || result == CF_ONPLANE) // if this is the first point tested or all other points have been on the plane
{
result = CF_ONPLANE; // classify the result so far as onplane
continue; // continue setting points
}
else continue; // otherwise leave result set as back or front and keep testing points
}
else if(distance > 0.0f) // if the distance is positive then the point is in fron of the plane.
{
if(result == CF_UNCLASSIFYED || result == CF_FRONT || result == CF_ONPLANE) // if the point is unclassifyed all front or all onplane so far then
{
result = CF_FRONT; // classify the result as front so far
continue; // continue checking points
}
else return CF_SPANNING; // otherwise we know that it is spannign and we can return for now.
}
else if(distance < 0.0f) // if the distance is negative then we know that the point is behind the plane
{
if(result == CF_UNCLASSIFYED || result == CF_BACK || result == CF_ONPLANE) // if the point is unclassifyed all back or all onplane so far then
{
result = CF_BACK; // classify the result as back so far
continue; // continue checking points
}
else return CF_SPANNING; // otherwise we know that it is spannign and we can return for now.
}
}
return result;
}
void Polygon::split(Plane *splitter, Polygon *front, Polygon *back)
{
int frontIndex = 0;
int backIndex = 0;
int i;
float dist;
float lastSide = 0.0f;
Vector3 *newVertex;
Ray *edge;
front->verts = (Vector3**)malloc(sizeof(Vector3*)*(this->numVerts+1)); // get some mem
back->verts = (Vector3**)malloc(sizeof(Vector3*)*(this->numVerts+1)); // get some mem
for(i=0; i<this->numVerts; i++)
{
dist = splitter->distance(*this->verts[i]);
if((dist<0.0f && lastSide>0.0f) || (dist>0.0f && lastSide<0.0f)) // edge spanning plane
{
edge = new Ray(*this->verts[i-1], *this->verts[i]); // make an ray to signify edge
newVertex = (Vector3*)malloc(sizeof(Vector3));
edge->getIntersection(splitter, newVertex); // find itersection with plane
front->verts[frontIndex++] = newVertex; // add new vert to front
back->verts[backIndex++] = newVertex; // add new vert to back
}
if(dist > 0.0f) // in front of the plane
{
front->verts[frontIndex++] = this->verts[i]; // add vert to front poly
}
else if(dist < 0.0f) // in back of plane
{
back->verts[backIndex++] = this->verts[i]; // add vert to the back poly
}
else{ // right on split plane
front->verts[frontIndex++] = this->verts[i]; // add to front
back->verts[backIndex++] = this->verts[i]; // and back
}
lastSide = dist;
}
dist = splitter->distance(*this->verts[0]); // last edge case below
if((dist<0.0f && lastSide>0.0f) || (dist>0.0f && lastSide<0.0f)) // edge spanning plane
{
edge = new Ray(*this->verts[0], *this->verts[this->numVerts-1]);// make an ray to signify edge
newVertex = (Vector3*)malloc(sizeof(Vector3));
edge->getIntersection(splitter, newVertex); // find itersection with plane
front->verts[frontIndex++] = newVertex; // add new vert to front
back->verts[backIndex++] = newVertex; // add new vert to back
}
front->numVerts = frontIndex; // record num verts for front poly
back->numVerts = backIndex; // record num verts for back poly
}
AABB* Polygon::getPolygonAABB()
{
AABB *aabb = new AABB();
for(int i=0; i<numVerts; i++)
{
aabb->addPoint(*verts[i]);
}
return aabb;
}
*/
} // end Affine namespace