root/OpenSceneGraph/trunk/src/osgManipulator/AntiSquish.cpp @ 8868

Revision 8868, 4.9 kB (checked in by robert, 6 years ago)

From Mathias Froehlich, "This is a generic optimization that does not depend on any cpu or instruction
set.

The optimization is based on the observation that matrix matrix multiplication
with a dense matrix 4x4 is 43 Operations whereas multiplication with a
transform, or scale matrix is only 4
2 operations. Which is a gain of a
*FACTOR*4* for these special cases.
The change implements these special cases, provides a unit test for these
implementation and converts uses of the expensiver dense matrix matrix
routine with the specialized versions.

Depending on the transform nodes in the scenegraph this change gives a
noticable improovement.
For example the osgforest code using the MatrixTransform? is about 20% slower
than the same codepath using the PositionAttitudeTransform? instead of the
MatrixTransform? with this patch applied.

If I remember right, the sse type optimizations did *not* provide a factor 4
improovement. Also these changes are totally independent of any cpu or
instruction set architecture. So I would prefer to have this current kind of
change instead of some hand coded and cpu dependent assembly stuff. If we
need that hand tuned stuff, these can go on top of this changes which must
provide than hand optimized additional variants for the specialized versions
to give a even better result in the end.

An other change included here is a change to rotation matrix from quaterion
code. There is a sqrt call which couold be optimized away. Since we divide in
effect by sqrt(length)*sqrt(length) which is just length ...
"

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13//osgManipulator - Copyright (C) 2007 Fugro-Jason B.V.
14
15#include <osgManipulator/AntiSquish>
16
17using namespace osgManipulator;
18
19namespace
20{
21    class AntiSquishCallback: public osg::NodeCallback
22    {
23        public:
24            AntiSquishCallback(AntiSquish* asq) : osg::NodeCallback(), _antiSquish(asq) {}
25            virtual ~AntiSquishCallback() {};
26           
27            virtual void operator() (osg::Node*, osg::NodeVisitor* nv)
28            {
29                // Get the node path.
30                osg::NodePath np = nv->getNodePath();
31
32                // Remove the last node which is the anti squish node itself.
33                np.pop_back();
34
35                // Get the accumulated modeling matrix.
36                osg::Matrix localToWorld = osg::computeLocalToWorld(np);
37
38                // compute the unsquished matrix.
39                bool flag = false;
40                osg::Matrix _unsquishedMatrix = _antiSquish->computeUnSquishedMatrix(localToWorld, flag);
41                if (flag)
42                    _antiSquish->setMatrix(_unsquishedMatrix);
43            }
44
45        protected:
46            AntiSquish* _antiSquish;
47    };
48
49}
50
51AntiSquish::AntiSquish() : _usePivot(true), _usePosition(false)
52{
53    _asqCallback = new AntiSquishCallback(this);
54    setUpdateCallback(_asqCallback);
55}
56
57AntiSquish::AntiSquish(const osg::Vec3& pivot) : _pivot(pivot), _usePivot(true), _usePosition(false)
58{
59    _asqCallback = new AntiSquishCallback(this);
60    setUpdateCallback(_asqCallback);
61}
62
63AntiSquish::AntiSquish(const osg::Vec3& pivot, const osg::Vec3& pos)
64    : _pivot(pivot), _usePivot(true), _position(pos), _usePosition(true)
65{
66    _asqCallback = new AntiSquishCallback(this);
67    setUpdateCallback(_asqCallback);
68}
69
70
71AntiSquish::AntiSquish(const AntiSquish& pat,const osg::CopyOp& copyop) :
72    MatrixTransform(pat,copyop),
73    _asqCallback(pat._asqCallback),
74    _pivot(pat._pivot),
75    _usePivot(pat._usePivot),
76    _position(pat._position),
77    _usePosition(pat._usePosition),
78    _cachedLocalToWorld(pat._cachedLocalToWorld)
79{
80}
81
82AntiSquish::~AntiSquish()
83{
84}
85
86osg::Matrix AntiSquish::computeUnSquishedMatrix(const osg::Matrix& LTW, bool& flag)
87{
88    osg::Vec3d t, s;
89    osg::Quat r, so;
90
91    if (LTW == _cachedLocalToWorld && _dirty == false)
92    {
93        flag = false;
94        return osg::Matrix::identity();
95    }
96
97    _cachedLocalToWorld = LTW;
98
99    LTW.decompose(t, r, s, so);
100
101    // Let's take an average of the scale.
102    double av = (s[0] + s[1] + s[2])/3.0;
103    s[0] = av; s[1] = av; s[2]=av;
104
105    if (av == 0)
106    {
107        flag = false;
108        return osg::Matrix::identity();
109    }
110
111    osg::Matrix unsquished;
112   
113    //
114    // Final Matrix: [-Pivot][SO]^[S][SO][R][T][Pivot][LOCALTOWORLD]^[position]
115    // OR [SO]^[S][SO][R][T][LOCALTOWORLD]^
116    //
117    if (_usePivot)
118    {
119        unsquished.postMultTranslate(-_pivot);
120
121        osg::Matrix tmps, invtmps;
122        so.get(tmps);
123        if (!invtmps.invert(tmps))
124        {
125            flag = false;
126            return osg::Matrix::identity();
127        }
128 
129        //SO^
130        unsquished.postMult(invtmps);
131        //S
132        unsquished.postMultScale(s);
133        //SO
134        unsquished.postMult(tmps);
135        //R
136        unsquished.postMultRotate(r);
137        //T
138        unsquished.postMultTranslate(t);
139
140        osg::Matrix invltw;
141        if (!invltw.invert(LTW))
142        {
143            flag = false;
144            return osg::Matrix::identity();
145        }
146        // LTW^
147        unsquished.postMult( invltw );
148
149        // Position
150        if (_usePosition)
151            unsquished.postMult(_position);
152        else
153            unsquished.postMult(_pivot);
154    }
155    else
156    {
157        osg::Matrix tmps, invtmps;
158        so.get(tmps);
159        if (!invtmps.invert(tmps))
160        {
161            flag = false;
162            return osg::Matrix::identity();
163        }
164        unsquished.postMult(invtmps);
165        unsquished.postMultScale(s);
166        unsquished.postMult(tmps);
167        unsquished.postMultRotate(r);
168        unsquished.postMultTranslate(t);
169        osg::Matrix invltw;
170        if (!invltw.invert(LTW))
171        {
172            flag = false;
173            return osg::Matrix::identity();
174        }
175        unsquished.postMult( invltw );
176    }
177
178    if (unsquished.isNaN())
179    {
180        flag = false;
181        return  osg::Matrix::identity();
182    }
183
184    flag = true;
185    _dirty = false;
186    return unsquished;
187}
188
189
Note: See TracBrowser for help on using the browser.