-
Notifications
You must be signed in to change notification settings - Fork 0
/
objrevolucion.cc
executable file
·204 lines (177 loc) · 7.52 KB
/
objrevolucion.cc
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
#include "aux.h"
#include "objrevolucion.h"
#include <iostream>
// *****************************************************************************
//
// Clase ObjRevolucion (práctica 2)
//
// *****************************************************************************
// *****************************************************************************
// objeto de revolución obtenido a partir de un perfil (en un PLY)
ObjRevolucion::ObjRevolucion() {}
ObjRevolucion::ObjRevolucion(const std::string & archivo, int num_instancias, bool tapa_sup, bool tapa_inf) {
// completar ......(práctica 2)
ply::read_vertices( archivo, this->v);
perfil_asociado = formatoPerfil(this->v) ;
crearMalla(perfil_asociado,num_instancias,tapa_sup,tapa_inf);
for(unsigned i = 0 ; i < v.size() ; i++){
colores_solido.push_back({0,0,0.5}) ;
colores_puntos.push_back({0.49,0.49,0}) ;
colores_linea.push_back({0,0,0}) ;
}
color_impar_ajedrez = {0,0,0.5} ;
color_par_ajedrez = {0,0,0} ;
MaterialAmarillo * ma = new MaterialAmarillo() ;
material = ma ;
}
// *****************************************************************************
// objeto de revolución obtenido a partir de un perfil (en un vector de puntos)
ObjRevolucion::ObjRevolucion(std::vector<Tupla3f> archivo, int num_instancias, bool tapa_sup, bool tapa_inf) {
crearMalla(archivo,num_instancias,tapa_sup,tapa_inf) ;
}
void ObjRevolucion::crearMalla(std::vector<Tupla3f> perfil_original, int num_instancias, bool tapa_sup, bool tapa_inf) {
perfil_asociado = perfil_original ;
instancias = num_instancias ;
std::vector<Tupla3f> despolarizado ;
for (int i = 0 ; i < perfil_original.size() - 2 ; i++){
despolarizado.push_back(perfil_original[i]) ;
}
detectarTapas(perfil_original) ;
crearTablaVertices(despolarizado,num_instancias) ;
crearTablaTriangulos(despolarizado,num_instancias) ;
crearTablaCoordenadas(despolarizado,num_instancias) ;
detectarTapas(perfil_original) ;
if (extremos[0] && tapa_inf) crearTapaInferior(perfil_original, num_instancias) ;
if (extremos[1] && tapa_sup) crearTapaSuperior(perfil_original, num_instancias) ;
}
void ObjRevolucion::crearTablaVertices(std::vector<Tupla3f> perfil_original, int num_instancias){
v.clear() ;
std::vector<Tupla3f> primerainstancia ;
for (unsigned i = 0 ; i < num_instancias ; i++){
for (unsigned j = 0 ; j < perfil_original.size() ; j++){
Tupla3f instancia_perfil_rotado = Rotacion(/*Añadir eje,*/ perfil_original[j],i,num_instancias) ;
v.push_back(instancia_perfil_rotado) ;
if (i == 0) primerainstancia.push_back(instancia_perfil_rotado) ;
}
}
for (int i = 0 ; i < primerainstancia.size() ; i++){
v.push_back(primerainstancia[i]) ;
}
}
void ObjRevolucion::crearTablaTriangulos(std::vector<Tupla3f> perfil_original, int num_instancias){
f.clear() ;
std::vector<Tupla3i> primerainstancia ;
for (unsigned i = 0 ; i < num_instancias ; i++){
for (unsigned j = 0 ; j < perfil_original.size() ; j++){
float a = perfil_original.size() * i + j ;
float b = perfil_original.size() * ((i+1) % num_instancias) + j ;
f.push_back({a,b,b+1}) ;
f.push_back({a,b+1,a+1}) ;
if (i == 0){
primerainstancia.push_back({a,b,b+1}) ;
primerainstancia.push_back({a,b+1,a+1}) ;
}
}
}
for (int i = 0 ; i < primerainstancia.size() ; i++){
std::cout << "Cara de primera instancia\n" ;
f.push_back(primerainstancia[i]) ;
}
}
void ObjRevolucion::crearTablaCoordenadas(std::vector<Tupla3f> perfil_original,int num_instancias){
ct.clear();
float x = 0,y,raiz ;
std::vector<float> dif ;
raiz = sqrt(pow(perfil_asociado[0][0] - perfil_asociado[perfil_asociado.size()-2][0],2)
+pow(perfil_asociado[0][1] - perfil_asociado[perfil_asociado.size()-2][1],2)
+pow(perfil_asociado[0][2] - perfil_asociado[perfil_asociado.size()-2][2],2)) ;
dif.push_back(raiz) ;
for (int i = 1 ; i < perfil_original.size() ; i++){
raiz = sqrt(pow(perfil_original[i][0] - perfil_original[i-1][0],2)
+pow(perfil_original[i][1] - perfil_original[i-1][1],2)
+pow(perfil_original[i][2] - perfil_original[i-1][2],2)) ;
dif.push_back(dif[i-1]+raiz) ;
}
for(int i = 0 ; i <= num_instancias ; i++){
for(int j = 0 ; j < perfil_original.size() ; j++){
y = dif[j]/dif[dif.size()-1] ;
ct.push_back({x,y}) ;
}
x = (i*1.0)/(num_instancias+1) ;
}
}
Tupla3f ObjRevolucion::Rotacion(Tupla3f vertice, unsigned instancia, unsigned num_instancias){
/*De momento rotará con el eje Y*/
Tupla3f rotado ;
float x, y, z;
float angulo = ((2 * PI * instancia) / num_instancias);
y = vertice[1];
x = ((cos(angulo) * vertice[0]) + (vertice[2] * sin(angulo)));
z = ((-sin(angulo) * vertice[0]) + (vertice[2] * cos(angulo)));
rotado = {x,y,z} ;
return rotado;
}
void ObjRevolucion::crearTapaInferior(std::vector<Tupla3f> perfil_original, int num_instancias){
std::vector<Tupla3f> despolarizado, polos ;
for (int i = 0 ; i < perfil_original.size() - 2 ; i++){
despolarizado.push_back(perfil_original[i]) ;
}
for (int i = perfil_original.size() - 2 ; i < perfil_original.size() ; i++){
polos.push_back(perfil_original[i]) ;
}
v.push_back(polos[0]) ;
int tam = v.size() ;
for (int i = 0 ; i <= num_instancias; i++ ){
f.push_back({ v.size() - 1 ,((i+2)*despolarizado.size() % (v.size()-1)) ,((i+1)*despolarizado.size() % (v.size()-1))});
}
}
void ObjRevolucion::crearTapaSuperior(std::vector<Tupla3f> perfil_original, int num_instancias){
std::vector<Tupla3f> despolarizado, polos ;
for (int i = 0 ; i < perfil_original.size() - 2 ; i++){
despolarizado.push_back(perfil_original[i]) ;
}
for (int i = perfil_original.size() - 2 ; i < perfil_original.size() ; i++){
polos.push_back(perfil_original[i]) ;
}
v.push_back(polos[1]) ;
int tam = v.size() ;
ct.push_back({0.5,0.5}) ;
for (int i = 0 ; i <= num_instancias; i++ ){
f.push_back({ v.size() - 1 ,(((i+1)*despolarizado.size()-1) % (v.size()-2)) ,(((i+2)*despolarizado.size()-1) % (v.size()-2))});
}
}
std::vector<Tupla3f> ObjRevolucion::formatoPerfil(std::vector<Tupla3f> perfil_original){
std::vector<Tupla3f> temporal, temporal_polos, formato ;
if(perfil_original[0][1] - perfil_original[1][1] <= 0)
for(int i = 0 ; i < perfil_original.size() ; i++){
temporal.push_back(perfil_original[i]) ;
}
else
{
for(int i = 0 ; i < perfil_original.size() ; i++){
temporal.push_back(perfil_original[perfil_original.size() - i - 1]) ;
}
}
for(int i = 0 ; i < temporal.size() ; i++){
if(temporal[i][0] == 0 && temporal[i][2] == 0)
temporal_polos.push_back(temporal[i]) ;
else
formato.push_back(temporal[i]) ;
}
if (temporal_polos.size() < 1)
formato.push_back({0,temporal[0][1],0}) ;
else
formato.push_back(temporal_polos[0]) ;
if (temporal_polos.size() < 2)
formato.push_back({0,temporal[temporal.size()-1][1],0}) ;
else
formato.push_back(temporal_polos[1]) ;
return formato ;
}
void ObjRevolucion::detectarTapas(std::vector<Tupla3f> perfil){
extremos[0] = (perfil[perfil.size() - 2][0] == 0 && perfil[perfil.size() - 2][2] == 0) ;
extremos[1] = (perfil[perfil.size() - 1][0] == 0 && perfil[perfil.size() - 1][0] == 0) ;
}
void ObjRevolucion::modificarTapas(bool tapa_sup, bool tapa_inf){
crearMalla(perfil_asociado,instancias,tapa_sup,tapa_inf) ;
}