root/OpenSceneGraph/trunk/src/osg/PositionAttitudeTransform.cpp @ 8868

Revision 8868, 2.0 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#include <osg/PositionAttitudeTransform>
14
15using namespace osg;
16
17PositionAttitudeTransform::PositionAttitudeTransform():
18    _scale(1.0,1.0,1.0)
19{
20}
21
22bool PositionAttitudeTransform::computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const
23{
24    if (_referenceFrame==RELATIVE_RF)
25    {
26        matrix.preMultTranslate(_position);
27        matrix.preMultRotate(_attitude);
28        matrix.preMultScale(_scale);
29        matrix.preMultTranslate(-_pivotPoint);
30    }
31    else // absolute
32    {
33        matrix.makeRotate(_attitude);
34        matrix.postMultTranslate(_position);
35        matrix.preMultScale(_scale);
36        matrix.preMultTranslate(-_pivotPoint);
37    }
38    return true;
39}
40
41
42bool PositionAttitudeTransform::computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor*) const
43{
44    if (_scale.x() == 0.0 || _scale.y() == 0.0 || _scale.z() == 0.0)
45        return false;
46
47    if (_referenceFrame==RELATIVE_RF)
48    {
49        matrix.postMultTranslate(-_position);
50        matrix.postMultRotate(_attitude.inverse());
51        matrix.postMultScale(Vec3d(1.0/_scale.x(), 1.0/_scale.y(), 1.0/_scale.z()));
52        matrix.postMultTranslate(_pivotPoint);
53    }
54    else // absolute
55    {
56        matrix.makeRotate(_attitude.inverse());
57        matrix.preMultTranslate(-_position);
58        matrix.postMultScale(Vec3d(1.0/_scale.x(), 1.0/_scale.y(), 1.0/_scale.z()));
59        matrix.postMultTranslate(_pivotPoint);
60    }
61    return true;
62}
Note: See TracBrowser for help on using the browser.