Cleaned up Degree, Radian, AngleAxis and Quaternion classes 68/37068/3
authorKimmo Hoikka <kimmo.hoikka@samsung.com>
Wed, 15 Apr 2015 19:30:41 +0000 (20:30 +0100)
committerKimmo Hoikka <kimmo.hoikka@samsung.com>
Thu, 16 Apr 2015 13:53:50 +0000 (14:53 +0100)
- Inline Degree, Radian and AngleAxis types to avoid unnecessary exports / export table lookups in using code
- Change AngleAxis to store a Radian angle for better accuracy
- Make Quaternion explicitly take a Radian as constructor

Change-Id: I61d807dc35e8b7b4cc0510b45c5019f12785878a

examples/blocks/blocks-example.cpp
examples/cluster/cluster-example.cpp
examples/new-window/new-window-example.cpp
examples/radial-menu/radial-sweep-view-impl.cpp
examples/shadow-bone-lighting/shadow-bone-lighting-example.cpp

index 972f95c..eb64275 100644 (file)
@@ -176,8 +176,8 @@ struct WobbleConstraint
    *
    * @param[in] deviation The max. deviation of wobble effect in degrees.
    */
-  WobbleConstraint(float deviation)
-  : mDeviation(Radian(Degree(deviation)))
+  WobbleConstraint( Degree deviation )
+  : mDeviation( deviation )
   {
 
   }
@@ -196,7 +196,7 @@ struct WobbleConstraint
     current = Quaternion(mDeviation * f, Vector3::ZAXIS);
   }
 
-  const float mDeviation;           ///< Deviation factor in radians.
+  Radian mDeviation;           ///< Deviation factor in radians.
 };
 
 } // unnamed namespace
@@ -285,7 +285,7 @@ private:
     mPaddleImage.SetSize( mPaddleFullSize );
 
     mWobbleProperty = mPaddle.RegisterProperty(WOBBLE_PROPERTY_NAME, 0.0f);
-    Constraint wobbleConstraint = Constraint::New<Quaternion>( mPaddle, Actor::Property::ORIENTATION, WobbleConstraint(10.0f));
+    Constraint wobbleConstraint = Constraint::New<Quaternion>( mPaddle, Actor::Property::ORIENTATION, WobbleConstraint(Degree( 10.0f )));
     wobbleConstraint.AddSource( LocalSource(mWobbleProperty) );
     wobbleConstraint.Apply();
 
