Line data Source code
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 : #ifndef INCLUDED_BASEGFX_VECTOR_B3IVECTOR_HXX
21 : #define INCLUDED_BASEGFX_VECTOR_B3IVECTOR_HXX
22 :
23 : #include <basegfx/tuple/b3ituple.hxx>
24 : #include <basegfx/basegfxdllapi.h>
25 :
26 : namespace basegfx
27 : {
28 : // predeclaration
29 : class B3DHomMatrix;
30 :
31 : /** Base Point class with three sal_Int32 values
32 :
33 : This class derives all operators and common handling for
34 : a 3D data class from B3ITuple. All necessary extensions
35 : which are special for 3D Vectors are added here.
36 :
37 : @see B3ITuple
38 : */
39 : class BASEGFX_DLLPUBLIC B3IVector : public ::basegfx::B3ITuple
40 : {
41 : public:
42 : /** Create a 3D Vector
43 :
44 : The vector is initialized to (0, 0, 0)
45 : */
46 : B3IVector()
47 : : B3ITuple()
48 : {}
49 :
50 : /** Create a 3D Vector
51 :
52 : @param nX
53 : This parameter is used to initialize the X-coordinate
54 : of the 3D Vector.
55 :
56 : @param nY
57 : This parameter is used to initialize the Y-coordinate
58 : of the 3D Vector.
59 :
60 : @param nZ
61 : This parameter is used to initialize the Z-coordinate
62 : of the 3D Vector.
63 : */
64 : B3IVector(sal_Int32 nX, sal_Int32 nY, sal_Int32 nZ)
65 : : B3ITuple(nX, nY, nZ)
66 : {}
67 :
68 : /** Create a copy of a 3D Vector
69 :
70 : @param rVec
71 : The 3D Vector which will be copied.
72 : */
73 0 : B3IVector(const B3IVector& rVec)
74 0 : : B3ITuple(rVec)
75 0 : {}
76 :
77 : /** constructor with tuple to allow copy-constructing
78 : from B3ITuple-based classes
79 : */
80 : B3IVector(const ::basegfx::B3ITuple& rTuple)
81 : : B3ITuple(rTuple)
82 : {}
83 :
84 0 : ~B3IVector()
85 0 : {}
86 :
87 : /** *=operator to allow usage from B3IVector, too
88 : */
89 : B3IVector& operator*=( const B3IVector& rPnt )
90 : {
91 : mnX *= rPnt.mnX;
92 : mnY *= rPnt.mnY;
93 : mnZ *= rPnt.mnZ;
94 : return *this;
95 : }
96 :
97 : /** *=operator to allow usage from B3IVector, too
98 : */
99 : B3IVector& operator*=(sal_Int32 t)
100 : {
101 : mnX *= t;
102 : mnY *= t;
103 : mnZ *= t;
104 : return *this;
105 : }
106 :
107 : /** assignment operator to allow assigning the results
108 : of B3ITuple calculations
109 : */
110 : B3IVector& operator=( const ::basegfx::B3ITuple& rVec )
111 : {
112 : mnX = rVec.getX();
113 : mnY = rVec.getY();
114 : mnZ = rVec.getZ();
115 : return *this;
116 : }
117 :
118 : /** Calculate the length of this 3D Vector
119 :
120 : @return The Length of the 3D Vector
121 : */
122 : double getLength(void) const
123 : {
124 : double fLen(scalar(*this));
125 : if((0 == fLen) || (1.0 == fLen))
126 : return fLen;
127 : return sqrt(fLen);
128 : }
129 :
130 : /** Calculate the length in the XY-Plane for this 3D Vector
131 :
132 : @return The XY-Plane Length of the 3D Vector
133 : */
134 : double getXYLength(void) const
135 : {
136 : double fLen((mnX * mnX) + (mnY * mnY));
137 : if((0 == fLen) || (1.0 == fLen))
138 : return fLen;
139 : return sqrt(fLen);
140 : }
141 :
142 : /** Calculate the length in the XZ-Plane for this 3D Vector
143 :
144 : @return The XZ-Plane Length of the 3D Vector
145 : */
146 : double getXZLength(void) const
147 : {
148 : double fLen((mnX * mnZ) + (mnY * mnZ));
149 : if((0 == fLen) || (1.0 == fLen))
150 : return fLen;
151 : return sqrt(fLen);
152 : }
153 :
154 : /** Calculate the length in the YZ-Plane for this 3D Vector
155 :
156 : @return The YZ-Plane Length of the 3D Vector
157 : */
158 : double getYZLength(void) const
159 : {
160 : double fLen((mnY * mnY) + (mnZ * mnZ));
161 : if((0 == fLen) || (1.0 == fLen))
162 : return fLen;
163 : return sqrt(fLen);
164 : }
165 :
166 : /** Set the length of this 3D Vector
167 :
168 : @param fLen
169 : The to be achieved length of the 3D Vector
170 : */
171 : B3IVector& setLength(double fLen)
172 : {
173 : double fLenNow(scalar(*this));
174 :
175 : if(!::basegfx::fTools::equalZero(fLenNow))
176 : {
177 : const double fOne(1.0);
178 :
179 : if(!::basegfx::fTools::equal(fOne, fLenNow))
180 : {
181 : fLen /= sqrt(fLenNow);
182 : }
183 :
184 : mnX = fround(mnX*fLen);
185 : mnY = fround(mnY*fLen);
186 : mnZ = fround(mnZ*fLen);
187 : }
188 :
189 : return *this;
190 : }
191 :
192 : /** Calculate the Scalar product
193 :
194 : This method calculates the Scalar product between this
195 : and the given 3D Vector.
196 :
197 : @param rVec
198 : A second 3D Vector.
199 :
200 : @return
201 : The Scalar Product of two 3D Vectors
202 : */
203 : double scalar(const B3IVector& rVec) const
204 : {
205 : return ((mnX * rVec.mnX) + (mnY * rVec.mnY) + (mnZ * rVec.mnZ));
206 : }
207 :
208 : /** Transform vector by given transformation matrix.
209 :
210 : Since this is a vector, translational components of the
211 : matrix are disregarded.
212 : */
213 : B3IVector& operator*=( const B3DHomMatrix& rMat );
214 : };
215 :
216 : // external operators
217 :
218 :
219 : /** Transform vector by given transformation matrix.
220 :
221 : Since this is a vector, translational components of the
222 : matrix are disregarded.
223 : */
224 : BASEGFX_DLLPUBLIC B3IVector operator*( const B3DHomMatrix& rMat, const B3IVector& rVec );
225 :
226 : /** Calculate the Cross Product of two 3D Vectors
227 :
228 : @param rVecA
229 : A first 3D Vector.
230 :
231 : @param rVecB
232 : A second 3D Vector.
233 :
234 : @return
235 : The Cross Product of both 3D Vectors
236 : */
237 : inline B3IVector cross(const B3IVector& rVecA, const B3IVector& rVecB)
238 : {
239 : B3IVector aVec(
240 : rVecA.getY() * rVecB.getZ() - rVecA.getZ() * rVecB.getY(),
241 : rVecA.getZ() * rVecB.getX() - rVecA.getX() * rVecB.getZ(),
242 : rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
243 : return aVec;
244 : }
245 : } // end of namespace basegfx
246 :
247 : #endif // INCLUDED_BASEGFX_VECTOR_B3IVECTOR_HXX
248 :
249 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|