SoZoomTranslation.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2011                                                    *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 #include "PreCompiled.h"
00024 
00025 #ifndef _PreComp_
00026 # include <Inventor/actions/SoGLRenderAction.h>
00027 # include <Inventor/misc/SoState.h>
00028 # include <math.h>
00029 # include <cfloat>
00030 #endif
00031 
00032 #include <Inventor/actions/SoGetMatrixAction.h>
00033 #include <Inventor/actions/SoGLRenderAction.h>
00034 #include <Inventor/elements/SoModelMatrixElement.h>
00035 #include <Inventor/elements/SoProjectionMatrixElement.h>
00036 #include <Inventor/elements/SoViewingMatrixElement.h>
00037 #include <Inventor/elements/SoViewVolumeElement.h>
00038 #include <Inventor/elements/SoViewportRegionElement.h>
00039 #include <Inventor/nodes/SoCamera.h>
00040 
00041 #include <Gui/Application.h>
00042 #include <Gui/Document.h>
00043 #include <Gui/MainWindow.h>
00044 #include <Gui/View3DInventor.h>
00045 #include <Gui/View3DInventorViewer.h>
00046 
00047 
00048 #include "SoZoomTranslation.h"
00049 
00050 // *************************************************************************
00051 
00052 using namespace SketcherGui;
00053 
00054 // ------------------------------------------------------
00055 
00056 SO_NODE_SOURCE(SoZoomTranslation);
00057 
00058 void SoZoomTranslation::initClass()
00059 {
00060     SO_NODE_INIT_CLASS(SoZoomTranslation, SoTranslation, "Translation");
00061 }
00062 
00063 float SoZoomTranslation::getScaleFactor()
00064 {
00065     // Dividing by 5 seems to work well
00066 
00067     Gui::MDIView *mdi = Gui::Application::Instance->activeDocument()->getActiveView();
00068     if (mdi && mdi->isDerivedFrom(Gui::View3DInventor::getClassTypeId())) {
00069         Gui::View3DInventorViewer *viewer = static_cast<Gui::View3DInventor *>(mdi)->getViewer();
00070         this->scale = viewer->getCamera()->getViewVolume(viewer->getCamera()->aspectRatio.getValue()).getWorldToScreenScale(SbVec3f(0.f, 0.f, 0.f), 0.1f) / 5;
00071         return this->scale;
00072     } else {
00073         return this->scale;
00074     }
00075 }
00076 
00077 SoZoomTranslation::SoZoomTranslation()
00078 {
00079     SO_NODE_CONSTRUCTOR(SoZoomTranslation);
00080     SO_NODE_ADD_FIELD(abPos, (SbVec3f(0.f,0.f,0.f)));
00081     this->scale = -1;
00082 }
00083 
00084 void SoZoomTranslation::GLRender(SoGLRenderAction * action)
00085 {   
00086     SoZoomTranslation::doAction((SoAction *)action);
00087 }
00088 
00089 // Doc in superclass.
00090 void SoZoomTranslation::doAction(SoAction * action)
00091 {
00092     SbVec3f v;
00093     if(this->translation.getValue() == SbVec3f(0.0f, 0.0f, 0.0f) && this->abPos.getValue() == SbVec3f(0.0f, 0.0f, 0.0f)) {
00094         return;
00095     } else {
00096         SbVec3f absVtr = this->abPos.getValue();
00097         SbVec3f relVtr = this->translation.getValue();
00098 
00099         float sf = this->getScaleFactor();
00100         // For Sketcher Keep Z value the same
00101         relVtr[0] = (relVtr[0] != 0) ? sf * relVtr[0] : 0;
00102         relVtr[1] = (relVtr[1] != 0) ? sf * relVtr[1] : 0;
00103 
00104         v = absVtr + relVtr;
00105     }
00106     
00107     SoModelMatrixElement::translateBy(action->getState(), this, v);
00108 }
00109 
00110 void SoZoomTranslation::getMatrix(SoGetMatrixAction * action)
00111 {
00112     SbVec3f v;
00113     if(this->translation.getValue() == SbVec3f(0.0f, 0.0f, 0.0f) && this->abPos.getValue() == SbVec3f(0.0f, 0.0f, 0.0f)) {
00114         return;
00115     } else {
00116         SbVec3f absVtr = this->abPos.getValue();
00117         SbVec3f relVtr = this->translation.getValue();
00118 
00119         float sf = this->getScaleFactor();
00120         // For Sketcher Keep Z value the same
00121         relVtr[0] = (relVtr[0] != 0) ? sf  * relVtr[0] : 0;
00122         relVtr[1] = (relVtr[1] != 0) ? sf  * relVtr[1] : 0;
00123 
00124         v = absVtr + relVtr;
00125     }
00126     
00127     SbMatrix m;
00128     m.setTranslate(v);
00129     action->getMatrix().multLeft(m);
00130     m.setTranslate(-v);
00131     action->getInverse().multRight(m);
00132   
00133 }
00134 
00135 void SoZoomTranslation::callback(SoCallbackAction * action)
00136 {
00137   SoZoomTranslation::doAction((SoAction *)action);
00138 }
00139 
00140 void SoZoomTranslation::getBoundingBox(SoGetBoundingBoxAction * action)
00141 {
00142   SoZoomTranslation::doAction((SoAction *)action);
00143 }
00144 
00145 void SoZoomTranslation::pick(SoPickAction * action)
00146 {
00147   SoZoomTranslation::doAction((SoAction *)action);
00148 }
00149 
00150 // Doc in superclass.
00151 void SoZoomTranslation::getPrimitiveCount(SoGetPrimitiveCountAction * action)
00152 {
00153   SoZoomTranslation::doAction((SoAction *)action);
00154 }

Generated on Wed Nov 23 19:00:40 2011 for FreeCAD by  doxygen 1.6.1