Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / drawinglayer / source / primitive3d / sdrsphereprimitive3d.cxx
blob449304d381a0fc9bb6df9ee381969ae87f1d8459
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 <drawinglayer/primitive3d/sdrsphereprimitive3d.hxx>
21 #include <basegfx/polygon/b3dpolypolygontools.hxx>
22 #include <basegfx/matrix/b2dhommatrix.hxx>
23 #include <basegfx/polygon/b3dpolygon.hxx>
24 #include <drawinglayer/primitive3d/sdrdecompositiontools3d.hxx>
25 #include <basegfx/utils/canvastools.hxx>
26 #include <drawinglayer/primitive3d/drawinglayer_primitivetypes3d.hxx>
27 #include <drawinglayer/attribute/sdrfillattribute.hxx>
28 #include <drawinglayer/attribute/sdrlineattribute.hxx>
29 #include <drawinglayer/attribute/sdrshadowattribute.hxx>
32 using namespace com::sun::star;
35 namespace drawinglayer
37 namespace primitive3d
39 Primitive3DContainer SdrSpherePrimitive3D::create3DDecomposition(const geometry::ViewInformation3D& /*rViewInformation*/) const
41 Primitive3DContainer aRetval;
42 const basegfx::B3DRange aUnitRange(0.0, 0.0, 0.0, 1.0, 1.0, 1.0);
43 const bool bCreateNormals(css::drawing::NormalsKind_SPECIFIC == getSdr3DObjectAttribute().getNormalsKind()
44 || css::drawing::NormalsKind_SPHERE == getSdr3DObjectAttribute().getNormalsKind());
46 // create unit geometry
47 basegfx::B3DPolyPolygon aFill(basegfx::utils::createSphereFillPolyPolygonFromB3DRange(aUnitRange,
48 getHorizontalSegments(), getVerticalSegments(), bCreateNormals));
50 // normal inversion
51 if(!getSdrLFSAttribute().getFill().isDefault()
52 && bCreateNormals
53 && getSdr3DObjectAttribute().getNormalsInvert()
54 && aFill.areNormalsUsed())
56 // invert normals
57 aFill = basegfx::utils::invertNormals(aFill);
60 // texture coordinates
61 if(!getSdrLFSAttribute().getFill().isDefault())
63 // handle texture coordinates X
64 const bool bParallelX(css::drawing::TextureProjectionMode_PARALLEL == getSdr3DObjectAttribute().getTextureProjectionX());
65 const bool bObjectSpecificX(css::drawing::TextureProjectionMode_OBJECTSPECIFIC == getSdr3DObjectAttribute().getTextureProjectionX());
66 const bool bSphereX(css::drawing::TextureProjectionMode_SPHERE == getSdr3DObjectAttribute().getTextureProjectionX());
68 // handle texture coordinates Y
69 const bool bParallelY(css::drawing::TextureProjectionMode_PARALLEL == getSdr3DObjectAttribute().getTextureProjectionY());
70 const bool bObjectSpecificY(css::drawing::TextureProjectionMode_OBJECTSPECIFIC == getSdr3DObjectAttribute().getTextureProjectionY());
71 const bool bSphereY(css::drawing::TextureProjectionMode_SPHERE == getSdr3DObjectAttribute().getTextureProjectionY());
73 if(bParallelX || bParallelY)
75 // apply parallel texture coordinates in X and/or Y
76 const basegfx::B3DRange aRange(basegfx::utils::getRange(aFill));
77 aFill = basegfx::utils::applyDefaultTextureCoordinatesParallel(aFill, aRange, bParallelX, bParallelY);
80 if(bSphereX || bObjectSpecificX || bSphereY || bObjectSpecificY)
82 double fRelativeAngle(0.0);
84 if(bObjectSpecificX)
86 // Since the texture coordinates are (for historical reasons)
87 // different from forced to sphere texture coordinates,
88 // create a old version from it by rotating to old state before applying
89 // the texture coordinates to emulate old behaviour
90 fRelativeAngle = F_2PI * (static_cast<double>((getHorizontalSegments() >> 1) - 1) / static_cast<double>(getHorizontalSegments()));
91 basegfx::B3DHomMatrix aRot;
92 aRot.rotate(0.0, fRelativeAngle, 0.0);
93 aFill.transform(aRot);
96 // apply spherical texture coordinates in X and/or Y
97 const basegfx::B3DRange aRange(basegfx::utils::getRange(aFill));
98 const basegfx::B3DPoint aCenter(aRange.getCenter());
99 aFill = basegfx::utils::applyDefaultTextureCoordinatesSphere(aFill, aCenter,
100 bSphereX || bObjectSpecificX, bSphereY || bObjectSpecificY);
102 if(bObjectSpecificX)
104 // rotate back again
105 basegfx::B3DHomMatrix aRot;
106 aRot.rotate(0.0, -fRelativeAngle, 0.0);
107 aFill.transform(aRot);
111 // transform texture coordinates to texture size
112 basegfx::B2DHomMatrix aTexMatrix;
113 aTexMatrix.scale(getTextureSize().getX(), getTextureSize().getY());
114 aFill.transformTextureCoordinates(aTexMatrix);
117 // build vector of PolyPolygons
118 std::vector< basegfx::B3DPolyPolygon > a3DPolyPolygonVector;
120 for(sal_uInt32 a(0); a < aFill.count(); a++)
122 a3DPolyPolygonVector.emplace_back(aFill.getB3DPolygon(a));
125 if(!getSdrLFSAttribute().getFill().isDefault())
127 // add fill
128 aRetval = create3DPolyPolygonFillPrimitives(
129 a3DPolyPolygonVector,
130 getTransform(),
131 getTextureSize(),
132 getSdr3DObjectAttribute(),
133 getSdrLFSAttribute().getFill(),
134 getSdrLFSAttribute().getFillFloatTransGradient());
136 else
138 // create simplified 3d hit test geometry
139 aRetval = createHiddenGeometryPrimitives3D(
140 a3DPolyPolygonVector,
141 getTransform(),
142 getTextureSize(),
143 getSdr3DObjectAttribute());
146 // add line
147 if(!getSdrLFSAttribute().getLine().isDefault())
149 basegfx::B3DPolyPolygon aSphere(basegfx::utils::createSpherePolyPolygonFromB3DRange(aUnitRange, getHorizontalSegments(), getVerticalSegments()));
150 const Primitive3DContainer aLines(create3DPolyPolygonLinePrimitives(
151 aSphere, getTransform(), getSdrLFSAttribute().getLine()));
152 aRetval.append(aLines);
155 // add shadow
156 if(!getSdrLFSAttribute().getShadow().isDefault()
157 && !aRetval.empty())
159 const Primitive3DContainer aShadow(createShadowPrimitive3D(
160 aRetval, getSdrLFSAttribute().getShadow(), getSdr3DObjectAttribute().getShadow3D()));
161 aRetval.append(aShadow);
164 return aRetval;
167 SdrSpherePrimitive3D::SdrSpherePrimitive3D(
168 const basegfx::B3DHomMatrix& rTransform,
169 const basegfx::B2DVector& rTextureSize,
170 const attribute::SdrLineFillShadowAttribute3D& rSdrLFSAttribute,
171 const attribute::Sdr3DObjectAttribute& rSdr3DObjectAttribute,
172 sal_uInt32 nHorizontalSegments,
173 sal_uInt32 nVerticalSegments)
174 : SdrPrimitive3D(rTransform, rTextureSize, rSdrLFSAttribute, rSdr3DObjectAttribute),
175 mnHorizontalSegments(nHorizontalSegments),
176 mnVerticalSegments(nVerticalSegments)
180 bool SdrSpherePrimitive3D::operator==(const BasePrimitive3D& rPrimitive) const
182 if(SdrPrimitive3D::operator==(rPrimitive))
184 const SdrSpherePrimitive3D& rCompare = static_cast< const SdrSpherePrimitive3D& >(rPrimitive);
186 return (getHorizontalSegments() == rCompare.getHorizontalSegments()
187 && getVerticalSegments() == rCompare.getVerticalSegments());
190 return false;
193 basegfx::B3DRange SdrSpherePrimitive3D::getB3DRange(const geometry::ViewInformation3D& /*rViewInformation*/) const
195 // use default from sdrPrimitive3D which uses transformation expanded by line width/2
196 // The parent implementation which uses the ranges of the decomposition would be more
197 // correct, but for historical reasons it is necessary to do the old method: To get
198 // the range of the non-transformed geometry and transform it then. This leads to different
199 // ranges where the new method is more correct, but the need to keep the old behaviour
200 // has priority here.
201 return getStandard3DRange();
204 // provide unique ID
205 ImplPrimitive3DIDBlock(SdrSpherePrimitive3D, PRIMITIVE3D_ID_SDRSPHEREPRIMITIVE3D)
207 } // end of namespace primitive3d
208 } // end of namespace drawinglayer
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */