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