Debian Codegraph @

vcg/wrap/gui/trackball.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: trackball.h,v $
27 Revision 1.17 2007/12/21 12:29:34 tarini
28 *** empty log message ***
29
30 Revision 1.16 2007/07/14 12:43:44 benedetti
31 Added Doxygen documentation.
32
33 Revision 1.15 2007/06/13 17:15:08 benedetti
34 Added one-level undo system and sticky trackmodes.
35
36 Revision 1.14 2007/05/15 15:00:47 benedetti
37 Moved the drawing code to trackmodes, some other minor changes
38
39 Revision 1.13 2007/02/26 01:30:02 cignoni
40 Added reflection Name
41
42 Revision 1.12 2007/01/15 15:04:15 tarini
43 added "ToAscii" and "SetFromAscii" methods to load/store current trackball status from/to ascii strings
44 (intended uses: clipboard operations and comments inside png snapshots!)
45
46 Revision 1.11 2006/08/23 15:40:57 marfr960
47 *** empty log message ***
48
49 Revision 1.10 2006/02/13 13:15:52 cignoni
50 Added Scale and Translate methods.
51 Added many drawing hints and raised the default num. of steps when drawing circles.
52 Added MouseDown without coords (for remembering changes of keys modifiers)
53 Added ZMode to the default modes under Alt+left
54 Added DrawPostApply (to be completed)
55
56 Revision 1.9 2005/10/17 01:29:46 cignoni
57 Main restructuring. Removed the Draw function and slightly changed the meaning of the trackball itself.
58 See the notes at the beginning of trackball.h
59
60 Revision 1.8 2004/07/11 22:06:56 cignoni
61 Added scaling by wheel
62
63 Revision 1.7 2004/06/09 14:01:13 cignoni
64 Heavily restructured. To be completed only rotation works...
65
66 Revision 1.6 2004/05/14 03:15:09 ponchio
67 Redesigned partial version.
68
69 Revision 1.5 2004/05/12 20:55:18 ponchio
70 *** empty log message ***
71
72 Revision 1.4 2004/05/07 12:46:08 cignoni
73 Restructured and adapted in a better way to opengl
74
75 Revision 1.3 2004/04/07 10:54:10 cignoni
76 Commented out unused parameter names and other minor warning related issues
77
78 Revision 1.2 2004/03/25 14:55:25 ponchio
79 Adding copyright.
80
81
82 ****************************************************************************/
83 #ifndef TRACKBALL_H
84 #define TRACKBALL_H
85
86 #include <vcg/math/similarity.h>
87 #include <vcg/space/color4.h>
88 #include <wrap/gui/view.h>
89 #include <wrap/gui/trackmode.h>
90 #include <list>
91 #include <vector>
92 #include <map>
93
94 namespace vcg {
95 /*!
96 @brief The base class for Trackball.
97
98 This class is useful for using a Trackball instance in a scene graph,
99 as a sort of interactive transform.
100 */
101 class Transform {
102 public:
103 /*!
104 @brief The constructor.
105
106 Initialize:
107 - track to the identity transform.
108 - center to origin 0,0,0.
109 - radius to unit size.
110 */
111 Transform();
112 /// A trackball stores a transformation called 'track' that effectively rototranslate the object.
113 Similarityf track;
114 /// track position in model space.
115 Point3f center;
116 /// size of the widget in model space.
117 float radius;
118 };
119
120 /*!
121 @brief Computes the linear interpolation between 2 transforms.
122
123 @param a The first transform.
124 @param b The second transform.
125 @param t The interpolation value (0: just a, 0.5: middle from a to b, 1: just b).
126 @return The linear interpolation.
127 */
128 Transform interpolate(const Transform &a, const Transform &b, float t);
129
130 class TrackMode;
131
132 /*!
133 @brief The manipulator manager system.
134
135 <em>Short usage note:</em>
136
137 - Center specify the center of rotation and scaling of the trackball and usually is set by the program and do not interactively change
138 - Radius specify the radius of the interactive ball shaped icon to specify rotation. It is in absolute unit.
139 - Like the previous one it is not changed during interaction.
140
141 When you specify a translation with the trackball the trackball center remain \b unchanged, in other words it means that the object move out of the trackball icon. Similarly when you apply a scaling the size of the manipulator icon do not change.
142
143 Typical use:
144 <pre>
145 glMatrixMode(GL_PROJECTION);
146 glLoadIdentity();
147 gluPerspective(60, float(width())/float(height()), 1, 100);
148 glMatrixMode(GL_MODELVIEW);
149 glLoadIdentity();
150 gluLookAt(0,0,3, 0,0,0, 0,1,0)
151
152 trackball.center=Point3f(0, 0, 0);
153 trackball.radius= 1;
154
155 trackball.GetView();
156 trackball.Apply(true); //false if you want an invisible trackball
157
158 float d=1.0f/mesh.bbox.Diag();
159 glScale(d);
160 glTranslate(-mesh.bbox.Center());
161 mesh->Render();
162 </pre>
163
164 Note on the typical use:
165 - Perspective and glulookat are choosed to frame the origin centered 1-radius trackball.
166 - The final scale and translate are just to fit a generic mesh to the 1sized origin centered where the trackball stays box.
167 - The trackball works also on Orthographic projections \b but that are not centered around origin (just move it back along the Z)
168 */
169 class Trackball: public Transform {
170 public:
171 // the drawing code has been moved to the trackmodes
172 // class DrawingHint {
173
174 // DrawingHint DH;
175
176 /// The componibile states of the manipulator system.
177 enum Button { BUTTON_NONE = 0x0000, ///< No mouse button pressed.
178 BUTTON_LEFT = 0x0001, ///< Left mouse button pressed.
179 BUTTON_MIDDLE = 0x0002, ///< Middle mouse button pressed.
180 BUTTON_RIGHT = 0x0004, ///< Right mouse button pressed.
181 WHEEL = 0x0008, ///< Mouse wheel activated.
182 KEY_SHIFT = 0x0010, ///< Shift key pressed.
183 KEY_CTRL = 0x0020, ///< Ctrl key pressed.
184 KEY_ALT = 0x0040, ///< Alt key pressed.
185 HANDLE = 0x0080 ///< Application-defined state activated.
186 };
187
188 /*!
189 @brief The constructor.
190
191 Initialize the internal state with default values
192 and call setDefaultMapping().
193 */
194 Trackball();
195 /*!
196 @brief The destructor.
197
198 @warning The destructor <b>does not</b> deallocate the memory allocated by setDefaultMapping(), because the application can change the modes map. This can lead to small memory leaks, so please explicitally delete any manipulator in the modes map if you are going to repeatly allocate and deallocate Trackball instances.
199 */
200 ~Trackball();
201 /*!
202 @brief Reset the trackball.
203
204 Equivalent to Reset().
205 */
206 void SetIdentity();
207 /*!
208 @brief Set the position of the trackball.
209
210 @param c The new position of the trackball.
211 @param millisec Currently not in use.
212 */
213 void SetPosition(const Point3f &c, int millisec = 0);
214 /*!
215 @brief Currently not in use.
216
217 @param s Currently not in use.
218 */
219 void SetScale(const float s) {radius=s;};
220 /*!
221 @brief Currently not in use.
222
223 @param transform Currently not in use.
224 @param millisec Currently not in use.
225 */
226 void SetTransform(const Transform &transform, int millisec = 0);
227 /*!
228 @brief Apply a translation on the current transformation.
229
230 @param tr The translation vector.
231 */
232 void Translate(Point3f tr);
233 /*!
234 @brief Apply a scaling on the current transformation.
235
236 @param f The scale factor.
237 */
238 void Scale(const float f);
239
240 //operating
241 /*!
242 @brief Initialize the camera instance.
243 */
244 void GetView();
245 /*!
246 @brief Apply the current transformation on the OpenGL modelview matrix.
247
248 @param Draw true if has to call DrawPostApply() after the application.
249 */
250 void Apply(bool Draw);
251 /*!
252 @brief Old application of the transformation.
253
254 @warning This function does \b not call DrawPostApply() after the application.
255 */
256 void Apply ();
257 /*!
258 @brief Draw the current manipulator.
259
260 Call the draw function of the current manipulator.
261 If no manipulator is selected call the draw function of the manipulator associated to inactive_mode.
262
263 @warning This function assumes that the OpenGL modelview matrix has been initialized with Apply ().
264 */
265 void DrawPostApply();
266 /*!
267 @brief Apply the \b inverse of current transformation on the OpenGL modelview matrix.
268 */
269 void ApplyInverse();
270 // DrawIcon() has been moved to trackutils.h
271 //void DrawIcon();
272 /*!
273 @brief Reset the transformation and every mapped manipulator.
274 */
275 void Reset();
276
277 // DrawCircle (), DrawPlane(), DrawPlaneHandle() has been moved to trackutils.h
278 // the drawing code has been moved to the trackmodes
279 // void DrawCircle ();
280 // void DrawPlane();
281 // void DrawPlaneHandle();
282
283 //interface
284 /*!
285 @brief Interface function relative to mouse down event in QT/SDL.
286
287 @param button The new state.
288 */
289 void MouseDown(/*Button*/ int button);
290 /*!
291 @brief Interface function relative to mouse down event in QT/SDL.
292
293 @param x The horizontal coordinate of the mouse pointer.
294 @param y The vertical coordinate of the mouse pointer.
295 @param button The new state.
296 */
297 void MouseDown(int x, int y, /*Button*/ int button);
298 /*!
299 @brief Interface function relative to mouse down event in QT/SDL.
300
301 @param x The horizontal coordinate of the mouse pointer.
302 @param y The vertical coordinate of the mouse pointer.
303 */
304 void MouseMove(int x, int y);
305 /*!
306 @brief Interface function relative to mouse down event in QT/SDL.
307
308 @param x The horizontal coordinate of the mouse pointer.
309 @param y The vertical coordinate of the mouse pointer.
310 @param button The new state.
311 */
312 void MouseUp(int x, int y, /*Button */ int button);
313 /*!
314 @brief Old interface function relative to mouse down event in QT/SDL.
315
316 @param notch The mouse wheel notch (1: one forward step, -1: one backward step).
317 */
318 void MouseWheel(float notch);
319 /*!
320 @brief Interface function relative to mouse down event in QT/SDL.
321
322 @param notch The mouse wheel notch (1: one forward step, -1: one backward step).
323 @param button The new state.
324 */
325 void MouseWheel (float notch, /*Button */ int button);
326 /*!
327 @brief Interface function relative to key down event in QT/SDL.
328
329 @param button the new state.
330 */
331 void ButtonUp(Button button);
332 /*!
333 @brief Interface function relative to key up event in QT/SDL.
334
335 @param button the new state.
336 */
337 void ButtonDown(Button button);
338 /*!
339 @brief Undo function for manipulator system.
340
341 A call of this function restores the state before last user action.
342 This function calls %Undo() on every mapped manipulator.
343 */
344 void Undo();
345
346 //default sensitivity 1
347 /*!
348 @brief Currently not in use.
349
350 @param s Currently not in use.
351 */
352 void SetSensitivity(float s);
353
354 //spinning interface
355 /*!
356 @brief Currently not in use.
357
358 @param on Currently not in use.
359 */
360 void SetSpinnable(bool on);
361 /*!
362 @brief Currently not in use.
363
364 @return A meaningless boolean value.
365 */
366 bool IsSpinnable();
367 /*!
368 @brief Currently not in use.
369
370 @param spin Currently not in use.
371 */
372 void SetSpinning(Quaternionf &spin);
373 /*!
374 @brief Currently not in use.
375 */
376 void StopSpinning();
377 /*!
378 @brief Currently not in use.
379
380 @return A meaningless boolean value.
381 */
382 bool IsSpinning();
383
384 //interfaccia navigation:
385 /*!
386 @brief Currently not in use.
387 */
388 void Back();
389 /*!
390 @brief Currently not in use.
391 */
392 void Forward();
393 /*!
394 @brief Currently not in use.
395 */
396 void Home();
397 /*!
398 @brief Currently not in use.
399 */
400 void Store();
401 /*!
402 @brief Currently not in use.
403 */
404 void HistorySize(int lenght);
405
406 /* //internals // commented out no more used this stuff!
407 enum Action { NONE = 0,
408 VIEW_ROTATE = 1,
409 // Axis Constrained Rotation
410 TRACK_ROTATE_X = 3, TRACK_ROTATE_Y = 4, TRACK_ROTATE_Z = 5,
411 // Drag constrained to an axis (trackball axis)
412 DRAG_X = 6, DRAG_Y = 7, DRAG_Z = 8,
413 // Drag constrained to a plane
414 DRAG_XY = 9, DRAG_YZ = 10, DRAG_XZ = 11,
415 //scale model respect to center of trackball
416 VIEW_SCALE = 12,
417 //scale trackball and model
418 TRACK_SCALE = 13
419 };
420 */
421 // loads stores current status from/to ascii stings
422 /*!
423 @brief Stores current status into an ascii stings
424
425 Stores current status into an ascii stings. This is useful for example to implement cut-and-paste operations of trackball status, or to embed used trackball into a comment inside a screenshot, etc.
426 @param st The string where to export (must be allocated).
427 */
428 void ToAscii(char * st);
429 /*!
430 @brief Loads current status from an ascii stings
431
432 Loads current status from an ascii stings. This is useful for example to implement cut-and-paste operations of trackball status, or to embed used trackball into a comment inside a screenshot, etc.
433 @param st The string where to read from (must be allocated). Use ToAscii() method to set it.
434 @return True iff the trackball was successfully recovered.
435 */
436 bool SetFromAscii(const char * st);
437
438 //protected:
439 /// The reference for point projection and unprojection from screen space to modelspace.
440 View<float> camera;
441 /*!
442 @brief Prepare Trackball and every mapped TrackMode for an user action.
443
444 This function is called automatically when an user action begins.
445 */
446 void SetCurrentAction();
447 /// Current state composition.
448 int current_button;
449 /// The selected manipulator.
450 TrackMode *current_mode;
451
452 /// The inactive manipulator. It is drawed when Trackball is inactive.
453 TrackMode *inactive_mode;
454
455 /*!
456 @brief Reset modes to default mapping.
457
458 Set the default modes mapping.
459 The default mapping is:
460 - \b LEFT : SphereMode.
461 - \b LEFT+CTRL or \b MIDDLE : PanMode.
462 - \b LEFT+SHIFT or \b WHEEL : ScaleMode.
463 - \b LEFT+ALT : ZMode.
464
465 @warning The memory allocated by this function <b>is not</b> automatically deallocated. see ~Trackball().
466 */
467 void setDefaultMapping ();
468
469 /// The manipulator mapping. Needs to be explicitally managed for custom mappings.
470 std::map<int, TrackMode *> modes;
471
472 // undo_track and last_track have different meanings..
473 /// Transformation before current user action.
474 Similarityf last_track;
475 /// track after an Undo() call.
476 Similarityf undo_track;
477 /// Currently not in use.
478 Similarityf last_view;
479 /// Mouse cursor coordinates before current action.
480 Point3f last_point;
481 /// Currently not in use.
482 std::vector<Point3f> Hits;
483 /// Currently not in use.
484 bool dragging;
485 /// Currently not in use.
486 int button_mask;
487
488 /// Currently not in use.
489 Quaternionf spin;
490 /// Currently not in use.
491 bool spinnable;
492 /// Currently not in use.
493 bool spinning;
494
495 /// Currently not in use.
496 std::list<Transform> history;
497 /// Currently not in use.
498 int history_size;
499
500 /// Manipulators needs full access to this class.
501 friend class TrackMode;
502 };
503
504
505 }//namespace
506
507 #endif