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