lok: Don't attempt to select the exact text after a failed search.
[LibreOffice.git] / svx / source / customshapes / EnhancedCustomShape3d.cxx
blobbe54d216df10c2dd512d8d5b6444e50ae8aa52cd
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 <svx/svdetc.hxx>
22 #include <svx/svdmodel.hxx>
23 #include <tools/poly.hxx>
24 #include <svx/svditer.hxx>
25 #include <svx/svdobj.hxx>
26 #include <svx/svdoashp.hxx>
27 #include <svl/poolitem.hxx>
28 #include <svl/itemset.hxx>
29 #include <svx/xfillit0.hxx>
30 #include <svx/xsflclit.hxx>
31 #include <svx/xit.hxx>
32 #include <svx/xbtmpit.hxx>
33 #include <svx/xflclit.hxx>
34 #include <svx/svdopath.hxx>
35 #include <svx/svdogrp.hxx>
36 #include <svx/svdpage.hxx>
37 #include <svx/polysc3d.hxx>
38 #include <svx/svddef.hxx>
39 #include <svx/svx3ditems.hxx>
40 #include <svx/extrud3d.hxx>
41 #include <svx/xflbmtit.hxx>
42 #include <vcl/svapp.hxx>
43 #include <svx/xlnclit.hxx>
44 #include <svx/sdasitm.hxx>
45 #include <com/sun/star/awt/Point.hpp>
46 #include <com/sun/star/drawing/Position3D.hpp>
47 #include <com/sun/star/drawing/Direction3D.hpp>
48 #include <com/sun/star/drawing/ShadeMode.hpp>
49 #include <svx/sdr/properties/properties.hxx>
50 #include <com/sun/star/drawing/EnhancedCustomShapeParameterPair.hpp>
51 #include <basegfx/polygon/b2dpolypolygontools.hxx>
52 #include <basegfx/range/b2drange.hxx>
53 #include <svx/sdr/primitive2d/sdrattributecreator.hxx>
54 #include <drawinglayer/attribute/sdrlineattribute.hxx>
55 #include <drawinglayer/attribute/sdrlinestartendattribute.hxx>
56 #include <svx/xlnwtit.hxx>
57 #include <svx/xlntrit.hxx>
58 #include <svx/xfltrit.hxx>
60 #define ITEMVALUE(ItemSet,Id,Cast) (static_cast<const Cast&>((ItemSet).Get(Id))).GetValue()
61 using namespace com::sun::star;
62 using namespace com::sun::star::uno;
64 namespace {
66 void GetOrigin( const SdrCustomShapeGeometryItem& rItem, double& rOriginX, double& rOriginY )
68 ::com::sun::star::drawing::EnhancedCustomShapeParameterPair aOriginParaPair;
69 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "Origin" );
70 if ( ! ( pAny && ( *pAny >>= aOriginParaPair ) && ( aOriginParaPair.First.Value >>= rOriginX ) && ( aOriginParaPair.Second.Value >>= rOriginY ) ) )
72 rOriginX = 0.50;
73 rOriginY =-0.50;
77 void GetRotateAngle( const SdrCustomShapeGeometryItem& rItem, double& rAngleX, double& rAngleY )
79 ::com::sun::star::drawing::EnhancedCustomShapeParameterPair aRotateAngleParaPair;
80 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "RotateAngle" );
81 if ( ! ( pAny && ( *pAny >>= aRotateAngleParaPair ) && ( aRotateAngleParaPair.First.Value >>= rAngleX ) && ( aRotateAngleParaPair.Second.Value >>= rAngleY ) ) )
83 rAngleX = 0.0;
84 rAngleY = 0.0;
86 rAngleX *= F_PI180;
87 rAngleY *= F_PI180;
90 void GetSkew( const SdrCustomShapeGeometryItem& rItem, double& rSkewAmount, double& rSkewAngle )
92 ::com::sun::star::drawing::EnhancedCustomShapeParameterPair aSkewParaPair;
93 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "Skew" );
94 if ( ! ( pAny && ( *pAny >>= aSkewParaPair ) && ( aSkewParaPair.First.Value >>= rSkewAmount ) && ( aSkewParaPair.Second.Value >>= rSkewAngle ) ) )
96 rSkewAmount = 50;
97 rSkewAngle = -135;
99 rSkewAngle *= F_PI180;
102 void GetExtrusionDepth( const SdrCustomShapeGeometryItem& rItem, const double* pMap, double& rBackwardDepth, double& rForwardDepth )
104 ::com::sun::star::drawing::EnhancedCustomShapeParameterPair aDepthParaPair;
105 double fDepth = 0, fFraction = 0;
106 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "Depth" );
107 if ( pAny && ( *pAny >>= aDepthParaPair ) && ( aDepthParaPair.First.Value >>= fDepth ) && ( aDepthParaPair.Second.Value >>= fFraction ) )
109 rForwardDepth = fDepth * fFraction;
110 rBackwardDepth = fDepth - rForwardDepth;
112 else
114 rBackwardDepth = 1270;
115 rForwardDepth = 0;
117 if ( pMap )
119 double fMap = *pMap;
120 rBackwardDepth *= fMap;
121 rForwardDepth *= fMap;
125 double GetDouble( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName, double fDefault, const double* pMap )
127 double fRetValue = fDefault;
128 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", rPropertyName );
129 if ( pAny )
130 *pAny >>= fRetValue;
131 if ( pMap )
132 fRetValue *= *pMap;
133 return fRetValue;
136 drawing::ShadeMode GetShadeMode( const SdrCustomShapeGeometryItem& rItem, const drawing::ShadeMode eDefault )
138 drawing::ShadeMode eRet( eDefault );
139 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", "ShadeMode" );
140 if ( pAny )
141 *pAny >>= eRet;
142 return eRet;
145 bool GetBool( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName, const bool bDefault )
147 bool bRetValue = bDefault;
148 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", rPropertyName );
149 if ( pAny )
150 *pAny >>= bRetValue;
151 return bRetValue;
154 drawing::Position3D GetPosition3D( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName,
155 const drawing::Position3D& rDefault, const double* pMap )
157 drawing::Position3D aRetValue( rDefault );
158 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", rPropertyName );
159 if ( pAny )
160 *pAny >>= aRetValue;
161 if ( pMap )
163 aRetValue.PositionX *= *pMap;
164 aRetValue.PositionY *= *pMap;
165 aRetValue.PositionZ *= *pMap;
167 return aRetValue;
170 drawing::Direction3D GetDirection3D( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName, const drawing::Direction3D& rDefault )
172 drawing::Direction3D aRetValue( rDefault );
173 const Any* pAny = rItem.GetPropertyValueByName( "Extrusion", rPropertyName );
174 if ( pAny )
175 *pAny >>= aRetValue;
176 return aRetValue;
181 EnhancedCustomShape3d::Transformation2D::Transformation2D( const SdrObject* pCustomShape, const Rectangle& /*rBoundRect*/, const double *pM )
182 : aCenter( pCustomShape->GetSnapRect().Center() )
183 , eProjectionMode( drawing::ProjectionMode_PARALLEL )
184 , fSkewAngle(0.0)
185 , fSkew(0.0)
186 , fZScreen(0.0)
187 , fOriginX(0.0)
188 , fOriginY(0.0)
189 , pMap( pM )
191 const SdrCustomShapeGeometryItem& rGeometryItem = static_cast<const SdrCustomShapeGeometryItem&>(pCustomShape->GetMergedItem( SDRATTR_CUSTOMSHAPE_GEOMETRY ));
192 const Any* pAny = rGeometryItem.GetPropertyValueByName( "Extrusion", "ProjectionMode" );
193 if ( pAny )
194 *pAny >>= eProjectionMode;
196 if ( eProjectionMode == drawing::ProjectionMode_PARALLEL )
197 GetSkew( rGeometryItem, fSkew, fSkewAngle );
198 else
200 GetOrigin( rGeometryItem, fOriginX, fOriginY );
201 fOriginX = fOriginX * pCustomShape->GetLogicRect().GetWidth();
202 fOriginY = fOriginY * pCustomShape->GetLogicRect().GetHeight();
204 drawing::Position3D aViewPointDefault( 3472, -3472, 25000 );
205 drawing::Position3D aViewPoint( GetPosition3D( rGeometryItem, "ViewPoint", aViewPointDefault, pMap ) );
206 fViewPoint.setX(aViewPoint.PositionX);
207 fViewPoint.setY(aViewPoint.PositionY);
208 fViewPoint.setZ(-aViewPoint.PositionZ);
212 basegfx::B3DPolygon EnhancedCustomShape3d::Transformation2D::ApplySkewSettings( const basegfx::B3DPolygon& rPoly3D ) const
214 basegfx::B3DPolygon aRetval;
216 sal_uInt32 j;
217 for ( j = 0L; j < rPoly3D.count(); j++ )
219 const basegfx::B3DPoint aPoint(rPoly3D.getB3DPoint(j));
220 double fDepth(-( aPoint.getZ() * fSkew ) / 100.0);
221 aRetval.append(basegfx::B3DPoint(
222 aPoint.getX() + (fDepth * cos( fSkewAngle )),
223 aPoint.getY() - (fDepth * sin( fSkewAngle )),
224 aPoint.getZ()));
227 return aRetval;
230 Point EnhancedCustomShape3d::Transformation2D::Transform2D( const basegfx::B3DPoint& rPoint3D ) const
232 Point aPoint2D;
233 if ( eProjectionMode == drawing::ProjectionMode_PARALLEL )
235 aPoint2D.X() = (sal_Int32)rPoint3D.getX();
236 aPoint2D.Y() = (sal_Int32)rPoint3D.getY();
238 else
240 double fX = rPoint3D.getX() - fOriginX;
241 double fY = rPoint3D.getY() - fOriginY;
242 double f = ( fZScreen - fViewPoint.getZ() ) / ( rPoint3D.getZ() - fViewPoint.getZ() );
243 aPoint2D.X() = (sal_Int32)(( fX - fViewPoint.getX() ) * f + fViewPoint.getX() + fOriginX );
244 aPoint2D.Y() = (sal_Int32)(( fY - fViewPoint.getY() ) * f + fViewPoint.getY() + fOriginY );
246 aPoint2D.Move( aCenter.X(), aCenter.Y() );
247 return aPoint2D;
250 bool EnhancedCustomShape3d::Transformation2D::IsParallel() const
252 return eProjectionMode == com::sun::star::drawing::ProjectionMode_PARALLEL;
255 SdrObject* EnhancedCustomShape3d::Create3DObject( const SdrObject* pShape2d, const SdrObject* pCustomShape )
257 SdrObject* pRet = NULL;
258 SdrModel* pModel = pCustomShape->GetModel();
259 const SdrCustomShapeGeometryItem& rGeometryItem = static_cast<const SdrCustomShapeGeometryItem&>(pCustomShape->GetMergedItem( SDRATTR_CUSTOMSHAPE_GEOMETRY ));
261 double fMap, *pMap = NULL;
262 if ( pModel )
264 fMap = 1.0;
265 Fraction aFraction( pModel->GetScaleFraction() );
266 if ( ( aFraction.GetNumerator() ) != 1 || ( aFraction.GetDenominator() != 1 ) )
268 fMap *= aFraction.GetNumerator();
269 fMap /= aFraction.GetDenominator();
270 pMap = &fMap;
272 if ( pModel->GetScaleUnit() != MAP_100TH_MM )
274 DBG_ASSERT( pModel->GetScaleUnit() == MAP_TWIP, "EnhancedCustomShape3d::Current MapMode is Unsupported" );
275 fMap *= 1440.0 / 2540.0;
276 pMap = &fMap;
279 if ( GetBool( rGeometryItem, "Extrusion", false ) )
281 bool bIsMirroredX = static_cast<const SdrObjCustomShape*>(pCustomShape)->IsMirroredX();
282 bool bIsMirroredY = static_cast<const SdrObjCustomShape*>(pCustomShape)->IsMirroredY();
283 Rectangle aSnapRect( pCustomShape->GetLogicRect() );
284 long nObjectRotation = pCustomShape->GetRotateAngle();
285 if ( nObjectRotation )
287 double a = ( 36000 - nObjectRotation ) * nPi180;
288 long dx = aSnapRect.Right() - aSnapRect.Left();
289 long dy = aSnapRect.Bottom()- aSnapRect.Top();
290 Point aP( aSnapRect.TopLeft() );
291 RotatePoint( aP, pCustomShape->GetSnapRect().Center(), sin( a ), cos( a ) );
292 aSnapRect.Left() = aP.X();
293 aSnapRect.Top() = aP.Y();
294 aSnapRect.Right() = aSnapRect.Left() + dx;
295 aSnapRect.Bottom() = aSnapRect.Top() + dy;
297 Point aCenter( aSnapRect.Center() );
299 SfxItemSet aSet( pCustomShape->GetMergedItemSet() );
301 //SJ: vertical writing is not required, by removing this item no outliner is created
302 aSet.ClearItem( SDRATTR_TEXTDIRECTION );
304 // #i105323# For 3D AutoShapes, the shadow attribute has to be applied to each
305 // created visualisation helper model shape individually. The shadow itself
306 // will then be rendered from the 3D renderer correctly for the whole 3D scene
307 // (and thus behind all objects of which the visualisation may be built). So,
308 // dio NOT remove it from the ItemSet here.
309 // aSet.ClearItem(SDRATTR_SHADOW);
311 std::vector< E3dCompoundObject* > aPlaceholderObjectList;
313 double fExtrusionBackward, fExtrusionForward;
314 GetExtrusionDepth( rGeometryItem, pMap, fExtrusionBackward, fExtrusionForward );
315 double fDepth = fExtrusionBackward - fExtrusionForward;
316 if ( fDepth < 1.0 )
317 fDepth = 1.0;
319 drawing::ProjectionMode eProjectionMode( drawing::ProjectionMode_PARALLEL );
320 const Any* pAny = rGeometryItem.GetPropertyValueByName( "Extrusion", "ProjectionMode" );
321 if ( pAny )
322 *pAny >>= eProjectionMode;
323 ProjectionType eProjectionType( eProjectionMode == drawing::ProjectionMode_PARALLEL ? PR_PARALLEL : PR_PERSPECTIVE );
324 // pShape2d Convert in scenes which include 3D Objects
325 E3dDefaultAttributes a3DDefaultAttr;
326 a3DDefaultAttr.SetDefaultLatheCharacterMode( true );
327 a3DDefaultAttr.SetDefaultExtrudeCharacterMode( true );
329 E3dScene* pScene = new E3dPolyScene( a3DDefaultAttr );
331 bool bSceneHasObjects ( false );
332 bool bUseTwoFillStyles( false );
334 drawing::ShadeMode eShadeMode( GetShadeMode( rGeometryItem, drawing::ShadeMode_FLAT ) );
335 bool bUseExtrusionColor = GetBool( rGeometryItem, "Color", false );
337 drawing::FillStyle eFillStyle( ITEMVALUE( aSet, XATTR_FILLSTYLE, XFillStyleItem ) );
338 pScene->GetProperties().SetObjectItem( Svx3DShadeModeItem( 0 ) );
339 aSet.Put( makeSvx3DPercentDiagonalItem( 0 ) );
340 aSet.Put( Svx3DTextureModeItem( 1 ) );
341 aSet.Put( Svx3DNormalsKindItem( 1 ) );
343 if ( eShadeMode == drawing::ShadeMode_DRAFT )
345 aSet.Put( XLineStyleItem( drawing::LineStyle_SOLID ) );
346 aSet.Put( XFillStyleItem ( drawing::FillStyle_NONE ) );
347 aSet.Put( makeSvx3DDoubleSidedItem( true ) );
349 else
351 aSet.Put( XLineStyleItem( drawing::LineStyle_NONE ) );
352 if ( eFillStyle == drawing::FillStyle_NONE )
353 aSet.Put( XFillStyleItem( drawing::FillStyle_SOLID ) );
354 else if ( ( eFillStyle == drawing::FillStyle_BITMAP ) || ( eFillStyle == drawing::FillStyle_GRADIENT ) || bUseExtrusionColor )
355 bUseTwoFillStyles = true;
357 // #116336#
358 // If shapes are mirrored once (mirroring two times correct geometry again)
359 // double-sided at the object and two-sided-lighting at the scene need to be set.
361 // #i122777# Also use double sided for two fill styles since there several 3d objects get
362 // created with a depth of 0; one of them is the backside which needs double-sided to
363 // get visible
364 if(bUseTwoFillStyles || (bIsMirroredX && !bIsMirroredY) || (!bIsMirroredX && bIsMirroredY))
366 aSet.Put( makeSvx3DDoubleSidedItem( true ) );
367 pScene->GetProperties().SetObjectItem( makeSvx3DTwoSidedLightingItem( true ) );
371 Rectangle aBoundRect2d;
372 SdrObjListIter aIter( *pShape2d, IM_DEEPNOGROUPS );
373 const bool bMultipleSubObjects(aIter.Count() > 1);
375 while( aIter.IsMore() )
377 const SdrObject* pNext = aIter.Next();
378 bool bIsPlaceholderObject = (static_cast<const XFillStyleItem&>(pNext->GetMergedItem( XATTR_FILLSTYLE )).GetValue() == drawing::FillStyle_NONE )
379 && (static_cast<const XLineStyleItem&>(pNext->GetMergedItem( XATTR_LINESTYLE )).GetValue() == drawing::LineStyle_NONE );
380 basegfx::B2DPolyPolygon aPolyPoly;
381 SfxItemSet aLocalSet(aSet);
382 drawing::FillStyle aLocalFillStyle(eFillStyle);
384 if ( pNext->ISA( SdrPathObj ) )
386 const SfxItemSet& rSet = pNext->GetMergedItemSet();
387 bool bNeedToConvertToContour(false);
389 // do conversion only for single line objects; for all others a fill and a
390 // line object get created. When we have fill, we want no line. That line has
391 // always been there, but since it was never converted to contour, it kept
392 // invisible (all this 'hidden' logic should be migrated to primitives).
393 if(!bMultipleSubObjects)
395 const drawing::FillStyle eStyle(static_cast<const XFillStyleItem&>(rSet.Get(XATTR_FILLSTYLE)).GetValue());
397 if(drawing::FillStyle_NONE == eStyle)
399 const drawinglayer::attribute::SdrLineAttribute aLine(
400 drawinglayer::primitive2d::createNewSdrLineAttribute(rSet));
402 bNeedToConvertToContour = (0.0 < aLine.getWidth() || 0.0 != aLine.getFullDotDashLen());
404 if(!bNeedToConvertToContour && !aLine.isDefault())
406 const drawinglayer::attribute::SdrLineStartEndAttribute aLineStartEnd(
407 drawinglayer::primitive2d::createNewSdrLineStartEndAttribute(rSet, aLine.getWidth()));
409 if((aLineStartEnd.getStartWidth() && aLineStartEnd.isStartActive())
410 || (aLineStartEnd.getEndWidth() && aLineStartEnd.isEndActive()))
412 bNeedToConvertToContour = true;
418 if(bNeedToConvertToContour)
420 SdrObject* pNewObj = pNext->ConvertToContourObj(const_cast< SdrObject* >(pNext));
421 SdrPathObj* pNewPathObj = dynamic_cast< SdrPathObj* >(pNewObj);
423 if(pNewPathObj)
425 aPolyPoly = pNewPathObj->GetPathPoly();
427 if(aPolyPoly.isClosed())
429 // correct item properties from line to fill style
430 if(eShadeMode == drawing::ShadeMode_DRAFT)
432 // for draft, create wireframe with fixed line width
433 aLocalSet.Put(XLineStyleItem(drawing::LineStyle_SOLID));
434 aLocalSet.Put(XLineWidthItem(40));
435 aLocalFillStyle = drawing::FillStyle_NONE;
437 else
439 // switch from line to fill, copy line attr to fill attr (color, transparence)
440 aLocalSet.Put(XLineWidthItem(0));
441 aLocalSet.Put(XLineStyleItem(drawing::LineStyle_NONE));
442 aLocalSet.Put(XFillColorItem(OUString(), static_cast<const XLineColorItem&>(aLocalSet.Get(XATTR_LINECOLOR)).GetColorValue()));
443 aLocalSet.Put(XFillStyleItem(drawing::FillStyle_SOLID));
444 aLocalSet.Put(XFillTransparenceItem(static_cast<const XLineTransparenceItem&>(aLocalSet.Get(XATTR_LINETRANSPARENCE)).GetValue()));
445 aLocalFillStyle = drawing::FillStyle_SOLID;
448 else
450 // correct item properties to hairlines
451 aLocalSet.Put(XLineWidthItem(0));
452 aLocalSet.Put(XLineStyleItem(drawing::LineStyle_SOLID));
456 SdrObject::Free(pNewObj);
458 else
460 aPolyPoly = static_cast<const SdrPathObj*>(pNext)->GetPathPoly();
463 else
465 SdrObject* pNewObj = pNext->ConvertToPolyObj( false, false );
466 SdrPathObj* pPath = PTR_CAST( SdrPathObj, pNewObj );
467 if ( pPath )
468 aPolyPoly = pPath->GetPathPoly();
469 SdrObject::Free( pNewObj );
472 if( aPolyPoly.count() )
474 if(aPolyPoly.areControlPointsUsed())
476 aPolyPoly = basegfx::tools::adaptiveSubdivideByAngle(aPolyPoly);
479 const basegfx::B2DRange aTempRange(basegfx::tools::getRange(aPolyPoly));
480 const Rectangle aBoundRect(basegfx::fround(aTempRange.getMinX()), basegfx::fround(aTempRange.getMinY()), basegfx::fround(aTempRange.getMaxX()), basegfx::fround(aTempRange.getMaxY()));
481 aBoundRect2d.Union( aBoundRect );
483 // #i122777# depth 0 is okay for planes when using double-sided
484 E3dCompoundObject* p3DObj = new E3dExtrudeObj( a3DDefaultAttr, aPolyPoly, bUseTwoFillStyles ? 0 : fDepth );
486 p3DObj->NbcSetLayer( pShape2d->GetLayer() );
487 p3DObj->SetMergedItemSet( aLocalSet );
488 if ( bIsPlaceholderObject )
489 aPlaceholderObjectList.push_back( p3DObj );
490 else if ( bUseTwoFillStyles )
492 BitmapEx aFillBmp;
493 bool bFillBmpTile = static_cast<const XFillBmpTileItem&>(p3DObj->GetMergedItem( XATTR_FILLBMP_TILE )).GetValue();
494 if ( bFillBmpTile )
496 const XFillBitmapItem& rBmpItm = static_cast<const XFillBitmapItem&>(p3DObj->GetMergedItem(XATTR_FILLBITMAP));
497 aFillBmp = rBmpItm.GetGraphicObject().GetGraphic().GetBitmapEx();
499 // #i122777# old adaption of FillStyle bitmap size to 5-times the original size; this is not needed
500 // anymore and was used in old times to male the fill look better when converting to 3D. Removed
501 // from regular 3D objects for some time, also needs to be removed from CustomShapes
503 //Size aLogicalSize = aFillBmp.GetPrefSize();
504 //if ( aFillBmp.GetPrefMapMode() == MAP_PIXEL )
505 // aLogicalSize = Application::GetDefaultDevice()->PixelToLogic( aLogicalSize, MAP_100TH_MM );
506 //else
507 // aLogicalSize = OutputDevice::LogicToLogic( aLogicalSize, aFillBmp.GetPrefMapMode(), MAP_100TH_MM );
508 //aLogicalSize.Width() *= 5; ;// :-( nice scaling, look at engine3d/obj3d.cxx
509 //aLogicalSize.Height() *= 5;
510 //aFillBmp.SetPrefSize( aLogicalSize );
511 //aFillBmp.SetPrefMapMode( MAP_100TH_MM );
512 //p3DObj->SetMergedItem(XFillBitmapItem(String(), Graphic(aFillBmp)));
514 else
516 if ( aSnapRect != aBoundRect && aSnapRect.GetWidth() > 0 && aSnapRect.GetHeight() > 0)
518 const XFillBitmapItem& rBmpItm = static_cast<const XFillBitmapItem&>(p3DObj->GetMergedItem(XATTR_FILLBITMAP));
519 aFillBmp = rBmpItm.GetGraphicObject().GetGraphic().GetBitmapEx();
520 Size aBmpSize( aFillBmp.GetSizePixel() );
521 double fXScale = (double)aBoundRect.GetWidth() / (double)aSnapRect.GetWidth();
522 double fYScale = (double)aBoundRect.GetHeight() / (double)aSnapRect.GetHeight();
524 Point aPt( (sal_Int32)( (double)( aBoundRect.Left() - aSnapRect.Left() )* (double)aBmpSize.Width() / (double)aSnapRect.GetWidth() ),
525 (sal_Int32)( (double)( aBoundRect.Top() - aSnapRect.Top() ) * (double)aBmpSize.Height() / (double)aSnapRect.GetHeight() ) );
526 Size aSize( (sal_Int32)( aBmpSize.Width() * fXScale ),
527 (sal_Int32)( aBmpSize.Height() * fYScale ) );
528 Rectangle aCropRect( aPt, aSize );
529 aFillBmp.Crop( aCropRect );
530 p3DObj->SetMergedItem(XFillBitmapItem(OUString(), Graphic(aFillBmp)));
533 pScene->Insert3DObj( p3DObj );
534 p3DObj = new E3dExtrudeObj( a3DDefaultAttr, aPolyPoly, fDepth );
535 p3DObj->NbcSetLayer( pShape2d->GetLayer() );
536 p3DObj->SetMergedItemSet( aLocalSet );
537 if ( bUseExtrusionColor )
538 p3DObj->SetMergedItem( XFillColorItem( "", static_cast<const XSecondaryFillColorItem&>(pCustomShape->GetMergedItem( XATTR_SECONDARYFILLCOLOR )).GetColorValue() ) );
539 p3DObj->SetMergedItem( XFillStyleItem( drawing::FillStyle_SOLID ) );
540 p3DObj->SetMergedItem( Svx3DCloseFrontItem( false ) );
541 p3DObj->SetMergedItem( Svx3DCloseBackItem( false ) );
542 pScene->Insert3DObj( p3DObj );
544 // #i122777# depth 0 is okay for planes when using double-sided
545 p3DObj = new E3dExtrudeObj( a3DDefaultAttr, aPolyPoly, 0 );
547 p3DObj->NbcSetLayer( pShape2d->GetLayer() );
548 p3DObj->SetMergedItemSet( aLocalSet );
550 basegfx::B3DHomMatrix aFrontTransform( p3DObj->GetTransform() );
551 aFrontTransform.translate( 0.0, 0.0, fDepth );
552 p3DObj->NbcSetTransform( aFrontTransform );
554 if ( ( aLocalFillStyle == drawing::FillStyle_BITMAP ) && !aFillBmp.IsEmpty() )
556 p3DObj->SetMergedItem(XFillBitmapItem(OUString(), Graphic(aFillBmp)));
559 else if ( aLocalFillStyle == drawing::FillStyle_NONE )
561 const XLineColorItem& rLineColor = static_cast<const XLineColorItem&>(p3DObj->GetMergedItem( XATTR_LINECOLOR ));
562 p3DObj->SetMergedItem( XFillColorItem( "", rLineColor.GetColorValue() ) );
563 p3DObj->SetMergedItem( makeSvx3DDoubleSidedItem( true ) );
564 p3DObj->SetMergedItem( Svx3DCloseFrontItem( false ) );
565 p3DObj->SetMergedItem( Svx3DCloseBackItem( false ) );
567 pScene->Insert3DObj( p3DObj );
568 bSceneHasObjects = true;
572 if ( bSceneHasObjects ) // is the SdrObject properly converted
574 // then we can change the return value
575 pRet = pScene;
577 // Camera settings, Perspective ...
578 Camera3D& rCamera = (Camera3D&)pScene->GetCamera();
579 const basegfx::B3DRange& rVolume = pScene->GetBoundVolume();
580 pScene->NbcSetSnapRect( aSnapRect );
582 // InitScene replacement
583 double fW = rVolume.getWidth();
584 double fH = rVolume.getHeight();
586 rCamera.SetAutoAdjustProjection( false );
587 rCamera.SetViewWindow( -fW / 2, - fH / 2, fW, fH);
588 basegfx::B3DPoint aLookAt( 0.0, 0.0, 0.0 );
589 basegfx::B3DPoint aCamPos( 0.0, 0.0, 100.0 );
590 rCamera.SetDefaults( basegfx::B3DPoint( 0.0, 0.0, 100.0 ), aLookAt, 100.0 );
591 rCamera.SetPosAndLookAt( aCamPos, aLookAt );
592 rCamera.SetFocalLength( 1.0 );
593 rCamera.SetProjection( eProjectionType );
594 pScene->SetCamera( rCamera );
595 pScene->SetRectsDirty();
597 double fOriginX, fOriginY;
598 GetOrigin( rGeometryItem, fOriginX, fOriginY );
599 fOriginX = fOriginX * aSnapRect.GetWidth();
600 fOriginY = fOriginY * aSnapRect.GetHeight();
602 basegfx::B3DHomMatrix aNewTransform( pScene->GetTransform() );
603 aNewTransform.translate( -aCenter.X(), aCenter.Y(), -pScene->GetBoundVolume().getDepth() );
605 double fXRotate, fYRotate;
606 GetRotateAngle( rGeometryItem, fXRotate, fYRotate );
607 double fZRotate = static_cast<const SdrObjCustomShape*>(pCustomShape)->GetObjectRotation() * F_PI180;
608 if ( fZRotate != 0.0 )
609 aNewTransform.rotate( 0.0, 0.0, fZRotate );
610 if ( bIsMirroredX )
611 aNewTransform.scale( -1.0, 1, 1 );
612 if ( bIsMirroredY )
613 aNewTransform.scale( 1, -1.0, 1 );
614 if( fYRotate != 0.0 )
615 aNewTransform.rotate( 0.0, -fYRotate, 0.0 );
616 if( fXRotate != 0.0 )
617 aNewTransform.rotate( -fXRotate, 0.0, 0.0 );
618 if ( eProjectionType == PR_PARALLEL )
620 double fSkew, fAlpha;
621 GetSkew( rGeometryItem, fSkew, fAlpha );
622 if ( fSkew != 0.0 )
624 double fInvTanBeta( fSkew / 100.0 );
625 if(fInvTanBeta)
627 aNewTransform.shearXY(
628 fInvTanBeta * cos(fAlpha),
629 fInvTanBeta * sin(fAlpha));
632 basegfx::B3DPoint _aLookAt( 0.0, 0.0, 0.0 );
633 basegfx::B3DPoint _aNewCamPos( 0.0, 0.0, 25000.0 );
634 rCamera.SetPosAndLookAt( _aNewCamPos, _aLookAt );
635 pScene->SetCamera( rCamera );
637 else
639 aNewTransform.translate( -fOriginX, fOriginY, 0.0 );
640 // now set correct camera position
641 drawing::Position3D aViewPointDefault( 3472, -3472, 25000 );
642 drawing::Position3D aViewPoint( GetPosition3D( rGeometryItem, "ViewPoint", aViewPointDefault, pMap ) );
643 double fViewPointX = aViewPoint.PositionX;
644 double fViewPointY = aViewPoint.PositionY;
645 double fViewPointZ = aViewPoint.PositionZ;
646 basegfx::B3DPoint _aLookAt( fViewPointX, -fViewPointY, 0.0 );
647 basegfx::B3DPoint aNewCamPos( fViewPointX, -fViewPointY, fViewPointZ );
648 rCamera.SetPosAndLookAt( aNewCamPos, _aLookAt );
649 pScene->SetCamera( rCamera );
652 pScene->NbcSetTransform( aNewTransform );
655 // light
657 double fAmbientIntensity = GetDouble( rGeometryItem, "Brightness", 22178.0 / 655.36, NULL ) / 100.0;
659 drawing::Direction3D aFirstLightDirectionDefault( 50000, 0, 10000 );
660 drawing::Direction3D aFirstLightDirection( GetDirection3D( rGeometryItem, "FirstLightDirection", aFirstLightDirectionDefault ) );
661 if ( aFirstLightDirection.DirectionZ == 0.0 )
662 aFirstLightDirection.DirectionZ = 1.0;
664 double fLightIntensity = GetDouble( rGeometryItem, "FirstLightLevel", 43712.0 / 655.36, NULL ) / 100.0;
666 /* sal_Bool bFirstLightHarsh = */ GetBool( rGeometryItem, "FirstLightHarsh", false );
668 drawing::Direction3D aSecondLightDirectionDefault( -50000, 0, 10000 );
669 drawing::Direction3D aSecondLightDirection( GetDirection3D( rGeometryItem, "SecondLightDirection", aSecondLightDirectionDefault ) );
670 if ( aSecondLightDirection.DirectionZ == 0.0 )
671 aSecondLightDirection.DirectionZ = -1;
673 double fLight2Intensity = GetDouble( rGeometryItem, "SecondLightLevel", 43712.0 / 655.36, NULL ) / 100.0;
675 /* sal_Bool bLight2Harsh = */ GetBool( rGeometryItem, "SecondLightHarsh", false );
676 /* sal_Bool bLightFace = */ GetBool( rGeometryItem, "LightFace", false );
678 sal_uInt16 nAmbientColor = (sal_uInt16)( fAmbientIntensity * 255.0 );
679 if ( nAmbientColor > 255 )
680 nAmbientColor = 255;
681 Color aGlobalAmbientColor( (sal_uInt8)nAmbientColor, (sal_uInt8)nAmbientColor, (sal_uInt8)nAmbientColor );
682 pScene->GetProperties().SetObjectItem( makeSvx3DAmbientcolorItem( aGlobalAmbientColor ) );
684 sal_uInt8 nSpotLight1 = (sal_uInt8)( fLightIntensity * 255.0 );
685 basegfx::B3DVector aSpotLight1( aFirstLightDirection.DirectionX, - ( aFirstLightDirection.DirectionY ), -( aFirstLightDirection.DirectionZ ) );
686 aSpotLight1.normalize();
687 pScene->GetProperties().SetObjectItem( makeSvx3DLightOnOff1Item( true ) );
688 Color aAmbientSpot1Color( nSpotLight1, nSpotLight1, nSpotLight1 );
689 pScene->GetProperties().SetObjectItem( makeSvx3DLightcolor1Item( aAmbientSpot1Color ) );
690 pScene->GetProperties().SetObjectItem( makeSvx3DLightDirection1Item( aSpotLight1 ) );
692 sal_uInt8 nSpotLight2 = (sal_uInt8)( fLight2Intensity * 255.0 );
693 basegfx::B3DVector aSpotLight2( aSecondLightDirection.DirectionX, -aSecondLightDirection.DirectionY, -aSecondLightDirection.DirectionZ );
694 aSpotLight2.normalize();
695 pScene->GetProperties().SetObjectItem( makeSvx3DLightOnOff2Item( true ) );
696 Color aAmbientSpot2Color( nSpotLight2, nSpotLight2, nSpotLight2 );
697 pScene->GetProperties().SetObjectItem( makeSvx3DLightcolor2Item( aAmbientSpot2Color ) );
698 pScene->GetProperties().SetObjectItem( makeSvx3DLightDirection2Item( aSpotLight2 ) );
700 sal_uInt8 nSpotLight3 = 70;
701 basegfx::B3DVector aSpotLight3( 0.0, 0.0, 1.0 );
702 pScene->GetProperties().SetObjectItem( makeSvx3DLightOnOff3Item( true ) );
703 Color aAmbientSpot3Color( nSpotLight3, nSpotLight3, nSpotLight3 );
704 pScene->GetProperties().SetObjectItem( makeSvx3DLightcolor3Item( aAmbientSpot3Color ) );
705 pScene->GetProperties().SetObjectItem( makeSvx3DLightDirection3Item( aSpotLight3 ) );
707 double fSpecular = GetDouble( rGeometryItem, "Specularity", 0, NULL ) / 100;
708 bool bMetal = GetBool( rGeometryItem, "Metal", false );
710 Color aSpecularCol( 225,225,225 );
711 if ( bMetal )
713 aSpecularCol = Color( 200, 200, 200 );
714 fSpecular += 0.15;
716 sal_Int32 nIntensity = (sal_Int32)fSpecular * 100;
717 if ( nIntensity > 100 )
718 nIntensity = 100;
719 else if ( nIntensity < 0 )
720 nIntensity = 0;
721 nIntensity = 100 - nIntensity;
722 pScene->GetProperties().SetObjectItem( makeSvx3DMaterialSpecularItem( aSpecularCol ) );
723 pScene->GetProperties().SetObjectItem( makeSvx3DMaterialSpecularIntensityItem( (sal_uInt16)nIntensity ) );
725 pScene->SetLogicRect( CalculateNewSnapRect( pCustomShape, aSnapRect, aBoundRect2d, pMap ) );
727 // removing placeholder objects
728 std::vector< E3dCompoundObject* >::iterator aObjectListIter( aPlaceholderObjectList.begin() );
729 while ( aObjectListIter != aPlaceholderObjectList.end() )
731 pScene->Remove3DObj( *aObjectListIter );
732 delete *aObjectListIter++;
735 else
736 delete pScene;
738 return pRet;
741 Rectangle EnhancedCustomShape3d::CalculateNewSnapRect( const SdrObject* pCustomShape, const Rectangle& rSnapRect, const Rectangle& rBoundRect, const double* pMap )
743 const SdrCustomShapeGeometryItem& rGeometryItem = static_cast<const SdrCustomShapeGeometryItem&>(pCustomShape->GetMergedItem( SDRATTR_CUSTOMSHAPE_GEOMETRY ));
744 const Point aCenter( rSnapRect.Center() );
745 double fExtrusionBackward, fExtrusionForward;
746 GetExtrusionDepth( rGeometryItem, pMap, fExtrusionBackward, fExtrusionForward );
747 sal_uInt32 i;
749 // creating initial bound volume ( without rotation. skewing.and camera )
750 basegfx::B3DPolygon aBoundVolume;
751 const Polygon aPolygon( rBoundRect );
753 for ( i = 0L; i < 4L; i++ )
755 aBoundVolume.append(basegfx::B3DPoint(aPolygon[ (sal_uInt16)i ].X() - aCenter.X(), aPolygon[ (sal_uInt16)i ].Y() - aCenter.Y(), fExtrusionForward));
758 for ( i = 0L; i < 4L; i++ )
760 aBoundVolume.append(basegfx::B3DPoint(aPolygon[ (sal_uInt16)i ].X() - aCenter.X(), aPolygon[ (sal_uInt16)i ].Y() - aCenter.Y(), fExtrusionBackward));
763 drawing::Direction3D aRotationCenterDefault( 0, 0, 0 ); // default seems to be wrong, a fractional size of shape has to be used!!
764 drawing::Direction3D aRotationCenter( GetDirection3D( rGeometryItem, "RotationCenter", aRotationCenterDefault ) );
766 double fXRotate, fYRotate;
767 GetRotateAngle( rGeometryItem, fXRotate, fYRotate );
768 double fZRotate = - static_cast<const SdrObjCustomShape*>(pCustomShape)->GetObjectRotation() * F_PI180;
770 // rotating bound volume
771 basegfx::B3DHomMatrix aMatrix;
772 aMatrix.translate(-aRotationCenter.DirectionX, -aRotationCenter.DirectionY, -aRotationCenter.DirectionZ);
773 if ( fZRotate != 0.0 )
774 aMatrix.rotate( 0.0, 0.0, fZRotate );
775 if ( static_cast<const SdrObjCustomShape*>(pCustomShape)->IsMirroredX() )
776 aMatrix.scale( -1.0, 1, 1 );
777 if ( static_cast<const SdrObjCustomShape*>(pCustomShape)->IsMirroredY() )
778 aMatrix.scale( 1, -1.0, 1 );
779 if( fYRotate != 0.0 )
780 aMatrix.rotate( 0.0, fYRotate, 0.0 );
781 if( fXRotate != 0.0 )
782 aMatrix.rotate( -fXRotate, 0.0, 0.0 );
783 aMatrix.translate(aRotationCenter.DirectionX, aRotationCenter.DirectionY, aRotationCenter.DirectionZ);
784 aBoundVolume.transform(aMatrix);
786 Transformation2D aTransformation2D( pCustomShape, rSnapRect, pMap );
787 if ( aTransformation2D.IsParallel() )
788 aBoundVolume = aTransformation2D.ApplySkewSettings( aBoundVolume );
790 Polygon aTransformed( 8 );
791 for ( i = 0L; i < 8L; i++ )
792 aTransformed[ (sal_uInt16)i ] = aTransformation2D.Transform2D( aBoundVolume.getB3DPoint( i ) );
794 return aTransformed.GetBoundRect();
797 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */