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