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

Revision 404, 10.2 KB checked in by mattausch, 19 years ago (diff)

fixed some bugs in pvs criterium, take other ray plane candidates

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