1 | #include "Polygon3.h"
|
---|
2 | #include "Mesh.h"
|
---|
3 | |
---|
4 | Polygon3::Polygon3() |
---|
5 | {} |
---|
6 | |
---|
7 | Polygon3::Polygon3(const VertexContainer &vertices): mVertices(vertices) |
---|
8 | {} |
---|
9 | |
---|
10 | Polygon3::Polygon3(Face *face, Mesh *parent) |
---|
11 | { |
---|
12 | VertexIndexContainer::const_iterator it; |
---|
13 | |
---|
14 | for (it = face->mVertexIndices.begin(); it != face->mVertexIndices.end(); ++ it) |
---|
15 | { |
---|
16 | mVertices.push_back(parent->mVertices[*it]); |
---|
17 | } |
---|
18 | } |
---|
19 | |
---|
20 | Plane3 Polygon3::GetSupportingPlane() |
---|
21 | { |
---|
22 | return Plane3(mVertices[0], mVertices[1], mVertices[2]); |
---|
23 | } |
---|
24 | |
---|
25 | void Polygon3::DeletePolygons(PolygonQueue *polys) |
---|
26 | { |
---|
27 | // don't need to store polygon information = delete polygons
|
---|
28 | while(!polys->empty())
|
---|
29 | {
|
---|
30 | Polygon3 *poly = polys->front();
|
---|
31 | polys->pop();
|
---|
32 | DEL_PTR(poly);
|
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | void Polygon3::Split(Plane3 *partition, Polygon3 *front, Polygon3 *back, int &splits) |
---|
37 | { |
---|
38 | splits = 0; |
---|
39 | Vector3 ptA = mVertices[mVertices.size() - 1];; |
---|
40 | |
---|
41 | int sideA = partition->Side(ptA); |
---|
42 | |
---|
43 | VertexContainer::const_iterator it; |
---|
44 | |
---|
45 | // find line - plane intersections |
---|
46 | for (it = mVertices.begin(); it != mVertices.end(); ++ it) |
---|
47 | { |
---|
48 | Vector3 ptB = (*it); |
---|
49 | int sideB = partition->Side(ptB); |
---|
50 | |
---|
51 | // vertices on different sides => split |
---|
52 | if ((sideA != 0) && (sideB != 0) && (sideA != sideB)) |
---|
53 | { |
---|
54 | Vector3 v = ptB - ptA; // line from A to B |
---|
55 | float dv = DotProd(partition->mNormal, v); |
---|
56 | float t = 0; |
---|
57 | |
---|
58 | if (dv) |
---|
59 | { |
---|
60 | t = - partition->Distance(ptA) / dv; |
---|
61 | } |
---|
62 | |
---|
63 | ++ splits; |
---|
64 | } |
---|
65 | if (sideB >= 0) |
---|
66 | { |
---|
67 | back->mVertices.push_back(ptB); |
---|
68 | } |
---|
69 | else if (sideB <= 0) |
---|
70 | { |
---|
71 | front->mVertices.push_back(ptB); |
---|
72 | } |
---|
73 | |
---|
74 | ptA = ptB; |
---|
75 | sideA = sideB; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | int Polygon3::Side(Plane3 *plane) |
---|
80 | { |
---|
81 | VertexContainer::const_iterator it; |
---|
82 | |
---|
83 | bool onFrontSide = false; |
---|
84 | bool onBackSide = false; |
---|
85 | |
---|
86 | // find line - plane intersections |
---|
87 | for (it = mVertices.begin(); it != mVertices.end(); ++ it) |
---|
88 | { |
---|
89 | int side = plane->Side(*it); |
---|
90 | |
---|
91 | if (side > 0) |
---|
92 | { |
---|
93 | onFrontSide = true; |
---|
94 | } |
---|
95 | else if (side < 0) |
---|
96 | { |
---|
97 | onBackSide = true; |
---|
98 | } |
---|
99 | |
---|
100 | if (onFrontSide && onBackSide) // split |
---|
101 | { |
---|
102 | return SPLIT; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | if (onBackSide) |
---|
107 | { |
---|
108 | return BACK_SIDE; |
---|
109 | } |
---|
110 | else if (onFrontSide) |
---|
111 | { |
---|
112 | return FRONT_SIDE; |
---|
113 | } |
---|
114 | |
---|
115 | return COINCIDENT; |
---|
116 | } |
---|
117 |
|
---|