update dev300-m58
[ooovba.git] / svx / source / sdr / contact / viewcontactofe3dpolygon.cxx
blobe61c73c2d8c5c348b666d70b281f749d9626e6fe
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: viewcontactofe3dpolygon.cxx,v $
11 * $Revision: 1.2.18.1 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_svx.hxx"
35 #include <svx/sdr/contact/viewcontactofe3dpolygon.hxx>
36 #include <svx/polygn3d.hxx>
37 #include <drawinglayer/primitive3d/sdrpolypolygonprimitive3d.hxx>
38 #include <svx/sdr/primitive2d/sdrattributecreator.hxx>
39 #include <svx/sdr/primitive3d/sdrattributecreator3d.hxx>
40 #include <basegfx/polygon/b3dpolygon.hxx>
41 #include <basegfx/polygon/b3dpolypolygontools.hxx>
43 //////////////////////////////////////////////////////////////////////////////
45 namespace sdr
47 namespace contact
49 ViewContactOfE3dPolygon::ViewContactOfE3dPolygon(E3dPolygonObj& rPolygon)
50 : ViewContactOfE3d(rPolygon)
54 ViewContactOfE3dPolygon::~ViewContactOfE3dPolygon()
58 drawinglayer::primitive3d::Primitive3DSequence ViewContactOfE3dPolygon::createViewIndependentPrimitive3DSequence() const
60 drawinglayer::primitive3d::Primitive3DSequence xRetval;
61 const SfxItemSet& rItemSet = GetE3dPolygonObj().GetMergedItemSet();
62 const bool bSuppressFill(GetE3dPolygonObj().GetLineOnly());
63 drawinglayer::attribute::SdrLineFillShadowAttribute* pAttribute = drawinglayer::primitive2d::createNewSdrLineFillShadowAttribute(rItemSet, bSuppressFill);
65 // for 3D Objects, always create a primitive even when not visible. This is necessary ATM
66 // since e.g. chart geometries rely on the occupied space of non-visible objects
67 if(!pAttribute)
69 pAttribute = new drawinglayer::attribute::SdrLineFillShadowAttribute(
70 impCreateFallbackLineAttribute(basegfx::BColor(0.0, 1.0, 0.0)),
71 0, 0, 0, 0);
74 // get extrude geometry
75 basegfx::B3DPolyPolygon aPolyPolygon3D(GetE3dPolygonObj().GetPolyPolygon3D());
76 const basegfx::B3DPolyPolygon aPolyNormals3D(GetE3dPolygonObj().GetPolyNormals3D());
77 const basegfx::B2DPolyPolygon aPolyTexture2D(GetE3dPolygonObj().GetPolyTexture2D());
78 const bool bNormals(aPolyNormals3D.count() && aPolyNormals3D.count() == aPolyPolygon3D.count());
79 const bool bTexture(aPolyTexture2D.count() && aPolyTexture2D.count() == aPolyPolygon3D.count());
81 if(bNormals || bTexture)
83 for(sal_uInt32 a(0L); a < aPolyPolygon3D.count(); a++)
85 basegfx::B3DPolygon aCandidate3D(aPolyPolygon3D.getB3DPolygon(a));
86 basegfx::B3DPolygon aNormals3D;
87 basegfx::B2DPolygon aTexture2D;
89 if(bNormals)
91 aNormals3D = aPolyNormals3D.getB3DPolygon(a);
94 if(bTexture)
96 aTexture2D = aPolyTexture2D.getB2DPolygon(a);
99 for(sal_uInt32 b(0L); b < aCandidate3D.count(); b++)
101 if(bNormals)
103 sal_uInt32 nNormalCount = aNormals3D.count();
104 if( b < nNormalCount )
105 aCandidate3D.setNormal(b, aNormals3D.getB3DPoint(b));
106 else if( nNormalCount > 0 )
107 aCandidate3D.setNormal(b, aNormals3D.getB3DPoint(0));
109 if(bTexture)
111 sal_uInt32 nTextureCount = aTexture2D.count();
112 if( b < nTextureCount )
113 aCandidate3D.setTextureCoordinate(b, aTexture2D.getB2DPoint(b));
114 else if( nTextureCount > 0 )
115 aCandidate3D.setTextureCoordinate(b, aTexture2D.getB2DPoint(0));
119 aPolyPolygon3D.setB3DPolygon(a, aCandidate3D);
123 // get 3D Object Attributes
124 drawinglayer::attribute::Sdr3DObjectAttribute* pSdr3DObjectAttribute = drawinglayer::primitive2d::createNewSdr3DObjectAttribute(rItemSet);
126 // calculate texture size
127 basegfx::B2DVector aTextureSize(1.0, 1.0);
129 if(bTexture)
131 // #i98314#
132 // create texture size from object's size
133 const basegfx::B3DRange aObjectRange(basegfx::tools::getRange(aPolyPolygon3D));
135 double fWidth(0.0);
136 double fHeight(0.0);
138 // this is a polygon object, so Width/Height and/or Depth may be zero (e.g. left
139 // wall of chart). Take this into account
140 if(basegfx::fTools::equalZero(aObjectRange.getWidth()))
142 // width is zero, use height and depth
143 fWidth = aObjectRange.getHeight();
144 fHeight = aObjectRange.getDepth();
146 else if(basegfx::fTools::equalZero(aObjectRange.getHeight()))
148 // height is zero, use width and depth
149 fWidth = aObjectRange.getWidth();
150 fHeight = aObjectRange.getDepth();
152 else
154 // use width and height
155 fWidth = aObjectRange.getWidth();
156 fHeight = aObjectRange.getHeight();
159 if(basegfx::fTools::lessOrEqual(fWidth, 0.0) ||basegfx::fTools::lessOrEqual(fHeight, 0.0))
161 // no texture; fallback to very small size
162 aTextureSize.setX(0.01);
163 aTextureSize.setY(0.01);
165 else
167 aTextureSize.setX(fWidth);
168 aTextureSize.setY(fHeight);
172 // #i98295#
173 // unfortunately, this SdrObject type which allows a free 3d geometry definition was defined
174 // wrong topologically in relation to it's plane normal and 3D visibility when it was invented
175 // a long time ago. Since the API allows creation of this SDrObject type, it is not possible to
176 // simply change this definition. Only the chart should use it, and at least this object type
177 // only exists at Runtime (is not saved and/or loaded in any FileFormat). Still someone external
178 // may have used it in it's API. To not risk wrong 3D lightings, i have to switch the orientation
179 // of the polygon here
180 aPolyPolygon3D.flip();
182 // create primitive and add
183 const basegfx::B3DHomMatrix aWorldTransform;
184 const drawinglayer::primitive3d::Primitive3DReference xReference(new drawinglayer::primitive3d::SdrPolyPolygonPrimitive3D(
185 aPolyPolygon3D, aWorldTransform, aTextureSize, *pAttribute, *pSdr3DObjectAttribute));
186 xRetval = drawinglayer::primitive3d::Primitive3DSequence(&xReference, 1);
188 // delete 3D Object Attributes
189 delete pSdr3DObjectAttribute;
190 delete pAttribute;
192 return xRetval;
194 } // end of namespace contact
195 } // end of namespace sdr
197 //////////////////////////////////////////////////////////////////////////////
198 // eof