Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / svx / source / engine3d / polygn3d.cxx
blob0f86809a4f3284a8c6f6c893ce2b535f6b5d51e5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <svx/polygn3d.hxx>
30 #include <svx/svdpage.hxx>
31 #include "svx/globl3d.hxx"
32 #include <basegfx/point/b3dpoint.hxx>
33 #include <svx/sdr/contact/viewcontactofe3dpolygon.hxx>
34 #include <basegfx/polygon/b3dpolygon.hxx>
35 #include <basegfx/polygon/b3dpolygontools.hxx>
37 TYPEINIT1(E3dPolygonObj, E3dCompoundObject);
39 //////////////////////////////////////////////////////////////////////////////
40 // DrawContact section
42 sdr::contact::ViewContact* E3dPolygonObj::CreateObjectSpecificViewContact()
44 return new sdr::contact::ViewContactOfE3dPolygon(*this);
47 E3dPolygonObj::E3dPolygonObj(
48 E3dDefaultAttributes& rDefault,
49 const basegfx::B3DPolyPolygon& rPolyPoly3D,
50 sal_Bool bLinOnly)
51 : E3dCompoundObject(rDefault),
52 bLineOnly(bLinOnly)
54 // Set geometry
55 SetPolyPolygon3D(rPolyPoly3D);
57 // Create default normals
58 CreateDefaultNormals();
60 // Create default texture coordinates
61 CreateDefaultTexture();
64 E3dPolygonObj::E3dPolygonObj()
65 : E3dCompoundObject(),
66 bLineOnly(false) // added missing initialisation
68 // Create no geometry
71 void E3dPolygonObj::CreateDefaultNormals()
73 basegfx::B3DPolyPolygon aPolyNormals;
75 // Create a complete PolyPolygon with the plane normal
76 for(sal_uInt32 a(0L); a < aPolyPoly3D.count(); a++)
78 // Find source polygon
79 const basegfx::B3DPolygon aPolygon(aPolyPoly3D.getB3DPolygon(a));
81 // Creating a new polygon for the normal
82 basegfx::B3DPolygon aNormals;
84 // Get normal (and invert)
85 basegfx::B3DVector aNormal(-basegfx::tools::getNormal(aPolygon));
87 // Fill new polygon
88 for(sal_uInt32 b(0L); b < aPolygon.count(); b++)
90 aNormals.append(aNormal);
93 // Insert new polygon into the PolyPolygon
94 aPolyNormals.append(aNormals);
97 // Set default normal
98 SetPolyNormals3D(aPolyNormals);
101 void E3dPolygonObj::CreateDefaultTexture()
103 basegfx::B2DPolyPolygon aPolyTexture;
104 // Create a complete PolyPolygon with the texture coordinates
105 // The texture coordinates extend over X,Y and Z
106 // on the whole extreme values in the range 0.0 .. 1.0
107 for(sal_uInt32 a(0L); a < aPolyPoly3D.count(); a++)
109 // Find source polygon
110 const basegfx::B3DPolygon& aPolygon(aPolyPoly3D.getB3DPolygon(a));
112 // Determine the total size of the object
113 basegfx::B3DRange aVolume(basegfx::tools::getRange(aPolygon));
115 // Get normal
116 basegfx::B3DVector aNormal(basegfx::tools::getNormal(aPolygon));
117 aNormal.setX(fabs(aNormal.getX()));
118 aNormal.setY(fabs(aNormal.getY()));
119 aNormal.setZ(fabs(aNormal.getZ()));
121 // Decide which coordinates should be used as a source for the mapping
122 sal_uInt16 nSourceMode = 0;
124 // Determine the greatest degree of freedom
125 if(!(aNormal.getX() > aNormal.getY() && aNormal.getX() > aNormal.getZ()))
127 if(aNormal.getY() > aNormal.getZ())
129 // Y is the largest, use X,Z as mapping
130 nSourceMode = 1;
132 else
134 // Z is the largest, use X,Y as mapping
135 nSourceMode = 2;
139 // Create new polygon for texture coordinates
140 basegfx::B2DPolygon aTexture;
142 // Fill new polygon
143 for(sal_uInt32 b(0L); b < aPolygon.count(); b++)
145 basegfx::B2DPoint aTex;
146 const basegfx::B3DPoint aCandidate(aPolygon.getB3DPoint(b));
148 switch(nSourceMode)
150 case 0: //Source is Y,Z
151 if(aVolume.getHeight())
152 aTex.setX((aCandidate.getY() - aVolume.getMinY()) / aVolume.getHeight());
153 if(aVolume.getDepth())
154 aTex.setY((aCandidate.getZ() - aVolume.getMinZ()) / aVolume.getDepth());
155 break;
157 case 1: // Source is X,Z
158 if(aVolume.getWidth())
159 aTex.setX((aCandidate.getX() - aVolume.getMinX()) / aVolume.getWidth());
160 if(aVolume.getDepth())
161 aTex.setY((aCandidate.getZ() - aVolume.getMinZ()) / aVolume.getDepth());
162 break;
164 case 2: // Source is X,Y
165 if(aVolume.getWidth())
166 aTex.setX((aCandidate.getX() - aVolume.getMinX()) / aVolume.getWidth());
167 if(aVolume.getHeight())
168 aTex.setY((aCandidate.getY() - aVolume.getMinY()) / aVolume.getHeight());
169 break;
172 aTexture.append(aTex);
175 // Insert new polygon into the PolyPolygon
176 aPolyTexture.append(aTexture);
179 // Set default Texture coordinates
180 SetPolyTexture2D(aPolyTexture);
183 E3dPolygonObj::~E3dPolygonObj()
187 sal_uInt16 E3dPolygonObj::GetObjIdentifier() const
189 return E3D_POLYGONOBJ_ID;
192 void E3dPolygonObj::SetPolyPolygon3D(const basegfx::B3DPolyPolygon& rNewPolyPoly3D)
194 if ( aPolyPoly3D != rNewPolyPoly3D )
196 // New PolyPolygon; copying
197 aPolyPoly3D = rNewPolyPoly3D;
199 // Create new geometry
200 ActionChanged();
204 void E3dPolygonObj::SetPolyNormals3D(const basegfx::B3DPolyPolygon& rNewPolyNormals3D)
206 if ( aPolyNormals3D != rNewPolyNormals3D )
208 // New PolyPolygon; copying
209 aPolyNormals3D = rNewPolyNormals3D;
211 // Create new geometry
212 ActionChanged();
216 void E3dPolygonObj::SetPolyTexture2D(const basegfx::B2DPolyPolygon& rNewPolyTexture2D)
218 if ( aPolyTexture2D != rNewPolyTexture2D )
220 // New PolyPolygon; copying
221 aPolyTexture2D = rNewPolyTexture2D;
223 // Create new geometry
224 ActionChanged();
228 // Convert the object into a group object consisting of 6 polygons
230 SdrObject *E3dPolygonObj::DoConvertToPolyObj(sal_Bool /*bBezier*/) const
232 return NULL;
235 E3dPolygonObj* E3dPolygonObj::Clone() const
237 return CloneHelper< E3dPolygonObj >();
240 void E3dPolygonObj::SetLineOnly(sal_Bool bNew)
242 if(bNew != bLineOnly)
244 bLineOnly = bNew;
245 ActionChanged();
249 // eof
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */