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 0 : 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 0 : void impInterpretPropertyValues(const uno::Sequence< beans::PropertyValue >& rViewParameters)
137 : {
138 0 : 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 0 : }
226 :
227 : // central method to create a Sequence of PropertyValues containing he complete
228 : // data set
229 0 : void impFillViewInformationFromContent()
230 : {
231 0 : const bool bObjectTransformationUsed(!maObjectTransformation.isIdentity());
232 0 : const bool bOrientationUsed(!maOrientation.isIdentity());
233 0 : const bool bProjectionUsed(!maProjection.isIdentity());
234 0 : const bool bDeviceToViewUsed(!maDeviceToView.isIdentity());
235 0 : const bool bTimeUsed(0.0 < mfViewTime);
236 0 : const bool bExtraInformation(mxExtendedInformation.hasElements());
237 :
238 : // projection may be defined using a frustum in which case the last line of
239 : // the 4x4 matrix is not (0,0,0,1). Since AffineMatrix3D does not support that,
240 : // these four values need to be treated extra
241 0 : const bool bProjectionUsed_30(bProjectionUsed && !basegfx::fTools::equalZero(maProjection.get(3, 0)));
242 0 : const bool bProjectionUsed_31(bProjectionUsed && !basegfx::fTools::equalZero(maProjection.get(3, 1)));
243 0 : const bool bProjectionUsed_32(bProjectionUsed && !basegfx::fTools::equalZero(maProjection.get(3, 2)));
244 0 : const bool bProjectionUsed_33(bProjectionUsed && !basegfx::fTools::equal(maProjection.get(3, 3), 1.0));
245 :
246 0 : sal_uInt32 nIndex(0);
247 : const sal_uInt32 nCount(
248 0 : (bObjectTransformationUsed ? 1 : 0) +
249 0 : (bOrientationUsed ? 1 : 0) +
250 0 : (bProjectionUsed ? 1 : 0) +
251 0 : (bProjectionUsed_30 ? 1 : 0) +
252 0 : (bProjectionUsed_31 ? 1 : 0) +
253 0 : (bProjectionUsed_32 ? 1 : 0) +
254 0 : (bProjectionUsed_33 ? 1 : 0) +
255 0 : (bDeviceToViewUsed ? 1 : 0) +
256 0 : (bTimeUsed ? 1 : 0) +
257 0 : (bExtraInformation ? mxExtendedInformation.getLength() : 0));
258 :
259 0 : mxViewInformation.realloc(nCount);
260 :
261 0 : if(bObjectTransformationUsed)
262 : {
263 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
264 0 : basegfx::unotools::affineMatrixFromHomMatrix3D(aAffineMatrix3D, maObjectTransformation);
265 0 : mxViewInformation[nIndex].Name = getNamePropertyObjectTransformation();
266 0 : mxViewInformation[nIndex].Value <<= aAffineMatrix3D;
267 0 : nIndex++;
268 : }
269 :
270 0 : if(bOrientationUsed)
271 : {
272 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
273 0 : basegfx::unotools::affineMatrixFromHomMatrix3D(aAffineMatrix3D, maOrientation);
274 0 : mxViewInformation[nIndex].Name = getNamePropertyOrientation();
275 0 : mxViewInformation[nIndex].Value <<= aAffineMatrix3D;
276 0 : nIndex++;
277 : }
278 :
279 0 : if(bProjectionUsed)
280 : {
281 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
282 0 : basegfx::unotools::affineMatrixFromHomMatrix3D(aAffineMatrix3D, maProjection);
283 0 : mxViewInformation[nIndex].Name = getNamePropertyProjection();
284 0 : mxViewInformation[nIndex].Value <<= aAffineMatrix3D;
285 0 : nIndex++;
286 : }
287 :
288 0 : if(bProjectionUsed_30)
289 : {
290 0 : mxViewInformation[nIndex].Name = getNamePropertyProjection_30();
291 0 : mxViewInformation[nIndex].Value <<= maProjection.get(3, 0);
292 0 : nIndex++;
293 : }
294 :
295 0 : if(bProjectionUsed_31)
296 : {
297 0 : mxViewInformation[nIndex].Name = getNamePropertyProjection_31();
298 0 : mxViewInformation[nIndex].Value <<= maProjection.get(3, 1);
299 0 : nIndex++;
300 : }
301 :
302 0 : if(bProjectionUsed_32)
303 : {
304 0 : mxViewInformation[nIndex].Name = getNamePropertyProjection_32();
305 0 : mxViewInformation[nIndex].Value <<= maProjection.get(3, 2);
306 0 : nIndex++;
307 : }
308 :
309 0 : if(bProjectionUsed_33)
310 : {
311 0 : mxViewInformation[nIndex].Name = getNamePropertyProjection_33();
312 0 : mxViewInformation[nIndex].Value <<= maProjection.get(3, 3);
313 0 : nIndex++;
314 : }
315 :
316 0 : if(bDeviceToViewUsed)
317 : {
318 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
319 0 : basegfx::unotools::affineMatrixFromHomMatrix3D(aAffineMatrix3D, maDeviceToView);
320 0 : mxViewInformation[nIndex].Name = getNamePropertyDeviceToView();
321 0 : mxViewInformation[nIndex].Value <<= aAffineMatrix3D;
322 0 : nIndex++;
323 : }
324 :
325 0 : if(bTimeUsed)
326 : {
327 0 : mxViewInformation[nIndex].Name = getNamePropertyTime();
328 0 : mxViewInformation[nIndex].Value <<= mfViewTime;
329 0 : nIndex++;
330 : }
331 :
332 0 : if(bExtraInformation)
333 : {
334 0 : const sal_Int32 nExtra(mxExtendedInformation.getLength());
335 :
336 0 : for(sal_Int32 a(0); a < nExtra; a++)
337 : {
338 0 : mxViewInformation[nIndex++] = mxExtendedInformation[a];
339 : }
340 : }
341 0 : }
342 :
343 : public:
344 0 : ImpViewInformation3D(
345 : const basegfx::B3DHomMatrix& rObjectTransformation,
346 : const basegfx::B3DHomMatrix& rOrientation,
347 : const basegfx::B3DHomMatrix& rProjection,
348 : const basegfx::B3DHomMatrix& rDeviceToView,
349 : double fViewTime,
350 : const uno::Sequence< beans::PropertyValue >& rExtendedParameters)
351 : : maObjectTransformation(rObjectTransformation),
352 : maOrientation(rOrientation),
353 : maProjection(rProjection),
354 : maDeviceToView(rDeviceToView),
355 : mfViewTime(fViewTime),
356 : mxViewInformation(),
357 0 : mxExtendedInformation()
358 : {
359 0 : impInterpretPropertyValues(rExtendedParameters);
360 0 : }
361 :
362 0 : explicit ImpViewInformation3D(const uno::Sequence< beans::PropertyValue >& rViewParameters)
363 : : maObjectTransformation(),
364 : maOrientation(),
365 : maProjection(),
366 : maDeviceToView(),
367 : mfViewTime(),
368 : mxViewInformation(rViewParameters),
369 0 : mxExtendedInformation()
370 : {
371 0 : impInterpretPropertyValues(rViewParameters);
372 0 : }
373 :
374 0 : ImpViewInformation3D()
375 : : maObjectTransformation(),
376 : maOrientation(),
377 : maProjection(),
378 : maDeviceToView(),
379 : mfViewTime(),
380 : mxViewInformation(),
381 0 : mxExtendedInformation()
382 : {
383 0 : }
384 :
385 0 : const basegfx::B3DHomMatrix& getObjectTransformation() const { return maObjectTransformation; }
386 0 : const basegfx::B3DHomMatrix& getOrientation() const { return maOrientation; }
387 0 : const basegfx::B3DHomMatrix& getProjection() const { return maProjection; }
388 0 : const basegfx::B3DHomMatrix& getDeviceToView() const { return maDeviceToView; }
389 0 : double getViewTime() const { return mfViewTime; }
390 :
391 0 : const basegfx::B3DHomMatrix& getObjectToView() const
392 : {
393 : // on demand WorldToView creation
394 :
395 0 : if(maObjectToView.isIdentity())
396 : {
397 0 : const_cast< ImpViewInformation3D* >(this)->maObjectToView = maDeviceToView * maProjection * maOrientation * maObjectTransformation;
398 : }
399 :
400 0 : return maObjectToView;
401 : }
402 :
403 0 : const uno::Sequence< beans::PropertyValue >& getViewInformationSequence() const
404 : {
405 0 : if(!mxViewInformation.hasElements())
406 : {
407 0 : const_cast< ImpViewInformation3D* >(this)->impFillViewInformationFromContent();
408 : }
409 :
410 0 : return mxViewInformation;
411 : }
412 :
413 0 : const uno::Sequence< beans::PropertyValue >& getExtendedInformationSequence() const
414 : {
415 0 : return mxExtendedInformation;
416 : }
417 :
418 0 : bool operator==(const ImpViewInformation3D& rCandidate) const
419 : {
420 0 : return (maObjectTransformation == rCandidate.maObjectTransformation
421 0 : && maOrientation == rCandidate.maOrientation
422 0 : && maProjection == rCandidate.maProjection
423 0 : && maDeviceToView == rCandidate.maDeviceToView
424 0 : && mfViewTime == rCandidate.mfViewTime
425 0 : && mxExtendedInformation == rCandidate.mxExtendedInformation);
426 : }
427 : };
428 : } // end of anonymous namespace
429 : } // end of namespace drawinglayer
430 :
431 :
432 :
433 : namespace drawinglayer
434 : {
435 : namespace geometry
436 : {
437 : namespace
438 : {
439 : struct theGlobalDefault :
440 : public rtl::Static< ViewInformation3D::ImplType, theGlobalDefault > {};
441 : }
442 :
443 0 : ViewInformation3D::ViewInformation3D(
444 : const basegfx::B3DHomMatrix& rObjectObjectTransformation,
445 : const basegfx::B3DHomMatrix& rOrientation,
446 : const basegfx::B3DHomMatrix& rProjection,
447 : const basegfx::B3DHomMatrix& rDeviceToView,
448 : double fViewTime,
449 : const uno::Sequence< beans::PropertyValue >& rExtendedParameters)
450 : : mpViewInformation3D(ImpViewInformation3D(
451 : rObjectObjectTransformation, rOrientation, rProjection,
452 0 : rDeviceToView, fViewTime, rExtendedParameters))
453 : {
454 0 : }
455 :
456 0 : ViewInformation3D::ViewInformation3D(const uno::Sequence< beans::PropertyValue >& rViewParameters)
457 0 : : mpViewInformation3D(ImpViewInformation3D(rViewParameters))
458 : {
459 0 : }
460 :
461 0 : ViewInformation3D::ViewInformation3D()
462 0 : : mpViewInformation3D(theGlobalDefault::get())
463 : {
464 0 : }
465 :
466 0 : ViewInformation3D::ViewInformation3D(const ViewInformation3D& rCandidate)
467 0 : : mpViewInformation3D(rCandidate.mpViewInformation3D)
468 : {
469 0 : }
470 :
471 0 : ViewInformation3D::~ViewInformation3D()
472 : {
473 0 : }
474 :
475 0 : bool ViewInformation3D::isDefault() const
476 : {
477 0 : return mpViewInformation3D.same_object(theGlobalDefault::get());
478 : }
479 :
480 0 : ViewInformation3D& ViewInformation3D::operator=(const ViewInformation3D& rCandidate)
481 : {
482 0 : mpViewInformation3D = rCandidate.mpViewInformation3D;
483 0 : return *this;
484 : }
485 :
486 0 : bool ViewInformation3D::operator==(const ViewInformation3D& rCandidate) const
487 : {
488 0 : return rCandidate.mpViewInformation3D == mpViewInformation3D;
489 : }
490 :
491 0 : const basegfx::B3DHomMatrix& ViewInformation3D::getObjectTransformation() const
492 : {
493 0 : return mpViewInformation3D->getObjectTransformation();
494 : }
495 :
496 0 : const basegfx::B3DHomMatrix& ViewInformation3D::getOrientation() const
497 : {
498 0 : return mpViewInformation3D->getOrientation();
499 : }
500 :
501 0 : const basegfx::B3DHomMatrix& ViewInformation3D::getProjection() const
502 : {
503 0 : return mpViewInformation3D->getProjection();
504 : }
505 :
506 0 : const basegfx::B3DHomMatrix& ViewInformation3D::getDeviceToView() const
507 : {
508 0 : return mpViewInformation3D->getDeviceToView();
509 : }
510 :
511 0 : const basegfx::B3DHomMatrix& ViewInformation3D::getObjectToView() const
512 : {
513 0 : return mpViewInformation3D->getObjectToView();
514 : }
515 :
516 0 : double ViewInformation3D::getViewTime() const
517 : {
518 0 : return mpViewInformation3D->getViewTime();
519 : }
520 :
521 0 : const uno::Sequence< beans::PropertyValue >& ViewInformation3D::getViewInformationSequence() const
522 : {
523 0 : return mpViewInformation3D->getViewInformationSequence();
524 : }
525 :
526 0 : const uno::Sequence< beans::PropertyValue >& ViewInformation3D::getExtendedInformationSequence() const
527 : {
528 0 : return mpViewInformation3D->getExtendedInformationSequence();
529 : }
530 : } // end of namespace geometry
531 : } // end of namespace drawinglayer
532 :
533 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|