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 <drawinglayer/geometry/viewinformation3d.hxx>
21 : #include <basegfx/range/b3drange.hxx>
22 : #include <basegfx/matrix/b3dhommatrix.hxx>
23 : #include <com/sun/star/geometry/AffineMatrix3D.hpp>
24 : #include <com/sun/star/geometry/RealRectangle3D.hpp>
25 : #include <basegfx/tools/canvastools.hxx>
26 : #include <rtl/instance.hxx>
27 :
28 : //////////////////////////////////////////////////////////////////////////////
29 :
30 : using namespace com::sun::star;
31 :
32 : //////////////////////////////////////////////////////////////////////////////
33 :
34 : namespace drawinglayer
35 : {
36 : namespace geometry
37 : {
38 : /** Implementation class for ViewInformation3D
39 : */
40 13885 : class ImpViewInformation3D
41 : {
42 : private:
43 : // ViewInformation3D implementation can change refcount, so we have only
44 : // two memory regions for pairs of ViewInformation3D/ImpViewInformation3D
45 : friend class ::drawinglayer::geometry::ViewInformation3D;
46 :
47 : // the 3D transformations
48 : // Object to World. This may change and being adapted when entering 3D transformation
49 : // groups
50 : basegfx::B3DHomMatrix maObjectTransformation;
51 :
52 : // World to Camera. This includes VRP, VPN and VUV camera coordinate system
53 : basegfx::B3DHomMatrix maOrientation;
54 :
55 : // Camera to Device with X,Y and Z [-1.0 .. 1.0]. This is the
56 : // 3D to 2D projection which may be parallell or perspective. When it is perspective,
57 : // the last line of the homogen matrix will NOT be unused
58 : basegfx::B3DHomMatrix maProjection;
59 :
60 : // Device to View with X,Y and Z [0.0 .. 1.0]. This converts from -1 to 1 coordinates
61 : // in camera coordinate system to 0 to 1 in unit 2D coordinates. This way it stays
62 : // view-independent. To get discrete coordinates, the 2D transformation of a scene
63 : // as 2D object needs to be involved
64 : basegfx::B3DHomMatrix maDeviceToView;
65 :
66 : // Object to View is the linear combination of all four transformations. It's
67 : // buffered to avoid too much matrix multiplying and created on demand
68 : basegfx::B3DHomMatrix maObjectToView;
69 :
70 : // the point in time
71 : double mfViewTime;
72 :
73 : // the complete PropertyValue representation (if already created)
74 : uno::Sequence< beans::PropertyValue > mxViewInformation;
75 :
76 : // the extra PropertyValues; does not contain the transformations
77 : uno::Sequence< beans::PropertyValue > mxExtendedInformation;
78 :
79 : // the local UNO API strings
80 0 : const OUString& getNamePropertyObjectTransformation()
81 : {
82 0 : static OUString s_sNameProperty("ObjectTransformation");
83 0 : return s_sNameProperty;
84 : }
85 :
86 0 : const OUString& getNamePropertyOrientation()
87 : {
88 0 : static OUString s_sNameProperty("Orientation");
89 0 : return s_sNameProperty;
90 : }
91 :
92 0 : const OUString& getNamePropertyProjection()
93 : {
94 0 : static OUString s_sNameProperty("Projection");
95 0 : return s_sNameProperty;
96 : }
97 :
98 0 : const OUString& getNamePropertyProjection_30()
99 : {
100 0 : static OUString s_sNameProperty("Projection30");
101 0 : return s_sNameProperty;
102 : }
103 :
104 0 : const OUString& getNamePropertyProjection_31()
105 : {
106 0 : static OUString s_sNameProperty("Projection31");
107 0 : return s_sNameProperty;
108 : }
109 :
110 0 : const OUString& getNamePropertyProjection_32()
111 : {
112 0 : static OUString s_sNameProperty("Projection32");
113 0 : return s_sNameProperty;
114 : }
115 :
116 0 : const OUString& getNamePropertyProjection_33()
117 : {
118 0 : static OUString s_sNameProperty("Projection33");
119 0 : return s_sNameProperty;
120 : }
121 :
122 0 : const OUString& getNamePropertyDeviceToView()
123 : {
124 0 : static OUString s_sNameProperty("DeviceToView");
125 0 : return s_sNameProperty;
126 : }
127 :
128 0 : const OUString& getNamePropertyTime()
129 : {
130 0 : static OUString s_sNamePropertyTime("Time");
131 0 : return s_sNamePropertyTime;
132 : }
133 :
134 : // a central PropertyValue parsing method to allow transportatin of
135 : // all ViewParameters using UNO API
136 4628 : void impInterpretPropertyValues(const uno::Sequence< beans::PropertyValue >& rViewParameters)
137 : {
138 4628 : if(rViewParameters.hasElements())
139 : {
140 0 : const sal_Int32 nCount(rViewParameters.getLength());
141 0 : sal_Int32 nExtendedInsert(0);
142 :
143 : // prepare extended information for filtering. Maximum size is nCount
144 0 : mxExtendedInformation.realloc(nCount);
145 :
146 0 : for(sal_Int32 a(0); a < nCount; a++)
147 : {
148 0 : const beans::PropertyValue& rProp = rViewParameters[a];
149 :
150 0 : if(rProp.Name == getNamePropertyObjectTransformation())
151 : {
152 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
153 0 : rProp.Value >>= aAffineMatrix3D;
154 0 : maObjectTransformation = basegfx::unotools::homMatrixFromAffineMatrix3D(aAffineMatrix3D);
155 : }
156 0 : else if(rProp.Name == getNamePropertyOrientation())
157 : {
158 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
159 0 : rProp.Value >>= aAffineMatrix3D;
160 0 : maOrientation = basegfx::unotools::homMatrixFromAffineMatrix3D(aAffineMatrix3D);
161 : }
162 0 : else if(rProp.Name == getNamePropertyProjection())
163 : {
164 : // projection may be defined using a frustum in which case the last line of
165 : // the 4x4 matrix is not (0,0,0,1). Since AffineMatrix3D does not support that,
166 : // these four values need to be treated extra
167 0 : const double f_30(maProjection.get(3, 0));
168 0 : const double f_31(maProjection.get(3, 1));
169 0 : const double f_32(maProjection.get(3, 2));
170 0 : const double f_33(maProjection.get(3, 3));
171 :
172 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
173 0 : rProp.Value >>= aAffineMatrix3D;
174 0 : maProjection = basegfx::unotools::homMatrixFromAffineMatrix3D(aAffineMatrix3D);
175 :
176 0 : maProjection.set(3, 0, f_30);
177 0 : maProjection.set(3, 1, f_31);
178 0 : maProjection.set(3, 2, f_32);
179 0 : maProjection.set(3, 3, f_33);
180 : }
181 0 : else if(rProp.Name == getNamePropertyProjection_30())
182 : {
183 0 : double f_30(0.0);
184 0 : rProp.Value >>= f_30;
185 0 : maProjection.set(3, 0, f_30);
186 : }
187 0 : else if(rProp.Name == getNamePropertyProjection_31())
188 : {
189 0 : double f_31(0.0);
190 0 : rProp.Value >>= f_31;
191 0 : maProjection.set(3, 1, f_31);
192 : }
193 0 : else if(rProp.Name == getNamePropertyProjection_32())
194 : {
195 0 : double f_32(0.0);
196 0 : rProp.Value >>= f_32;
197 0 : maProjection.set(3, 2, f_32);
198 : }
199 0 : else if(rProp.Name == getNamePropertyProjection_33())
200 : {
201 0 : double f_33(1.0);
202 0 : rProp.Value >>= f_33;
203 0 : maProjection.set(3, 3, f_33);
204 : }
205 0 : else if(rProp.Name == getNamePropertyDeviceToView())
206 : {
207 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
208 0 : rProp.Value >>= aAffineMatrix3D;
209 0 : maDeviceToView = basegfx::unotools::homMatrixFromAffineMatrix3D(aAffineMatrix3D);
210 : }
211 0 : else if(rProp.Name == getNamePropertyTime())
212 : {
213 0 : rProp.Value >>= mfViewTime;
214 : }
215 : else
216 : {
217 : // extra information; add to filtered information
218 0 : mxExtendedInformation[nExtendedInsert++] = rProp;
219 : }
220 : }
221 :
222 : // extra information size is now known; realloc to final size
223 0 : mxExtendedInformation.realloc(nExtendedInsert);
224 : }
225 4628 : }
226 :
227 : // central method to create a Sequence of PropertyValues containing he complete
228 : // data set
229 0 : void impFillViewInformationFromContent()
230 : {
231 0 : uno::Sequence< beans::PropertyValue > xRetval;
232 0 : const bool bObjectTransformationUsed(!maObjectTransformation.isIdentity());
233 0 : const bool bOrientationUsed(!maOrientation.isIdentity());
234 0 : const bool bProjectionUsed(!maProjection.isIdentity());
235 0 : const bool bDeviceToViewUsed(!maDeviceToView.isIdentity());
236 0 : const bool bTimeUsed(0.0 < mfViewTime);
237 0 : const bool bExtraInformation(mxExtendedInformation.hasElements());
238 :
239 : // projection may be defined using a frustum in which case the last line of
240 : // the 4x4 matrix is not (0,0,0,1). Since AffineMatrix3D does not support that,
241 : // these four values need to be treated extra
242 0 : const bool bProjectionUsed_30(bProjectionUsed && !basegfx::fTools::equalZero(maProjection.get(3, 0)));
243 0 : const bool bProjectionUsed_31(bProjectionUsed && !basegfx::fTools::equalZero(maProjection.get(3, 1)));
244 0 : const bool bProjectionUsed_32(bProjectionUsed && !basegfx::fTools::equalZero(maProjection.get(3, 2)));
245 0 : const bool bProjectionUsed_33(bProjectionUsed && !basegfx::fTools::equal(maProjection.get(3, 3), 1.0));
246 :
247 0 : sal_uInt32 nIndex(0);
248 : const sal_uInt32 nCount(
249 0 : (bObjectTransformationUsed ? 1 : 0) +
250 0 : (bOrientationUsed ? 1 : 0) +
251 0 : (bProjectionUsed ? 1 : 0) +
252 0 : (bProjectionUsed_30 ? 1 : 0) +
253 0 : (bProjectionUsed_31 ? 1 : 0) +
254 0 : (bProjectionUsed_32 ? 1 : 0) +
255 0 : (bProjectionUsed_33 ? 1 : 0) +
256 0 : (bDeviceToViewUsed ? 1 : 0) +
257 0 : (bTimeUsed ? 1 : 0) +
258 0 : (bExtraInformation ? mxExtendedInformation.getLength() : 0));
259 :
260 0 : mxViewInformation.realloc(nCount);
261 :
262 0 : if(bObjectTransformationUsed)
263 : {
264 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
265 0 : basegfx::unotools::affineMatrixFromHomMatrix3D(aAffineMatrix3D, maObjectTransformation);
266 0 : mxViewInformation[nIndex].Name = getNamePropertyObjectTransformation();
267 0 : mxViewInformation[nIndex].Value <<= aAffineMatrix3D;
268 0 : nIndex++;
269 : }
270 :
271 0 : if(bOrientationUsed)
272 : {
273 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
274 0 : basegfx::unotools::affineMatrixFromHomMatrix3D(aAffineMatrix3D, maOrientation);
275 0 : mxViewInformation[nIndex].Name = getNamePropertyOrientation();
276 0 : mxViewInformation[nIndex].Value <<= aAffineMatrix3D;
277 0 : nIndex++;
278 : }
279 :
280 0 : if(bProjectionUsed)
281 : {
282 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
283 0 : basegfx::unotools::affineMatrixFromHomMatrix3D(aAffineMatrix3D, maProjection);
284 0 : mxViewInformation[nIndex].Name = getNamePropertyProjection();
285 0 : mxViewInformation[nIndex].Value <<= aAffineMatrix3D;
286 0 : nIndex++;
287 : }
288 :
289 0 : if(bProjectionUsed_30)
290 : {
291 0 : mxViewInformation[nIndex].Name = getNamePropertyProjection_30();
292 0 : mxViewInformation[nIndex].Value <<= maProjection.get(3, 0);
293 0 : nIndex++;
294 : }
295 :
296 0 : if(bProjectionUsed_31)
297 : {
298 0 : mxViewInformation[nIndex].Name = getNamePropertyProjection_31();
299 0 : mxViewInformation[nIndex].Value <<= maProjection.get(3, 1);
300 0 : nIndex++;
301 : }
302 :
303 0 : if(bProjectionUsed_32)
304 : {
305 0 : mxViewInformation[nIndex].Name = getNamePropertyProjection_32();
306 0 : mxViewInformation[nIndex].Value <<= maProjection.get(3, 2);
307 0 : nIndex++;
308 : }
309 :
310 0 : if(bProjectionUsed_33)
311 : {
312 0 : mxViewInformation[nIndex].Name = getNamePropertyProjection_33();
313 0 : mxViewInformation[nIndex].Value <<= maProjection.get(3, 3);
314 0 : nIndex++;
315 : }
316 :
317 0 : if(bDeviceToViewUsed)
318 : {
319 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
320 0 : basegfx::unotools::affineMatrixFromHomMatrix3D(aAffineMatrix3D, maDeviceToView);
321 0 : mxViewInformation[nIndex].Name = getNamePropertyDeviceToView();
322 0 : mxViewInformation[nIndex].Value <<= aAffineMatrix3D;
323 0 : nIndex++;
324 : }
325 :
326 0 : if(bTimeUsed)
327 : {
328 0 : mxViewInformation[nIndex].Name = getNamePropertyTime();
329 0 : mxViewInformation[nIndex].Value <<= mfViewTime;
330 0 : nIndex++;
331 : }
332 :
333 0 : if(bExtraInformation)
334 : {
335 0 : const sal_Int32 nExtra(mxExtendedInformation.getLength());
336 :
337 0 : for(sal_Int32 a(0); a < nExtra; a++)
338 : {
339 0 : mxViewInformation[nIndex++] = mxExtendedInformation[a];
340 : }
341 0 : }
342 0 : }
343 :
344 : public:
345 2356 : ImpViewInformation3D(
346 : const basegfx::B3DHomMatrix& rObjectTransformation,
347 : const basegfx::B3DHomMatrix& rOrientation,
348 : const basegfx::B3DHomMatrix& rProjection,
349 : const basegfx::B3DHomMatrix& rDeviceToView,
350 : double fViewTime,
351 : const uno::Sequence< beans::PropertyValue >& rExtendedParameters)
352 : : maObjectTransformation(rObjectTransformation),
353 : maOrientation(rOrientation),
354 : maProjection(rProjection),
355 : maDeviceToView(rDeviceToView),
356 : mfViewTime(fViewTime),
357 : mxViewInformation(),
358 2356 : mxExtendedInformation()
359 : {
360 2356 : impInterpretPropertyValues(rExtendedParameters);
361 2356 : }
362 :
363 2272 : explicit ImpViewInformation3D(const uno::Sequence< beans::PropertyValue >& rViewParameters)
364 : : maObjectTransformation(),
365 : maOrientation(),
366 : maProjection(),
367 : maDeviceToView(),
368 : mfViewTime(),
369 : mxViewInformation(rViewParameters),
370 2272 : mxExtendedInformation()
371 : {
372 2272 : impInterpretPropertyValues(rViewParameters);
373 2272 : }
374 :
375 1 : ImpViewInformation3D()
376 : : maObjectTransformation(),
377 : maOrientation(),
378 : maProjection(),
379 : maDeviceToView(),
380 : mfViewTime(),
381 : mxViewInformation(),
382 1 : mxExtendedInformation()
383 : {
384 1 : }
385 :
386 2421 : const basegfx::B3DHomMatrix& getObjectTransformation() const { return maObjectTransformation; }
387 2387 : const basegfx::B3DHomMatrix& getOrientation() const { return maOrientation; }
388 2211 : const basegfx::B3DHomMatrix& getProjection() const { return maProjection; }
389 2206 : const basegfx::B3DHomMatrix& getDeviceToView() const { return maDeviceToView; }
390 2045 : double getViewTime() const { return mfViewTime; }
391 :
392 2221 : const basegfx::B3DHomMatrix& getObjectToView() const
393 : {
394 : // on demand WorldToView creation
395 :
396 2221 : if(maObjectToView.isIdentity())
397 : {
398 1983 : const_cast< ImpViewInformation3D* >(this)->maObjectToView = maDeviceToView * maProjection * maOrientation * maObjectTransformation;
399 : }
400 :
401 2221 : return maObjectToView;
402 : }
403 :
404 0 : const uno::Sequence< beans::PropertyValue >& getViewInformationSequence() const
405 : {
406 0 : if(!mxViewInformation.hasElements())
407 : {
408 0 : const_cast< ImpViewInformation3D* >(this)->impFillViewInformationFromContent();
409 : }
410 :
411 0 : return mxViewInformation;
412 : }
413 :
414 2045 : const uno::Sequence< beans::PropertyValue >& getExtendedInformationSequence() const
415 : {
416 2045 : return mxExtendedInformation;
417 : }
418 :
419 3 : bool operator==(const ImpViewInformation3D& rCandidate) const
420 : {
421 3 : return (maObjectTransformation == rCandidate.maObjectTransformation
422 3 : && maOrientation == rCandidate.maOrientation
423 3 : && maProjection == rCandidate.maProjection
424 3 : && maDeviceToView == rCandidate.maDeviceToView
425 3 : && mfViewTime == rCandidate.mfViewTime
426 6 : && mxExtendedInformation == rCandidate.mxExtendedInformation);
427 : }
428 : };
429 : } // end of anonymous namespace
430 : } // end of namespace drawinglayer
431 :
432 : //////////////////////////////////////////////////////////////////////////////
433 :
434 : namespace drawinglayer
435 : {
436 : namespace geometry
437 : {
438 : namespace
439 : {
440 : struct theGlobalDefault :
441 : public rtl::Static< ViewInformation3D::ImplType, theGlobalDefault > {};
442 : }
443 :
444 2356 : ViewInformation3D::ViewInformation3D(
445 : const basegfx::B3DHomMatrix& rObjectObjectTransformation,
446 : const basegfx::B3DHomMatrix& rOrientation,
447 : const basegfx::B3DHomMatrix& rProjection,
448 : const basegfx::B3DHomMatrix& rDeviceToView,
449 : double fViewTime,
450 : const uno::Sequence< beans::PropertyValue >& rExtendedParameters)
451 : : mpViewInformation3D(ImpViewInformation3D(
452 : rObjectObjectTransformation, rOrientation, rProjection,
453 2356 : rDeviceToView, fViewTime, rExtendedParameters))
454 : {
455 2356 : }
456 :
457 2272 : ViewInformation3D::ViewInformation3D(const uno::Sequence< beans::PropertyValue >& rViewParameters)
458 2272 : : mpViewInformation3D(ImpViewInformation3D(rViewParameters))
459 : {
460 2272 : }
461 :
462 2171 : ViewInformation3D::ViewInformation3D()
463 2171 : : mpViewInformation3D(theGlobalDefault::get())
464 : {
465 2171 : }
466 :
467 2101 : ViewInformation3D::ViewInformation3D(const ViewInformation3D& rCandidate)
468 2101 : : mpViewInformation3D(rCandidate.mpViewInformation3D)
469 : {
470 2101 : }
471 :
472 8900 : ViewInformation3D::~ViewInformation3D()
473 : {
474 8900 : }
475 :
476 1954 : bool ViewInformation3D::isDefault() const
477 : {
478 1954 : return mpViewInformation3D.same_object(theGlobalDefault::get());
479 : }
480 :
481 4291 : ViewInformation3D& ViewInformation3D::operator=(const ViewInformation3D& rCandidate)
482 : {
483 4291 : mpViewInformation3D = rCandidate.mpViewInformation3D;
484 4291 : return *this;
485 : }
486 :
487 29 : bool ViewInformation3D::operator==(const ViewInformation3D& rCandidate) const
488 : {
489 29 : return rCandidate.mpViewInformation3D == mpViewInformation3D;
490 : }
491 :
492 2421 : const basegfx::B3DHomMatrix& ViewInformation3D::getObjectTransformation() const
493 : {
494 2421 : return mpViewInformation3D->getObjectTransformation();
495 : }
496 :
497 2387 : const basegfx::B3DHomMatrix& ViewInformation3D::getOrientation() const
498 : {
499 2387 : return mpViewInformation3D->getOrientation();
500 : }
501 :
502 2211 : const basegfx::B3DHomMatrix& ViewInformation3D::getProjection() const
503 : {
504 2211 : return mpViewInformation3D->getProjection();
505 : }
506 :
507 2206 : const basegfx::B3DHomMatrix& ViewInformation3D::getDeviceToView() const
508 : {
509 2206 : return mpViewInformation3D->getDeviceToView();
510 : }
511 :
512 2221 : const basegfx::B3DHomMatrix& ViewInformation3D::getObjectToView() const
513 : {
514 2221 : return mpViewInformation3D->getObjectToView();
515 : }
516 :
517 2045 : double ViewInformation3D::getViewTime() const
518 : {
519 2045 : return mpViewInformation3D->getViewTime();
520 : }
521 :
522 0 : const uno::Sequence< beans::PropertyValue >& ViewInformation3D::getViewInformationSequence() const
523 : {
524 0 : return mpViewInformation3D->getViewInformationSequence();
525 : }
526 :
527 2045 : const uno::Sequence< beans::PropertyValue >& ViewInformation3D::getExtendedInformationSequence() const
528 : {
529 2045 : return mpViewInformation3D->getExtendedInformationSequence();
530 : }
531 : } // end of namespace geometry
532 : } // end of namespace drawinglayer
533 :
534 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|