xref: /trunk/main/basegfx/source/vector/b3dvector.cxx (revision a3cdc23e488c57f3433f22cd4458e65c27aa499c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_basegfx.hxx"
24 #include <basegfx/vector/b3dvector.hxx>
25 #include <basegfx/matrix/b3dhommatrix.hxx>
26 
27 namespace basegfx
28 {
29     B3DVector& B3DVector::normalize()
30     {
31         double fLen(scalar(*this));
32 
33         if(!::basegfx::fTools::equalZero(fLen))
34         {
35             const double fOne(1.0);
36 
37             if(!::basegfx::fTools::equal(fOne, fLen))
38             {
39                 fLen = sqrt(fLen);
40 
41                 if(!::basegfx::fTools::equalZero(fLen))
42                 {
43                     mfX /= fLen;
44                     mfY /= fLen;
45                     mfZ /= fLen;
46                 }
47             }
48         }
49 
50         return *this;
51     }
52 
53     B3DVector B3DVector::getPerpendicular(const B3DVector& rNormalizedVec) const
54     {
55         B3DVector aNew(*this);
56         aNew = cross(aNew, rNormalizedVec);
57         aNew.normalize();
58         return aNew;
59     }
60 
61     B3DVector B3DVector::getProjectionOnPlane(const B3DVector& rNormalizedPlane) const
62     {
63         B3DVector aNew(*this);
64         aNew = cross(aNew, rNormalizedPlane);
65         aNew = cross(aNew, rNormalizedPlane);
66 
67         aNew.mfX = mfX - aNew.mfX;
68         aNew.mfY = mfY - aNew.mfY;
69         aNew.mfZ = mfZ - aNew.mfZ;
70 
71         return aNew;
72     }
73 
74     B3DVector& B3DVector::operator*=( const ::basegfx::B3DHomMatrix& rMat )
75     {
76         const double fTempX( rMat.get(0,0)*mfX + rMat.get(0,1)*mfY + rMat.get(0,2)*mfZ );
77         const double fTempY( rMat.get(1,0)*mfX + rMat.get(1,1)*mfY + rMat.get(1,2)*mfZ );
78         const double fTempZ( rMat.get(2,0)*mfX + rMat.get(2,1)*mfY + rMat.get(2,2)*mfZ );
79         mfX = fTempX;
80         mfY = fTempY;
81         mfZ = fTempZ;
82 
83         return *this;
84     }
85 
86     B3DVector operator*( const ::basegfx::B3DHomMatrix& rMat, const B3DVector& rVec )
87     {
88         B3DVector aRes( rVec );
89         return aRes*=rMat;
90     }
91 
92     bool areParallel( const B3DVector& rVecA, const B3DVector& rVecB )
93     {
94         // I think fastest is to compare relations, need no square or division
95         if(!fTools::equal(rVecA.getX() * rVecB.getY(), rVecA.getY() * rVecB.getX()))
96             return false;
97 
98         if(!fTools::equal(rVecA.getX() * rVecB.getZ(), rVecA.getZ() * rVecB.getX()))
99             return false;
100 
101         return (fTools::equal(rVecA.getY() * rVecB.getZ(), rVecA.getZ() * rVecB.getY()));
102     }
103 
104 } // end of namespace basegfx
105 
106 /* vim: set noet sw=4 ts=4: */
107