Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <basegfx/vector/b2dvector.hxx>
30 : : #include <basegfx/matrix/b2dhommatrix.hxx>
31 : : #include <basegfx/numeric/ftools.hxx>
32 : :
33 : : namespace basegfx
34 : : {
35 : 210793 : B2DVector& B2DVector::normalize()
36 : : {
37 [ + - ]: 210793 : double fLen(scalar(*this));
38 : :
39 [ - + ]: 210793 : if(fTools::equalZero(fLen))
40 : : {
41 : 0 : mfX = 0.0;
42 : 0 : mfY = 0.0;
43 : : }
44 : : else
45 : : {
46 : 210793 : const double fOne(1.0);
47 : :
48 [ + + ]: 210793 : if(!fTools::equal(fOne, fLen))
49 : : {
50 : 109130 : fLen = sqrt(fLen);
51 : :
52 [ + - ]: 109130 : if(!fTools::equalZero(fLen))
53 : : {
54 : 109130 : mfX /= fLen;
55 : 210793 : mfY /= fLen;
56 : : }
57 : : }
58 : : }
59 : :
60 : 210793 : return *this;
61 : : }
62 : :
63 : 17607 : B2DVector& B2DVector::operator=( const B2DTuple& rVec )
64 : : {
65 : 17607 : mfX = rVec.getX();
66 : 17607 : mfY = rVec.getY();
67 : 17607 : return *this;
68 : : }
69 : :
70 : :
71 : 414191 : double B2DVector::getLength() const
72 : : {
73 [ + + ]: 414191 : if(fTools::equalZero(mfX))
74 : : {
75 : 54428 : return fabs(mfY);
76 : : }
77 [ + + ]: 359763 : else if(fTools::equalZero(mfY))
78 : : {
79 : 341936 : return fabs(mfX);
80 : : }
81 : :
82 : 414191 : return hypot( mfX, mfY );
83 : : }
84 : :
85 : 240007 : double B2DVector::scalar( const B2DVector& rVec ) const
86 : : {
87 : 240007 : return((mfX * rVec.mfX) + (mfY * rVec.mfY));
88 : : }
89 : :
90 : 31253 : double B2DVector::cross( const B2DVector& rVec ) const
91 : : {
92 : 31253 : return(mfX * rVec.getY() - mfY * rVec.getX());
93 : : }
94 : :
95 : 3200 : double B2DVector::angle( const B2DVector& rVec ) const
96 : : {
97 : 3200 : return atan2(mfX * rVec.getY() - mfY * rVec.getX(),
98 : 6400 : mfX * rVec.getX() + mfY * rVec.getY());
99 : : }
100 : :
101 : 53 : const B2DVector& B2DVector::getEmptyVector()
102 : : {
103 : 53 : return (const B2DVector&) B2DTuple::getEmptyTuple();
104 : : }
105 : :
106 : 630133 : B2DVector& B2DVector::operator*=( const B2DHomMatrix& rMat )
107 : : {
108 : 630133 : const double fTempX( rMat.get(0,0)*mfX +
109 : 630133 : rMat.get(0,1)*mfY );
110 : 630133 : const double fTempY( rMat.get(1,0)*mfX +
111 : 630133 : rMat.get(1,1)*mfY );
112 : 630133 : mfX = fTempX;
113 : 630133 : mfY = fTempY;
114 : :
115 : 630133 : return *this;
116 : : }
117 : :
118 : 0 : B2DVector& B2DVector::setLength(double fLen)
119 : : {
120 [ # # ]: 0 : double fLenNow(scalar(*this));
121 : :
122 [ # # ]: 0 : if(!fTools::equalZero(fLenNow))
123 : : {
124 : 0 : const double fOne(10.0);
125 : :
126 [ # # ]: 0 : if(!fTools::equal(fOne, fLenNow))
127 : : {
128 : 0 : fLen /= sqrt(fLenNow);
129 : : }
130 : :
131 : 0 : mfX *= fLen;
132 : 0 : mfY *= fLen;
133 : : }
134 : :
135 : 0 : return *this;
136 : : }
137 : :
138 : 79891 : bool areParallel( const B2DVector& rVecA, const B2DVector& rVecB )
139 : : {
140 : 79891 : const double fValA(rVecA.getX() * rVecB.getY());
141 : 79891 : const double fValB(rVecA.getY() * rVecB.getX());
142 : :
143 : 79891 : return fTools::equal(fValA, fValB);
144 : : }
145 : :
146 : 7953 : B2VectorOrientation getOrientation( const B2DVector& rVecA, const B2DVector& rVecB )
147 : : {
148 : 7953 : double fVal(rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
149 : :
150 [ + + ]: 7953 : if(fTools::equalZero(fVal))
151 : : {
152 : 845 : return ORIENTATION_NEUTRAL;
153 : : }
154 : :
155 [ + + ]: 7108 : if(fVal > 0.0)
156 : : {
157 : 5906 : return ORIENTATION_POSITIVE;
158 : : }
159 : : else
160 : : {
161 : 7953 : return ORIENTATION_NEGATIVE;
162 : : }
163 : : }
164 : :
165 : 21604 : B2DVector getPerpendicular( const B2DVector& rNormalizedVec )
166 : : {
167 : 21604 : B2DVector aPerpendicular(-rNormalizedVec.getY(), rNormalizedVec.getX());
168 : 21604 : return aPerpendicular;
169 : : }
170 : :
171 : 69485 : B2DVector getNormalizedPerpendicular( const B2DVector& rVec )
172 : : {
173 : 69485 : B2DVector aPerpendicular(rVec);
174 [ + - ]: 69485 : aPerpendicular.normalize();
175 : 69485 : const double aTemp(-aPerpendicular.getY());
176 : 69485 : aPerpendicular.setY(aPerpendicular.getX());
177 : 69485 : aPerpendicular.setX(aTemp);
178 : 69485 : return aPerpendicular;
179 : : }
180 : :
181 : 630108 : B2DVector operator*( const B2DHomMatrix& rMat, const B2DVector& rVec )
182 : : {
183 : 630108 : B2DVector aRes( rVec );
184 [ + - ]: 630108 : return aRes*=rMat;
185 : : }
186 : :
187 : 64217 : B2VectorContinuity getContinuity(const B2DVector& rBackVector, const B2DVector& rForwardVector )
188 : : {
189 [ + + ][ - + ]: 64217 : if(rBackVector.equalZero() || rForwardVector.equalZero())
[ + + ]
190 : : {
191 : 6191 : return CONTINUITY_NONE;
192 : : }
193 : :
194 [ + + ][ + + ]: 58026 : if(fTools::equal(rBackVector.getX(), -rForwardVector.getX()) && fTools::equal(rBackVector.getY(), -rForwardVector.getY()))
[ + + ][ + + ]
[ + - ][ + - ]
[ + + ]
195 : : {
196 : : // same direction and same length -> C2
197 : 15084 : return CONTINUITY_C2;
198 : : }
199 : :
200 [ + + ][ + + ]: 42942 : if(areParallel(rBackVector, rForwardVector) && rBackVector.scalar(rForwardVector) < 0.0)
[ + + ]
201 : : {
202 : : // parallel and opposite direction -> C1
203 : 20695 : return CONTINUITY_C1;
204 : : }
205 : :
206 : 64217 : return CONTINUITY_NONE;
207 : : }
208 : : } // end of namespace basegfx
209 : :
210 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|