Debian Codegraph @

vcg/wrap/gui/trackutils.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 /****************************************************************************
24 History
25
26 $Log: trackutils.h,v $
27 Revision 1.14 2008/02/26 18:55:55 ponchio
28 removed some garbage.
29
30 Revision 1.13 2008/02/26 18:46:55 ponchio
31 Fixed bug in drawing position of the trackball when changin center.
32
33 Revision 1.12 2008/02/24 18:10:54 ponchio
34 Fixed scale behaviour.
35
36 Revision 1.11 2008/02/24 18:05:08 ponchio
37 Should work as before. I didn't test cylinder and other exotic modes.
38
39 Revision 1.10 2008/02/24 14:37:00 ponchio
40 Restored trackball functionality. Not very much tested, and code will need some
41 cleanup.
42
43 Revision 1.9 2008/02/22 18:57:47 benedetti
44 first attempt to correct after quaternion ToMatrix() inversion (does not work yet)
45
46 Revision 1.8 2008/02/15 20:54:45 benedetti
47 removed some variable initialization related warning
48
49 Revision 1.7 2007/10/22 14:39:54 cignoni
50 corrected bug into the drawsphere (thanks to nico and guido!)
51
52 Revision 1.6 2007/08/17 09:19:40 cignoni
53 glEnable (GL_LINE_SMOOTH) should go before changing the linewidth.
54
55 Revision 1.5 2007/07/14 12:44:40 benedetti
56 Minor edits in Doxygen documentation.
57
58 Revision 1.4 2007/07/09 22:41:22 benedetti
59 Added Doxygen documentation, removed using namespace std, some other minor changes.
60
61 Revision 1.3 2007/06/12 08:58:08 benedetti
62 Minor fix in DrawUglyCylinderMode()
63
64 Revision 1.2 2007/05/28 08:10:47 fiorin
65 Removed type cast warnings
66
67 Revision 1.1 2007/05/15 14:57:34 benedetti
68 Utility functions for the trackmodes, first version
69
70 ****************************************************************************/
71
72 #ifndef TRACKUTILS_H
73 #define TRACKUTILS_H
74
75 #include <assert.h>
76 #include <vcg/math/base.h>
77 #include <vcg/math/similarity.h>
78 #include <vcg/space/intersection3.h>
79 #include <vcg/space/line3.h>
80 #include <vcg/space/plane3.h>
81 #include <wrap/gl/math.h>
82 #include <wrap/gl/space.h>
83 #include <vector>
84 #include <iostream>
85 using namespace std;
86
87 namespace vcg {
88
89 /*!
90 @brief This namespace contains some support functions used by TrackMode subclassess.
91
92 \warning Many of these functions shouldn't be here and are supposed to be moved to some appropriate place by the library administrator.
93 \warning The \e DrawUgly series of functions is a \b TEMPORARY solution, used while waiting for the \e DrawBeautiful series...
94 */
95 namespace trackutils {
96
97 /*!
98 @brief Compute the plane perpedicular to view dir and passing through the manipulator center.
99
100 @param camera the camera of the manipulator.
101 @param center the center of the manipulator.
102 @return the plane perpedicular to view dir and passing through the manipulator center.
103 */
104 Plane3f GetViewPlane (const View < float >&camera, const Point3f & center)
105 {
106 Point3f vp = camera.ViewPoint ();
107 Plane3f pl;
108 Point3f plnorm = vp - center;
109 plnorm.Normalize ();
110 pl.Set (plnorm, plnorm * center);
111 return pl;
112 }
113
114 /*!
115 @brief Convert a line to a normalized ray.
116
117 @param l the line to be converted.
118 @return the normalized ray.
119 */
120 Ray3f line2ray(const Line3f &l){
121 Ray3f r(l.Origin(),l.Direction());
122 r.Normalize();
123 return r;
124 }
125
126
127 /*!
128 @brief Project a window coordinate point on the plane perpedicular to view dir and passing through the manipulator center.
129
130 @param tb the manipulator.
131 @param p the window coordinate point.
132 @return p's projection on plane perpedicular to view dir and passing through the manipulator center.
133 */
134 Point3f HitViewPlane (Trackball * tb, const Point3f & p)
135 {
136 Plane3f vp = GetViewPlane (tb->camera, tb->center);
137 Line3fN ln = tb->camera.ViewLineFromWindow (Point3f (p[0], p[1], 0));
138 Point3f PonVP;
139 IntersectionLinePlane < float >(vp, ln, PonVP);
140 return PonVP;
141 }
142
143 // nota: non ho scritto io questa funzione,
144 // quindi la documentazione doxy potrebbe non essere accurata al 100%.
145 /*!
146 @brief Project a window coordinate point on the rotational hyperboloid relative to the manipulator.
147
148 <br>The original documentation (in italian) follows:
149 <pre>
150 dato un punto in coordinate di schermo e.g. in pixel stile opengl
151 calcola il punto di intersezione tra la viewline che passa per
152 viewpoint e per hitplane e l'iperboloide.
153 l'iperboloide si assume essere quello di rotazione attorno alla
154 retta viewpoint-center e di raggio rad
155 si assume come sistema di riferimento quello con l'origine
156 su center ecome x la retta center-viewpoint
157
158 eq linea
159 hitplane.y
160 y = - ----------- * x + hitplane.y
161 viewpoint.x
162
163 eq hiperboloide di raggio r (e.g. che passa per (r/sqrt2,r/sqrt2)
164
165 1
166 y = --- * (r^2 /2.0)
167 x
168
169 hitplane.y
170 ----------- * x^2 - hitplane.y *x + (r^2/2.0) == 0
171 viewpoint.x
172 </pre>
173
174 @param center the center of the manipulator.
175 @param radius the radius of the manipulator.
176 @param viewpoint the view point.
177 @param vp the view plane.
178 @param hitplane the projection of the window coordinate point on the view plane.
179 @param hit the projection of hitplane on the rotational hyperboloid relative to the manipulator.
180 @return true if and only if hit is valid.
181 */
182 bool HitHyper (Point3f center, float radius, Point3f viewpoint, Plane3f vp,
183 Point3f hitplane, Point3f & hit)
184 {
185 float hitplaney = Distance (center, hitplane);
186 float viewpointx = Distance (center, viewpoint);
187
188 float a = hitplaney / viewpointx;
189 float b = -hitplaney;
190 float c = radius * radius / 2.0f;
191 float delta = b * b - 4 * a * c;
192 float x1, x2, xval, yval;
193
194 if (delta > 0) {
195 x1 = (-b - sqrt (delta)) / (2.0f * a);
196 x2 = (-b + sqrt (delta)) / (2.0f * a);
197
198 xval = x1; // always take the minimum value solution
199 yval = c / xval; // alternatively it also could be the other part of the equation yval=-(hitplaney/viewpointx)*xval+hitplaney;
200 }
201 else {
202 return false;
203 }
204 // Computing the result in 3d space;
205 Point3f dirRadial = hitplane - center;
206 dirRadial.Normalize ();
207 Point3f dirView = vp.Direction ();
208 dirView.Normalize ();
209 hit = center + dirRadial * yval + dirView * xval;
210 return true;
211 }
212
213 // nota: non ho scritto io questa funzione,
214 // quindi la documentazione doxy potrebbe non essere accurata al 100%.
215 /*!
216 @brief Project a window coordinate point on the sphere relative to the manipulator.
217
218 <br>The original documentation (in italian) follows:
219 <pre>
220 dato un punto in coordinate di schermo e.g. in pixel stile opengl
221 restituisce un punto in coordinate di mondo sulla superficie
222 della trackball.
223 La superficie della trackball e' data da una sfera + una porzione
224 di iperboloide di rotazione.
225 Assumiamo la sfera di raggio unitario e centrata sull'origine e
226 di guardare lungo la y negativa.
227
228 X 0 sqrt(1/2) 1
229 eq sfera: y=sqrt(1-x*x); 1 sqrt(1/2) 0
230 eq iperboloide : y=1/2*x; inf sqrt(1/2) 1/2
231 eq cono y=x+sqrt(2);
232 </pre>
233
234 @param tb the manipulator.
235 @param p the window coordinate point.
236 @return the projection of p on the sphere relative to the manipulator.
237 */
238 Point3f HitSphere (Trackball * tb, const Point3f & p)
239 {
240 Point3f center = tb->center;
241 Line3fN ln = tb->camera.ViewLineFromWindow (Point3f (p[0], p[1], 0));
242 Plane3f vp = GetViewPlane (tb->camera, center);
243 Point3f hitPlane(0,0,0), //intersection view plane with point touched
244 hitSphere(0,0,0),
245 hitSphere1(0,0,0),
246 hitSphere2(0,0,0),
247 hitHyper(0,0,0);
248 IntersectionLinePlane < float >(vp, ln, hitPlane);
249
250 Sphere3f sphere (center, tb->radius);//trackball sphere
251 bool resSp = IntersectionLineSphere < float >(sphere, ln, hitSphere1, hitSphere2);
252
253 Point3f viewpoint = tb->camera.ViewPoint ();
254 if (resSp == true) {
255 if (Distance (viewpoint, hitSphere1) < Distance (viewpoint, hitSphere2))
256 hitSphere = hitSphere1;
257 else
258 hitSphere = hitSphere2;
259 }
260
261 /*float dl= */ Distance (ln, center);
262 bool resHp = HitHyper (center, tb->radius, viewpoint, vp, hitPlane, hitHyper);
263
264 // four cases
265
266 // 1) Degenerate line tangent to both sphere and hyperboloid!
267 if ((!resSp && !resHp)) {
268 Point3f hit = ClosestPoint (ln, center);
269 //printf("closest point to line %f\n",Distance(hit,tb->center));
270 return hit;
271 }
272 if ((resSp && !resHp))
273 return hitSphere; // 2) line cross only the sphere
274 if ((!resSp && resHp))
275 return hitHyper; // 3) line cross only the hyperboloid
276
277 // 4) line cross both sphere and hyperboloid: choose according angle.
278 float angleDeg = math::ToDeg (Angle ((viewpoint - center), (hitSphere - center)));
279
280 //printf("Angle %f (%5.2f %5.2f %5.2f) (%5.2f %5.2f %5.2f)\n",angleDeg,hitSphere[0],hitSphere[1],hitSphere[2],hitHyper[0],hitHyper[1],hitHyper[2]);
281 if (angleDeg < 45)
282 return hitSphere;
283 else
284 return hitHyper;
285
286 // Codice ORIGINALE PONCHIO
287 //vp.SetOffset(vp.Offset() + Thr);
288
289 //Point3f hit;
290 //bool res = Intersection<float>(vp, ln, hit);
291 //float d = Distance(tb->center - vn.Direction()*Thr, hit);
292 //if(d < Thr) {
293 // Point3f hit2;
294 // Sphere3f sphere(tb->center, tb->radius);
295 // bool res = Intersection<float>(sphere, ln, hit, hit2);
296
297 // //find closest intersection to sphere
298 // float d = (hit - viewpoint).Norm();
299 // float d2 = (hit2 - viewpoint).Norm();
300 // if(d > d2) hit = hit2;
301 // hit -= tb->center;
302 //} else {
303 // if(d > 2.99 * Thr)
304 // d = 2.99 * Thr;
305 // Point3f norm = (hit - tb->center)^(viewpoint - tb->center);
306 // norm.Normalize();
307 // float phi = -M_PI/4 - 3*M_PI/8 *(d - Thr)/Thr;
308
309 // Quaternionf q(phi, norm);
310 // hit = q.Rotate((viewpoint - tb->center).Normalize() * tb->radius);
311 //}
312 // hit.Normalize();
313 // return hit;
314 }
315
316 /*!
317 @brief Calculates the minimal distance between 2 lines.
318
319 P and Q are the lines, P_s and Q_t are set to be the closest points on these lines.
320
321 it's returned the distance from P_s and Q_t, and a boolean value which is true
322 if the lines are parallel enough.
323
324 if P and Q are parallel P_s and Q_t aren't set.
325
326 the formula is taken from pages 81-83 of
327 <em>"Eric Lengyel - Mathematics for 3D Game Programming & Computer Graphics"</em>
328 @param P the first line.
329 @param Q the second line.
330 @param P_s the point on P closest to Q.
331 @param Q_t the point on Q closest to P.
332 @return a std::pair made with the distance from P_s to Q_t and a boolean value, true if and only if P and Q are almost parallel.
333 */
334 std::pair< float, bool > LineLineDistance(const Line3f & P,const Line3f & Q,Point3f & P_s, Point3f & Q_t){
335 Point3f p0 = P.Origin (), Vp = P.Direction ();
336 Point3f q0 = Q.Origin (), Vq = Q.Direction ();
337 float VPVP = Vp * Vp;
338 float VQVQ = Vq * Vq;
339 float VPVQ = Vp * Vq;
340 const float det = ( VPVP * VQVQ ) - ( VPVQ * VPVQ );
341 const float EPSILON = 0.00001f;
342 if ( fabs(det) < EPSILON ) {
343 return std::make_pair(Distance(P,q0), true);
344 }
345 float b1= (q0 - p0) * Vp;
346 float b2= (p0 - q0) * Vq;
347 float s = ( (VQVQ * b1) + (VPVQ * b2) ) / det;
348 float t = ( (VPVQ * b1) + (VPVP * b2) ) / det;
349 P_s = p0 + (Vp * s);
350 Q_t = q0 + (Vq * t);
351 return std::make_pair(Distance(P_s,Q_t),false);
352 }
353
354 /*!
355 @brief Calculates the minimal distance between a ray and a line.
356
357 R is the ray and Q is the line, R_s and Q_t are set to be the closest points on
358 the ray and the line.
359
360 it's returned the distance from R_s and Q_t, and a boolean value which is true
361 if the ray and the line are parallel enough.
362
363 if R and Q are parallel R_s and Q_t aren't set.
364 @param R the ray.
365 @param Q the line.
366 @param R_s the point on R closest to Q.
367 @param Q_t the point on Q closest to R.
368 @return a std::pair made with the distance from R_s to Q_t and a boolean value, true if and only if P and Q are almost parallel.
369 */
370 std::pair< float, bool > RayLineDistance(const Ray3f & R,const Line3f & Q,Point3f & R_s, Point3f & Q_t){
371 Point3f r0 = R.Origin (), Vr = R.Direction ();
372 Point3f q0 = Q.Origin (), Vq = Q.Direction ();
373 float VRVR = Vr * Vr;
374 float VQVQ = Vq * Vq;
375 float VRVQ = Vr * Vq;
376 const float det = ( VRVR * VQVQ ) - ( VRVQ * VRVQ );
377 const float EPSILON = 0.00001f;
378 if ( ( det >= 0.0f ? det : -det) < EPSILON ) {
379 return std::make_pair(Distance(Q,r0), true);
380 }
381 float b1= (q0 - r0) * Vr;
382 float b2= (r0 - q0) * Vq;
383 float s = ( (VQVQ * b1) + (VRVQ * b2) ) / det;
384 float t = ( (VRVQ * b1) + (VRVR * b2) ) / det;
385 if(s<0){
386 R_s = r0;
387 Q_t = ClosestPoint(Q,R_s);
388 }else {
389 R_s = r0 + (Vr * s);
390 Q_t = q0 + (Vq * t);
391 }
392 return std::make_pair(Distance(R_s,Q_t),false);
393 }
394
395 /*!
396 @brief Calculates the minimal distance between 2 segments.
397
398 R e Q are the segments, R_s and Q_t are set to be the closest points on
399 the segments.
400
401 it's returned the distance from R_s and Q_t, and a boolean value which is true
402 if the segments are parallel enough.
403 @param R the first segment.
404 @param Q the second segment.
405 @param R_s the point on R closest to Q.
406 @param Q_t the point on Q closest to R.
407 @return a std::pair made with the distance from R_s to Q_t and a boolean value, true if and only if P and Q are almost parallel.
408 */
409 std::pair< float, bool > SegmentSegmentDistance(const Segment3f & R, const Segment3f & Q, Point3f & R_s, Point3f & Q_t)
410 {
411 float R_len=Distance(R.P0(),R.P1());
412 float Q_len=Distance(Q.P0(),Q.P1());
413 const float EPSILON_LENGTH = std::max(R_len,Q_len)*0.0001f;
414 if(R_len < EPSILON_LENGTH){
415 R_s=R.P0();
416 Q_t=ClosestPoint(Q,R_s);
417 return std::make_pair(Distance(R_s,Q_t),true);
418 }
419 if( Q_len < EPSILON_LENGTH){
420 Q_t=Q.P0();
421 R_s=ClosestPoint(R,Q_t);
422 return std::make_pair(Distance(R_s,Q_t),true);
423 }
424 Point3f r0 = R.P0(), Vr = (R.P1()-R.P0()).Normalize();
425 Point3f q0 = Q.P0(), Vq = (Q.P1()-Q.P0()).Normalize();
426 float VRVR = Vr * Vr;
427 float VQVQ = Vq * Vq;
428 float VRVQ = Vr * Vq;
429 const float det = ( VRVR * VQVQ ) - ( VRVQ * VRVQ );
430 const float EPSILON = 0.00001f;
431 if ( ( det >= 0.0f ? det : -det) < EPSILON ) {
432 Line3f lR(R.P0(),R.P1());
433 float qa=lR.Projection(Q.P0());
434 float qb=lR.Projection(Q.P1());
435 if( (qa<=0.0f) && qb<=(0.0f)){
436 R_s=R.P0();
437 Q_t=ClosestPoint(Q,R_s);
438 } else if ( (qa >= 1.0f) && (qb >= 1.0f) ){
439 R_s=R.P1();
440 Q_t=ClosestPoint(Q,R_s);
441 } else {
442 if( (qa >= 0.0f) && (qa <= 1.0f) ){
443 Q_t=Q.P0();
444 R_s=ClosestPoint(R,Q_t);
445 } else if((qb >= 0.0f) && (qb <= 1.0f) ){
446 Q_t=Q.P1();
447 R_s=ClosestPoint(R,Q_t);
448 } else {
449 if( ((qa<=0.0f)&&(qb>=1.0f) ||((qb<=0.0f)&&(qa>=1.0f)))){
450 R_s=R.P0();
451 Q_t=ClosestPoint(Q,R_s);
452 }else{
453 assert(0);
454 }
455 }
456 }
457 return std::make_pair(Distance(R_s,Q_t),true);
458 }
459 float b1= (q0 - r0) * Vr;
460 float b2= (r0 - q0) * Vq;
461 float s = ( (VQVQ * b1) + (VRVQ * b2) ) / det;
462 float t = ( (VRVQ * b1) + (VRVR * b2) ) / det;
463 if( s < 0 ){
464 R_s = R.P0();
465 }else if ( s > R_len ){
466 R_s = R.P1();
467 } else {
468 R_s = r0 + (Vr * s);
469 }
470 if( t < 0){
471 Q_t = Q.P0();
472 }else if ( t > Q_len ){
473 Q_t = Q.P1();
474 }else{
475 Q_t = q0 + (Vq * t);
476 }
477 return std::make_pair(Distance(R_s,Q_t),false);
478 }
479
480 /*!
481 @brief Compute the point on a line closest to the ray projection of a window coordinate point.
482
483 Given a window coordinate point, computes a ray starting from the manipulator
484 camera eye and passing through the point's projection on the viewplane, then uses RayLineDistance()
485 to get the closest point to ray on a given line.
486 @see RayLineDistance(const Ray3f & R,const Line3f & Q,Point3f & R_s, Point3f & Q_t)
487 @param tb the manipulator.
488 @param axis the axis.
489 @param point the window coordinate point.
490 @return a std::pair made with the point on axis closest to the ray projection of point and a boolean true if and only if the ray doesn't diverges respect to the axis.
491 */
492 std::pair< Point3f,bool > HitNearestPointOnAxis (Trackball * tb,Line3f axis, Point3f point)
493 {
494 Ray3fN ray = line2ray(tb->camera.ViewLineFromWindow (point));
495 Point3f axis_p(0,0,0), ray_p(0,0,0);
496 std::pair< float, bool > resp=RayLineDistance(ray,axis,ray_p,axis_p);
497 if(resp.second || (ray_p == ray.Origin())){
498 return std::make_pair(Point3f(0,0,0),false);
499 }
500 return std::make_pair(axis_p,true);
501 }
502
503 /*!
504 @brief Project a line into a plane.
505
506 Given a line and a plane, returns the line projection on the plane.
507
508 The line returned is \e not normalized.
509 @param ln the line.
510 @param pl the plane.
511 @return the (non normalized) line projected.
512 */
513 Line3f ProjectLineOnPlane(const Line3f & ln, const Plane3f & pl)
514 {
515 Point3f l0=ln.Origin();
516 Point3f l1=l0+ln.Direction();
517 Point3f p1,p2;
518 p1=pl.Projection(l0);
519 p2=pl.Projection(l1);
520 Line3f res(p1,p2-p1);
521 return res;
522 }
523
524 /*!
525 @brief Computes a signed line-point distance.
526
527 Given a line, a point and a positivity direction, computes the signed distance between the line and the point.
528 @param line the line.
529 @param pt the point.
530 @param positive_dir the positivity direction.
531 @return the signed distance.
532 */
533 float signedDistance(Line3f line,Point3f pt,Point3f positive_dir)
534 {
535 return Distance(line,pt) * ((((pt-ClosestPoint(line,pt)) * positive_dir) >= 0.0f )? 1.0f: -1.0f);
536 }
537
538 /*!
539 @brief Computes the verical component of an user mouse drag.
540
541 @param tb the manipulator.
542 @param new_point the new mouse pointer coordinate.
543 @return The verical component of the user mouse drag.
544 */
545 float getDeltaY(Trackball * tb, Point3f new_point)
546 {
547 float ScreenHeight = float (tb->camera.viewport[3] - tb->camera.viewport[1]);
548 return (new_point[1] - tb->last_point[1]) / ScreenHeight;
549 }
550
551 /*!
552 @brief Computes the intersection between a ray and a plane.
553
554 @param pl the plane.
555 @param ray the ray.
556 @param po the intersection point.
557 @return true if and only if there is intersection (po is valid).
558 */
559 template<class T>
560 inline bool IntersectionRayPlane( const Plane3<T> & pl, const Ray3<T> & ray, Point3<T> &po){
561 const T epsilon = T(1e-8);
562
563 T k = pl.Direction() * ray.Direction(); // Compute 'k' factor
564 if( (k > -epsilon) && (k < epsilon))
565 return false;
566 T r = (pl.Offset() - pl.Direction()*ray.Origin())/k; // Compute ray distance
567 if (r < 0)
568 return false;
569 po = ray.Origin() + ray.Direction()*r;
570 return true;
571 }
572
573 /*!
574 @brief Project a window coordinate point on a plane.
575
576 Given a window coordinate point, computes a ray starting from the manipulator
577 camera eye and passing through the point's projection on the viewplane, then uses IntersectionRayPlane()
578 to get the ray intersection with a given plane.
579
580 @see IntersectionRayPlane()
581 @param tb the manipulator.
582 @param point the window coordinate point.
583 @param plane the plane.
584 @return a std::pair made with p's projection on the.plane and a boolean true if and only if the ray doesn't diverges respect to the plane.
585 */
586 std::pair< Point3f, bool > HitPlane (Trackball * tb, Point3f point, Plane3f plane)
587 {
588 Ray3fN ray = line2ray(tb->camera.ViewLineFromWindow (point));
589 Point3f p(0,0,0);
590 bool res = IntersectionRayPlane < float >(plane, ray, p);
591 return std::make_pair(p,res);
592 }
593
594
595 // drawing section
596
597 // nota: non ho scritto io questa classe,
598 // quindi la documentazione doxy potrebbe non essere accurata al 100%.
599 /*!
600 @brief Drawing hints for manipulators
601
602 This class is an holder for drawing-related variables.
603
604 It's mainly used for SphereMode and InactiveMode drawings.
605 */
606 class DrawingHint {
607 public:
608 /*!
609 @brief Drawing hints constructor
610
611 assign the drawing-related variables.
612 */
613 DrawingHint () {
614 CircleStep = 64;
615 HideStill = false;
616 DrawTrack = false;
617 LineWidthStill = 0.5f;
618 LineWidthMoving = 1.5f;
619 color = Color4b::LightBlue;
620 }
621 /// The circles resolution.
622 int CircleStep;
623 /// currently not in use.
624 bool HideStill;
625 /// currently not in use.
626 bool DrawTrack;
627 /// circle color
628 Color4b color;
629 /// circle line width when inactive.
630 float LineWidthStill;
631 /// circle line width when active.
632 float LineWidthMoving;
633 };
634
635 /// the drawing hint used by the manipulators
636 DrawingHint DH;
637
638 // nota: non ho scritto io questa funzione,
639 // quindi la documentazione doxy potrebbe non essere accurata al 100%.
640 /*!
641 @brief Draw 2 squares, used by DrawCircle().
642 */
643 void DrawPlaneHandle ()
644 {
645 float r = 1.0;
646 float dr = r / 10.0f;
647
648 glBegin (GL_LINE_STRIP);
649 glVertex3f (+r + dr, +r, 0.0);
650 glVertex3f (+r, +r + dr, 0.0);
651 glVertex3f (+r - dr, +r, 0.0);
652 glVertex3f (+r, +r - dr, 0.0);
653 glVertex3f (+r + dr, +r, 0.0);
654 glEnd ();
655 glBegin (GL_LINE_STRIP);
656 glVertex3f (-r + dr, -r, 0.0);
657 glVertex3f (-r, -r + dr, 0.0);
658 glVertex3f (-r - dr, -r, 0.0);
659 glVertex3f (-r, -r - dr, 0.0);
660 glVertex3f (-r + dr, -r, 0.0);
661 glEnd ();
662 }
663
664 // nota: non ho scritto io questa funzione,
665 // quindi la documentazione doxy potrebbe non essere accurata al 100%.
666 /*!
667 @brief Draw a circle with 2 squares, used by DrawSphereIcon().
668 */
669 void DrawCircle ()
670 {
671 int nside = DH.CircleStep;
672 const double pi2 = 3.14159265 * 2.0;
673 glBegin (GL_LINE_LOOP);
674 for (double i = 0; i < nside; i++) {
675 glNormal3d (cos (i * pi2 / nside), sin (i * pi2 / nside), 0.0);
676 glVertex3d (cos (i * pi2 / nside), sin (i * pi2 / nside), 0.0);
677 }
678 glEnd ();
679 DrawPlaneHandle ();
680 }
681
682 /*!
683 @brief Draw a spherical manipulator icon.
684
685 @param tb the manipulator.
686 @param active boolean to be set to true if the icon is active.
687 */
688 void DrawSphereIcon (Trackball * tb,bool active)
689 {
690 glPushAttrib (GL_TRANSFORM_BIT |GL_ENABLE_BIT | GL_LINE_BIT | GL_CURRENT_BIT | GL_LIGHTING_BIT);
691 glMatrixMode(GL_MODELVIEW);
692 glPushMatrix ();
693
694 Point3f center = tb->center + tb->track.InverseMatrix()*Point3f(0, 0, 0);
695 glTranslate(center);
696 glScale (tb->radius/tb->track.sca);
697
698 float amb[4] = { .3f, .3f, .3f, 1.0f };
699 float col[4] = { .5f, .5f, .8f, 1.0f };
700 glEnable (GL_LINE_SMOOTH);
701 if (active)
702 glLineWidth (DH.LineWidthMoving);
703 else
704 glLineWidth (DH.LineWidthStill);
705 glEnable (GL_LIGHTING);
706 glEnable (GL_LIGHT0);
707 glEnable (GL_BLEND);
708 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
709 glColor (DH.color);
710 glMaterialfv (GL_FRONT_AND_BACK, GL_EMISSION, amb);
711 glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, col);
712
713 DrawCircle ();
714 glRotatef (90, 1, 0, 0);
715 DrawCircle ();
716 glRotatef (90, 0, 1, 0);
717 DrawCircle ();
718
719 glPopMatrix ();
720 glPopAttrib ();
721 }
722
723 // TEMPORARY drawing section
724 // Disclaimer: the following code is of VERY POOR quality
725 // feel free to delete and rewrite everything
726
727 /*!
728 @brief Support function for the \e DrawUgly series of functions
729
730 Prepare the OpenGL attributes.
731 \warning this method is part of the \e DrawUgly series of functions, which is a \b TEMPORARY solution, used while waiting for the \e DrawBeautiful series...
732 */
733 void prepare_attrib()
734 {
735 float amb[4] = { .3f, .3f, .3f, 1.0f };
736 float col[4] = { .5f, .5f, .8f, 1.0f };
737 glEnable (GL_LIGHTING);
738 glEnable (GL_LIGHT0);
739 glEnable (GL_LINE_SMOOTH);
740 glEnable (GL_BLEND);
741 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
742 glMaterialfv (GL_FRONT_AND_BACK, GL_EMISSION, amb);
743 glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, col);
744 }
745
746 /*!
747 @brief Support function for the \e DrawUgly series of functions.
748
749 Draw a coordinate vector, usually a letter, near the manipulator icon in a user readable oriantation.
750 \warning this method is part of the \e DrawUgly series of functions, which is a \b TEMPORARY solution, used while waiting for the \e DrawBeautiful series...
751 @param tb the manipulator.
752 @param ugly_letter the coordinate vector.
753 */
754 void DrawUglyLetter(Trackball * tb,std::vector<Point3f> ugly_letter)
755 {
756 Point3f center=tb->camera.Project(tb->center);
757 float offset=0;
758 offset=std::max(offset,Distance(center,tb->camera.Project(tb->center+(Point3f(1,0,0) * tb->radius))));
759 offset=std::max(offset,Distance(center,tb->camera.Project(tb->center+(Point3f(0,1,0) * tb->radius))));
760 offset=std::max(offset,Distance(center,tb->camera.Project(tb->center+(Point3f(0,0,1) * tb->radius))));
761 glPushMatrix();
762 glPushAttrib (GL_ALL_ATTRIB_BITS);
763 // go to world coords
764 glTranslate (tb->center);
765 glMultMatrix (tb->track.InverseMatrix ());
766 glTranslate (-tb->center);
767 prepare_attrib();
768 glColor3f(1,1,1);
769 glLineWidth(4.0);
770
771 glBegin(GL_LINE_STRIP);
772 for(unsigned int i=0;i<ugly_letter.size();i++){
773 glVertex(tb->camera.UnProject(center+(ugly_letter[i] * offset * 0.25)
774 +Point3f(-offset,-offset,0)));
775 }
776 glEnd();
777 glPopAttrib ();
778 glPopMatrix();
779
780 }
781
782 /*!
783 @brief PanMode drawing function, member of the \e DrawUgly series.
784
785 Draw a PanMode manipulator in an ugly way.
786 \warning this method is part of the \e DrawUgly series of functions, which is a \b TEMPORARY solution, used while waiting for the \e DrawBeautiful series...
787 @param tb the manipulator.
788 */
789 void DrawUglyPanMode(Trackball * tb)
790 {
791 std::vector<Point3f> ugly_p;
792 ugly_p.push_back(Point3f(-1,-1,0));
793 ugly_p.push_back(Point3f(-1,1,0));
794 ugly_p.push_back(Point3f(1,1,0));
795 ugly_p.push_back(Point3f(1,0,0));
796 ugly_p.push_back(Point3f(-1,0,0));
797
798 DrawUglyLetter(tb,ugly_p);
799 }
800
801 /*!
802 @brief ZMode drawing function, member of the \e DrawUgly series.
803
804 Draw a ZMode manipulator in an ugly way.
805 \warning this method is part of the \e DrawUgly series of functions, which is a \b TEMPORARY solution, used while waiting for the \e DrawBeautiful series...
806 @param tb the manipulator.
807 */
808 void DrawUglyZMode(Trackball * tb)
809 {
810 std::vector<Point3f> ugly_z;
811 ugly_z.push_back(Point3f(-1,1,0));
812 ugly_z.push_back(Point3f(1,1,0));
813 ugly_z.push_back(Point3f(-1,-1,0));
814 ugly_z.push_back(Point3f(1,-1,0));
815 DrawUglyLetter(tb,ugly_z);
816 }
817
818 /*!
819 @brief ScaleMode drawing function, member of the \e DrawUgly series.
820
821 Draw a ScaleMode manipulator in an ugly way.
822 \warning this method is part of the \e DrawUgly series of functions, which is a \b TEMPORARY solution, used while waiting for the \e DrawBeautiful series...
823 @param tb the manipulator.
824 */
825 void DrawUglyScaleMode(Trackball * tb)
826 {
827 std::vector<Point3f> ugly_s;
828 ugly_s.push_back(Point3f(1,1,0));
829 ugly_s.push_back(Point3f(-1,1,0));
830 ugly_s.push_back(Point3f(-1,0,0));
831 ugly_s.push_back(Point3f(1,0,0));
832 ugly_s.push_back(Point3f(1,-1,0));
833 ugly_s.push_back(Point3f(-1,-1,0));
834 DrawUglyLetter(tb,ugly_s);
835 }
836
837 /*!
838 @brief AxisMode drawing function, member of the \e DrawUgly series.
839
840 Draw an AxisMode manipulator in an ugly way.
841 \warning this method is part of the \e DrawUgly series of functions, which is a \b TEMPORARY solution, used while waiting for the \e DrawBeautiful series...
842 @param tb the manipulator.
843 @param axis AxisMode's axis.
844 */
845 void DrawUglyAxisMode(Trackball * tb,Line3f axis)
846 {
847 glPushMatrix();
848 glPushAttrib (GL_ALL_ATTRIB_BITS);
849 // go to world coords
850 glTranslate (tb->center);
851 glMultMatrix (tb->track.InverseMatrix ());
852 glTranslate (-tb->center);
853 prepare_attrib();
854 glColor3f(0.9f, 0.9f, 0.2f);
855 glLineWidth(2.0);
856 glBegin(GL_LINES);
857 glVertex(axis.Origin()+(axis.Direction()*100));
858 glVertex(axis.Origin()-(axis.Direction()*100));
859 glEnd();
860 glPointSize(8.0);
861 glColor3f(0.2f, 0.2f, 0.9f);
862 glBegin(GL_POINTS);
863 glVertex(axis.Origin());
864 glEnd();
865 glPopAttrib ();
866 glPopMatrix();
867 }
868
869 /*!
870 @brief PlaneMode drawing function, member of the \e DrawUgly series.
871
872 Draw a PlaneMode manipulator in an ugly way.
873 \warning this method is part of the \e DrawUgly series of functions, which is a \b TEMPORARY solution, used while waiting for the \e DrawBeautiful series...
874 @param tb the manipulator.
875 @param plane PlaneMode's plane.
876 */
877 void DrawUglyPlaneMode(Trackball * tb,Plane3f plane)
878 {
879 glPushMatrix();
880 glPushAttrib (GL_ALL_ATTRIB_BITS);
881 // go to world coords
882 glTranslate (tb->center);
883 glMultMatrix (tb->track.InverseMatrix ());
884 glTranslate (-tb->center);
885 prepare_attrib();
886 Point3f p0,d1,d2,norm;
887 norm=plane.Direction();
888 p0=plane.Projection(Point3f(0,0,0));
889 d1=Point3f(0,1,0);
890 if(norm == d1 || norm == -d1)
891 d1 = Point3f(1,0,0);
892 d2=plane.Projection(d1);
893 d1=(d2 - p0).Normalize();
894 d2=(d1 ^ norm).Normalize();
895 glLineWidth(3.0);
896 glColor3f(0.2f, 0.2f, 0.9f);
897 glBegin(GL_LINES);
898 glVertex(p0);
899 glVertex(p0+norm);
900 glEnd();
901 glLineWidth(1.0);
902 for(float i=0.5f; i<100.0f; i+=0.7f){
903 glBegin(GL_LINE_LOOP);
904 for(int a=0;a<360;a+=10){
905 float f0=i*cosf((float(M_PI)*float(a))/180.0f);
906 float f1=i*sinf((float(M_PI)*float(a))/180.0f);
907 glVertex(p0+(d1*f0)+(d2*f1));
908 }
909 glEnd();
910 }
911 glColor3f(0.9f, 0.9f, 0.2f);
912 glPointSize(8.0f);
913 glBegin(GL_POINTS);
914 glVertex(p0);
915 glEnd();
916 glColor3f(0.7f, 0.7f, 0.0f);
917 glPointSize(6.0);
918 glBegin(GL_POINTS);
919 glVertex(p0+norm);
920 glEnd();
921 glPopAttrib ();
922 glPopMatrix();
923 }
924
925 /*!
926 @brief CylinderMode drawing function, member of the \e DrawUgly series.
927
928 Draw a CylinderMode manipulator in an ugly way.
929 \warning this method is part of the \e DrawUgly series of functions, which is a \b TEMPORARY solution, used while waiting for the \e DrawBeautiful series...
930 @param tb the manipulator.
931 @param axis CylinderMode's axis.
932 */
933 void DrawUglyCylinderMode(Trackball * tb,Line3f axis)
934 {
935 glPushMatrix();
936 glPushAttrib (GL_ALL_ATTRIB_BITS);
937 // go to world coords
938 glTranslate (tb->center);
939 glMultMatrix (tb->track.InverseMatrix ());
940 glTranslate (-tb->center);
941 prepare_attrib();
942 Plane3f plane;
943 plane.Init(axis.Origin(),axis.Direction());
944 Point3f p0,d1,d2,norm;
945 norm=plane.Direction();
946 p0=plane.Projection(Point3f(0,0,0));
947 d1=Point3f(0,1,0);
948 if(norm == d1 || norm == -d1)
949 d1 = Point3f(1,0,0);
950 d2=plane.Projection(d1);
951 d1=(d2 - p0).Normalize();
952 d2=(d1 ^ norm).Normalize();
953 glLineWidth(1.0);
954 glColor3f(0.2f, 0.2f, 0.9f);
955 for(int i=-100;i<100;i++){
956 glBegin(GL_LINE_LOOP);
957 for(int a=0;a<360;a+=10){
958 float f0=(tb->radius)*cosf((float(M_PI)*float(a))/180.0f);
959 float f1=(tb->radius)*sinf((float(M_PI)*float(a))/180.0f);
960 glVertex(axis.Origin()+p0+(norm*float(i))+(d1*f0)+(d2*f1));
961 }
962 glEnd();
963 }
964 glLineWidth(3.0);
965 glColor3f(0.2f, 0.2f, 0.9f);
966 glBegin(GL_LINES);
967 glVertex(axis.Origin());
968 glVertex(axis.Origin()+(axis.Direction()*100));
969 glEnd();
970 glLineWidth(1.5);
971 glColor3f(0.9f, 0.2f, 0.9f);
972 glBegin(GL_LINES);
973 glVertex(axis.Origin());
974 glVertex(axis.Origin()-(axis.Direction()*100));
975 glEnd();
976 glColor3f(0.9f, 0.9f, 0.2f);
977 glPointSize(8.0);
978 glBegin(GL_POINTS);
979 glVertex(axis.Origin());
980 glEnd();
981 glPopAttrib ();
982 glPopMatrix();
983 }
984
985 /*!
986 @brief PathMode drawing function, member of the \e DrawUgly series.
987
988 Draw a PathMode manipulator in an ugly way.
989 \warning this method is part of the \e DrawUgly series of functions, which is a \b TEMPORARY solution, used while waiting for the \e DrawBeautiful series...
990 @param tb the manipulator.
991 @param points PathMode's points.
992 @param current_point PathMode's current point.
993 @param prev_point PathMode's prev point.
994 @param next_point PathMode's next point.
995 @param old_hitpoint PathMode's old hitpoint.
996 @param wrap PathMode's wrap.
997 */
998 void DrawUglyPathMode(Trackball * tb,const std::vector < Point3f > &points,
999 Point3f current_point,Point3f prev_point,
1000 Point3f next_point,Point3f old_hitpoint,bool wrap)
1001 {
1002 glPushMatrix();
1003 glPushAttrib (GL_ALL_ATTRIB_BITS);
1004 // go to world coords
1005 glTranslate (tb->center);
1006 glMultMatrix (tb->track.InverseMatrix ());
1007 glTranslate (-tb->center);
1008 prepare_attrib();
1009 glColor3f(0.9f, 0.9f, 0.2f);
1010 glLineWidth(2.0);
1011 if(wrap)
1012 glBegin(GL_LINE_LOOP);
1013 else
1014 glBegin(GL_LINE_STRIP);
1015 for (std::vector < Point3f >::const_iterator i = points.begin (); i != points.end (); ++i){
1016 glVertex(*i);
1017 }
1018 glEnd();
1019 glColor3f(1,0,1);
1020 glPointSize(8.0);
1021 glBegin(GL_POINTS);
1022 glVertex(current_point);
1023 glEnd();
1024 glColor3f(0.6f, 0.0f, 0.6f);
1025 glPointSize(7.0);
1026 glBegin(GL_POINTS);
1027 glVertex(old_hitpoint);
1028 glEnd();
1029 glColor3f(0.7f, 0.7f, 0.7f);
1030 glPointSize(6.5);
1031 glBegin(GL_POINTS);
1032 glVertex(prev_point);
1033 glVertex(next_point);
1034 glEnd();
1035 glPopAttrib ();
1036 glPopMatrix();
1037 }
1038
1039 /*!
1040 @brief AreaMode drawing function, member of the \e DrawUgly series.
1041
1042 Draw an AreaMode manipulator in an ugly way.
1043 \warning this method is part of the \e DrawUgly series of functions, which is a \b TEMPORARY solution, used while waiting for the \e DrawBeautiful series...
1044 @param tb the manipulator.
1045 @param points AreaMode's points.
1046 @param status AreaMode's status.
1047 @param old_status AreaMode's old status.
1048 @param plane AreaMode's plane.
1049 @param path AreaMode's path.
1050 @param rubberband_handle AreaMode's rubberband handle.
1051 */
1052 void DrawUglyAreaMode(Trackball * tb,const std::vector < Point3f > &points,
1053 Point3f status,Point3f old_status,Plane3f plane,
1054 const std::vector < Point3f > &path,Point3f rubberband_handle)
1055 {
1056 glPushMatrix();
1057 glPushAttrib (GL_ALL_ATTRIB_BITS);
1058 // go to world coords
1059 glTranslate (tb->center);
1060 glMultMatrix (tb->track.InverseMatrix ());
1061 glTranslate (-tb->center);
1062 prepare_attrib();
1063 glColor3f(0.9f, 0.9f, 0.2f);
1064 glLineWidth(2.0);
1065 glBegin(GL_LINE_LOOP);
1066 for (std::vector < Point3f >::const_iterator i = points.begin (); i != points.end (); ++i){
1067 glVertex(*i);
1068 }
1069 glEnd();
1070 glColor3f(0.0f, 0.9f, 0.2f);
1071 glLineWidth(1.2f);
1072 glBegin(GL_LINE_STRIP);
1073 for (std::vector < Point3f >::const_iterator i = path.begin (); i != path.end (); ++i){
1074 glVertex(*i);
1075 }
1076 glEnd();
1077 glColor3f(1,0,1);
1078 glPointSize(8.0);
1079 glBegin(GL_POINTS);
1080 glVertex(status);
1081 glEnd();
1082 glColor3f(0.6f, 0.0f, 0.6f);
1083 glPointSize(7.0);
1084 glBegin(GL_POINTS);
1085 glVertex(old_status);
1086 glEnd();
1087 glColor3f(0.6f, 0.0f, 0.0f);
1088 glPointSize(6.0);
1089 glBegin(GL_POINTS);
1090 glVertex(rubberband_handle);
1091 glEnd();
1092 glLineWidth(1.0);
1093 glBegin(GL_LINES);
1094 glVertex(rubberband_handle);
1095 glVertex(status);
1096 glEnd();
1097 Point3f p0,d1,d2,norm;
1098 norm=plane.Direction();
1099 p0=plane.Projection(Point3f(0,0,0));
1100 d1=Point3f(0,1,0);
1101 if(norm == d1 || norm == -d1)
1102 d1 = Point3f(1,0,0);
1103 d2=plane.Projection(d1);
1104 d1=(d2 - p0).Normalize();
1105 d2=(d1 ^ norm).Normalize();
1106 glLineWidth(3.0);
1107 glColor3f(0.2f, 0.2f, 0.9f);
1108 glBegin(GL_LINES);
1109 glVertex(p0);
1110 glVertex(p0+norm);
1111 glEnd();
1112 glLineWidth(0.1f);
1113 for(float i=0.5f;i<100.0f; i+=0.7f){
1114 glBegin(GL_LINE_LOOP);
1115 for(int a=0;a<360;a+=10){
1116 float f0=i*cosf((float(M_PI)*float(a))/180.0f);
1117 float f1=i*sinf((float(M_PI)*float(a))/180.0f);
1118 glVertex(p0+(d1*f0)+(d2*f1));
1119 }
1120 glEnd();
1121 }
1122
1123 glPopAttrib ();
1124 glPopMatrix();
1125 }
1126
1127
1128 } //end namespace trackutils
1129
1130 } //end namespace vcg
1131
1132 #endif //TRACKUTILS_H