/
maillage.cpp
184 lines (160 loc) · 5.59 KB
/
maillage.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
#include "maillage.h"
#include <iostream>
const QVector<QVector3D>& Maillage::getGeom() const
{
return geom;
}
const QVector<int>& Maillage::getTopo() const
{
return topo;
}
const QVector<QVector3D>& Maillage::getNormales() const
{
return normales;
}
const QVector<int>& Maillage::getNormalIds() const
{
return normalIds;
}
Maillage::Maillage()
{
}
Maillage::Maillage(const Maillage &m):geom(m.geom),topo(m.topo),normales(m.normales),normalIds(m.normalIds)
{
std::cout<<"copie "<<geom.size()<<std::endl;
}
void Maillage::Merge(Maillage figure2)
{
QVector<int> changement;
int temp;
//on parcours la liste des points de la seconde figure
//on vérifie si il y a des points communs entre les deux figures
for(int p1 = 0; p1 < figure2.geom.size(); p1++)
{
// temp = this->ComparePoint(figure2.geom.at(p1));
// si le point n'existe pas deja alors on l'ajoute à la geométrie de la premiere figure
// on stock également le nouvel indice de ce point
// if(temp == -1)
// {
this->geom.append(figure2.geom.at(p1));
temp = this->geom.size()-1;
//}
//std::cout << "l'indice " << p1 << "est maintenant l'indice " << temp << std::endl;
changement.append(temp);
}
//on ajoute a la premiere topo la seconde en mettant a jour les indices
for(int i = 0; i < figure2.topo.size(); i++)
{
this->topo.append(changement.at(figure2.topo.at(i)));
}
}
void Maillage::Ecriture(std::string nom)
{
std::ofstream fichier(nom, std::ios::out | std::ios::trunc);
if(fichier)
{
fichier << "o " << nom << std::endl;
fichier << std::endl;
QVector3D temp;
for (int i = 0; i < geom.size(); ++i)
{
temp = geom.at(i);
fichier << "v " << temp.x() << " " << temp.y() << " " << temp.z() << std::endl;
}
fichier << std::endl;
for (int i = 0; i < normales.size(); ++i)
{
temp = normales.at(i);
fichier << "vn " << temp.x() << " " << temp.y() << " " << temp.z() << std::endl;
}
fichier << std::endl;
for (int i = 0; i < topo.size(); i+=3)
{
fichier << "f " << topo.at(i)+1 << " " << topo.at(i+1)+1 << " " << topo.at(i+2)+1 << std::endl;
}
fichier.close(); // on referme le fichier
}
else
{
std::cerr << "Erreur à l'ouverture !" << std::endl;
}
}
void Maillage::translate(const QVector3D &t, glm::vec3 &min, glm::vec3 &max)
{
min=glm::vec3(std::numeric_limits<float>::infinity(),std::numeric_limits<float>::infinity(),std::numeric_limits<float>::infinity());
max=glm::vec3(std::numeric_limits<float>::lowest(),std::numeric_limits<float>::lowest(),std::numeric_limits<float>::lowest());
for(int i=0;i<geom.size();++i){
geom[i]+=t;
//std::cout<<geom[i].x()<<std::endl;
min.x=std::min(min.x,geom[i].x());
min.y=std::min(min.y,geom[i].y());
min.z=std::min(min.z,geom[i].z());
max.x=std::max(max.x,geom[i].x());
max.y=std::max(max.y,geom[i].y());
max.z=std::max(max.z,geom[i].z());
}
/*min = glm::vec3(min.x+t.x(), min.y+t.y(),min.z+t.z());
max = glm::vec3(max.x+t.x(), max.y+t.y(),max.z+t.z());*/
}
Maillage Maillage::Rotation(const double matrice[3][3])
{
QVector<QVector3D> geom2;
QVector3D temp;
for (int i = 0; i < geom.size(); ++i)
{
temp = geom.at(i);
temp.setX(temp.x() * matrice[0][0] + temp.y() * matrice[0][1] + temp.z() * matrice[0][2]);
temp.setY(temp.x() * matrice[2][0] + temp.y() * matrice[2][1] + temp.z() * matrice[2][2]);
temp.setZ(temp.x() * matrice[1][0] + temp.y() * matrice[1][1] + temp.z() * matrice[1][2]);
geom2.append(temp);
}
Maillage * sphere = new Maillage(geom2, topo, normales);
return *sphere;
}
Maillage::~Maillage()
{
}
void Maillage::geometry(const QVector3D ¢er, const char* obj, glm::vec3 &min, glm::vec3 &max) {
QVector3D minVal(1E100, 1E100, 1E100), maxVal(-1E100, -1E100, -1E100);
FILE* f = fopen(obj, "r");
while (!feof(f)) {
char line[255];
fgets(line, 255, f);
if (line[0]=='v' && line[1]==' ') {
QVector3D vec;
sscanf(line, "v %f %f %f\n", &vec[0], &vec[2], &vec[1]);
vec[2] = -vec[2];
QVector3D p = vec*50. + center;
geom.push_back(p);
maxVal[0] = std::max(maxVal[0], p[0]);
maxVal[1] = std::max(maxVal[1], p[1]);
maxVal[2] = std::max(maxVal[2], p[2]);
minVal[0] = std::min(minVal[0], p[0]);
minVal[1] = std::min(minVal[1], p[1]);
minVal[2] = std::min(minVal[2], p[2]);
min = glm::vec3(minVal[0], minVal[1], minVal[2]);
max = glm::vec3(maxVal[0], maxVal[1], maxVal[2]);
}
if (line[0]=='v' && line[1]=='n') {
QVector3D vec;
sscanf(line, "vn %f %f %f\n", &vec[0], &vec[2], &vec[1]);
vec[2] = -vec[2];
normales.push_back(vec);
}
if (line[0]=='f') {
int i0, i1, i2;
int j0,j1,j2;
int k0,k1,k2;
sscanf(line, "f %u/%u/%u %u/%u/%u %u/%u/%u\n", &i0, &j0, &k0, &i1, &j1, &k1, &i2, &j2, &k2 );
topo.push_back(i0-1);
topo.push_back(i1-1);
topo.push_back(i2-1);
normalIds.push_back(k0-1);
normalIds.push_back(k1-1);
normalIds.push_back(k2-1);
}
}
/*boundingSphere.C = 0.5*(minVal+maxVal);
boundingSphere.R = sqrt((maxVal-minVal).sqrNorm())*0.5;*/
fclose(f);
}