-
Notifications
You must be signed in to change notification settings - Fork 0
/
depliage.cpp
372 lines (324 loc) · 14.9 KB
/
depliage.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "depliage.h"
//#include <QPen>
Depliage::Depliage() {}
Depliage::Depliage(MainWindow* p) {
parent = QPointer<MainWindow>(p);
if (scene3d == nullptr) {
scene3d = new QGraphicsScene(parent);
//scene3d->setStickyFocus(true);
}
if (scene2d == nullptr) {
scene2d = new QGraphicsScene(parent);
//scene2d->setStickyFocus(true);
}
meshModel = new Mesh;
fYaw = 0.0f;
//fPas = 0.15f;
fThetaX = 0.0f;
fThetaY = 0.0f;
fThetaZ = 0.0f;
}
void Depliage::dessineModele()
{
if (!meshModel->faces.empty())
{
// sauver selection
sauveSel.clear();
for (auto&& s: scene2d->selectedItems()) {
int i = s->data(0).toInt();
sauveSel.push_back(i);
}
scene3d->clear();
// Set up rotation matrices
mat4x4 matRotZ, matRotY, matRotX;
// Projection Matrix
mat4x4 matProj;
matProj = matProj.Matrix_MakeProjection(90.0f, 1.0f, 0.1f, 1200.0f);
matRotZ = matRotZ.Matrix_MakeRotationZ(fThetaZ);
matRotY = matRotY.Matrix_MakeRotationY(fThetaY);
matRotX = matRotX.Matrix_MakeRotationX(fThetaX);
mat4x4 matTrans;
matTrans = matTrans.Matrix_MakeTranslation(0.0f, 0.0f, 5.0f);
mat4x4 matWorld;
matWorld = matWorld.Matrix_MakeIdentity(); // Form World Matrix
matWorld = matWorld.Matrix_MultiplyMatrix(matRotZ, matRotY); // Transform by rotation
matWorld = matWorld.Matrix_MultiplyMatrix(matWorld, matRotX);
matWorld = matWorld.Matrix_MultiplyMatrix(matWorld, matTrans); // Transform by translation
vec3d vCamera(0,0,0);
vec3d vLookDir(0,0,0);
// Create "Point At" Matrix for camera
vec3d vUp(0,1,0);
vec3d vTarget(0,0,1);
mat4x4 matCameraRot;
matCameraRot = matCameraRot.Matrix_MakeIdentity();
vLookDir = matCameraRot.Matrix_MultiplyVector(matCameraRot, vTarget);
vTarget = vCamera.Vector_Add(vLookDir);
mat4x4 matCamera;
matCamera = matCamera.Matrix_PointAt(vCamera, vTarget, vUp);
// Make view matrix from camera
mat4x4 matView;
matView = matView.Matrix_QuickInverse(matCamera);
// Store triagles for rastering later
std::vector<facette> vecTrianglesToRaster;
// Draw Triangles
for (auto &tri : meshModel->faces)
{
facette triProjected, triTransformed, triViewed;
// World Matrix Transform
//triTransformed = tri;
triTransformed.p[0] = matWorld.Matrix_MultiplyVector(matWorld, tri.p[0]);
triTransformed.p[1] = matWorld.Matrix_MultiplyVector(matWorld, tri.p[1]);
triTransformed.p[2] = matWorld.Matrix_MultiplyVector(matWorld, tri.p[2]);
triTransformed.col = tri.col;
triTransformed.id = tri.id;
// Calculate triangle Normal
vec3d normal, line1, line2;
// Get lines either side of triangle
line1 = triTransformed.p[1].Vector_Sub(triTransformed.p[0]);
line2 = triTransformed.p[2].Vector_Sub(triTransformed.p[0]);
// Take cross product of lines to get normal to triangle surface
normal = line1.Vector_CrossProduct(line2);
// You normally need to normalise a normal!
normal = normal.Vector_Normalise();
// Get Ray from triangle to camera
//vec3d vCameraRay = triTransformed.p[0].Vector_Sub(vCamera);
// If ray is aligned with normal, then triangle is visible
//if (normal.Vector_DotProduct(vCameraRay) < 0.0f)
//{
// Illumination
vec3d light_direction = { 0.0f, 1.0f, -1.0f };
light_direction = light_direction.Vector_Normalise();
// How "aligned" are light direction and triangle surface normal?
//float dp = std::max(0.1f, Vector_DotProduct(light_direction, normal));
// Convert World Space --> View Space
triViewed = triTransformed;
triViewed.p[0] = matView.Matrix_MultiplyVector(matView, triTransformed.p[0]);
triViewed.p[1] = matView.Matrix_MultiplyVector(matView, triTransformed.p[1]);
triViewed.p[2] = matView.Matrix_MultiplyVector(matView, triTransformed.p[2]);
//triViewed.col = triTransformed.col;
//triViewed.id = triTransformed.id;
// Clip Viewed Triangle against near plane, this could form two additional
// additional triangles.
int nClippedTriangles = 0;
facette clipped[2];
nClippedTriangles = clipped[0].ClipAgainstPlane({ 0.0f, 0.0f, 0.1f }, { 0.0f, 0.0f, 1.0f }, triViewed, clipped[0], clipped[1]);
// We may end up with multiple triangles from the clip, so project as
// required
for (int n = 0; n < nClippedTriangles; n++)
{
// Project triangles from 3D --> 2D
triProjected = triViewed;
triProjected.p[0] = matProj.Matrix_MultiplyVector(matProj, clipped[n].p[0]);
triProjected.p[1] = matProj.Matrix_MultiplyVector(matProj, clipped[n].p[1]);
triProjected.p[2] = matProj.Matrix_MultiplyVector(matProj, clipped[n].p[2]);
//triProjected.col = clipped[n].col;
//triProjected.id = clipped[n].id;
// Scale into view, we moved the normalising into cartesian space
// out of the matrix.vector function from the previous videos, so
// do this manually
triProjected.p[0] = triProjected.p[0].Vector_Div(triProjected.p[0].w);
triProjected.p[1] = triProjected.p[1].Vector_Div(triProjected.p[1].w);
triProjected.p[2] = triProjected.p[2].Vector_Div(triProjected.p[2].w);
// X/Y are inverted so put them back
triProjected.p[0].x *= -1.0f;
triProjected.p[1].x *= -1.0f;
triProjected.p[2].x *= -1.0f;
triProjected.p[0].y *= -1.0f;
triProjected.p[1].y *= -1.0f;
triProjected.p[2].y *= -1.0f;
// Offset verts into visible normalised space
vec3d vOffsetView = { 1,1,0 };
triProjected.p[0] = triProjected.p[0].Vector_Add(vOffsetView);
triProjected.p[1] = triProjected.p[1].Vector_Add(vOffsetView);
triProjected.p[2] = triProjected.p[2].Vector_Add(vOffsetView);
triProjected.p[0].x *= 600.0f;
triProjected.p[0].y *= 600.0f;
triProjected.p[1].x *= 600.0f;
triProjected.p[1].y *= 600.0f;
triProjected.p[2].x *= 600.0f;
triProjected.p[2].y *= 600.0f;
// Store triangle for sorting
vecTrianglesToRaster.push_back(triProjected);
}
//}
}
// Sort triangles from back to front
sort(vecTrianglesToRaster.begin(), vecTrianglesToRaster.end(), [](facette& t1, facette& t2)
{
float z1 = (t1.p[0].z + t1.p[1].z + t1.p[2].z) / 3.0f;
float z2 = (t2.p[0].z + t2.p[1].z + t2.p[2].z) / 3.0f;
return z1 > z2;
});
// Loop through all transformed, viewed, projected, and sorted triangles
for (auto& triToRaster : vecTrianglesToRaster)
{
// Clip triangles against all four screen edges, this could yield
// a bunch of triangles, so create a queue that we traverse to
// ensure we only test new triangles generated against planes
facette clipped[2];
std::list<facette> listTriangles;
// Add initial triangle
listTriangles.push_back(triToRaster);
size_t nNewTriangles = 1;
for (int p = 0; p < 4; p++)
{
int nTrisToAdd = 0;
while (nNewTriangles > 0)
{
// Take triangle from front of queue
facette test = listTriangles.front();
listTriangles.pop_front();
nNewTriangles--;
// Clip it against a plane. We only need to test each
// subsequent plane, against subsequent new triangles
// as all triangles after a plane clip are guaranteed
// to lie on the inside of the plane. I like how this
// comment is almost completely and utterly justified
clipped[0] = triToRaster;
switch (p)
{
case 0: nTrisToAdd = clipped[0].ClipAgainstPlane({ 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, test, clipped[0], clipped[1]); break;
case 1: nTrisToAdd = clipped[0].ClipAgainstPlane({ 0.0f, 1199.0f, 0.0f }, { 0.0f, -1.0f, 0.0f }, test, clipped[0], clipped[1]); break;
case 2: nTrisToAdd = clipped[0].ClipAgainstPlane({ 0.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f }, test, clipped[0], clipped[1]); break;
case 3: nTrisToAdd = clipped[0].ClipAgainstPlane({ 1199.0f, 0.0f, 0.0f }, { -1.0f, 0.0f, 0.0f }, test, clipped[0], clipped[1]); break;
}
// Clipping may yield a variable number of triangles,
// so add these new ones to the back of the queue for
// subsequent clipping against next planes
for (int w = 0; w < nTrisToAdd; w++) {
listTriangles.push_back(clipped[w]);
}
}
nNewTriangles = listTriangles.size();
}
// Draw the transformed, viewed, clipped, projected, sorted, clipped triangles
for (auto& t : listTriangles)
{
QPolygonF poly;
poly << t.p[0].toPointF() << t.p[1].toPointF() << t.p[2].toPointF();
TriangleItem *ti = new TriangleItem(parent, poly, t.id, t.col);
basculeSelectionChanged(false);
for(int s: sauveSel) {
if (s == t.id) {
ti->setSelected(true);
break;
}
}
basculeSelectionChanged(true);
scene3d->addItem(ti);
}
scene3d->update();
}
}
}
void Depliage::creeFaces2d()
{
// liste 2d basique
qreal deltaW = 2;
qreal deltaH = 2;
scene2d->clear();
QPen pPage = QPen(Qt::blue);
pPage.setWidth(3);
QGraphicsRectItem *pageCourante = new QGraphicsRectItem(0,0, 210 ,297);
pageCourante->setPen(pPage);
pageCourante->setFlag(QGraphicsItem::ItemIsMovable);
pages.push_back(pageCourante);
scene2d->addItem(pageCourante);
qreal H = 2;
qreal W = 0;
TriangleItem * gp;
for (auto&& t : meshModel->faces) {
Triangle2d t2 = t.d2ize();
//t.neighbors =
QPolygonF p;
p << t2.a*50 << t2.b*50 << t2.c*50;
p.translate(-p.boundingRect().left(), -p.boundingRect().top());
W = p.boundingRect().width();
H = std::max(H, p.boundingRect().height());
bool ok = false;
if ( ((W + deltaW) < (pageCourante->boundingRect().right() -2))
&& ((H + deltaH) < (pageCourante->boundingRect().bottom() -2)) )
{
p.translate(deltaW, deltaH);
ok = true;
} else {
if ((H + deltaH) < (pageCourante->boundingRect().bottom() -2)) {
// descend dans la page
deltaW = pageCourante->boundingRect().left() +2;
deltaH += H;
if ((H + deltaH) < (pageCourante->boundingRect().bottom() -2))
{
p.translate(deltaW, deltaH);
H = p.boundingRect().height();
ok = true;
}
}
}
if (!ok)
{ // nouvelle page
pageCourante = new QGraphicsRectItem(pages.size()*220, 0, 210 ,297);
pageCourante->setPen(pPage);
pageCourante->setFlag(QGraphicsItem::ItemIsMovable);
pages.push_back(pageCourante);
scene2d->addItem(pageCourante);
// debut sur nouvelle page
deltaH = 2;
deltaW = pageCourante->boundingRect().left() +2;
p.translate(deltaW, deltaH);
H = p.boundingRect().height();
}
gp = new TriangleItem(parent, p, t.id, t.col);
t2d.push_back(gp);
gp->setFlag(QGraphicsItem::ItemIsMovable);
gp->setParentItem(pageCourante);
auto newItemPos = pageCourante->mapFromScene(gp->scenePos());
gp->setPos(newItemPos);
//scene2d->addItem(gp);
deltaW = p.boundingRect().right() + 2;
}
}
void Depliage::trouveVoisinage()
{
int i;
int vi;
bool ok;
int nbFaces = meshModel->faces.size();
std::array<Voisin, 3> tmpV;
for (auto &&ti : meshModel->faces) {
i = ti.id;
for (int j = 0; j < 3; j++) {
vi = 0;
ok = false;
do {
if (vi != i) {
for (int k = 0; (k < 3) && !ok; k++)
if ( (meshModel->faces[vi].pi[k] == ti.pi[next(j)])
&& (meshModel->faces[vi].pi[next(k)] == ti.pi[j]) ) {
tmpV[j] = Voisin(j, vi, next(k));
if (j == 2) {
ti.voisins = tmpV;
//qDebug() << i << ti.pi[0] << ti.pi[1] << ti.pi[2] << ti.voisins[0].nF << ti.voisins[1].nF << ti.voisins[2].nF;
}
ok = true;
}
if (!ok)
vi++;
} else
vi++;
} while ((vi < nbFaces) && !ok);
}
}
}
void Depliage::basculeSelectionChanged(bool etat) {
if (etat) {
parent->connect(scene3d, &QGraphicsScene::selectionChanged, parent, &MainWindow::SelectionDansScene3D);
parent->connect(scene2d, &QGraphicsScene::selectionChanged, parent, &MainWindow::SelectionDansScene2D);
} else {
parent->disconnect(scene3d, &QGraphicsScene::selectionChanged, parent, &MainWindow::SelectionDansScene3D);
parent->disconnect(scene2d, &QGraphicsScene::selectionChanged, parent, &MainWindow::SelectionDansScene2D);
}
}
// next et prev retourne le prochain/précédent dans
// une liste contenant [0,1,2] en bouclant si besoin
int next(int n) { return n == 2 ? 0 : n+1; }
int prev(int n) { return n == 0 ? 2 : n-1; }