| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2011 Luke Parry * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <Inventor/actions/SoGetMatrixAction.h> |
| 25 |
#include <Inventor/actions/SoGLRenderAction.h> |
| 26 |
#include <Inventor/actions/SoCallbackAction.h> |
| 27 |
#include <Inventor/actions/SoPickAction.h> |
| 28 |
#include <Inventor/actions/SoGetBoundingBoxAction.h> |
| 29 |
#include <Inventor/actions/SoGetPrimitiveCountAction.h> |
| 30 |
#include <Inventor/elements/SoModelMatrixElement.h> |
| 31 |
#include <Inventor/elements/SoViewportRegionElement.h> |
| 32 |
#include <Inventor/elements/SoViewVolumeElement.h> |
| 33 |
#include <Inventor/nodes/SoCamera.h> |
| 34 |
|
| 35 |
|
| 36 |
#include "SoAutoZoomTranslation.h" |
| 37 |
|
| 38 |
|
| 39 |
using namespace Gui; |
| 40 |
|
| 41 |
// ------------------------------------------------------ |
| 42 |
|
| 43 |
SO_NODE_SOURCE(SoAutoZoomTranslation) |
| 44 |
|
| 45 |
void SoAutoZoomTranslation::initClass() |
| 46 |
{ |
| 47 |
SO_NODE_INIT_CLASS(SoAutoZoomTranslation, SoTransformation, "AutoZoom"); |
| 48 |
|
| 49 |
// Enable elements for SoGetMatrixAction (#0002268) |
| 50 |
// SoCamera::initClass() enables the SoViewVolumeElement for |
| 51 |
// * SoGLRenderAction |
| 52 |
// * SoGetBoundingBoxAction |
| 53 |
// * SoRayPickAction |
| 54 |
// * SoCallbackAction |
| 55 |
// * SoGetPrimitiveCountAction |
| 56 |
// The element SoViewportRegionElement is enabled by the |
| 57 |
// above listed actions. |
| 58 |
// Additionally, SoViewVolumeElement is enabled for |
| 59 |
// * SoAudioRenderAction |
| 60 |
// * SoHandleEventAction |
| 61 |
// And SoViewportRegionElement is enabled for |
| 62 |
// * SoHandleEventAction |
| 63 |
// * SoGetMatrixAction |
| 64 |
SO_ENABLE(SoGetMatrixAction, SoViewVolumeElement); |
| 65 |
} |
| 66 |
|
| 67 |
float SoAutoZoomTranslation::getScaleFactor(SoAction* action) const |
| 68 |
{ |
| 69 |
float scale = scaleFactor.getValue(); |
| 70 |
if (!scale) { |
| 71 |
return 1.0; |
| 72 |
} |
| 73 |
// Dividing by 5 seems to work well |
| 74 |
SbViewVolume vv = SoViewVolumeElement::get(action->getState()); |
| 75 |
float aspectRatio = SoViewportRegionElement::get(action->getState()).getViewportAspectRatio(); |
| 76 |
scale *= vv.getWorldToScreenScale(SbVec3f(0.f, 0.f, 0.f), 0.1f) / (5 * aspectRatio); |
| 77 |
return scale; |
| 78 |
} |
| 79 |
|
| 80 |
SoAutoZoomTranslation::SoAutoZoomTranslation() |
| 81 |
{ |
| 82 |
SO_NODE_CONSTRUCTOR(SoAutoZoomTranslation); |
| 83 |
SO_NODE_ADD_FIELD(scaleFactor, (1.0f)); |
| 84 |
// SO_NODE_ADD_FIELD(abPos, (SbVec3f(0.f,0.f,0.f))); |
| 85 |
} |
| 86 |
|
| 87 |
void SoAutoZoomTranslation::GLRender(SoGLRenderAction* action) |
| 88 |
{ |
| 89 |
SoAutoZoomTranslation::doAction(static_cast<SoAction*>(action)); |
| 90 |
inherited::GLRender(action); |
| 91 |
} |
| 92 |
|
| 93 |
// Doc in superclass. |
| 94 |
void SoAutoZoomTranslation::doAction(SoAction* action) |
| 95 |
{ |
| 96 |
float sf = this->getScaleFactor(action); |
| 97 |
auto state = action->getState(); |
| 98 |
SbRotation r, so; |
| 99 |
SbVec3f s, t; |
| 100 |
SbMatrix matrix = SoModelMatrixElement::get(action->getState()); // clazy:exclude=rule-of-two-soft |
| 101 |
matrix.getTransform(t, r, s, so); |
| 102 |
matrix.multVecMatrix(SbVec3f(0, 0, 0), t); |
| 103 |
// reset current model scale factor |
| 104 |
matrix.setTransform(t, r, SbVec3f(sf, sf, sf)); |
| 105 |
SoModelMatrixElement::set(state, this, matrix); |
| 106 |
} |
| 107 |
|
| 108 |
// set the auto scale factor. |
| 109 |
// void SoAutoZoomTranslation::setAutoScale(void) |
| 110 |
//{ |
| 111 |
// float sf = this->getScaleFactor(); |
| 112 |
// //this->enableNotify ( false ); |
| 113 |
// scaleFactor.setValue(SbVec3f(sf,sf,sf)); |
| 114 |
// //this->enableNotify ( true ); |
| 115 |
// //scaleFactor.setDirty (true); |
| 116 |
// |
| 117 |
//} |
| 118 |
|
| 119 |
void SoAutoZoomTranslation::getMatrix(SoGetMatrixAction* action) |
| 120 |
{ |
| 121 |
float sf = this->getScaleFactor(action); |
| 122 |
|
| 123 |
SbMatrix& m = action->getMatrix(); |
| 124 |
|
| 125 |
SbRotation r, so; |
| 126 |
SbVec3f s, t; |
| 127 |
m.getTransform(t, r, s, so); |
| 128 |
m.multVecMatrix(SbVec3f(0, 0, 0), t); |
| 129 |
m.setTransform(t, r, SbVec3f(sf, sf, sf)); |
| 130 |
|
| 131 |
action->getInverse() = m.inverse(); |
| 132 |
} |
| 133 |
|
| 134 |
void SoAutoZoomTranslation::callback(SoCallbackAction* action) |
| 135 |
{ |
| 136 |
SoAutoZoomTranslation::doAction(static_cast<SoAction*>(action)); |
| 137 |
} |
| 138 |
|
| 139 |
void SoAutoZoomTranslation::getBoundingBox(SoGetBoundingBoxAction* action) |
| 140 |
{ |
| 141 |
SoAutoZoomTranslation::doAction(static_cast<SoAction*>(action)); |
| 142 |
} |
| 143 |
|
| 144 |
void SoAutoZoomTranslation::pick(SoPickAction* action) |
| 145 |
{ |
| 146 |
SoAutoZoomTranslation::doAction(static_cast<SoAction*>(action)); |
| 147 |
} |
| 148 |
|
| 149 |
// Doc in superclass. |
| 150 |
void SoAutoZoomTranslation::getPrimitiveCount(SoGetPrimitiveCountAction* action) |
| 151 |
{ |
| 152 |
SoAutoZoomTranslation::doAction(static_cast<SoAction*>(action)); |
| 153 |
} |