index c6d97a9..59af760 100644 (file)
@@ -186,7 +186,7 @@ struct CarouselEffectOrientationConstraint
   void operator()( Vector2& current, const PropertyInputContainer& inputs )
   {
     Vector3 axis;
-    float angle;
+    Radian angle;
     inputs[0]->GetQuaternion().ToAxisAngle( axis, angle );
 
     current.x = cosf(angle);
@@ -242,7 +242,7 @@ struct ShearEffectConstraint
     // Channel this shear value into either the X or Y axis depending on
     // the component mask passed in.
     Vector3 axis;
-    float angle;
+    Radian angle;
     inputs[1]->GetQuaternion().ToAxisAngle( axis, angle );
     Vector2 direction( cosf(angle), sinf(angle) );
     float yield = direction.x * mComponentMask.x + direction.y * mComponentMask.y;
index 877aa2d..fe1fa9f 100644 (file)
@@ -264,7 +264,6 @@ FrameBufferImage NewWindowController::CreateFrameBufferForImage(const char* imag
   cameraActor.SetNearClippingPlane(1.0f);
   cameraActor.SetAspectRatio(FBOSize.width / FBOSize.height);
   cameraActor.SetType(Dali::Camera::FREE_LOOK); // camera orientation based solely on actor
-  cameraActor.SetOrientation(Quaternion(M_PI, Vector3::YAXIS));
   cameraActor.SetPosition(0.0f, 0.0f, ((FBOSize.height * 0.5f) / tanf(Math::PI * 0.125f)));
   stage.Add(cameraActor);
 
index bc9925c..50736a2 100644 (file)
@@ -26,10 +26,8 @@ namespace
  * Method to project a point on a circle of radius halfSide at given
  * angle onto a square of side 2 * halfSide
  */
-void CircleSquareProjection( Vector3& position, Degree angle, float halfSide )
+void CircleSquareProjection( Vector3& position, Radian angle, float halfSide )
 {
-  Radian angleInRadians(angle);
-
   //  135  90   45
   //     +--+--+
   //     | \|/ |
@@ -37,25 +35,25 @@ void CircleSquareProjection( Vector3& position, Degree angle, float halfSide )
   //     | /|\ |
   //     +--+--+
   //  225  270  315
-  if( angle >= 45.0f && angle < 135.0f )
+  if( angle >= Dali::ANGLE_45 && angle < Dali::ANGLE_135 )
   {
-    position.x = halfSide * cosf(angleInRadians) / sinf(angleInRadians);
+    position.x = halfSide * cosf(angle) / sinf(angle);
     position.y = -halfSide;
   }
-  else if( angle >= 135.0f && angle < 225.0f )
+  else if( angle >= Dali::ANGLE_135 && angle < Dali::ANGLE_225 )
   {
     position.x = -halfSide;
-    position.y = halfSide * sinf(angleInRadians) / cosf(angleInRadians);
+    position.y = halfSide * sinf(angle) / cosf(angle);
   }
-  else if( angle >= 225.0f && angle < 315.0f )
+  else if( angle >= Dali::ANGLE_225 && angle < Dali::ANGLE_315 )
   {
-    position.x = -halfSide * cosf(angleInRadians) / sinf(angleInRadians);
+    position.x = -halfSide * cosf(angle) / sinf(angle);
     position.y =  halfSide;
   }
   else
   {
     position.x = halfSide;
-    position.y = -halfSide * sinf(angleInRadians) / cosf(angleInRadians);
+    position.y = -halfSide * sinf(angle) / cosf(angle);
   }
 
   position.z = 0.0f;
@@ -273,7 +271,7 @@ void RadialSweepViewImpl::Activate( Animation anim, float offsetTime, float dura
   }
 
   mStencilActor.SetOrientation( Degree(mInitialAngle), Vector3::ZAXIS );
-  mStencilActor.SetProperty( mRotationAngleIndex, static_cast<float>(mInitialSector) );
+  mStencilActor.SetProperty( mRotationAngleIndex, mInitialSector.degree );
 
   if( mRotateActors )
   {
@@ -282,13 +280,13 @@ void RadialSweepViewImpl::Activate( Animation anim, float offsetTime, float dura
       Actor actor = mLayer.GetChildAt(i);
       if( actor != mStencilActor )
       {
-        anim.AnimateTo( Property( actor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( mInitialActorAngle ) ), Vector3::ZAXIS ) );
+        anim.AnimateTo( Property( actor, Actor::Property::ORIENTATION ), Quaternion( Radian( mInitialActorAngle ), Vector3::ZAXIS ) );
       }
     }
   }
 
-  anim.AnimateTo( Property( mStencilActor, mRotationAngleIndex ), static_cast<float>(mFinalSector), mEasingFunction, TimePeriod( offsetTime, duration ) );
-  anim.AnimateTo( Property( mStencilActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( mFinalAngle ) ), Vector3::ZAXIS ), mEasingFunction, TimePeriod( offsetTime, duration ) );
+  anim.AnimateTo( Property( mStencilActor, mRotationAngleIndex ), mFinalSector.degree, mEasingFunction, TimePeriod( offsetTime, duration ) );
+  anim.AnimateTo( Property( mStencilActor, Actor::Property::ORIENTATION ), Quaternion( Radian( mFinalAngle ), Vector3::ZAXIS ), mEasingFunction, TimePeriod( offsetTime, duration ) );
 
   if( mRotateActorsWithStencil )
   {
@@ -297,7 +295,7 @@ void RadialSweepViewImpl::Activate( Animation anim, float offsetTime, float dura
       Actor actor = mLayer.GetChildAt(i);
       if( actor != mStencilActor )
       {
-        anim.AnimateTo( Property( actor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( mFinalAngle - mInitialAngle ) ), Vector3::ZAXIS ), mEasingFunction, TimePeriod( offsetTime, duration ) );
+        anim.AnimateTo( Property( actor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( mFinalAngle.degree - mInitialAngle.degree ) ), Vector3::ZAXIS ), mEasingFunction, TimePeriod( offsetTime, duration ) );
       }
     }
   }
