constraint.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  Copyright (C) 2002-2013 Gilles Debunne. All rights reserved.
00004 
00005  This file is part of the QGLViewer library version 2.4.0.
00006 
00007  http://www.libqglviewer.com - contact@libqglviewer.com
00008 
00009  This file may be used under the terms of the GNU General Public License 
00010  versions 2.0 or 3.0 as published by the Free Software Foundation and
00011  appearing in the LICENSE file included in the packaging of this file.
00012  In addition, as a special exception, Gilles Debunne gives you certain 
00013  additional rights, described in the file GPL_EXCEPTION in this package.
00014 
00015  libQGLViewer uses dual licensing. Commercial/proprietary software must
00016  purchase a libQGLViewer Commercial License.
00017 
00018  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00019  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00020 
00021 *****************************************************************************/
00022 
00023 #include "constraint.h"
00024 #include "frame.h"
00025 #include "camera.h"
00026 
00027 using namespace qglviewer;
00028 using namespace std;
00029 
00031 //                                  Constraint                                //
00033 
00038 AxisPlaneConstraint::AxisPlaneConstraint()
00039   : translationConstraintType_(FREE), rotationConstraintType_(FREE)
00040 {
00041   // Do not use set since setRotationConstraintType needs a read.
00042 }
00043 
00045 void AxisPlaneConstraint::setTranslationConstraint(Type type, const Vec& direction)
00046 {
00047   setTranslationConstraintType(type);
00048   setTranslationConstraintDirection(direction);
00049 }
00050 
00052 void AxisPlaneConstraint::setTranslationConstraintDirection(const Vec& direction)
00053 {
00054   if ((translationConstraintType()!=AxisPlaneConstraint::FREE) && (translationConstraintType()!=AxisPlaneConstraint::FORBIDDEN))
00055     {
00056       const float norm = direction.norm();
00057       if (norm < 1E-8)
00058         {
00059           qWarning("AxisPlaneConstraint::setTranslationConstraintDir: null vector for translation constraint");
00060           translationConstraintType_ = AxisPlaneConstraint::FREE;
00061         }
00062       else
00063         translationConstraintDir_ = direction/norm;
00064     }
00065 }
00066 
00068 void AxisPlaneConstraint::setRotationConstraint(Type type, const Vec& direction)
00069 {
00070   setRotationConstraintType(type);
00071   setRotationConstraintDirection(direction);
00072 }
00073 
00075 void AxisPlaneConstraint::setRotationConstraintDirection(const Vec& direction)
00076 {
00077   if ((rotationConstraintType()!=AxisPlaneConstraint::FREE) && (rotationConstraintType()!=AxisPlaneConstraint::FORBIDDEN))
00078     {
00079       float norm = direction.norm();
00080       if (norm < 1E-8)
00081         {
00082           qWarning("AxisPlaneConstraint::setRotationConstraintDir: null vector for rotation constraint");
00083           rotationConstraintType_ = AxisPlaneConstraint::FREE;
00084         }
00085       else
00086         rotationConstraintDir_ = direction/norm;
00087     }
00088 }
00089 
00101 void AxisPlaneConstraint::setRotationConstraintType(Type type)
00102 {
00103   if (rotationConstraintType() == AxisPlaneConstraint::PLANE)
00104     {
00105       qWarning("AxisPlaneConstraint::setRotationConstraintType: the PLANE type cannot be used for a rotation constraints");
00106       return;
00107     }
00108 
00109   rotationConstraintType_ = type;
00110 }
00111 
00112 
00114 //                               LocalConstraint                              //
00116 
00120 void LocalConstraint::constrainTranslation(Vec& translation, Frame* const frame)
00121 {
00122   Vec proj;
00123   switch (translationConstraintType())
00124     {
00125     case AxisPlaneConstraint::FREE:
00126       break;
00127     case AxisPlaneConstraint::PLANE:
00128       proj = frame->rotation().rotate(translationConstraintDirection());
00129       translation.projectOnPlane(proj);
00130       break;
00131     case AxisPlaneConstraint::AXIS:
00132       proj = frame->rotation().rotate(translationConstraintDirection());
00133       translation.projectOnAxis(proj);
00134       break;
00135     case AxisPlaneConstraint::FORBIDDEN:
00136       translation = Vec(0.0, 0.0, 0.0);
00137       break;
00138     }
00139 }
00140 
00144 void LocalConstraint::constrainRotation(Quaternion& rotation, Frame* const)
00145 {
00146   switch (rotationConstraintType())
00147     {
00148     case AxisPlaneConstraint::FREE:
00149       break;
00150     case AxisPlaneConstraint::PLANE:
00151       break;
00152     case AxisPlaneConstraint::AXIS:
00153       {
00154         Vec axis = rotationConstraintDirection();
00155         Vec quat = Vec(rotation[0], rotation[1], rotation[2]);
00156         quat.projectOnAxis(axis);
00157         rotation = Quaternion(quat, 2.0*acos(rotation[3]));
00158       }
00159       break;
00160     case AxisPlaneConstraint::FORBIDDEN:
00161       rotation = Quaternion(); // identity
00162       break;
00163     }
00164 }
00165 
00167 //                               WorldConstraint                              //
00169 
00173 void WorldConstraint::constrainTranslation(Vec& translation, Frame* const frame)
00174 {
00175   Vec proj;
00176   switch (translationConstraintType())
00177     {
00178     case AxisPlaneConstraint::FREE:
00179       break;
00180     case AxisPlaneConstraint::PLANE:
00181       if (frame->referenceFrame())
00182         {
00183           proj = frame->referenceFrame()->transformOf(translationConstraintDirection());
00184           translation.projectOnPlane(proj);
00185         }
00186       else
00187         translation.projectOnPlane(translationConstraintDirection());
00188       break;
00189     case AxisPlaneConstraint::AXIS:
00190       if (frame->referenceFrame())
00191         {
00192           proj = frame->referenceFrame()->transformOf(translationConstraintDirection());
00193           translation.projectOnAxis(proj);
00194         }
00195       else
00196         translation.projectOnAxis(translationConstraintDirection());
00197       break;
00198     case AxisPlaneConstraint::FORBIDDEN:
00199       translation = Vec(0.0, 0.0, 0.0);
00200       break;
00201     }
00202 }
00203 
00207 void WorldConstraint::constrainRotation(Quaternion& rotation, Frame* const frame)
00208 {
00209   switch (rotationConstraintType())
00210     {
00211     case AxisPlaneConstraint::FREE:
00212       break;
00213     case AxisPlaneConstraint::PLANE:
00214       break;
00215     case AxisPlaneConstraint::AXIS:
00216       {
00217         Vec quat(rotation[0], rotation[1], rotation[2]);
00218         Vec axis = frame->transformOf(rotationConstraintDirection());
00219         quat.projectOnAxis(axis);
00220         rotation = Quaternion(quat, 2.0*acos(rotation[3]));
00221         break;
00222       }
00223     case AxisPlaneConstraint::FORBIDDEN:
00224       rotation = Quaternion(); // identity
00225       break;
00226     }
00227 }
00228 
00230 //                               CameraConstraint                              //
00232 
00235 CameraConstraint::CameraConstraint(const Camera* const camera)
00236   : AxisPlaneConstraint(), camera_(camera)
00237 {}
00238 
00242 void CameraConstraint::constrainTranslation(Vec& translation, Frame* const frame)
00243 {
00244   Vec proj;
00245   switch (translationConstraintType())
00246     {
00247     case AxisPlaneConstraint::FREE:
00248       break;
00249     case AxisPlaneConstraint::PLANE:
00250       proj = camera()->frame()->inverseTransformOf(translationConstraintDirection());
00251       if (frame->referenceFrame())
00252         proj = frame->referenceFrame()->transformOf(proj);
00253       translation.projectOnPlane(proj);
00254       break;
00255     case AxisPlaneConstraint::AXIS:
00256       proj = camera()->frame()->inverseTransformOf(translationConstraintDirection());
00257       if (frame->referenceFrame())
00258         proj = frame->referenceFrame()->transformOf(proj);
00259       translation.projectOnAxis(proj);
00260       break;
00261     case AxisPlaneConstraint::FORBIDDEN:
00262       translation = Vec(0.0, 0.0, 0.0);
00263       break;
00264     }
00265 }
00266 
00270 void CameraConstraint::constrainRotation(Quaternion& rotation, Frame* const frame)
00271 {
00272   switch (rotationConstraintType())
00273     {
00274     case AxisPlaneConstraint::FREE:
00275       break;
00276     case AxisPlaneConstraint::PLANE:
00277       break;
00278     case AxisPlaneConstraint::AXIS:
00279       {
00280         Vec axis = frame->transformOf(camera()->frame()->inverseTransformOf(rotationConstraintDirection()));
00281         Vec quat = Vec(rotation[0], rotation[1], rotation[2]);
00282         quat.projectOnAxis(axis);
00283         rotation = Quaternion(quat, 2.0*acos(rotation[3]));
00284       }
00285       break;
00286     case AxisPlaneConstraint::FORBIDDEN:
00287       rotation = Quaternion(); // identity
00288       break;
00289     }
00290 }


octovis
Author(s): Kai M. Wurm , Armin Hornung
autogenerated on Thu Aug 27 2015 14:13:26