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

Revision 264, 3.3 KB checked in by mattausch, 19 years ago (diff)

debugged bsp stuff

Line 
1#include "Polygon3.h"
2#include "Mesh.h"
3
4#define SIDE_TOLERANCE 0.002f // TODO: Test different values
5
6Polygon3::Polygon3(): mMaterial(NULL)
7{}
8
9Polygon3::Polygon3(const VertexContainer &vertices): mVertices(vertices), mMaterial(NULL)
10{}
11
12Polygon3::Polygon3(Face *face, Mesh *parent)
13{       
14        VertexIndexContainer::iterator it = face->mVertexIndices.begin();
15        for (; it != face->mVertexIndices.end();  ++it)
16        {
17                mVertices.push_back(parent->mVertices[*it]);
18                mMaterial = parent->mMaterial;
19               
20                //Debug << parent->mVertices[*it] << endl;
21        }
22}
23
24Plane3 Polygon3::GetSupportingPlane() const
25{
26        Vector3 v1 = mVertices[0] - mVertices[1];
27        Vector3 v2 = mVertices[2] - mVertices[1];
28#ifdef _DEBUG
29        Debug << "plane spanned by " <<  v1 << ", " << v2  << endl;
30#endif
31        return Plane3(mVertices[0], mVertices[1], mVertices[2]);
32}
33
34void Polygon3::Split(Plane3 *partition, Polygon3 *front, Polygon3 *back, int &splits)
35{
36        splits = 0;
37        Vector3 ptA = mVertices[mVertices.size() - 1];
38       
39        int sideA = partition->Side(ptA, SIDE_TOLERANCE);
40       
41        VertexContainer::const_iterator it;
42
43        // find line - plane intersections
44        for (it = mVertices.begin(); it != mVertices.end(); ++ it)
45        {
46                Vector3 ptB = (*it);
47                int sideB = partition->Side(ptB, SIDE_TOLERANCE);
48       
49                // vertices on different sides => split
50            if (sideB > 0)
51                {
52                        if (sideA < 0)
53                        {
54                                //-- plane - line intersection
55                                Vector3 splitPt = partition->FindIntersection(ptA, ptB);
56                       
57                                // add vertex to both polygons
58                                front->mVertices.push_back(splitPt);
59                                back->mVertices.push_back(splitPt);
60                       
61                                ++ splits;
62                        }
63                        front->mVertices.push_back(ptB);
64                }
65                else if (sideB < 0)
66                {
67                        if (sideA > 0)
68                        {
69                                //-- plane - line intersection
70                                Vector3 splitPt = partition->FindIntersection(ptA, ptB);
71                       
72                                // add vertex to both polygons
73                                front->mVertices.push_back(splitPt);
74                                back->mVertices.push_back(splitPt);
75
76                                ++ splits;
77                        }
78                        back->mVertices.push_back(ptB);
79                }
80                else
81                {
82                        // vertex on plane => add vertex to both polygons
83                        front->mVertices.push_back(ptB);
84                        back->mVertices.push_back(ptB);
85                }
86       
87                ptA = ptB;
88                sideA = sideB;
89        }
90}
91
92int Polygon3::Side(const Plane3 &plane) const
93{
94        int classification = ClassifyPlane(plane);
95       
96        if (classification == BACK_SIDE)
97                return -1;
98        else if (classification == FRONT_SIDE)
99                return 1;
100
101        return 0;
102}
103
104int Polygon3::ClassifyPlane(const Plane3 &plane) const
105{
106        VertexContainer::const_iterator it;
107
108        bool onFrontSide = false;
109        bool onBackSide = false;
110
111        // find possible line-plane intersections
112        for (it = mVertices.begin(); it != mVertices.end(); ++ it)
113        {
114                int side = plane.Side(*it, SIDE_TOLERANCE);
115               
116                if (side > 0)
117                {
118                        onFrontSide = true;
119                }
120                else if (side < 0)
121                {
122                        onBackSide = true;
123                }
124                //TODO: check if split goes through vertex
125                if (onFrontSide && onBackSide) // split
126                {
127                        return SPLIT;
128                }
129        }
130
131        if (onBackSide)
132        {
133                return BACK_SIDE;
134        }
135        else if (onFrontSide)
136        {
137                return FRONT_SIDE;
138        }
139
140        // plane and polygon are coincident
141        return COINCIDENT;
142}
143
144
145Vector3
146Polygon3::Center() const
147{
148        int i;
149        Vector3 sum = mVertices[0];
150        for (i=1; i < mVertices.size(); i++)
151                sum += mVertices[i];
152       
153        return sum/i;
154}
155
156
157void
158Polygon3::Scale(const float scale)
159{
160        int i;
161        Vector3 center = Center();
162        for (i=0; i < mVertices.size(); i++) {
163                mVertices[i] = center + scale*(mVertices[i] - center);
164        }
165}
Note: See TracBrowser for help on using the repository browser.