WGridTransformOrtho.cpp

00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include "../common/exceptions/WPreconditionNotMet.h"
00026 
00027 #include "WGridTransformOrtho.h"
00028 
00029 WGridTransformOrtho::WGridTransformOrtho()
00030     : m_directionX( 1.0, 0.0, 0.0 ),
00031       m_directionY( 0.0, 1.0, 0.0 ),
00032       m_directionZ( 0.0, 0.0, 1.0 ),
00033       m_scaling( 1.0, 1.0, 1.0 ),
00034       m_origin( 0.0, 0.0, 0.0 )
00035 {
00036 }
00037 
00038 WGridTransformOrtho::WGridTransformOrtho( double scaleX, double scaleY, double scaleZ )
00039     : m_directionX( ( scaleX > 0.0 ) - ( scaleX < 0.0 ), 0.0, 0.0 ),
00040       m_directionY( 0.0, ( scaleY > 0.0 ) - ( scaleY < 0.0 ), 0.0 ),
00041       m_directionZ( 0.0, 0.0, ( scaleZ > 0.0 ) - ( scaleZ < 0.0 ) ),
00042       m_scaling( fabs( scaleX ), fabs( scaleY ), fabs( scaleZ ) ),
00043       m_origin( 0.0, 0.0, 0.0 )
00044 {
00045     WPrecond( m_scaling[ 0 ] != 0.0 && m_scaling[ 1 ] != 0.0 && m_scaling[ 2 ] != 0.0, "" );
00046 }
00047 
00048 WGridTransformOrtho::WGridTransformOrtho( WMatrix< double > const& mat )
00049 {
00050     WPrecond( mat.getNbRows() == 4 && mat.getNbCols() == 4, "" );
00051     m_directionX = WVector3d( mat( 0, 0 ), mat( 1, 0 ), mat( 2, 0 ) );
00052     m_directionY = WVector3d( mat( 0, 1 ), mat( 1, 1 ), mat( 2, 1 ) );
00053     m_directionZ = WVector3d( mat( 0, 2 ), mat( 1, 2 ), mat( 2, 2 ) );
00054 
00055     m_scaling = WVector3d( length( m_directionX ), length( m_directionY ), length( m_directionZ ) );
00056 
00057     WPrecond( m_scaling[ 0 ] != 0.0 && m_scaling[ 1 ] != 0.0 && m_scaling[ 2 ] != 0.0, "" );
00058     m_directionX /= m_scaling[ 0 ];
00059     m_directionY /= m_scaling[ 1 ];
00060     m_directionZ /= m_scaling[ 2 ];
00061 
00062     WPrecondLess( fabs( dot( m_directionX, m_directionY ) ), 0.0001 );
00063     WPrecondLess( fabs( dot( m_directionX, m_directionZ ) ), 0.0001 );
00064     WPrecondLess( fabs( dot( m_directionY, m_directionZ ) ), 0.0001 );
00065     m_origin = WVector3d( mat( 0, 3 ), mat( 1, 3 ), mat( 2, 3 ) );
00066 }
00067 
00068 WGridTransformOrtho::~WGridTransformOrtho()
00069 {
00070 }
00071 
00072 WVector3d WGridTransformOrtho::positionToWorldSpace( WVector3d const& position ) const
00073 {
00074     return WVector3d( m_scaling[ 0 ] * position[ 0 ] * m_directionX[ 0 ] + m_scaling[ 1 ] * position[ 1 ] * m_directionY[ 0 ]
00075                     + m_scaling[ 2 ] * position[ 2 ] * m_directionZ[ 0 ] + m_origin[ 0 ],
00076                       m_scaling[ 0 ] * position[ 0 ] * m_directionX[ 1 ] + m_scaling[ 1 ] * position[ 1 ] * m_directionY[ 1 ]
00077                     + m_scaling[ 2 ] * position[ 2 ] * m_directionZ[ 1 ] + m_origin[ 1 ],
00078                       m_scaling[ 0 ] * position[ 0 ] * m_directionX[ 2 ] + m_scaling[ 1 ] * position[ 1 ] * m_directionY[ 2 ]
00079                     + m_scaling[ 2 ] * position[ 2 ] * m_directionZ[ 2 ] + m_origin[ 2 ] );
00080 }
00081 
00082 WVector3d WGridTransformOrtho::positionToGridSpace( WVector3d const& position ) const
00083 {
00084     WVector3d p = position - m_origin;
00085     p = WVector3d( dot( p, m_directionX ), dot( p, m_directionY ), dot( p, m_directionZ ) );
00086     p[ 0 ] /= m_scaling[ 0 ];
00087     p[ 1 ] /= m_scaling[ 1 ];
00088     p[ 2 ] /= m_scaling[ 2 ];
00089     return p;
00090 }
00091 
00092 WVector3d WGridTransformOrtho::directionToWorldSpace( WVector3d const& direction ) const
00093 {
00094     return WVector3d( m_scaling[ 0 ] * direction[ 0 ] * m_directionX[ 0 ] + m_scaling[ 1 ] * direction[ 1 ] * m_directionY[ 0 ]
00095                     + m_scaling[ 2 ] * direction[ 2 ] * m_directionZ[ 0 ],
00096                       m_scaling[ 0 ] * direction[ 0 ] * m_directionX[ 1 ] + m_scaling[ 1 ] * direction[ 1 ] * m_directionY[ 1 ]
00097                     + m_scaling[ 2 ] * direction[ 2 ] * m_directionZ[ 1 ],
00098                       m_scaling[ 0 ] * direction[ 0 ] * m_directionX[ 2 ] + m_scaling[ 1 ] * direction[ 1 ] * m_directionY[ 2 ]
00099                     + m_scaling[ 2 ] * direction[ 2 ] * m_directionZ[ 2 ] );
00100 }
00101 
00102 WVector3d WGridTransformOrtho::directionToGridSpace( WVector3d const& direction ) const
00103 {
00104     WVector3d p( dot( direction, m_directionX ), dot( direction, m_directionY ), dot( direction, m_directionZ ) );
00105     p[ 0 ] /= m_scaling[ 0 ];
00106     p[ 1 ] /= m_scaling[ 1 ];
00107     p[ 2 ] /= m_scaling[ 2 ];
00108     return p;
00109 }
00110 
00111 double WGridTransformOrtho::getOffsetX() const
00112 {
00113     return m_scaling[ 0 ];
00114 }
00115 
00116 double WGridTransformOrtho::getOffsetY() const
00117 {
00118     return m_scaling[ 1 ];
00119 }
00120 
00121 double WGridTransformOrtho::getOffsetZ() const
00122 {
00123     return m_scaling[ 2 ];
00124 }
00125 
00126 WVector3d WGridTransformOrtho::getUnitDirectionX() const
00127 {
00128     return m_directionX;
00129 }
00130 
00131 WVector3d WGridTransformOrtho::getUnitDirectionY() const
00132 {
00133     return m_directionY;
00134 }
00135 
00136 WVector3d WGridTransformOrtho::getUnitDirectionZ() const
00137 {
00138     return m_directionZ;
00139 }
00140 
00141 WVector3d WGridTransformOrtho::getDirectionX() const
00142 {
00143     return m_directionX * m_scaling[ 0 ];
00144 }
00145 
00146 WVector3d WGridTransformOrtho::getDirectionY() const
00147 {
00148     return m_directionY * m_scaling[ 1 ];
00149 }
00150 
00151 WVector3d WGridTransformOrtho::getDirectionZ() const
00152 {
00153     return m_directionZ * m_scaling[ 2 ];
00154 }
00155 
00156 WPosition WGridTransformOrtho::getOrigin() const
00157 {
00158     return m_origin;
00159 }
00160 
00161 WMatrix< double > WGridTransformOrtho::getTransformationMatrix() const
00162 {
00163     WMatrix< double > mat( 4, 4 );
00164     mat.makeIdentity();
00165     mat( 0, 0 ) = m_scaling[ 0 ] * m_directionX[ 0 ];
00166     mat( 1, 0 ) = m_scaling[ 0 ] * m_directionX[ 1 ];
00167     mat( 2, 0 ) = m_scaling[ 0 ] * m_directionX[ 2 ];
00168     mat( 0, 1 ) = m_scaling[ 1 ] * m_directionY[ 0 ];
00169     mat( 1, 1 ) = m_scaling[ 1 ] * m_directionY[ 1 ];
00170     mat( 2, 1 ) = m_scaling[ 1 ] * m_directionY[ 2 ];
00171     mat( 0, 2 ) = m_scaling[ 2 ] * m_directionZ[ 0 ];
00172     mat( 1, 2 ) = m_scaling[ 2 ] * m_directionZ[ 1 ];
00173     mat( 2, 2 ) = m_scaling[ 2 ] * m_directionZ[ 2 ];
00174     mat( 0, 3 ) = m_origin[ 0 ];
00175     mat( 1, 3 ) = m_origin[ 1 ];
00176     mat( 2, 3 ) = m_origin[ 2 ];
00177     return mat;
00178 }
00179 
00180 bool WGridTransformOrtho::isNotRotated() const
00181 {
00182     return m_directionX == WVector3d( 1.0, 0.0, 0.0 )
00183         && m_directionY == WVector3d( 0.0, 1.0, 0.0 )
00184         && m_directionZ == WVector3d( 0.0, 0.0, 1.0 );
00185 }
00186 
00187 WGridTransformOrtho::operator WMatrix4d() const
00188 {
00189     WMatrix4d mat = WMatrix4d::identity();
00190     mat( 0, 0 ) = m_scaling[ 0 ] * m_directionX[ 0 ];
00191     mat( 0, 1 ) = m_scaling[ 0 ] * m_directionX[ 1 ];
00192     mat( 0, 2 ) = m_scaling[ 0 ] * m_directionX[ 2 ];
00193     mat( 1, 0 ) = m_scaling[ 1 ] * m_directionY[ 0 ];
00194     mat( 1, 1 ) = m_scaling[ 1 ] * m_directionY[ 1 ];
00195     mat( 1, 2 ) = m_scaling[ 1 ] * m_directionY[ 2 ];
00196     mat( 2, 0 ) = m_scaling[ 2 ] * m_directionZ[ 0 ];
00197     mat( 2, 1 ) = m_scaling[ 2 ] * m_directionZ[ 1 ];
00198     mat( 2, 2 ) = m_scaling[ 2 ] * m_directionZ[ 2 ];
00199     mat( 3, 0 ) = m_origin[ 0 ];
00200     mat( 3, 1 ) = m_origin[ 1 ];
00201     mat( 3, 2 ) = m_origin[ 2 ];
00202     return mat;
00203 }
00204 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends