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 : #include <rtl/instance.hxx>
21 : #include <basegfx/matrix/b3dhommatrix.hxx>
22 : #include <hommatrixtemplate.hxx>
23 : #include <basegfx/vector/b3dvector.hxx>
24 :
25 : namespace basegfx
26 : {
27 209056 : class Impl3DHomMatrix : public ::basegfx::internal::ImplHomMatrixTemplate< 4 >
28 : {
29 : };
30 :
31 : namespace { struct IdentityMatrix : public rtl::Static< B3DHomMatrix::ImplType,
32 : IdentityMatrix > {}; }
33 :
34 88200 : B3DHomMatrix::B3DHomMatrix() :
35 88200 : mpImpl( IdentityMatrix::get() ) // use common identity matrix
36 : {
37 88200 : }
38 :
39 66409 : B3DHomMatrix::B3DHomMatrix(const B3DHomMatrix& rMat) :
40 66409 : mpImpl(rMat.mpImpl)
41 : {
42 66409 : }
43 :
44 154609 : B3DHomMatrix::~B3DHomMatrix()
45 : {
46 154609 : }
47 :
48 41122 : B3DHomMatrix& B3DHomMatrix::operator=(const B3DHomMatrix& rMat)
49 : {
50 41122 : mpImpl = rMat.mpImpl;
51 41122 : return *this;
52 : }
53 :
54 774855 : double B3DHomMatrix::get(sal_uInt16 nRow, sal_uInt16 nColumn) const
55 : {
56 774855 : return mpImpl->get(nRow, nColumn);
57 : }
58 :
59 435550 : void B3DHomMatrix::set(sal_uInt16 nRow, sal_uInt16 nColumn, double fValue)
60 : {
61 435550 : mpImpl->set(nRow, nColumn, fValue);
62 435550 : }
63 :
64 49710 : bool B3DHomMatrix::isLastLineDefault() const
65 : {
66 49710 : return mpImpl->isLastLineDefault();
67 : }
68 :
69 67246 : bool B3DHomMatrix::isIdentity() const
70 : {
71 67246 : if(mpImpl.same_object(IdentityMatrix::get()))
72 23440 : return true;
73 :
74 43806 : return mpImpl->isIdentity();
75 : }
76 :
77 2476 : void B3DHomMatrix::identity()
78 : {
79 2476 : mpImpl = IdentityMatrix::get();
80 2476 : }
81 :
82 2883 : bool B3DHomMatrix::invert()
83 : {
84 2883 : Impl3DHomMatrix aWork(*mpImpl);
85 2883 : sal_uInt16* pIndex = new sal_uInt16[mpImpl->getEdgeLength()];
86 : sal_Int16 nParity;
87 :
88 2883 : if(aWork.ludcmp(pIndex, nParity))
89 : {
90 2883 : mpImpl->doInvert(aWork, pIndex);
91 2883 : delete[] pIndex;
92 :
93 2883 : return true;
94 : }
95 :
96 0 : delete[] pIndex;
97 0 : return false;
98 : }
99 :
100 699 : double B3DHomMatrix::determinant() const
101 : {
102 699 : return mpImpl->doDeterminant();
103 : }
104 :
105 0 : B3DHomMatrix& B3DHomMatrix::operator+=(const B3DHomMatrix& rMat)
106 : {
107 0 : mpImpl->doAddMatrix(*rMat.mpImpl);
108 0 : return *this;
109 : }
110 :
111 0 : B3DHomMatrix& B3DHomMatrix::operator-=(const B3DHomMatrix& rMat)
112 : {
113 0 : mpImpl->doSubMatrix(*rMat.mpImpl);
114 0 : return *this;
115 : }
116 :
117 0 : B3DHomMatrix& B3DHomMatrix::operator*=(double fValue)
118 : {
119 0 : const double fOne(1.0);
120 :
121 0 : if(!fTools::equal(fOne, fValue))
122 0 : mpImpl->doMulMatrix(fValue);
123 :
124 0 : return *this;
125 : }
126 :
127 0 : B3DHomMatrix& B3DHomMatrix::operator/=(double fValue)
128 : {
129 0 : const double fOne(1.0);
130 :
131 0 : if(!fTools::equal(fOne, fValue))
132 0 : mpImpl->doMulMatrix(1.0 / fValue);
133 :
134 0 : return *this;
135 : }
136 :
137 28948 : B3DHomMatrix& B3DHomMatrix::operator*=(const B3DHomMatrix& rMat)
138 : {
139 28948 : if(!rMat.isIdentity())
140 18725 : mpImpl->doMulMatrix(*rMat.mpImpl);
141 :
142 28948 : return *this;
143 : }
144 :
145 10227 : bool B3DHomMatrix::operator==(const B3DHomMatrix& rMat) const
146 : {
147 10227 : if(mpImpl.same_object(rMat.mpImpl))
148 7949 : return true;
149 :
150 2278 : return mpImpl->isEqual(*rMat.mpImpl);
151 : }
152 :
153 2294 : bool B3DHomMatrix::operator!=(const B3DHomMatrix& rMat) const
154 : {
155 2294 : return !(*this == rMat);
156 : }
157 :
158 1348 : void B3DHomMatrix::rotate(double fAngleX,double fAngleY,double fAngleZ)
159 : {
160 1348 : if(!fTools::equalZero(fAngleX) || !fTools::equalZero(fAngleY) || !fTools::equalZero(fAngleZ))
161 : {
162 60 : if(!fTools::equalZero(fAngleX))
163 : {
164 60 : Impl3DHomMatrix aRotMatX;
165 60 : double fSin(sin(fAngleX));
166 60 : double fCos(cos(fAngleX));
167 :
168 60 : aRotMatX.set(1, 1, fCos);
169 60 : aRotMatX.set(2, 2, fCos);
170 60 : aRotMatX.set(2, 1, fSin);
171 60 : aRotMatX.set(1, 2, -fSin);
172 :
173 60 : mpImpl->doMulMatrix(aRotMatX);
174 : }
175 :
176 60 : if(!fTools::equalZero(fAngleY))
177 : {
178 58 : Impl3DHomMatrix aRotMatY;
179 58 : double fSin(sin(fAngleY));
180 58 : double fCos(cos(fAngleY));
181 :
182 58 : aRotMatY.set(0, 0, fCos);
183 58 : aRotMatY.set(2, 2, fCos);
184 58 : aRotMatY.set(0, 2, fSin);
185 58 : aRotMatY.set(2, 0, -fSin);
186 :
187 58 : mpImpl->doMulMatrix(aRotMatY);
188 : }
189 :
190 60 : if(!fTools::equalZero(fAngleZ))
191 : {
192 58 : Impl3DHomMatrix aRotMatZ;
193 58 : double fSin(sin(fAngleZ));
194 58 : double fCos(cos(fAngleZ));
195 :
196 58 : aRotMatZ.set(0, 0, fCos);
197 58 : aRotMatZ.set(1, 1, fCos);
198 58 : aRotMatZ.set(1, 0, fSin);
199 58 : aRotMatZ.set(0, 1, -fSin);
200 :
201 58 : mpImpl->doMulMatrix(aRotMatZ);
202 : }
203 : }
204 1348 : }
205 :
206 27960 : void B3DHomMatrix::translate(double fX, double fY, double fZ)
207 : {
208 27960 : if(!fTools::equalZero(fX) || !fTools::equalZero(fY) || !fTools::equalZero(fZ))
209 : {
210 21627 : Impl3DHomMatrix aTransMat;
211 :
212 21627 : aTransMat.set(0, 3, fX);
213 21627 : aTransMat.set(1, 3, fY);
214 21627 : aTransMat.set(2, 3, fZ);
215 :
216 21627 : mpImpl->doMulMatrix(aTransMat);
217 : }
218 27960 : }
219 :
220 8131 : void B3DHomMatrix::scale(double fX, double fY, double fZ)
221 : {
222 8131 : const double fOne(1.0);
223 :
224 8131 : if(!fTools::equal(fOne, fX) || !fTools::equal(fOne, fY) ||!fTools::equal(fOne, fZ))
225 : {
226 8131 : Impl3DHomMatrix aScaleMat;
227 :
228 8131 : aScaleMat.set(0, 0, fX);
229 8131 : aScaleMat.set(1, 1, fY);
230 8131 : aScaleMat.set(2, 2, fZ);
231 :
232 8131 : mpImpl->doMulMatrix(aScaleMat);
233 : }
234 8131 : }
235 :
236 6 : void B3DHomMatrix::shearXY(double fSx, double fSy)
237 : {
238 : // #i76239# do not test againt 1.0, but against 0.0. We are talking about a value not on the diagonal (!)
239 6 : if(!fTools::equalZero(fSx) || !fTools::equalZero(fSy))
240 : {
241 6 : Impl3DHomMatrix aShearXYMat;
242 :
243 6 : aShearXYMat.set(0, 2, fSx);
244 6 : aShearXYMat.set(1, 2, fSy);
245 :
246 6 : mpImpl->doMulMatrix(aShearXYMat);
247 : }
248 6 : }
249 :
250 0 : void B3DHomMatrix::shearXZ(double fSx, double fSz)
251 : {
252 : // #i76239# do not test againt 1.0, but against 0.0. We are talking about a value not on the diagonal (!)
253 0 : if(!fTools::equalZero(fSx) || !fTools::equalZero(fSz))
254 : {
255 0 : Impl3DHomMatrix aShearXZMat;
256 :
257 0 : aShearXZMat.set(0, 1, fSx);
258 0 : aShearXZMat.set(2, 1, fSz);
259 :
260 0 : mpImpl->doMulMatrix(aShearXZMat);
261 : }
262 0 : }
263 96 : void B3DHomMatrix::frustum(double fLeft, double fRight, double fBottom, double fTop, double fNear, double fFar)
264 : {
265 96 : const double fZero(0.0);
266 96 : const double fOne(1.0);
267 :
268 96 : if(!fTools::more(fNear, fZero))
269 : {
270 0 : fNear = 0.001;
271 : }
272 :
273 96 : if(!fTools::more(fFar, fZero))
274 : {
275 0 : fFar = fOne;
276 : }
277 :
278 96 : if(fTools::equal(fNear, fFar))
279 : {
280 0 : fFar = fNear + fOne;
281 : }
282 :
283 96 : if(fTools::equal(fLeft, fRight))
284 : {
285 8 : fLeft -= fOne;
286 8 : fRight += fOne;
287 : }
288 :
289 96 : if(fTools::equal(fTop, fBottom))
290 : {
291 0 : fBottom -= fOne;
292 0 : fTop += fOne;
293 : }
294 :
295 96 : Impl3DHomMatrix aFrustumMat;
296 :
297 96 : aFrustumMat.set(0, 0, 2.0 * fNear / (fRight - fLeft));
298 96 : aFrustumMat.set(1, 1, 2.0 * fNear / (fTop - fBottom));
299 96 : aFrustumMat.set(0, 2, (fRight + fLeft) / (fRight - fLeft));
300 96 : aFrustumMat.set(1, 2, (fTop + fBottom) / (fTop - fBottom));
301 96 : aFrustumMat.set(2, 2, -fOne * ((fFar + fNear) / (fFar - fNear)));
302 96 : aFrustumMat.set(3, 2, -fOne);
303 96 : aFrustumMat.set(2, 3, -fOne * ((2.0 * fFar * fNear) / (fFar - fNear)));
304 96 : aFrustumMat.set(3, 3, fZero);
305 :
306 96 : mpImpl->doMulMatrix(aFrustumMat);
307 96 : }
308 :
309 526 : void B3DHomMatrix::ortho(double fLeft, double fRight, double fBottom, double fTop, double fNear, double fFar)
310 : {
311 526 : if(fTools::equal(fNear, fFar))
312 : {
313 0 : fFar = fNear + 1.0;
314 : }
315 :
316 526 : if(fTools::equal(fLeft, fRight))
317 : {
318 0 : fLeft -= 1.0;
319 0 : fRight += 1.0;
320 : }
321 :
322 526 : if(fTools::equal(fTop, fBottom))
323 : {
324 0 : fBottom -= 1.0;
325 0 : fTop += 1.0;
326 : }
327 :
328 526 : Impl3DHomMatrix aOrthoMat;
329 :
330 526 : aOrthoMat.set(0, 0, 2.0 / (fRight - fLeft));
331 526 : aOrthoMat.set(1, 1, 2.0 / (fTop - fBottom));
332 526 : aOrthoMat.set(2, 2, -1.0 * (2.0 / (fFar - fNear)));
333 526 : aOrthoMat.set(0, 3, -1.0 * ((fRight + fLeft) / (fRight - fLeft)));
334 526 : aOrthoMat.set(1, 3, -1.0 * ((fTop + fBottom) / (fTop - fBottom)));
335 526 : aOrthoMat.set(2, 3, -1.0 * ((fFar + fNear) / (fFar - fNear)));
336 :
337 526 : mpImpl->doMulMatrix(aOrthoMat);
338 526 : }
339 :
340 311 : void B3DHomMatrix::orientation(B3DPoint aVRP, B3DVector aVPN, B3DVector aVUV)
341 : {
342 311 : Impl3DHomMatrix aOrientationMat;
343 :
344 : // translate -VRP
345 311 : aOrientationMat.set(0, 3, -aVRP.getX());
346 311 : aOrientationMat.set(1, 3, -aVRP.getY());
347 311 : aOrientationMat.set(2, 3, -aVRP.getZ());
348 :
349 : // build rotation
350 311 : aVUV.normalize();
351 311 : aVPN.normalize();
352 :
353 : // build x-axis as peroendicular fron aVUV and aVPN
354 622 : B3DVector aRx(aVUV.getPerpendicular(aVPN));
355 311 : aRx.normalize();
356 :
357 : // y-axis perpendicular to that
358 622 : B3DVector aRy(aVPN.getPerpendicular(aRx));
359 311 : aRy.normalize();
360 :
361 : // the calculated normals are the line vectors of the rotation matrix,
362 : // set them to create rotation
363 311 : aOrientationMat.set(0, 0, aRx.getX());
364 311 : aOrientationMat.set(0, 1, aRx.getY());
365 311 : aOrientationMat.set(0, 2, aRx.getZ());
366 311 : aOrientationMat.set(1, 0, aRy.getX());
367 311 : aOrientationMat.set(1, 1, aRy.getY());
368 311 : aOrientationMat.set(1, 2, aRy.getZ());
369 311 : aOrientationMat.set(2, 0, aVPN.getX());
370 311 : aOrientationMat.set(2, 1, aVPN.getY());
371 311 : aOrientationMat.set(2, 2, aVPN.getZ());
372 :
373 622 : mpImpl->doMulMatrix(aOrientationMat);
374 311 : }
375 :
376 699 : bool B3DHomMatrix::decompose(B3DTuple& rScale, B3DTuple& rTranslate, B3DTuple& rRotate, B3DTuple& rShear) const
377 : {
378 : // when perspective is used, decompose is not made here
379 699 : if(!mpImpl->isLastLineDefault())
380 0 : return false;
381 :
382 : // If determinant is zero, decomposition is not possible
383 699 : if(0.0 == determinant())
384 0 : return false;
385 :
386 : // isolate translation
387 699 : rTranslate.setX(mpImpl->get(0, 3));
388 699 : rTranslate.setY(mpImpl->get(1, 3));
389 699 : rTranslate.setZ(mpImpl->get(2, 3));
390 :
391 : // correct translate values
392 699 : rTranslate.correctValues();
393 :
394 : // get scale and shear
395 699 : B3DVector aCol0(mpImpl->get(0, 0), mpImpl->get(1, 0), mpImpl->get(2, 0));
396 1398 : B3DVector aCol1(mpImpl->get(0, 1), mpImpl->get(1, 1), mpImpl->get(2, 1));
397 1398 : B3DVector aCol2(mpImpl->get(0, 2), mpImpl->get(1, 2), mpImpl->get(2, 2));
398 1398 : B3DVector aTemp;
399 :
400 : // get ScaleX
401 699 : rScale.setX(aCol0.getLength());
402 699 : aCol0.normalize();
403 :
404 : // get ShearXY
405 699 : rShear.setX(aCol0.scalar(aCol1));
406 :
407 699 : if(fTools::equalZero(rShear.getX()))
408 : {
409 699 : rShear.setX(0.0);
410 : }
411 : else
412 : {
413 0 : aTemp.setX(aCol1.getX() - rShear.getX() * aCol0.getX());
414 0 : aTemp.setY(aCol1.getY() - rShear.getX() * aCol0.getY());
415 0 : aTemp.setZ(aCol1.getZ() - rShear.getX() * aCol0.getZ());
416 0 : aCol1 = aTemp;
417 : }
418 :
419 : // get ScaleY
420 699 : rScale.setY(aCol1.getLength());
421 699 : aCol1.normalize();
422 :
423 699 : const double fShearX(rShear.getX());
424 :
425 699 : if(!fTools::equalZero(fShearX))
426 : {
427 0 : rShear.setX(rShear.getX() / rScale.getY());
428 : }
429 :
430 : // get ShearXZ
431 699 : rShear.setY(aCol0.scalar(aCol2));
432 :
433 699 : if(fTools::equalZero(rShear.getY()))
434 : {
435 699 : rShear.setY(0.0);
436 : }
437 : else
438 : {
439 0 : aTemp.setX(aCol2.getX() - rShear.getY() * aCol0.getX());
440 0 : aTemp.setY(aCol2.getY() - rShear.getY() * aCol0.getY());
441 0 : aTemp.setZ(aCol2.getZ() - rShear.getY() * aCol0.getZ());
442 0 : aCol2 = aTemp;
443 : }
444 :
445 : // get ShearYZ
446 699 : rShear.setZ(aCol1.scalar(aCol2));
447 :
448 699 : if(fTools::equalZero(rShear.getZ()))
449 : {
450 699 : rShear.setZ(0.0);
451 : }
452 : else
453 : {
454 0 : aTemp.setX(aCol2.getX() - rShear.getZ() * aCol1.getX());
455 0 : aTemp.setY(aCol2.getY() - rShear.getZ() * aCol1.getY());
456 0 : aTemp.setZ(aCol2.getZ() - rShear.getZ() * aCol1.getZ());
457 0 : aCol2 = aTemp;
458 : }
459 :
460 : // get ScaleZ
461 699 : rScale.setZ(aCol2.getLength());
462 699 : aCol2.normalize();
463 :
464 699 : const double fShearY(rShear.getY());
465 :
466 699 : if(!fTools::equalZero(fShearY))
467 : {
468 0 : rShear.setY(rShear.getY() / rScale.getZ());
469 : }
470 :
471 699 : const double fShearZ(rShear.getZ());
472 :
473 699 : if(!fTools::equalZero(fShearZ))
474 : {
475 0 : rShear.setZ(rShear.getZ() / rScale.getZ());
476 : }
477 :
478 : // correct shear values
479 699 : rShear.correctValues();
480 :
481 : // Coordinate system flip?
482 699 : if(0.0 > aCol0.scalar(aCol1.getPerpendicular(aCol2)))
483 : {
484 531 : rScale = -rScale;
485 531 : aCol0 = -aCol0;
486 531 : aCol1 = -aCol1;
487 531 : aCol2 = -aCol2;
488 : }
489 :
490 : // correct scale values
491 699 : rScale.correctValues(1.0);
492 :
493 : // Get rotations
494 : {
495 699 : double fy=0;
496 699 : double cy=0;
497 :
498 2796 : if( ::basegfx::fTools::equal( aCol0.getZ(), 1.0 )
499 2796 : || aCol0.getZ() > 1.0 )
500 : {
501 0 : fy = -F_PI/2.0;
502 0 : cy = 0.0;
503 : }
504 2796 : else if( ::basegfx::fTools::equal( aCol0.getZ(), -1.0 )
505 2796 : || aCol0.getZ() < -1.0 )
506 : {
507 0 : fy = F_PI/2.0;
508 0 : cy = 0.0;
509 : }
510 : else
511 : {
512 699 : fy = asin( -aCol0.getZ() );
513 699 : cy = cos(fy);
514 : }
515 :
516 699 : rRotate.setY(fy);
517 699 : if( ::basegfx::fTools::equalZero( cy ) )
518 : {
519 0 : if( aCol0.getZ() > 0.0 )
520 0 : rRotate.setX(atan2(-1.0*aCol1.getX(), aCol1.getY()));
521 : else
522 0 : rRotate.setX(atan2(aCol1.getX(), aCol1.getY()));
523 0 : rRotate.setZ(0.0);
524 : }
525 : else
526 : {
527 699 : rRotate.setX(atan2(aCol1.getZ(), aCol2.getZ()));
528 699 : rRotate.setZ(atan2(aCol0.getY(), aCol0.getX()));
529 : }
530 :
531 : // corrcet rotate values
532 699 : rRotate.correctValues();
533 : }
534 :
535 1398 : return true;
536 : }
537 : } // end of namespace basegfx
538 :
539 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|