1*f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f6e50924SAndrew Rist  * distributed with this work for additional information
6*f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9*f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f6e50924SAndrew Rist  *
11*f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f6e50924SAndrew Rist  *
13*f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15*f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f6e50924SAndrew Rist  * specific language governing permissions and limitations
18*f6e50924SAndrew Rist  * under the License.
19*f6e50924SAndrew Rist  *
20*f6e50924SAndrew Rist  *************************************************************/
21*f6e50924SAndrew Rist 
22*f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <svx/sdr/contact/viewcontactofe3dpolygon.hxx>
28cdf0e10cSrcweir #include <svx/polygn3d.hxx>
29cdf0e10cSrcweir #include <drawinglayer/primitive3d/sdrpolypolygonprimitive3d.hxx>
30cdf0e10cSrcweir #include <svx/sdr/primitive2d/sdrattributecreator.hxx>
31cdf0e10cSrcweir #include <svx/sdr/primitive3d/sdrattributecreator3d.hxx>
32cdf0e10cSrcweir #include <basegfx/polygon/b3dpolygon.hxx>
33cdf0e10cSrcweir #include <basegfx/polygon/b3dpolypolygontools.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
36cdf0e10cSrcweir 
37cdf0e10cSrcweir namespace sdr
38cdf0e10cSrcweir {
39cdf0e10cSrcweir 	namespace contact
40cdf0e10cSrcweir 	{
ViewContactOfE3dPolygon(E3dPolygonObj & rPolygon)41cdf0e10cSrcweir 		ViewContactOfE3dPolygon::ViewContactOfE3dPolygon(E3dPolygonObj& rPolygon)
42cdf0e10cSrcweir 		:	ViewContactOfE3d(rPolygon)
43cdf0e10cSrcweir 		{
44cdf0e10cSrcweir 		}
45cdf0e10cSrcweir 
~ViewContactOfE3dPolygon()46cdf0e10cSrcweir 		ViewContactOfE3dPolygon::~ViewContactOfE3dPolygon()
47cdf0e10cSrcweir 		{
48cdf0e10cSrcweir 		}
49cdf0e10cSrcweir 
createViewIndependentPrimitive3DSequence() const50cdf0e10cSrcweir 		drawinglayer::primitive3d::Primitive3DSequence ViewContactOfE3dPolygon::createViewIndependentPrimitive3DSequence() const
51cdf0e10cSrcweir 		{
52cdf0e10cSrcweir 			drawinglayer::primitive3d::Primitive3DSequence xRetval;
53cdf0e10cSrcweir 			const SfxItemSet& rItemSet = GetE3dPolygonObj().GetMergedItemSet();
54cdf0e10cSrcweir 			const bool bSuppressFill(GetE3dPolygonObj().GetLineOnly());
55cdf0e10cSrcweir 			const drawinglayer::attribute::SdrLineFillShadowAttribute3D aAttribute(
56cdf0e10cSrcweir 				drawinglayer::primitive2d::createNewSdrLineFillShadowAttribute(rItemSet, bSuppressFill));
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 			// get extrude geometry
59cdf0e10cSrcweir 			basegfx::B3DPolyPolygon aPolyPolygon3D(GetE3dPolygonObj().GetPolyPolygon3D());
60cdf0e10cSrcweir 			const basegfx::B3DPolyPolygon aPolyNormals3D(GetE3dPolygonObj().GetPolyNormals3D());
61cdf0e10cSrcweir 			const basegfx::B2DPolyPolygon aPolyTexture2D(GetE3dPolygonObj().GetPolyTexture2D());
62cdf0e10cSrcweir             const bool bNormals(aPolyNormals3D.count() && aPolyNormals3D.count() == aPolyPolygon3D.count());
63cdf0e10cSrcweir             const bool bTexture(aPolyTexture2D.count() && aPolyTexture2D.count() == aPolyPolygon3D.count());
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 			if(bNormals || bTexture)
66cdf0e10cSrcweir 			{
67cdf0e10cSrcweir 				for(sal_uInt32 a(0L); a < aPolyPolygon3D.count(); a++)
68cdf0e10cSrcweir 				{
69cdf0e10cSrcweir 					basegfx::B3DPolygon aCandidate3D(aPolyPolygon3D.getB3DPolygon(a));
70cdf0e10cSrcweir 					basegfx::B3DPolygon aNormals3D;
71cdf0e10cSrcweir 					basegfx::B2DPolygon aTexture2D;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 					if(bNormals)
74cdf0e10cSrcweir 					{
75cdf0e10cSrcweir 						aNormals3D = aPolyNormals3D.getB3DPolygon(a);
76cdf0e10cSrcweir 					}
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 					if(bTexture)
79cdf0e10cSrcweir 					{
80cdf0e10cSrcweir 						aTexture2D = aPolyTexture2D.getB2DPolygon(a);
81cdf0e10cSrcweir 					}
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 					for(sal_uInt32 b(0L); b < aCandidate3D.count(); b++)
84cdf0e10cSrcweir 					{
85cdf0e10cSrcweir                         if(bNormals)
86cdf0e10cSrcweir                         {
87cdf0e10cSrcweir                             sal_uInt32 nNormalCount = aNormals3D.count();
88cdf0e10cSrcweir                             if( b < nNormalCount )
89cdf0e10cSrcweir                                 aCandidate3D.setNormal(b, aNormals3D.getB3DPoint(b));
90cdf0e10cSrcweir                             else if( nNormalCount > 0 )
91cdf0e10cSrcweir                                 aCandidate3D.setNormal(b, aNormals3D.getB3DPoint(0));
92cdf0e10cSrcweir                         }
93cdf0e10cSrcweir                         if(bTexture)
94cdf0e10cSrcweir                         {
95cdf0e10cSrcweir                             sal_uInt32 nTextureCount = aTexture2D.count();
96cdf0e10cSrcweir                             if( b < nTextureCount )
97cdf0e10cSrcweir                                 aCandidate3D.setTextureCoordinate(b, aTexture2D.getB2DPoint(b));
98cdf0e10cSrcweir                             else if( nTextureCount > 0 )
99cdf0e10cSrcweir                                 aCandidate3D.setTextureCoordinate(b, aTexture2D.getB2DPoint(0));
100cdf0e10cSrcweir                         }
101cdf0e10cSrcweir 					}
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 					aPolyPolygon3D.setB3DPolygon(a, aCandidate3D);
104cdf0e10cSrcweir 				}
105cdf0e10cSrcweir 			}
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 			// get 3D Object Attributes
108cdf0e10cSrcweir 			drawinglayer::attribute::Sdr3DObjectAttribute* pSdr3DObjectAttribute = drawinglayer::primitive2d::createNewSdr3DObjectAttribute(rItemSet);
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 			// calculate texture size
111cdf0e10cSrcweir 			basegfx::B2DVector aTextureSize(1.0, 1.0);
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 			if(bTexture)
114cdf0e10cSrcweir 			{
115cdf0e10cSrcweir                 // #i98314#
116cdf0e10cSrcweir                 // create texture size from object's size
117cdf0e10cSrcweir                 const basegfx::B3DRange aObjectRange(basegfx::tools::getRange(aPolyPolygon3D));
118cdf0e10cSrcweir 
119cdf0e10cSrcweir                 double fWidth(0.0);
120cdf0e10cSrcweir                 double fHeight(0.0);
121cdf0e10cSrcweir 
122cdf0e10cSrcweir                 // this is a polygon object, so Width/Height and/or Depth may be zero (e.g. left
123cdf0e10cSrcweir                 // wall of chart). Take this into account
124cdf0e10cSrcweir                 if(basegfx::fTools::equalZero(aObjectRange.getWidth()))
125cdf0e10cSrcweir                 {
126cdf0e10cSrcweir                     // width is zero, use height and depth
127cdf0e10cSrcweir                     fWidth = aObjectRange.getHeight();
128cdf0e10cSrcweir                     fHeight = aObjectRange.getDepth();
129cdf0e10cSrcweir                 }
130cdf0e10cSrcweir                 else if(basegfx::fTools::equalZero(aObjectRange.getHeight()))
131cdf0e10cSrcweir                 {
132cdf0e10cSrcweir                     // height is zero, use width and depth
133cdf0e10cSrcweir                     fWidth = aObjectRange.getWidth();
134cdf0e10cSrcweir                     fHeight = aObjectRange.getDepth();
135cdf0e10cSrcweir                 }
136cdf0e10cSrcweir                 else
137cdf0e10cSrcweir                 {
138cdf0e10cSrcweir                     // use width and height
139cdf0e10cSrcweir                     fWidth = aObjectRange.getWidth();
140cdf0e10cSrcweir                     fHeight = aObjectRange.getHeight();
141cdf0e10cSrcweir                 }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir                 if(basegfx::fTools::lessOrEqual(fWidth, 0.0) ||basegfx::fTools::lessOrEqual(fHeight, 0.0))
144cdf0e10cSrcweir                 {
145cdf0e10cSrcweir                     // no texture; fallback to very small size
146cdf0e10cSrcweir 				    aTextureSize.setX(0.01);
147cdf0e10cSrcweir 				    aTextureSize.setY(0.01);
148cdf0e10cSrcweir                 }
149cdf0e10cSrcweir                 else
150cdf0e10cSrcweir                 {
151cdf0e10cSrcweir 				    aTextureSize.setX(fWidth);
152cdf0e10cSrcweir 				    aTextureSize.setY(fHeight);
153cdf0e10cSrcweir                 }
154cdf0e10cSrcweir 			}
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 			// #i98295#
157cdf0e10cSrcweir 			// unfortunately, this SdrObject type which allows a free 3d geometry definition was defined
158cdf0e10cSrcweir 			// wrong topologically in relation to it's plane normal and 3D visibility when it was invented
159cdf0e10cSrcweir 			// a long time ago. Since the API allows creation of this SDrObject type, it is not possible to
160cdf0e10cSrcweir 			// simply change this definition. Only the chart should use it, and at least this object type
161cdf0e10cSrcweir 			// only exists at Runtime (is not saved and/or loaded in any FileFormat). Still someone external
162cdf0e10cSrcweir 			// may have used it in it's API. To not risk wrong 3D lightings, i have to switch the orientation
163cdf0e10cSrcweir 			// of the polygon here
164cdf0e10cSrcweir 			aPolyPolygon3D.flip();
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 			// create primitive and add
167cdf0e10cSrcweir 			const basegfx::B3DHomMatrix aWorldTransform;
168cdf0e10cSrcweir 			const drawinglayer::primitive3d::Primitive3DReference xReference(
169cdf0e10cSrcweir 				new drawinglayer::primitive3d::SdrPolyPolygonPrimitive3D(
170cdf0e10cSrcweir 					aPolyPolygon3D, aWorldTransform, aTextureSize, aAttribute, *pSdr3DObjectAttribute));
171cdf0e10cSrcweir             xRetval = drawinglayer::primitive3d::Primitive3DSequence(&xReference, 1);
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 			// delete 3D Object Attributes
174cdf0e10cSrcweir 			delete pSdr3DObjectAttribute;
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 			return xRetval;
177cdf0e10cSrcweir 		}
178cdf0e10cSrcweir 	} // end of namespace contact
179cdf0e10cSrcweir } // end of namespace sdr
180cdf0e10cSrcweir 
181cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
182cdf0e10cSrcweir // eof
183