-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
413 lines (364 loc) · 11.5 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include <vrg3d/VRG3D.h>
#include "pointmanager.h"
#include "animationcontroller.h"
#include "filter.h"
#define PROFILING
#undef PROFILING
#ifdef PROFILING
#include "gperftools/profiler.h"
#endif
using namespace G3D;
enum struct Mode { STANDARD, ANIMATION, FILTER, SLICES};
class DinoApp : public VRApp
{
public:
DinoApp(std::string setup, std::string dataFile, std::string pathFile, std::string surfaceFile, bool showAllPaths = false) : VRApp(),
ac()
{
std::string profFile = "/users/jtveite/d/prof/profile-" + setup;
#ifdef PROFILING
ProfilerStart(profFile.c_str());
#endif
std::string logName = "logs/dino-log-" + setup;
Log *dinolog = new Log(logName);
log = dinolog;
init(setup, dinolog);
_mouseToTracker = new MouseToTracker(getCamera(), 2);
frameTime = clock();
// pm.ReadFile("/users/jtveite/data/jtveite/slices-130.out");
// pm.ReadFile("/users/jtveite/dinotrackviewer/test-data")
dinolog->printf("startish\n");
pm.ReadFile(dataFile, true);
dinolog->printf("File read\n");
pm.SetupDraw(showAllPaths);
if (pathFile != "" && pathFile != "a"){
pm.ReadPathlines(pathFile);
}
dinolog->printf("pathlines read\n");
if (surfaceFile != "" && surfaceFile != "a"){
pm.ReadSurface(surfaceFile);
}
// pm.ReadSurface("tris/active");
dinolog->printf("surface read\n");
Matrix3 scale = Matrix3::fromDiagonal(Vector3(20, 20, 20));
CoordinateFrame scaleC (scale);
CoordinateFrame rotate = CoordinateFrame::fromXYZYPRDegrees(
2, 0, -6,
-90, -90, 180);
_owm = rotate * scale;
ac.setFrameCount(pm.getLength());
ac.setSpeed(15);
//std::cout << GL_NO_ERROR << ' ' << GL_INVALID_ENUM << ' ' << GL_INVALID_VALUE << ' ' << GL_INVALID_OPERATION << ' ' << GL_INVALID_FRAMEBUFFER_OPERATION << std::endl;
//
targetTracker = "Wand_Tracker";
if (setup == "" || setup == "desktop"){
targetTracker = "Mouse1_Tracker";
}
_slicer = new SliceFilter();
}
virtual ~DinoApp() {
#ifdef PROFILING
ProfilerFlush();
ProfilerStop();
#endif
}
//general skeleton of user input from demo app
void doUserInput(Array<VRG3D::EventRef> &events)
{
Array<VRG3D::EventRef> newEvents;
_mouseToTracker->doUserInput(events, newEvents);
events.append(newEvents);
for (int i = 0; i < events.size(); i++) {
std::string eventName = events[i]->getName();
if (endsWith(events[i]->getName(), "_Tracker")) {
if (_trackerFrames.containsKey(events[i]->getName())){
_trackerFrames[events[i]->getName()] = events[i]->getCoordinateFrameData();
}
else {
_trackerFrames.set(events[i]->getName(), events[i]->getCoordinateFrameData());
}
}
else if (eventName == "B09_down" || eventName == "kbd_1_down"){
mode = Mode::STANDARD;
}
else if (eventName == "B10_down" || eventName == "kbd_2_down"){
//mode = Mode::ANIMATION;
pm.showSurface = !pm.showSurface;
}
else if (eventName == "B11_down" || eventName == "kbd_3_down"){
mode = Mode::FILTER;
pm.SetFilter(new MotionFilter(motionThreshold));
}
else if (eventName == "B12_down" || eventName == "kbd_4_down"){
//mode = Mode::SLICES;
//pm.SetFilter(_slicer);
_slicing = !_slicing;
}
else if(eventName == "Mouse_Left_Btn_down"){
_moving = true;
}
else if(eventName == "Mouse_Left_Btn_up"){
_moving = false;
}
else if (eventName == "Wand_Right_Btn_down"){
_moving = true;
}
else if (eventName == "Wand_Right_Btn_up"){
_moving = false;
}
else if (eventName == "Wand_Left_Btn_down"){
_placePathline = true;
}
else if (eventName == "kbd_Q_down"){
_placePathline = true;
printf("QQQQQQQ\n");
}
else if (eventName == "kbd_DOWN_down"){
ac.decreaseSpeed();
}
else if (eventName == "kbd_UP_down"){
ac.increaseSpeed();
}
else if (eventName == "B04_down"){
// ac.decreaseSpeed();
if (mode == Mode::STANDARD){
pm.pointSize /= 1.3;
}
if (mode == Mode::ANIMATION){
ac.decreaseSpeed();
}
if (mode == Mode::SLICES){
_slicer->addStart(.001);
pm.SetFilter(_slicer);
}
if (mode == Mode::FILTER){
motionThreshold *= 1.414;
pm.SetFilter(new MotionFilter(motionThreshold));
}
}
else if (eventName == "B03_down"){
if (mode == Mode::STANDARD){
pm.pointSize *= 1.3;
}
if (mode == Mode::ANIMATION){
ac.increaseSpeed();
}
if (mode == Mode::SLICES){
_slicer->addStart(-.001);
pm.SetFilter(_slicer);
}
if (mode == Mode::FILTER){
motionThreshold /= 1.414;
pm.SetFilter(new MotionFilter(motionThreshold));
}
}
else if (eventName == "B05_down"){
if (mode == Mode::STANDARD || mode == Mode::ANIMATION || mode == Mode::FILTER){
ac.stepBackward();
}
if (mode == Mode::SLICES){
_slicer->addGap(-.0025);
pm.SetFilter(_slicer);
}
}
else if (eventName == "B06_down"){
if (mode == Mode::STANDARD || mode == Mode::ANIMATION || mode == Mode::FILTER){
ac.stepForward();
}
if (mode == Mode::SLICES){
_slicer->addGap(.0025);
pm.SetFilter(_slicer);
}
}
else if (eventName == "B07_down"){
ac.togglePlay();
}
else if (eventName == "kbd_SPACE_down"){
ac.togglePlay();
}
else if (eventName == "kbd_LEFT_down"){
ac.stepBackward();
}
else if (eventName == "kbd_RIGHT_down"){
ac.stepForward();
}
else if (eventName == "kbd_C_down"){
pm.pointSize *= 1.3;
}
else if (eventName == "kbd_V_down"){
pm.pointSize /= 1.3;
}
/*
else if (eventName == "B09_down"){
pm.SetFilter(new Filter());
}
else if (eventName == "B10_down"){
pm.SetFilter(new MotionFilter(motionThreshold));
}
else if (eventName == "B11_down" || eventName == "kbd_J_down"){
motionThreshold *= 1.414;
pm.SetFilter(new MotionFilter(motionThreshold));
}
else if (eventName == "B12_down" || eventName == "kbd_K_down"){
motionThreshold /= 1.414;
pm.SetFilter(new MotionFilter(motionThreshold));
}
*/
else if (eventName == "kbd_T_down"){
pm.SetFilter(_slicer);
}
else if (eventName == "kbd_R_down"){
_slicer->addStart(0.0025);
pm.SetFilter(_slicer);
}
else if (eventName == "kbd_F_down"){
_slicer->addGap(0.0025);
pm.SetFilter(_slicer);
}
else if (eventName == "kbd_G_down"){
_slicer->addSize(0.0025);
pm.SetFilter(_slicer);
}
else if (beginsWith(eventName, "aimo")){}
else if (eventName == "SynchedTime"){}
else{
//std::cout << eventName << std::endl;
}
}
}
void doGraphics(RenderDevice *rd)
{
log->print("start of graphics");
_lastFrame = ac.getFrame();
float t = _lastFrame;
//std::cout << "Rendering frame: " << _frameCounter << std::endl;
//Do rotation of trackers if needed
//std::string targetTracker = "Mouse1_Tracker";
//std::string targetTracker = "Wand_Tracker";
Array<std::string> trackerNames = _trackerFrames.getKeys();
Vector4 cuttingPlane;
for (int i = 0; i < trackerNames.size(); i++){
CoordinateFrame trackerFrame = _trackerFrames[trackerNames[i]];
if (trackerNames[i] == targetTracker){
if (_moving){
CoordinateFrame amountMoved = trackerFrame * _lastTrackerLocation.inverse();
_owm = amountMoved * _owm;
}
_lastTrackerLocation = trackerFrame;
Vector3 location = trackerFrame.translation;
Vector3 up = trackerFrame.upVector();
//Draw::arrow( location, up, rd, Color3::orange(), .1);
Matrix4 owm = _owm.toMatrix4();
Matrix4 transform = owm.inverse();
location = (transform * Vector4(location, 1.0)).xyz();
up = (transform * Vector4(up, 0.0)).xyz();
float offset = -(up.x * location.x + up.y * location.y + up.z * location.z);
if (_slicing){
cuttingPlane = Vector4(up.x, up.y, up.z, offset);
}
}
}
if (_placePathline){
_placePathline = false;
// CoordinateFrame oldSpace = _lastTrackerLocation * _owm.inverse();
// Vector3 location = oldSpace.translation;
Vector3 location = _lastTrackerLocation.translation;
//location = _lastTrackerLocation.pointToWorldSpace(Vector3(0,0,0));
Matrix4 owm = _owm.toMatrix4();
Vector4 l = owm.inverse() * Vector4(location, 1.0);
pm.AddPathline(l.xyz(), t);
//std::cout << l.xyz() << std::endl;
//printf("placing a pathline at %f, %f, %f\n", location.x, location.y, location.z);
//printf("placing a pathline at %f, %f, %f\n", location.x, location.y, location.z);
}
Vector3 location = _lastTrackerLocation.translation;
//location = _lastTrackerLocation.pointToWorldSpace(Vector3(0,0,0));
Matrix4 owm = _owm.toMatrix4();
Vector4 l = owm.inverse() * Vector4(location, 1.0);
pm.TempPathline(l.xyz(), t);
rd->pushState();
//rd->setObjectToWorldMatrix(_virtualToRoomSpace);
int gl_error;
while((gl_error = glGetError()) != GL_NO_ERROR)
{
std::cout << "Flushing gl errors " << gl_error << std::endl;
}
rd->pushState();
rd->setObjectToWorldMatrix(_owm);
Matrix4 mvp = rd->invertYMatrix() * rd->modelViewProjectionMatrix();
//std::cout << t << std::endl;
log->print("Before draw\n");
pm.Draw(rd, t, mvp, cuttingPlane);
log->print("after draw\n");
rd->popState();
Vector3 hp = getCamera()->getHeadPos();
// printf("Head position: %f, %f, %f\n", hp.x, hp.y, hp.z);
Vector3 lv = getCamera()->getLookVec();
//Draw a small ball at the tracker location.
//
rd->pushState();
rd->setObjectToWorldMatrix(CoordinateFrame());
Sphere s (_lastTrackerLocation.translation, 0.02);
Draw::sphere(s, rd, Color3::red());
rd->popState();
// printf("Look Vector: %f, %f, %f\n", lv.x, lv.y, lv.z);
rd->popState();
clock_t newTime = clock();
frameTime = newTime;
}
protected:
PointManager pm;
MouseToTrackerRef _mouseToTracker;
CoordinateFrame _virtualToRoomSpace;
CoordinateFrame _owm;
CoordinateFrame _lastTrackerLocation;
Table<std::string, CoordinateFrame> _trackerFrames;
bool _moving;
clock_t frameTime;
AnimationController ac;
int _lastFrame;
bool _placePathline;
std::string targetTracker;
float motionThreshold = 0.01;
SliceFilter* _slicer;
Mode mode = Mode::STANDARD;
bool _slicing;
Log* log;
};
int main(int argc, char **argv )
{
std::string setup;
std::string dataFile;
std::string pathsFile;
std::string surfaceFile;
bool showAllPaths = false;
if (argc >= 2)
{
setup = std::string(argv[1]);
}
if(argc >= 3)
{
dataFile = std::string(argv[2]);
}
else{
dataFile = "/users/jtveite/data/jtveite/dino/slices-68-trimmed.out";
}
if (argc >= 4)
{
pathsFile = std::string(argv[3]);
}
else{
pathsFile = "";
}
if (argc >= 5)
{
surfaceFile = std::string(argv[4]);
}
if (argc >= 6)
{
showAllPaths = true;
}
DinoApp *app = new DinoApp(setup, dataFile, pathsFile, surfaceFile, showAllPaths);
app->run();
return 0;
}