@@ -308,7 +306,7 @@ void RadialSweepViewImpl::Activate( Animation anim, float offsetTime, float dura
       Actor actor = mLayer.GetChildAt(i);
       if( actor != mStencilActor )
       {
-        anim.AnimateTo( Property( actor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( mFinalActorAngle ) ), Vector3::ZAXIS ), mEasingFunction, TimePeriod( offsetTime, duration ) );
+        anim.AnimateTo( Property( actor, Actor::Property::ORIENTATION ), Quaternion( Radian( mFinalActorAngle ), Vector3::ZAXIS ), mEasingFunction, TimePeriod( offsetTime, duration ) );
       }
     }
   }
@@ -357,10 +355,10 @@ void RadialSweepViewImpl::CreateStencil( Degree initialSector )
   mStencilActor.SetCullFace(CullNone); // Allow clockwise & anticlockwise faces
 
   mStartAngleIndex = mStencilActor.RegisterProperty("start-angle", Property::Value(0.0f));
-  mRotationAngleIndex = mStencilActor.RegisterProperty("rotation-angle", Property::Value(initialSector));
+  mRotationAngleIndex = mStencilActor.RegisterProperty("rotation-angle", Property::Value(initialSector.degree));
 
   Source srcStart( mStencilActor, mStartAngleIndex );
-  Source srcRot( mStencilActor, mRotationAngleIndex );
+  Source srcRotation( mStencilActor, mRotationAngleIndex );
 
   // Constrain the vertices of the square mesh to sweep out a sector as the
   // rotation angle is animated.
@@ -371,27 +369,27 @@ void RadialSweepViewImpl::CreateStencil( Degree initialSector )
 
   constraint = Constraint::New<Vector3>( mMesh, mMesh.GetPropertyIndex(2, AnimatableVertex::Property::POSITION), SquareFanConstraint(0) );
   constraint.AddSource( srcStart );
-  constraint.AddSource( srcRot );
+  constraint.AddSource( srcRotation );
   constraint.Apply();
 
   constraint = Constraint::New<Vector3>( mMesh, mMesh.GetPropertyIndex(3, AnimatableVertex::Property::POSITION), SquareFanConstraint(1) );
   constraint.AddSource( srcStart );
-  constraint.AddSource( srcRot );
+  constraint.AddSource( srcRotation );
   constraint.Apply();
 
   constraint = Constraint::New<Vector3>( mMesh, mMesh.GetPropertyIndex(4, AnimatableVertex::Property::POSITION), SquareFanConstraint(2) );
   constraint.AddSource( srcStart );
-  constraint.AddSource( srcRot );
+  constraint.AddSource( srcRotation );
   constraint.Apply();
 
   constraint = Constraint::New<Vector3>( mMesh, mMesh.GetPropertyIndex(5, AnimatableVertex::Property::POSITION), SquareFanConstraint(3) );
   constraint.AddSource( srcStart );
-  constraint.AddSource( srcRot );
+  constraint.AddSource( srcRotation );
   constraint.Apply();
 
   constraint = Constraint::New<Vector3>( mMesh, mMesh.GetPropertyIndex(6, AnimatableVertex::Property::POSITION), SquareFanConstraint(4) );
   constraint.AddSource( srcStart );
-  constraint.AddSource( srcRot );
+  constraint.AddSource( srcRotation );
   constraint.Apply();
 
   mStencilActor.SetDrawMode( DrawMode::STENCIL );
