Revert "Revert "Revert "stronger typing for SwClient::GetRegisteredIn"" and fix SwIte...
[LibreOffice.git] / svx / source / engine3d / polygn3d.cxx
blob3303cfe77d60bb193db6bdd31e0af5a8bb708bd9
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 <polygn3d.hxx>
21 #include <svx/svdobjkind.hxx>
22 #include <basegfx/point/b3dpoint.hxx>
23 #include <sdr/contact/viewcontactofe3dpolygon.hxx>
24 #include <basegfx/polygon/b3dpolygon.hxx>
25 #include <basegfx/polygon/b3dpolygontools.hxx>
27 // DrawContact section
28 std::unique_ptr<sdr::contact::ViewContact> E3dPolygonObj::CreateObjectSpecificViewContact()
30 return std::make_unique<sdr::contact::ViewContactOfE3dPolygon>(*this);
33 E3dPolygonObj::E3dPolygonObj(SdrModel& rSdrModel, const basegfx::B3DPolyPolygon& rPolyPoly3D)
34 : E3dCompoundObject(rSdrModel)
35 , bLineOnly(true)
37 // Set geometry
38 SetPolyPolygon3D(rPolyPoly3D);
40 // Create default normals
41 CreateDefaultNormals();
43 // Create default texture coordinates
44 CreateDefaultTexture();
47 E3dPolygonObj::E3dPolygonObj(SdrModel& rSdrModel)
48 : E3dCompoundObject(rSdrModel)
49 , bLineOnly(false)
51 // Create no geometry
54 E3dPolygonObj::E3dPolygonObj(SdrModel& rSdrModel, E3dPolygonObj const& rSource)
55 : E3dCompoundObject(rSdrModel, rSource)
56 , bLineOnly(false)
58 // Create no geometry
60 aPolyPoly3D = rSource.aPolyPoly3D;
61 aPolyNormals3D = rSource.aPolyNormals3D;
62 aPolyTexture2D = rSource.aPolyTexture2D;
63 bLineOnly = rSource.bLineOnly;
66 void E3dPolygonObj::CreateDefaultNormals()
68 basegfx::B3DPolyPolygon aPolyNormals;
70 // Create a complete tools::PolyPolygon with the plane normal
71 for (sal_uInt32 a(0); a < aPolyPoly3D.count(); a++)
73 // Find source polygon
74 const basegfx::B3DPolygon aPolygon(aPolyPoly3D.getB3DPolygon(a));
76 // Creating a new polygon for the normal
77 basegfx::B3DPolygon aNormals;
79 // Get normal (and invert)
80 basegfx::B3DVector aNormal(-aPolygon.getNormal());
82 // Fill new polygon
83 for (sal_uInt32 b(0); b < aPolygon.count(); b++)
85 aNormals.append(aNormal);
88 // Insert new polygon into the PolyPolygon
89 aPolyNormals.append(aNormals);
92 // Set default normal
93 SetPolyNormals3D(aPolyNormals);
96 void E3dPolygonObj::CreateDefaultTexture()
98 basegfx::B2DPolyPolygon aPolyTexture;
99 // Create a complete tools::PolyPolygon with the texture coordinates
100 // The texture coordinates extend over X,Y and Z
101 // on the whole extreme values in the range 0.0 .. 1.0
102 for (sal_uInt32 a(0); a < aPolyPoly3D.count(); a++)
104 // Find source polygon
105 const basegfx::B3DPolygon& aPolygon(aPolyPoly3D.getB3DPolygon(a));
107 // Determine the total size of the object
108 basegfx::B3DRange aVolume(basegfx::utils::getRange(aPolygon));
110 // Get normal
111 basegfx::B3DVector aNormal(aPolygon.getNormal());
112 aNormal.setX(fabs(aNormal.getX()));
113 aNormal.setY(fabs(aNormal.getY()));
114 aNormal.setZ(fabs(aNormal.getZ()));
116 // Decide which coordinates should be used as a source for the mapping
117 sal_uInt16 nSourceMode = 0;
119 // Determine the greatest degree of freedom
120 if (aNormal.getX() <= aNormal.getY() || aNormal.getX() <= aNormal.getZ())
122 if (aNormal.getY() > aNormal.getZ())
124 // Y is the largest, use X,Z as mapping
125 nSourceMode = 1;
127 else
129 // Z is the largest, use X,Y as mapping
130 nSourceMode = 2;
134 // Create new polygon for texture coordinates
135 basegfx::B2DPolygon aTexture;
137 // Fill new polygon
138 for (sal_uInt32 b(0); b < aPolygon.count(); b++)
140 basegfx::B2DPoint aTex;
141 const basegfx::B3DPoint aCandidate(aPolygon.getB3DPoint(b));
143 switch (nSourceMode)
145 case 0: //Source is Y,Z
146 if (aVolume.getHeight())
147 aTex.setX((aCandidate.getY() - aVolume.getMinY()) / aVolume.getHeight());
148 if (aVolume.getDepth())
149 aTex.setY((aCandidate.getZ() - aVolume.getMinZ()) / aVolume.getDepth());
150 break;
152 case 1: // Source is X,Z
153 if (aVolume.getWidth())
154 aTex.setX((aCandidate.getX() - aVolume.getMinX()) / aVolume.getWidth());
155 if (aVolume.getDepth())
156 aTex.setY((aCandidate.getZ() - aVolume.getMinZ()) / aVolume.getDepth());
157 break;
159 case 2: // Source is X,Y
160 if (aVolume.getWidth())
161 aTex.setX((aCandidate.getX() - aVolume.getMinX()) / aVolume.getWidth());
162 if (aVolume.getHeight())
163 aTex.setY((aCandidate.getY() - aVolume.getMinY()) / aVolume.getHeight());
164 break;
167 aTexture.append(aTex);
170 // Insert new polygon into the PolyPolygon
171 aPolyTexture.append(aTexture);
174 // Set default Texture coordinates
175 SetPolyTexture2D(aPolyTexture);
178 E3dPolygonObj::~E3dPolygonObj() {}
180 SdrObjKind E3dPolygonObj::GetObjIdentifier() const { return SdrObjKind::E3D_Polygon; }
182 void E3dPolygonObj::SetPolyPolygon3D(const basegfx::B3DPolyPolygon& rNewPolyPoly3D)
184 if (aPolyPoly3D != rNewPolyPoly3D)
186 // New PolyPolygon; copying
187 aPolyPoly3D = rNewPolyPoly3D;
189 // Create new geometry
190 ActionChanged();
194 void E3dPolygonObj::SetPolyNormals3D(const basegfx::B3DPolyPolygon& rNewPolyNormals3D)
196 if (aPolyNormals3D != rNewPolyNormals3D)
198 // New PolyPolygon; copying
199 aPolyNormals3D = rNewPolyNormals3D;
201 // Create new geometry
202 ActionChanged();
206 void E3dPolygonObj::SetPolyTexture2D(const basegfx::B2DPolyPolygon& rNewPolyTexture2D)
208 if (aPolyTexture2D != rNewPolyTexture2D)
210 // New PolyPolygon; copying
211 aPolyTexture2D = rNewPolyTexture2D;
213 // Create new geometry
214 ActionChanged();
218 // Convert the object into a group object consisting of 6 polygons
220 rtl::Reference<SdrObject> E3dPolygonObj::DoConvertToPolyObj(bool /*bBezier*/,
221 bool /*bAddText*/) const
223 return nullptr;
226 rtl::Reference<SdrObject> E3dPolygonObj::CloneSdrObject(SdrModel& rTargetModel) const
228 return new E3dPolygonObj(rTargetModel, *this);
231 void E3dPolygonObj::SetLineOnly(bool bNew)
233 if (bNew != bLineOnly)
235 bLineOnly = bNew;
236 ActionChanged();
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */