source: trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp @ 538

Revision 538, 11.6 KB checked in by mattausch, 18 years ago (diff)
Line 
1#include "Polygon3.h"
2#include "Mesh.h"
3#include "AxisAlignedBox3.h"
4#include "Ray.h"
5#include "Triangle3.h"
6
7Polygon3::Polygon3():
8mMaterial(NULL), mParent(NULL), mPiercingRays(0)
9{}
10
11Polygon3::Polygon3(const VertexContainer &vertices):
12mVertices(vertices), mMaterial(NULL), mParent(NULL), mPiercingRays(0)
13{}
14
15Polygon3::Polygon3(MeshInstance *parent):
16mMaterial(NULL), mParent(parent), mPiercingRays(0)
17{}
18
19Polygon3::Polygon3(Face *face, Mesh *parentMesh):
20mMaterial(NULL), mParent(NULL), mPiercingRays(0)
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
30Plane3 Polygon3::GetSupportingPlane() const
31{
32        return Plane3(mVertices[0], mVertices[1], mVertices[2]);
33}
34
35Vector3 Polygon3::GetNormal() const
36{
37    return Normalize(CrossProd(mVertices[2] - mVertices[1],
38                                                           mVertices[0] - mVertices[1]));
39}
40
41void Polygon3::Split(const Plane3 &partition,
42                                         Polygon3 &front,
43                                         Polygon3 &back,
44                                         const float epsilon)
45{
46        Vector3 ptA = mVertices.back();
47       
48        int sideA = partition.Side(ptA, epsilon);
49       
50        VertexContainer::const_iterator it;
51       
52        Vector3 lastSplit;
53
54        bool foundSplit = false;
55
56        //-- find line - plane intersections
57        for (it = mVertices.begin(); it != mVertices.end(); ++ it)
58        {
59                Vector3 ptB = *it;
60                int sideB = partition.Side(ptB, epsilon);
61       
62                // vertices on different sides => split
63            if (sideB > 0)
64                {
65                        if (sideA < 0)
66                        {
67                                //-- plane - line intersection
68                                Vector3 splitPt = partition.FindIntersection(ptA, ptB);
69                       
70                                // test if split point not too close to previous split point
71                                if (!foundSplit || (!EpsilonEqualV3(splitPt, lastSplit, epsilon)))
72                                {
73                                        // add vertex to both polygons
74                                        front.mVertices.push_back(splitPt);
75                                        back.mVertices.push_back(splitPt);
76                                       
77                                        lastSplit = splitPt;
78                                        foundSplit = true;
79                                }
80                        }
81                        front.mVertices.push_back(ptB);
82                }
83                else if (sideB < 0)
84                {
85                        if (sideA > 0)
86                        {
87                                //-- plane - line intersection
88                                Vector3 splitPt = partition.FindIntersection(ptA, ptB);
89                                // test if split point not too close to other split point
90                                        // test if split point not too close to previous split point
91                                if (!foundSplit || (!EpsilonEqualV3(splitPt, lastSplit, epsilon)))
92                                {
93                                        // add vertex to both polygons
94                                        front.mVertices.push_back(splitPt);
95                                        back.mVertices.push_back(splitPt);
96
97                                        lastSplit = splitPt;
98                                        foundSplit = true;
99                                }       
100                        }
101                        back.mVertices.push_back(ptB);
102                }
103                else
104                {
105                        // vertex on plane => add vertex to both polygons
106                        front.mVertices.push_back(ptB);
107                        back.mVertices.push_back(ptB);
108                }
109       
110                ptA = ptB;
111                sideA = sideB;
112        }
113}
114
115float Polygon3::GetArea() const
116{
117        Vector3 v = CrossProd(mVertices.back(), mVertices.front());
118   
119    for (int i=0; i < mVertices.size() - 1; ++i)
120                v += CrossProd(mVertices[i], mVertices[i+1]);
121   
122    //Debug << "area2: " << 0.5f * fabs(DotProd(GetNormal(), v)) << endl; 
123        return 0.5f * fabs(DotProd(GetNormal(), v));
124}
125
126int Polygon3::Side(const Plane3 &plane,
127                                   const float epsilon) const
128{
129        int classification = ClassifyPlane(plane, epsilon);
130       
131        if (classification == BACK_SIDE)
132                return -1;
133        else if (classification == FRONT_SIDE)
134                return 1;
135        // plane splits polygon
136        return 0;
137}
138
139int Polygon3::ClassifyPlane(const Plane3 &plane,
140                                                        const float epsilon) const
141{
142        VertexContainer::const_iterator it;
143
144        bool onFrontSide = false;
145        bool onBackSide = false;
146       
147        int count = 0;
148        // find possible line-plane intersections
149        for (it = mVertices.begin(); it != mVertices.end(); ++ it)
150        {
151                const int side = plane.Side(*it, epsilon);
152               
153                if (side > 0)
154                        onFrontSide = true;
155                else if (side < 0)
156                        onBackSide = true;
157               
158                //TODO: check if split goes through vertex
159                if (onFrontSide && onBackSide) // split
160                {
161                        return SPLIT;
162                }
163                // 3 vertices enough to decide coincident
164                else if (((++ count) >= 3) && !onFrontSide && !onBackSide)
165                {   
166                        return COINCIDENT;
167                }
168        }
169
170        if (onBackSide)
171        {
172                return BACK_SIDE;
173        }
174        else if (onFrontSide)
175        {
176                return FRONT_SIDE;
177        }
178       
179        return COINCIDENT; // plane and polygon are coincident
180}
181
182/*int Polygon3::ClassifyPlane(const Plane3 &plane) const
183{
184        return ClassifyPlane(plane, Limits::Small);
185}*/
186
187
188Vector3
189Polygon3::Center() const
190{
191        int i;
192        Vector3 sum = mVertices[0];
193        for (i=1; i < mVertices.size(); i++)
194                sum += mVertices[i];
195       
196        return sum/(float)i;
197}
198
199
200void
201Polygon3::Scale(const float scale)
202{
203        int i;
204        Vector3 center = Center();
205        for (i=0; i < mVertices.size(); i++) {
206                mVertices[i] = center + scale*(mVertices[i] - center);
207        }
208}
209
210bool Polygon3::Valid(const float epsilon) const
211{
212        if (mVertices.size() < 3)
213                return false;
214
215#if 1
216        // check if area exceeds certain size
217        if (AREA_LIMIT > GetArea())
218        {
219                //Debug << "area too small: " << GetArea() << endl;
220                return false;
221        }
222#else
223        Vector3 vtx = mVertices.back();
224        VertexContainer::const_iterator it, it_end = mVertices.end();
225
226        for (it = mVertices.begin(); it != it_end; ++it)
227        {
228                if (!(EpsilonEqual(vtx, *it)))
229                {
230                        //Debug << "Malformed vertices:\n" << *this << endl;
231                        return false;
232                }
233                vtx = *it;
234        }
235#endif 
236        return true;
237}
238
239void Polygon3::IncludeInBox(const PolygonContainer &polys, AxisAlignedBox3 &box)
240{
241        PolygonContainer::const_iterator it, it_end = polys.end();
242
243        for (it = polys.begin(); it != it_end; ++ it)
244                box.Include(*(*it));
245}
246
247// int_lineseg returns 1 if the given line segment intersects a 2D
248// ray travelling in the positive X direction.  This is used in the
249// Jordan curve computation for polygon intersection.
250inline int
251int_lineseg(float px,
252            float py,
253            float u1,
254            float v1,
255            float u2,
256            float v2)
257{
258        float t;
259        float ydiff;
260
261        u1 -= px; u2 -= px;       // translate line
262        v1 -= py; v2 -= py;
263
264        if ((v1 > 0 && v2 > 0) ||
265                (v1 < 0 && v2 < 0) ||
266                (u1 < 0 && u2 < 0))
267        return 0;
268
269        if (u1 > 0 && u2 > 0)
270        return 1;
271
272        ydiff = v2 - v1;
273
274        if (fabs(ydiff) < Limits::Small)
275        {         // denominator near 0
276                if (((fabs(v1) > Limits::Small) || (u1 > 0) || (u2 > 0)))
277                        return 0;
278                return 1;
279        }
280
281        t = -v1 / ydiff;                  // Compute parameter
282
283        return (u1 + t * (u2 - u1)) > 0;
284}
285
286int Polygon3::CastRay(const Ray &ray, float &t, const float nearestT)
287{
288        Plane3 plane = GetSupportingPlane();
289        float dot = DotProd(plane.mNormal, ray.GetDir());
290
291        // Watch for near-zero denominator
292        // ONLY single sided polygons!!!!!
293        if (dot > -Limits::Small)
294        //  if (fabs(dot) < Limits::Small)
295        return Ray::NO_INTERSECTION;
296
297        t = (-plane.mD - DotProd(plane.mNormal, ray.GetLoc())) / dot;
298
299        if (t <= Limits::Small)
300        return Ray::INTERSECTION_OUT_OF_LIMITS;
301
302        if (t >= nearestT) {
303        return Ray::INTERSECTION_OUT_OF_LIMITS; // no intersection was found
304        }
305
306        int count = 0;
307        float u, v, u1, v1, u2, v2;
308        int i;
309
310        int paxis = plane.mNormal.DrivingAxis();
311
312        // Project the intersection point onto the coordinate plane
313        // specified by which.
314        ray.Extrap(t).ExtractVerts(&u, &v, paxis);
315
316        int size = (int)mVertices.size();
317
318        mVertices.back().ExtractVerts(&u1, &v1, paxis );
319
320        if (0 && size <= 4)
321        {
322                // assume a convex face
323                for (i = 0; i < size; i++)
324                {
325                mVertices[i].ExtractVerts(&u2, &v2, paxis);
326           
327                        // line u1, v1, u2, v2
328           
329                        if ((v2 - v1)*(u1 - u) > (u2 - u1)*(v1 - v))
330                                return Ray::NO_INTERSECTION;
331
332                        u1 = u2;
333                        v1 = v2;
334        }
335
336        return Ray::INTERSECTION;
337        }
338
339        // We're stuck with the Jordan curve computation.  Count number
340        // of intersections between the line segments the polygon comprises
341        // with a ray originating at the point of intersection and
342        // travelling in the positive X direction.
343        for (i = 0; i < size; i++)
344        {
345                mVertices[i].ExtractVerts(&u2, &v2, paxis);
346
347                count += (int_lineseg(u, v, u1, v1, u2, v2) != 0);
348
349                u1 = u2;
350                v1 = v2;
351        }
352
353        // We hit polygon if number of intersections is odd.
354        return (count & 1) ? Ray::INTERSECTION : Ray::NO_INTERSECTION;
355}
356
357void Polygon3::InheritRays(Polygon3 &front_piece,
358                                               Polygon3 &back_piece) const
359{
360        if (mPiercingRays.empty())
361                return;
362
363        RayContainer::const_iterator it,
364                it_end = mPiercingRays.end();
365
366        for (it = mPiercingRays.begin(); it != it_end; ++ it)
367        {
368                switch((*it)->GetId())
369                {
370                case Ray::BACK:
371                        back_piece.mPiercingRays.push_back(*it);
372                        break;
373                case Ray::FRONT:
374                        front_piece.mPiercingRays.push_back(*it);
375                        break;
376                case Ray::FRONT_BACK:
377                        back_piece.mPiercingRays.push_back(*it);
378                        break;
379                case Ray::BACK_FRONT:
380                        front_piece.mPiercingRays.push_back(*it);
381                        break;
382                default:
383                        break;
384                }
385        }
386}
387
388int Polygon3::ClassifyPlane(const PolygonContainer &polys,
389                                                        const Plane3 &plane,
390                                                        const float epsilon)
391{
392        PolygonContainer::const_iterator it;
393
394        bool onFrontSide = false;
395        bool onBackSide = false;
396       
397        // find intersections
398        for (it = polys.begin(); it != polys.end(); ++ it)
399        {
400        const int cf = (*it)->ClassifyPlane(plane, epsilon);
401               
402        if (cf == FRONT_SIDE)
403                        onFrontSide = true;
404                else if (cf == BACK_SIDE)
405                        onBackSide = true;
406               
407                if ((cf == SPLIT) || (cf == COINCIDENT) || (onFrontSide && onBackSide))
408                {
409                        return SPLIT;
410                }
411        }
412
413        if (onBackSide)
414        {
415                return BACK_SIDE;
416        }
417        else if (onFrontSide)
418        {
419                return FRONT_SIDE;
420        }
421       
422        return SPLIT;
423}
424
425int Polygon3::ParentObjectsSize(const PolygonContainer &polys)
426{
427        int count = 0;
428
429        PolygonContainer::const_iterator it, it_end = polys.end();
430
431        MeshInstance::NewMail();
432
433        for (it = polys.begin(); it != it_end; ++ it)
434        {
435                if ((*it)->mParent && !(*it)->mParent->Mailed())
436                {
437                        ++ count;
438                        (*it)->mParent->Mail();
439                }
440        }
441        return count;
442}
443
444float Polygon3::GetArea(const PolygonContainer &cell)
445{
446        float area = 0;
447        PolygonContainer::const_iterator pit;
448
449        for (pit = cell.begin(); pit != cell.end(); ++ pit)
450        {
451                if ((*pit)->mVertices.size() < 3)
452                        Debug << "ERROR" << endl;
453
454                area += (*pit)->GetArea();
455        }
456
457        return area;
458}
459
460
461Polygon3 *Polygon3::CreateReversePolygon() const
462{
463        Polygon3 *revPoly = new Polygon3();
464
465        VertexContainer::const_reverse_iterator rit,
466                        rit_end = mVertices.rend();
467
468        for(rit = mVertices.rbegin(); rit != rit_end; ++ rit)
469                revPoly->mVertices.push_back(*rit);
470
471        return revPoly;
472}
473
474
475void Polygon3::Triangulate(vector<Triangle3> &triangles)
476{
477        int i = 1;
478        int j = 0;
479        int k = (int)mVertices.size() - 1;
480        int count = 0;
481
482        while (i < k)
483        {
484                triangles.push_back(Triangle3(mVertices[i], mVertices[j], mVertices[k]));
485
486                if ((count ++) % 2)
487                        j = i ++;
488                else
489                        j = k --;
490        }
491}
492
493
494void Polygon3::Triangulate(VertexIndexContainer &indices)
495{
496        int i = 1;
497        int j = 0;
498        int k = (int)mVertices.size() - 1;
499
500        int count = 0;
501
502        while (i < k)
503        {
504                indices.push_back(k);
505                indices.push_back(j);
506                indices.push_back(i);
507
508                if ((count ++) % 2)
509                        j = i ++;
510                else
511                        j = k --;
512        }
513}
514
515
516void Polygon3::AddToMesh(Mesh &mesh)
517{
518        const int n = (int)mesh.mVertices.size();
519
520    //-- add the vertices
521    VertexContainer::const_iterator vit, vit_end = mVertices.end();
522        for (vit = mVertices.begin(); vit != vit_end; ++ vit)
523        {
524                mesh.mVertices.push_back(*vit);
525        }
526
527        // one quad => no triangulation necessary
528        if ((int)mVertices.size() == 4)
529        {
530                mesh.AddFace(new Face(n, n + 1, n + 2, n + 3));
531        }
532        else
533        {
534                VertexIndexContainer indices;
535                Triangulate(indices);
536
537                // add indices of triangle strip
538                for (int i = 0; i < (int)indices.size(); i += 3)
539                {
540                        Face *face = new Face(n + indices[i],
541                                                                  n + indices[i + 1],
542                                                                  n + indices[i + 2]);
543                        mesh.AddFace(face);
544                }
545        }
546}
Note: See TracBrowser for help on using the repository browser.