-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkeletor.cpp
199 lines (179 loc) · 4.97 KB
/
Skeletor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "stdafx.h"
#include "Skeletor.h"
#include "Geometry.h"
#include <OgreException.h>
using namespace Ogre;
using namespace std;
SLAV::SLAV()
{
_root = 0;
_size = 0;
}
SLAV::SLAV(const Real inset, const vector<Vector3> &poly)
{
_root = 0;
_size = 0;
size_t i,j,N = poly.size();
assert(N >= 3);
size_t lastI = N-1;
for(i=0; i<N; i++)
{
j = (i+1) % N;
InsetVertex *iv = new InsetVertex();
iv->_pos = poly[i];
iv->_insetTarget = Geometry::calcInsetTarget(poly[lastI], poly[i],
poly[j], inset);
iv->_inset = inset;
iv->_intersectionTested = false;
add(iv);
lastI = i;
}
}
SLAV::SLAV(const vector<Real> insets, const vector<Vector3> &poly)
{
_root = 0;
_size = 0;
size_t i,j,N = poly.size();
assert(N >= 3);
size_t lastI = N-1;
for(i=0; i<N; i++)
{
j = (i+1) % N;
InsetVertex *iv = new InsetVertex();
iv->_pos = poly[i];
iv->_insetTarget = Geometry::calcInsetTarget(poly[lastI], poly[i],
poly[j], insets[lastI], insets[i]);
iv->_inset = insets[i];
iv->_intersectionTested = false;
add(iv);
lastI = i;
}
}
SLAV::~SLAV()
{
for(InsetVertex* iv = _root->_right->_right; iv != _root; iv = iv->_right) delete iv->_left;
delete _root;
}
void SLAV::add(InsetVertex* v)
{
//if(_isnan(v->_pos.x) || _isnan(v->_pos.y) || _isnan(v->_pos.z)
// || _isnan(v->_insetTarget.x) || _isnan(v->_insetTarget.y))
// throw Ogre::Exception(Exception::ERR_INVALIDPARAMS, "Nan", "SLAV::add");
if(_root == 0)
{
_root = v;
_root->_left = _root;
_root->_right = _root;
}
else
{
v->_left = _root->_left;
v->_right = _root;
_root->_left->_right = v;
_root->_left = v;
}
_size++;
}
void SLAV::remove(InsetVertex* v)
{
if(v == _root) _root = v->_right;
v->_left->_right = v->_right;
v->_right->_left = v->_left;
delete v;
_size--;
}
bool Skeletor::processInset(SLAV &sv, vector<Vector3> &poly)
{
//TODO: Use a slav and a queue instead of the list
//queue<InsetVertex*> ivQueue;
//BOOST_FOREACH(InsetVertex& iv, ivList) ivQueue.push(&iv);
while(true)
{
///////////////////////////////////////////////////////////////////
// 1. Intersection test for all inset vectors
///////////////////////////////////////////////////////////////////
// find the earliest intersection
Real intersectionLocation = 1;
Vector2 intersection(Vector2::ZERO);
InsetVertex *iv,*firstOffender=0;
iv = sv.getRoot();
do
{
if(!iv->_intersectionTested)
{
Vector2 ivPos2D(iv->_pos.x, iv->_pos.z);
Vector2 nextIvPos2D(iv->_right->_pos.x, iv->_right->_pos.z);
// check if the pair intersect and store the lowest
Real r;
Vector2 tmpInscn;
if(Geometry::lineIntersect(ivPos2D, iv->_insetTarget,
nextIvPos2D, iv->_right->_insetTarget, tmpInscn, r) &&
r >= 0 && r <= 1)
{
// TODO: tolerance value could be used here
if(r < intersectionLocation)
{
intersectionLocation = r;
firstOffender = iv;
intersection = tmpInscn;
}
}
else
iv->_intersectionTested = true;
}
iv = iv->_right;
}
while(iv != sv.getRoot());
///////////////////////////////////////////////////////////////////
// 2. Process Bisector Intersection
///////////////////////////////////////////////////////////////////
// find the closest intersection
if(intersectionLocation != 1.0)
{
// remove the first offender
InsetVertex *secondOffender=firstOffender->_right;
sv.remove(firstOffender);
// if there is a valid polygon remaining
if(sv.getSize() >= 3)
{
// update the pos and inset of the remaining vertices
iv = sv.getRoot();
do
{
iv->_pos.x = iv->_pos.x + intersectionLocation * (iv->_insetTarget.x - iv->_pos.x);
iv->_pos.z = iv->_pos.z + intersectionLocation * (iv->_insetTarget.y - iv->_pos.z);
iv->_inset = iv->_inset * (1 - intersectionLocation);
iv = iv->_right;
}
while(iv != sv.getRoot());
// update the second offender
secondOffender->_pos.x = intersection.x;
secondOffender->_pos.z = intersection.y;
secondOffender->_insetTarget = Geometry::calcInsetTarget(secondOffender->_left->_pos, secondOffender->_pos,
secondOffender->_right->_pos, secondOffender->_left->_inset, secondOffender->_inset);
secondOffender->_intersectionTested = false;
secondOffender->_left->_intersectionTested = false;
}
else
{
//LogManager::getSingleton().logMessage("Less than 3 vertices after collapse.");
return false;
}
}
else
{
//LogManager::getSingleton().logMessage("Valid.");
poly.reserve(sv.getSize());
//poly.push_back(Vector3(sv.getRoot()->_insetTarget.x, sv.getRoot()->_pos.y, sv.getRoot()->_insetTarget.y));
iv = sv.getRoot();
do
{
poly.push_back(Vector3(iv->_insetTarget.x, iv->_pos.y, iv->_insetTarget.y));
iv = iv->_right;
}
while(iv != sv.getRoot());
break;
}
}
return true;
}