| 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: trackmode.h,v $ |
| 27 | Revision 1.16 2008/02/15 20:57:22 benedetti |
| 28 | added snap to CylinderMode |
| 29 | |
| 30 | Revision 1.15 2007/07/14 12:43:44 benedetti |
| 31 | Added Doxygen documentation. |
| 32 | |
| 33 | Revision 1.14 2007/07/09 22:47:18 benedetti |
| 34 | Removed using namespace std and modified accordingly. |
| 35 | |
| 36 | Revision 1.13 2007/06/25 10:21:38 fiorin |
| 37 | Added some std:: here and there |
| 38 | |
| 39 | Revision 1.12 2007/06/13 17:15:09 benedetti |
| 40 | Added one-level undo system and sticky trackmodes. |
| 41 | |
| 42 | Revision 1.11 2007/05/15 14:59:10 benedetti |
| 43 | Main restructuring. added many new modes |
| 44 | |
| 45 | Revision 1.10 2007/02/26 01:30:02 cignoni |
| 46 | Added reflection Name |
| 47 | |
| 48 | Revision 1.9 2006/02/13 13:10:27 cignoni |
| 49 | Added Zmode for moving objects along the perpendicular to the viewplane |
| 50 | |
| 51 | Revision 1.8 2004/07/18 06:54:08 cignoni |
| 52 | Added Scaling |
| 53 | |
| 54 | Revision 1.7 2004/07/11 22:06:56 cignoni |
| 55 | Added scaling by wheel |
| 56 | |
| 57 | Revision 1.6 2004/06/09 14:01:13 cignoni |
| 58 | Heavily restructured. To be completed only rotation works... |
| 59 | |
| 60 | Revision 1.5 2004/05/14 03:15:09 ponchio |
| 61 | Redesigned partial version. |
| 62 | |
| 63 | Revision 1.4 2004/05/07 12:46:08 cignoni |
| 64 | Restructured and adapted in a better way to opengl |
| 65 | |
| 66 | Revision 1.3 2004/04/07 10:54:11 cignoni |
| 67 | Commented out unused parameter names and other minor warning related issues |
| 68 | |
| 69 | Revision 1.2 2004/03/25 14:55:25 ponchio |
| 70 | Adding copyright. |
| 71 | |
| 72 | |
| 73 | ****************************************************************************/ |
| 74 | |
| 75 | #ifndef TRACKMODE_H |
| 76 | #define TRACKMODE_H |
| 77 | |
| 78 | #include <vcg/space/line3.h> |
| 79 | #include <vcg/space/plane3.h> |
| 80 | #include <vcg/space/segment3.h> |
| 81 | #include <vcg/space/ray3.h> |
| 82 | #include <wrap/gui/view.h> |
| 83 | |
| 84 | namespace vcg { |
| 85 | |
| 86 | class Trackball; |
| 87 | |
| 88 | /*! |
| 89 | @brief Base class for all the manipulators. |
| 90 | |
| 91 | Functions in this class implements the default |
| 92 | behaviour of a manipulator: <em>doing nothing</em>. |
| 93 | |
| 94 | Every manipulator must be subclass of this class. |
| 95 | */ |
| 96 | class TrackMode { |
| 97 | public: |
| 98 | /*! |
| 99 | @brief The default virtual destructor |
| 100 | */ |
| 101 | virtual ~TrackMode () { |
| 102 | } |
| 103 | /*! |
| 104 | @brief The default manipulator application for mouse drags. |
| 105 | |
| 106 | This default application does nothing. |
| 107 | |
| 108 | @param trackball the manipulator manager. |
| 109 | @param new_point the new mouse pointer coordinate. |
| 110 | */ |
| 111 | virtual void Apply (Trackball * trackball, Point3f new_point); |
| 112 | /*! |
| 113 | @brief The default manipulator application for mouse scrolls. |
| 114 | |
| 115 | This default application does nothing. |
| 116 | |
| 117 | @param trackball the manipulator manager. |
| 118 | @param WheelNotch the mouse wheel notch. |
| 119 | */ |
| 120 | virtual void Apply (Trackball * trackball, float WheelNotch); |
| 121 | /*! |
| 122 | @brief The default manipulator's begin action function. |
| 123 | |
| 124 | This default implementation does nothing. |
| 125 | */ |
| 126 | virtual void SetAction (); |
| 127 | /*! |
| 128 | @brief The default manipulator's reset function. |
| 129 | |
| 130 | If a manipulator has a state, it can be reset to the inital state calling this function. |
| 131 | */ |
| 132 | virtual void Reset (); |
| 133 | /*! |
| 134 | @brief The default manipulator's name. |
| 135 | |
| 136 | @return the constant string "TrackMode" |
| 137 | */ |
| 138 | virtual const char *Name (){ |
| 139 | return "TrackMode"; |
| 140 | }; |
| 141 | /*! |
| 142 | @brief The default manipulator's render function. |
| 143 | |
| 144 | @param trackball the manipulator manager. |
| 145 | */ |
| 146 | virtual void Draw (Trackball * trackball); |
| 147 | /*! |
| 148 | @brief The default avaibility to manipulator changes inside an action. |
| 149 | |
| 150 | Every manipulator class can choose if the manipulator manager can switch |
| 151 | between it and another manipulator in the middle of an user action, |
| 152 | e.g. switching Trackball's current_mode without releasing the mouse button. |
| 153 | |
| 154 | The default behaviour is to allow the switch. |
| 155 | |
| 156 | Blocking switches is useful for stateful manipulators, regarding state |
| 157 | consistency respect to Trackball's %Undo() calls. |
| 158 | @return false if manipulator permits the switch. |
| 159 | */ |
| 160 | virtual bool isSticky(); |
| 161 | /*! |
| 162 | @brief The default manipulator's undo function. |
| 163 | |
| 164 | If a manipulator has a state, it must be undoable with a call of this function. |
| 165 | The undo must recreate the state present before the last Apply() call. |
| 166 | |
| 167 | This default implementation does nothing. |
| 168 | */ |
| 169 | virtual void Undo(); |
| 170 | }; |
| 171 | |
| 172 | /*! |
| 173 | @brief An inactive manipulator. |
| 174 | |
| 175 | This manipulator is useful only for drawing the inactive trackball |
| 176 | and for feeding occasional Trackball's modes with inactive manipulators. |
| 177 | */ |
| 178 | class InactiveMode:public TrackMode { |
| 179 | public: |
| 180 | /*! |
| 181 | @brief Return this manipulator's name. |
| 182 | |
| 183 | @return the constant string "InactiveMode" |
| 184 | */ |
| 185 | const char *Name () { |
| 186 | return "InactiveMode"; |
| 187 | }; |
| 188 | /*! |
| 189 | @brief Render this manipulator. |
| 190 | |
| 191 | @param trackball the manipulator manager. |
| 192 | */ |
| 193 | void Draw (Trackball * trackball); |
| 194 | }; |
| 195 | |
| 196 | /* View space modes */ |
| 197 | |
| 198 | // old interfaces |
| 199 | /* |
| 200 | class SphereMode: public TrackMode { |
| 201 | } |
| 202 | class CylinderMode: public TrackMode { |
| 203 | } |
| 204 | class PlaneMode: public TrackMode { |
| 205 | } |
| 206 | class ZMode: public TrackMode { |
| 207 | } |
| 208 | class LineMode: public TrackMode { |
| 209 | } |
| 210 | class LineMode: public TrackMode { |
| 211 | } |
| 212 | class ScaleMode: public TrackMode { |
| 213 | |
| 214 | */ |
| 215 | |
| 216 | // Sphere mode. |
| 217 | /*! |
| 218 | @brief The classic \e arcball manipulator. |
| 219 | |
| 220 | This class implements the classic free rotation manipulator, |
| 221 | called \e arcball or \e trackball. |
| 222 | |
| 223 | This is a stateless manipulator, result of the Apply function is |
| 224 | determined only by the mouse coordinates. |
| 225 | */ |
| 226 | class SphereMode:public TrackMode { |
| 227 | public: |
| 228 | /*! |
| 229 | @brief Apply a rotation, function of the user mouse drag action. |
| 230 | |
| 231 | Map a mouse drag action on a rotation, and apply the rotation to the manipulated objects. |
| 232 | |
| 233 | If the user does not hit the sphere that surrounds the manipulated object(s), |
| 234 | a rotational hyperboloid is used to compute the rotation. |
| 235 | @param trackball the manipulator manager. |
| 236 | @param new_point the new mouse pointer coordinate. |
| 237 | */ |
| 238 | void Apply (Trackball * trackball, Point3f new_point); |
| 239 | /*! |
| 240 | @brief Return this manipulator's name. |
| 241 | |
| 242 | @return the constant string "SphereMode" |
| 243 | */ |
| 244 | const char *Name () { |
| 245 | return "SphereMode"; |
| 246 | }; |
| 247 | /*! |
| 248 | @brief Render this manipulator. |
| 249 | |
| 250 | @param trackball the manipulator manager. |
| 251 | */ |
| 252 | void Draw (Trackball * trackball); |
| 253 | }; |
| 254 | |
| 255 | // Panning mode. |
| 256 | /*! |
| 257 | @brief The panning manipulator. |
| 258 | |
| 259 | This manipulator implements a bidimensional translation |
| 260 | on the view plane. |
| 261 | |
| 262 | This is a stateless manipulator, result of the Apply function is |
| 263 | determined only by the mouse coordinates. |
| 264 | */ |
| 265 | class PanMode:public TrackMode { |
| 266 | public: |
| 267 | /*! |
| 268 | @brief Apply a translation, function of the user mouse drag action. |
| 269 | |
| 270 | The manipulated object is dragged in the plane parallel to the screen. |
| 271 | |
| 272 | @param trackball the manipulator manager. |
| 273 | @param new_point the new mouse pointer coordinate. |
| 274 | */ |
| 275 | void Apply (Trackball * trackball, Point3f new_point); |
| 276 | /*! |
| 277 | @brief Return this manipulator's name. |
| 278 | |
| 279 | @return the constant string "PanMode" |
| 280 | */ |
| 281 | const char *Name () { |
| 282 | return "PanMode"; |
| 283 | }; |
| 284 | /*! |
| 285 | @brief Render this manipulator. |
| 286 | |
| 287 | @param trackball the manipulator manager. |
| 288 | */ |
| 289 | void Draw (Trackball * trackball); |
| 290 | }; |
| 291 | |
| 292 | // Z mode. |
| 293 | /*! |
| 294 | @brief The Z-directional manipulator. |
| 295 | |
| 296 | This manipulator implements a monodimensional translation |
| 297 | on the axis normal to the view plane. |
| 298 | |
| 299 | Dragging the mouse up and down or scrolling the |
| 300 | mouse wheel will move the object along the Z of the camera. |
| 301 | |
| 302 | This is a stateless manipulator, result of the Apply functions is |
| 303 | determined only either by the mouse coordinates or by the mouse wheel notch. |
| 304 | */ |
| 305 | class ZMode:public TrackMode { |
| 306 | public: |
| 307 | /*! |
| 308 | @brief Return this manipulator's name. |
| 309 | |
| 310 | @return the constant string "ZMode" |
| 311 | */ |
| 312 | const char *Name () { |
| 313 | return "ZMode"; |
| 314 | }; |
| 315 | /*! |
| 316 | @brief Apply a translation, function of the user mouse drag action. |
| 317 | |
| 318 | The manipulated object is moved along the Z of the camera: |
| 319 | - Dragging the mouse down will move the object nearer to the camera. |
| 320 | - Dragging the mouse up will move the object farther from the camera. |
| 321 | |
| 322 | @param trackball the manipulator manager. |
| 323 | @param new_point the new mouse pointer coordinate. |
| 324 | */ |
| 325 | void Apply (Trackball * trackball, Point3f new_point); |
| 326 | /*! |
| 327 | @brief Apply a translation, function of the user mouse wheel action. |
| 328 | |
| 329 | The manipulated object is moved along the Z of the camera: |
| 330 | - Scrolling the mouse wheel down will move the object nearer to the camera. |
| 331 | - Scrolling the mouse wheel up will move the object farther from the camera. |
| 332 | |
| 333 | @param trackball the manipulator manager. |
| 334 | @param WheelNotch the mouse wheel notch. |
| 335 | */ |
| 336 | void Apply (Trackball * trackball, float WheelNotch); |
| 337 | /*! |
| 338 | @brief Render this manipulator. |
| 339 | |
| 340 | @param trackball the manipulator manager. |
| 341 | */ |
| 342 | void Draw (Trackball * trackball); |
| 343 | }; |
| 344 | |
| 345 | // Scale Mode. |
| 346 | /*! |
| 347 | @brief The scale manipulator. |
| 348 | |
| 349 | This manipulator implements a scaling transformation. |
| 350 | |
| 351 | Dragging the mouse up and down or scrolling the |
| 352 | mouse wheel will scale the object. |
| 353 | |
| 354 | This is a stateless manipulator, result of the Apply functions is |
| 355 | determined only either by the mouse coordinates or by the mouse wheel notch. |
| 356 | */ |
| 357 | class ScaleMode:public TrackMode { |
| 358 | public: |
| 359 | /*! |
| 360 | @brief Return this manipulator's name. |
| 361 | |
| 362 | @return the constant string "ScaleMode" |
| 363 | */ |
| 364 | const char *Name () { |
| 365 | return "ScaleMode"; |
| 366 | }; |
| 367 | /*! |
| 368 | @brief Apply a scaling, function of the user mouse drag action. |
| 369 | |
| 370 | The manipulated object is scaled in this way: |
| 371 | - Dragging the mouse up will scale the object to a smaller dimension. |
| 372 | - Dragging the mouse down will scale the object to a greater dimension. |
| 373 | |
| 374 | @param trackball the manipulator manager. |
| 375 | @param new_point the new mouse pointer coordinate. |
| 376 | */ |
| 377 | void Apply (Trackball * trackball, Point3f new_point); |
| 378 | /*! |
| 379 | @brief Apply a scaling, function of the user mouse wheel action. |
| 380 | |
| 381 | The manipulated object is scaled in this way: |
| 382 | - Scrolling the mouse wheel up will scale the object to a smaller dimension. |
| 383 | - Scrolling the mouse wheel down will scale the object to a greater dimension. |
| 384 | |
| 385 | @param trackball the manipulator manager. |
| 386 | @param WheelNotch the mouse wheel notch. |
| 387 | */ |
| 388 | void Apply (Trackball * trackball, float WheelNotch); |
| 389 | /*! |
| 390 | @brief Render this manipulator. |
| 391 | |
| 392 | @param trackball the manipulator manager. |
| 393 | */ |
| 394 | void Draw (Trackball * trackball); |
| 395 | }; |
| 396 | |
| 397 | // Axis mode. |
| 398 | /*! |
| 399 | @brief The one-directional manipulator. |
| 400 | |
| 401 | This manipulator implements a monodimensional translation |
| 402 | on a constrained direction. |
| 403 | |
| 404 | Dragging the mouse up and down or scrolling the |
| 405 | mouse wheel will move the object along the direction. |
| 406 | |
| 407 | This is a stateless manipulator, result of the Apply functions is |
| 408 | determined only either by the mouse coordinates or by the mouse wheel notch. |
| 409 | */ |
| 410 | class AxisMode:public TrackMode { |
| 411 | public: |
| 412 | /*! |
| 413 | @brief The line constructor. |
| 414 | |
| 415 | This manipulator needs to be initialized with a direction. |
| 416 | This constructor can initialize it with a Line3f. |
| 417 | |
| 418 | The line will be normalized. |
| 419 | @param ln the line that represent the direction. |
| 420 | */ |
| 421 | AxisMode (const Line3f & ln) |
| 422 | : axis (ln) { |
| 423 | } |
| 424 | /*! |
| 425 | @brief The origin-direction constructor. |
| 426 | |
| 427 | This manipulator needs to be initialized with a direction. |
| 428 | This constructor can initialize it with two Point3f, |
| 429 | representing a point and a vector. |
| 430 | |
| 431 | @param origin a point on the line. |
| 432 | @param direction the line direction. |
| 433 | */ |
| 434 | AxisMode (const Point3f & origin, const Point3f & direction) |
| 435 | : axis(Line3fN (origin, direction)) { |
| 436 | } |
| 437 | /*! |
| 438 | @brief Return this manipulator's name. |
| 439 | |
| 440 | @return the constant string "AxisMode" |
| 441 | */ |
| 442 | const char *Name () { |
| 443 | return "AxisMode"; |
| 444 | }; |
| 445 | /*! |
| 446 | @brief Apply a translation, function of the user mouse drag action. |
| 447 | |
| 448 | The manipulated object is moved along the direction. |
| 449 | |
| 450 | If the pointer ray is divergent from the direction the |
| 451 | object is not moved. |
| 452 | |
| 453 | @param trackball the manipulator manager. |
| 454 | @param new_point the new mouse pointer coordinate. |
| 455 | */ |
| 456 | void Apply (Trackball * trackball, Point3f new_point); |
| 457 | /*! |
| 458 | @brief Apply a translation, function of the user mouse wheel action. |
| 459 | |
| 460 | The manipulated object is moved along the direction. |
| 461 | |
| 462 | @param trackball the manipulator manager. |
| 463 | @param WheelNotch the mouse wheel notch. |
| 464 | */ |
| 465 | void Apply (Trackball * trackball, float WheelNotch); |
| 466 | /*! |
| 467 | @brief Render this manipulator. |
| 468 | |
| 469 | @param trackball the manipulator manager. |
| 470 | */ |
| 471 | void Draw (Trackball * trackball); |
| 472 | private: |
| 473 | /// The direction, stored as a normalized line. |
| 474 | Line3fN axis; |
| 475 | }; |
| 476 | |
| 477 | // Plane mode. |
| 478 | /*! |
| 479 | @brief The planar manipulator. |
| 480 | |
| 481 | This manipulator implements a bidimensional translation |
| 482 | on a constrained plane. |
| 483 | |
| 484 | This is a stateless manipulator, result of the Apply function is |
| 485 | determined only by the mouse coordinates. |
| 486 | */ |
| 487 | class PlaneMode:public TrackMode { |
| 488 | public: |
| 489 | /*! |
| 490 | @brief The plane costants constructor. |
| 491 | |
| 492 | This manipulator needs to be initialized with a plane. |
| 493 | This constructor can initialize it with the four coefficients |
| 494 | of the plane equation \f$ ax + by + cz + d = 0 \f$. |
| 495 | |
| 496 | @param a the first coefficient of the plane equation. |
| 497 | @param b the second coefficient of the plane equation. |
| 498 | @param c the third coefficient of the plane equation. |
| 499 | @param d the fourth coefficient of the plane equation. |
| 500 | */ |
| 501 | PlaneMode (float a, float b, float c, float d) |
| 502 | : plane(Plane3f(d,Point3f(a,b,c))){ |
| 503 | } |
| 504 | /*! |
| 505 | @brief The plane constructor. |
| 506 | |
| 507 | This manipulator needs to be initialized with a plane. |
| 508 | This constructor can initialize it with a Plane3f. |
| 509 | |
| 510 | @param pl the plane. |
| 511 | */ |
| 512 | PlaneMode (Plane3f & pl) |
| 513 | : plane(pl) { |
| 514 | } |
| 515 | /*! |
| 516 | @brief Return this manipulator's name. |
| 517 | |
| 518 | @return the constant string "PlaneMode" |
| 519 | */ |
| 520 | const char *Name () { |
| 521 | return "PlaneMode"; |
| 522 | }; |
| 523 | /*! |
| 524 | @brief Apply a translation, function of the user mouse drag action. |
| 525 | |
| 526 | The manipulated object is dragged in the plane. |
| 527 | |
| 528 | If the pointer ray is divergent from the plane the |
| 529 | object is not moved. |
| 530 | |
| 531 | @param trackball the manipulator manager. |
| 532 | @param new_point the new mouse pointer coordinate. |
| 533 | */ |
| 534 | void Apply (Trackball * trackball, Point3f new_point); |
| 535 | /*! |
| 536 | @brief Render this manipulator. |
| 537 | |
| 538 | @param trackball the manipulator manager. |
| 539 | */ |
| 540 | void Draw (Trackball * trackball); |
| 541 | private: |
| 542 | /// The plane. |
| 543 | Plane3f plane; |
| 544 | }; |
| 545 | |
| 546 | // Cylinder mode. |
| 547 | /*! |
| 548 | @brief The constrained rotation manipulator. |
| 549 | |
| 550 | This manipulator implements a rotation manipulator, that |
| 551 | make the rotation constrained around a given axis. |
| 552 | |
| 553 | The user can either drag the mouse or scroll the wheel, |
| 554 | in either cases the rotation's angle is influenced by |
| 555 | the radius of the trackball. |
| 556 | |
| 557 | This is a stateless manipulator, result of the Apply functions is |
| 558 | determined only either by the mouse coordinates or by the mouse wheel notch. |
| 559 | */ |
| 560 | class CylinderMode:public TrackMode { |
| 561 | public: |
| 562 | /*! |
| 563 | @brief The line constructor. |
| 564 | |
| 565 | This manipulator needs to be initialized with an axis. |
| 566 | This constructor can initialize it with a Line3f. |
| 567 | |
| 568 | The line will be normalized. |
| 569 | @param ln the line that represent the axis. |
| 570 | @param s a rotational snap angle non negative |
| 571 | */ |
| 572 | CylinderMode (Line3fN & ln,float s=0.0f) |
| 573 | : axis (ln), snap(s){ |
| 574 | assert(snap>=0.0); |
| 575 | } |
| 576 | /*! |
| 577 | @brief The origin-direction constructor. |
| 578 | |
| 579 | This manipulator needs to be initialized with a axis. |
| 580 | This constructor can initialize it with two Point3f, |
| 581 | representing a point and a vector. |
| 582 | |
| 583 | @param origin a point on the axis. |
| 584 | @param direction the axis direction. |
| 585 | @param s a rotational snap angle (non negative) |
| 586 | */ |
| 587 | CylinderMode (const Point3f & origin, const Point3f & direction,float s=0.0f) |
| 588 | : axis (Line3fN(origin,direction)), snap(s){ |
| 589 | assert(snap>=0.0); |
| 590 | } |
| 591 | /*! |
| 592 | @brief Return this manipulator's name. |
| 593 | |
| 594 | @return the constant string "CylinderMode" |
| 595 | */ |
| 596 | const char *Name () { |
| 597 | return "CylinderMode"; |
| 598 | }; |
| 599 | /*! |
| 600 | @brief Apply a rotation, function of the user mouse drag action. |
| 601 | |
| 602 | The manipulated object is rotated around the axis. |
| 603 | |
| 604 | if the axis is too perpendicular to view plane, the angle is specified |
| 605 | only by the vertical component of the mouse drag and the radius. |
| 606 | |
| 607 | @param trackball the manipulator manager. |
| 608 | @param new_point the new mouse pointer coordinate. |
| 609 | */ |
| 610 | void Apply (Trackball * trackball, Point3f new_point); |
| 611 | /*! |
| 612 | @brief Apply a rotation, function of the user mouse wheel action. |
| 613 | |
| 614 | The manipulated object is rotated around the axis. |
| 615 | |
| 616 | @param trackball the manipulator manager. |
| 617 | @param WheelNotch the mouse wheel notch. |
| 618 | */ |
| 619 | void Apply (Trackball * trackball, float WheelNotch); |
| 620 | /*! |
| 621 | @brief Render this manipulator. |
| 622 | |
| 623 | @param trackball the manipulator manager. |
| 624 | */ |
| 625 | void Draw (Trackball * trackball); |
| 626 | private: |
| 627 | /// The axis, stored as a normalized line. |
| 628 | Line3fN axis; |
| 629 | /// The rotational snap value |
| 630 | float snap; |
| 631 | }; |
| 632 | |
| 633 | // Path mode. |
| 634 | /*! |
| 635 | @brief The path constrained manipulator. |
| 636 | |
| 637 | This manipulator moves the object along an eventually closed path. |
| 638 | |
| 639 | The user can either drag the mouse or scroll the wheel, |
| 640 | when the user drags the mouse, the object tries to slide toward it. |
| 641 | |
| 642 | The object is assumed to initially be on the same position |
| 643 | of the first point on the path. |
| 644 | |
| 645 | This is a \b stateful manipulator, result of the Apply functions is |
| 646 | determined by the objects's position along the path and |
| 647 | by either the mouse coordinates or the mouse wheel notch. |
| 648 | */ |
| 649 | class PathMode:public TrackMode { |
| 650 | public: |
| 651 | /*! |
| 652 | @brief The vector-boolean constructor. |
| 653 | |
| 654 | The vector passed to build the path is copied locally. |
| 655 | The boolean value specifies if the path is to be closed. |
| 656 | If the boolean value is not specified, the path is not closed. |
| 657 | |
| 658 | @warning the path is \b not assumed to have 0-length segments, so, if you want to close the path, please <b>do not</b> add a copy of the first point on the end of the vector. |
| 659 | |
| 660 | @param pts the path nodes. |
| 661 | @param w a boolean value that closes the path. |
| 662 | */ |
| 663 | PathMode ( const std::vector < Point3f > &pts, bool w = false) |
| 664 | : points(), wrap(w), current_state(0), initial_state(0), old_hitpoint() |
| 665 | { |
| 666 | Init(pts); |
| 667 | assert(min_seg_length > 0.0f); |
| 668 | } |
| 669 | /*! |
| 670 | @brief The segment constructor. |
| 671 | |
| 672 | If the path is a simple segment, it can be specified just with the endpoints. |
| 673 | |
| 674 | @param start the starting point. |
| 675 | @param end the ending point. |
| 676 | */ |
| 677 | PathMode ( const Point3f &start, const Point3f &end ) |
| 678 | : points(), wrap(false), current_state(0), initial_state(0), old_hitpoint() |
| 679 | { |
| 680 | points.push_back(start); |
| 681 | points.push_back(end); |
| 682 | path_length=Distance(start,end); |
| 683 | min_seg_length=path_length; |
| 684 | assert(min_seg_length > 0.0f); |
| 685 | } |
| 686 | /*! |
| 687 | @brief Return this manipulator's name. |
| 688 | |
| 689 | @return the constant string "PathMode" |
| 690 | */ |
| 691 | const char *Name () { |
| 692 | return "PathMode"; |
| 693 | }; |
| 694 | /*! |
| 695 | @brief Apply a translation, function of the user mouse drag action. |
| 696 | |
| 697 | The manipulated object is moved along the path. |
| 698 | This function implements an algorithm that makes |
| 699 | the object try to slide on the path towards the |
| 700 | mouse pointer. |
| 701 | |
| 702 | @param trackball the manipulator manager. |
| 703 | @param new_point the new mouse pointer coordinate. |
| 704 | */ |
| 705 | void Apply (Trackball * trackball, Point3f new_point); |
| 706 | /*! |
| 707 | @brief Apply a translation, function of the user mouse wheel action. |
| 708 | |
| 709 | The manipulated object is moved along the path. |
| 710 | A step of the mouse wheel makes the object slide |
| 711 | by a distance equal to the half of the shortest |
| 712 | segment on the path. |
| 713 | |
| 714 | @param trackball the manipulator manager. |
| 715 | @param WheelNotch the mouse wheel notch. |
| 716 | */ |
| 717 | void Apply (Trackball * trackball, float WheelNotch); |
| 718 | /*! |
| 719 | @brief Render this manipulator. |
| 720 | |
| 721 | @param trackball the manipulator manager. |
| 722 | */ |
| 723 | void Draw (Trackball * trackball); |
| 724 | /*! |
| 725 | @brief The begin action function. |
| 726 | |
| 727 | This function is to be called at the begin of an user action. |
| 728 | */ |
| 729 | void SetAction (); |
| 730 | /*! |
| 731 | @brief The reset function. |
| 732 | |
| 733 | This function reset the object position to the initial point. |
| 734 | */ |
| 735 | void Reset (); |
| 736 | /*! |
| 737 | @brief Try to set the inital point. |
| 738 | |
| 739 | This function try to set the starting point in the point |
| 740 | passed as parameter, if the point passed does not reside |
| 741 | on the path, the start is put on the closest point on it. |
| 742 | |
| 743 | @param p the point wished for the start. |
| 744 | @return the starting point on the path. |
| 745 | */ |
| 746 | Point3f SetStartNear(Point3f p); |
| 747 | /*! |
| 748 | @brief The (non) avaibility to manipulator changes inside an action. |
| 749 | |
| 750 | This manipulator has an internal state and does not allow a |
| 751 | switch in the middle of a function. |
| 752 | |
| 753 | @return the costant boolean true. |
| 754 | */ |
| 755 | bool isSticky(); |
| 756 | /*! |
| 757 | @brief The undo function. |
| 758 | |
| 759 | This function recreates the state present before the last Apply() call. |
| 760 | */ |
| 761 | void Undo(); |
| 762 | private: |
| 763 | /*! |
| 764 | @brief The data initializer. |
| 765 | |
| 766 | Initialize the internal state and checks params validity. |
| 767 | |
| 768 | @param points the path nodes. |
| 769 | */ |
| 770 | void Init(const std::vector < Point3f > &points); |
| 771 | /*! |
| 772 | @brief The state interpreter. |
| 773 | |
| 774 | Given the state, return the current point, the previous node and |
| 775 | the next node on the path. |
| 776 | The algoritm is linear in the node paths. |
| 777 | |
| 778 | @param state the given state. |
| 779 | @param point is set to the current point. |
| 780 | @param prev_point is set to the point of current point's previous node. |
| 781 | @param next_point is set to the point of current point's next node. |
| 782 | */ |
| 783 | void GetPoints(float state, Point3f & point, Point3f & prev_point, Point3f & next_point); |
| 784 | /*! |
| 785 | @brief The state normalizer. |
| 786 | |
| 787 | Normalize a given state in the right interval: |
| 788 | - \f$ [0 \ldots 1] \f$ if the path is open. |
| 789 | - \f$ [0 \ldots 1) \f$ if the path is closed (because it wraps). |
| 790 | |
| 791 | @param state the given state. |
| 792 | */ |
| 793 | float Normalize(float state); |
| 794 | /*! |
| 795 | @brief Compute the new point and the \f$\Delta\f$-state. |
| 796 | |
| 797 | Given a state and the mouse coords ray, computes the new |
| 798 | state point and return the \f$\Delta\f$-state. |
| 799 | The algoritm is linear in the node paths. |
| 800 | |
| 801 | @param state the given state. |
| 802 | @param ray the ray relative to mouse coords. |
| 803 | @param hit_point is set to the new state point. |
| 804 | @return the \f$\Delta\f$-state. |
| 805 | */ |
| 806 | float HitPoint(float state, Ray3fN ray, Point3f &hit_point); |
| 807 | /*! |
| 808 | @brief Compute the verse to follow for slide nearer to a given point. |
| 809 | |
| 810 | Given the current state point, the point of previus and |
| 811 | next node and a reference point, compute the verse to |
| 812 | follow for the object to come closer to the reference point. |
| 813 | |
| 814 | @param reference_point |
| 815 | @param current_point |
| 816 | @param prev_point |
| 817 | @param next_point |
| 818 | @return -1, 0 or 1 if the verse is respectively towars the startpoint, null or towards the endpoint. |
| 819 | */ |
| 820 | int Verse(Point3f reference_point,Point3f current_point,Point3f prev_point,Point3f next_point); |
| 821 | |
| 822 | /// The node vector. |
| 823 | std::vector < Point3f > points; |
| 824 | /// True if the path is closed, false otherwise. |
| 825 | bool wrap; |
| 826 | /// The current state. |
| 827 | float current_state; |
| 828 | /// The initial state. |
| 829 | float initial_state; |
| 830 | /// The path length. |
| 831 | float path_length; |
| 832 | /// The length of the shostest path segment |
| 833 | float min_seg_length; |
| 834 | /// The point relative to the old state. |
| 835 | Point3f old_hitpoint; |
| 836 | /// current_state after an Undo() call. |
| 837 | float undo_current_state; |
| 838 | /// old_hitpoint after an Undo() call. |
| 839 | Point3f undo_old_hitpoint; |
| 840 | }; |
| 841 | |
| 842 | // Area mode. |
| 843 | /*! |
| 844 | @brief The area constrained manipulator. |
| 845 | |
| 846 | This manipulator moves the object inside a poligonal area. |
| 847 | |
| 848 | The user can drag the object inside a planar area, defined by a polygon. |
| 849 | The polygon can be non convex, and is specified with a vector of vertexes. |
| 850 | |
| 851 | If the object's trajectory intersects some poligon side, it tries to slide |
| 852 | around it, in a <em>"rubber band flavoured"</em> way. |
| 853 | |
| 854 | The object is assumed to initially be on the same position of the |
| 855 | first vertex. |
| 856 | |
| 857 | This is a \b stateful manipulator, result of the Apply function is |
| 858 | determined by the objects's position inside the area and |
| 859 | by the mouse coordinates. |
| 860 | */ |
| 861 | class AreaMode:public TrackMode { |
| 862 | public: |
| 863 | /*! |
| 864 | @brief The constructor. |
| 865 | |
| 866 | From the given vector, is calculated the plane of the polygon, then |
| 867 | every point in the vector is projected on this plane. |
| 868 | |
| 869 | The vector passed to build the polygon is copied locally. |
| 870 | |
| 871 | @warning The vector is assumed to be formed of \b non collinear points. |
| 872 | @warning The polygon is \b not assumed to have 0-length sides, so please <b>do not</b> add a copy of the first point on the end of the vector. |
| 873 | |
| 874 | @param pts the vertexes vector. |
| 875 | */ |
| 876 | AreaMode (const std::vector < Point3f > &pts) |
| 877 | { |
| 878 | Init(pts); |
| 879 | assert(min_side_length > 0.0f); |
| 880 | } |
| 881 | /*! |
| 882 | @brief Return this manipulator's name. |
| 883 | |
| 884 | @return the constant string "AreaMode" |
| 885 | */ |
| 886 | const char *Name () { |
| 887 | return "AreaMode"; |
| 888 | }; |
| 889 | /*! |
| 890 | @brief Apply a translation, function of the user mouse drag action. |
| 891 | |
| 892 | The manipulated object is moved inside the poligon. |
| 893 | This function implements an algorithm that makes |
| 894 | the object try to slide around the polygon borders. |
| 895 | |
| 896 | @param trackball the manipulator manager. |
| 897 | @param new_point the new mouse pointer coordinate. |
| 898 | */ |
| 899 | void Apply (Trackball * trackball, Point3f new_point); |
| 900 | /*! |
| 901 | @brief Render this manipulator. |
| 902 | |
| 903 | @param trackball the manipulator manager. |
| 904 | */ |
| 905 | void Draw (Trackball * trackball); |
| 906 | /*! |
| 907 | @brief The begin action function. |
| 908 | |
| 909 | This function is to be called at the begin of an user action. |
| 910 | */ |
| 911 | void SetAction (); |
| 912 | /*! |
| 913 | @brief The reset function. |
| 914 | |
| 915 | This function reset the object position to the initial point. |
| 916 | */ |
| 917 | void Reset (); |
| 918 | /*! |
| 919 | @brief Try to set the inital point. |
| 920 | |
| 921 | This function try to set the starting point in the point |
| 922 | passed as parameter, if the point passed does not reside |
| 923 | in the area, the start is put in the closest point on it. |
| 924 | |
| 925 | @param p the point wished for the start. |
| 926 | @return the starting point in the area. |
| 927 | */ |
| 928 | Point3f SetStartNear(Point3f p); |
| 929 | /*! |
| 930 | @brief The (non) avaibility to manipulator changes inside an action. |
| 931 | |
| 932 | This manipulator has an internal state and does not allow a |
| 933 | switch in the middle of a function. |
| 934 | |
| 935 | @return The costant boolean true. |
| 936 | */ |
| 937 | bool isSticky(); |
| 938 | /*! |
| 939 | @brief The undo function. |
| 940 | |
| 941 | This function recreates the state present before the last Apply() call. |
| 942 | */ |
| 943 | void Undo(); |
| 944 | private: |
| 945 | /*! |
| 946 | @brief The data initializer. |
| 947 | |
| 948 | Initialize the internal state and checks params validity. |
| 949 | |
| 950 | @param pts The polygon vertexes. |
| 951 | */ |
| 952 | void Init(const std::vector < Point3f > &pts); |
| 953 | /*! |
| 954 | @brief Point in Polygon test. |
| 955 | |
| 956 | Checks if a given point relies inside the poligon area, |
| 957 | using the ray tracing algorithm, linear in the number |
| 958 | of vertexes. |
| 959 | |
| 960 | @param point The point to test. |
| 961 | @return true if the point is inside the polygon, false otherwise. |
| 962 | */ |
| 963 | bool Inside(Point3f point); |
| 964 | /*! |
| 965 | @brief Try to move the object inside the polygon |
| 966 | |
| 967 | Given a point inside the area and a destination in the |
| 968 | poligon plane, try to move the point toward the destination, |
| 969 | sliding on any evenual border of the polygon. |
| 970 | The object can be imagined tied with a rubber attached to |
| 971 | destination, so it won't go back from it to slide around a border. |
| 972 | |
| 973 | The algorithm is quadratic in the number of vertexes |
| 974 | (worst case, really really unlikely). |
| 975 | |
| 976 | @param start the starting point <b>assumed inside</b>. |
| 977 | @param end the destination <b>assumed in the plane</b>. |
| 978 | @return the final move vector. |
| 979 | */ |
| 980 | Point3f Move(Point3f start,Point3f end); |
| 981 | |
| 982 | /// The vertexes vector. |
| 983 | std::vector < Point3f > points; |
| 984 | /// True in time inteval between a call to SetAction () and a call to Apply() |
| 985 | bool begin_action; |
| 986 | /// One of the two dimensions used during the point in polygon test. |
| 987 | int first_coord_kept; |
| 988 | /// One of the two dimensions used during the point in polygon test. |
| 989 | int second_coord_kept; |
| 990 | /// The length of the shortest border |
| 991 | float min_side_length; |
| 992 | /// The current status. |
| 993 | Point3f status; |
| 994 | /// The screen space differenve between the object and the cursor during an action. |
| 995 | Point3f delta_mouse; |
| 996 | /// The old status. |
| 997 | Point3f old_status; |
| 998 | /// The initial status. |
| 999 | Point3f initial_status; |
| 1000 | /// The polygon plane |
| 1001 | Plane3f plane; |
| 1002 | /// The rubberband handle (current destination in Move()) |
| 1003 | Point3f rubberband_handle ; |
| 1004 | /// Current action's object trace |
| 1005 | std::vector < Point3f > path; |
| 1006 | /// begin_action after an Undo() call. |
| 1007 | bool undo_begin_action; |
| 1008 | /// status after an Undo() call. |
| 1009 | Point3f undo_status; |
| 1010 | /// delta_mouse after an Undo() call. |
| 1011 | Point3f undo_delta_mouse; |
| 1012 | /// old_status after an Undo() call. |
| 1013 | Point3f undo_old_status; |
| 1014 | /// rubberband_handle after an Undo() call. |
| 1015 | Point3f undo_rubberband_handle; |
| 1016 | /// path endpoint after an Undo() call. |
| 1017 | unsigned int undo_path_index; |
| 1018 | |
| 1019 | }; |
| 1020 | |
| 1021 | }//namespace |
| 1022 | |
| 1023 | #endif |