1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
)
38 SetPolyPolygon3D(rPolyPoly3D
);
40 // Create default normals
41 CreateDefaultNormals();
43 // Create default texture coordinates
44 CreateDefaultTexture();
47 E3dPolygonObj::E3dPolygonObj(SdrModel
& rSdrModel
)
48 : E3dCompoundObject(rSdrModel
)
54 void E3dPolygonObj::CreateDefaultNormals()
56 basegfx::B3DPolyPolygon aPolyNormals
;
58 // Create a complete tools::PolyPolygon with the plane normal
59 for (sal_uInt32
a(0); a
< aPolyPoly3D
.count(); a
++)
61 // Find source polygon
62 const basegfx::B3DPolygon
aPolygon(aPolyPoly3D
.getB3DPolygon(a
));
64 // Creating a new polygon for the normal
65 basegfx::B3DPolygon aNormals
;
67 // Get normal (and invert)
68 basegfx::B3DVector
aNormal(-aPolygon
.getNormal());
71 for (sal_uInt32
b(0); b
< aPolygon
.count(); b
++)
73 aNormals
.append(aNormal
);
76 // Insert new polygon into the PolyPolygon
77 aPolyNormals
.append(aNormals
);
81 SetPolyNormals3D(aPolyNormals
);
84 void E3dPolygonObj::CreateDefaultTexture()
86 basegfx::B2DPolyPolygon aPolyTexture
;
87 // Create a complete tools::PolyPolygon with the texture coordinates
88 // The texture coordinates extend over X,Y and Z
89 // on the whole extreme values in the range 0.0 .. 1.0
90 for (sal_uInt32
a(0); a
< aPolyPoly3D
.count(); a
++)
92 // Find source polygon
93 const basegfx::B3DPolygon
& aPolygon(aPolyPoly3D
.getB3DPolygon(a
));
95 // Determine the total size of the object
96 basegfx::B3DRange
aVolume(basegfx::utils::getRange(aPolygon
));
99 basegfx::B3DVector
aNormal(aPolygon
.getNormal());
100 aNormal
.setX(fabs(aNormal
.getX()));
101 aNormal
.setY(fabs(aNormal
.getY()));
102 aNormal
.setZ(fabs(aNormal
.getZ()));
104 // Decide which coordinates should be used as a source for the mapping
105 sal_uInt16 nSourceMode
= 0;
107 // Determine the greatest degree of freedom
108 if (aNormal
.getX() <= aNormal
.getY() || aNormal
.getX() <= aNormal
.getZ())
110 if (aNormal
.getY() > aNormal
.getZ())
112 // Y is the largest, use X,Z as mapping
117 // Z is the largest, use X,Y as mapping
122 // Create new polygon for texture coordinates
123 basegfx::B2DPolygon aTexture
;
126 for (sal_uInt32
b(0); b
< aPolygon
.count(); b
++)
128 basegfx::B2DPoint aTex
;
129 const basegfx::B3DPoint
aCandidate(aPolygon
.getB3DPoint(b
));
133 case 0: //Source is Y,Z
134 if (aVolume
.getHeight())
135 aTex
.setX((aCandidate
.getY() - aVolume
.getMinY()) / aVolume
.getHeight());
136 if (aVolume
.getDepth())
137 aTex
.setY((aCandidate
.getZ() - aVolume
.getMinZ()) / aVolume
.getDepth());
140 case 1: // Source is X,Z
141 if (aVolume
.getWidth())
142 aTex
.setX((aCandidate
.getX() - aVolume
.getMinX()) / aVolume
.getWidth());
143 if (aVolume
.getDepth())
144 aTex
.setY((aCandidate
.getZ() - aVolume
.getMinZ()) / aVolume
.getDepth());
147 case 2: // Source is X,Y
148 if (aVolume
.getWidth())
149 aTex
.setX((aCandidate
.getX() - aVolume
.getMinX()) / aVolume
.getWidth());
150 if (aVolume
.getHeight())
151 aTex
.setY((aCandidate
.getY() - aVolume
.getMinY()) / aVolume
.getHeight());
155 aTexture
.append(aTex
);
158 // Insert new polygon into the PolyPolygon
159 aPolyTexture
.append(aTexture
);
162 // Set default Texture coordinates
163 SetPolyTexture2D(aPolyTexture
);
166 E3dPolygonObj::~E3dPolygonObj() {}
168 SdrObjKind
E3dPolygonObj::GetObjIdentifier() const { return E3D_POLYGONOBJ_ID
; }
170 void E3dPolygonObj::SetPolyPolygon3D(const basegfx::B3DPolyPolygon
& rNewPolyPoly3D
)
172 if (aPolyPoly3D
!= rNewPolyPoly3D
)
174 // New PolyPolygon; copying
175 aPolyPoly3D
= rNewPolyPoly3D
;
177 // Create new geometry
182 void E3dPolygonObj::SetPolyNormals3D(const basegfx::B3DPolyPolygon
& rNewPolyNormals3D
)
184 if (aPolyNormals3D
!= rNewPolyNormals3D
)
186 // New PolyPolygon; copying
187 aPolyNormals3D
= rNewPolyNormals3D
;
189 // Create new geometry
194 void E3dPolygonObj::SetPolyTexture2D(const basegfx::B2DPolyPolygon
& rNewPolyTexture2D
)
196 if (aPolyTexture2D
!= rNewPolyTexture2D
)
198 // New PolyPolygon; copying
199 aPolyTexture2D
= rNewPolyTexture2D
;
201 // Create new geometry
206 // Convert the object into a group object consisting of 6 polygons
208 SdrObjectUniquePtr
E3dPolygonObj::DoConvertToPolyObj(bool /*bBezier*/, bool /*bAddText*/) const
213 E3dPolygonObj
* E3dPolygonObj::CloneSdrObject(SdrModel
& rTargetModel
) const
215 return CloneHelper
<E3dPolygonObj
>(rTargetModel
);
218 E3dPolygonObj
& E3dPolygonObj::operator=(const E3dPolygonObj
& rObj
)
222 E3dCompoundObject::operator=(rObj
);
224 aPolyPoly3D
= rObj
.aPolyPoly3D
;
225 aPolyNormals3D
= rObj
.aPolyNormals3D
;
226 aPolyTexture2D
= rObj
.aPolyTexture2D
;
227 bLineOnly
= rObj
.bLineOnly
;
232 void E3dPolygonObj::SetLineOnly(bool bNew
)
234 if (bNew
!= bLineOnly
)
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */