-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
218 lines (170 loc) · 6.58 KB
/
main.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
#include <iostream>
#include "./src/TestCaseGenerator.h"
#include "./src/HalfEdgeTestCaseGenerator.h"
#include "./src/DrawTriangulation.h"
#include <GL/glut.h>
GLfloat xRotated, yRotated, zRotated;
GLdouble radius=1;
GLfloat ratio ;
int ancho, alto;
void display(void);
void reshape(int x, int y);
DrawTriangulation* drawTriangulation = nullptr;
void display(void)
{
drawTriangulation->render();
}
void reshape(int x, int y)
{
ancho = x;
alto = y;
// No debemos dividir por cero al calcular el ratio
if(alto == 0) alto = 1;
// Establezco mi view port
glViewport(0, 0, ancho, alto);
// Inicializo la matriz de proyeccion
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Calculo el ratio
ratio = (float)ancho / (float)alto;
if (ancho <= alto)
glOrtho (-100, 100.0, -100 / ratio, 100.0 / ratio, 1.0, -1.0);
else
glOrtho (-100.0 * ratio, 100.0 * ratio, -100.0, 100.0, 1.0, -1.0);
}
void keyboard (unsigned char key, int x, int y)
{
drawTriangulation->keyboard(key, x, y);
// cout<<endl<<"DISTANCE CHANGED: "<<drawTriangulation->getDistance();
glutPostRedisplay();
}
void arrowKeys (int key, int x, int y)
{
drawTriangulation->arrowKeys(key, x, y);
// cout<<endl<<"CAMERA CHANGED: (xi="<<drawTriangulation->getXi()<<", yi="<<drawTriangulation->getYi()<<");";
glutPostRedisplay();
}
void mouseControllerFunc(int button, int state, int x, int y)
{
/**
* donde los parámetros que la función nos da (automáticamente y sin tener que hacer nada) son los siguientes:
button, un entero que puede tomar los valores GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON o GLUT_RIGHT_BUTTON
según el usuario haya pulsado el botón izquierdo, el del medio o el derecho, respectivamente.
Estas constantes estan predefinidas y podemos usarlas sin problema en nuestro código pues
GLUT las interpretará correctamente.
state, puede tomar los valores GLUT_UP o GLUT_DOWN, según si se ha pulsado/soltado el correspondiente botón.
X e Y, son las coordenadas referidas a la ventana de visualización, no al mundo virtual, en las que se pulsó/soltó el susodicho botón.
* */
if (!drawTriangulation->isManualMode())
return;
int max = drawTriangulation->getMaxValue();
int min = -drawTriangulation->getMaxValue();
int h = alto;
int w = ancho;
if(state == GLUT_UP)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
try {
// cout<<endl<<"SOLTE BOTON IZQUIERDO en x="<<x<<", y="<<y<<")";
float ancho_total_seccion_espacio;
float alto_total_seccion_espacio;
float relacionX, relacionY;
if (ratio<1)
{
ancho_total_seccion_espacio = max - min; // Coord X
alto_total_seccion_espacio = max/ratio - (min/ratio); // Coord Y
}else{
ancho_total_seccion_espacio = max*ratio - (min*ratio);// Coord X
alto_total_seccion_espacio = max - (min); // Coord Y
}
relacionX = ancho_total_seccion_espacio/w;
relacionY = alto_total_seccion_espacio/h;
float espX, espY;
espX = (x-w/2) * relacionX;
espY = (y-h/2) * relacionY;
char cadena [350];
sprintf (cadena, "Pantalla: %d, %d --- Espacio: %f, %f",x,y,espX,espY);
glutSetWindowTitle(cadena);
// cout<<endl<<"Pantalla: "<<x<<","<<y<<" --- Espacio: "<<espX<<","<<espY;
drawTriangulation->addPointToTriangulation(espX, espY);
}
catch(exception& e)
{
cout <<endl<<e.what();
}
glutPostRedisplay();
break;
case GLUT_MIDDLE_BUTTON:
// cout<<endl<<"SOLTE BOTON DEL MEDIO en x="<<x<<", y="<<y<<")";
break;
case GLUT_RIGHT_BUTTON:
// cout<<endl<<"SOLTE BOTON DERECHO en x="<<x<<", y="<<y<<")";
break;
}
glutPostRedisplay();
}
}
/* Funciones de Control del menú seleccionado */
/* Se ejecutan cuando el usuario utilize los menús */
void menu_nivel_1(int identificador){
/* Según la opción de 1er nivel activada, ejecutaré una rutina u otra */
switch( identificador){
case 0: // add random point
drawTriangulation->addRandomPointToTriangulation();
glutPostRedisplay();
break;
case 1: // add point cloud
drawTriangulation->addPointCloudToTriangulation();
drawTriangulation->setMode(DrawTriangulation::GRID);
glutPostRedisplay();
break;
case 2: // add grid
drawTriangulation->addPointGridToTriangulation();
drawTriangulation->setMode(DrawTriangulation::GRID);
glutPostRedisplay();
break;
case 3: // set manual mode to use mouse
drawTriangulation->setMode(DrawTriangulation::MANUAL);
glutPostRedisplay();
break;
case 4: // reset
drawTriangulation->reset();
glutPostRedisplay();
break;
default:
exit( -1 );
}
}
void mouseMotionControllerFunc(GLsizei x, GLsizei y)
{
// glutPostRedisplay();
}
// Ejecución principal
int main(int argc, char **argv) {
drawTriangulation = new DrawTriangulation(MAX_VALUE, 5000, 5000);
glutInit(&argc, argv);
glutInitWindowSize(5000,5000);
glutInitWindowPosition (100, 100);
glutCreateWindow("Delaunay Triangulation");
/* Creación del menú */
int submenu = glutCreateMenu(menu_nivel_1);
glutAddMenuEntry("Adicionar Punto Aleatorio", 0);
glutAddMenuEntry("Adicionar Nube de Puntos Aleatorios", 1);
glutAddMenuEntry("Adicionar Grilla", 2);
glutAddMenuEntry("Modo Manual", 3);
glutAddMenuEntry("Reset", 4);
glutAddMenuEntry("Salir", 5);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(arrowKeys);
glutMouseFunc( mouseControllerFunc);
glutMotionFunc( mouseMotionControllerFunc);
glutMainLoop();
// HalfEdgeTestCaseGenerator testCaseGenerator("analysis_half.txt", 100, 100, 100);
// testCaseGenerator.testALL();
return 0;
}