bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / engine3d / polygn3d.cxx
blob44ea9816c7065314691d37155f9ebab0d53d62b0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <svx/polygn3d.hxx>
21 #include <svx/svdpage.hxx>
22 #include "svx/globl3d.hxx"
23 #include <basegfx/point/b3dpoint.hxx>
24 #include <sdr/contact/viewcontactofe3dpolygon.hxx>
25 #include <basegfx/polygon/b3dpolygon.hxx>
26 #include <basegfx/polygon/b3dpolygontools.hxx>
28 TYPEINIT1(E3dPolygonObj, E3dCompoundObject);
31 // DrawContact section
33 sdr::contact::ViewContact* E3dPolygonObj::CreateObjectSpecificViewContact()
35 return new sdr::contact::ViewContactOfE3dPolygon(*this);
38 E3dPolygonObj::E3dPolygonObj(
39 E3dDefaultAttributes& rDefault,
40 const basegfx::B3DPolyPolygon& rPolyPoly3D,
41 bool bLinOnly)
42 : E3dCompoundObject(rDefault),
43 bLineOnly(bLinOnly)
45 // Set geometry
46 SetPolyPolygon3D(rPolyPoly3D);
48 // Create default normals
49 CreateDefaultNormals();
51 // Create default texture coordinates
52 CreateDefaultTexture();
55 E3dPolygonObj::E3dPolygonObj()
56 : E3dCompoundObject(),
57 bLineOnly(false) // added missing initialisation
59 // Create no geometry
62 void E3dPolygonObj::CreateDefaultNormals()
64 basegfx::B3DPolyPolygon aPolyNormals;
66 // Create a complete tools::PolyPolygon with the plane normal
67 for(sal_uInt32 a(0L); a < aPolyPoly3D.count(); a++)
69 // Find source polygon
70 const basegfx::B3DPolygon aPolygon(aPolyPoly3D.getB3DPolygon(a));
72 // Creating a new polygon for the normal
73 basegfx::B3DPolygon aNormals;
75 // Get normal (and invert)
76 basegfx::B3DVector aNormal(-basegfx::tools::getNormal(aPolygon));
78 // Fill new polygon
79 for(sal_uInt32 b(0L); b < aPolygon.count(); b++)
81 aNormals.append(aNormal);
84 // Insert new polygon into the PolyPolygon
85 aPolyNormals.append(aNormals);
88 // Set default normal
89 SetPolyNormals3D(aPolyNormals);
92 void E3dPolygonObj::CreateDefaultTexture()
94 basegfx::B2DPolyPolygon aPolyTexture;
95 // Create a complete tools::PolyPolygon with the texture coordinates
96 // The texture coordinates extend over X,Y and Z
97 // on the whole extreme values in the range 0.0 .. 1.0
98 for(sal_uInt32 a(0L); a < aPolyPoly3D.count(); a++)
100 // Find source polygon
101 const basegfx::B3DPolygon& aPolygon(aPolyPoly3D.getB3DPolygon(a));
103 // Determine the total size of the object
104 basegfx::B3DRange aVolume(basegfx::tools::getRange(aPolygon));
106 // Get normal
107 basegfx::B3DVector aNormal(basegfx::tools::getNormal(aPolygon));
108 aNormal.setX(fabs(aNormal.getX()));
109 aNormal.setY(fabs(aNormal.getY()));
110 aNormal.setZ(fabs(aNormal.getZ()));
112 // Decide which coordinates should be used as a source for the mapping
113 sal_uInt16 nSourceMode = 0;
115 // Determine the greatest degree of freedom
116 if(!(aNormal.getX() > aNormal.getY() && aNormal.getX() > aNormal.getZ()))
118 if(aNormal.getY() > aNormal.getZ())
120 // Y is the largest, use X,Z as mapping
121 nSourceMode = 1;
123 else
125 // Z is the largest, use X,Y as mapping
126 nSourceMode = 2;
130 // Create new polygon for texture coordinates
131 basegfx::B2DPolygon aTexture;
133 // Fill new polygon
134 for(sal_uInt32 b(0L); b < aPolygon.count(); b++)
136 basegfx::B2DPoint aTex;
137 const basegfx::B3DPoint aCandidate(aPolygon.getB3DPoint(b));
139 switch(nSourceMode)
141 case 0: //Source is Y,Z
142 if(aVolume.getHeight())
143 aTex.setX((aCandidate.getY() - aVolume.getMinY()) / aVolume.getHeight());
144 if(aVolume.getDepth())
145 aTex.setY((aCandidate.getZ() - aVolume.getMinZ()) / aVolume.getDepth());
146 break;
148 case 1: // Source is X,Z
149 if(aVolume.getWidth())
150 aTex.setX((aCandidate.getX() - aVolume.getMinX()) / aVolume.getWidth());
151 if(aVolume.getDepth())
152 aTex.setY((aCandidate.getZ() - aVolume.getMinZ()) / aVolume.getDepth());
153 break;
155 case 2: // Source is X,Y
156 if(aVolume.getWidth())
157 aTex.setX((aCandidate.getX() - aVolume.getMinX()) / aVolume.getWidth());
158 if(aVolume.getHeight())
159 aTex.setY((aCandidate.getY() - aVolume.getMinY()) / aVolume.getHeight());
160 break;
163 aTexture.append(aTex);
166 // Insert new polygon into the PolyPolygon
167 aPolyTexture.append(aTexture);
170 // Set default Texture coordinates
171 SetPolyTexture2D(aPolyTexture);
174 E3dPolygonObj::~E3dPolygonObj()
178 sal_uInt16 E3dPolygonObj::GetObjIdentifier() const
180 return E3D_POLYGONOBJ_ID;
183 void E3dPolygonObj::SetPolyPolygon3D(const basegfx::B3DPolyPolygon& rNewPolyPoly3D)
185 if ( aPolyPoly3D != rNewPolyPoly3D )
187 // New PolyPolygon; copying
188 aPolyPoly3D = rNewPolyPoly3D;
190 // Create new geometry
191 ActionChanged();
195 void E3dPolygonObj::SetPolyNormals3D(const basegfx::B3DPolyPolygon& rNewPolyNormals3D)
197 if ( aPolyNormals3D != rNewPolyNormals3D )
199 // New PolyPolygon; copying
200 aPolyNormals3D = rNewPolyNormals3D;
202 // Create new geometry
203 ActionChanged();
207 void E3dPolygonObj::SetPolyTexture2D(const basegfx::B2DPolyPolygon& rNewPolyTexture2D)
209 if ( aPolyTexture2D != rNewPolyTexture2D )
211 // New PolyPolygon; copying
212 aPolyTexture2D = rNewPolyTexture2D;
214 // Create new geometry
215 ActionChanged();
219 // Convert the object into a group object consisting of 6 polygons
221 SdrObject *E3dPolygonObj::DoConvertToPolyObj(bool /*bBezier*/, bool /*bAddText*/) const
223 return NULL;
226 E3dPolygonObj* E3dPolygonObj::Clone() const
228 return CloneHelper< E3dPolygonObj >();
231 void E3dPolygonObj::SetLineOnly(bool bNew)
233 if(bNew != bLineOnly)
235 bLineOnly = bNew;
236 ActionChanged();
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */