OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WGridTransformOrtho.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include "../common/exceptions/WPreconditionNotMet.h"
26 
27 #include "WGridTransformOrtho.h"
28 
30  : m_directionX( 1.0, 0.0, 0.0 ),
31  m_directionY( 0.0, 1.0, 0.0 ),
32  m_directionZ( 0.0, 0.0, 1.0 ),
33  m_scaling( 1.0, 1.0, 1.0 ),
34  m_origin( 0.0, 0.0, 0.0 )
35 {
36 }
37 
38 WGridTransformOrtho::WGridTransformOrtho( double scaleX, double scaleY, double scaleZ )
39  : m_directionX( ( scaleX > 0.0 ) - ( scaleX < 0.0 ), 0.0, 0.0 ),
40  m_directionY( 0.0, ( scaleY > 0.0 ) - ( scaleY < 0.0 ), 0.0 ),
41  m_directionZ( 0.0, 0.0, ( scaleZ > 0.0 ) - ( scaleZ < 0.0 ) ),
42  m_scaling( fabs( scaleX ), fabs( scaleY ), fabs( scaleZ ) ),
43  m_origin( 0.0, 0.0, 0.0 )
44 {
45  WPrecond( m_scaling[ 0 ] != 0.0 && m_scaling[ 1 ] != 0.0 && m_scaling[ 2 ] != 0.0, "" );
46 }
47 
49 {
50  WPrecond( mat.getNbRows() == 4 && mat.getNbCols() == 4, "" );
51  m_directionX = WVector3d( mat( 0, 0 ), mat( 1, 0 ), mat( 2, 0 ) );
52  m_directionY = WVector3d( mat( 0, 1 ), mat( 1, 1 ), mat( 2, 1 ) );
53  m_directionZ = WVector3d( mat( 0, 2 ), mat( 1, 2 ), mat( 2, 2 ) );
54 
55  m_scaling = WVector3d( length( m_directionX ), length( m_directionY ), length( m_directionZ ) );
56 
57  WPrecond( m_scaling[ 0 ] != 0.0 && m_scaling[ 1 ] != 0.0 && m_scaling[ 2 ] != 0.0, "" );
58  m_directionX /= m_scaling[ 0 ];
59  m_directionY /= m_scaling[ 1 ];
60  m_directionZ /= m_scaling[ 2 ];
61 
62  WPrecondLess( fabs( dot( m_directionX, m_directionY ) ), 0.0001 );
63  WPrecondLess( fabs( dot( m_directionX, m_directionZ ) ), 0.0001 );
64  WPrecondLess( fabs( dot( m_directionY, m_directionZ ) ), 0.0001 );
65  m_origin = WVector3d( mat( 0, 3 ), mat( 1, 3 ), mat( 2, 3 ) );
66 }
67 
69 {
70 }
71 
73 {
74  return WVector3d( m_scaling[ 0 ] * position[ 0 ] * m_directionX[ 0 ] + m_scaling[ 1 ] * position[ 1 ] * m_directionY[ 0 ]
75  + m_scaling[ 2 ] * position[ 2 ] * m_directionZ[ 0 ] + m_origin[ 0 ],
76  m_scaling[ 0 ] * position[ 0 ] * m_directionX[ 1 ] + m_scaling[ 1 ] * position[ 1 ] * m_directionY[ 1 ]
77  + m_scaling[ 2 ] * position[ 2 ] * m_directionZ[ 1 ] + m_origin[ 1 ],
78  m_scaling[ 0 ] * position[ 0 ] * m_directionX[ 2 ] + m_scaling[ 1 ] * position[ 1 ] * m_directionY[ 2 ]
79  + m_scaling[ 2 ] * position[ 2 ] * m_directionZ[ 2 ] + m_origin[ 2 ] );
80 }
81 
83 {
84  WVector3d p = position - m_origin;
85  p = WVector3d( dot( p, m_directionX ), dot( p, m_directionY ), dot( p, m_directionZ ) );
86  p[ 0 ] /= m_scaling[ 0 ];
87  p[ 1 ] /= m_scaling[ 1 ];
88  p[ 2 ] /= m_scaling[ 2 ];
89  return p;
90 }
91 
93 {
94  return WVector3d( m_scaling[ 0 ] * direction[ 0 ] * m_directionX[ 0 ] + m_scaling[ 1 ] * direction[ 1 ] * m_directionY[ 0 ]
95  + m_scaling[ 2 ] * direction[ 2 ] * m_directionZ[ 0 ],
96  m_scaling[ 0 ] * direction[ 0 ] * m_directionX[ 1 ] + m_scaling[ 1 ] * direction[ 1 ] * m_directionY[ 1 ]
97  + m_scaling[ 2 ] * direction[ 2 ] * m_directionZ[ 1 ],
98  m_scaling[ 0 ] * direction[ 0 ] * m_directionX[ 2 ] + m_scaling[ 1 ] * direction[ 1 ] * m_directionY[ 2 ]
99  + m_scaling[ 2 ] * direction[ 2 ] * m_directionZ[ 2 ] );
100 }
101 
103 {
104  WVector3d p( dot( direction, m_directionX ), dot( direction, m_directionY ), dot( direction, m_directionZ ) );
105  p[ 0 ] /= m_scaling[ 0 ];
106  p[ 1 ] /= m_scaling[ 1 ];
107  p[ 2 ] /= m_scaling[ 2 ];
108  return p;
109 }
110 
112 {
113  return m_scaling[ 0 ];
114 }
115 
117 {
118  return m_scaling[ 1 ];
119 }
120 
122 {
123  return m_scaling[ 2 ];
124 }
125 
127 {
128  return m_directionX;
129 }
130 
132 {
133  return m_directionY;
134 }
135 
137 {
138  return m_directionZ;
139 }
140 
142 {
143  return m_directionX * m_scaling[ 0 ];
144 }
145 
147 {
148  return m_directionY * m_scaling[ 1 ];
149 }
150 
152 {
153  return m_directionZ * m_scaling[ 2 ];
154 }
155 
157 {
158  return m_origin;
159 }
160 
162 {
163  WMatrix< double > mat( 4, 4 );
164  mat.makeIdentity();
165  mat( 0, 0 ) = m_scaling[ 0 ] * m_directionX[ 0 ];
166  mat( 1, 0 ) = m_scaling[ 0 ] * m_directionX[ 1 ];
167  mat( 2, 0 ) = m_scaling[ 0 ] * m_directionX[ 2 ];
168  mat( 0, 1 ) = m_scaling[ 1 ] * m_directionY[ 0 ];
169  mat( 1, 1 ) = m_scaling[ 1 ] * m_directionY[ 1 ];
170  mat( 2, 1 ) = m_scaling[ 1 ] * m_directionY[ 2 ];
171  mat( 0, 2 ) = m_scaling[ 2 ] * m_directionZ[ 0 ];
172  mat( 1, 2 ) = m_scaling[ 2 ] * m_directionZ[ 1 ];
173  mat( 2, 2 ) = m_scaling[ 2 ] * m_directionZ[ 2 ];
174  mat( 0, 3 ) = m_origin[ 0 ];
175  mat( 1, 3 ) = m_origin[ 1 ];
176  mat( 2, 3 ) = m_origin[ 2 ];
177  return mat;
178 }
179 
181 {
182  return m_directionX == WVector3d( 1.0, 0.0, 0.0 )
183  && m_directionY == WVector3d( 0.0, 1.0, 0.0 )
184  && m_directionZ == WVector3d( 0.0, 0.0, 1.0 );
185 }
186 
187 WGridTransformOrtho::operator WMatrix4d() const
188 {
190  mat( 0, 0 ) = m_scaling[ 0 ] * m_directionX[ 0 ];
191  mat( 0, 1 ) = m_scaling[ 0 ] * m_directionX[ 1 ];
192  mat( 0, 2 ) = m_scaling[ 0 ] * m_directionX[ 2 ];
193  mat( 1, 0 ) = m_scaling[ 1 ] * m_directionY[ 0 ];
194  mat( 1, 1 ) = m_scaling[ 1 ] * m_directionY[ 1 ];
195  mat( 1, 2 ) = m_scaling[ 1 ] * m_directionY[ 2 ];
196  mat( 2, 0 ) = m_scaling[ 2 ] * m_directionZ[ 0 ];
197  mat( 2, 1 ) = m_scaling[ 2 ] * m_directionZ[ 1 ];
198  mat( 2, 2 ) = m_scaling[ 2 ] * m_directionZ[ 2 ];
199  mat( 3, 0 ) = m_origin[ 0 ];
200  mat( 3, 1 ) = m_origin[ 1 ];
201  mat( 3, 2 ) = m_origin[ 2 ];
202  return mat;
203 }
204