-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomum.cpp
364 lines (299 loc) · 8.55 KB
/
comum.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
#include <cstdio>
#include <GL/glut.h>
#include <cmath>
#include <cassert>
#include <cstring>
#include <cerrno>
#include "comum.h"
using namespace std;
// flags para compilar (linux)
// -lGL -lGLU -lglut -lm
// TODO
// implementar toda as funcoes necessario para o Obj3d
// uma delas eh o desenhar que recebe o Struct como primeiro parametro
// ** eh NAO chama glClear(GL_COLOR_BUFFER_BIT);
//
// usar c++ nao necessaria mente eu tenho que usar clases
// =====================================================================
void die(const char *message)
{
if(errno){
perror(message);
}
else {
printf("ERRO: %s\n", message);
exit(1);
}
}
void ponto2i(int x, int y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void ponto3i(int x, int y, int z)
{
glBegin(GL_POINTS);
glVertex3i(x, y, z);
glEnd();
glFlush();
}
void desenhar_poligono_pontos(float * p1, float * p2, float * p3)
{
glBegin(GL_POLYGON);
glVertex3d(p1[0], p1[1], p1[2]);
glVertex3d(p2[0], p2[1], p2[2]);
glVertex3d(p3[0], p3[1], p3[2]);
glEnd();
}
void desenhar_arestas_pontos(float * p1, float * p2, float * p3)
{
glBegin(GL_LINE_LOOP);
glVertex3d(p1[0], p1[1], p1[2]);
glVertex3d(p2[0], p2[1], p2[2]);
glVertex3d(p3[0], p3[1], p3[2]);
glEnd();
}
inline float get_distancia(float *p1, float *p2)
{ return sqrt(pow(p1[0] - p2[0], 2) + pow(p1[1] - p2[1], 2) + pow(p1[2] - p2[2], 2)); }
void get_centro_massa(vector<float *> & lst, float * destiny)
{
int i, max = lst.size();
float x, y, z;
float menor_x, maior_x, menor_y, maior_y, menor_z, maior_z;
float * point_corrent = NULL;
for(i=0; i<max; i++){
point_corrent = lst[i];
x = point_corrent[0];
y = point_corrent[1];
z = point_corrent[2];
if(i==0 || x < menor_x){
menor_x = x;
}
if(i==0 || x > maior_x){
maior_x = x;
}
if(i==0 || y < menor_y){
menor_y = y;
}
if(i==0 || y > maior_y){
maior_y = y;
}
if(i == 0 || z > maior_z){
maior_z = z;
}
if(i == 0 || z < menor_z){
menor_z = z;
}
}
// centro de massa
destiny[0] = (menor_x + maior_x)/2;
destiny[1] = (menor_y + maior_y)/2;
destiny[2] = (menor_z + maior_z)/2;
}
// retorna o posicao na list do ponto mais proximo de point
int get_mais_proximo_de(vector<float *> & lst, float *point)
{
float valor_menor = 0.0;
float * corrente = NULL;
int i, max = lst.size();
int local_menor = -1;
for(i=0; i<max; i++){
corrente = lst[i];
if(i == 0 || get_distancia(corrente, point) < valor_menor){
valor_menor = get_distancia(corrente, point);
local_menor = i;
}
}
assert(local_menor != -1);
return local_menor;
}
void transladar_pontos_n(vector<float *> & lst, float value, int eixo)
{
float * point = NULL;
int i, max = lst.size();
for(i=0; i<max; i++){
point = lst[i];
assert(point != NULL);
point[eixo] += value;
point = NULL;
}
}
// levar para cm para origem , escalar e voltar para ponto original
void escalar_pontos(vector<float *> & lst, float value)
{
float * point = NULL;
int i, max = lst.size();
// escala
for(i=0; i<max; i++){
point = lst[i];
assert(point != NULL);
point[0] = point[0] * value;
point[1] = point[1] * value;
point[2] = point[2] * value;
point = NULL;
}
}
// x - cm_x -> vai em direcao ao centro
void desloca_centro(vector<float *> & lst, float * cm_point)
{
int i, max = lst.size();
float x = cm_point[0], y = cm_point[1], z = cm_point[2];
float * tmp_point;
for(i=0; i<max; i++){
tmp_point = lst[i];
tmp_point[0] = tmp_point[0] - x;
tmp_point[1] = tmp_point[1] - y;
tmp_point[2] = tmp_point[2] - z;
}
}
void desloca_to(vector<float *> & lst, float * cm_point)
{
int i, max = lst.size();
float x = cm_point[0], y = cm_point[1], z = cm_point[2];
float * tmp_point;
for(i=0; i<max; i++){
tmp_point = lst[i];
tmp_point[0] = tmp_point[0] + x;
tmp_point[1] = tmp_point[1] + y;
tmp_point[2] = tmp_point[2] + z;
}
}
int contain(char * source, char target)
{
int i = 0;
while(source[i] != '\0'){
if(source[i] == target)
return 1;
i++;
}
return 0;
}
int get_at(char * source, char target)
{
int i = 0;
while(source[i] != '\0'){
if(source[i] == target)
return i;
i++;
}
return -1;
}
/// e como o vetor vai desalocar as paradas
void pegar_obj_de_arquivo(vector<float *> & vertices, vector<int *> & faces, const char * local)
{
FILE *arquivo;
const int len_buffer = 255;
char buffer[len_buffer];
arquivo = fopen(local, "r");
if(!arquivo) die("Nao foi possivel abrir o arquivo");
// leitura
// a face pode ter
// f 883/8837
// f x/y -> ponto con tres coordenadas, considera o z zero
char * tmp_char;
int a, b, c, tmpi, index;
float d, e, f, tmpf;
int count_faces = 0;
int count_vetices = 0;
/* float *p1, *p2, *p3;*/
while(fgets(buffer, len_buffer, arquivo)){
if(buffer[0] == '#') continue;
if(buffer[0] == 'f' && buffer[1] == ' '){// faces
/* printf("%s", buffer);*/
tmp_char = strtok(buffer+1, " \n");
while(tmp_char){
// tmp_char pode ter '8373/83874'
if(contain(tmp_char, '/')){
index = get_at(tmp_char, '/');
assert(index != -1);
tmp_char[index] = '\0';
}
tmpi = atoi(tmp_char);// apenas int
tmpi = tmpi - 1;
count_faces++;
if(count_faces == 1){
a = tmpi;
} else if(count_faces == 2){
b = tmpi;
} else if(count_faces == 3){ // ponto chave
c = tmpi;
count_faces = 0;
// add(faces, create_lst3i(a, b, c));
faces.push_back(create_lst3(a, b, c));
}
tmp_char = strtok(NULL, " \n");
}
} else if(buffer[0] == 'v' && buffer[1] == ' '){// vetices
tmp_char = strtok(buffer+1, " \n");
while(tmp_char){
tmpf = atof(tmp_char);
count_vetices++;
if(count_vetices == 1){
d = tmpf;
} else if(count_vetices == 2){
e = tmpf;
} else if(count_vetices == 3){// ponto chave
f = tmpf;
count_vetices = 0;
//add(vertices, create_lst3f(d, e, f));
vertices.push_back(create_lst3(d, e, f));
}
tmp_char = strtok(NULL, " \n");
}
}
}
fclose(arquivo);
}
int * create_lst2(int a, int b)
{
int *nv_lst = new int[2]; //malloc(2*(sizeof(int)));
assert(nv_lst != NULL);
nv_lst[0] = a;
nv_lst[1] = b;
return nv_lst;
}
// nao chama clear
void desenhar_obj(vector<float *> & lst_vertices, vector<int *> & lst_faces)
{
float *p1, *p2, *p3;
int * pt_face;
int i, max = lst_faces.size();
for(i=0; i<max; i++){
pt_face = lst_faces[i];//get(lst_faces, i);
// 93 96 97
p1 = lst_vertices[pt_face[0]];//get(lst_vertices, pt_face[0]); // primeiro valor 1 da face
p2 = lst_vertices[pt_face[1]];//get(lst_vertices, pt_face[1]);
p3 = lst_vertices[pt_face[2]];//get(lst_vertices, pt_face[2]);
desenhar_arestas_pontos(p1, p2, p3);
}
//glFlush();
//glutSwapBuffers(); // net.c
}
// *****************
// *** nova parte **
// *****************
Obj3d * st_create_obj()
{
Obj3d * nw_obj = new Obj3d;
nw_obj->lst_faces = new vector<int *>;
nw_obj->lst_vetices = new vector<float *>;
return nw_obj;
}
// deve ser chamado no final do processo
void st_destroy_obj(Obj3d * ob)
{
// primeiro eu tenho que deletar o conteudo
// depois o vetor propriamente dito
clear_lst(*ob->lst_faces);// uso * em um ponteiro pq clear_lst
// espera uma referencia a um objeto
clear_lst(*ob->lst_vetices);
delete ob->lst_faces;
delete ob->lst_vetices;
delete ob;
}
void st_desesenhar_obj(Obj3d * ob)
{
// usa o desenhar_obj
desenhar_obj(*ob->lst_vetices, *ob->lst_faces);
}