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/matrix/b3dhommatrix.hxx>
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <com/sun/star/geometry/AffineMatrix3D.hpp>
24 #include <basegfx/utils/canvastools.hxx>
25 #include <com/sun/star/uno/Sequence.hxx>
29 using namespace com::sun::star
;
32 namespace drawinglayer::geometry
34 /** Implementation class for ViewInformation3D
36 class ImpViewInformation3D
39 // ViewInformation3D implementation can change refcount, so we have only
40 // two memory regions for pairs of ViewInformation3D/ImpViewInformation3D
41 friend class ::drawinglayer::geometry::ViewInformation3D
;
43 // the 3D transformations
44 // Object to World. This may change and being adapted when entering 3D transformation
46 basegfx::B3DHomMatrix maObjectTransformation
;
48 // World to Camera. This includes VRP, VPN and VUV camera coordinate system
49 basegfx::B3DHomMatrix maOrientation
;
51 // Camera to Device with X,Y and Z [-1.0 .. 1.0]. This is the
52 // 3D to 2D projection which may be parallel or perspective. When it is perspective,
53 // the last line of the homogen matrix will NOT be unused
54 basegfx::B3DHomMatrix maProjection
;
56 // Device to View with X,Y and Z [0.0 .. 1.0]. This converts from -1 to 1 coordinates
57 // in camera coordinate system to 0 to 1 in unit 2D coordinates. This way it stays
58 // view-independent. To get discrete coordinates, the 2D transformation of a scene
59 // as 2D object needs to be involved
60 basegfx::B3DHomMatrix maDeviceToView
;
62 // Object to View is the linear combination of all four transformations. It's
63 // buffered to avoid too much matrix multiplying and created on demand
64 basegfx::B3DHomMatrix maObjectToView
;
69 // the extra PropertyValues; does not contain the transformations
70 uno::Sequence
< beans::PropertyValue
> mxExtendedInformation
;
72 // the local UNO API strings
73 static OUString
getNamePropertyObjectTransformation()
75 return "ObjectTransformation";
78 static OUString
getNamePropertyOrientation()
83 static OUString
getNamePropertyProjection()
88 static OUString
getNamePropertyProjection_30()
90 return "Projection30";
93 static OUString
getNamePropertyProjection_31()
95 return "Projection31";
98 static OUString
getNamePropertyProjection_32()
100 return "Projection32";
103 static OUString
getNamePropertyProjection_33()
105 return "Projection33";
108 static OUString
getNamePropertyDeviceToView()
110 return "DeviceToView";
113 static OUString
getNamePropertyTime()
118 // a central PropertyValue parsing method to allow transportation of
119 // all ViewParameters using UNO API
120 void impInterpretPropertyValues(const uno::Sequence
< beans::PropertyValue
>& rViewParameters
)
122 if(!rViewParameters
.hasElements())
125 const sal_Int32
nCount(rViewParameters
.getLength());
126 sal_Int32
nExtendedInsert(0);
128 // prepare extended information for filtering. Maximum size is nCount
129 mxExtendedInformation
.realloc(nCount
);
130 auto pExtendedInformation
= mxExtendedInformation
.getArray();
132 for(sal_Int32
a(0); a
< nCount
; a
++)
134 const beans::PropertyValue
& rProp
= rViewParameters
[a
];
136 if(rProp
.Name
== getNamePropertyObjectTransformation())
138 css::geometry::AffineMatrix3D aAffineMatrix3D
;
139 rProp
.Value
>>= aAffineMatrix3D
;
140 maObjectTransformation
= basegfx::unotools::homMatrixFromAffineMatrix3D(aAffineMatrix3D
);
142 else if(rProp
.Name
== getNamePropertyOrientation())
144 css::geometry::AffineMatrix3D aAffineMatrix3D
;
145 rProp
.Value
>>= aAffineMatrix3D
;
146 maOrientation
= basegfx::unotools::homMatrixFromAffineMatrix3D(aAffineMatrix3D
);
148 else if(rProp
.Name
== getNamePropertyProjection())
150 // projection may be defined using a frustum in which case the last line of
151 // the 4x4 matrix is not (0,0,0,1). Since AffineMatrix3D does not support that,
152 // these four values need to be treated extra
153 const double f_30(maProjection
.get(3, 0));
154 const double f_31(maProjection
.get(3, 1));
155 const double f_32(maProjection
.get(3, 2));
156 const double f_33(maProjection
.get(3, 3));
158 css::geometry::AffineMatrix3D aAffineMatrix3D
;
159 rProp
.Value
>>= aAffineMatrix3D
;
160 maProjection
= basegfx::unotools::homMatrixFromAffineMatrix3D(aAffineMatrix3D
);
162 maProjection
.set(3, 0, f_30
);
163 maProjection
.set(3, 1, f_31
);
164 maProjection
.set(3, 2, f_32
);
165 maProjection
.set(3, 3, f_33
);
167 else if(rProp
.Name
== getNamePropertyProjection_30())
170 rProp
.Value
>>= f_30
;
171 maProjection
.set(3, 0, f_30
);
173 else if(rProp
.Name
== getNamePropertyProjection_31())
176 rProp
.Value
>>= f_31
;
177 maProjection
.set(3, 1, f_31
);
179 else if(rProp
.Name
== getNamePropertyProjection_32())
182 rProp
.Value
>>= f_32
;
183 maProjection
.set(3, 2, f_32
);
185 else if(rProp
.Name
== getNamePropertyProjection_33())
188 rProp
.Value
>>= f_33
;
189 maProjection
.set(3, 3, f_33
);
191 else if(rProp
.Name
== getNamePropertyDeviceToView())
193 css::geometry::AffineMatrix3D aAffineMatrix3D
;
194 rProp
.Value
>>= aAffineMatrix3D
;
195 maDeviceToView
= basegfx::unotools::homMatrixFromAffineMatrix3D(aAffineMatrix3D
);
197 else if(rProp
.Name
== getNamePropertyTime())
199 rProp
.Value
>>= mfViewTime
;
203 // extra information; add to filtered information
204 pExtendedInformation
[nExtendedInsert
++] = rProp
;
208 // extra information size is now known; realloc to final size
209 mxExtendedInformation
.realloc(nExtendedInsert
);
213 ImpViewInformation3D(
214 basegfx::B3DHomMatrix aObjectTransformation
,
215 basegfx::B3DHomMatrix aOrientation
,
216 basegfx::B3DHomMatrix aProjection
,
217 basegfx::B3DHomMatrix aDeviceToView
,
219 const uno::Sequence
< beans::PropertyValue
>& rExtendedParameters
)
220 : maObjectTransformation(std::move(aObjectTransformation
)),
221 maOrientation(std::move(aOrientation
)),
222 maProjection(std::move(aProjection
)),
223 maDeviceToView(std::move(aDeviceToView
)),
224 mfViewTime(fViewTime
)
226 impInterpretPropertyValues(rExtendedParameters
);
229 explicit ImpViewInformation3D(const uno::Sequence
< beans::PropertyValue
>& rViewParameters
)
232 impInterpretPropertyValues(rViewParameters
);
235 ImpViewInformation3D()
240 const basegfx::B3DHomMatrix
& getObjectTransformation() const { return maObjectTransformation
; }
241 const basegfx::B3DHomMatrix
& getOrientation() const { return maOrientation
; }
242 const basegfx::B3DHomMatrix
& getProjection() const { return maProjection
; }
243 const basegfx::B3DHomMatrix
& getDeviceToView() const { return maDeviceToView
; }
244 double getViewTime() const { return mfViewTime
; }
246 const basegfx::B3DHomMatrix
& getObjectToView() const
248 // on demand WorldToView creation
250 if(maObjectToView
.isIdentity())
252 const_cast< ImpViewInformation3D
* >(this)->maObjectToView
= maDeviceToView
* maProjection
* maOrientation
* maObjectTransformation
;
255 return maObjectToView
;
258 const uno::Sequence
< beans::PropertyValue
>& getExtendedInformationSequence() const
260 return mxExtendedInformation
;
263 bool operator==(const ImpViewInformation3D
& rCandidate
) const
265 return (maObjectTransformation
== rCandidate
.maObjectTransformation
266 && maOrientation
== rCandidate
.maOrientation
267 && maProjection
== rCandidate
.maProjection
268 && maDeviceToView
== rCandidate
.maDeviceToView
269 && mfViewTime
== rCandidate
.mfViewTime
270 && mxExtendedInformation
== rCandidate
.mxExtendedInformation
);
273 } // end of namespace drawinglayer::geometry
276 namespace drawinglayer::geometry
280 ViewInformation3D::ImplType
& theGlobalDefault()
282 static ViewInformation3D::ImplType SINGLETON
;
287 ViewInformation3D::ViewInformation3D(
288 const basegfx::B3DHomMatrix
& rObjectObjectTransformation
,
289 const basegfx::B3DHomMatrix
& rOrientation
,
290 const basegfx::B3DHomMatrix
& rProjection
,
291 const basegfx::B3DHomMatrix
& rDeviceToView
,
293 const uno::Sequence
< beans::PropertyValue
>& rExtendedParameters
)
294 : mpViewInformation3D(ImpViewInformation3D(
295 rObjectObjectTransformation
, rOrientation
, rProjection
,
296 rDeviceToView
, fViewTime
, rExtendedParameters
))
300 ViewInformation3D::ViewInformation3D(const uno::Sequence
< beans::PropertyValue
>& rViewParameters
)
301 : mpViewInformation3D(ImpViewInformation3D(rViewParameters
))
305 ViewInformation3D::ViewInformation3D()
306 : mpViewInformation3D(theGlobalDefault())
310 ViewInformation3D::ViewInformation3D(const ViewInformation3D
&) = default;
312 ViewInformation3D::ViewInformation3D(ViewInformation3D
&&) = default;
314 ViewInformation3D::~ViewInformation3D() = default;
316 bool ViewInformation3D::isDefault() const
318 return mpViewInformation3D
.same_object(theGlobalDefault());
321 ViewInformation3D
& ViewInformation3D::operator=(const ViewInformation3D
&) = default;
323 ViewInformation3D
& ViewInformation3D::operator=(ViewInformation3D
&&) = default;
325 bool ViewInformation3D::operator==(const ViewInformation3D
& rCandidate
) const
327 return rCandidate
.mpViewInformation3D
== mpViewInformation3D
;
330 const basegfx::B3DHomMatrix
& ViewInformation3D::getObjectTransformation() const
332 return mpViewInformation3D
->getObjectTransformation();
335 const basegfx::B3DHomMatrix
& ViewInformation3D::getOrientation() const
337 return mpViewInformation3D
->getOrientation();
340 const basegfx::B3DHomMatrix
& ViewInformation3D::getProjection() const
342 return mpViewInformation3D
->getProjection();
345 const basegfx::B3DHomMatrix
& ViewInformation3D::getDeviceToView() const
347 return mpViewInformation3D
->getDeviceToView();
350 const basegfx::B3DHomMatrix
& ViewInformation3D::getObjectToView() const
352 return mpViewInformation3D
->getObjectToView();
355 double ViewInformation3D::getViewTime() const
357 return mpViewInformation3D
->getViewTime();
360 const uno::Sequence
< beans::PropertyValue
>& ViewInformation3D::getExtendedInformationSequence() const
362 return mpViewInformation3D
->getExtendedInformationSequence();
365 } // end of namespace
367 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */