OSGTransform.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038
00039 #include <cstdlib>
00040 #include <cstdio>
00041
00042 #include "OSGConfig.h"
00043
00044 #include "OSGGL.h"
00045
00046 #ifndef OSG_EMBEDDED
00047 #include "OSGIntersectAction.h"
00048 #endif
00049 
00050 #include "OSGRenderAction.h"
00051
00052 #ifdef OSG_HAVE_ACTION //CHECK
00053 #include "OSGIntersectActor.h"
00054 #endif
00055 
00056 #include "OSGTransform.h"
00057 #include "OSGVolume.h"
00058
00059 OSG_USING_NAMESPACE
00060
00061 // Documentation for this class is emited in the
00062 // OSGTransformBase.cpp file.
00063 // To modify it, please change the .fcd file (OSGTransform.fcd) and
00064 // regenerate the base file.
00065
00066 /*-------------------------------------------------------------------------*/
00067 /*                               Sync                                      */
00068
00069 void Transform::changed(ConstFieldMaskArg whichField,
00070                         UInt32            origin,
00071                         BitVector         details)
00072 {
00073     if(whichField & MatrixFieldMask)
00074     {
00075         invalidateVolume();
00076     }
00077
00078     Inherited::changed(whichField, origin, details);
00079 }
00080
00081 /*-------------------------------------------------------------------------*/
00082 /*                               Helper                                    */
00083
00084 void Transform::accumulateMatrix(Matrixr &result)
00085 {
00086     result.mult(getMatrix());
00087 }
00088
00089 void Transform::adjustVolume(Volume &volume)
00090 {
00091     volume.transform(_sfMatrix.getValue());
00092 }
00093
00094 /*-------------------------------------------------------------------------*/
00095 /*                                Dump                                     */
00096
00097 void Transform::dump(      UInt32    uiIndent,
00098                      const BitVector bvFlags ) const
00099 {
00100    Inherited::dump(uiIndent, bvFlags);
00101 }
00102
00103 /*-------------------------------------------------------------------------*/
00104 /*                            Constructors                                 */
00105
00106 Transform::Transform(void) :
00107     Inherited()
00108 {
00109     _sfMatrix.getValue().setIdentity();
00110 }
00111
00112 Transform::Transform(const Transform &source) :
00113     Inherited(source)
00114 {
00115 }
00116
00117 /*-------------------------------------------------------------------------*/
00118 /*                             Destructor                                  */
00119
00120 Transform::~Transform(void)
00121 {
00122 }
00123
00124
00125 /*-------------------------------------------------------------------------*/
00126 /*                                Render                                   */
00127
00128 ActionBase::ResultE Transform::renderEnter(Action *action)
00129 {
00130     RenderAction *pAction =
00131         dynamic_cast<RenderAction *>(action);
00132
00133     pAction->pushVisibility();
00134
00135     pAction->pushMatrix(this->getMatrix());
00136
00137     return ActionBase::Continue;
00138 }
00139
00140 ActionBase::ResultE Transform::renderLeave(Action *action)
00141 {
00142     RenderAction *pAction =
00143         dynamic_cast<RenderAction *>(action);
00144
00145     pAction->popVisibility();
00146
00147     pAction->popMatrix();
00148
00149     return ActionBase::Continue;
00150 }
00151
00152 /*-------------------------------------------------------------------------*/
00153 /*                            Intersect                                    */
00154
00155 #ifndef OSG_EMBEDDED
00156 ActionBase::ResultE Transform::intersectEnter(Action *action)
00157 {
00158     // Use parent class for trivial reject
00159     if(Inherited::intersect(action) == Action::Skip)
00160         return Action::Skip;
00161
00162     // Need to check children
00163     IntersectAction *ia = dynamic_cast<IntersectAction *>(action);
00164     Matrix           m  = this->getMatrix();
00165
00166     m.invert();
00167
00168     Pnt3f pos;
00169     Vec3f dir;
00170
00171     m.multFull(ia->getLine().getPosition (), pos);
00172     m.mult    (ia->getLine().getDirection(), dir);
00173
00174     Real32 length = dir.length();
00175
00176     if(length < Eps)
00177         SWARNING << "Transform::intersectEnter: Near-zero scale!" << std::endl;
00178
00179     ia->setLine(Line(pos, dir), ia->getMaxDist());
00180     ia->scale  (length                          );
00181
00182     return ActionBase::Continue;
00183 }
00184
00185 ActionBase::ResultE Transform::intersectLeave(Action *action)
00186 {
00187     IntersectAction *ia = dynamic_cast<IntersectAction *>(action);
00188     Matrix           m  = this->getMatrix();
00189
00190     Pnt3f pos;
00191     Vec3f dir;
00192
00193     m.multFull(ia->getLine().getPosition (), pos);
00194     m.mult    (ia->getLine().getDirection(), dir);
00195
00196     ia->setLine(Line(pos, dir), ia->getMaxDist());
00197     ia->scale(dir.length());
00198
00199     return ActionBase::Continue;
00200 }
00201 #endif
00202 
00203 #ifdef OSG_HAVE_ACTION //CHECK
00204 NewActionTypes::ResultE Transform::intersectActorEnter(
00205     ActorBase::FunctorArgumentType &funcArg)
00206 {
00207     IntersectActor *pIA        = dynamic_cast<IntersectActor *>(
00208                                                            funcArg.getActor());
00209     Matrix          matrix     = this->getMatrix();
00210     Line            transLine;
00211     Pnt3f           pos;
00212     Vec3f           dir;
00213
00214     matrix.invert();
00215
00216     matrix.multFull(pIA->getRay().getPosition (), pos);
00217     matrix.mult    (pIA->getRay().getDirection(), dir);
00218
00219     transLine.setValue(pos, dir);
00220
00221     pIA->beginEditState();
00222     {
00223         pIA->setRay        (transLine                           );
00224         pIA->setScaleFactor(pIA->getScaleFactor() / dir.length());
00225     }
00226     pIA->endEditState  ();
00227
00228     pIA->setupChildrenPriorities();
00229
00230     return NewActionTypes::Continue;
00231 }
00232
00233 NewActionTypes::ResultE Transform::intersectActorLeave(
00234     ActorBase::FunctorArgumentType &funcArg)
00235 {
00236     IntersectActor *pIA    = dynamic_cast<IntersectActor *>(
00237                                                            funcArg.getActor());
00238     const Matrix   &matrix = this->getMatrix();
00239           Pnt3f     pos;
00240           Vec3f     dir;
00241
00242     matrix.multFull(pIA->getRay().getPosition (), pos);
00243     matrix.mult    (pIA->getRay().getDirection(), dir);
00244
00245     pIA->beginEditState();
00246     {
00247         pIA->setRay        (Line(pos, dir)                      );
00248         pIA->setScaleFactor(pIA->getScaleFactor() / dir.length());
00249     }
00250     pIA->endEditState  ();
00251
00252     return NewActionTypes::Continue;
00253 }
00254 #endif
00255 
00256
00257 /*-------------------------------------------------------------------------*/
00258 /*                                Init                                     */
00259
00260 void Transform::initMethod(InitPhase ePhase)
00261 {
00262     Inherited::initMethod(ePhase);
00263
00264     if(ePhase == TypeObject::SystemPost)
00265     {
00266 #ifndef OSG_EMBEDDED
00267         IntersectAction::registerEnterDefault(
00268             getClassType(),
00269             reinterpret_cast<Action::Callback>(&Transform::intersectEnter));
00270
00271         IntersectAction::registerLeaveDefault(
00272             getClassType(),
00273             reinterpret_cast<Action::Callback>(&Transform::intersectLeave));
00274 #endif
00275 
00276         RenderAction::registerEnterDefault(
00277             Transform::getClassType(),
00278             reinterpret_cast<Action::Callback>(&Transform::renderEnter));
00279
00280         RenderAction::registerLeaveDefault(
00281             Transform::getClassType(),
00282             reinterpret_cast<Action::Callback>(&Transform::renderLeave));
00283
00284 #ifdef OSG_HAVE_ACTION //CHECK
00285     IntersectActor::regClassEnter(
00286         osgTypedMethodFunctor2BaseCPtr<
00287           NewActionTypes::ResultE,
00288           TransformPtr           ,
00289           NodeCorePtr            ,
00290           ActorBase::FunctorArgumentType &>(&Transform::intersectActorEnter),
00291         getClassType());
00292
00293     IntersectActor::regClassLeave(
00294         osgTypedMethodFunctor2BaseCPtr<
00295           NewActionTypes::ResultE,
00296           TransformPtr           ,
00297           NodeCorePtr            ,
00298           ActorBase::FunctorArgumentType &>(&Transform::intersectActorLeave),
00299         getClassType());
00300 #endif
00301     }
00302 }
00303