index 74b1a88..42f0c0d 100644 (file)
@@ -33,7 +33,7 @@ const char* BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-default.png" );
 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
 
 const char* APPLICATION_TITLE_PAN_LIGHT( "Lighting: Pan Light" );
-const char* APPLICATION_TITLE_PAN_OBJECT( "Lighting: Pan Object" );
+const char* APPLICATION_TITLE_PAN_OBJECT( "Lighting: Rotate Object" );
 const char* APPLICATION_TITLE_PAN_SCENE( "Lighting: Pan Scene" );
 const char* APPLICATION_TITLE_ROTATE_SCENE( "Lighting: Rotate Scene" );
 const char* CHANGE_EFFECT_IMAGE( DALI_IMAGE_DIR "icon-change.png" );
@@ -43,7 +43,6 @@ const char* SCENE_IMAGE_1( DALI_IMAGE_DIR "gallery-small-10.jpg");
 const char* SCENE_IMAGE_2( DALI_IMAGE_DIR "gallery-small-42.jpg");
 const char* SCENE_IMAGE_3( DALI_IMAGE_DIR "gallery-small-48.jpg");
 
-const Quaternion JAUNTY_ROTATION(Math::PI/5.0f, Math::PI/5.0f, 0.0f); // Euler angles
 const float MIN_PINCH_SCALE( 0.3f );
 const float MAX_PINCH_SCALE( 2.05f );
 
@@ -55,6 +54,11 @@ const Vector3 FRONT_POINT( 0.0f, 0.0f, 20.0f);
 
 const Vector2 DEFAULT_STAGE_SIZE( 480.0f, 800.0f );
 
