glc_axis.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  http://glc-lib.sourceforge.net
00006 
00007  GLC-lib is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU Lesser General Public License as published by
00009  the Free Software Foundation; either version 2.1 of the License, or
00010  (at your option) any later version.
00011 
00012  GLC-lib is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU Lesser General Public License for more details.
00016 
00017  You should have received a copy of the GNU Lesser General Public License
00018  along with GLC-lib; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021  *****************************************************************************/
00023 
00024 #include "glc_axis.h"
00025 #include "../glc_factory.h"
00026 #include "../maths/glc_line3d.h"
00027 #include "../maths/glc_geomtools.h"
00028 #include "../geometry/glc_cylinder.h"
00029 #include "../geometry/glc_cone.h"
00030 #include "glc_pullmanipulator.h"
00031 
00032 GLC_Axis::GLC_Axis(const GLC_Point3d& center, GLC_3DWidgetManagerHandle*  pWidgetManagerHandle)
00033 : GLC_3DWidget(pWidgetManagerHandle)
00034 , m_Center(center)
00035 , m_ScaleFactor(1.0)
00036 , m_CurrentManipulator(NoneManipulator)
00037 , m_pCurrentManipulator(NULL)
00038 , m_AxisLength(1.0)
00039 , m_AxisRadiusRatio(0.03)
00040 {
00041 
00042 }
00043 
00044 GLC_Axis::GLC_Axis(const GLC_Axis& axis)
00045 : GLC_3DWidget(axis)
00046 , m_Center(axis.m_Center)
00047 , m_ScaleFactor(axis.m_ScaleFactor)
00048 , m_CurrentManipulator(axis.m_CurrentManipulator)
00049 , m_pCurrentManipulator(NULL)
00050 , m_AxisLength(axis.m_AxisLength)
00051 , m_AxisRadiusRatio(axis.m_AxisRadiusRatio)
00052 {
00053         if (NULL != axis.m_pCurrentManipulator)
00054         {
00055                 m_pCurrentManipulator= axis.m_pCurrentManipulator->clone();
00056         }
00057 }
00058 
00059 GLC_Axis::~GLC_Axis()
00060 {
00061         delete m_pCurrentManipulator;
00062 }
00063 
00064 GLC_Axis& GLC_Axis::operator=(const GLC_Axis& axis)
00065 {
00066         GLC_3DWidget::operator=(axis);
00067 
00068         m_Center= axis.m_Center;
00069         delete m_pCurrentManipulator;
00070         if (NULL != axis.m_pCurrentManipulator)
00071         {
00072                 m_pCurrentManipulator= axis.m_pCurrentManipulator->clone();
00073         }
00074 
00075         return *this;
00076 }
00077 
00078 void GLC_Axis::updateWidgetRep()
00079 {
00080         const double viewTangent= GLC_3DWidget::widgetManagerHandle()->viewportTangent();
00081         const GLC_Point3d eye(GLC_3DWidget::widgetManagerHandle()->cameraHandle()->eye());
00082         const double distanceToNormal= (m_Center - eye).length();
00083         const double viewWidth= distanceToNormal * viewTangent;
00084 
00085         m_ScaleFactor= viewWidth * 0.1;
00086         moveManipulatorRep(m_Center);
00087 }
00088 
00089 void GLC_Axis::setAxisLength(double length)
00090 {
00091         m_AxisLength= length;
00092         if (!GLC_3DWidget::isEmpty())
00093         {
00094                 GLC_3DWidget::remove3DViewInstance();
00095                 create3DviewInstance();
00096         }
00097 }
00098 
00099 void GLC_Axis::setCenter(const GLC_Point3d& newCenter)
00100 {
00101         m_Center= newCenter;
00102         moveManipulatorRep(m_Center);
00103 }
00104 
00105 glc::WidgetEventFlag GLC_Axis::select(const GLC_Point3d& pos, GLC_uint id)
00106 {
00107         //qDebug() << "GLC_Axis::select";
00108         Q_ASSERT(NULL == m_pCurrentManipulator);
00109         Q_ASSERT(NoneManipulator == m_CurrentManipulator);
00110 
00111         const int selectedInstanceIndex= GLC_3DWidget::indexOfIntsanceId(id);
00113         GLC_Viewport* pViewport= GLC_3DWidget::widgetManagerHandle()->viewport();
00114         if (selectedInstanceIndex < 2)
00115         {
00116                 m_pCurrentManipulator= new GLC_PullManipulator(pViewport, glc::X_AXIS);
00117                 m_CurrentManipulator= X_AxisManipulator;
00118                 GLC_3DWidget::instanceHandle(0)->geomAt(0)->firstMaterial()->setDiffuseColor(Qt::yellow);
00119         }
00120         else if (selectedInstanceIndex < 4)
00121         {
00122                 m_pCurrentManipulator= new GLC_PullManipulator(pViewport, glc::Y_AXIS);
00123                 m_CurrentManipulator= Y_AxisManipulator;
00124                 GLC_3DWidget::instanceHandle(2)->geomAt(0)->firstMaterial()->setDiffuseColor(Qt::yellow);
00125         }
00126         else
00127         {
00128                 Q_ASSERT((selectedInstanceIndex < 6) && (selectedInstanceIndex >= 4));
00129                 m_pCurrentManipulator= new GLC_PullManipulator(pViewport, glc::Z_AXIS);
00130                 m_CurrentManipulator= Z_AxisManipulator;
00131                 GLC_3DWidget::instanceHandle(4)->geomAt(0)->firstMaterial()->setDiffuseColor(Qt::yellow);
00132         }
00133 
00134         m_pCurrentManipulator->enterManipulateState(pos);
00135 
00136         updateWidgetRep();
00137 
00138         return glc::BlockedEvent;
00139 }
00140 
00141 glc::WidgetEventFlag GLC_Axis::mousePressed(const GLC_Point3d& pos, Qt::MouseButton button, GLC_uint id)
00142 {
00143         //qDebug() << "GLC_Axis::mousePressed";
00144         glc::WidgetEventFlag returnFlag= glc::IgnoreEvent;
00145         if (button == Qt::LeftButton)
00146         {
00147                 returnFlag= select(pos, id);
00148         }
00149 
00150         return returnFlag;
00151 }
00152 
00153 glc::WidgetEventFlag GLC_Axis::mouseReleased(Qt::MouseButton button)
00154 {
00155         //qDebug() << "GLC_Axis::mouseReleased";
00156         glc::WidgetEventFlag returnFlag= glc::IgnoreEvent;
00157         if (button == Qt::LeftButton)
00158         {
00159 
00160                 // get selected instance index
00161 
00162                 if (m_CurrentManipulator == X_AxisManipulator)
00163                 {
00164                         GLC_3DWidget::instanceHandle(0)->geomAt(0)->firstMaterial()->setDiffuseColor(Qt::red);
00165                 }
00166                 else if (m_CurrentManipulator == Y_AxisManipulator)
00167                 {
00168                         GLC_3DWidget::instanceHandle(2)->geomAt(0)->firstMaterial()->setDiffuseColor(Qt::green);
00169                 }
00170                 else if (m_CurrentManipulator == Z_AxisManipulator)
00171                 {
00172                         GLC_3DWidget::instanceHandle(4)->geomAt(0)->firstMaterial()->setDiffuseColor(Qt::blue);
00173                 }
00174 
00175                 m_CurrentManipulator= NoneManipulator;
00176                 delete m_pCurrentManipulator;
00177                 m_pCurrentManipulator= NULL;
00178 
00179                 returnFlag= glc::BlockedEvent;
00180         }
00181         return returnFlag;
00182 }
00183 
00184 glc::WidgetEventFlag GLC_Axis::unselect(const GLC_Point3d&, GLC_uint)
00185 {
00186         //qDebug() << "GLC_Axis::unselect";
00187         delete m_pCurrentManipulator;
00188         m_pCurrentManipulator= NULL;
00189 
00190         m_CurrentManipulator= NoneManipulator;
00191 
00192         return glc::AcceptEvent;
00193 }
00194 
00195 glc::WidgetEventFlag GLC_Axis::mouseMove(const GLC_Point3d& pos, Qt::MouseButtons button, GLC_uint)
00196 {
00197         glc::WidgetEventFlag returnFlag= glc::IgnoreEvent;
00198         if (button & Qt::LeftButton)
00199         {
00200                 if (NULL != m_pCurrentManipulator)
00201                 {
00202                         GLC_Matrix4x4 moveMatrix(m_pCurrentManipulator->manipulate(pos));
00203                         m_Center= moveMatrix * m_Center;
00204                         // Update the instance
00205                         for (int i= 0; i < 6; ++i)
00206                         {
00207                                 GLC_3DWidget::instanceHandle(i)->multMatrix(moveMatrix);
00208                         }
00209 
00210                         // Plane throw intersection and plane normal and camera up vector
00211                         m_pCurrentManipulator->enterManipulateState(m_pCurrentManipulator->previousPosition());
00212 
00213                         emit asChanged();
00214                         returnFlag= glc::AcceptEvent;
00215                 }
00216         }
00217 
00218         return returnFlag;
00219 }
00220 void GLC_Axis::create3DviewInstance()
00221 {
00222         Q_ASSERT(GLC_3DWidget::isEmpty());
00223         const double axisRadius= m_AxisLength * m_AxisRadiusRatio;
00224         const double arrowLength= m_AxisLength * 0.3;
00225         const double arrowFactor= 2.5;
00226 
00227         { // Create X axis
00228                 // The X axis material
00229                 GLC_Material* pMaterial= new GLC_Material(Qt::red);
00230 
00231                 GLC_3DViewInstance axisInstance= GLC_Factory::instance()->createCylinder(axisRadius, m_AxisLength);
00232                 axisInstance.representation().geomAt(0)->replaceMasterMaterial(pMaterial);
00233                 GLC_3DViewInstance arrowInstance= GLC_Factory::instance()->createCone(arrowFactor * axisRadius, arrowLength);
00234                 arrowInstance.representation().geomAt(0)->replaceMasterMaterial(pMaterial);
00235                 arrowInstance.translate(0.0, 0.0, m_AxisLength);
00236                 // Rotate the axis
00237                 GLC_Matrix4x4 rotation(glc::Y_AXIS, glc::PI / 2);
00238                 axisInstance.multMatrix(rotation);
00239                 arrowInstance.multMatrix(rotation);
00240 
00241                 GLC_3DWidget::add3DViewInstance(axisInstance);
00242                 GLC_3DWidget::add3DViewInstance(arrowInstance);
00243         }
00244         { // Create Y axis
00245                 // The Y axis material
00246                 GLC_Material* pMaterial= new GLC_Material(Qt::green);
00247 
00248                 GLC_3DViewInstance axisInstance= GLC_Factory::instance()->createCylinder(axisRadius, m_AxisLength);
00249                 axisInstance.representation().geomAt(0)->replaceMasterMaterial(pMaterial);
00250                 GLC_3DViewInstance arrowInstance= GLC_Factory::instance()->createCone(arrowFactor * axisRadius, arrowLength);
00251                 arrowInstance.representation().geomAt(0)->replaceMasterMaterial(pMaterial);
00252                 arrowInstance.translate(0.0, 0.0, m_AxisLength);
00253                 // Rotate the axis
00254                 GLC_Matrix4x4 rotation(glc::X_AXIS, - glc::PI / 2);
00255                 axisInstance.multMatrix(rotation);
00256                 arrowInstance.multMatrix(rotation);
00257 
00258                 GLC_3DWidget::add3DViewInstance(axisInstance);
00259                 GLC_3DWidget::add3DViewInstance(arrowInstance);
00260         }
00261         { // Create Z axis
00262                 // The Z axis material
00263                 GLC_Material* pMaterial= new GLC_Material(Qt::blue);
00264 
00265                 GLC_3DViewInstance axisInstance= GLC_Factory::instance()->createCylinder(axisRadius, m_AxisLength);
00266                 axisInstance.representation().geomAt(0)->replaceMasterMaterial(pMaterial);
00267                 GLC_3DViewInstance arrowInstance= GLC_Factory::instance()->createCone(arrowFactor * axisRadius, arrowLength);
00268                 arrowInstance.representation().geomAt(0)->replaceMasterMaterial(pMaterial);
00269                 arrowInstance.translate(0.0, 0.0, m_AxisLength);
00270 
00271                 GLC_3DWidget::add3DViewInstance(axisInstance);
00272                 GLC_3DWidget::add3DViewInstance(arrowInstance);
00273         }
00274 }
00275 
00276 void GLC_Axis::resetViewState()
00277 {
00278 
00279 }
00280 
00281 void GLC_Axis::moveManipulatorRep(const GLC_Point3d& pos)
00282 {
00283 
00284         const GLC_Matrix4x4 translationMatrix(pos);
00285         GLC_Matrix4x4 scaleMatrix;
00286         scaleMatrix.setMatScaling(m_ScaleFactor, m_ScaleFactor, m_ScaleFactor);
00287         for (int i= 0; i < 6; ++i)
00288         {
00289                 GLC_Matrix4x4 rotationMatrix;
00290                 GLC_Matrix4x4 intTranslation;
00291                 if (i < 2)
00292                 {
00293                         rotationMatrix.setMatRot(glc::Y_AXIS, glc::PI / 2);
00294                         if (i == 1)
00295                         {
00296                                 intTranslation.setMatTranslate(0.0, 0.0, m_AxisLength);
00297                         }
00298                 }
00299                 else if (i < 4)
00300                 {
00301                         rotationMatrix.setMatRot(glc::X_AXIS, - glc::PI / 2);
00302                         if (i == 3)
00303                         {
00304                                 intTranslation.setMatTranslate(0.0, 0.0, m_AxisLength);
00305                         }
00306 
00307                 }
00308                 else if (i == 5)
00309                 {
00310                         intTranslation.setMatTranslate(0.0, 0.0, m_AxisLength);
00311                 }
00312 
00313                 GLC_3DWidget::instanceHandle(i)->setMatrix(translationMatrix * scaleMatrix * rotationMatrix * intTranslation);
00314         }
00315 }

SourceForge.net Logo

©2005-2011 Laurent Ribon