Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / drawinglayer / source / primitive3d / sdrextrudeprimitive3d.cxx
blob94fb0f34a0d9faed954b5140c2ec1e7349e1c3db
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 <sal/config.h>
22 #include <cmath>
24 #include <drawinglayer/primitive3d/sdrextrudeprimitive3d.hxx>
25 #include <basegfx/matrix/b2dhommatrix.hxx>
26 #include <basegfx/polygon/b2dpolygontools.hxx>
27 #include <basegfx/polygon/b3dpolypolygontools.hxx>
28 #include <drawinglayer/primitive3d/sdrdecompositiontools3d.hxx>
29 #include <basegfx/utils/canvastools.hxx>
30 #include <drawinglayer/primitive3d/drawinglayer_primitivetypes3d.hxx>
31 #include <drawinglayer/geometry/viewinformation3d.hxx>
32 #include <drawinglayer/attribute/sdrfillattribute.hxx>
33 #include <drawinglayer/attribute/sdrlineattribute.hxx>
34 #include <drawinglayer/attribute/sdrshadowattribute.hxx>
37 using namespace com::sun::star;
40 namespace drawinglayer
42 namespace primitive3d
44 Primitive3DContainer SdrExtrudePrimitive3D::create3DDecomposition(const geometry::ViewInformation3D& rViewInformation) const
46 Primitive3DContainer aRetval;
48 // get slices
49 const Slice3DVector& rSliceVector = getSlices();
51 if(!rSliceVector.empty())
53 sal_uInt32 a;
55 // decide what to create
56 const css::drawing::NormalsKind eNormalsKind(getSdr3DObjectAttribute().getNormalsKind());
57 const bool bCreateNormals(css::drawing::NormalsKind_SPECIFIC == eNormalsKind);
58 const bool bCreateTextureCoordinatesX(css::drawing::TextureProjectionMode_OBJECTSPECIFIC == getSdr3DObjectAttribute().getTextureProjectionX());
59 const bool bCreateTextureCoordinatesY(css::drawing::TextureProjectionMode_OBJECTSPECIFIC == getSdr3DObjectAttribute().getTextureProjectionY());
60 basegfx::B2DHomMatrix aTexTransform;
62 if(!getSdrLFSAttribute().getFill().isDefault() && (bCreateTextureCoordinatesX || bCreateTextureCoordinatesY))
64 const basegfx::B2DPolygon aFirstPolygon(maCorrectedPolyPolygon.getB2DPolygon(0));
65 const double fFrontLength(basegfx::utils::getLength(aFirstPolygon));
66 const double fFrontArea(basegfx::utils::getArea(aFirstPolygon));
67 const double fSqrtFrontArea(sqrt(fFrontArea));
68 double fRelativeTextureWidth = basegfx::fTools::equalZero(fSqrtFrontArea) ? 1.0 : fFrontLength / fSqrtFrontArea;
69 fRelativeTextureWidth = std::trunc(fRelativeTextureWidth - 0.5);
71 if(fRelativeTextureWidth < 1.0)
73 fRelativeTextureWidth = 1.0;
76 aTexTransform.translate(-0.5, -0.5);
77 aTexTransform.scale(-1.0, -1.0);
78 aTexTransform.translate(0.5, 0.5);
79 aTexTransform.scale(fRelativeTextureWidth, 1.0);
82 // create geometry
83 std::vector< basegfx::B3DPolyPolygon > aFill;
84 extractPlanesFromSlice(aFill, rSliceVector,
85 bCreateNormals, getSmoothNormals(), getSmoothLids(), false,
86 0.5, 0.6, bCreateTextureCoordinatesX || bCreateTextureCoordinatesY, aTexTransform);
88 // get full range
89 const basegfx::B3DRange aRange(getRangeFrom3DGeometry(aFill));
91 // normal creation
92 if(!getSdrLFSAttribute().getFill().isDefault())
94 if(css::drawing::NormalsKind_SPHERE == eNormalsKind)
96 applyNormalsKindSphereTo3DGeometry(aFill, aRange);
98 else if(css::drawing::NormalsKind_FLAT == eNormalsKind)
100 applyNormalsKindFlatTo3DGeometry(aFill);
103 if(getSdr3DObjectAttribute().getNormalsInvert())
105 applyNormalsInvertTo3DGeometry(aFill);
109 // texture coordinates
110 if(!getSdrLFSAttribute().getFill().isDefault())
112 applyTextureTo3DGeometry(
113 getSdr3DObjectAttribute().getTextureProjectionX(),
114 getSdr3DObjectAttribute().getTextureProjectionY(),
115 aFill,
116 aRange,
117 getTextureSize());
120 if(!getSdrLFSAttribute().getFill().isDefault())
122 // add fill
123 aRetval = create3DPolyPolygonFillPrimitives(
124 aFill,
125 getTransform(),
126 getTextureSize(),
127 getSdr3DObjectAttribute(),
128 getSdrLFSAttribute().getFill(),
129 getSdrLFSAttribute().getFillFloatTransGradient());
131 else
133 // create simplified 3d hit test geometry
134 aRetval = createHiddenGeometryPrimitives3D(
135 aFill,
136 getTransform(),
137 getTextureSize(),
138 getSdr3DObjectAttribute());
141 // add line
142 if(!getSdrLFSAttribute().getLine().isDefault())
144 if(getSdr3DObjectAttribute().getReducedLineGeometry())
146 // create geometric outlines with reduced line geometry for chart.
147 const basegfx::B3DPolyPolygon aVerLine(extractVerticalLinesFromSlice(rSliceVector));
148 const sal_uInt32 nCount(aVerLine.count());
149 basegfx::B3DPolyPolygon aReducedLoops;
150 basegfx::B3DPolyPolygon aNewLineGeometry;
152 // sort out doubles (front and back planes when no edge rounding is done). Since
153 // this is a line geometry merged from PolyPolygons, loop over all Polygons
154 for(a = 0; a < nCount; a++)
156 const sal_uInt32 nReducedCount(aReducedLoops.count());
157 const basegfx::B3DPolygon& aCandidate(aVerLine.getB3DPolygon(a));
158 bool bAdd(true);
160 if(nReducedCount)
162 for(sal_uInt32 b(0); bAdd && b < nReducedCount; b++)
164 if(aCandidate == aReducedLoops.getB3DPolygon(b))
166 bAdd = false;
171 if(bAdd)
173 aReducedLoops.append(aCandidate);
177 // from here work with reduced loops and reduced count without changing them
178 const sal_uInt32 nReducedCount(aReducedLoops.count());
180 if(nReducedCount > 1)
182 for(sal_uInt32 b(1); b < nReducedCount; b++)
184 // get loop pair
185 const basegfx::B3DPolygon& aCandA(aReducedLoops.getB3DPolygon(b - 1));
186 const basegfx::B3DPolygon& aCandB(aReducedLoops.getB3DPolygon(b));
188 // for each loop pair create the connection edges
189 createReducedOutlines(
190 rViewInformation,
191 getTransform(),
192 aCandA,
193 aCandB,
194 aNewLineGeometry);
198 // add reduced loops themselves
199 aNewLineGeometry.append(aReducedLoops);
201 // to create vertical edges at non-C1/C2 steady loops, use maCorrectedPolyPolygon
202 // directly since the 3D Polygons do not support this.
204 // Unfortunately there is no bezier polygon provided by the chart module; one reason is
205 // that the API for extrude wants a 3D polygon geometry (for historical reasons, i guess)
206 // and those have no beziers. Another reason is that he chart module uses self-created
207 // stuff to create the 2D geometry (in ShapeFactory::createPieSegment), but this geometry
208 // does not contain bezier infos, either. The only way which is possible for now is to 'detect'
209 // candidates for vertical edges of pie segments by looking for the angles in the polygon.
211 // This is all not very well designed ATM. Ideally, the ReducedLineGeometry is responsible
212 // for creating the outer geometry edges (createReducedOutlines), but for special edges
213 // like the vertical ones for pie center and both start/end, the incarnation with the
214 // knowledge about that it needs to create those and IS a pie segment -> in this case,
215 // the chart itself.
216 const sal_uInt32 nPolyCount(maCorrectedPolyPolygon.count());
218 for(sal_uInt32 c(0); c < nPolyCount; c++)
220 const basegfx::B2DPolygon aCandidate(maCorrectedPolyPolygon.getB2DPolygon(c));
221 const sal_uInt32 nPointCount(aCandidate.count());
223 if(nPointCount > 2)
225 sal_uInt32 nIndexA(nPointCount);
226 sal_uInt32 nIndexB(nPointCount);
227 sal_uInt32 nIndexC(nPointCount);
229 for(sal_uInt32 d(0); d < nPointCount; d++)
231 const sal_uInt32 nPrevInd((d + nPointCount - 1) % nPointCount);
232 const sal_uInt32 nNextInd((d + 1) % nPointCount);
233 const basegfx::B2DPoint aPoint(aCandidate.getB2DPoint(d));
234 const basegfx::B2DVector aPrev(aCandidate.getB2DPoint(nPrevInd) - aPoint);
235 const basegfx::B2DVector aNext(aCandidate.getB2DPoint(nNextInd) - aPoint);
236 const double fAngle(aPrev.angle(aNext));
238 // take each angle which deviates more than 10% from going straight as
239 // special edge. This will detect the two outer edges of pie segments,
240 // but not always the center one (think about a near 180 degree pie)
241 if(F_PI - fabs(fAngle) > F_PI * 0.1)
243 if(nPointCount == nIndexA)
245 nIndexA = d;
247 else if(nPointCount == nIndexB)
249 nIndexB = d;
251 else if(nPointCount == nIndexC)
253 nIndexC = d;
254 d = nPointCount;
259 const bool bIndexAUsed(nIndexA != nPointCount);
260 const bool bIndexBUsed(nIndexB != nPointCount);
261 bool bIndexCUsed(nIndexC != nPointCount);
263 if(bIndexCUsed)
265 // already three special edges found, so the center one was already detected
266 // and does not need to be searched
268 else if(bIndexAUsed && bIndexBUsed)
270 // outer edges detected (they are approx. 90 degrees), but center one not.
271 // Look with the knowledge that it's in-between the two found ones
272 if(((nIndexA + 2) % nPointCount) == nIndexB)
274 nIndexC = (nIndexA + 1) % nPointCount;
276 else if(((nIndexA + nPointCount - 2) % nPointCount) == nIndexB)
278 nIndexC = (nIndexA + nPointCount - 1) % nPointCount;
281 bIndexCUsed = (nIndexC != nPointCount);
284 if(bIndexAUsed)
286 const basegfx::B2DPoint aPoint(aCandidate.getB2DPoint(nIndexA));
287 const basegfx::B3DPoint aStart(aPoint.getX(), aPoint.getY(), 0.0);
288 const basegfx::B3DPoint aEnd(aPoint.getX(), aPoint.getY(), getDepth());
289 basegfx::B3DPolygon aToBeAdded;
291 aToBeAdded.append(aStart);
292 aToBeAdded.append(aEnd);
293 aNewLineGeometry.append(aToBeAdded);
296 if(bIndexBUsed)
298 const basegfx::B2DPoint aPoint(aCandidate.getB2DPoint(nIndexB));
299 const basegfx::B3DPoint aStart(aPoint.getX(), aPoint.getY(), 0.0);
300 const basegfx::B3DPoint aEnd(aPoint.getX(), aPoint.getY(), getDepth());
301 basegfx::B3DPolygon aToBeAdded;
303 aToBeAdded.append(aStart);
304 aToBeAdded.append(aEnd);
305 aNewLineGeometry.append(aToBeAdded);
308 if(bIndexCUsed)
310 const basegfx::B2DPoint aPoint(aCandidate.getB2DPoint(nIndexC));
311 const basegfx::B3DPoint aStart(aPoint.getX(), aPoint.getY(), 0.0);
312 const basegfx::B3DPoint aEnd(aPoint.getX(), aPoint.getY(), getDepth());
313 basegfx::B3DPolygon aToBeAdded;
315 aToBeAdded.append(aStart);
316 aToBeAdded.append(aEnd);
317 aNewLineGeometry.append(aToBeAdded);
322 // append loops themselves
323 aNewLineGeometry.append(aReducedLoops);
325 if(aNewLineGeometry.count())
327 const Primitive3DContainer aLines(create3DPolyPolygonLinePrimitives(
328 aNewLineGeometry, getTransform(), getSdrLFSAttribute().getLine()));
329 aRetval.append(aLines);
332 else
334 // extract line geometry from slices
335 const basegfx::B3DPolyPolygon aHorLine(extractHorizontalLinesFromSlice(rSliceVector, false));
336 const basegfx::B3DPolyPolygon aVerLine(extractVerticalLinesFromSlice(rSliceVector));
338 // add horizontal lines
339 const Primitive3DContainer aHorLines(create3DPolyPolygonLinePrimitives(
340 aHorLine, getTransform(), getSdrLFSAttribute().getLine()));
341 aRetval.append(aHorLines);
343 // add vertical lines
344 const Primitive3DContainer aVerLines(create3DPolyPolygonLinePrimitives(
345 aVerLine, getTransform(), getSdrLFSAttribute().getLine()));
346 aRetval.append(aVerLines);
350 // add shadow
351 if(!getSdrLFSAttribute().getShadow().isDefault() && !aRetval.empty())
353 const Primitive3DContainer aShadow(createShadowPrimitive3D(
354 aRetval, getSdrLFSAttribute().getShadow(), getSdr3DObjectAttribute().getShadow3D()));
355 aRetval.append(aShadow);
359 return aRetval;
362 void SdrExtrudePrimitive3D::impCreateSlices()
364 // prepare the polygon. No double points, correct orientations and a correct
365 // outmost polygon are needed
366 // Also important: subdivide here to ensure equal point count for all slices (!)
367 maCorrectedPolyPolygon = basegfx::utils::adaptiveSubdivideByAngle(getPolyPolygon());
368 maCorrectedPolyPolygon.removeDoublePoints();
369 maCorrectedPolyPolygon = basegfx::utils::correctOrientations(maCorrectedPolyPolygon);
370 maCorrectedPolyPolygon = basegfx::utils::correctOutmostPolygon(maCorrectedPolyPolygon);
372 // prepare slices as geometry
373 createExtrudeSlices(maSlices, maCorrectedPolyPolygon, getBackScale(), getDiagonal(), getDepth(), getCharacterMode(), getCloseFront(), getCloseBack());
376 const Slice3DVector& SdrExtrudePrimitive3D::getSlices() const
378 // This can be made dependent of getSdrLFSAttribute().getFill() and getSdrLFSAttribute().getLine()
379 // again when no longer geometry is needed for non-visible 3D objects as it is now for chart
380 if(getPolyPolygon().count() && maSlices.empty())
382 ::osl::MutexGuard aGuard( m_aMutex );
384 const_cast< SdrExtrudePrimitive3D& >(*this).impCreateSlices();
387 return maSlices;
390 SdrExtrudePrimitive3D::SdrExtrudePrimitive3D(
391 const basegfx::B3DHomMatrix& rTransform,
392 const basegfx::B2DVector& rTextureSize,
393 const attribute::SdrLineFillShadowAttribute3D& rSdrLFSAttribute,
394 const attribute::Sdr3DObjectAttribute& rSdr3DObjectAttribute,
395 const basegfx::B2DPolyPolygon& rPolyPolygon,
396 double fDepth,
397 double fDiagonal,
398 double fBackScale,
399 bool bSmoothNormals,
400 bool bSmoothLids,
401 bool bCharacterMode,
402 bool bCloseFront,
403 bool bCloseBack)
404 : SdrPrimitive3D(rTransform, rTextureSize, rSdrLFSAttribute, rSdr3DObjectAttribute),
405 maCorrectedPolyPolygon(),
406 maSlices(),
407 maPolyPolygon(rPolyPolygon),
408 mfDepth(fDepth),
409 mfDiagonal(fDiagonal),
410 mfBackScale(fBackScale),
411 mbSmoothNormals(bSmoothNormals),
412 mbSmoothLids(bSmoothLids),
413 mbCharacterMode(bCharacterMode),
414 mbCloseFront(bCloseFront),
415 mbCloseBack(bCloseBack)
417 // make sure depth is positive
418 if(basegfx::fTools::lessOrEqual(getDepth(), 0.0))
420 mfDepth = 0.0;
423 // make sure the percentage value getDiagonal() is between 0.0 and 1.0
424 if(basegfx::fTools::lessOrEqual(getDiagonal(), 0.0))
426 mfDiagonal = 0.0;
428 else if(basegfx::fTools::moreOrEqual(getDiagonal(), 1.0))
430 mfDiagonal = 1.0;
433 // no close front/back when polygon is not closed
434 if(getPolyPolygon().count() && !getPolyPolygon().getB2DPolygon(0).isClosed())
436 mbCloseFront = mbCloseBack = false;
439 // no edge rounding when not closing
440 if(!getCloseFront() && !getCloseBack())
442 mfDiagonal = 0.0;
446 SdrExtrudePrimitive3D::~SdrExtrudePrimitive3D()
450 bool SdrExtrudePrimitive3D::operator==(const BasePrimitive3D& rPrimitive) const
452 if(SdrPrimitive3D::operator==(rPrimitive))
454 const SdrExtrudePrimitive3D& rCompare = static_cast< const SdrExtrudePrimitive3D& >(rPrimitive);
456 return (getPolyPolygon() == rCompare.getPolyPolygon()
457 && getDepth() == rCompare.getDepth()
458 && getDiagonal() == rCompare.getDiagonal()
459 && getBackScale() == rCompare.getBackScale()
460 && getSmoothNormals() == rCompare.getSmoothNormals()
461 && getSmoothLids() == rCompare.getSmoothLids()
462 && getCharacterMode() == rCompare.getCharacterMode()
463 && getCloseFront() == rCompare.getCloseFront()
464 && getCloseBack() == rCompare.getCloseBack());
467 return false;
470 basegfx::B3DRange SdrExtrudePrimitive3D::getB3DRange(const geometry::ViewInformation3D& /*rViewInformation*/) const
472 // use default from sdrPrimitive3D which uses transformation expanded by line width/2
473 // The parent implementation which uses the ranges of the decomposition would be more
474 // correct, but for historical reasons it is necessary to do the old method: To get
475 // the range of the non-transformed geometry and transform it then. This leads to different
476 // ranges where the new method is more correct, but the need to keep the old behaviour
477 // has priority here.
478 return get3DRangeFromSlices(getSlices());
481 Primitive3DContainer SdrExtrudePrimitive3D::get3DDecomposition(const geometry::ViewInformation3D& rViewInformation) const
483 if(getSdr3DObjectAttribute().getReducedLineGeometry())
485 if(!mpLastRLGViewInformation ||
486 (!getBuffered3DDecomposition().empty()
487 && *mpLastRLGViewInformation != rViewInformation))
489 ::osl::MutexGuard aGuard( m_aMutex );
491 // conditions of last local decomposition with reduced lines have changed. Remember
492 // new one and clear current decompositiopn
493 SdrExtrudePrimitive3D* pThat = const_cast< SdrExtrudePrimitive3D* >(this);
494 pThat->setBuffered3DDecomposition(Primitive3DContainer());
495 pThat->mpLastRLGViewInformation.reset( new geometry::ViewInformation3D(rViewInformation) );
499 // no test for buffering needed, call parent
500 return SdrPrimitive3D::get3DDecomposition(rViewInformation);
503 // provide unique ID
504 ImplPrimitive3DIDBlock(SdrExtrudePrimitive3D, PRIMITIVE3D_ID_SDREXTRUDEPRIMITIVE3D)
506 } // end of namespace primitive3d
507 } // end of namespace drawinglayer
509 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */