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 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_basegfx.hxx"
26 #include <basegfx/vector/b3dvector.hxx>
27 #include <basegfx/matrix/b3dhommatrix.hxx>
28 
29 //////////////////////////////////////////////////////////////////////////////
30 
31 namespace basegfx
32 {
normalize()33 	B3DVector& B3DVector::normalize()
34 	{
35 		double fLen(scalar(*this));
36 
37 		if(!::basegfx::fTools::equalZero(fLen))
38 		{
39 			const double fOne(1.0);
40 
41 			if(!::basegfx::fTools::equal(fOne, fLen))
42 			{
43 				fLen = sqrt(fLen);
44 
45 				if(!::basegfx::fTools::equalZero(fLen))
46 				{
47 					mfX /= fLen;
48 					mfY /= fLen;
49 					mfZ /= fLen;
50 				}
51 			}
52 		}
53 
54 		return *this;
55 	}
56 
getPerpendicular(const B3DVector & rNormalizedVec) const57 	B3DVector B3DVector::getPerpendicular(const B3DVector& rNormalizedVec) const
58 	{
59 		B3DVector aNew(*this);
60 		aNew = cross(aNew, rNormalizedVec);
61 		aNew.normalize();
62 		return aNew;
63 	}
64 
getProjectionOnPlane(const B3DVector & rNormalizedPlane) const65 	B3DVector B3DVector::getProjectionOnPlane(const B3DVector& rNormalizedPlane) const
66 	{
67 		B3DVector aNew(*this);
68 		aNew = cross(aNew, rNormalizedPlane);
69 		aNew = cross(aNew, rNormalizedPlane);
70 
71 		aNew.mfX = mfX - aNew.mfX;
72 		aNew.mfY = mfY - aNew.mfY;
73 		aNew.mfZ = mfZ - aNew.mfZ;
74 
75 		return aNew;
76 	}
77 
operator *=(const::basegfx::B3DHomMatrix & rMat)78 	B3DVector& B3DVector::operator*=( const ::basegfx::B3DHomMatrix& rMat )
79 	{
80 		const double fTempX( rMat.get(0,0)*mfX + rMat.get(0,1)*mfY + rMat.get(0,2)*mfZ );
81 		const double fTempY( rMat.get(1,0)*mfX + rMat.get(1,1)*mfY + rMat.get(1,2)*mfZ );
82 		const double fTempZ( rMat.get(2,0)*mfX + rMat.get(2,1)*mfY + rMat.get(2,2)*mfZ );
83 		mfX = fTempX;
84 		mfY = fTempY;
85 		mfZ = fTempZ;
86 
87 		return *this;
88 	}
89 
operator *(const::basegfx::B3DHomMatrix & rMat,const B3DVector & rVec)90 	B3DVector operator*( const ::basegfx::B3DHomMatrix& rMat, const B3DVector& rVec )
91 	{
92 		B3DVector aRes( rVec );
93 		return aRes*=rMat;
94 	}
95 
areParallel(const B3DVector & rVecA,const B3DVector & rVecB)96     bool areParallel( const B3DVector& rVecA, const B3DVector& rVecB )
97     {
98         // i think fastest is to compare relations, need no square or division
99         if(!fTools::equal(rVecA.getX() * rVecB.getY(), rVecA.getY() * rVecB.getX()))
100             return false;
101 
102         if(!fTools::equal(rVecA.getX() * rVecB.getZ(), rVecA.getZ() * rVecB.getX()))
103 			return false;
104 
105 		return (fTools::equal(rVecA.getY() * rVecB.getZ(), rVecA.getZ() * rVecB.getY()));
106     }
107 
108 } // end of namespace basegfx
109 
110 //////////////////////////////////////////////////////////////////////////////
111 // eof
112