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