xref: /trunk/main/basegfx/source/vector/b3dvector.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org 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 version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_basegfx.hxx"
30 #include <basegfx/vector/b3dvector.hxx>
31 #include <basegfx/matrix/b3dhommatrix.hxx>
32 
33 //////////////////////////////////////////////////////////////////////////////
34 
35 namespace basegfx
36 {
37     B3DVector& B3DVector::normalize()
38     {
39         double fLen(scalar(*this));
40 
41         if(!::basegfx::fTools::equalZero(fLen))
42         {
43             const double fOne(1.0);
44 
45             if(!::basegfx::fTools::equal(fOne, fLen))
46             {
47                 fLen = sqrt(fLen);
48 
49                 if(!::basegfx::fTools::equalZero(fLen))
50                 {
51                     mfX /= fLen;
52                     mfY /= fLen;
53                     mfZ /= fLen;
54                 }
55             }
56         }
57 
58         return *this;
59     }
60 
61     B3DVector B3DVector::getPerpendicular(const B3DVector& rNormalizedVec) const
62     {
63         B3DVector aNew(*this);
64         aNew = cross(aNew, rNormalizedVec);
65         aNew.normalize();
66         return aNew;
67     }
68 
69     B3DVector B3DVector::getProjectionOnPlane(const B3DVector& rNormalizedPlane) const
70     {
71         B3DVector aNew(*this);
72         aNew = cross(aNew, rNormalizedPlane);
73         aNew = cross(aNew, rNormalizedPlane);
74 
75         aNew.mfX = mfX - aNew.mfX;
76         aNew.mfY = mfY - aNew.mfY;
77         aNew.mfZ = mfZ - aNew.mfZ;
78 
79         return aNew;
80     }
81 
82     B3DVector& B3DVector::operator*=( const ::basegfx::B3DHomMatrix& rMat )
83     {
84         const double fTempX( rMat.get(0,0)*mfX + rMat.get(0,1)*mfY + rMat.get(0,2)*mfZ );
85         const double fTempY( rMat.get(1,0)*mfX + rMat.get(1,1)*mfY + rMat.get(1,2)*mfZ );
86         const double fTempZ( rMat.get(2,0)*mfX + rMat.get(2,1)*mfY + rMat.get(2,2)*mfZ );
87         mfX = fTempX;
88         mfY = fTempY;
89         mfZ = fTempZ;
90 
91         return *this;
92     }
93 
94     B3DVector operator*( const ::basegfx::B3DHomMatrix& rMat, const B3DVector& rVec )
95     {
96         B3DVector aRes( rVec );
97         return aRes*=rMat;
98     }
99 
100     bool areParallel( const B3DVector& rVecA, const B3DVector& rVecB )
101     {
102         // i think fastest is to compare relations, need no square or division
103         if(!fTools::equal(rVecA.getX() * rVecB.getY(), rVecA.getY() * rVecB.getX()))
104             return false;
105 
106         if(!fTools::equal(rVecA.getX() * rVecB.getZ(), rVecA.getZ() * rVecB.getX()))
107             return false;
108 
109         return (fTools::equal(rVecA.getY() * rVecB.getZ(), rVecA.getZ() * rVecB.getY()));
110     }
111 
112 } // end of namespace basegfx
113 
114 //////////////////////////////////////////////////////////////////////////////
115 // eof
116