bump product version to 7.2.5.1
[LibreOffice.git] / svx / source / customshapes / EnhancedCustomShape3d.cxx
blobd52d9c5d4b605be1770c9074a87f6715066320b9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "EnhancedCustomShape3d.hxx"
21 #include <o3tl/unit_conversion.hxx>
22 #include <svx/deflt3d.hxx>
23 #include <svx/svdmodel.hxx>
24 #include <tools/poly.hxx>
25 #include <svx/svditer.hxx>
26 #include <svx/svdobj.hxx>
27 #include <svx/svdoashp.hxx>
28 #include <svl/itemset.hxx>
29 #include <svx/xfillit0.hxx>
30 #include <svx/xlineit0.hxx>
31 #include <svx/xsflclit.hxx>
32 #include <svx/xbtmpit.hxx>
33 #include <svx/xflclit.hxx>
34 #include <svx/svdopath.hxx>
35 #include <svx/svddef.hxx>
36 #include <svx/svx3ditems.hxx>
37 #include <extrud3d.hxx>
38 #include <svx/xflbmtit.hxx>
39 #include <svx/xlnclit.hxx>
40 #include <svx/sdasitm.hxx>
41 #include <svx/scene3d.hxx>
42 #include <com/sun/star/drawing/Position3D.hpp>
43 #include <com/sun/star/drawing/Direction3D.hpp>
44 #include <com/sun/star/drawing/ShadeMode.hpp>
45 #include <svx/sdr/properties/properties.hxx>
46 #include <com/sun/star/drawing/EnhancedCustomShapeParameterPair.hpp>
47 #include <basegfx/polygon/b2dpolypolygontools.hxx>
48 #include <basegfx/range/b2drange.hxx>
49 #include <sdr/primitive2d/sdrattributecreator.hxx>
50 #include <drawinglayer/attribute/sdrlineattribute.hxx>
51 #include <drawinglayer/attribute/sdrlinestartendattribute.hxx>
52 #include <svx/xlnwtit.hxx>
53 #include <svx/xlntrit.hxx>
54 #include <svx/xfltrit.hxx>
56 using namespace com::sun::star;
57 using namespace com::sun::star::uno;
59 namespace {
61 void GetOrigin( const SdrCustomShapeGeometryItem& rItem, double& rOriginX, double& rOriginY )
63 css::drawing::EnhancedCustomShapeParameterPair aOriginParaPair;
64 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "Origin" );
65 if ( ! ( pAny && ( *pAny >>= aOriginParaPair ) && ( aOriginParaPair.First.Value >>= rOriginX ) && ( aOriginParaPair.Second.Value >>= rOriginY ) ) )
67 rOriginX = 0.50;
68 rOriginY =-0.50;
72 void GetRotateAngle( const SdrCustomShapeGeometryItem& rItem, double& rAngleX, double& rAngleY )
74 css::drawing::EnhancedCustomShapeParameterPair aRotateAngleParaPair;
75 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "RotateAngle" );
76 if ( ! ( pAny && ( *pAny >>= aRotateAngleParaPair ) && ( aRotateAngleParaPair.First.Value >>= rAngleX ) && ( aRotateAngleParaPair.Second.Value >>= rAngleY ) ) )
78 rAngleX = 0.0;
79 rAngleY = 0.0;
81 rAngleX = basegfx::deg2rad(rAngleX);
82 rAngleY = basegfx::deg2rad(rAngleY);
85 void GetSkew( const SdrCustomShapeGeometryItem& rItem, double& rSkewAmount, double& rSkewAngle )
87 css::drawing::EnhancedCustomShapeParameterPair aSkewParaPair;
88 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "Skew" );
89 if ( ! ( pAny && ( *pAny >>= aSkewParaPair ) && ( aSkewParaPair.First.Value >>= rSkewAmount ) && ( aSkewParaPair.Second.Value >>= rSkewAngle ) ) )
91 rSkewAmount = 50;
92 rSkewAngle = -135;
94 rSkewAngle = basegfx::deg2rad(rSkewAngle);
97 void GetExtrusionDepth( const SdrCustomShapeGeometryItem& rItem, const double* pMap, double& rBackwardDepth, double& rForwardDepth )
99 css::drawing::EnhancedCustomShapeParameterPair aDepthParaPair;
100 double fDepth = 0, fFraction = 0;
101 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "Depth" );
102 if ( pAny && ( *pAny >>= aDepthParaPair ) && ( aDepthParaPair.First.Value >>= fDepth ) && ( aDepthParaPair.Second.Value >>= fFraction ) )
104 rForwardDepth = fDepth * fFraction;
105 rBackwardDepth = fDepth - rForwardDepth;
107 else
109 rBackwardDepth = 1270;
110 rForwardDepth = 0;
112 if ( pMap )
114 double fMap = *pMap;
115 rBackwardDepth *= fMap;
116 rForwardDepth *= fMap;
120 double GetDouble( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName, double fDefault )
122 double fRetValue = fDefault;
123 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", rPropertyName );
124 if ( pAny )
125 *pAny >>= fRetValue;
126 return fRetValue;
129 drawing::ShadeMode GetShadeMode( const SdrCustomShapeGeometryItem& rItem, const drawing::ShadeMode eDefault )
131 drawing::ShadeMode eRet( eDefault );
132 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "ShadeMode" );
133 if ( pAny )
134 *pAny >>= eRet;
135 return eRet;
138 bool GetBool( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName, const bool bDefault )
140 bool bRetValue = bDefault;
141 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", rPropertyName );
142 if ( pAny )
143 *pAny >>= bRetValue;
144 return bRetValue;
147 drawing::Position3D GetPosition3D( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName,
148 const drawing::Position3D& rDefault, const double* pMap )
150 drawing::Position3D aRetValue( rDefault );
151 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", rPropertyName );
152 if ( pAny )
153 *pAny >>= aRetValue;
154 if ( pMap )
156 aRetValue.PositionX *= *pMap;
157 aRetValue.PositionY *= *pMap;
158 aRetValue.PositionZ *= *pMap;
160 return aRetValue;
163 drawing::Direction3D GetDirection3D( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName, const drawing::Direction3D& rDefault )
165 drawing::Direction3D aRetValue( rDefault );
166 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", rPropertyName );
167 if ( pAny )
168 *pAny >>= aRetValue;
169 return aRetValue;
174 EnhancedCustomShape3d::Transformation2D::Transformation2D(
175 const SdrObjCustomShape& rSdrObjCustomShape,
176 const double *pMap)
177 : aCenter(rSdrObjCustomShape.GetSnapRect().Center())
178 , eProjectionMode( drawing::ProjectionMode_PARALLEL )
179 , fSkewAngle(0.0)
180 , fSkew(0.0)
181 , fOriginX(0.0)
182 , fOriginY(0.0)
184 const SdrCustomShapeGeometryItem& rGeometryItem(rSdrObjCustomShape.GetMergedItem( SDRATTR_CUSTOMSHAPE_GEOMETRY ));
185 const Any* pAny = rGeometryItem.GetPropertyValueByName( "Extrusion", "ProjectionMode" );
186 if ( pAny )
187 *pAny >>= eProjectionMode;
189 if ( eProjectionMode == drawing::ProjectionMode_PARALLEL )
190 GetSkew( rGeometryItem, fSkew, fSkewAngle );
191 else
193 GetOrigin( rGeometryItem, fOriginX, fOriginY );
194 fOriginX = fOriginX * rSdrObjCustomShape.GetLogicRect().GetWidth();
195 fOriginY = fOriginY * rSdrObjCustomShape.GetLogicRect().GetHeight();
197 drawing::Position3D aViewPointDefault( 3472, -3472, 25000 );
198 drawing::Position3D aViewPoint( GetPosition3D( rGeometryItem, "ViewPoint", aViewPointDefault, pMap ) );
199 fViewPoint.setX(aViewPoint.PositionX);
200 fViewPoint.setY(aViewPoint.PositionY);
201 fViewPoint.setZ(-aViewPoint.PositionZ);
205 basegfx::B3DPolygon EnhancedCustomShape3d::Transformation2D::ApplySkewSettings( const basegfx::B3DPolygon& rPoly3D ) const
207 basegfx::B3DPolygon aRetval;
209 sal_uInt32 j;
210 for ( j = 0; j < rPoly3D.count(); j++ )
212 const basegfx::B3DPoint aPoint(rPoly3D.getB3DPoint(j));
213 double fDepth(-( aPoint.getZ() * fSkew ) / 100.0);
214 aRetval.append(basegfx::B3DPoint(
215 aPoint.getX() + (fDepth * cos( fSkewAngle )),
216 aPoint.getY() - (fDepth * sin( fSkewAngle )),
217 aPoint.getZ()));
220 return aRetval;
223 Point EnhancedCustomShape3d::Transformation2D::Transform2D( const basegfx::B3DPoint& rPoint3D ) const
225 Point aPoint2D;
226 if ( eProjectionMode == drawing::ProjectionMode_PARALLEL )
228 aPoint2D.setX( static_cast<sal_Int32>(rPoint3D.getX()) );
229 aPoint2D.setY( static_cast<sal_Int32>(rPoint3D.getY()) );
231 else
233 double fX = rPoint3D.getX() - fOriginX;
234 double fY = rPoint3D.getY() - fOriginY;
235 double f = ( - fViewPoint.getZ() ) / ( rPoint3D.getZ() - fViewPoint.getZ() );
236 aPoint2D.setX( static_cast<sal_Int32>(( fX - fViewPoint.getX() ) * f + fViewPoint.getX() + fOriginX ) );
237 aPoint2D.setY( static_cast<sal_Int32>(( fY - fViewPoint.getY() ) * f + fViewPoint.getY() + fOriginY ) );
239 aPoint2D.Move( aCenter.X(), aCenter.Y() );
240 return aPoint2D;
243 bool EnhancedCustomShape3d::Transformation2D::IsParallel() const
245 return eProjectionMode == css::drawing::ProjectionMode_PARALLEL;
248 SdrObject* EnhancedCustomShape3d::Create3DObject(
249 const SdrObject* pShape2d,
250 const SdrObjCustomShape& rSdrObjCustomShape)
252 SdrObject* pRet(nullptr);
253 const SdrCustomShapeGeometryItem& rGeometryItem(rSdrObjCustomShape.GetMergedItem(SDRATTR_CUSTOMSHAPE_GEOMETRY));
254 double fMap(1.0), *pMap = nullptr;
255 Fraction aFraction( rSdrObjCustomShape.getSdrModelFromSdrObject().GetScaleFraction() );
257 if ( aFraction.GetNumerator() != 1 || aFraction.GetDenominator() != 1 )
259 fMap *= double(aFraction);
260 pMap = &fMap;
263 if ( rSdrObjCustomShape.getSdrModelFromSdrObject().GetScaleUnit() != MapUnit::Map100thMM )
265 DBG_ASSERT( rSdrObjCustomShape.getSdrModelFromSdrObject().GetScaleUnit() == MapUnit::MapTwip, "EnhancedCustomShape3d::Current MapMode is Unsupported" );
266 // But we could use MapToO3tlUnit from <tools/UnitConversion> ... ?
267 fMap *= o3tl::convert(1.0, o3tl::Length::mm100, o3tl::Length::twip);
268 pMap = &fMap;
271 if ( GetBool( rGeometryItem, "Extrusion", false ) )
273 bool bIsMirroredX(rSdrObjCustomShape.IsMirroredX());
274 bool bIsMirroredY(rSdrObjCustomShape.IsMirroredY());
275 tools::Rectangle aSnapRect(rSdrObjCustomShape.GetLogicRect());
276 Degree100 nObjectRotation(rSdrObjCustomShape.GetRotateAngle());
277 if ( nObjectRotation )
279 double a = (36000 - nObjectRotation.get()) * F_PI18000;
280 tools::Long dx = aSnapRect.Right() - aSnapRect.Left();
281 tools::Long dy = aSnapRect.Bottom()- aSnapRect.Top();
282 Point aP( aSnapRect.TopLeft() );
283 RotatePoint( aP, rSdrObjCustomShape.GetSnapRect().Center(), sin( a ), cos( a ) );
284 aSnapRect.SetLeft( aP.X() );
285 aSnapRect.SetTop( aP.Y() );
286 aSnapRect.SetRight( aSnapRect.Left() + dx );
287 aSnapRect.SetBottom( aSnapRect.Top() + dy );
289 Point aCenter( aSnapRect.Center() );
291 SfxItemSet aSet( rSdrObjCustomShape.GetMergedItemSet() );
293 //SJ: vertical writing is not required, by removing this item no outliner is created
294 aSet.ClearItem( SDRATTR_TEXTDIRECTION );
296 // #i105323# For 3D AutoShapes, the shadow attribute has to be applied to each
297 // created visualisation helper model shape individually. The shadow itself
298 // will then be rendered from the 3D renderer correctly for the whole 3D scene
299 // (and thus behind all objects of which the visualisation may be built). So,
300 // do NOT remove it from the ItemSet here.
301 // aSet.ClearItem(SDRATTR_SHADOW);
303 std::vector< E3dCompoundObject* > aPlaceholderObjectList;
305 double fExtrusionBackward, fExtrusionForward;
306 GetExtrusionDepth( rGeometryItem, pMap, fExtrusionBackward, fExtrusionForward );
307 double fDepth = fExtrusionBackward + fExtrusionForward;
308 if ( fDepth < 1.0 )
309 fDepth = 1.0;
311 drawing::ProjectionMode eProjectionMode( drawing::ProjectionMode_PARALLEL );
312 const Any* pAny = rGeometryItem.GetPropertyValueByName( "Extrusion", "ProjectionMode" );
313 if ( pAny )
314 *pAny >>= eProjectionMode;
315 ProjectionType eProjectionType( eProjectionMode == drawing::ProjectionMode_PARALLEL ? ProjectionType::Parallel : ProjectionType::Perspective );
316 // pShape2d Convert in scenes which include 3D Objects
317 E3dDefaultAttributes a3DDefaultAttr;
318 a3DDefaultAttr.SetDefaultLatheCharacterMode( true );
319 a3DDefaultAttr.SetDefaultExtrudeCharacterMode( true );
321 E3dScene* pScene = new E3dScene(rSdrObjCustomShape.getSdrModelFromSdrObject());
323 bool bSceneHasObjects ( false );
324 bool bUseTwoFillStyles( false );
326 drawing::ShadeMode eShadeMode( GetShadeMode( rGeometryItem, drawing::ShadeMode_FLAT ) );
327 bool bUseExtrusionColor = GetBool( rGeometryItem, "Color", false );
329 drawing::FillStyle eFillStyle( aSet.Get(XATTR_FILLSTYLE).GetValue() );
330 pScene->GetProperties().SetObjectItem( Svx3DShadeModeItem( 0 ) );
331 aSet.Put( makeSvx3DPercentDiagonalItem( 0 ) );
332 aSet.Put( Svx3DTextureModeItem( 1 ) );
333 aSet.Put( Svx3DNormalsKindItem( 1 ) );
335 if ( eShadeMode == drawing::ShadeMode_DRAFT )
337 aSet.Put( XLineStyleItem( drawing::LineStyle_SOLID ) );
338 aSet.Put( XFillStyleItem ( drawing::FillStyle_NONE ) );
339 aSet.Put( makeSvx3DDoubleSidedItem( true ) );
341 else
343 aSet.Put( XLineStyleItem( drawing::LineStyle_NONE ) );
344 if ( eFillStyle == drawing::FillStyle_NONE )
345 aSet.Put( XFillStyleItem( drawing::FillStyle_SOLID ) );
346 else if ( ( eFillStyle == drawing::FillStyle_BITMAP ) || ( eFillStyle == drawing::FillStyle_GRADIENT ) || bUseExtrusionColor )
347 bUseTwoFillStyles = true;
349 // If shapes are mirrored once (mirroring two times correct geometry again)
350 // double-sided at the object and two-sided-lighting at the scene need to be set.
352 // #i122777# Also use double sided for two fill styles since there several 3d objects get
353 // created with a depth of 0; one of them is the backside which needs double-sided to
354 // get visible
355 if(bUseTwoFillStyles || (bIsMirroredX && !bIsMirroredY) || (!bIsMirroredX && bIsMirroredY))
357 aSet.Put( makeSvx3DDoubleSidedItem( true ) );
358 pScene->GetProperties().SetObjectItem( makeSvx3DTwoSidedLightingItem( true ) );
362 tools::Rectangle aBoundRect2d;
363 SdrObjListIter aIter( *pShape2d, SdrIterMode::DeepNoGroups );
364 const bool bMultipleSubObjects(aIter.Count() > 1);
366 while( aIter.IsMore() )
368 const SdrObject* pNext = aIter.Next();
369 bool bIsPlaceholderObject = (pNext->GetMergedItem( XATTR_FILLSTYLE ).GetValue() == drawing::FillStyle_NONE )
370 && (pNext->GetMergedItem( XATTR_LINESTYLE ).GetValue() == drawing::LineStyle_NONE );
371 basegfx::B2DPolyPolygon aPolyPoly;
372 SfxItemSet aLocalSet(aSet);
373 drawing::FillStyle aLocalFillStyle(eFillStyle);
375 if ( auto pPathObj = dynamic_cast<const SdrPathObj*>(pNext) )
377 const SfxItemSet& rSet = pNext->GetMergedItemSet();
378 bool bNeedToConvertToContour(false);
380 // do conversion only for single line objects; for all others a fill and a
381 // line object get created. When we have fill, we want no line. That line has
382 // always been there, but since it was never converted to contour, it kept
383 // invisible (all this 'hidden' logic should be migrated to primitives).
384 if(!bMultipleSubObjects)
386 const drawing::FillStyle eStyle(rSet.Get(XATTR_FILLSTYLE).GetValue());
388 if(drawing::FillStyle_NONE == eStyle)
390 const drawinglayer::attribute::SdrLineAttribute aLine(
391 drawinglayer::primitive2d::createNewSdrLineAttribute(rSet));
393 bNeedToConvertToContour = (0.0 < aLine.getWidth() || 0.0 != aLine.getFullDotDashLen());
395 if(!bNeedToConvertToContour && !aLine.isDefault())
397 const drawinglayer::attribute::SdrLineStartEndAttribute aLineStartEnd(
398 drawinglayer::primitive2d::createNewSdrLineStartEndAttribute(rSet, aLine.getWidth()));
400 if((aLineStartEnd.getStartWidth() && aLineStartEnd.isStartActive())
401 || (aLineStartEnd.getEndWidth() && aLineStartEnd.isEndActive()))
403 bNeedToConvertToContour = true;
409 if(bNeedToConvertToContour)
411 SdrObject* pNewObj = pNext->ConvertToContourObj(const_cast< SdrObject* >(pNext));
412 SdrPathObj* pNewPathObj = dynamic_cast< SdrPathObj* >(pNewObj);
414 if(pNewPathObj)
416 aPolyPoly = pNewPathObj->GetPathPoly();
418 if(aPolyPoly.isClosed())
420 // correct item properties from line to fill style
421 if(eShadeMode == drawing::ShadeMode_DRAFT)
423 // for draft, create wireframe with fixed line width
424 aLocalSet.Put(XLineStyleItem(drawing::LineStyle_SOLID));
425 aLocalSet.Put(XLineWidthItem(40));
426 aLocalFillStyle = drawing::FillStyle_NONE;
428 else
430 // switch from line to fill, copy line attr to fill attr (color, transparence)
431 aLocalSet.Put(XLineWidthItem(0));
432 aLocalSet.Put(XLineStyleItem(drawing::LineStyle_NONE));
433 aLocalSet.Put(XFillColorItem(OUString(), aLocalSet.Get(XATTR_LINECOLOR).GetColorValue()));
434 aLocalSet.Put(XFillStyleItem(drawing::FillStyle_SOLID));
435 aLocalSet.Put(XFillTransparenceItem(aLocalSet.Get(XATTR_LINETRANSPARENCE).GetValue()));
436 aLocalFillStyle = drawing::FillStyle_SOLID;
439 else
441 // correct item properties to hairlines
442 aLocalSet.Put(XLineWidthItem(0));
443 aLocalSet.Put(XLineStyleItem(drawing::LineStyle_SOLID));
447 SdrObject::Free(pNewObj);
449 else
451 aPolyPoly = pPathObj->GetPathPoly();
454 else
456 SdrObjectUniquePtr pNewObj = pNext->ConvertToPolyObj( false, false );
457 SdrPathObj* pPath = dynamic_cast<SdrPathObj*>( pNewObj.get() );
458 if ( pPath )
459 aPolyPoly = pPath->GetPathPoly();
462 if( aPolyPoly.count() )
464 if(aPolyPoly.areControlPointsUsed())
466 aPolyPoly = basegfx::utils::adaptiveSubdivideByAngle(aPolyPoly);
469 const basegfx::B2DRange aTempRange(basegfx::utils::getRange(aPolyPoly));
470 const tools::Rectangle aBoundRect(basegfx::fround(aTempRange.getMinX()), basegfx::fround(aTempRange.getMinY()), basegfx::fround(aTempRange.getMaxX()), basegfx::fround(aTempRange.getMaxY()));
471 aBoundRect2d.Union( aBoundRect );
473 // #i122777# depth 0 is okay for planes when using double-sided
474 E3dCompoundObject* p3DObj = new E3dExtrudeObj(
475 rSdrObjCustomShape.getSdrModelFromSdrObject(),
476 a3DDefaultAttr,
477 aPolyPoly,
478 bUseTwoFillStyles ? 0 : fDepth );
480 p3DObj->NbcSetLayer( pShape2d->GetLayer() );
481 p3DObj->SetMergedItemSet( aLocalSet );
483 if ( bIsPlaceholderObject )
484 aPlaceholderObjectList.push_back( p3DObj );
485 else if ( bUseTwoFillStyles )
487 BitmapEx aFillBmp;
488 bool bFillBmpTile = p3DObj->GetMergedItem( XATTR_FILLBMP_TILE ).GetValue();
489 if ( bFillBmpTile )
491 const XFillBitmapItem& rBmpItm = p3DObj->GetMergedItem(XATTR_FILLBITMAP);
492 aFillBmp = rBmpItm.GetGraphicObject().GetGraphic().GetBitmapEx();
494 // #i122777# old adaptation of FillStyle bitmap size to 5-times the original size; this is not needed
495 // anymore and was used in old times to male the fill look better when converting to 3D. Removed
496 // from regular 3D objects for some time, also needs to be removed from CustomShapes
498 //Size aLogicalSize = aFillBmp.GetPrefSize();
499 //if ( aFillBmp.GetPrefMapMode() == MapUnit::MapPixel )
500 // aLogicalSize = Application::GetDefaultDevice()->PixelToLogic( aLogicalSize, MapUnit::Map100thMM );
501 //else
502 // aLogicalSize = OutputDevice::LogicToLogic( aLogicalSize, aFillBmp.GetPrefMapMode(), MapUnit::Map100thMM );
503 //aLogicalSize.Width() *= 5; ;// :-( nice scaling, look at engine3d/obj3d.cxx
504 //aLogicalSize.Height() *= 5;
505 //aFillBmp.SetPrefSize( aLogicalSize );
506 //aFillBmp.SetPrefMapMode( MapUnit::Map100thMM );
507 //p3DObj->SetMergedItem(XFillBitmapItem(String(), Graphic(aFillBmp)));
509 else
511 if ( aSnapRect != aBoundRect && aSnapRect.GetWidth() > 0 && aSnapRect.GetHeight() > 0)
513 const XFillBitmapItem& rBmpItm = p3DObj->GetMergedItem(XATTR_FILLBITMAP);
514 aFillBmp = rBmpItm.GetGraphicObject().GetGraphic().GetBitmapEx();
515 Size aBmpSize( aFillBmp.GetSizePixel() );
516 double fXScale = static_cast<double>(aBoundRect.GetWidth()) / static_cast<double>(aSnapRect.GetWidth());
517 double fYScale = static_cast<double>(aBoundRect.GetHeight()) / static_cast<double>(aSnapRect.GetHeight());
519 Point aPt( static_cast<sal_Int32>( static_cast<double>( aBoundRect.Left() - aSnapRect.Left() )* static_cast<double>(aBmpSize.Width()) / static_cast<double>(aSnapRect.GetWidth()) ),
520 static_cast<sal_Int32>( static_cast<double>( aBoundRect.Top() - aSnapRect.Top() ) * static_cast<double>(aBmpSize.Height()) / static_cast<double>(aSnapRect.GetHeight()) ) );
521 Size aSize( static_cast<sal_Int32>( aBmpSize.Width() * fXScale ),
522 static_cast<sal_Int32>( aBmpSize.Height() * fYScale ) );
523 tools::Rectangle aCropRect( aPt, aSize );
524 aFillBmp.Crop( aCropRect );
525 p3DObj->SetMergedItem(XFillBitmapItem(OUString(), Graphic(aFillBmp)));
528 pScene->InsertObject( p3DObj );
529 p3DObj = new E3dExtrudeObj(
530 rSdrObjCustomShape.getSdrModelFromSdrObject(),
531 a3DDefaultAttr,
532 aPolyPoly,
533 fDepth);
534 p3DObj->NbcSetLayer( pShape2d->GetLayer() );
535 p3DObj->SetMergedItemSet( aLocalSet );
536 if ( bUseExtrusionColor )
537 p3DObj->SetMergedItem( XFillColorItem( "", rSdrObjCustomShape.GetMergedItem( XATTR_SECONDARYFILLCOLOR ).GetColorValue() ) );
538 p3DObj->SetMergedItem( XFillStyleItem( drawing::FillStyle_SOLID ) );
539 p3DObj->SetMergedItem( Svx3DCloseFrontItem( false ) );
540 p3DObj->SetMergedItem( Svx3DCloseBackItem( false ) );
541 pScene->InsertObject( p3DObj );
543 // #i122777# depth 0 is okay for planes when using double-sided
544 p3DObj = new E3dExtrudeObj(
545 rSdrObjCustomShape.getSdrModelFromSdrObject(),
546 a3DDefaultAttr,
547 aPolyPoly,
550 p3DObj->NbcSetLayer( pShape2d->GetLayer() );
551 p3DObj->SetMergedItemSet( aLocalSet );
553 basegfx::B3DHomMatrix aFrontTransform( p3DObj->GetTransform() );
554 aFrontTransform.translate( 0.0, 0.0, fDepth );
555 p3DObj->NbcSetTransform( aFrontTransform );
557 if ( ( aLocalFillStyle == drawing::FillStyle_BITMAP ) && !aFillBmp.IsEmpty() )
559 p3DObj->SetMergedItem(XFillBitmapItem(OUString(), Graphic(aFillBmp)));
562 else if ( aLocalFillStyle == drawing::FillStyle_NONE )
564 const XLineColorItem& rLineColor = p3DObj->GetMergedItem( XATTR_LINECOLOR );
565 p3DObj->SetMergedItem( XFillColorItem( "", rLineColor.GetColorValue() ) );
566 p3DObj->SetMergedItem( makeSvx3DDoubleSidedItem( true ) );
567 p3DObj->SetMergedItem( Svx3DCloseFrontItem( false ) );
568 p3DObj->SetMergedItem( Svx3DCloseBackItem( false ) );
570 pScene->InsertObject( p3DObj );
571 bSceneHasObjects = true;
575 if ( bSceneHasObjects ) // is the SdrObject properly converted
577 // then we can change the return value
578 pRet = pScene;
580 // Camera settings, Perspective ...
581 Camera3D rCamera = pScene->GetCamera();
582 const basegfx::B3DRange& rVolume = pScene->GetBoundVolume();
583 pScene->NbcSetSnapRect( aSnapRect );
585 // InitScene replacement
586 double fW = rVolume.getWidth();
587 double fH = rVolume.getHeight();
589 rCamera.SetAutoAdjustProjection( false );
590 rCamera.SetViewWindow( -fW / 2, - fH / 2, fW, fH);
591 basegfx::B3DPoint aLookAt( 0.0, 0.0, 0.0 );
592 basegfx::B3DPoint aCamPos( 0.0, 0.0, 100.0 );
593 rCamera.SetPosAndLookAt( aCamPos, aLookAt );
594 rCamera.SetFocalLength( 1.0 );
595 rCamera.SetProjection( eProjectionType );
596 pScene->SetCamera( rCamera );
597 pScene->SetRectsDirty();
599 double fOriginX, fOriginY;
600 GetOrigin( rGeometryItem, fOriginX, fOriginY );
601 fOriginX = fOriginX * aSnapRect.GetWidth();
602 fOriginY = fOriginY * aSnapRect.GetHeight();
604 basegfx::B3DHomMatrix aNewTransform( pScene->GetTransform() );
605 aNewTransform.translate( -aCenter.X(), aCenter.Y(), -pScene->GetBoundVolume().getDepth() );
607 double fXRotate, fYRotate;
608 GetRotateAngle( rGeometryItem, fXRotate, fYRotate );
609 double fZRotate(basegfx::deg2rad(rSdrObjCustomShape.GetObjectRotation()));
610 if ( fZRotate != 0.0 )
611 aNewTransform.rotate( 0.0, 0.0, fZRotate );
612 if ( bIsMirroredX )
613 aNewTransform.scale( -1.0, 1, 1 );
614 if ( bIsMirroredY )
615 aNewTransform.scale( 1, -1.0, 1 );
616 if( fYRotate != 0.0 )
617 aNewTransform.rotate( 0.0, -fYRotate, 0.0 );
618 if( fXRotate != 0.0 )
619 aNewTransform.rotate( -fXRotate, 0.0, 0.0 );
620 if ( eProjectionType == ProjectionType::Parallel )
622 double fSkew, fAlpha;
623 GetSkew( rGeometryItem, fSkew, fAlpha );
624 if ( fSkew != 0.0 )
626 double fInvTanBeta( fSkew / 100.0 );
627 if(fInvTanBeta)
629 aNewTransform.shearXY(
630 fInvTanBeta * cos(fAlpha),
631 fInvTanBeta * sin(fAlpha));
634 basegfx::B3DPoint _aLookAt( 0.0, 0.0, 0.0 );
635 basegfx::B3DPoint _aNewCamPos( 0.0, 0.0, 25000.0 );
636 rCamera.SetPosAndLookAt( _aNewCamPos, _aLookAt );
637 pScene->SetCamera( rCamera );
639 else
641 aNewTransform.translate( -fOriginX, fOriginY, 0.0 );
642 // now set correct camera position
643 drawing::Position3D aViewPointDefault( 3472, -3472, 25000 );
644 drawing::Position3D aViewPoint( GetPosition3D( rGeometryItem, "ViewPoint", aViewPointDefault, pMap ) );
645 double fViewPointX = aViewPoint.PositionX;
646 double fViewPointY = aViewPoint.PositionY;
647 double fViewPointZ = aViewPoint.PositionZ;
648 basegfx::B3DPoint _aLookAt( fViewPointX, -fViewPointY, 0.0 );
649 basegfx::B3DPoint aNewCamPos( fViewPointX, -fViewPointY, fViewPointZ );
650 rCamera.SetPosAndLookAt( aNewCamPos, _aLookAt );
651 pScene->SetCamera( rCamera );
654 pScene->NbcSetTransform( aNewTransform );
657 // light
659 double fAmbientIntensity = GetDouble( rGeometryItem, "Brightness", 22178.0 / 655.36 ) / 100.0;
661 drawing::Direction3D aFirstLightDirectionDefault( 50000, 0, 10000 );
662 drawing::Direction3D aFirstLightDirection( GetDirection3D( rGeometryItem, "FirstLightDirection", aFirstLightDirectionDefault ) );
663 if ( aFirstLightDirection.DirectionZ == 0.0 )
664 aFirstLightDirection.DirectionZ = 1.0;
666 double fLightIntensity = GetDouble( rGeometryItem, "FirstLightLevel", 43712.0 / 655.36 ) / 100.0;
668 /* sal_Bool bFirstLightHarsh = */ GetBool( rGeometryItem, "FirstLightHarsh", false );
670 drawing::Direction3D aSecondLightDirectionDefault( -50000, 0, 10000 );
671 drawing::Direction3D aSecondLightDirection( GetDirection3D( rGeometryItem, "SecondLightDirection", aSecondLightDirectionDefault ) );
672 if ( aSecondLightDirection.DirectionZ == 0.0 )
673 aSecondLightDirection.DirectionZ = -1;
675 double fLight2Intensity = GetDouble( rGeometryItem, "SecondLightLevel", 43712.0 / 655.36 ) / 100.0;
677 /* sal_Bool bLight2Harsh = */ GetBool( rGeometryItem, "SecondLightHarsh", false );
678 /* sal_Bool bLightFace = */ GetBool( rGeometryItem, "LightFace", false );
680 sal_uInt16 nAmbientColor = static_cast<sal_uInt16>( fAmbientIntensity * 255.0 );
681 if ( nAmbientColor > 255 )
682 nAmbientColor = 255;
683 Color aGlobalAmbientColor( static_cast<sal_uInt8>(nAmbientColor), static_cast<sal_uInt8>(nAmbientColor), static_cast<sal_uInt8>(nAmbientColor) );
684 pScene->GetProperties().SetObjectItem( makeSvx3DAmbientcolorItem( aGlobalAmbientColor ) );
686 sal_uInt8 nSpotLight1 = static_cast<sal_uInt8>( fLightIntensity * 255.0 );
687 basegfx::B3DVector aSpotLight1( aFirstLightDirection.DirectionX, - ( aFirstLightDirection.DirectionY ), -( aFirstLightDirection.DirectionZ ) );
688 aSpotLight1.normalize();
689 pScene->GetProperties().SetObjectItem( makeSvx3DLightOnOff1Item( true ) );
690 Color aAmbientSpot1Color( nSpotLight1, nSpotLight1, nSpotLight1 );
691 pScene->GetProperties().SetObjectItem( makeSvx3DLightcolor1Item( aAmbientSpot1Color ) );
692 pScene->GetProperties().SetObjectItem( makeSvx3DLightDirection1Item( aSpotLight1 ) );
694 sal_uInt8 nSpotLight2 = static_cast<sal_uInt8>( fLight2Intensity * 255.0 );
695 basegfx::B3DVector aSpotLight2( aSecondLightDirection.DirectionX, -aSecondLightDirection.DirectionY, -aSecondLightDirection.DirectionZ );
696 aSpotLight2.normalize();
697 pScene->GetProperties().SetObjectItem( makeSvx3DLightOnOff2Item( true ) );
698 Color aAmbientSpot2Color( nSpotLight2, nSpotLight2, nSpotLight2 );
699 pScene->GetProperties().SetObjectItem( makeSvx3DLightcolor2Item( aAmbientSpot2Color ) );
700 pScene->GetProperties().SetObjectItem( makeSvx3DLightDirection2Item( aSpotLight2 ) );
702 sal_uInt8 nSpotLight3 = 70;
703 basegfx::B3DVector aSpotLight3( 0.0, 0.0, 1.0 );
704 pScene->GetProperties().SetObjectItem( makeSvx3DLightOnOff3Item( true ) );
705 Color aAmbientSpot3Color( nSpotLight3, nSpotLight3, nSpotLight3 );
706 pScene->GetProperties().SetObjectItem( makeSvx3DLightcolor3Item( aAmbientSpot3Color ) );
707 pScene->GetProperties().SetObjectItem( makeSvx3DLightDirection3Item( aSpotLight3 ) );
709 double fSpecular = GetDouble( rGeometryItem, "Specularity", 0 ) / 100;
710 bool bMetal = GetBool( rGeometryItem, "Metal", false );
712 Color aSpecularCol( 225,225,225 );
713 if ( bMetal )
715 aSpecularCol = Color( 200, 200, 200 );
716 fSpecular += 0.15;
718 sal_Int32 nIntensity = static_cast<sal_Int32>(fSpecular) * 100;
719 if ( nIntensity > 100 )
720 nIntensity = 100;
721 else if ( nIntensity < 0 )
722 nIntensity = 0;
723 nIntensity = 100 - nIntensity;
724 pScene->GetProperties().SetObjectItem( makeSvx3DMaterialSpecularItem( aSpecularCol ) );
725 pScene->GetProperties().SetObjectItem( makeSvx3DMaterialSpecularIntensityItem( static_cast<sal_uInt16>(nIntensity) ) );
727 pScene->SetLogicRect(
728 CalculateNewSnapRect(
729 rSdrObjCustomShape,
730 aSnapRect,
731 aBoundRect2d,
732 pMap));
734 // removing placeholder objects
735 for (E3dCompoundObject* pTemp : aPlaceholderObjectList)
737 pScene->RemoveObject( pTemp->GetOrdNum() );
738 // always use SdrObject::Free(...) for SdrObjects (!)
739 SdrObject* pTemp2(pTemp);
740 SdrObject::Free(pTemp2);
743 else
745 // always use SdrObject::Free(...) for SdrObjects (!)
746 SdrObject* pTemp(pScene);
747 SdrObject::Free(pTemp);
750 return pRet;
753 tools::Rectangle EnhancedCustomShape3d::CalculateNewSnapRect(
754 const SdrObjCustomShape& rSdrObjCustomShape,
755 const tools::Rectangle& rSnapRect,
756 const tools::Rectangle& rBoundRect,
757 const double* pMap)
759 const SdrCustomShapeGeometryItem& rGeometryItem(rSdrObjCustomShape.GetMergedItem( SDRATTR_CUSTOMSHAPE_GEOMETRY ));
760 const Point aCenter( rSnapRect.Center() );
761 double fExtrusionBackward, fExtrusionForward;
762 GetExtrusionDepth( rGeometryItem, pMap, fExtrusionBackward, fExtrusionForward );
763 sal_uInt32 i;
765 // creating initial bound volume ( without rotation. skewing.and camera )
766 basegfx::B3DPolygon aBoundVolume;
767 const tools::Polygon aPolygon( rBoundRect );
769 for ( i = 0; i < 4; i++ )
771 aBoundVolume.append(basegfx::B3DPoint(aPolygon[ static_cast<sal_uInt16>(i) ].X() - aCenter.X(), aPolygon[ static_cast<sal_uInt16>(i) ].Y() - aCenter.Y(), -fExtrusionForward));
774 for ( i = 0; i < 4; i++ )
776 aBoundVolume.append(basegfx::B3DPoint(aPolygon[ static_cast<sal_uInt16>(i) ].X() - aCenter.X(), aPolygon[ static_cast<sal_uInt16>(i) ].Y() - aCenter.Y(), fExtrusionBackward));
779 drawing::Direction3D aRotationCenterDefault( 0, 0, 0 ); // default seems to be wrong, a fractional size of shape has to be used!!
780 drawing::Direction3D aRotationCenter( GetDirection3D( rGeometryItem, "RotationCenter", aRotationCenterDefault ) );
782 double fXRotate, fYRotate;
783 GetRotateAngle( rGeometryItem, fXRotate, fYRotate );
784 double fZRotate(basegfx::deg2rad(rSdrObjCustomShape.GetObjectRotation()));
786 // rotating bound volume
787 basegfx::B3DHomMatrix aMatrix;
788 aMatrix.translate(-aRotationCenter.DirectionX, -aRotationCenter.DirectionY, -aRotationCenter.DirectionZ);
789 if ( fZRotate != 0.0 )
790 aMatrix.rotate( 0.0, 0.0, fZRotate );
791 if (rSdrObjCustomShape.IsMirroredX())
792 aMatrix.scale( -1.0, 1, 1 );
793 if (rSdrObjCustomShape.IsMirroredY())
794 aMatrix.scale( 1, -1.0, 1 );
795 if( fYRotate != 0.0 )
796 aMatrix.rotate( 0.0, fYRotate, 0.0 );
797 if( fXRotate != 0.0 )
798 aMatrix.rotate( -fXRotate, 0.0, 0.0 );
799 aMatrix.translate(aRotationCenter.DirectionX, aRotationCenter.DirectionY, aRotationCenter.DirectionZ);
800 aBoundVolume.transform(aMatrix);
802 Transformation2D aTransformation2D(
803 rSdrObjCustomShape,
804 pMap);
806 if ( aTransformation2D.IsParallel() )
807 aBoundVolume = aTransformation2D.ApplySkewSettings( aBoundVolume );
809 tools::Polygon aTransformed( 8 );
810 for ( i = 0; i < 8; i++ )
811 aTransformed[ static_cast<sal_uInt16>(i) ] = aTransformation2D.Transform2D( aBoundVolume.getB3DPoint( i ) );
813 return aTransformed.GetBoundRect();
816 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */