-
Notifications
You must be signed in to change notification settings - Fork 2
/
glut.c++
268 lines (241 loc) · 7.05 KB
/
glut.c++
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
// Interactive demo, using freeglut.
// https://www.opengl.org/resources/libraries/glut/spec3/spec3.html
// http://freeglut.sourceforge.net/
#include <cmath>
#include <GL/freeglut.h> // Instead of glut.h, to get glutLeaveMainLoop().
#include "si.h"
bool fInside = true;
bool fQuit = false;
d_simplex sFound;
vertex wFound; // Actually d+1 weights for a weighted sum.
vertex rFound;
constexpr auto NaN = std::numeric_limits<double>::signaling_NaN();
vertex vQ{NaN, NaN};
vertex vP;
double scale = NaN;
double margin = NaN;
double dmargin = NaN;
void drawChar(const vertex& v, char c) {
glRasterPos2f(v[0], v[1]);
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, c);
}
// Convert vector to GLdouble*.
void glVert2(const vertex& v) {
glVertex2d(v[0], v[1]);
}
void drawSimplices(bool fInside) {
if (fInside)
glColor3f(1,0,.4);
else
glColor3f(.3,0,.12);
for (const auto& s: si) {
glBegin(GL_LINE_LOOP);
for (auto i: s) glVert2(qi[i]);
glEnd();
}
}
void drawRaysimplices(bool fInside) {
if (fInside)
glColor3f(0,.35,0);
else
glColor3f(0,0.7,0);
for (const auto& s: siRay) {
glBegin(GL_LINE_LOOP);
glVert2(qC);
glVert2(qi[s[0]]);
glVert2(qi[s[1]]);
glEnd();
}
}
void display() {
if (fQuit)
return;
glClear(GL_COLOR_BUFFER_BIT);
// gluOrtho2D set the range for both x and y to -margin .. scale+margin.
// Mouse moved, thus assigning to vQ.
// GLUT can't report mouse position until then.
const bool fInited = !std::isnan(vQ[0]);
// Draw layers, most to least hidden.
if (fInited) {
// Bounding simplex (i.e., triangle).
glBegin(GL_TRIANGLES);
if (fInside) {
glColor3f(.25,0,.08);
for (auto i: sFound) glVert2(qi[i]);
} else {
glColor3f(0,.15,0);
glVert2(qC);
auto v = qi[sFound[0]];
v[0] += 100.0 * (v[0] - qC[0]);
v[1] += 100.0 * (v[1] - qC[1]);
glVert2(v);
v = qi[sFound[1]];
v[0] += 100.0 * (v[0] - qC[0]);
v[1] += 100.0 * (v[1] - qC[1]);
glVert2(v);
}
glEnd();
// Bar graph of output values.
// (If e exceeds the window's width in pixels,
// aliasing amusingly omits some bars.)
glBegin(GL_QUADS);
const auto e = vP.size();
for (auto i=0u; i<e; ++i) {
const auto y0 = -0.5 * margin;
const auto y1 = scale * vP[i];
const auto x0 = scale * (i+0.4)/e;
const auto x1 = scale * (i+0.6)/e;
glColor3f(0.0, 0.0, 0.1); glVertex2d(x0, y0);
glColor3f(0.3, 0.3, 0.7); glVertex2d(x0, y1);
glColor3f(0.3, 0.3, 0.7); glVertex2d(x1, y1);
glColor3f(0.0, 0.0, 0.1); glVertex2d(x1, y0);
}
glEnd();
// Disc around each vertex of containing simplex.
// Disc's radius indicates weight (barycentric coordinate) of corresponding vertex.
const auto d = vQ.size(); // Fussy. It must be 2.
for (auto i=0u; i<=d; ++i) {
const auto iVertex = sFound[i];
const auto& v = iVertex < 0 ? qC : qi[iVertex];
const auto r0 = 0.07 * scale;
const bool negative = wFound[i] < 0.0;
const auto r = 0.07 * scale * sqrt(fabs(wFound[i]));
glPushMatrix();
glTranslatef(v[0], v[1], 0);
if (negative)
glColor3f(0.1,0.7,0.1);
else
glColor3f(0.5,0.5,0.5);
glScalef(r, r, 0);
// This isn't worth precomputing or moving to a display list.
glBegin(GL_POLYGON);
for (auto a = 0.0; a <= 2.0*M_PI; a += 0.05)
glVertex2f(cos(a), sin(a));
glEnd();
glColor3f(0.2,0.2,0.2);
glScalef(r0/r, r0/r, 0);
glBegin(GL_LINE_LOOP);
for (auto a = 0.0; a <= 2.0*M_PI; a += 0.05)
glVertex2f(cos(a), sin(a));
glEnd();
glPopMatrix();
}
}
// Color the hull correctly.
if (fInside) {
drawRaysimplices(true);
drawSimplices(true);
} else {
drawSimplices(false);
drawRaysimplices(false);
}
// Center vertex of ray simplices.
glColor3f(1,1,1);
drawChar(qC, 'C');
if (fInited) {
#ifdef TESTING
// Reconstructed point.
glColor3f(1,.2,.2);
drawChar(rFound, 'R');
#endif
// Query point.
glColor3f(1,1,1);
drawChar(vQ, 'q');
}
glutSwapBuffers();
}
void keyboard(unsigned char key, int /*x*/, int /*y*/) {
switch (key) {
case 'q':
case 27: // Esc
fQuit = true; // Prevent display() from reading the cleared qi[] etc.
terminate();
#ifdef __APPLE__
exit(0);
#else
glutLeaveMainLoop();
#endif
}
}
// Dimensions of window.
auto xSize = 950;
auto ySize = 950;
void XYFromMouse(double& x, double& y, int xM, int yM) {
x = double(xM) / xSize * (scale + 2*margin) - margin;
y = (1. - double(yM) / ySize) * (scale + 2*margin) - margin;
}
constexpr auto badMouse = std::numeric_limits<int>::max();
auto xMouse = badMouse;
auto yMouse = badMouse;
void mouse_hover(int x, int y) {
if (fQuit)
return;
XYFromMouse(vQ[0], vQ[1], xMouse=x, yMouse=y);
vP = eval(vQ, &fInside, &sFound, &wFound, &rFound);
}
void mouse_kick() {
if (xMouse != badMouse && yMouse != badMouse)
mouse_hover(xMouse, yMouse);
}
void reshape(int w, int h) {
glViewport(0, 0, xSize=w, ySize=h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-margin, scale+margin, -margin, scale+margin);
}
void mousewheel(int button, int state, int /*x*/, int /*y*/) {
// glutMouseWheelFunc() isn't yet reliably implemented on X11.
// 3 and 4 is common for mice, and for macOS trackpad vertical scrolling.
// But some mice use 5 and 6.
if (button == 3 || button == 4) {
if (state == GLUT_UP)
return;
margin += button == 3 ? -dmargin : dmargin;
// reshape(xSize, ySize) would work, but only after an actual window resize. Freeglut bug.
reshape(xSize-1, ySize);
reshape(xSize+1, ySize);
// During a zoom, prevent vQ from drifting away from mouse cursor.
mouse_kick();
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv); // Parse -display, -geometry 200x200+30+50, -gldebug etc.
if (argc != 3) {
printf("usage: %s highDim numPoints\n", argv[0]);
return 1;
}
constexpr auto d = 2;
const auto e = atoi(argv[1]);
const auto cPoint = atoi(argv[2]);
wFound.resize(d+1);
if (!init(d, e, cPoint, qi_kind::spaced))
return 1;
// Assumes that the smallest x and smallest y are near zero.
scale = 0.0;
for (const auto& q: qi)
scale = std::max(scale, std::max(q[0], q[1]));
margin = 0.2 * scale;
dmargin = 0.15 * margin;
#ifndef __APPLE__
// Freeglut bug. This and glutLeaveMainLoop() abort, because !fgState.Initialised,
// because src/osx is empty and in particular lacks fg_init_osx.c,
// whose fgPlatformInitialize() would have set fgState.Initialised.
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
#endif
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(xSize, ySize);
glutCreateWindow("Simplicial Interpolator");
glutKeyboardFunc(keyboard);
glutPassiveMotionFunc(mouse_hover);
glutMotionFunc(mouse_hover);
glutMouseFunc(mousewheel);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutIdleFunc(display);
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
glutMainLoop();
return 0;
}