// Copyright (C) 2002-2003 Gideon May (gideon@computer.org) // // Permission to copy, use, sell and distribute this software is granted // provided this copyright notice appears in all copies. // Permission to modify the code and to distribute modified code is granted // provided this copyright notice appears in all copies, and a notice // that the code was modified is included with the copyright notice. // // This software is provided "as is" without express or implied warranty, // and with no claim as to its suitability for any purpose. #ifdef __USE_OSX_IMPLEMENTATION__ #include #include "missing.hpp" #include #undef check #endif #include #include #include #include #include #include #include #include #include "producer_ptr.hpp" using namespace boost::python; BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(setDistance_overloads, Producer::Trackball::setDistance, 1, 2); BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(translate_overloads, Producer::Trackball::translate, 3, 4); BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(input_overloads, Producer::Trackball::input, 3, 4); BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(reset_overloads, Producer::Trackball::reset, 0, 1); namespace PyProd { void init_Trackball() { class_, bases, boost::noncopyable > trackball("Trackball", no_init); scope trackball_scope(trackball); trackball .def(init<>()) .def("mapButtonState", &Producer::Trackball::mapButtonState) .def("setOrientation", &Producer::Trackball::setOrientation) .def("setDistance", &Producer::Trackball::setDistance, setDistance_overloads()) .def("translate", &Producer::Trackball::translate, translate_overloads()) .def("input", &Producer::Trackball::input, input_overloads()) .def("reset", &Producer::Trackball::reset, reset_overloads()) .def("getMatrix", (Producer::Matrix& (Producer::Trackball::*)(void)) &Producer::Trackball::getMatrix, return_internal_reference<>()) .def("setReference", &Producer::Trackball::setReference) ; #define ENUM_UPDATE_MODE(VALUE) \ (update_mode.value(#VALUE, Producer::Trackball::VALUE), \ trackball.def(#VALUE, object(Producer::Trackball::VALUE))) enum_ update_mode("UpdateMode"); ENUM_UPDATE_MODE(UpdateNone); ENUM_UPDATE_MODE(UpdateRotation); ENUM_UPDATE_MODE(UpdatePan); ENUM_UPDATE_MODE(UpdateDistance); #define ENUM_ORIENTATION(VALUE) \ (orientation.value(#VALUE, Producer::Trackball::VALUE), \ trackball.def(#VALUE, object(Producer::Trackball::VALUE))) enum_ orientation("Orientation"); ENUM_ORIENTATION(Y_UP); ENUM_ORIENTATION(Z_UP); #define ENUM_TRANSFORM_ORDER(VALUE) \ (transform_order.value(#VALUE, Producer::Trackball::VALUE), \ trackball.def(#VALUE, object(Producer::Trackball::VALUE))) enum_ transform_order("TransformOrder"); ENUM_TRANSFORM_ORDER(RotateTranslate); ENUM_TRANSFORM_ORDER(TranslateRotate); #define ENUM_ROTATIONAL_MODE(VALUE) \ (rotational_mode.value(#VALUE, Producer::Trackball::VALUE), \ trackball.def(#VALUE, object(Producer::Trackball::VALUE))) enum_ rotational_mode("RotationalMode"); ENUM_ROTATIONAL_MODE(FixedAxis); ENUM_ROTATIONAL_MODE(Spherical); #define ENUM_THROW_MODE(VALUE) \ (throw_mode.value(#VALUE, Producer::Trackball::VALUE), \ trackball.def(#VALUE, object(Producer::Trackball::VALUE))) enum_ throw_mode("ThrowMode"); ENUM_THROW_MODE(ThrowNone); ENUM_THROW_MODE(ThrowRotation); ENUM_THROW_MODE(ThrowPan); ENUM_THROW_MODE(ThrowPanRotation); ENUM_THROW_MODE(ThrowDistance); ENUM_THROW_MODE(ThrowRotationDistance); ENUM_THROW_MODE(ThrowPanDistance); ENUM_THROW_MODE(ThrowRotationPanDistance); #define ENUM_OPERATIONAL_MODE(VALUE) \ (operational_mode.value(#VALUE, Producer::Trackball::VALUE), \ trackball.def(#VALUE, object(Producer::Trackball::VALUE))) enum_ operational_mode("OperationalMode"); ENUM_OPERATIONAL_MODE(DefaultOperationalMode); ENUM_OPERATIONAL_MODE(InventorLike); ENUM_OPERATIONAL_MODE(PerformerLike); } } #if 0 class PR_EXPORT Trackball { public : //------------------------------------------------------------------------- // Constructor Trackball( void ); //------------------------------------------------------------------------- // Destructor ~Trackball( void ) {} //------------------------------------------------------------------------- // Update Mode - Enumerants, specifying update taken by the trackball. // UpdateRotation - rotation about a fixed axis // UpdatePan - movements up and down relative to and parallel to // the screen // UpdateDistance - Displacement toward and away from viewer // enum UpdateMode { UpdateNone, UpdateRotation, UpdatePan, UpdateDistance }; //------------------------------------------------------------------------- // mapButtonState() // getMappedButtonState() // state - Bitwise or of push buttons // mode - One of UpdateMode's // // mapButtonState() maps a button or combination of button presses to an // update type. // getMappedButtonState() returns the UpdateMode associated with a button // state. void mapButtonState( unsigned int state, UpdateMode mode ) { _buttonMap[state] = mode; } UpdateMode getMappedButtonState( unsigned int state ) const { std::map::const_iterator p; p = _buttonMap.find( state ); if( p != _buttonMap.end() ) return p->second; else return UpdateNone; } //------------------------------------------------------------------------- // Orientation - Enumerants referring to default orientation of the // identity. // Y_UP - Orientation referring to and relative to the screen: // X right, Y UP, and Z toward the eye. // Z_UP - X right, Z UP, Y away from the eye. // // setOrientation() // getOrientation() // // mode - One of Y_UP, or Z_UP. // // sets/gets the orientation of the identity. // enum Orientation { Y_UP, Z_UP }; void setOrientation( Orientation mode ); Orientation getOrientation() const { return _orientation; } void setComputeOrientation( bool flag ) { _computeOrientation = flag; } bool getComputeOrientation() { return _computeOrientation; } //------------------------------------------------------------------------- // Transform Order - Enumerants referring to the order in which rotation/ // translation operations should occur. // RotateTranslate - Operation applies a rotation matrix to a translation // matrix. This is the style of the Inventor trackball, // for example // TranslateRotate - Operation applies a translation matrix to a rotation // matrix. This is the style of the Performer trackball, // for example. // // setTransformOrder() // getTransformOrder() // mode - One of RotateTranslate or TranslateRotate // // set/get Transform order. enum TransformOrder { RotateTranslate, TranslateRotate }; void setTransformOrder( TransformOrder mode ) { _transform_order = mode; } TransformOrder getTransformOrder() const { return _transform_order; } //------------------------------------------------------------------------- // RotationalMode - Enumerants referring to the method used for doing // rotations. // FixedAxis - Refers to applying rotations directly around the // upward axis, or the axis perpendicular to the screen. // Performer trackball functions like this, for example. // Spherical - Refers to applying rotations in a spherical manner. // Inventor trackball functions like this, for example. // // setRotationalMode() // getRotationalMode() - set/get the Rotational mode. // // mode - One of FixedAxis or Spherical // enum RotationalMode { FixedAxis, Spherical }; void setRotationalMode( RotationalMode mode ) { _rotational_mode = mode; } RotationalMode getRotationalMode() const { return _rotational_mode; } //------------------------------------------------------------------------- // ThrowMode - Enumerants referring to modes used to control which operations // may be allowed to continuously update. // ThrowRotation - Allows continuous update of Rotation operation. // ThrowPan - Allows continuous update of Pan operation. // ThrowPanRotation - Allows continuous update of Rotation and Pan // operations. // ThrowDistance - Allows continuous update of Distancing operation. // ThrowRotationDistance, - Allows continuous update of Rotation and // Distancing operations. // ThrowPanDistance - Allows continuous update of Pan and Distancing // operations // ThrowRotationPanDistance - Allows continuous update of all three of // Rotation, Pan and Distancing operations. // // setThrowMode() // getThrowMode() - set/get Throw mode // addThrowMode() // removeThrowMode() - Adds and removes a single attribute of the // ThrowMode. For example, if current ThrowMode // is ThrowRotationPanDistance and // removeThrowMode( ThrowDistance ); // is called, resulting ThrowMode will be // ThrowModePanRotation // // mode - One of the operations specfied in ThrowMode enumerant. // enum ThrowMode { ThrowNone, ThrowRotation, ThrowPan, ThrowPanRotation, ThrowDistance, ThrowRotationDistance, ThrowPanDistance, ThrowRotationPanDistance }; void setThrowMode( ThrowMode throw_mode ) { _throw_mode = throw_mode; } ThrowMode getThrowMode() const{ return _throw_mode; } void addThrowMode( ThrowMode throw_mode ) { unsigned int t0 = static_cast(_throw_mode); unsigned int t1 = static_cast(throw_mode); _throw_mode = static_cast(t0|t1); } void removeThrowMode( ThrowMode throw_mode ) { unsigned int t0 = static_cast(_throw_mode); unsigned int t1 = static_cast(throw_mode); _throw_mode = static_cast(t0&(~t1)); } //------------------------------------------------------------------------- // OperationalMode - Enumerants specifying a configuration of button // mappings, orientation,transform order, rotational mode // throw mode, and auto scale that correspond to some // familiar behavior. // DefaultOperationalMode - The default for this trackball // InventorLike - Behavior similar to the Inventor trackball // PerformerLike - Behavior similar to the Performer trackball // // setOperationalMode() // getOperationalMode() - sets/gets the operational mode. May be changed on // the fly. // mode - one of DefaultOperationalMode, InventorLike or // PerformerLike enum OperationalMode { DefaultOperationalMode, InventorLike, PerformerLike }; void setOperationalMode( OperationalMode ); OperationalMode getOperationalMode() const { return _operational_mode; } //------------------------------------------------------------------------- // setAutoScale() // getAutoScale() - set/get auto scale. Auto scale causes the scaling of // translations and distance displacements based on the // distance from the viewing center // flag - boolean // // setScale() // getScale() - set/get scaling. Ignored when auto scale is enabled. // Provides a method of controlling the scale if auto // scaling is off. // // scale - floating point number specifying the scale // // setPanFOV() // getPanFOV() - set/get the panning FOV. Used to compute the distance // a Pan operation should translate when auto scale is on. // This should correspond to the viewing angle of the 3D // viewing frustum. void setAutoScale( bool flag ){ _auto_scale = flag; } bool getAutoScale( void ) const { return _auto_scale; } void setMinimumScale( float minScale ); void setScale( float scale ); float getScale( void ) const { return _scale; } void setRotScale( float scale ) { _rscale = scale; } float getRotScale( void ) const { return _rscale; } void setPanFOV( float panFov ) { _pan_fov = panFov; } float getPanFOV( void ) const { return _pan_fov; } //------------------------------------------------------------------------- // setReference() - Record the current state of the transform matrix as // a reference state. When reset() is called, the // transform matrix will return to the reference state. void setReference( void ); //------------------------------------------------------------------------- // setDistance() // getDistance() // resetDistance() // distanceHasChanged() - set/get the distance to the viewing center of the // scene. resetDistance() returns the distance to the // reference distance. distanceHasChanged() returns // a boolean which can be used for outside operations // based on whether the viewing distance has changed. // void setMinimumDistance( float dist ); void setDistance( float dist, bool do_update=true ); void resetDistance(bool do_update=true ); float getDistance() const { return _distance; } bool distanceHasChanged() const { return _distance_has_changed; } //------------------------------------------------------------------------- // setTranslation() // getTranslation() - set/get the current translation transformation matrix // setRotation() // getRotation() - set/get the current rotation transformation matrix // Matrix - matrix used to set or returned from a get. void setTranslation( const Matrix tmat ) { T = tmat; } const Matrix & getTranslation() const { return T; } void setRotation( const Matrix rmat ) { R = rmat; } const Matrix & getRotation() const { return R; } void setScaleMatrix( const Matrix smat ) { S = smat; } const Matrix & getScaleMatrix(void) { return S; } //------------------------------------------------------------------------- // translate() - translate the transform matrix from its current state // x, y, z - translation parameters // rotate() - rotate the transform matrix from its current state // rad - rotation angle in radians // x, y, z - Rotation vector (rotate around x,y,z) void translate( float x, float y, float z, bool do_update=true ); void rotate( float rad, float x, float y, float z, bool do_update=true); void scale( float sx, float sy, float sz, bool do_update=true ); //------------------------------------------------------------------------- // input() - Updates the Trackball with new input, and/or for continuous // matrix updates. // mx, my - "Normalized" values. For example, an application receiving // mouse events on a window that is 800x800 pixels in size, // should normalize the mouse positions such that the range // will be -1.0 at the left most side of the window and 1.0 at // the right most side of the window for mx. // mbutton - button state. A bit representation of pressed/released // buttons. // void input( float mx, float my, unsigned int mbutton, bool do_update=true ); //------------------------------------------------------------------------- // update() - Updates the transform matrix. Called internally from // input(), translate() and rotate(), unless do_update=false. // restart() - Restarts the initial x an y reference values. For example, // if a button has been newly pressed and before motion events // begin to change. Used internally, for the most part. // reset() - Returns the transform matrix to its reference state. void update( void ); void restart( float mx, float my ); void reset( bool do_update=true ); //------------------------------------------------------------------------- // getMatrix() - Returns the transform matrix as an Matrix. Note that // this matrix is not valid until update() has been called. Matrix &getMatrix( void ) { return TX; } const Matrix &getMatrix( void ) const { return TX; } private : Matrix S; // Scale matrix Matrix O; // Orientation Matrix Matrix R; // Rotation Matrix Matrix Rr; // Reference Rotation Matrix Matrix T; // Translation Matrix Matrix Tr; // Reference Translation :Matrix Matrix TX; // Transform matrix float _distance; float _min_distance; bool _min_distance_is_set; float _lastx, _lasty; float _distance_ref; Orientation _orientation; unsigned int _mbutton; bool _auto_scale; bool _minimum_scale_is_set; float _minimum_scale; float _scale; float _rscale; float _pan_fov; UpdateMode _update_mode; std::map _buttonMap; ThrowMode _throw_mode; TransformOrder _transform_order; RotationalMode _rotational_mode; OperationalMode _operational_mode; bool _distance_has_changed; bool _computeOrientation; void updateScale(); }; } // namespace Producer #endif