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