1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svx.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <svx/sdr/contact/viewcontactofe3dpolygon.hxx>
32*cdf0e10cSrcweir #include <svx/polygn3d.hxx>
33*cdf0e10cSrcweir #include <drawinglayer/primitive3d/sdrpolypolygonprimitive3d.hxx>
34*cdf0e10cSrcweir #include <svx/sdr/primitive2d/sdrattributecreator.hxx>
35*cdf0e10cSrcweir #include <svx/sdr/primitive3d/sdrattributecreator3d.hxx>
36*cdf0e10cSrcweir #include <basegfx/polygon/b3dpolygon.hxx>
37*cdf0e10cSrcweir #include <basegfx/polygon/b3dpolypolygontools.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir namespace sdr
42*cdf0e10cSrcweir {
43*cdf0e10cSrcweir 	namespace contact
44*cdf0e10cSrcweir 	{
45*cdf0e10cSrcweir 		ViewContactOfE3dPolygon::ViewContactOfE3dPolygon(E3dPolygonObj& rPolygon)
46*cdf0e10cSrcweir 		:	ViewContactOfE3d(rPolygon)
47*cdf0e10cSrcweir 		{
48*cdf0e10cSrcweir 		}
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir 		ViewContactOfE3dPolygon::~ViewContactOfE3dPolygon()
51*cdf0e10cSrcweir 		{
52*cdf0e10cSrcweir 		}
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 		drawinglayer::primitive3d::Primitive3DSequence ViewContactOfE3dPolygon::createViewIndependentPrimitive3DSequence() const
55*cdf0e10cSrcweir 		{
56*cdf0e10cSrcweir 			drawinglayer::primitive3d::Primitive3DSequence xRetval;
57*cdf0e10cSrcweir 			const SfxItemSet& rItemSet = GetE3dPolygonObj().GetMergedItemSet();
58*cdf0e10cSrcweir 			const bool bSuppressFill(GetE3dPolygonObj().GetLineOnly());
59*cdf0e10cSrcweir 			const drawinglayer::attribute::SdrLineFillShadowAttribute3D aAttribute(
60*cdf0e10cSrcweir 				drawinglayer::primitive2d::createNewSdrLineFillShadowAttribute(rItemSet, bSuppressFill));
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 			// get extrude geometry
63*cdf0e10cSrcweir 			basegfx::B3DPolyPolygon aPolyPolygon3D(GetE3dPolygonObj().GetPolyPolygon3D());
64*cdf0e10cSrcweir 			const basegfx::B3DPolyPolygon aPolyNormals3D(GetE3dPolygonObj().GetPolyNormals3D());
65*cdf0e10cSrcweir 			const basegfx::B2DPolyPolygon aPolyTexture2D(GetE3dPolygonObj().GetPolyTexture2D());
66*cdf0e10cSrcweir             const bool bNormals(aPolyNormals3D.count() && aPolyNormals3D.count() == aPolyPolygon3D.count());
67*cdf0e10cSrcweir             const bool bTexture(aPolyTexture2D.count() && aPolyTexture2D.count() == aPolyPolygon3D.count());
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 			if(bNormals || bTexture)
70*cdf0e10cSrcweir 			{
71*cdf0e10cSrcweir 				for(sal_uInt32 a(0L); a < aPolyPolygon3D.count(); a++)
72*cdf0e10cSrcweir 				{
73*cdf0e10cSrcweir 					basegfx::B3DPolygon aCandidate3D(aPolyPolygon3D.getB3DPolygon(a));
74*cdf0e10cSrcweir 					basegfx::B3DPolygon aNormals3D;
75*cdf0e10cSrcweir 					basegfx::B2DPolygon aTexture2D;
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 					if(bNormals)
78*cdf0e10cSrcweir 					{
79*cdf0e10cSrcweir 						aNormals3D = aPolyNormals3D.getB3DPolygon(a);
80*cdf0e10cSrcweir 					}
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 					if(bTexture)
83*cdf0e10cSrcweir 					{
84*cdf0e10cSrcweir 						aTexture2D = aPolyTexture2D.getB2DPolygon(a);
85*cdf0e10cSrcweir 					}
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 					for(sal_uInt32 b(0L); b < aCandidate3D.count(); b++)
88*cdf0e10cSrcweir 					{
89*cdf0e10cSrcweir                         if(bNormals)
90*cdf0e10cSrcweir                         {
91*cdf0e10cSrcweir                             sal_uInt32 nNormalCount = aNormals3D.count();
92*cdf0e10cSrcweir                             if( b < nNormalCount )
93*cdf0e10cSrcweir                                 aCandidate3D.setNormal(b, aNormals3D.getB3DPoint(b));
94*cdf0e10cSrcweir                             else if( nNormalCount > 0 )
95*cdf0e10cSrcweir                                 aCandidate3D.setNormal(b, aNormals3D.getB3DPoint(0));
96*cdf0e10cSrcweir                         }
97*cdf0e10cSrcweir                         if(bTexture)
98*cdf0e10cSrcweir                         {
99*cdf0e10cSrcweir                             sal_uInt32 nTextureCount = aTexture2D.count();
100*cdf0e10cSrcweir                             if( b < nTextureCount )
101*cdf0e10cSrcweir                                 aCandidate3D.setTextureCoordinate(b, aTexture2D.getB2DPoint(b));
102*cdf0e10cSrcweir                             else if( nTextureCount > 0 )
103*cdf0e10cSrcweir                                 aCandidate3D.setTextureCoordinate(b, aTexture2D.getB2DPoint(0));
104*cdf0e10cSrcweir                         }
105*cdf0e10cSrcweir 					}
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 					aPolyPolygon3D.setB3DPolygon(a, aCandidate3D);
108*cdf0e10cSrcweir 				}
109*cdf0e10cSrcweir 			}
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 			// get 3D Object Attributes
112*cdf0e10cSrcweir 			drawinglayer::attribute::Sdr3DObjectAttribute* pSdr3DObjectAttribute = drawinglayer::primitive2d::createNewSdr3DObjectAttribute(rItemSet);
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 			// calculate texture size
115*cdf0e10cSrcweir 			basegfx::B2DVector aTextureSize(1.0, 1.0);
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 			if(bTexture)
118*cdf0e10cSrcweir 			{
119*cdf0e10cSrcweir                 // #i98314#
120*cdf0e10cSrcweir                 // create texture size from object's size
121*cdf0e10cSrcweir                 const basegfx::B3DRange aObjectRange(basegfx::tools::getRange(aPolyPolygon3D));
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir                 double fWidth(0.0);
124*cdf0e10cSrcweir                 double fHeight(0.0);
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir                 // this is a polygon object, so Width/Height and/or Depth may be zero (e.g. left
127*cdf0e10cSrcweir                 // wall of chart). Take this into account
128*cdf0e10cSrcweir                 if(basegfx::fTools::equalZero(aObjectRange.getWidth()))
129*cdf0e10cSrcweir                 {
130*cdf0e10cSrcweir                     // width is zero, use height and depth
131*cdf0e10cSrcweir                     fWidth = aObjectRange.getHeight();
132*cdf0e10cSrcweir                     fHeight = aObjectRange.getDepth();
133*cdf0e10cSrcweir                 }
134*cdf0e10cSrcweir                 else if(basegfx::fTools::equalZero(aObjectRange.getHeight()))
135*cdf0e10cSrcweir                 {
136*cdf0e10cSrcweir                     // height is zero, use width and depth
137*cdf0e10cSrcweir                     fWidth = aObjectRange.getWidth();
138*cdf0e10cSrcweir                     fHeight = aObjectRange.getDepth();
139*cdf0e10cSrcweir                 }
140*cdf0e10cSrcweir                 else
141*cdf0e10cSrcweir                 {
142*cdf0e10cSrcweir                     // use width and height
143*cdf0e10cSrcweir                     fWidth = aObjectRange.getWidth();
144*cdf0e10cSrcweir                     fHeight = aObjectRange.getHeight();
145*cdf0e10cSrcweir                 }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir                 if(basegfx::fTools::lessOrEqual(fWidth, 0.0) ||basegfx::fTools::lessOrEqual(fHeight, 0.0))
148*cdf0e10cSrcweir                 {
149*cdf0e10cSrcweir                     // no texture; fallback to very small size
150*cdf0e10cSrcweir 				    aTextureSize.setX(0.01);
151*cdf0e10cSrcweir 				    aTextureSize.setY(0.01);
152*cdf0e10cSrcweir                 }
153*cdf0e10cSrcweir                 else
154*cdf0e10cSrcweir                 {
155*cdf0e10cSrcweir 				    aTextureSize.setX(fWidth);
156*cdf0e10cSrcweir 				    aTextureSize.setY(fHeight);
157*cdf0e10cSrcweir                 }
158*cdf0e10cSrcweir 			}
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 			// #i98295#
161*cdf0e10cSrcweir 			// unfortunately, this SdrObject type which allows a free 3d geometry definition was defined
162*cdf0e10cSrcweir 			// wrong topologically in relation to it's plane normal and 3D visibility when it was invented
163*cdf0e10cSrcweir 			// a long time ago. Since the API allows creation of this SDrObject type, it is not possible to
164*cdf0e10cSrcweir 			// simply change this definition. Only the chart should use it, and at least this object type
165*cdf0e10cSrcweir 			// only exists at Runtime (is not saved and/or loaded in any FileFormat). Still someone external
166*cdf0e10cSrcweir 			// may have used it in it's API. To not risk wrong 3D lightings, i have to switch the orientation
167*cdf0e10cSrcweir 			// of the polygon here
168*cdf0e10cSrcweir 			aPolyPolygon3D.flip();
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 			// create primitive and add
171*cdf0e10cSrcweir 			const basegfx::B3DHomMatrix aWorldTransform;
172*cdf0e10cSrcweir 			const drawinglayer::primitive3d::Primitive3DReference xReference(
173*cdf0e10cSrcweir 				new drawinglayer::primitive3d::SdrPolyPolygonPrimitive3D(
174*cdf0e10cSrcweir 					aPolyPolygon3D, aWorldTransform, aTextureSize, aAttribute, *pSdr3DObjectAttribute));
175*cdf0e10cSrcweir             xRetval = drawinglayer::primitive3d::Primitive3DSequence(&xReference, 1);
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 			// delete 3D Object Attributes
178*cdf0e10cSrcweir 			delete pSdr3DObjectAttribute;
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 			return xRetval;
181*cdf0e10cSrcweir 		}
182*cdf0e10cSrcweir 	} // end of namespace contact
183*cdf0e10cSrcweir } // end of namespace sdr
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
186*cdf0e10cSrcweir // eof
187