/src/libreoffice/basegfx/source/vector/b2dvector.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
11 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
12 | | * contributor license agreements. See the NOTICE file distributed |
13 | | * with this work for additional information regarding copyright |
14 | | * ownership. The ASF licenses this file to you under the Apache |
15 | | * License, Version 2.0 (the "License"); you may not use this file |
16 | | * except in compliance with the License. You may obtain a copy of |
17 | | * the License at http://www.apache.org/licenses/LICENSE-2.0 . |
18 | | */ |
19 | | |
20 | | #include <basegfx/vector/b2dvector.hxx> |
21 | | #include <basegfx/matrix/b2dhommatrix.hxx> |
22 | | #include <basegfx/numeric/ftools.hxx> |
23 | | #include <basegfx/vector/b2enums.hxx> |
24 | | #include <cassert> |
25 | | |
26 | | namespace basegfx |
27 | | { |
28 | | B2DVector& B2DVector::normalize() |
29 | 1.54M | { |
30 | 1.54M | double fLen(std::hypot(mnX, mnY)); |
31 | | |
32 | 1.54M | if(!fTools::equalZero(fLen)) |
33 | 1.54M | { |
34 | 1.54M | assert(fLen != 0 && "help coverity see it's not zero"); |
35 | | |
36 | 1.54M | const double fOne(1.0); |
37 | | |
38 | 1.54M | if(!fTools::equal(fOne, fLen)) |
39 | 1.54M | { |
40 | 1.54M | mnX /= fLen; |
41 | 1.54M | mnY /= fLen; |
42 | 1.54M | } |
43 | 1.54M | } |
44 | 3.90k | else |
45 | 3.90k | { |
46 | 3.90k | mnX = 0.0; |
47 | 3.90k | mnY = 0.0; |
48 | 3.90k | } |
49 | | |
50 | 1.54M | return *this; |
51 | 1.54M | } |
52 | | |
53 | | double B2DVector::getLength() const |
54 | 39.4M | { |
55 | 39.4M | if(fTools::equalZero(mnX)) |
56 | 969k | { |
57 | 969k | return fabs(mnY); |
58 | 969k | } |
59 | 38.5M | else if(fTools::equalZero(mnY)) |
60 | 1.23M | { |
61 | 1.23M | return fabs(mnX); |
62 | 1.23M | } |
63 | | |
64 | 37.2M | return hypot( mnX, mnY ); |
65 | 39.4M | } |
66 | | |
67 | | double B2DVector::angle( const B2DVector& rVec ) const |
68 | 18.7M | { |
69 | 18.7M | return atan2(mnX * rVec.getY() - mnY * rVec.getX(), |
70 | 18.7M | mnX * rVec.getX() + mnY * rVec.getY()); |
71 | 18.7M | } |
72 | | |
73 | | const B2DVector& B2DVector::getEmptyVector() |
74 | 2.21M | { |
75 | 2.21M | return static_cast<const B2DVector&>( B2DTuple::getEmptyTuple() ); |
76 | 2.21M | } |
77 | | |
78 | | B2DVector& B2DVector::operator*=( const B2DHomMatrix& rMat ) |
79 | 9.47M | { |
80 | 9.47M | const double fTempX( rMat.get(0,0)*mnX + |
81 | 9.47M | rMat.get(0,1)*mnY ); |
82 | 9.47M | const double fTempY( rMat.get(1,0)*mnX + |
83 | 9.47M | rMat.get(1,1)*mnY ); |
84 | 9.47M | mnX = fTempX; |
85 | 9.47M | mnY = fTempY; |
86 | | |
87 | 9.47M | return *this; |
88 | 9.47M | } |
89 | | |
90 | | B2DVector& B2DVector::setLength(double fLen) |
91 | 371k | { |
92 | 371k | double fLenNow(std::hypot(mnX, mnY)); |
93 | | |
94 | 371k | if(!fTools::equalZero(fLenNow)) |
95 | 369k | { |
96 | 369k | assert(fLenNow != 0 && "help coverity see it's not zero"); |
97 | | |
98 | 369k | const double fOne(1.0); |
99 | | |
100 | 369k | if(!fTools::equal(fOne, fLenNow)) |
101 | 367k | { |
102 | 367k | fLen /= fLenNow; |
103 | 367k | } |
104 | | |
105 | 369k | mnX *= fLen; |
106 | 369k | mnY *= fLen; |
107 | 369k | } |
108 | | |
109 | 371k | return *this; |
110 | 371k | } |
111 | | |
112 | | bool areParallel( const B2DVector& rVecA, const B2DVector& rVecB ) |
113 | 1.78G | { |
114 | 1.78G | const double fValA(rVecA.getX() * rVecB.getY()); |
115 | 1.78G | const double fValB(rVecA.getY() * rVecB.getX()); |
116 | | |
117 | 1.78G | return fTools::equal(fValA, fValB); |
118 | 1.78G | } |
119 | | |
120 | | B2VectorOrientation getOrientation( const B2DVector& rVecA, const B2DVector& rVecB ) |
121 | 549k | { |
122 | 549k | double fVal(rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX()); |
123 | | |
124 | 549k | if(fTools::equalZero(fVal)) |
125 | 347k | { |
126 | 347k | return B2VectorOrientation::Neutral; |
127 | 347k | } |
128 | | |
129 | 201k | if(fVal > 0.0) |
130 | 101k | { |
131 | 101k | return B2VectorOrientation::Positive; |
132 | 101k | } |
133 | 99.8k | else |
134 | 99.8k | { |
135 | 99.8k | return B2VectorOrientation::Negative; |
136 | 99.8k | } |
137 | 201k | } |
138 | | |
139 | | B2DVector getPerpendicular( const B2DVector& rNormalizedVec ) |
140 | 871k | { |
141 | 871k | B2DVector aPerpendicular(-rNormalizedVec.getY(), rNormalizedVec.getX()); |
142 | 871k | return aPerpendicular; |
143 | 871k | } |
144 | | |
145 | | B2DVector getNormalizedPerpendicular( const B2DVector& rVec ) |
146 | 23.2k | { |
147 | 23.2k | B2DVector aPerpendicular(rVec); |
148 | 23.2k | aPerpendicular.normalize(); |
149 | 23.2k | const double aTemp(-aPerpendicular.getY()); |
150 | 23.2k | aPerpendicular.setY(aPerpendicular.getX()); |
151 | 23.2k | aPerpendicular.setX(aTemp); |
152 | 23.2k | return aPerpendicular; |
153 | 23.2k | } |
154 | | |
155 | | B2DVector operator*( const B2DHomMatrix& rMat, const B2DVector& rVec ) |
156 | 9.47M | { |
157 | 9.47M | B2DVector aRes( rVec ); |
158 | 9.47M | aRes *= rMat; |
159 | 9.47M | return aRes; |
160 | 9.47M | } |
161 | | |
162 | | B2VectorContinuity getContinuity(const B2DVector& rBackVector, const B2DVector& rForwardVector ) |
163 | 1.06M | { |
164 | 1.06M | if(rBackVector.equalZero() || rForwardVector.equalZero()) |
165 | 329k | { |
166 | 329k | return B2VectorContinuity::NONE; |
167 | 329k | } |
168 | | |
169 | 739k | if(fTools::equal(rBackVector.getX(), -rForwardVector.getX()) && fTools::equal(rBackVector.getY(), -rForwardVector.getY())) |
170 | 457k | { |
171 | | // same direction and same length -> C2 |
172 | 457k | return B2VectorContinuity::C2; |
173 | 457k | } |
174 | | |
175 | 281k | if(areParallel(rBackVector, rForwardVector) && rBackVector.scalar(rForwardVector) < 0.0) |
176 | 209k | { |
177 | | // parallel and opposite direction -> C1 |
178 | 209k | return B2VectorContinuity::C1; |
179 | 209k | } |
180 | | |
181 | 71.9k | return B2VectorContinuity::NONE; |
182 | 281k | } |
183 | | } // end of namespace basegfx |
184 | | |
185 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |