1 | #include "Polygon3.h"
|
---|
2 | #include "Mesh.h"
|
---|
3 | #include "ViewCellBsp.h" // TODO: erase this |
---|
4 | #include "Mesh.h" |
---|
5 | #include "AxisAlignedBox3.h" |
---|
6 | #include "Ray.h" |
---|
7 | |
---|
8 | Polygon3::Polygon3(): |
---|
9 | mMaterial(NULL), mParent(NULL), mPiercingRays(NULL) |
---|
10 | {} |
---|
11 | |
---|
12 | Polygon3::Polygon3(const VertexContainer &vertices): |
---|
13 | mVertices(vertices), mMaterial(NULL), mParent(NULL), mPiercingRays(NULL) |
---|
14 | {} |
---|
15 | |
---|
16 | Polygon3::Polygon3(MeshInstance *parent): |
---|
17 | mMaterial(NULL), mParent(parent) |
---|
18 | {} |
---|
19 | |
---|
20 | Polygon3::Polygon3(Face *face, Mesh *parentMesh) |
---|
21 | { |
---|
22 | VertexIndexContainer::iterator it = face->mVertexIndices.begin(); |
---|
23 | for (; it != face->mVertexIndices.end(); ++it) |
---|
24 | { |
---|
25 | mVertices.push_back(parentMesh->mVertices[*it]); |
---|
26 | mMaterial = parentMesh->mMaterial; |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | Polygon3::~Polygon3() |
---|
31 | { |
---|
32 | DEL_PTR(mPiercingRays); |
---|
33 | } |
---|
34 | |
---|
35 | Plane3 Polygon3::GetSupportingPlane() const |
---|
36 | { |
---|
37 | return Plane3(mVertices[0], mVertices[1], mVertices[2]); |
---|
38 | } |
---|
39 | |
---|
40 | Vector3 Polygon3::GetNormal() const
|
---|
41 | {
|
---|
42 | return Normalize(CrossProd(mVertices[2] - mVertices[1],
|
---|
43 | mVertices[0] - mVertices[1]));
|
---|
44 | } |
---|
45 | |
---|
46 | void Polygon3::Split(const Plane3 &partition, |
---|
47 | Polygon3 &front, |
---|
48 | Polygon3 &back, |
---|
49 | VertexContainer &splitPts) |
---|
50 | { |
---|
51 | Vector3 ptA = mVertices.back(); |
---|
52 | |
---|
53 | int sideA = partition.Side(ptA, SIDE_TOLERANCE); |
---|
54 | |
---|
55 | VertexContainer::const_iterator it; |
---|
56 | |
---|
57 | bool foundSplit = false; |
---|
58 | // find line - plane intersections |
---|
59 | for (it = mVertices.begin(); it != mVertices.end(); ++ it) |
---|
60 | { |
---|
61 | Vector3 ptB = *it; |
---|
62 | int sideB = partition.Side(ptB, SIDE_TOLERANCE); |
---|
63 | |
---|
64 | // vertices on different sides => split |
---|
65 | if (sideB > 0)
|
---|
66 | {
|
---|
67 | if (sideA < 0)
|
---|
68 | { |
---|
69 | //-- plane - line intersection |
---|
70 | Vector3 splitPt = partition.FindIntersection(ptA, ptB); |
---|
71 | |
---|
72 | // test if split point not too close to previous split point |
---|
73 | if (!foundSplit || |
---|
74 | (SqrDistance(splitPt, splitPts.back()) > SIDE_TOLERANCE_SQRD)) |
---|
75 | { |
---|
76 | // add vertex to both polygons |
---|
77 | front.mVertices.push_back(splitPt); |
---|
78 | back.mVertices.push_back(splitPt); |
---|
79 | |
---|
80 | splitPts.push_back(splitPt); |
---|
81 | foundSplit = true; |
---|
82 | } |
---|
83 | } |
---|
84 | front.mVertices.push_back(ptB); |
---|
85 | } |
---|
86 | else if (sideB < 0)
|
---|
87 | {
|
---|
88 | if (sideA > 0)
|
---|
89 | { |
---|
90 | //-- plane - line intersection |
---|
91 | Vector3 splitPt = partition.FindIntersection(ptA, ptB); |
---|
92 | // test if split point not too close to other split point |
---|
93 | // test if split point not too close to previous split point |
---|
94 | if (!foundSplit || |
---|
95 | (SqrDistance(splitPt, splitPts.back()) > SIDE_TOLERANCE_SQRD)) |
---|
96 | { |
---|
97 | // add vertex to both polygons |
---|
98 | front.mVertices.push_back(splitPt); |
---|
99 | back.mVertices.push_back(splitPt); |
---|
100 | |
---|
101 | splitPts.push_back(splitPt); |
---|
102 | foundSplit = true; |
---|
103 | } |
---|
104 | } |
---|
105 | back.mVertices.push_back(ptB); |
---|
106 | } |
---|
107 | else |
---|
108 | { |
---|
109 | // vertex on plane => add vertex to both polygons |
---|
110 | front.mVertices.push_back(ptB); |
---|
111 | back.mVertices.push_back(ptB); |
---|
112 | } |
---|
113 | |
---|
114 | ptA = ptB; |
---|
115 | sideA = sideB; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | float Polygon3::GetArea() const |
---|
120 | { |
---|
121 | Vector3 v = CrossProd(mVertices.back(), mVertices.front());
|
---|
122 |
|
---|
123 | for (int i=0; i < mVertices.size() - 1; ++i)
|
---|
124 | v += CrossProd(mVertices[i], mVertices[i+1]);
|
---|
125 |
|
---|
126 | //Debug << "area2: " << 0.5f * fabs(DotProd(GetNormal(), v)) << endl; |
---|
127 | return 0.5f * fabs(DotProd(GetNormal(), v)); |
---|
128 | } |
---|
129 | |
---|
130 | int Polygon3::Side(const Plane3 &plane) const |
---|
131 | { |
---|
132 | int classification = ClassifyPlane(plane); |
---|
133 | |
---|
134 | if (classification == BACK_SIDE) |
---|
135 | return -1; |
---|
136 | else if (classification == FRONT_SIDE) |
---|
137 | return 1; |
---|
138 | |
---|
139 | return 0; |
---|
140 | } |
---|
141 | |
---|
142 | int Polygon3::ClassifyPlane(const Plane3 &plane) const |
---|
143 | { |
---|
144 | VertexContainer::const_iterator it; |
---|
145 | |
---|
146 | bool onFrontSide = false; |
---|
147 | bool onBackSide = false; |
---|
148 | |
---|
149 | int count = 0; |
---|
150 | // find possible line-plane intersections |
---|
151 | for (it = mVertices.begin(); it != mVertices.end(); ++ it) |
---|
152 | { |
---|
153 | int side = plane.Side(*it, SIDE_TOLERANCE); |
---|
154 | |
---|
155 | if (side > 0) |
---|
156 | onFrontSide = true; |
---|
157 | else if (side < 0) |
---|
158 | onBackSide = true; |
---|
159 | |
---|
160 | //TODO: check if split goes through vertex |
---|
161 | if (onFrontSide && onBackSide) // split |
---|
162 | { |
---|
163 | return SPLIT; |
---|
164 | } |
---|
165 | // 3 vertices enough to decide coincident |
---|
166 | else if (((++ count) >= 3) && !onFrontSide && !onBackSide) |
---|
167 | { |
---|
168 | return COINCIDENT; |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | if (onBackSide) |
---|
173 | { |
---|
174 | return BACK_SIDE; |
---|
175 | } |
---|
176 | else if (onFrontSide) |
---|
177 | { |
---|
178 | return FRONT_SIDE; |
---|
179 | } |
---|
180 | |
---|
181 | return COINCIDENT; // plane and polygon are coincident |
---|
182 | } |
---|
183 |
|
---|
184 | |
---|
185 | Vector3 |
---|
186 | Polygon3::Center() const |
---|
187 | { |
---|
188 | int i; |
---|
189 | Vector3 sum = mVertices[0]; |
---|
190 | for (i=1; i < mVertices.size(); i++) |
---|
191 | sum += mVertices[i]; |
---|
192 | |
---|
193 | return sum/(float)i; |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | void |
---|
198 | Polygon3::Scale(const float scale) |
---|
199 | { |
---|
200 | int i; |
---|
201 | Vector3 center = Center(); |
---|
202 | for (i=0; i < mVertices.size(); i++) { |
---|
203 | mVertices[i] = center + scale*(mVertices[i] - center); |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | bool Polygon3::Valid() const |
---|
208 | { |
---|
209 | if (mVertices.size() < 3) |
---|
210 | return false; |
---|
211 | |
---|
212 | #if 1 |
---|
213 | // check if area exceeds certain size |
---|
214 | if (AREA_LIMIT > GetArea()) |
---|
215 | { |
---|
216 | //Debug << "area too small: " << GetArea() << endl; |
---|
217 | return false; |
---|
218 | } |
---|
219 | #else |
---|
220 | Vector3 vtx = mVertices.back(); |
---|
221 | VertexContainer::const_iterator it, it_end = mVertices.end(); |
---|
222 | |
---|
223 | for (it = mVertices.begin(); it != it_end; ++it) |
---|
224 | { |
---|
225 | if (!(SqrDistance(vtx, *it) > SIDE_TOLERANCE_SQRD)) |
---|
226 | { |
---|
227 | Debug << "Malformed vertices:\n" << *this << endl; |
---|
228 | return false; |
---|
229 | } |
---|
230 | vtx = *it; |
---|
231 | } |
---|
232 | #endif |
---|
233 | return true; |
---|
234 | } |
---|
235 | |
---|
236 | void Polygon3::IncludeInBox(const PolygonContainer &polys, AxisAlignedBox3 &box) |
---|
237 | { |
---|
238 | PolygonContainer::const_iterator it, it_end = polys.end(); |
---|
239 | |
---|
240 | for (it = polys.begin(); it != it_end; ++ it) |
---|
241 | box.Include(*(*it)); |
---|
242 | } |
---|
243 |
|
---|
244 | // int_lineseg returns 1 if the given line segment intersects a 2D
|
---|
245 | // ray travelling in the positive X direction. This is used in the
|
---|
246 | // Jordan curve computation for polygon intersection.
|
---|
247 | inline int
|
---|
248 | int_lineseg(float px,
|
---|
249 | float py,
|
---|
250 | float u1,
|
---|
251 | float v1,
|
---|
252 | float u2,
|
---|
253 | float v2)
|
---|
254 | {
|
---|
255 | float t;
|
---|
256 | float ydiff;
|
---|
257 |
|
---|
258 | u1 -= px; u2 -= px; // translate line
|
---|
259 | v1 -= py; v2 -= py;
|
---|
260 |
|
---|
261 | if ((v1 > 0 && v2 > 0) ||
|
---|
262 | (v1 < 0 && v2 < 0) ||
|
---|
263 | (u1 < 0 && u2 < 0))
|
---|
264 | return 0;
|
---|
265 |
|
---|
266 | if (u1 > 0 && u2 > 0)
|
---|
267 | return 1;
|
---|
268 |
|
---|
269 | ydiff = v2 - v1;
|
---|
270 |
|
---|
271 | if (fabs(ydiff) < Limits::Small)
|
---|
272 | { // denominator near 0
|
---|
273 | if (((fabs(v1) > Limits::Small) || (u1 > 0) || (u2 > 0)))
|
---|
274 | return 0;
|
---|
275 | return 1;
|
---|
276 | }
|
---|
277 |
|
---|
278 | t = -v1 / ydiff; // Compute parameter
|
---|
279 |
|
---|
280 | return (u1 + t * (u2 - u1)) > 0;
|
---|
281 | }
|
---|
282 |
|
---|
283 | int Polygon3::CastRay(const Ray &ray, float &t, const float nearestT)
|
---|
284 | {
|
---|
285 | Plane3 plane = GetSupportingPlane();
|
---|
286 | float dot = DotProd(plane.mNormal, ray.GetDir());
|
---|
287 |
|
---|
288 | // Watch for near-zero denominator
|
---|
289 | // ONLY single sided polygons!!!!!
|
---|
290 | if (dot > -Limits::Small)
|
---|
291 | // if (fabs(dot) < Limits::Small)
|
---|
292 | return Ray::NO_INTERSECTION;
|
---|
293 |
|
---|
294 | t = (-plane.mD - DotProd(plane.mNormal, ray.GetLoc())) / dot;
|
---|
295 |
|
---|
296 | if (t <= Limits::Small)
|
---|
297 | return Ray::INTERSECTION_OUT_OF_LIMITS;
|
---|
298 |
|
---|
299 | if (t >= nearestT) {
|
---|
300 | return Ray::INTERSECTION_OUT_OF_LIMITS; // no intersection was found
|
---|
301 | }
|
---|
302 |
|
---|
303 | int count = 0;
|
---|
304 | float u, v, u1, v1, u2, v2;
|
---|
305 | int i;
|
---|
306 |
|
---|
307 | int paxis = plane.mNormal.DrivingAxis();
|
---|
308 |
|
---|
309 | // Project the intersection point onto the coordinate plane
|
---|
310 | // specified by which.
|
---|
311 | ray.Extrap(t).ExtractVerts(&u, &v, paxis);
|
---|
312 |
|
---|
313 | int size = (int)mVertices.size();
|
---|
314 |
|
---|
315 | mVertices.back().ExtractVerts(&u1, &v1, paxis );
|
---|
316 |
|
---|
317 | if (0 && size <= 4)
|
---|
318 | {
|
---|
319 | // assume a convex face
|
---|
320 | for (i = 0; i < size; i++)
|
---|
321 | {
|
---|
322 | mVertices[i].ExtractVerts(&u2, &v2, paxis);
|
---|
323 |
|
---|
324 | // line u1, v1, u2, v2
|
---|
325 |
|
---|
326 | if ((v2 - v1)*(u1 - u) > (u2 - u1)*(v1 - v))
|
---|
327 | return Ray::NO_INTERSECTION;
|
---|
328 |
|
---|
329 | u1 = u2;
|
---|
330 | v1 = v2;
|
---|
331 | }
|
---|
332 |
|
---|
333 | return Ray::INTERSECTION;
|
---|
334 | }
|
---|
335 |
|
---|
336 | // We're stuck with the Jordan curve computation. Count number
|
---|
337 | // of intersections between the line segments the polygon comprises
|
---|
338 | // with a ray originating at the point of intersection and
|
---|
339 | // travelling in the positive X direction.
|
---|
340 | for (i = 0; i < size; i++)
|
---|
341 | {
|
---|
342 | mVertices[i].ExtractVerts(&u2, &v2, paxis);
|
---|
343 |
|
---|
344 | count += (int_lineseg(u, v, u1, v1, u2, v2) != 0);
|
---|
345 |
|
---|
346 | u1 = u2;
|
---|
347 | v1 = v2;
|
---|
348 | }
|
---|
349 |
|
---|
350 | // We hit polygon if number of intersections is odd.
|
---|
351 | return (count & 1) ? Ray::INTERSECTION : Ray::NO_INTERSECTION;
|
---|
352 | }
|
---|
353 |
|
---|
354 | RayContainer *Polygon3::GetPiercingRays()
|
---|
355 | {
|
---|
356 | if (!mPiercingRays)
|
---|
357 | mPiercingRays = new RayContainer();
|
---|
358 | return mPiercingRays;
|
---|
359 | }
|
---|
360 |
|
---|
361 | void Polygon3::AddPiercingRay(Ray *ray)
|
---|
362 | {
|
---|
363 | if (!mPiercingRays)
|
---|
364 | mPiercingRays = new RayContainer();
|
---|
365 | //if (binary_search(mPiercingRays.begin(), mPiercingRays.end(), ray)) return false;
|
---|
366 |
|
---|
367 | mPiercingRays->push_back(ray);
|
---|
368 | } |
---|