+const float X_ROTATION_DISPLACEMENT_FACTOR = 60.f;
+const float Y_ROTATION_DISPLACEMENT_FACTOR = 60.f;
+const float LIGHT_PAN_X_DISPLACEMENT_FACTOR = 180.f;
+const float LIGHT_PAN_Y_DISPLACEMENT_FACTOR = 180.f;
+
 }
 
 /**
@@ -74,12 +78,12 @@ public:
   : mApp(app),
     mPaused(false),
     mTranslation(Vector3::ZERO),
-    mLongitudinal(15.0f),
-    mAxisTilt(30.0f),
-    mLightLongitudinal(0.0f),
-    mLightAxisTilt(0.0f),
-    mObjectLongitudinal(0.0f),
-    mObjectAxisTilt(0.0f),
+    mSceneYRotation( Dali::ANGLE_30 * 0.5f ),
+    mSceneXRotation( Dali::ANGLE_30 ),
+    mLightYRotation(0.0f),
+    mLightXRotation(0.0f),
+    mObjectYRotation(0.0f),
+    mObjectXRotation(0.0f),
     mPinchScale(0.5f),
     mScaleAtPinchStart(0.5f),
     mPanState(PAN_SCENE)
@@ -94,18 +98,6 @@ public:
   }
 
 public:
-  struct PositionInFrontOf
-  {
-    PositionInFrontOf()
-    {
-    }
-
-    void operator()( Vector3& current, const PropertyInputContainer& inputs )
-    {
-      current = inputs[0]->GetVector3();
-      current.z += 1.0f;
-    }
-  };
 
   struct RotationConstraint
   {
@@ -116,8 +108,8 @@ public:
 
     void operator()( Quaternion& current, const PropertyInputContainer& inputs )
     {
-      Degree angle( inputs[0]->GetFloat() );
-      current = Quaternion( Radian(angle) * mSign, Vector3::YAXIS );
+      Radian angle( inputs[0]->GetFloat() );
+      current = Quaternion( angle * mSign, Vector3::YAXIS );
     }
 
     float mSign;
@@ -176,7 +168,7 @@ public:
     mView.SetPosition(Vector3(0.0f, 0.0f, -50));
 
     mContents.SetPosition(mTranslation);
-    mContents.SetOrientation(CalculateWorldRotation(Radian(mLongitudinal), Radian(mAxisTilt)));
+    mContents.SetOrientation( CalculateWorldRotation( mSceneXRotation, mSceneYRotation ) );
     mContents.SetScale(mPinchScale, mPinchScale, mPinchScale);
 
     mPanGestureDetector = PanGestureDetector::New();
@@ -220,7 +212,7 @@ public:
     mLightAnchor = Actor::New();
     mLightAnchor.SetParentOrigin(ParentOrigin::CENTER);
     mLightAnchor.SetAnchorPoint(AnchorPoint::CENTER);
-    mLightAnchor.SetOrientation(CalculateWorldRotation(Radian(mLightLongitudinal), Radian(mLightAxisTilt)));
+    mLightAnchor.SetOrientation( CalculateWorldRotation( mLightXRotation, mLightYRotation ) );
 
     // Work out a scaling factor as the initial light position was calculated for desktop
     // Need to scale light position as scene actor size is based on stage size (i.e. much bigger on device)
@@ -271,7 +263,7 @@ public:
     mImageActor2.Add(mImageActor1);
     mImageActor2.Add(mImageActor3);
 
-    Property::Index angleIndex = mImageActor2.RegisterProperty("angle", Property::Value(30.0f));
+    Property::Index angleIndex = mImageActor2.RegisterProperty("angle", Property::Value( Dali::ANGLE_30 ) );
     Source angleSrc( mImageActor2, angleIndex );
 
     Constraint constraint = Constraint::New<Quaternion>( mImageActor1, Actor::Property::ORIENTATION, RotationConstraint(-1.0f) );
@@ -286,7 +278,7 @@ public:
 
     // Want to animate angle from 30 => -30 and back again smoothly.
 
-    mSceneAnimation.AnimateTo( Property( mImageActor2, angleIndex ), Property::Value(-30.0f), AlphaFunctions::Sin );
+    mSceneAnimation.AnimateTo( Property( mImageActor2, angleIndex ), Property::Value(-Dali::ANGLE_30), AlphaFunctions::Sin );
 
     mSceneAnimation.SetLooping(true);
     mSceneAnimation.Play();
@@ -297,10 +289,10 @@ public:
   }
 
 
-  Quaternion CalculateWorldRotation(Radian longitude, Radian axisTilt )
+  Quaternion CalculateWorldRotation( Radian XRotation, Radian YRotation )
   {
-    Quaternion q(longitude, Vector3::YAXIS);
-    Quaternion p(axisTilt, Vector3::XAXIS);
+    Quaternion p( XRotation, Vector3::XAXIS );
+    Quaternion q( YRotation, Vector3::YAXIS );
     return p*q;
   }
 
@@ -331,10 +323,11 @@ public:
         {
           case PAN_LIGHT:
           {
-            mLightLongitudinal += gesture.displacement.x/4.0f;
-            mLightAxisTilt -= gesture.displacement.y/6.0f;
-            mLightAxisTilt = Clamp<float>(mLightAxisTilt, -90.0f, 90.0f);
-            mLightAnchor.SetOrientation(CalculateWorldRotation(Radian(mLightLongitudinal), Radian(mLightAxisTilt)));
+            mLightXRotation = mLightXRotation - gesture.displacement.y / LIGHT_PAN_X_DISPLACEMENT_FACTOR; // X displacement rotates around Y axis
+            mLightXRotation = Clamp(mLightXRotation, -Dali::ANGLE_45, Dali::ANGLE_45 );
+            mLightYRotation = mLightYRotation + gesture.displacement.x / LIGHT_PAN_Y_DISPLACEMENT_FACTOR; // Y displacement rotates around X axis
+            mLightYRotation = Clamp(mLightYRotation, -Dali::ANGLE_45, Dali::ANGLE_45 );
+            mLightAnchor.SetOrientation( CalculateWorldRotation( mLightXRotation, mLightYRotation ) );
             break;
           }
 
@@ -347,19 +340,19 @@ public:
 
           case ROTATE_SCENE:
           {
-            mLongitudinal += gesture.displacement.x/4.0f;
-            mAxisTilt -= gesture.displacement.y/6.0f;
-            mAxisTilt = Clamp<float>(mAxisTilt, -90.0f, 90.0f);
-            mContents.SetOrientation(CalculateWorldRotation(Radian(mLongitudinal), Radian(mAxisTilt)));
+            mSceneXRotation = mSceneXRotation - gesture.displacement.y / X_ROTATION_DISPLACEMENT_FACTOR; // X displacement rotates around Y axis
+            mSceneXRotation = Clamp( mSceneXRotation, -Dali::ANGLE_90, Dali::ANGLE_90 );
+            mSceneYRotation = mSceneYRotation + gesture.displacement.x / Y_ROTATION_DISPLACEMENT_FACTOR; // Y displacement rotates around X axis
+            mSceneYRotation = Clamp( mSceneYRotation, -Dali::ANGLE_90, Dali::ANGLE_90 );
+            mContents.SetOrientation( CalculateWorldRotation( mSceneXRotation, mSceneYRotation ) );
             break;
           }
 
-          case PAN_OBJECT:
+          case ROTATE_OBJECT:
           {
-            mObjectLongitudinal += gesture.displacement.x/4.0f;
-            mObjectAxisTilt -= gesture.displacement.y/6.0f;
-            mObjectAxisTilt = Clamp<float>(mObjectAxisTilt, -90.0f, 90.0f);
-            mSceneActor.SetOrientation(CalculateWorldRotation(Radian(mObjectLongitudinal), Radian(mObjectAxisTilt)));
+            mObjectXRotation = mObjectXRotation - gesture.displacement.y / X_ROTATION_DISPLACEMENT_FACTOR; // X displacement rotates around Y axis
+            mObjectYRotation = mObjectYRotation + gesture.displacement.x / Y_ROTATION_DISPLACEMENT_FACTOR; // Y displacement rotates around X axis
+            mSceneActor.SetOrientation( CalculateWorldRotation( mObjectXRotation, mObjectYRotation ) );
             break;
           }
         }
@@ -422,10 +415,10 @@ public:
         mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_PAN_LIGHT) );
         break;
       case PAN_LIGHT:
-        mPanState = PAN_OBJECT;
+        mPanState = ROTATE_OBJECT;
         mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_PAN_OBJECT) );
         break;
-      case PAN_OBJECT:
+      case ROTATE_OBJECT:
         mPanState = PAN_SCENE;
         mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_PAN_SCENE) );
         break;
@@ -443,9 +436,9 @@ public:
     mContents.SetPosition(mTranslation);
 
     // Align scene so that light anchor orientation is Z Axis
-    mAxisTilt = -mLightAxisTilt;
-    mLongitudinal = -mLightLongitudinal;
-    mContents.SetOrientation(CalculateWorldRotation(Radian(mLongitudinal), Radian(mAxisTilt)));
+    mSceneXRotation = -mLightXRotation;
+    mSceneYRotation = -mLightYRotation;
+    mContents.SetOrientation( CalculateWorldRotation( mSceneXRotation, mSceneYRotation ) );
 
     return true;
   }
@@ -470,12 +463,12 @@ private:
   PinchGestureDetector      mPinchGestureDetector;
   TapGestureDetector        mTapGestureDetector;
   Vector3                   mTranslation;
-  Degree                    mLongitudinal;
-  Degree                    mAxisTilt;
-  Degree                    mLightLongitudinal;
-  Degree                    mLightAxisTilt;
-  Degree                    mObjectLongitudinal;
-  Degree                    mObjectAxisTilt;
+  Radian                    mSceneYRotation;
+  Radian                    mSceneXRotation;
+  Radian                    mLightYRotation;
+  Radian                    mLightXRotation;
+  Radian                    mObjectYRotation;
+  Radian                    mObjectXRotation;
   float                     mPinchScale;
   float                     mScaleAtPinchStart;
 
@@ -489,7 +482,7 @@ private:
     PAN_SCENE,
     ROTATE_SCENE,
     PAN_LIGHT,
-    PAN_OBJECT
+    ROTATE_OBJECT
   };
 
   PanState                  mPanState;