1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
30 using namespace com::sun::star
;
34 namespace drawinglayer
38 /** Implementation class for ViewInformation3D
40 class ImpViewInformation3D
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
;
47 // the 3D transformations
48 // Object to World. This may change and being adapted when entering 3D transformation
50 basegfx::B3DHomMatrix maObjectTransformation
;
52 // World to Camera. This includes VRP, VPN and VUV camera coordinate system
53 basegfx::B3DHomMatrix maOrientation
;
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
;
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
;
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
;
73 // the complete PropertyValue representation (if already created)
74 uno::Sequence
< beans::PropertyValue
> mxViewInformation
;
76 // the extra PropertyValues; does not contain the transformations
77 uno::Sequence
< beans::PropertyValue
> mxExtendedInformation
;
79 // the local UNO API strings
80 static OUString
getNamePropertyObjectTransformation()
82 return OUString("ObjectTransformation");
85 static OUString
getNamePropertyOrientation()
87 return OUString("Orientation");
90 static OUString
getNamePropertyProjection()
92 return OUString("Projection");
95 static OUString
getNamePropertyProjection_30()
97 return OUString("Projection30");
100 static OUString
getNamePropertyProjection_31()
102 return OUString("Projection31");
105 static OUString
getNamePropertyProjection_32()
107 return OUString("Projection32");
110 static OUString
getNamePropertyProjection_33()
112 return OUString("Projection33");
115 static OUString
getNamePropertyDeviceToView()
117 return OUString("DeviceToView");
120 static OUString
getNamePropertyTime()
122 return OUString("Time");
125 // a central PropertyValue parsing method to allow transportatin of
126 // all ViewParameters using UNO API
127 void impInterpretPropertyValues(const uno::Sequence
< beans::PropertyValue
>& rViewParameters
)
129 if(rViewParameters
.hasElements())
131 const sal_Int32
nCount(rViewParameters
.getLength());
132 sal_Int32
nExtendedInsert(0);
134 // prepare extended information for filtering. Maximum size is nCount
135 mxExtendedInformation
.realloc(nCount
);
137 for(sal_Int32
a(0); a
< nCount
; a
++)
139 const beans::PropertyValue
& rProp
= rViewParameters
[a
];
141 if(rProp
.Name
== getNamePropertyObjectTransformation())
143 com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D
;
144 rProp
.Value
>>= aAffineMatrix3D
;
145 maObjectTransformation
= basegfx::unotools::homMatrixFromAffineMatrix3D(aAffineMatrix3D
);
147 else if(rProp
.Name
== getNamePropertyOrientation())
149 com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D
;
150 rProp
.Value
>>= aAffineMatrix3D
;
151 maOrientation
= basegfx::unotools::homMatrixFromAffineMatrix3D(aAffineMatrix3D
);
153 else if(rProp
.Name
== getNamePropertyProjection())
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 const double f_30(maProjection
.get(3, 0));
159 const double f_31(maProjection
.get(3, 1));
160 const double f_32(maProjection
.get(3, 2));
161 const double f_33(maProjection
.get(3, 3));
163 com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D
;
164 rProp
.Value
>>= aAffineMatrix3D
;
165 maProjection
= basegfx::unotools::homMatrixFromAffineMatrix3D(aAffineMatrix3D
);
167 maProjection
.set(3, 0, f_30
);
168 maProjection
.set(3, 1, f_31
);
169 maProjection
.set(3, 2, f_32
);
170 maProjection
.set(3, 3, f_33
);
172 else if(rProp
.Name
== getNamePropertyProjection_30())
175 rProp
.Value
>>= f_30
;
176 maProjection
.set(3, 0, f_30
);
178 else if(rProp
.Name
== getNamePropertyProjection_31())
181 rProp
.Value
>>= f_31
;
182 maProjection
.set(3, 1, f_31
);
184 else if(rProp
.Name
== getNamePropertyProjection_32())
187 rProp
.Value
>>= f_32
;
188 maProjection
.set(3, 2, f_32
);
190 else if(rProp
.Name
== getNamePropertyProjection_33())
193 rProp
.Value
>>= f_33
;
194 maProjection
.set(3, 3, f_33
);
196 else if(rProp
.Name
== getNamePropertyDeviceToView())
198 com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D
;
199 rProp
.Value
>>= aAffineMatrix3D
;
200 maDeviceToView
= basegfx::unotools::homMatrixFromAffineMatrix3D(aAffineMatrix3D
);
202 else if(rProp
.Name
== getNamePropertyTime())
204 rProp
.Value
>>= mfViewTime
;
208 // extra information; add to filtered information
209 mxExtendedInformation
[nExtendedInsert
++] = rProp
;
213 // extra information size is now known; realloc to final size
214 mxExtendedInformation
.realloc(nExtendedInsert
);
218 // central method to create a Sequence of PropertyValues containing he complete
220 void impFillViewInformationFromContent()
222 const bool bObjectTransformationUsed(!maObjectTransformation
.isIdentity());
223 const bool bOrientationUsed(!maOrientation
.isIdentity());
224 const bool bProjectionUsed(!maProjection
.isIdentity());
225 const bool bDeviceToViewUsed(!maDeviceToView
.isIdentity());
226 const bool bTimeUsed(0.0 < mfViewTime
);
227 const bool bExtraInformation(mxExtendedInformation
.hasElements());
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 const bool bProjectionUsed_30(bProjectionUsed
&& !basegfx::fTools::equalZero(maProjection
.get(3, 0)));
233 const bool bProjectionUsed_31(bProjectionUsed
&& !basegfx::fTools::equalZero(maProjection
.get(3, 1)));
234 const bool bProjectionUsed_32(bProjectionUsed
&& !basegfx::fTools::equalZero(maProjection
.get(3, 2)));
235 const bool bProjectionUsed_33(bProjectionUsed
&& !basegfx::fTools::equal(maProjection
.get(3, 3), 1.0));
237 sal_uInt32
nIndex(0);
238 const sal_uInt32
nCount(
239 (bObjectTransformationUsed
? 1 : 0) +
240 (bOrientationUsed
? 1 : 0) +
241 (bProjectionUsed
? 1 : 0) +
242 (bProjectionUsed_30
? 1 : 0) +
243 (bProjectionUsed_31
? 1 : 0) +
244 (bProjectionUsed_32
? 1 : 0) +
245 (bProjectionUsed_33
? 1 : 0) +
246 (bDeviceToViewUsed
? 1 : 0) +
247 (bTimeUsed
? 1 : 0) +
248 (bExtraInformation
? mxExtendedInformation
.getLength() : 0));
250 mxViewInformation
.realloc(nCount
);
252 if(bObjectTransformationUsed
)
254 com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D
;
255 basegfx::unotools::affineMatrixFromHomMatrix3D(aAffineMatrix3D
, maObjectTransformation
);
256 mxViewInformation
[nIndex
].Name
= getNamePropertyObjectTransformation();
257 mxViewInformation
[nIndex
].Value
<<= aAffineMatrix3D
;
263 com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D
;
264 basegfx::unotools::affineMatrixFromHomMatrix3D(aAffineMatrix3D
, maOrientation
);
265 mxViewInformation
[nIndex
].Name
= getNamePropertyOrientation();
266 mxViewInformation
[nIndex
].Value
<<= aAffineMatrix3D
;
272 com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D
;
273 basegfx::unotools::affineMatrixFromHomMatrix3D(aAffineMatrix3D
, maProjection
);
274 mxViewInformation
[nIndex
].Name
= getNamePropertyProjection();
275 mxViewInformation
[nIndex
].Value
<<= aAffineMatrix3D
;
279 if(bProjectionUsed_30
)
281 mxViewInformation
[nIndex
].Name
= getNamePropertyProjection_30();
282 mxViewInformation
[nIndex
].Value
<<= maProjection
.get(3, 0);
286 if(bProjectionUsed_31
)
288 mxViewInformation
[nIndex
].Name
= getNamePropertyProjection_31();
289 mxViewInformation
[nIndex
].Value
<<= maProjection
.get(3, 1);
293 if(bProjectionUsed_32
)
295 mxViewInformation
[nIndex
].Name
= getNamePropertyProjection_32();
296 mxViewInformation
[nIndex
].Value
<<= maProjection
.get(3, 2);
300 if(bProjectionUsed_33
)
302 mxViewInformation
[nIndex
].Name
= getNamePropertyProjection_33();
303 mxViewInformation
[nIndex
].Value
<<= maProjection
.get(3, 3);
307 if(bDeviceToViewUsed
)
309 com::sun::star::geometry::AffineMatrix3D aAffineMatrix3D
;
310 basegfx::unotools::affineMatrixFromHomMatrix3D(aAffineMatrix3D
, maDeviceToView
);
311 mxViewInformation
[nIndex
].Name
= getNamePropertyDeviceToView();
312 mxViewInformation
[nIndex
].Value
<<= aAffineMatrix3D
;
318 mxViewInformation
[nIndex
].Name
= getNamePropertyTime();
319 mxViewInformation
[nIndex
].Value
<<= mfViewTime
;
323 if(bExtraInformation
)
325 const sal_Int32
nExtra(mxExtendedInformation
.getLength());
327 for(sal_Int32
a(0); a
< nExtra
; a
++)
329 mxViewInformation
[nIndex
++] = mxExtendedInformation
[a
];
335 ImpViewInformation3D(
336 const basegfx::B3DHomMatrix
& rObjectTransformation
,
337 const basegfx::B3DHomMatrix
& rOrientation
,
338 const basegfx::B3DHomMatrix
& rProjection
,
339 const basegfx::B3DHomMatrix
& rDeviceToView
,
341 const uno::Sequence
< beans::PropertyValue
>& rExtendedParameters
)
342 : maObjectTransformation(rObjectTransformation
),
343 maOrientation(rOrientation
),
344 maProjection(rProjection
),
345 maDeviceToView(rDeviceToView
),
346 mfViewTime(fViewTime
),
348 mxExtendedInformation()
350 impInterpretPropertyValues(rExtendedParameters
);
353 explicit ImpViewInformation3D(const uno::Sequence
< beans::PropertyValue
>& rViewParameters
)
354 : maObjectTransformation(),
359 mxViewInformation(rViewParameters
),
360 mxExtendedInformation()
362 impInterpretPropertyValues(rViewParameters
);
365 ImpViewInformation3D()
366 : maObjectTransformation(),
372 mxExtendedInformation()
376 const basegfx::B3DHomMatrix
& getObjectTransformation() const { return maObjectTransformation
; }
377 const basegfx::B3DHomMatrix
& getOrientation() const { return maOrientation
; }
378 const basegfx::B3DHomMatrix
& getProjection() const { return maProjection
; }
379 const basegfx::B3DHomMatrix
& getDeviceToView() const { return maDeviceToView
; }
380 double getViewTime() const { return mfViewTime
; }
382 const basegfx::B3DHomMatrix
& getObjectToView() const
384 // on demand WorldToView creation
386 if(maObjectToView
.isIdentity())
388 const_cast< ImpViewInformation3D
* >(this)->maObjectToView
= maDeviceToView
* maProjection
* maOrientation
* maObjectTransformation
;
391 return maObjectToView
;
394 const uno::Sequence
< beans::PropertyValue
>& getViewInformationSequence() const
396 if(!mxViewInformation
.hasElements())
398 const_cast< ImpViewInformation3D
* >(this)->impFillViewInformationFromContent();
401 return mxViewInformation
;
404 const uno::Sequence
< beans::PropertyValue
>& getExtendedInformationSequence() const
406 return mxExtendedInformation
;
409 bool operator==(const ImpViewInformation3D
& rCandidate
) const
411 return (maObjectTransformation
== rCandidate
.maObjectTransformation
412 && maOrientation
== rCandidate
.maOrientation
413 && maProjection
== rCandidate
.maProjection
414 && maDeviceToView
== rCandidate
.maDeviceToView
415 && mfViewTime
== rCandidate
.mfViewTime
416 && mxExtendedInformation
== rCandidate
.mxExtendedInformation
);
419 } // end of anonymous namespace
420 } // end of namespace drawinglayer
424 namespace drawinglayer
430 struct theGlobalDefault
:
431 public rtl::Static
< ViewInformation3D::ImplType
, theGlobalDefault
> {};
434 ViewInformation3D::ViewInformation3D(
435 const basegfx::B3DHomMatrix
& rObjectObjectTransformation
,
436 const basegfx::B3DHomMatrix
& rOrientation
,
437 const basegfx::B3DHomMatrix
& rProjection
,
438 const basegfx::B3DHomMatrix
& rDeviceToView
,
440 const uno::Sequence
< beans::PropertyValue
>& rExtendedParameters
)
441 : mpViewInformation3D(ImpViewInformation3D(
442 rObjectObjectTransformation
, rOrientation
, rProjection
,
443 rDeviceToView
, fViewTime
, rExtendedParameters
))
447 ViewInformation3D::ViewInformation3D(const uno::Sequence
< beans::PropertyValue
>& rViewParameters
)
448 : mpViewInformation3D(ImpViewInformation3D(rViewParameters
))
452 ViewInformation3D::ViewInformation3D()
453 : mpViewInformation3D(theGlobalDefault::get())
457 ViewInformation3D::ViewInformation3D(const ViewInformation3D
& rCandidate
)
458 : mpViewInformation3D(rCandidate
.mpViewInformation3D
)
462 ViewInformation3D::~ViewInformation3D()
466 bool ViewInformation3D::isDefault() const
468 return mpViewInformation3D
.same_object(theGlobalDefault::get());
471 ViewInformation3D
& ViewInformation3D::operator=(const ViewInformation3D
& rCandidate
)
473 mpViewInformation3D
= rCandidate
.mpViewInformation3D
;
477 bool ViewInformation3D::operator==(const ViewInformation3D
& rCandidate
) const
479 return rCandidate
.mpViewInformation3D
== mpViewInformation3D
;
482 const basegfx::B3DHomMatrix
& ViewInformation3D::getObjectTransformation() const
484 return mpViewInformation3D
->getObjectTransformation();
487 const basegfx::B3DHomMatrix
& ViewInformation3D::getOrientation() const
489 return mpViewInformation3D
->getOrientation();
492 const basegfx::B3DHomMatrix
& ViewInformation3D::getProjection() const
494 return mpViewInformation3D
->getProjection();
497 const basegfx::B3DHomMatrix
& ViewInformation3D::getDeviceToView() const
499 return mpViewInformation3D
->getDeviceToView();
502 const basegfx::B3DHomMatrix
& ViewInformation3D::getObjectToView() const
504 return mpViewInformation3D
->getObjectToView();
507 double ViewInformation3D::getViewTime() const
509 return mpViewInformation3D
->getViewTime();
512 const uno::Sequence
< beans::PropertyValue
>& ViewInformation3D::getViewInformationSequence() const
514 return mpViewInformation3D
->getViewInformationSequence();
517 const uno::Sequence
< beans::PropertyValue
>& ViewInformation3D::getExtendedInformationSequence() const
519 return mpViewInformation3D
->getExtendedInformationSequence();
521 } // end of namespace geometry
522 } // end of namespace drawinglayer
524 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */