-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeshbuilder.cpp
253 lines (211 loc) · 6.78 KB
/
meshbuilder.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
#include "meshbuilder.h"
#include <QFile>
#include <QTextStream>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
#ifndef fmin
#define fmin(a,b) a<b?a:b
#endif
#define PI 3.14159265358979323846
using namespace std;
MeshBuilder::MeshBuilder()
{
}
void MeshBuilder::saveMesh(const string &nom, const Mesh &mesh) const
{
ofstream out;
out.open(nom);
out << fixed << setprecision(2);
out << "o "<<mesh.getNom().toStdString()<<"\n";
for(QList<Vector3D>::const_iterator itVect = mesh.getGeom().begin(); itVect != mesh.getGeom().end(); ++itVect) {
out << "v " << itVect->x() << " " << itVect->y() << " " << itVect->z() << "\n";
}
out << "\n";
for(QList<Vector3D>::const_iterator itNorm = mesh.getNorm().begin(); itNorm != mesh.getNorm().end(); ++itNorm) {
out << "vn " << itNorm->x() << " " << itNorm->y() << " " << itNorm->z() << "\n";
}
out << "\n";
out << "vt 0.0 0.0 \n\n";
for(QList<int>::const_iterator it = mesh.getTopo().begin(); it != mesh.getTopo().end();) {
out << "f ";
for(int i = 0; i < 3; i++) {
int face = (*(it++)) + 1;
int texture = (*(it++)) + 1;
int normale = (*(it++)) + 1;
out << face << "/" << texture << "/" << normale << " ";
}
out << "\n";
}
out.close();
}
// Ne prend pas les textures en compte
Mesh MeshBuilder::loadMesh(const QString &nom) const
{
QList<Vector3D> geoms;
QList<Vector3D> norms;
QList<int> topos;
QFile file(nom);
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(&file);
QString read;
while(true) {
read = in.readLine();
QStringList l = read.split(" ");
if(l.at(0) == "v") {
Vector3D tmp(l.at(1).toDouble(), l.at(2).toDouble(), l.at(3).toDouble());
geoms.append(tmp);
}
if(l.at(0) == "vn") {
Vector3D tmp(l.at(1).toDouble(), l.at(2).toDouble(), l.at(3).toDouble());
norms.append(tmp);
}
if(l.at(0) == "f") {
for(int i = 1; i < 4; ++i){
QStringList l2 = l.at(i).split("/");
for(int j = 0; j < l2.length(); ++j) {
topos.append(l2.at(j).toInt()-1);
}
}
}
if(in.atEnd()) {
break;
}
}
Mesh res(geoms, topos, norms, nom);
std::cout << nom.toStdString() << " chargé" << std::endl;
return res;
}
MeshBuilder::~MeshBuilder()
{
}
Mesh MeshBuilder::generationPolyanglesRelies(const QVector<PolyangleHauteur> &polyangles) const
{
QList<Vector3D> geom;
QList<int> topo;
QList<Vector3D> normales;
int nbPoly = polyangles.size();
int nbPoints = polyangles[0].getPolyangle().getLesPoints().size();
// Géométrie
for (int i = 0; i < nbPoints; i++)
{
for (int j = 0; j < nbPoly; j++)
{
geom << Vector3D(polyangles[j].getPolyangle().getLesPoints()[i], polyangles[j].getHauteur());
}
}
int cptNormales = 0;
// Topologie
for (int i = 0; i < nbPoints*nbPoly; i++)
{
if ((i+1)%nbPoly != 0)
{
int p1, p2, p3, p4;
p1 = i;
p2 = (i + nbPoly) % (nbPoly*nbPoints);
p3 = i + 1;
p4 = p2 + 1;
Vector3D n = (geom[p3]-geom[p2])^(geom[p1]-geom[p2]);
n.normalize();
normales << n;
// indice point, indice texture, indice normale
// Premier triangle
topo << p1 << 0 << cptNormales
<< p2 << 0 << cptNormales
<< p3 << 0 << cptNormales;
// Second triangle
topo << p3 << 0 << cptNormales
<< p2 << 0 << cptNormales
<< p4 << 0 << cptNormales;
cptNormales++;
}
}
// Topologie plafond
for (int i = 1; i < nbPoints-1; i++)
{
int p1, p2, p3;
p1 = nbPoly-1;
p2 = nbPoly-1 + i*nbPoly;
p3 = nbPoly-1 + (i+1)*nbPoly;
Vector3D n(0,0,1);
normales << n;
topo << p1 << 0 << cptNormales
<< p2 << 0 << cptNormales
<< p3 << 0 << cptNormales;
cptNormales++;
}
return Mesh(geom, topo, normales, "PolyanglesRelies");
}
Mesh MeshBuilder::generationCone(const PolyangleHauteur &poly, const float hauteurCone)
{
QList<Vector3D> geom;
QList<int> topo;
QList<Vector3D> normales;
int nbPoints = poly.getPolyangle().getLesPoints().size();
// Géométrie
Vector3D e(0,0,0);
for (int i = 0; i < nbPoints; i++)
{
e += Vector3D(poly.getPolyangle().getLesPoints()[i], poly.getHauteur() + hauteurCone);
geom << Vector3D(poly.getPolyangle().getLesPoints()[i], poly.getHauteur());
}
e /= nbPoints;
geom << e;
// Topologie
for (int i = 0; i < nbPoints; i++)
{
int p1, p2, p3;
p1 = nbPoints;
p2 = i;
p3 = (i+1)%nbPoints;
Vector3D n = (geom[p1]-geom[p3])^(geom[p2]-geom[p3]);
n.normalize();
normales.push_back(n);
// indice point, indice texture, indice normale
topo << p1 << 0 << i
<< p2 << 0 << i
<< p3 << 0 << i;
}
return Mesh(geom, topo, normales, "cone");
}
Mesh MeshBuilder::generationCone(const Vector3D ¢re, const float hauteurCone, const int definition, const float rayon)
{
float pi = 3.14159265358979323846;
QList<Vector3D> geom;
QList<int> topo;
QList<Vector3D> normales;
Vector3D pointe = Vector3D(centre.x(), centre.y(), centre.z() + hauteurCone);
for (int i=0; i < definition; i++)
{
float x = cos(2*pi*i/definition)*rayon;
float y = sin(2*pi*i/definition)*rayon;
geom << centre + Vector3D(x, y, 0);
}
geom << pointe;
for (int i = 0; i < definition; i++)
{
int p1, p2, p3;
p1 = definition;
p2 = i;
p3 = (i+1)%definition;
Vector3D n = (geom[p1]-geom[p3])^(geom[p2]-geom[p3]);
n.normalize();
normales.push_back(n);
// indice point, indice texture, indice normale
topo << p1 << 0 << i
<< p2 << 0 << i
<< p3 << 0 << i;
}
return Mesh(geom, topo, normales, "cone");
}
Mesh MeshBuilder::generationEtage(const Batiment *etage) const
{
Polyangle baseShrinked = etage->getBase().shrink(etage->getBase().plusPetitCote()/20);
QVector<PolyangleHauteur> polyangles;
polyangles << PolyangleHauteur(baseShrinked, etage->getHauteur())
<< PolyangleHauteur(baseShrinked, etage->getHauteur() + etage->getHauteurEtage()/10)
<< PolyangleHauteur(etage->getBase(), etage->getHauteur() + etage->getHauteurEtage()/10)
<< PolyangleHauteur(etage->getBase(), etage->getHauteur() + etage->getHauteurEtage());
return generationPolyanglesRelies(polyangles);
}