-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
423 lines (317 loc) · 10.8 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
414
415
416
417
418
419
420
421
422
423
/*****************************************************************************
Copyright (c) 2004 SensAble Technologies, Inc. All rights reserved.
OpenHaptics(TM) toolkit. The material embodied in this software and use of
this software is subject to the terms and conditions of the clickthrough
Development License Agreement.
For questions, comments or bug reports, go to forums at:
http://dsc.sensable.com
Module Name:
main.cpp
Description:
The main file that performs all haptics-relevant operation. Within a
asynchronous callback the graphics thread reads the position and sets
the force. Within a synchronous callback the graphics thread gets the
position and constructs graphics elements (e.g. force vector).
*******************************************************************************/
#include <stdlib.h>
#include <iostream>
#include <cstdio>
#include <cassert>
#include <HD/hd.h>
#include "helper.h"
#include <HDU/hduError.h>
#include <HDU/hduVector.h>
// Sample code to read in test cases:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
static double sphereRadius = 15;
static double mySphereRadius = 3;
/* Charge (positive/negative) */
int charge = 1;
static HHD ghHD = HD_INVALID_HANDLE;
static HDSchedulerHandle gSchedulerCallback = HD_INVALID_HANDLE;
/* Glut callback functions used by helper.cpp */
void displayFunction(void);
void handleIdle(void);
hduVector3Dd forceField(hduVector3Dd pos, hduVector3Dd* shape, int shape_size);
hduVector3Dd* Shape;
int Shape_size = 0;
/* Haptic device record. */
struct DeviceDisplayState
{
HHD m_hHD;
hduVector3Dd position;
hduVector3Dd force;
};
/*******************************************************************************
Client callback used by the graphics main loop function.
Use this callback synchronously.
Gets data, in a thread safe manner, that is constantly being modified by the
haptics thread.
*******************************************************************************/
HDCallbackCode HDCALLBACK DeviceStateCallback(void *pUserData)
{
DeviceDisplayState *pDisplayState =
static_cast<DeviceDisplayState *>(pUserData);
hdGetDoublev(HD_CURRENT_POSITION, pDisplayState->position);
hdGetDoublev(HD_CURRENT_FORCE, pDisplayState->force);
// execute this only once.
return HD_CALLBACK_DONE;
}
/*******************************************************************************
Graphics main loop function.
*******************************************************************************/
void displayFunction(void)
{
// Setup model transformations.
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
setupGraphicsState();
drawAxes(sphereRadius*3.0);
// Draw the fixed sphere.
static const hduVector3Dd fixedSpherePosition(0, 0, 0);
static const float fixedSphereColor[4] = {.2, .8, .8, .8};
GLUquadricObj* pQuadObj = gluNewQuadric();
// drawSphere(pQuadObj, fixedSpherePosition, fixedSphereColor, sphereRadius);
// Get the current position of end effector.
DeviceDisplayState state;
hdScheduleSynchronous(DeviceStateCallback, &state,
HD_MIN_SCHEDULER_PRIORITY);
// Draw a sphere to represent the haptic cursor and the dynamic
// charge.
static const float dynamicSphereColor[4] = { .8, .2, .2, .8 };
drawSphere(pQuadObj,
state.position,
dynamicSphereColor,
sphereRadius);
for (int i = 0; i < Shape_size; i++)
{
//glBegin(GL_POINTS); //starts drawing of points
//glVertex3f(Shape[0][0], Shape[0][1], Shape[0][2]);//upper-right corner
GLUquadricObj* pQuadObj1 = gluNewQuadric();
drawSphere(pQuadObj1, Shape[i], fixedSphereColor, mySphereRadius);
//glEnd();//end drawing of points
}
// Create the force vector.
hduVector3Dd forceVector = 40.0 * forceField(state.position, Shape, Shape_size);
//if (forceVector.magnitude() > 40.0) forceVector = normalize(forceVector) * 40.0;
drawForceVector(pQuadObj,
state.position,
forceVector,
sphereRadius*.1);
gluDeleteQuadric(pQuadObj);
glPopMatrix();
glutSwapBuffers();
}
/*******************************************************************************
Called periodically by the GLUT framework.
*******************************************************************************/
void handleIdle(void)
{
glutPostRedisplay();
if (!hdWaitForCompletion(gSchedulerCallback, HD_WAIT_CHECK_STATUS))
{
printf("The main scheduler callback has exited\n");
printf("Press any key to quit.\n");
getchar();
exit(-1);
}
}
/******************************************************************************
Popup menu handler
******************************************************************************/
void handleMenu(int ID)
{
switch(ID)
{
case 0:
exit(0);
break;
case 1:
charge *= -1;
break;
}
}
/*******************************************************************************
Given the position is space, calculates the (modified) coulomb force.
*******************************************************************************/
hduVector3Dd forceField(hduVector3Dd pos, hduVector3Dd* shape, int shape_size)
{
double scale = 12.0;
hduVector3Dd forceVec(0, 0, 0);
for (int i = 0; i < shape_size; i++) {
hduVector3Dd diff = pos - shape[i];
double dist = diff.magnitude();
// if two charges overlap...
if (dist < sphereRadius*2.0)
{
// Attract the charge to the center of the sphere.
hduVector3Dd unitPos = normalize(diff);
return -scale*unitPos;
}
else
{
hduVector3Dd unitPos = normalize(diff);
forceVec += -scale*unitPos / (dist*dist);
}
}
forceVec *= charge;
return forceVec;
}
/*******************************************************************************
Main callback that calculates and sets the force.
*******************************************************************************/
HDCallbackCode HDCALLBACK CoulombCallback(void *data)
{
HHD hHD = hdGetCurrentDevice();
hdBeginFrame(hHD);
hduVector3Dd pos;
hdGetDoublev(HD_CURRENT_POSITION,pos);
hduVector3Dd forceVec;
forceVec = forceField(pos, Shape, Shape_size);
hdSetDoublev(HD_CURRENT_FORCE, forceVec);
hdEndFrame(hHD);
HDErrorInfo error;
if (HD_DEVICE_ERROR(error = hdGetError()))
{
hduPrintError(stderr, &error, "Error during scheduler callback");
if (hduIsSchedulerError(&error))
{
return HD_CALLBACK_DONE;
}
}
return HD_CALLBACK_CONTINUE;
}
/*******************************************************************************
Schedules the coulomb force callback.
*******************************************************************************/
void CoulombForceField()
{
gSchedulerCallback = hdScheduleAsynchronous(
CoulombCallback, 0, HD_DEFAULT_SCHEDULER_PRIORITY);
HDErrorInfo error;
if (HD_DEVICE_ERROR(error = hdGetError()))
{
hduPrintError(stderr, &error, "Failed to initialize haptic device");
fprintf(stderr, "\nPress any key to quit.\n");
getchar();
exit(-1);
}
glutMainLoop(); // Enter GLUT main loop.
}
/******************************************************************************
This handler gets called when the process is exiting. Ensures that HDAPI is
properly shutdown
******************************************************************************/
void exitHandler()
{
hdStopScheduler();
hdUnschedule(gSchedulerCallback);
if (ghHD != HD_INVALID_HANDLE)
{
hdDisableDevice(ghHD);
ghHD = HD_INVALID_HANDLE;
}
}
void parse(string fileName)
{
cout << "Parse called" << endl;
ifstream stream1(fileName);
string line;
double coords[3];
cout << "At while" << endl;
while (getline(stream1, line)) {
Shape_size++;
}
Shape_size++;
cout<<Shape_size << endl;
Shape = new hduVector3Dd[Shape_size];
ifstream stream(fileName);
int j = 0;
while (getline(stream, line)) {
j++;
size_t i = 2;
for (int coord = 0; coord < 3; coord++) {
//for each coordinate find corresponding substring n and convert to float
string number;
int count = 0;
while (i < line.length() - 1 && line[i] != ' ') {
i++;
count++;
}
i++;
count++;
number = line.substr(i - count, count);
coords[coord] = stod(number)*10;
}
hduVector3Dd vc(coords[0], coords[2]-35, coords[1]);
Shape[j] = vc;
}
cout << "exited parse\n";
return;
}
void print(hduVector3Dd* Shape, int Shape_size) {
for (int i = 0; i < Shape_size; i++)
{
cout << "x = " << Shape[i][0] << " y = " << Shape[i][1] << " z = " << Shape[i][2] << "\n";
}
}
/******************************************************************************
Main function.
******************************************************************************/
int main(int argc, char* argv[])
{
HDErrorInfo error;
printf("Starting application\n");
atexit(exitHandler);
string file = "models/justcurves2.obj";
glPointSize(10.0f);
cout << "entering parse\n";
parse(file);
cout << "exiting parse\n";
cout << "entering print\n";
print(Shape, Shape_size);
cout << "exiting print\n";
glBegin(GL_POINTS);
glVertex3f(2, 2, 0);//upper-right corner
glEnd();
// Initialize the device. This needs to be called before any other
// actions on the device are performed.
ghHD = hdInitDevice(HD_DEFAULT_DEVICE);
if (HD_DEVICE_ERROR(error = hdGetError()))
{
hduPrintError(stderr, &error, "Failed to initialize haptic device");
fprintf(stderr, "\nPress any key to quit.\n");
getchar();
exit(-1);
}
printf("Found device %s\n",hdGetString(HD_DEVICE_MODEL_TYPE));
hdEnable(HD_FORCE_OUTPUT);
hdEnable(HD_MAX_FORCE_CLAMPING);
hdStartScheduler();
if (HD_DEVICE_ERROR(error = hdGetError()))
{
hduPrintError(stderr, &error, "Failed to start scheduler");
fprintf(stderr, "\nPress any key to quit.\n");
getchar();
exit(-1);
}
initGlut(argc, argv);
// Get the workspace dimensions.
HDdouble maxWorkspace[6];
hdGetDoublev(HD_MAX_WORKSPACE_DIMENSIONS, maxWorkspace);
// Low/left/back point of device workspace.
hduVector3Dd LLB(maxWorkspace[0], maxWorkspace[1], maxWorkspace[2]);
// Top/right/front point of device workspace.
hduVector3Dd TRF(maxWorkspace[3], maxWorkspace[4], maxWorkspace[5]);
initGraphics(LLB, TRF);
// Application loop.
CoulombForceField();
delete[] Shape;
printf("Done\n");
return 0;
}
/******************************************************************************/