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 63368 : 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 : static OUString getNamePropertyObjectTransformation()
81 : {
82 0 : return OUString("ObjectTransformation");
83 : }
84 :
85 0 : static OUString getNamePropertyOrientation()
86 : {
87 0 : return OUString("Orientation");
88 : }
89 :
90 0 : static OUString getNamePropertyProjection()
91 : {
92 0 : return OUString("Projection");
93 : }
94 :
95 0 : static OUString getNamePropertyProjection_30()
96 : {
97 0 : return OUString("Projection30");
98 : }
99 :
100 0 : static OUString getNamePropertyProjection_31()
101 : {
102 0 : return OUString("Projection31");
103 : }
104 :
105 0 : static OUString getNamePropertyProjection_32()
106 : {
107 0 : return OUString("Projection32");
108 : }
109 :
110 0 : static OUString getNamePropertyProjection_33()
111 : {
112 0 : return OUString("Projection33");
113 : }
114 :
115 0 : static OUString getNamePropertyDeviceToView()
116 : {
117 0 : return OUString("DeviceToView");
118 : }
119 :
120 0 : static OUString getNamePropertyTime()
121 : {
122 0 : return OUString("Time");
123 : }
124 :
125 : // a central PropertyValue parsing method to allow transportatin of
126 : // all ViewParameters using UNO API
127 21121 : void impInterpretPropertyValues(const uno::Sequence< beans::PropertyValue >& rViewParameters)
128 : {
129 21121 : if(rViewParameters.hasElements())
130 : {
131 0 : const sal_Int32 nCount(rViewParameters.getLength());
132 0 : sal_Int32 nExtendedInsert(0);
133 :
134 : // prepare extended information for filtering. Maximum size is nCount
135 0 : mxExtendedInformation.realloc(nCount);
136 :
137 0 : for(sal_Int32 a(0); a < nCount; a++)
138 : {
139 0 : const beans::PropertyValue& rProp = rViewParameters[a];
140 :
141 0 : if(rProp.Name == getNamePropertyObjectTransformation())
142 : {
143 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
144 0 : rProp.Value >>= aAffineMatrix3D;
145 0 : maObjectTransformation = basegfx::unotools::homMatrixFromAffineMatrix3D(aAffineMatrix3D);
146 : }
147 0 : else if(rProp.Name == getNamePropertyOrientation())
148 : {
149 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
150 0 : rProp.Value >>= aAffineMatrix3D;
151 0 : maOrientation = basegfx::unotools::homMatrixFromAffineMatrix3D(aAffineMatrix3D);
152 : }
153 0 : else if(rProp.Name == getNamePropertyProjection())
154 : {
155 : // projection may be defined using a frustum in which case the last line of
156 : // the 4x4 matrix is not (0,0,0,1). Since AffineMatrix3D does not support that,
157 : // these four values need to be treated extra
158 0 : const double f_30(maProjection.get(3, 0));
159 0 : const double f_31(maProjection.get(3, 1));
160 0 : const double f_32(maProjection.get(3, 2));
161 0 : const double f_33(maProjection.get(3, 3));
162 :
163 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
164 0 : rProp.Value >>= aAffineMatrix3D;
165 0 : maProjection = basegfx::unotools::homMatrixFromAffineMatrix3D(aAffineMatrix3D);
166 :
167 0 : maProjection.set(3, 0, f_30);
168 0 : maProjection.set(3, 1, f_31);
169 0 : maProjection.set(3, 2, f_32);
170 0 : maProjection.set(3, 3, f_33);
171 : }
172 0 : else if(rProp.Name == getNamePropertyProjection_30())
173 : {
174 0 : double f_30(0.0);
175 0 : rProp.Value >>= f_30;
176 0 : maProjection.set(3, 0, f_30);
177 : }
178 0 : else if(rProp.Name == getNamePropertyProjection_31())
179 : {
180 0 : double f_31(0.0);
181 0 : rProp.Value >>= f_31;
182 0 : maProjection.set(3, 1, f_31);
183 : }
184 0 : else if(rProp.Name == getNamePropertyProjection_32())
185 : {
186 0 : double f_32(0.0);
187 0 : rProp.Value >>= f_32;
188 0 : maProjection.set(3, 2, f_32);
189 : }
190 0 : else if(rProp.Name == getNamePropertyProjection_33())
191 : {
192 0 : double f_33(1.0);
193 0 : rProp.Value >>= f_33;
194 0 : maProjection.set(3, 3, f_33);
195 : }
196 0 : else if(rProp.Name == getNamePropertyDeviceToView())
197 : {
198 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
199 0 : rProp.Value >>= aAffineMatrix3D;
200 0 : maDeviceToView = basegfx::unotools::homMatrixFromAffineMatrix3D(aAffineMatrix3D);
201 : }
202 0 : else if(rProp.Name == getNamePropertyTime())
203 : {
204 0 : rProp.Value >>= mfViewTime;
205 : }
206 : else
207 : {
208 : // extra information; add to filtered information
209 0 : mxExtendedInformation[nExtendedInsert++] = rProp;
210 : }
211 : }
212 :
213 : // extra information size is now known; realloc to final size
214 0 : mxExtendedInformation.realloc(nExtendedInsert);
215 : }
216 21121 : }
217 :
218 : // central method to create a Sequence of PropertyValues containing he complete
219 : // data set
220 0 : void impFillViewInformationFromContent()
221 : {
222 0 : const bool bObjectTransformationUsed(!maObjectTransformation.isIdentity());
223 0 : const bool bOrientationUsed(!maOrientation.isIdentity());
224 0 : const bool bProjectionUsed(!maProjection.isIdentity());
225 0 : const bool bDeviceToViewUsed(!maDeviceToView.isIdentity());
226 0 : const bool bTimeUsed(0.0 < mfViewTime);
227 0 : const bool bExtraInformation(mxExtendedInformation.hasElements());
228 :
229 : // projection may be defined using a frustum in which case the last line of
230 : // the 4x4 matrix is not (0,0,0,1). Since AffineMatrix3D does not support that,
231 : // these four values need to be treated extra
232 0 : const bool bProjectionUsed_30(bProjectionUsed && !basegfx::fTools::equalZero(maProjection.get(3, 0)));
233 0 : const bool bProjectionUsed_31(bProjectionUsed && !basegfx::fTools::equalZero(maProjection.get(3, 1)));
234 0 : const bool bProjectionUsed_32(bProjectionUsed && !basegfx::fTools::equalZero(maProjection.get(3, 2)));
235 0 : const bool bProjectionUsed_33(bProjectionUsed && !basegfx::fTools::equal(maProjection.get(3, 3), 1.0));
236 :
237 0 : sal_uInt32 nIndex(0);
238 : const sal_uInt32 nCount(
239 0 : (bObjectTransformationUsed ? 1 : 0) +
240 0 : (bOrientationUsed ? 1 : 0) +
241 0 : (bProjectionUsed ? 1 : 0) +
242 0 : (bProjectionUsed_30 ? 1 : 0) +
243 0 : (bProjectionUsed_31 ? 1 : 0) +
244 0 : (bProjectionUsed_32 ? 1 : 0) +
245 0 : (bProjectionUsed_33 ? 1 : 0) +
246 0 : (bDeviceToViewUsed ? 1 : 0) +
247 0 : (bTimeUsed ? 1 : 0) +
248 0 : (bExtraInformation ? mxExtendedInformation.getLength() : 0));
249 :
250 0 : mxViewInformation.realloc(nCount);
251 :
252 0 : if(bObjectTransformationUsed)
253 : {
254 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
255 0 : basegfx::unotools::affineMatrixFromHomMatrix3D(aAffineMatrix3D, maObjectTransformation);
256 0 : mxViewInformation[nIndex].Name = getNamePropertyObjectTransformation();
257 0 : mxViewInformation[nIndex].Value <<= aAffineMatrix3D;
258 0 : nIndex++;
259 : }
260 :
261 0 : if(bOrientationUsed)
262 : {
263 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
264 0 : basegfx::unotools::affineMatrixFromHomMatrix3D(aAffineMatrix3D, maOrientation);
265 0 : mxViewInformation[nIndex].Name = getNamePropertyOrientation();
266 0 : mxViewInformation[nIndex].Value <<= aAffineMatrix3D;
267 0 : nIndex++;
268 : }
269 :
270 0 : if(bProjectionUsed)
271 : {
272 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
273 0 : basegfx::unotools::affineMatrixFromHomMatrix3D(aAffineMatrix3D, maProjection);
274 0 : mxViewInformation[nIndex].Name = getNamePropertyProjection();
275 0 : mxViewInformation[nIndex].Value <<= aAffineMatrix3D;
276 0 : nIndex++;
277 : }
278 :
279 0 : if(bProjectionUsed_30)
280 : {
281 0 : mxViewInformation[nIndex].Name = getNamePropertyProjection_30();
282 0 : mxViewInformation[nIndex].Value <<= maProjection.get(3, 0);
283 0 : nIndex++;
284 : }
285 :
286 0 : if(bProjectionUsed_31)
287 : {
288 0 : mxViewInformation[nIndex].Name = getNamePropertyProjection_31();
289 0 : mxViewInformation[nIndex].Value <<= maProjection.get(3, 1);
290 0 : nIndex++;
291 : }
292 :
293 0 : if(bProjectionUsed_32)
294 : {
295 0 : mxViewInformation[nIndex].Name = getNamePropertyProjection_32();
296 0 : mxViewInformation[nIndex].Value <<= maProjection.get(3, 2);
297 0 : nIndex++;
298 : }
299 :
300 0 : if(bProjectionUsed_33)
301 : {
302 0 : mxViewInformation[nIndex].Name = getNamePropertyProjection_33();
303 0 : mxViewInformation[nIndex].Value <<= maProjection.get(3, 3);
304 0 : nIndex++;
305 : }
306 :
307 0 : if(bDeviceToViewUsed)
308 : {
309 0 : com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D;
310 0 : basegfx::unotools::affineMatrixFromHomMatrix3D(aAffineMatrix3D, maDeviceToView);
311 0 : mxViewInformation[nIndex].Name = getNamePropertyDeviceToView();
312 0 : mxViewInformation[nIndex].Value <<= aAffineMatrix3D;
313 0 : nIndex++;
314 : }
315 :
316 0 : if(bTimeUsed)
317 : {
318 0 : mxViewInformation[nIndex].Name = getNamePropertyTime();
319 0 : mxViewInformation[nIndex].Value <<= mfViewTime;
320 0 : nIndex++;
321 : }
322 :
323 0 : if(bExtraInformation)
324 : {
325 0 : const sal_Int32 nExtra(mxExtendedInformation.getLength());
326 :
327 0 : for(sal_Int32 a(0); a < nExtra; a++)
328 : {
329 0 : mxViewInformation[nIndex++] = mxExtendedInformation[a];
330 : }
331 : }
332 0 : }
333 :
334 : public:
335 15872 : ImpViewInformation3D(
336 : const basegfx::B3DHomMatrix& rObjectTransformation,
337 : const basegfx::B3DHomMatrix& rOrientation,
338 : const basegfx::B3DHomMatrix& rProjection,
339 : const basegfx::B3DHomMatrix& rDeviceToView,
340 : double fViewTime,
341 : const uno::Sequence< beans::PropertyValue >& rExtendedParameters)
342 : : maObjectTransformation(rObjectTransformation),
343 : maOrientation(rOrientation),
344 : maProjection(rProjection),
345 : maDeviceToView(rDeviceToView),
346 : mfViewTime(fViewTime),
347 : mxViewInformation(),
348 15872 : mxExtendedInformation()
349 : {
350 15872 : impInterpretPropertyValues(rExtendedParameters);
351 15872 : }
352 :
353 5249 : explicit ImpViewInformation3D(const uno::Sequence< beans::PropertyValue >& rViewParameters)
354 : : maObjectTransformation(),
355 : maOrientation(),
356 : maProjection(),
357 : maDeviceToView(),
358 : mfViewTime(),
359 : mxViewInformation(rViewParameters),
360 5249 : mxExtendedInformation()
361 : {
362 5249 : impInterpretPropertyValues(rViewParameters);
363 5249 : }
364 :
365 5 : ImpViewInformation3D()
366 : : maObjectTransformation(),
367 : maOrientation(),
368 : maProjection(),
369 : maDeviceToView(),
370 : mfViewTime(),
371 : mxViewInformation(),
372 5 : mxExtendedInformation()
373 : {
374 5 : }
375 :
376 45193 : const basegfx::B3DHomMatrix& getObjectTransformation() const { return maObjectTransformation; }
377 44930 : const basegfx::B3DHomMatrix& getOrientation() const { return maOrientation; }
378 26541 : const basegfx::B3DHomMatrix& getProjection() const { return maProjection; }
379 26528 : const basegfx::B3DHomMatrix& getDeviceToView() const { return maDeviceToView; }
380 15245 : double getViewTime() const { return mfViewTime; }
381 :
382 41538 : const basegfx::B3DHomMatrix& getObjectToView() const
383 : {
384 : // on demand WorldToView creation
385 :
386 41538 : if(maObjectToView.isIdentity())
387 : {
388 9790 : const_cast< ImpViewInformation3D* >(this)->maObjectToView = maDeviceToView * maProjection * maOrientation * maObjectTransformation;
389 : }
390 :
391 41538 : return maObjectToView;
392 : }
393 :
394 0 : const uno::Sequence< beans::PropertyValue >& getViewInformationSequence() const
395 : {
396 0 : if(!mxViewInformation.hasElements())
397 : {
398 0 : const_cast< ImpViewInformation3D* >(this)->impFillViewInformationFromContent();
399 : }
400 :
401 0 : return mxViewInformation;
402 : }
403 :
404 15245 : const uno::Sequence< beans::PropertyValue >& getExtendedInformationSequence() const
405 : {
406 15245 : return mxExtendedInformation;
407 : }
408 :
409 54 : bool operator==(const ImpViewInformation3D& rCandidate) const
410 : {
411 54 : return (maObjectTransformation == rCandidate.maObjectTransformation
412 54 : && maOrientation == rCandidate.maOrientation
413 54 : && maProjection == rCandidate.maProjection
414 54 : && maDeviceToView == rCandidate.maDeviceToView
415 6 : && mfViewTime == rCandidate.mfViewTime
416 60 : && mxExtendedInformation == rCandidate.mxExtendedInformation);
417 : }
418 : };
419 : } // end of anonymous namespace
420 : } // end of namespace drawinglayer
421 :
422 :
423 :
424 : namespace drawinglayer
425 : {
426 : namespace geometry
427 : {
428 : namespace
429 : {
430 : struct theGlobalDefault :
431 : public rtl::Static< ViewInformation3D::ImplType, theGlobalDefault > {};
432 : }
433 :
434 15872 : ViewInformation3D::ViewInformation3D(
435 : const basegfx::B3DHomMatrix& rObjectObjectTransformation,
436 : const basegfx::B3DHomMatrix& rOrientation,
437 : const basegfx::B3DHomMatrix& rProjection,
438 : const basegfx::B3DHomMatrix& rDeviceToView,
439 : double fViewTime,
440 : const uno::Sequence< beans::PropertyValue >& rExtendedParameters)
441 : : mpViewInformation3D(ImpViewInformation3D(
442 : rObjectObjectTransformation, rOrientation, rProjection,
443 15872 : rDeviceToView, fViewTime, rExtendedParameters))
444 : {
445 15872 : }
446 :
447 5249 : ViewInformation3D::ViewInformation3D(const uno::Sequence< beans::PropertyValue >& rViewParameters)
448 5249 : : mpViewInformation3D(ImpViewInformation3D(rViewParameters))
449 : {
450 5249 : }
451 :
452 8142 : ViewInformation3D::ViewInformation3D()
453 8142 : : mpViewInformation3D(theGlobalDefault::get())
454 : {
455 8142 : }
456 :
457 15656 : ViewInformation3D::ViewInformation3D(const ViewInformation3D& rCandidate)
458 15656 : : mpViewInformation3D(rCandidate.mpViewInformation3D)
459 : {
460 15656 : }
461 :
462 44919 : ViewInformation3D::~ViewInformation3D()
463 : {
464 44919 : }
465 :
466 4489 : bool ViewInformation3D::isDefault() const
467 : {
468 4489 : return mpViewInformation3D.same_object(theGlobalDefault::get());
469 : }
470 :
471 33826 : ViewInformation3D& ViewInformation3D::operator=(const ViewInformation3D& rCandidate)
472 : {
473 33826 : mpViewInformation3D = rCandidate.mpViewInformation3D;
474 33826 : return *this;
475 : }
476 :
477 77 : bool ViewInformation3D::operator==(const ViewInformation3D& rCandidate) const
478 : {
479 77 : return rCandidate.mpViewInformation3D == mpViewInformation3D;
480 : }
481 :
482 45193 : const basegfx::B3DHomMatrix& ViewInformation3D::getObjectTransformation() const
483 : {
484 45193 : return mpViewInformation3D->getObjectTransformation();
485 : }
486 :
487 44930 : const basegfx::B3DHomMatrix& ViewInformation3D::getOrientation() const
488 : {
489 44930 : return mpViewInformation3D->getOrientation();
490 : }
491 :
492 26541 : const basegfx::B3DHomMatrix& ViewInformation3D::getProjection() const
493 : {
494 26541 : return mpViewInformation3D->getProjection();
495 : }
496 :
497 26528 : const basegfx::B3DHomMatrix& ViewInformation3D::getDeviceToView() const
498 : {
499 26528 : return mpViewInformation3D->getDeviceToView();
500 : }
501 :
502 41538 : const basegfx::B3DHomMatrix& ViewInformation3D::getObjectToView() const
503 : {
504 41538 : return mpViewInformation3D->getObjectToView();
505 : }
506 :
507 15245 : double ViewInformation3D::getViewTime() const
508 : {
509 15245 : return mpViewInformation3D->getViewTime();
510 : }
511 :
512 0 : const uno::Sequence< beans::PropertyValue >& ViewInformation3D::getViewInformationSequence() const
513 : {
514 0 : return mpViewInformation3D->getViewInformationSequence();
515 : }
516 :
517 15245 : const uno::Sequence< beans::PropertyValue >& ViewInformation3D::getExtendedInformationSequence() const
518 : {
519 15245 : return mpViewInformation3D->getExtendedInformationSequence();
520 : }
521 : } // end of namespace geometry
522 : } // end of namespace drawinglayer
523 :
524 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|