/
drawUtils.cpp
337 lines (293 loc) · 11.6 KB
/
drawUtils.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
#include "drawUtils.h"
void drawCylinder(const btTransform& tr, float half_height, float r) {
glPushMatrix();
btScalar transform[16];
tr.getOpenGLMatrix(transform);
glMultMatrixf((float*) transform);
double cylinder[4][3] = { {-half_height-1.0, 0.0, 0.0} , {-half_height, 0.0, 0.0} , {half_height, 0.0, 0.0} ,
{half_height+1.0, 0.0, 0.0} };
glePolyCylinder(4, cylinder, NULL, r);
glPopMatrix();
}
void drawSphere(const btTransform& tr, float radius) {
glPushMatrix();
glTranslated(tr.getOrigin().getX(), tr.getOrigin().getY(), tr.getOrigin().getZ());
glutSolidSphere(radius, 20, 16);
glPopMatrix();
}
void drawCapsule(btCollisionObject* col_obj, bool include_repulsion_dist)
{
btCapsuleShapeX* capsule_shape = static_cast<btCapsuleShapeX*>(col_obj->getCollisionShape());
glPushMatrix();
btScalar transform[16];
col_obj->getWorldTransform().getOpenGLMatrix(transform);
glMultMatrixf((float*) transform);
float half_height = capsule_shape->getHalfHeight();
float radius = capsule_shape->getRadius() - ((include_repulsion_dist) ? 0.0 : REPULSION_DIST);
double cylinder[4][3] = { {-half_height-1.0, 0.0, 0.0} , {-half_height, 0.0, 0.0} , {half_height, 0.0, 0.0} ,
{half_height+1.0, 0.0, 0.0} };
#ifdef PICTURE
gleSetNumSides(60);
#endif
glePolyCylinder(4, cylinder, NULL, radius);
glTranslated(-half_height, 0.0, 0.0);
glutSolidSphere(radius, 20, 16);
glTranslated(2.0*half_height, 0.0, 0.0);
glutSolidSphere(radius, 20, 16);
glPopMatrix();
}
void drawCylinder(Vector3d pos, Matrix3d rot, double h, double r) {
glPushMatrix();
double transform[16] = { rot(0,0) , rot(1,0) , rot(2,0) , 0 ,
rot(0,1) , rot(1,1) , rot(2,1) , 0 ,
rot(0,2) , rot(1,2) , rot(2,2) , 0 ,
pos(0) , pos(1) , pos(2) , 1 };
glMultMatrixd(transform);
double cylinder[4][3] = { {-h-1.0, 0.0, 0.0} , {-h, 0.0, 0.0} , {0.0, 0.0, 0.0} ,
{1.0, 0.0, 0.0} };
glePolyCylinder(4, cylinder, NULL, r);
glPopMatrix();
}
// height has opposite sign as above
/*void drawCylinderD(Vector3d pos, Matrix3d rot, float radius, float height, float color0, float color1, float color2) {
glPushMatrix();
glColor3f(color0, color1, color2);
Vector3d vector_array[4];
double point_array[4][3];
Vector3d a = pos;
Vector3d b = pos + height * rot.col(0);
vector_array[0] = a - (b - a);
vector_array[1] = a;
vector_array[2] = b;
vector_array[3] = b + (b - a);
for (int pt_ind = 0; pt_ind < 4; pt_ind++)
{
point_array[pt_ind][0] = vector_array[pt_ind](0);
point_array[pt_ind][1] = vector_array[pt_ind](1);
point_array[pt_ind][2] = vector_array[pt_ind](2);
}
glePolyCylinder(4, point_array, NULL, radius);
glPopMatrix();
}*/
void drawCylinder(Vector3d start_pos, Vector3d end_pos, double r)
{
glPushMatrix();
Vector3d vector_array[4];
double point_array[4][3];
vector_array[0] = start_pos - (end_pos - start_pos);
vector_array[1] = start_pos;
vector_array[2] = end_pos;
vector_array[3] = end_pos + (end_pos - start_pos);
for (int pt_ind = 0; pt_ind < 4; pt_ind++)
{
point_array[pt_ind][0] = vector_array[pt_ind](0);
point_array[pt_ind][1] = vector_array[pt_ind](1);
point_array[pt_ind][2] = vector_array[pt_ind](2);
}
glePolyCylinder(4, point_array, NULL, r);
glPopMatrix();
}
void drawEndEffector(Vector3d pos, Matrix3d rot, double degrees, float color0, float color1, float color2) {
glPushMatrix();
double transform[16] = { rot(0,0) , rot(1,0) , rot(2,0) , 0 ,
rot(0,1) , rot(1,1) , rot(2,1) , 0 ,
rot(0,2) , rot(1,2) , rot(2,2) , 0 ,
pos(0) , pos(1) , pos(2) , 1 };
glMultMatrixd(transform);
int pieces = 6;
double h = 1.5;
double start = -3;
double handle_r = 1.2;
double end = ((double) pieces)*h + start;
double grab_offset = 12.0;
glColor3f(0.3, 0.3, 0.0);
double cylinder[4][3] = { {grab_offset-4.0, 0.0, 0.0} , {grab_offset-3.0, 0.0, 0.0} , {grab_offset, 0.0, 0.0} ,
{grab_offset+1.0, 0.0, 0.0} };
glePolyCylinder(4, cylinder, NULL, 1.6);
glColor3f(color0, color1, color2);
double grip_handle[4][3] = { {end-1.0, 0.0, 0.0} , {end, 0.0, 0.0} , {end+30.0, 0.0, 0.0} ,
{end+31.0, 0.0, 0.0} };
glePolyCylinder(4, grip_handle, NULL, handle_r);
glTranslatef(end, 0.0, 0.0);
glRotatef(-degrees, 0.0, 0.0, 1.0);
glTranslatef(-end, 0.0, 0.0);
Matrix3d open_rot = (Matrix3d) Eigen::AngleAxisd(-degrees*M_PI/180.0, rot*Vector3d::UnitZ());
Vector3d new_pos;
Matrix3d new_rot;
for (int piece=0; piece<pieces; piece++) {
double r = 0.5+((double) piece)*((0.8*handle_r)-0.5)/((double) pieces-1);
gleDouble grip_tip[4][3] = { {start+((double) piece)*h-1.0, r, 0.0} , {start+((double) piece)*h, r, 0.0} , {start+((double) piece+1)*h, r, 0.0} , {start+((double) piece+1)*h+1.0, r, 0.0} };
glePolyCylinder(4, grip_tip, NULL, r);
new_rot = open_rot * rot;
new_pos = open_rot * rot * Vector3d(-end, 0.0, 0.0) + rot * Vector3d(end, 0.0, 0.0) + pos;
}
glTranslatef(end, 0.0, 0.0);
glRotatef(2*degrees, 0.0, 0.0, 1.0);
glTranslatef(-end, 0.0, 0.0);
for (int piece=0; piece<pieces; piece++) {
double r = 0.5+((double) piece)*((0.8*handle_r)-0.5)/((double) pieces-1);
gleDouble grip_tip[4][3] = { {start+((double) piece)*h-1.0, -r, 0.0} , {start+((double) piece)*h, -r, 0.0} , {start+((double) piece+1)*h, -r, 0.0} , {start+((double) piece+1)*h+1.0, -r, 0.0} };
glePolyCylinder(4, grip_tip, NULL, r);
new_rot = open_rot.transpose() * rot;
new_pos = open_rot.transpose() * rot * Vector3d(-end, 0.0, 0.0) + rot * Vector3d(end, 0.0, 0.0) + pos;
}
glPopMatrix();
}
void drawGrip(Vector3d pos, Matrix3d rot, double radius, double degrees, float color0, float color1, float color2) {
glPushMatrix();
double transform[16] = { rot(0,0) , rot(1,0) , rot(2,0) , 0 ,
rot(0,1) , rot(1,1) , rot(2,1) , 0 ,
rot(0,2) , rot(1,2) , rot(2,2) , 0 ,
pos(0) , pos(1) , pos(2) , 1 };
glMultMatrixd(transform);
glRotated(-90,0,0,1);
glColor3f(color0, color1, color2);
double grip_handle[4][3] = { {0.0, 9.0, 0.0} , {0.0,11.0, 0.0} , {0.0,19.0, 0.0} ,
{0.0,21.0, 0.0} };
glePolyCylinder(4, grip_handle, NULL, 1);
glTranslatef(0.0, 11.0, 0.0);
glRotatef(-degrees, 1.0, 0.0, 0.0);
glTranslatef(0.0, -11.0, 0.0);
double grip_tip0[4][3] = { {0.0, 0.0,-2.0} , {0.0, 0.0, 0.0} , {0.0, 0.0, 2.0} ,
{0.0, 0.0, 4.0} };
glePolyCylinder(4, grip_tip0, NULL, 0.95*radius);
double grip_side0[8][3] = { {0.0, 0.0,-2.0} , {0.0, 0.0, 0.0} , {0.0, 0.0, 2.0} ,
{0.0, 3.0, 5.0} , {0.0, 6.0, 5.0} , {0.0,11.0, 0.0} ,
{0.0,13.0, 0.0} };
glePolyCylinder(8, grip_side0, NULL, 1);
glTranslatef(0.0, 11.0, 0.0);
glRotatef(2*degrees, 1.0, 0.0, 0.0);
glTranslatef(0.0, -11.0, 0.0);
double grip_tip1[4][3] = { {0.0, 0.0, 2.0} , {0.0, 0.0, 0.0} , {0.0, 0.0,-2.0} ,
{0.0, 0.0,-4.0} };
glePolyCylinder(4, grip_tip1, NULL, 0.95*radius);
double grip_side1[8][3] = { {0.0, 0.0, 2.0} , {0.0, 0.0, 0.0} , {0.0, 0.0,-2.0} ,
{0.0, 3.0,-5.0} , {0.0, 6.0,-5.0} , {0.0,11.0, 0.0} ,
{0.0,13.0, 0.0} };
glePolyCylinder(8, grip_side1, NULL, 1);
glPopMatrix();
}
void drawSphere(Vector3d position, float radius) {
glPushMatrix();
glTranslated(position(0), position(1), position(2));
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glEnable(GL_COLOR_MATERIAL);
glutSolidSphere(radius, 20, 16);
//glFlush ();
glPopMatrix();
}
void drawCursor(double proxyxform[16], float color) {
static const double kCursorRadius = 0.5;
static const double kCursorHeight = 1.5;
static const int kCursorTess = 4;
GLUquadricObj *qobj = 0;
glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT);
glPushMatrix();
if (!gCursorDisplayList) {
gCursorDisplayList = glGenLists(1);
glNewList(gCursorDisplayList, GL_COMPILE);
qobj = gluNewQuadric();
gluCylinder(qobj, 0.0, kCursorRadius, kCursorHeight,
kCursorTess, kCursorTess);
glTranslated(0.0, 0.0, kCursorHeight);
gluCylinder(qobj, kCursorRadius, 0.0, kCursorHeight / 5.0,
kCursorTess, kCursorTess);
gluDeleteQuadric(qobj);
glEndList();
}
// Get the proxy transform in world coordinates
glMultMatrixd(proxyxform);
// Apply the local cursor scale factor.
glScaled(gCursorScale, gCursorScale, gCursorScale);
glEnable(GL_COLOR_MATERIAL);
glColor3f(0.0, color, 1.0);
glCallList(gCursorDisplayList);
glPopMatrix();
glPopAttrib();
}
void drawAxes(Vector3d pos, Matrix3d rot) {
glPushMatrix();
double transform[] = { rot(0,0) , rot(1,0) , rot(2,0) , 0 ,
rot(0,1) , rot(1,1) , rot(2,1) , 0 ,
rot(0,2) , rot(1,2) , rot(2,2) , 0 ,
pos(0) , pos(1) , pos(2) , 1 };
glMultMatrixd(transform);
glBegin(GL_LINES);
glEnable(GL_LINE_SMOOTH);
glColor3d(1.0, 0.0, 0.0); //red
glVertex3f(0.0, 0.0, 0.0); //x
glVertex3f(10.0, 0.0, 0.0);
glColor3d(0.0, 1.0, 0.0); //green
glVertex3f(0.0, 0.0, 0.0); //y
glVertex3f(0.0, 10.0, 0.0);
glColor3d(0.0, 0.0, 1.0); //blue
glVertex3f(0.0, 0.0, 0.0); //z
glVertex3f(0.0, 0.0, 10.0);
glEnd();
glPopMatrix();
}
void labelAxes(Vector3d pos, Matrix3d rot) {
glPushMatrix();
Vector3d diff_pos = pos;
double rotation_scale_factor = 20.0;
Matrix3d rotations_project = rot*rotation_scale_factor;
void * font = GLUT_BITMAP_HELVETICA_18;
glColor3d(1.0, 0.0, 0.0); //red
//glRasterPos3i(20.0, 0.0, -1.0);
glRasterPos3i((float)(diff_pos(0)+rotations_project(0,0)), (float)(diff_pos(1)+rotations_project(1,0)), (float)(diff_pos(2)+rotations_project(2,0)));
glutBitmapCharacter(font, 'X');
glColor3d(0.0, 1.0, 0.0); //red
//glRasterPos3i(0.0, 20.0, -1.0);
glRasterPos3i((float)(diff_pos(0)+rotations_project(0,1)), (float)(diff_pos(1)+rotations_project(1,1)), (float)(diff_pos(2)+rotations_project(2,1)));
glutBitmapCharacter(font, 'Y');
glColor3d(0.0, 0.0, 1.0); //red
//glRasterPos3i(-1.0, 0.0, 20.0);
glRasterPos3i((float)(diff_pos(0)+rotations_project(0,2)), (float)(diff_pos(1)+rotations_project(1,2)), (float)(diff_pos(2)+rotations_project(2,2)));
glutBitmapCharacter(font, 'Z');
glPopMatrix();
}
void drawArrow(Vector3d pos, Vector3d direction) {
double norm = direction.norm();
Matrix3d rot;
rotation_from_tangent(direction.normalized(), rot);
glPushMatrix();
double transform[16] = { rot(0,0) , rot(1,0) , rot(2,0) , 0 ,
rot(0,1) , rot(1,1) , rot(2,1) , 0 ,
rot(0,2) , rot(1,2) , rot(2,2) , 0 ,
pos(0), pos(1), pos(2), 1};
glMultMatrixd(transform);
glBegin(GL_LINES);
// glEnable(GL_LINE_SMOOTH);
// glEnable(GL_COLOR_MATERIAL);
// glColor3d(color0, color1, color2);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(norm,0,0);
glEnd();
glTranslated(norm-0.2*norm,0,0);
glRotated(90,0,1,0);
glutSolidCone(0.05*norm,0.2*norm,20,16);
glPopMatrix();
}
void drawPlane(Vector3d pos, Vector3d normal, float side) {
Matrix3d rot;
rotation_from_tangent(normal.normalized(), rot);
Vector3d x = rot.col(1);
Vector3d y = rot.col(2);
glPushMatrix();
glBegin(GL_QUADS);
glVertex3f(pos(0) + side*(x(0)+y(0)), pos(1) + side*(x(1)+y(1)), pos(2) + side*(x(2)+y(2)));
glVertex3f(pos(0) + side*(x(0)-y(0)), pos(1) + side*(x(1)-y(1)), pos(2) + side*(x(2)-y(2)));
glVertex3f(pos(0) + side*(-x(0)-y(0)), pos(1) + side*(-x(1)-y(1)), pos(2) + side*(-x(2)-y(2)));
glVertex3f(pos(0) + side*(-x(0)+y(0)), pos(1) + side*(-x(1)+y(1)), pos(2) + side*(-x(2)+y(2)));
glEnd();
glPopMatrix();
}
void printText(float x, float y, const char *string)
{
int len, i;
glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, string[i]);
}
}