nss: upgrade to release 3.73
[LibreOffice.git] / drawinglayer / source / primitive3d / sdrextrudeprimitive3d.cxx
blobb7d1c8abdd9b41de60424b43eef919b04e0b82fc
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 <primitive3d/sdrdecompositiontools3d.hxx>
28 #include <drawinglayer/primitive3d/drawinglayer_primitivetypes3d.hxx>
29 #include <drawinglayer/geometry/viewinformation3d.hxx>
30 #include <drawinglayer/attribute/sdrfillattribute.hxx>
31 #include <drawinglayer/attribute/sdrlineattribute.hxx>
32 #include <drawinglayer/attribute/sdrshadowattribute.hxx>
35 using namespace com::sun::star;
38 namespace drawinglayer::primitive3d
40 Primitive3DContainer SdrExtrudePrimitive3D::create3DDecomposition(const geometry::ViewInformation3D& rViewInformation) const
42 Primitive3DContainer aRetval;
44 // get slices
45 const Slice3DVector& rSliceVector = getSlices();
47 if(!rSliceVector.empty())
49 sal_uInt32 a;
51 // decide what to create
52 const css::drawing::NormalsKind eNormalsKind(getSdr3DObjectAttribute().getNormalsKind());
53 const bool bCreateNormals(css::drawing::NormalsKind_SPECIFIC == eNormalsKind);
54 const bool bCreateTextureCoordinatesX(css::drawing::TextureProjectionMode_OBJECTSPECIFIC == getSdr3DObjectAttribute().getTextureProjectionX());
55 const bool bCreateTextureCoordinatesY(css::drawing::TextureProjectionMode_OBJECTSPECIFIC == getSdr3DObjectAttribute().getTextureProjectionY());
56 basegfx::B2DHomMatrix aTexTransform;
58 if(!getSdrLFSAttribute().getFill().isDefault() && (bCreateTextureCoordinatesX || bCreateTextureCoordinatesY))
60 const basegfx::B2DPolygon aFirstPolygon(maCorrectedPolyPolygon.getB2DPolygon(0));
61 const double fFrontLength(basegfx::utils::getLength(aFirstPolygon));
62 const double fFrontArea(basegfx::utils::getArea(aFirstPolygon));
63 const double fSqrtFrontArea(sqrt(fFrontArea));
64 double fRelativeTextureWidth = basegfx::fTools::equalZero(fSqrtFrontArea) ? 1.0 : fFrontLength / fSqrtFrontArea;
65 fRelativeTextureWidth = std::trunc(fRelativeTextureWidth - 0.5);
67 if(fRelativeTextureWidth < 1.0)
69 fRelativeTextureWidth = 1.0;
72 aTexTransform.translate(-0.5, -0.5);
73 aTexTransform.scale(-1.0, -1.0);
74 aTexTransform.translate(0.5, 0.5);
75 aTexTransform.scale(fRelativeTextureWidth, 1.0);
78 // create geometry
79 std::vector< basegfx::B3DPolyPolygon > aFill;
80 extractPlanesFromSlice(aFill, rSliceVector,
81 bCreateNormals, getSmoothNormals(), getSmoothLids(), false,
82 0.5, 0.6, bCreateTextureCoordinatesX || bCreateTextureCoordinatesY, aTexTransform);
84 // get full range
85 const basegfx::B3DRange aRange(getRangeFrom3DGeometry(aFill));
87 // normal creation
88 if(!getSdrLFSAttribute().getFill().isDefault())
90 if(css::drawing::NormalsKind_SPHERE == eNormalsKind)
92 applyNormalsKindSphereTo3DGeometry(aFill, aRange);
94 else if(css::drawing::NormalsKind_FLAT == eNormalsKind)
96 applyNormalsKindFlatTo3DGeometry(aFill);
99 if(getSdr3DObjectAttribute().getNormalsInvert())
101 applyNormalsInvertTo3DGeometry(aFill);
105 // texture coordinates
106 if(!getSdrLFSAttribute().getFill().isDefault())
108 applyTextureTo3DGeometry(
109 getSdr3DObjectAttribute().getTextureProjectionX(),
110 getSdr3DObjectAttribute().getTextureProjectionY(),
111 aFill,
112 aRange,
113 getTextureSize());
116 if(!getSdrLFSAttribute().getFill().isDefault())
118 // add fill
119 aRetval = create3DPolyPolygonFillPrimitives(
120 aFill,
121 getTransform(),
122 getTextureSize(),
123 getSdr3DObjectAttribute(),
124 getSdrLFSAttribute().getFill(),
125 getSdrLFSAttribute().getFillFloatTransGradient());
127 else
129 // create simplified 3d hit test geometry
130 aRetval = createHiddenGeometryPrimitives3D(
131 aFill,
132 getTransform(),
133 getTextureSize(),
134 getSdr3DObjectAttribute());
137 // add line
138 if(!getSdrLFSAttribute().getLine().isDefault())
140 if(getSdr3DObjectAttribute().getReducedLineGeometry())
142 // create geometric outlines with reduced line geometry for chart.
143 const basegfx::B3DPolyPolygon aVerLine(extractVerticalLinesFromSlice(rSliceVector));
144 const sal_uInt32 nCount(aVerLine.count());
145 basegfx::B3DPolyPolygon aReducedLoops;
146 basegfx::B3DPolyPolygon aNewLineGeometry;
148 // sort out doubles (front and back planes when no edge rounding is done). Since
149 // this is a line geometry merged from PolyPolygons, loop over all Polygons
150 for(a = 0; a < nCount; a++)
152 const sal_uInt32 nReducedCount(aReducedLoops.count());
153 const basegfx::B3DPolygon& aCandidate(aVerLine.getB3DPolygon(a));
154 bool bAdd(true);
156 if(nReducedCount)
158 for(sal_uInt32 b(0); bAdd && b < nReducedCount; b++)
160 if(aCandidate == aReducedLoops.getB3DPolygon(b))
162 bAdd = false;
167 if(bAdd)
169 aReducedLoops.append(aCandidate);
173 // from here work with reduced loops and reduced count without changing them
174 const sal_uInt32 nReducedCount(aReducedLoops.count());
176 if(nReducedCount > 1)
178 for(sal_uInt32 b(1); b < nReducedCount; b++)
180 // get loop pair
181 const basegfx::B3DPolygon& aCandA(aReducedLoops.getB3DPolygon(b - 1));
182 const basegfx::B3DPolygon& aCandB(aReducedLoops.getB3DPolygon(b));
184 // for each loop pair create the connection edges
185 createReducedOutlines(
186 rViewInformation,
187 getTransform(),
188 aCandA,
189 aCandB,
190 aNewLineGeometry);
194 // add reduced loops themselves
195 aNewLineGeometry.append(aReducedLoops);
197 // to create vertical edges at non-C1/C2 steady loops, use maCorrectedPolyPolygon
198 // directly since the 3D Polygons do not support this.
200 // Unfortunately there is no bezier polygon provided by the chart module; one reason is
201 // that the API for extrude wants a 3D polygon geometry (for historical reasons, i guess)
202 // and those have no beziers. Another reason is that he chart module uses self-created
203 // stuff to create the 2D geometry (in ShapeFactory::createPieSegment), but this geometry
204 // does not contain bezier infos, either. The only way which is possible for now is to 'detect'
205 // candidates for vertical edges of pie segments by looking for the angles in the polygon.
207 // This is all not very well designed ATM. Ideally, the ReducedLineGeometry is responsible
208 // for creating the outer geometry edges (createReducedOutlines), but for special edges
209 // like the vertical ones for pie center and both start/end, the incarnation with the
210 // knowledge about that it needs to create those and IS a pie segment -> in this case,
211 // the chart itself.
212 const sal_uInt32 nPolyCount(maCorrectedPolyPolygon.count());
214 for(sal_uInt32 c(0); c < nPolyCount; c++)
216 const basegfx::B2DPolygon aCandidate(maCorrectedPolyPolygon.getB2DPolygon(c));
217 const sal_uInt32 nPointCount(aCandidate.count());
219 if(nPointCount > 2)
221 sal_uInt32 nIndexA(nPointCount);
222 sal_uInt32 nIndexB(nPointCount);
223 sal_uInt32 nIndexC(nPointCount);
225 for(sal_uInt32 d(0); d < nPointCount; d++)
227 const sal_uInt32 nPrevInd((d + nPointCount - 1) % nPointCount);
228 const sal_uInt32 nNextInd((d + 1) % nPointCount);
229 const basegfx::B2DPoint aPoint(aCandidate.getB2DPoint(d));
230 const basegfx::B2DVector aPrev(aCandidate.getB2DPoint(nPrevInd) - aPoint);
231 const basegfx::B2DVector aNext(aCandidate.getB2DPoint(nNextInd) - aPoint);
232 const double fAngle(aPrev.angle(aNext));
234 // take each angle which deviates more than 10% from going straight as
235 // special edge. This will detect the two outer edges of pie segments,
236 // but not always the center one (think about a near 180 degree pie)
237 if(F_PI - fabs(fAngle) > F_PI * 0.1)
239 if(nPointCount == nIndexA)
241 nIndexA = d;
243 else if(nPointCount == nIndexB)
245 nIndexB = d;
247 else if(nPointCount == nIndexC)
249 nIndexC = d;
250 d = nPointCount;
255 const bool bIndexAUsed(nIndexA != nPointCount);
256 const bool bIndexBUsed(nIndexB != nPointCount);
257 bool bIndexCUsed(nIndexC != nPointCount);
259 if(bIndexCUsed)
261 // already three special edges found, so the center one was already detected
262 // and does not need to be searched
264 else if(bIndexAUsed && bIndexBUsed)
266 // outer edges detected (they are approx. 90 degrees), but center one not.
267 // Look with the knowledge that it's in-between the two found ones
268 if(((nIndexA + 2) % nPointCount) == nIndexB)
270 nIndexC = (nIndexA + 1) % nPointCount;
272 else if(((nIndexA + nPointCount - 2) % nPointCount) == nIndexB)
274 nIndexC = (nIndexA + nPointCount - 1) % nPointCount;
277 bIndexCUsed = (nIndexC != nPointCount);
280 if(bIndexAUsed)
282 const basegfx::B2DPoint aPoint(aCandidate.getB2DPoint(nIndexA));
283 const basegfx::B3DPoint aStart(aPoint.getX(), aPoint.getY(), 0.0);
284 const basegfx::B3DPoint aEnd(aPoint.getX(), aPoint.getY(), getDepth());
285 basegfx::B3DPolygon aToBeAdded;
287 aToBeAdded.append(aStart);
288 aToBeAdded.append(aEnd);
289 aNewLineGeometry.append(aToBeAdded);
292 if(bIndexBUsed)
294 const basegfx::B2DPoint aPoint(aCandidate.getB2DPoint(nIndexB));
295 const basegfx::B3DPoint aStart(aPoint.getX(), aPoint.getY(), 0.0);
296 const basegfx::B3DPoint aEnd(aPoint.getX(), aPoint.getY(), getDepth());
297 basegfx::B3DPolygon aToBeAdded;
299 aToBeAdded.append(aStart);
300 aToBeAdded.append(aEnd);
301 aNewLineGeometry.append(aToBeAdded);
304 if(bIndexCUsed)
306 const basegfx::B2DPoint aPoint(aCandidate.getB2DPoint(nIndexC));
307 const basegfx::B3DPoint aStart(aPoint.getX(), aPoint.getY(), 0.0);
308 const basegfx::B3DPoint aEnd(aPoint.getX(), aPoint.getY(), getDepth());
309 basegfx::B3DPolygon aToBeAdded;
311 aToBeAdded.append(aStart);
312 aToBeAdded.append(aEnd);
313 aNewLineGeometry.append(aToBeAdded);
318 // append loops themselves
319 aNewLineGeometry.append(aReducedLoops);
321 if(aNewLineGeometry.count())
323 const Primitive3DContainer aLines(create3DPolyPolygonLinePrimitives(
324 aNewLineGeometry, getTransform(), getSdrLFSAttribute().getLine()));
325 aRetval.append(aLines);
328 else
330 // extract line geometry from slices
331 const basegfx::B3DPolyPolygon aHorLine(extractHorizontalLinesFromSlice(rSliceVector, false));
332 const basegfx::B3DPolyPolygon aVerLine(extractVerticalLinesFromSlice(rSliceVector));
334 // add horizontal lines
335 const Primitive3DContainer aHorLines(create3DPolyPolygonLinePrimitives(
336 aHorLine, getTransform(), getSdrLFSAttribute().getLine()));
337 aRetval.append(aHorLines);
339 // add vertical lines
340 const Primitive3DContainer aVerLines(create3DPolyPolygonLinePrimitives(
341 aVerLine, getTransform(), getSdrLFSAttribute().getLine()));
342 aRetval.append(aVerLines);
346 // add shadow
347 if(!getSdrLFSAttribute().getShadow().isDefault() && !aRetval.empty())
349 const Primitive3DContainer aShadow(createShadowPrimitive3D(
350 aRetval, getSdrLFSAttribute().getShadow(), getSdr3DObjectAttribute().getShadow3D()));
351 aRetval.append(aShadow);
355 return aRetval;
358 void SdrExtrudePrimitive3D::impCreateSlices()
360 // prepare the polygon. No double points, correct orientations and a correct
361 // outmost polygon are needed
362 // Also important: subdivide here to ensure equal point count for all slices (!)
363 maCorrectedPolyPolygon = basegfx::utils::adaptiveSubdivideByAngle(getPolyPolygon());
364 maCorrectedPolyPolygon.removeDoublePoints();
365 maCorrectedPolyPolygon = basegfx::utils::correctOrientations(maCorrectedPolyPolygon);
366 maCorrectedPolyPolygon = basegfx::utils::correctOutmostPolygon(maCorrectedPolyPolygon);
368 // prepare slices as geometry
369 createExtrudeSlices(maSlices, maCorrectedPolyPolygon, getBackScale(), getDiagonal(), getDepth(), getCharacterMode(), getCloseFront(), getCloseBack());
372 const Slice3DVector& SdrExtrudePrimitive3D::getSlices() const
374 // This can be made dependent of getSdrLFSAttribute().getFill() and getSdrLFSAttribute().getLine()
375 // again when no longer geometry is needed for non-visible 3D objects as it is now for chart
376 if(getPolyPolygon().count() && maSlices.empty())
378 ::osl::MutexGuard aGuard( m_aMutex );
380 const_cast< SdrExtrudePrimitive3D& >(*this).impCreateSlices();
383 return maSlices;
386 SdrExtrudePrimitive3D::SdrExtrudePrimitive3D(
387 const basegfx::B3DHomMatrix& rTransform,
388 const basegfx::B2DVector& rTextureSize,
389 const attribute::SdrLineFillShadowAttribute3D& rSdrLFSAttribute,
390 const attribute::Sdr3DObjectAttribute& rSdr3DObjectAttribute,
391 const basegfx::B2DPolyPolygon& rPolyPolygon,
392 double fDepth,
393 double fDiagonal,
394 double fBackScale,
395 bool bSmoothNormals,
396 bool bSmoothLids,
397 bool bCharacterMode,
398 bool bCloseFront,
399 bool bCloseBack)
400 : SdrPrimitive3D(rTransform, rTextureSize, rSdrLFSAttribute, rSdr3DObjectAttribute),
401 maCorrectedPolyPolygon(),
402 maSlices(),
403 maPolyPolygon(rPolyPolygon),
404 mfDepth(fDepth),
405 mfDiagonal(fDiagonal),
406 mfBackScale(fBackScale),
407 mbSmoothNormals(bSmoothNormals),
408 mbSmoothLids(bSmoothLids),
409 mbCharacterMode(bCharacterMode),
410 mbCloseFront(bCloseFront),
411 mbCloseBack(bCloseBack)
413 // make sure depth is positive
414 if(basegfx::fTools::lessOrEqual(getDepth(), 0.0))
416 mfDepth = 0.0;
419 // make sure the percentage value getDiagonal() is between 0.0 and 1.0
420 if(basegfx::fTools::lessOrEqual(getDiagonal(), 0.0))
422 mfDiagonal = 0.0;
424 else if(basegfx::fTools::moreOrEqual(getDiagonal(), 1.0))
426 mfDiagonal = 1.0;
429 // no close front/back when polygon is not closed
430 if(getPolyPolygon().count() && !getPolyPolygon().getB2DPolygon(0).isClosed())
432 mbCloseFront = mbCloseBack = false;
435 // no edge rounding when not closing
436 if(!getCloseFront() && !getCloseBack())
438 mfDiagonal = 0.0;
442 SdrExtrudePrimitive3D::~SdrExtrudePrimitive3D()
446 bool SdrExtrudePrimitive3D::operator==(const BasePrimitive3D& rPrimitive) const
448 if(SdrPrimitive3D::operator==(rPrimitive))
450 const SdrExtrudePrimitive3D& rCompare = static_cast< const SdrExtrudePrimitive3D& >(rPrimitive);
452 return (getPolyPolygon() == rCompare.getPolyPolygon()
453 && getDepth() == rCompare.getDepth()
454 && getDiagonal() == rCompare.getDiagonal()
455 && getBackScale() == rCompare.getBackScale()
456 && getSmoothNormals() == rCompare.getSmoothNormals()
457 && getSmoothLids() == rCompare.getSmoothLids()
458 && getCharacterMode() == rCompare.getCharacterMode()
459 && getCloseFront() == rCompare.getCloseFront()
460 && getCloseBack() == rCompare.getCloseBack());
463 return false;
466 basegfx::B3DRange SdrExtrudePrimitive3D::getB3DRange(const geometry::ViewInformation3D& /*rViewInformation*/) const
468 // use default from sdrPrimitive3D which uses transformation expanded by line width/2
469 // The parent implementation which uses the ranges of the decomposition would be more
470 // correct, but for historical reasons it is necessary to do the old method: To get
471 // the range of the non-transformed geometry and transform it then. This leads to different
472 // ranges where the new method is more correct, but the need to keep the old behaviour
473 // has priority here.
474 return get3DRangeFromSlices(getSlices());
477 Primitive3DContainer SdrExtrudePrimitive3D::get3DDecomposition(const geometry::ViewInformation3D& rViewInformation) const
479 if(getSdr3DObjectAttribute().getReducedLineGeometry())
481 if(!mpLastRLGViewInformation ||
482 (!getBuffered3DDecomposition().empty()
483 && *mpLastRLGViewInformation != rViewInformation))
485 ::osl::MutexGuard aGuard( m_aMutex );
487 // conditions of last local decomposition with reduced lines have changed. Remember
488 // new one and clear current decompositiopn
489 SdrExtrudePrimitive3D* pThat = const_cast< SdrExtrudePrimitive3D* >(this);
490 pThat->setBuffered3DDecomposition(Primitive3DContainer());
491 pThat->mpLastRLGViewInformation.reset( new geometry::ViewInformation3D(rViewInformation) );
495 // no test for buffering needed, call parent
496 return SdrPrimitive3D::get3DDecomposition(rViewInformation);
499 // provide unique ID
500 ImplPrimitive3DIDBlock(SdrExtrudePrimitive3D, PRIMITIVE3D_ID_SDREXTRUDEPRIMITIVE3D)
502 } // end of namespace
504 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */