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 .
21 #include <sdr/contact/viewcontactofe3dpolygon.hxx>
22 #include <polygn3d.hxx>
23 #include <drawinglayer/primitive3d/sdrpolypolygonprimitive3d.hxx>
24 #include <sdr/primitive2d/sdrattributecreator.hxx>
25 #include <sdr/primitive3d/sdrattributecreator3d.hxx>
26 #include <basegfx/polygon/b3dpolygon.hxx>
27 #include <basegfx/polygon/b3dpolypolygontools.hxx>
30 namespace sdr::contact
32 ViewContactOfE3dPolygon::ViewContactOfE3dPolygon(E3dPolygonObj
& rPolygon
)
33 : ViewContactOfE3d(rPolygon
)
37 ViewContactOfE3dPolygon::~ViewContactOfE3dPolygon()
41 drawinglayer::primitive3d::Primitive3DContainer
ViewContactOfE3dPolygon::createViewIndependentPrimitive3DContainer() const
43 drawinglayer::primitive3d::Primitive3DContainer xRetval
;
44 const SfxItemSet
& rItemSet
= GetE3dPolygonObj().GetMergedItemSet();
45 const bool bSuppressFill(GetE3dPolygonObj().GetLineOnly());
46 const drawinglayer::attribute::SdrLineFillShadowAttribute3D
aAttribute(
47 drawinglayer::primitive2d::createNewSdrLineFillShadowAttribute(rItemSet
, bSuppressFill
));
49 // get extrude geometry
50 basegfx::B3DPolyPolygon
aPolyPolygon3D(GetE3dPolygonObj().GetPolyPolygon3D());
51 const basegfx::B3DPolyPolygon
aPolyNormals3D(GetE3dPolygonObj().GetPolyNormals3D());
52 const basegfx::B2DPolyPolygon
aPolyTexture2D(GetE3dPolygonObj().GetPolyTexture2D());
53 const bool bNormals(aPolyNormals3D
.count() && aPolyNormals3D
.count() == aPolyPolygon3D
.count());
54 const bool bTexture(aPolyTexture2D
.count() && aPolyTexture2D
.count() == aPolyPolygon3D
.count());
56 if(bNormals
|| bTexture
)
58 for(sal_uInt32
a(0); a
< aPolyPolygon3D
.count(); a
++)
60 basegfx::B3DPolygon
aCandidate3D(aPolyPolygon3D
.getB3DPolygon(a
));
61 basegfx::B3DPolygon aNormals3D
;
62 basegfx::B2DPolygon aTexture2D
;
66 aNormals3D
= aPolyNormals3D
.getB3DPolygon(a
);
71 aTexture2D
= aPolyTexture2D
.getB2DPolygon(a
);
74 for(sal_uInt32
b(0); b
< aCandidate3D
.count(); b
++)
78 sal_uInt32 nNormalCount
= aNormals3D
.count();
79 if( b
< nNormalCount
)
80 aCandidate3D
.setNormal(b
, aNormals3D
.getB3DPoint(b
));
81 else if( nNormalCount
> 0 )
82 aCandidate3D
.setNormal(b
, aNormals3D
.getB3DPoint(0));
86 sal_uInt32 nTextureCount
= aTexture2D
.count();
87 if( b
< nTextureCount
)
88 aCandidate3D
.setTextureCoordinate(b
, aTexture2D
.getB2DPoint(b
));
89 else if( nTextureCount
> 0 )
90 aCandidate3D
.setTextureCoordinate(b
, aTexture2D
.getB2DPoint(0));
94 aPolyPolygon3D
.setB3DPolygon(a
, aCandidate3D
);
98 // get 3D Object Attributes
99 drawinglayer::attribute::Sdr3DObjectAttribute
aSdr3DObjectAttribute(drawinglayer::primitive2d::createNewSdr3DObjectAttribute(rItemSet
));
101 // calculate texture size
102 basegfx::B2DVector
aTextureSize(1.0, 1.0);
107 // create texture size from object's size
108 const basegfx::B3DRange
aObjectRange(basegfx::utils::getRange(aPolyPolygon3D
));
113 // this is a polygon object, so Width/Height and/or Depth may be zero (e.g. left
114 // wall of chart). Take this into account
115 if(basegfx::fTools::equalZero(aObjectRange
.getWidth()))
117 // width is zero, use height and depth
118 fWidth
= aObjectRange
.getHeight();
119 fHeight
= aObjectRange
.getDepth();
121 else if(basegfx::fTools::equalZero(aObjectRange
.getHeight()))
123 // height is zero, use width and depth
124 fWidth
= aObjectRange
.getWidth();
125 fHeight
= aObjectRange
.getDepth();
129 // use width and height
130 fWidth
= aObjectRange
.getWidth();
131 fHeight
= aObjectRange
.getHeight();
134 if(fWidth
<= 0.0 || fHeight
<= 0.0)
136 // no texture; fallback to very small size
137 aTextureSize
.setX(0.01);
138 aTextureSize
.setY(0.01);
142 aTextureSize
.setX(fWidth
);
143 aTextureSize
.setY(fHeight
);
148 // unfortunately, this SdrObject type which allows a free 3d geometry definition was defined
149 // wrong topologically in relation to its plane normal and 3D visibility when it was invented
150 // a long time ago. Since the API allows creation of this SDrObject type, it is not possible to
151 // simply change this definition. Only the chart should use it, and at least this object type
152 // only exists at Runtime (is not saved and/or loaded in any FileFormat). Still someone external
153 // may have used it in its API. To not risk wrong 3D lightings, I have to switch the orientation
154 // of the polygon here
155 aPolyPolygon3D
.flip();
157 // create primitive and add
158 const basegfx::B3DHomMatrix aWorldTransform
;
159 const drawinglayer::primitive3d::Primitive3DReference
xReference(
160 new drawinglayer::primitive3d::SdrPolyPolygonPrimitive3D(
161 std::move(aPolyPolygon3D
), aWorldTransform
, aTextureSize
, aAttribute
, aSdr3DObjectAttribute
));
162 xRetval
= { xReference
};
167 } // end of namespace
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */