Forráskód Böngészése

medit with reantweakbar

Former-commit-id: 95007fff0773ebb085539cb1b7787901679d4694
Alec Jacobson (jalec 12 éve
szülő
commit
918eb788b0

+ 9 - 1
include/igl/EPS.cpp

@@ -4,12 +4,20 @@ template <> IGL_INLINE float igl::EPS()
 {
   return igl::FLOAT_EPS;
 }
-
 template <> IGL_INLINE double igl::EPS()
 {
   return igl::DOUBLE_EPS;
 }
 
+template <> IGL_INLINE float igl::EPS_SQ()
+{
+  return igl::FLOAT_EPS_SQ;
+}
+template <> IGL_INLINE double igl::EPS_SQ()
+{
+  return igl::DOUBLE_EPS_SQ;
+}
+
 #ifndef IGL_HEADER_ONLY
 // Explicit template specialization
 #endif

+ 3 - 0
include/igl/EPS.h

@@ -10,9 +10,12 @@ namespace igl
   const float FLOAT_EPS_SQ = 1.0e-14;
   // Function returning EPS for corresponding type
   template <typename S_type> IGL_INLINE S_type EPS();
+  template <typename S_type> IGL_INLINE S_type EPS_SQ();
   // Template specializations for float and double
   template <> IGL_INLINE float EPS<float>();
   template <> IGL_INLINE double EPS<double>();
+  template <> IGL_INLINE float EPS_SQ<float>();
+  template <> IGL_INLINE double EPS_SQ<double>();
 }
 
 #ifdef IGL_HEADER_ONLY

+ 30 - 1
include/igl/ReAntTweakBar.cpp

@@ -347,6 +347,13 @@ std::string igl::ReTwBar::get_value_as_string(
         sstr << *(static_cast<int*>(var));
         break;
       }
+    case TW_TYPE_UINT8:
+      {
+        sstr << "TW_TYPE_UINT8" << " ";
+        // Cast to int so that it's human readable
+        sstr << (int)*(static_cast<unsigned char*>(var));
+        break;
+      }
     case TW_TYPE_INT32:
       {
         sstr << "TW_TYPE_INT32" << " ";
@@ -442,7 +449,7 @@ bool igl::ReTwBar::load(const char *file_name)
     }
 
     sscanf(line,"%[^:]: %s %[^\n]",name,type_str,value_str);
-    //printf("%s: %s %s\n",name, type_str,value_str);
+    printf("%s: %s %s\n",name, type_str,value_str);
 
     TwType type;
     if(!type_from_string(type_str,type))
@@ -500,6 +507,7 @@ bool igl::ReTwBar::set_value_from_string(
   float f[4];
   double d[4];
   bool b;
+  unsigned char uc;
 
   // First try to get value from default types
   switch(type)
@@ -570,6 +578,20 @@ bool igl::ReTwBar::set_value_from_string(
         }
         break;
       }
+    case TW_TYPE_UINT8:
+      {
+        if(sscanf(value_str," %d",&i) == 1)
+        {
+          // Cast to unsigned char
+          uc = (unsigned char) i;
+          value = &uc;
+        }else
+        {
+          printf("ERROR: Bad value format...\n");
+          return false;
+        }
+        break;
+      }
     case TW_TYPE_BOOL32:
     case TW_TYPE_INT32:
       {
@@ -700,6 +722,13 @@ bool igl::ReTwBar::set_value_from_string(
             fvar[2] = fvalue[2];
             break;
           }
+        case TW_TYPE_UINT8:
+          {
+            unsigned char * ucvar = static_cast<unsigned char*>(var);
+            unsigned char * ucvalue = static_cast<unsigned char*>(value);
+            *ucvar = *ucvalue;
+            break;
+          }
         case TW_TYPE_BOOL32:
         case TW_TYPE_INT32:
           {

+ 4 - 0
include/igl/ReAntTweakBar.h

@@ -9,13 +9,17 @@
 // here:
 //   TW_TYPE_BOOLCPP
 //   TW_TYPE_QUAT4F
+//   TW_TYPE_QUAT4D
 //   TW_TYPE_COLOR4F
+//   TW_TYPE_COLOR4D
 //   TW_TYPE_COLOR3F
 //   TW_TYPE_DIR3F
+//   TW_TYPE_DIR3D
 //   TW_TYPE_BOOL32
 //   TW_TYPE_INT32
 //   TW_TYPE_FLOAT
 //   TW_TYPE_DOUBLE
+//   TW_TYPE_UINT8
 //   and
 //   custom TwTypes made with TwDefineEnum
 // 

+ 2 - 0
include/igl/axis_angle_to_quat.cpp

@@ -1,4 +1,6 @@
 #include "axis_angle_to_quat.h"
+#include "EPS.h"
+#include <cmath>
 
 // http://www.antisphere.com/Wiki/tools:anttweakbar
 template <typename Q_type>

+ 0 - 2
include/igl/axis_angle_to_quat.h

@@ -2,8 +2,6 @@
 #define IGL_AXIS_ANGLE_TO_QUAT_H
 #include "igl_inline.h"
 
-#include "EPS.h"
-#include <cmath>
 namespace igl
 {
   // Convert axis angle representation of a rotation to a quaternion

+ 1 - 0
include/igl/mat_to_quat.cpp

@@ -78,4 +78,5 @@ IGL_INLINE void igl::mat4_to_quat(const Q_type * mat, Q_type * q)
 #ifndef IGL_HEADER_ONLY
 // Explicit template specialization
 template void igl::mat4_to_quat<double>(double const*, double*);
+template void igl::mat4_to_quat<float>(float const*, float*);
 #endif

+ 69 - 0
include/igl/quat_to_axis_angle.cpp

@@ -0,0 +1,69 @@
+#include "quat_to_axis_angle.h"
+#include "EPS.h"
+#include "PI.h"
+#include <cmath>
+#include <cstdio>
+//
+// http://www.antisphere.com/Wiki/tools:anttweakbar
+template <typename Q_type>
+IGL_INLINE void igl::quat_to_axis_angle(
+  const Q_type *q,
+  Q_type *axis, 
+  Q_type & angle)
+{
+  using namespace igl;
+    if( fabs(q[3])>(1.0 + igl::EPS<Q_type>()) )
+    {
+        //axis[0] = axis[1] = axis[2] = 0; // no, keep the previous value
+        angle = 0;
+    }
+    else
+    {
+        double a;
+        if( q[3]>=1.0f )
+            a = 0; // and keep V
+        else if( q[3]<=-1.0f )
+            a = PI; // and keep V
+        else if( fabs(q[0]*q[0]+q[1]*q[1]+q[2]*q[2]+q[3]*q[3])<igl::EPS_SQ<Q_type>())
+        {
+            a = 0;
+        }else
+        {
+            a = acos(q[3]);
+            if( a*angle<0 ) // Preserve the sign of angle
+                a = -a;
+            double f = 1.0f / sin(a);
+            axis[0] = q[0] * f;
+            axis[1] = q[1] * f;
+            axis[2] = q[2] * f;
+        }
+        angle = 2.0*a;
+    }
+
+    //  if( angle>FLOAT_PI )
+    //      angle -= 2.0f*FLOAT_PI;
+    //  else if( angle<-FLOAT_PI )
+    //      angle += 2.0f*FLOAT_PI;
+    //angle = RadToDeg(angle);
+
+    if( fabs(angle)<igl::EPS<Q_type>()&& fabs(axis[0]*axis[0]+axis[1]*axis[1]+axis[2]*axis[2])<igl::EPS_SQ<Q_type>())
+    {
+        axis[0] = 1.0e-7;    // all components cannot be null
+    }
+}
+
+template <typename Q_type>
+IGL_INLINE void igl::quat_to_axis_angle_deg(
+  const Q_type *q,
+  Q_type *axis, 
+  Q_type & angle)
+{
+  igl::quat_to_axis_angle(q,axis,angle);
+  angle = angle*(180.0/PI);
+}
+
+#ifndef IGL_NO_HEADER
+// Explicit template instanciation
+template void igl::quat_to_axis_angle<float>(float const*, float*, float&);
+template void igl::quat_to_axis_angle_deg<float>(float const*, float*, float&);
+#endif

+ 33 - 0
include/igl/quat_to_axis_angle.h

@@ -0,0 +1,33 @@
+#ifndef IGL_QUAT_TO_AXIS_ANGLE_H
+#define IGL_QUAT_TO_AXIS_ANGLE_H
+#include "igl_inline.h"
+
+namespace igl
+{
+  // Convert quat representation of a rotation to axis angle
+  // A Quaternion, q, is defined here as an arrays of four scalars (x,y,z,w),
+  // such that q = x*i + y*j + z*k + w
+  // Inputs:
+  //   q quaternion
+  // Outputs:
+  //   axis  3d vector
+  //   angle  scalar in radians
+  template <typename Q_type>
+  IGL_INLINE void quat_to_axis_angle(
+    const Q_type *q,
+    Q_type *axis, 
+    Q_type & angle);
+  // Wrapper with angle in degrees
+  template <typename Q_type>
+  IGL_INLINE void quat_to_axis_angle_deg(
+    const Q_type *q,
+    Q_type *axis, 
+    Q_type & angle);
+}
+
+#ifdef IGL_HEADER_ONLY
+#  include "quat_to_axis_angle.cpp"
+#endif
+
+#endif
+