1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_drawinglayer.hxx"
26 
27 #include <drawinglayer/primitive3d/sdrextrudeprimitive3d.hxx>
28 #include <basegfx/matrix/b2dhommatrix.hxx>
29 #include <basegfx/polygon/b2dpolygontools.hxx>
30 #include <basegfx/polygon/b3dpolypolygontools.hxx>
31 #include <drawinglayer/primitive3d/sdrdecompositiontools3d.hxx>
32 #include <basegfx/tools/canvastools.hxx>
33 #include <drawinglayer/primitive3d/drawinglayer_primitivetypes3d.hxx>
34 #include <drawinglayer/geometry/viewinformation3d.hxx>
35 #include <drawinglayer/attribute/sdrfillattribute.hxx>
36 #include <drawinglayer/attribute/sdrlineattribute.hxx>
37 #include <drawinglayer/attribute/sdrshadowattribute.hxx>
38 
39 //////////////////////////////////////////////////////////////////////////////
40 
41 using namespace com::sun::star;
42 
43 //////////////////////////////////////////////////////////////////////////////
44 
45 namespace drawinglayer
46 {
47 	namespace primitive3d
48 	{
49 		Primitive3DSequence SdrExtrudePrimitive3D::create3DDecomposition(const geometry::ViewInformation3D& rViewInformation) const
50 		{
51 			Primitive3DSequence aRetval;
52 
53 			// get slices
54 			const Slice3DVector& rSliceVector = getSlices();
55 
56 			if(rSliceVector.size())
57 			{
58 				sal_uInt32 a;
59 
60 				// decide what to create
61 				const ::com::sun::star::drawing::NormalsKind eNormalsKind(getSdr3DObjectAttribute().getNormalsKind());
62 				const bool bCreateNormals(::com::sun::star::drawing::NormalsKind_SPECIFIC == eNormalsKind);
63 				const bool bCreateTextureCoordiantesX(::com::sun::star::drawing::TextureProjectionMode_OBJECTSPECIFIC == getSdr3DObjectAttribute().getTextureProjectionX());
64 				const bool bCreateTextureCoordiantesY(::com::sun::star::drawing::TextureProjectionMode_OBJECTSPECIFIC == getSdr3DObjectAttribute().getTextureProjectionY());
65 				double fRelativeTextureWidth(1.0);
66 				basegfx::B2DHomMatrix aTexTransform;
67 
68 				if(!getSdrLFSAttribute().getFill().isDefault() && (bCreateTextureCoordiantesX || bCreateTextureCoordiantesY))
69 				{
70 					const basegfx::B2DPolygon aFirstPolygon(maCorrectedPolyPolygon.getB2DPolygon(0L));
71 					const double fFrontLength(basegfx::tools::getLength(aFirstPolygon));
72 					const double fFrontArea(basegfx::tools::getArea(aFirstPolygon));
73 					const double fSqrtFrontArea(sqrt(fFrontArea));
74 					fRelativeTextureWidth = basegfx::fTools::equalZero(fSqrtFrontArea) ? 1.0 : fFrontLength / fSqrtFrontArea;
75 					fRelativeTextureWidth = (double)((sal_uInt32)(fRelativeTextureWidth - 0.5));
76 
77 					if(fRelativeTextureWidth < 1.0)
78 					{
79 						fRelativeTextureWidth = 1.0;
80 					}
81 
82 					aTexTransform.translate(-0.5, -0.5);
83 					aTexTransform.scale(-1.0, -1.0);
84 					aTexTransform.translate(0.5, 0.5);
85 					aTexTransform.scale(fRelativeTextureWidth, 1.0);
86 				}
87 
88 				// create geometry
89 				::std::vector< basegfx::B3DPolyPolygon > aFill;
90 				extractPlanesFromSlice(aFill, rSliceVector,
91 					bCreateNormals, getSmoothHorizontalNormals(), getSmoothNormals(), getSmoothLids(), false,
92 					0.5, 0.6, bCreateTextureCoordiantesX || bCreateTextureCoordiantesY, aTexTransform);
93 
94 				// get full range
95 				const basegfx::B3DRange aRange(getRangeFrom3DGeometry(aFill));
96 
97 				// normal creation
98 				if(!getSdrLFSAttribute().getFill().isDefault())
99 				{
100 					if(::com::sun::star::drawing::NormalsKind_SPHERE == eNormalsKind)
101 					{
102 						applyNormalsKindSphereTo3DGeometry(aFill, aRange);
103 					}
104 					else if(::com::sun::star::drawing::NormalsKind_FLAT == eNormalsKind)
105 					{
106 						applyNormalsKindFlatTo3DGeometry(aFill);
107 					}
108 
109 					if(getSdr3DObjectAttribute().getNormalsInvert())
110 					{
111 						applyNormalsInvertTo3DGeometry(aFill);
112 					}
113 				}
114 
115 				// texture coordinates
116 				if(!getSdrLFSAttribute().getFill().isDefault())
117 				{
118 					applyTextureTo3DGeometry(
119 						getSdr3DObjectAttribute().getTextureProjectionX(),
120 						getSdr3DObjectAttribute().getTextureProjectionY(),
121 						aFill,
122 						aRange,
123 						getTextureSize());
124 				}
125 
126 				if(!getSdrLFSAttribute().getFill().isDefault())
127 				{
128 					// add fill
129 					aRetval = create3DPolyPolygonFillPrimitives(
130 						aFill,
131 						getTransform(),
132 						getTextureSize(),
133 						getSdr3DObjectAttribute(),
134 						getSdrLFSAttribute().getFill(),
135 						getSdrLFSAttribute().getFillFloatTransGradient());
136 				}
137 				else
138 				{
139 					// create simplified 3d hit test geometry
140                     aRetval = createHiddenGeometryPrimitives3D(
141 				        aFill,
142 				        getTransform(),
143 				        getTextureSize(),
144 				        getSdr3DObjectAttribute());
145 				}
146 
147 				// add line
148 				if(!getSdrLFSAttribute().getLine().isDefault())
149 				{
150 					if(getSdr3DObjectAttribute().getReducedLineGeometry())
151                     {
152 						// create geometric outlines with reduced line geometry for chart.
153 						const basegfx::B3DPolyPolygon aVerLine(extractVerticalLinesFromSlice(rSliceVector));
154 						const sal_uInt32 nCount(aVerLine.count());
155 						basegfx::B3DPolyPolygon aReducedLoops;
156 						basegfx::B3DPolyPolygon aNewLineGeometry;
157 
158 						// sort out doubles (front and back planes when no edge rounding is done). Since
159 						// this is a line geometry merged from PolyPolygons, loop over all Polygons
160 						for(a = 0; a < nCount; a++)
161 						{
162 							const sal_uInt32 nReducedCount(aReducedLoops.count());
163 							const basegfx::B3DPolygon aCandidate(aVerLine.getB3DPolygon(a));
164 							bool bAdd(true);
165 
166 							if(nReducedCount)
167 							{
168 								for(sal_uInt32 b(0); bAdd && b < nReducedCount; b++)
169 								{
170 									if(aCandidate == aReducedLoops.getB3DPolygon(b))
171 									{
172 										bAdd = false;
173 									}
174 								}
175 							}
176 
177 							if(bAdd)
178 							{
179 								aReducedLoops.append(aCandidate);
180 							}
181 						}
182 
183 						// from here work with reduced loops and reduced count without changing them
184 						const sal_uInt32 nReducedCount(aReducedLoops.count());
185 
186 						if(nReducedCount > 1)
187 						{
188 							for(sal_uInt32 b(1); b < nReducedCount; b++)
189 							{
190 								// get loop pair
191 								const basegfx::B3DPolygon aCandA(aReducedLoops.getB3DPolygon(b - 1));
192 								const basegfx::B3DPolygon aCandB(aReducedLoops.getB3DPolygon(b));
193 
194 								// for each loop pair create the connection edges
195 								createReducedOutlines(
196 									rViewInformation,
197 									getTransform(),
198 									aCandA,
199 									aCandB,
200 									aNewLineGeometry);
201 							}
202 						}
203 
204 						// add reduced loops themselves
205 						aNewLineGeometry.append(aReducedLoops);
206 
207 						// to create vertical edges at non-C1/C2 steady loops, use maCorrectedPolyPolygon
208 						// directly since the 3D Polygons do not suport this.
209 						//
210 						// Unfortunately there is no bezier polygon provided by the chart module; one reason is
211 						// that the API for extrude wants a 3D polygon geometry (for historical reasons, i guess)
212 						// and those have no beziers. Another reason is that he chart module uses self-created
213 						// stuff to create the 2D geometry (in ShapeFactory::createPieSegment), but this geometry
214 						// does not contain bezier infos, either. The only way which is possible for now is to 'detect'
215 						// candidates for vertical edges of pie segments by looking for the angles in the polygon.
216 						//
217 						// This is all not very well designed ATM. Ideally, the ReducedLineGeometry is responsible
218 						// for creating the outer geometry edges (createReducedOutlines), but for special edges
219 						// like the vertical ones for pie center and both start/end, the incarnation with the
220 						// knowledge about that it needs to create those and IS a pie segment -> in this case,
221 						// the chart itself.
222 						const sal_uInt32 nPolyCount(maCorrectedPolyPolygon.count());
223 
224 						for(sal_uInt32 c(0); c < nPolyCount; c++)
225 						{
226 							const basegfx::B2DPolygon aCandidate(maCorrectedPolyPolygon.getB2DPolygon(c));
227 							const sal_uInt32 nPointCount(aCandidate.count());
228 
229 							if(nPointCount > 2)
230 							{
231 								sal_uInt32 nIndexA(nPointCount);
232 								sal_uInt32 nIndexB(nPointCount);
233 								sal_uInt32 nIndexC(nPointCount);
234 
235 								for(sal_uInt32 d(0); d < nPointCount; d++)
236 								{
237 									const sal_uInt32 nPrevInd((d + nPointCount - 1) % nPointCount);
238 									const sal_uInt32 nNextInd((d + 1) % nPointCount);
239 									const basegfx::B2DPoint aPoint(aCandidate.getB2DPoint(d));
240 									const basegfx::B2DVector aPrev(aCandidate.getB2DPoint(nPrevInd) - aPoint);
241 									const basegfx::B2DVector aNext(aCandidate.getB2DPoint(nNextInd) - aPoint);
242 									const double fAngle(aPrev.angle(aNext));
243 
244 									// take each angle which deviates more than 10% from going straight as
245 									// special edge. This will detect the two outer edges of pie segments,
246 									// but not always the center one (think about a near 180 degree pie)
247 									if(F_PI - fabs(fAngle) > F_PI * 0.1)
248 									{
249 										if(nPointCount == nIndexA)
250 										{
251 											nIndexA = d;
252 										}
253 										else if(nPointCount == nIndexB)
254 										{
255 											nIndexB = d;
256 										}
257 										else if(nPointCount == nIndexC)
258 										{
259 											nIndexC = d;
260 											d = nPointCount;
261 										}
262 									}
263 								}
264 
265 								const bool bIndexAUsed(nIndexA != nPointCount);
266 								const bool bIndexBUsed(nIndexB != nPointCount);
267 								bool bIndexCUsed(nIndexC != nPointCount);
268 
269 								if(bIndexCUsed)
270 								{
271 									// already three special edges found, so the center one was already detected
272 									// and does not need to be searched
273 								}
274 								else if(bIndexAUsed && bIndexBUsed)
275 								{
276 									// outer edges detected (they are approx. 90 degrees), but center one not.
277 									// Look with the knowledge that it's in-between the two found ones
278 									if(((nIndexA + 2) % nPointCount) == nIndexB)
279 									{
280 										nIndexC = (nIndexA + 1) % nPointCount;
281 									}
282 									else if(((nIndexA + nPointCount - 2) % nPointCount) == nIndexB)
283 									{
284 										nIndexC = (nIndexA + nPointCount - 1) % nPointCount;
285 									}
286 
287 									bIndexCUsed = (nIndexC != nPointCount);
288 								}
289 
290 								if(bIndexAUsed)
291 								{
292 									const basegfx::B2DPoint aPoint(aCandidate.getB2DPoint(nIndexA));
293 									const basegfx::B3DPoint aStart(aPoint.getX(), aPoint.getY(), 0.0);
294 									const basegfx::B3DPoint aEnd(aPoint.getX(), aPoint.getY(), getDepth());
295 									basegfx::B3DPolygon aToBeAdded;
296 
297 									aToBeAdded.append(aStart);
298 									aToBeAdded.append(aEnd);
299 									aNewLineGeometry.append(aToBeAdded);
300 								}
301 
302 								if(bIndexBUsed)
303 								{
304 									const basegfx::B2DPoint aPoint(aCandidate.getB2DPoint(nIndexB));
305 									const basegfx::B3DPoint aStart(aPoint.getX(), aPoint.getY(), 0.0);
306 									const basegfx::B3DPoint aEnd(aPoint.getX(), aPoint.getY(), getDepth());
307 									basegfx::B3DPolygon aToBeAdded;
308 
309 									aToBeAdded.append(aStart);
310 									aToBeAdded.append(aEnd);
311 									aNewLineGeometry.append(aToBeAdded);
312 								}
313 
314 								if(bIndexCUsed)
315 								{
316 									const basegfx::B2DPoint aPoint(aCandidate.getB2DPoint(nIndexC));
317 									const basegfx::B3DPoint aStart(aPoint.getX(), aPoint.getY(), 0.0);
318 									const basegfx::B3DPoint aEnd(aPoint.getX(), aPoint.getY(), getDepth());
319 									basegfx::B3DPolygon aToBeAdded;
320 
321 									aToBeAdded.append(aStart);
322 									aToBeAdded.append(aEnd);
323 									aNewLineGeometry.append(aToBeAdded);
324 								}
325 							}
326 						}
327 
328 						// append loops themselves
329 						aNewLineGeometry.append(aReducedLoops);
330 
331 						if(aNewLineGeometry.count())
332 						{
333 							const Primitive3DSequence aLines(create3DPolyPolygonLinePrimitives(
334                                 aNewLineGeometry, getTransform(), getSdrLFSAttribute().getLine()));
335 							appendPrimitive3DSequenceToPrimitive3DSequence(aRetval, aLines);
336 						}
337 					}
338 					else
339 					{
340 						// extract line geometry from slices
341 						const basegfx::B3DPolyPolygon aHorLine(extractHorizontalLinesFromSlice(rSliceVector, false));
342 						const basegfx::B3DPolyPolygon aVerLine(extractVerticalLinesFromSlice(rSliceVector));
343 
344 						// add horizontal lines
345 						const Primitive3DSequence aHorLines(create3DPolyPolygonLinePrimitives(
346                             aHorLine, getTransform(), getSdrLFSAttribute().getLine()));
347 						appendPrimitive3DSequenceToPrimitive3DSequence(aRetval, aHorLines);
348 
349 						// add vertical lines
350 						const Primitive3DSequence aVerLines(create3DPolyPolygonLinePrimitives(
351                             aVerLine, getTransform(), getSdrLFSAttribute().getLine()));
352 						appendPrimitive3DSequenceToPrimitive3DSequence(aRetval, aVerLines);
353 					}
354 				}
355 
356 				// add shadow
357 				if(!getSdrLFSAttribute().getShadow().isDefault() && aRetval.hasElements())
358 				{
359 					const Primitive3DSequence aShadow(createShadowPrimitive3D(
360                         aRetval, getSdrLFSAttribute().getShadow(), getSdr3DObjectAttribute().getShadow3D()));
361 					appendPrimitive3DSequenceToPrimitive3DSequence(aRetval, aShadow);
362 				}
363 			}
364 
365 			return aRetval;
366 		}
367 
368 		void SdrExtrudePrimitive3D::impCreateSlices()
369 		{
370 			// prepare the polygon. No double points, correct orientations and a correct
371 			// outmost polygon are needed
372 			maCorrectedPolyPolygon = getPolyPolygon();
373 			maCorrectedPolyPolygon.removeDoublePoints();
374 			maCorrectedPolyPolygon = basegfx::tools::correctOrientations(maCorrectedPolyPolygon);
375 			maCorrectedPolyPolygon = basegfx::tools::correctOutmostPolygon(maCorrectedPolyPolygon);
376 
377 			// prepare slices as geometry
378 			createExtrudeSlices(maSlices, maCorrectedPolyPolygon, getBackScale(), getDiagonal(), getDepth(), getCharacterMode(), getCloseFront(), getCloseBack());
379 		}
380 
381 		const Slice3DVector& SdrExtrudePrimitive3D::getSlices() const
382 		{
383 			// This can be made dependent of  getSdrLFSAttribute().getFill() and getSdrLFSAttribute().getLine()
384 			// again when no longer geometry is needed for non-visible 3D objects as it is now for chart
385 			if(getPolyPolygon().count() && !maSlices.size())
386 			{
387 			    ::osl::Mutex m_mutex;
388 				const_cast< SdrExtrudePrimitive3D& >(*this).impCreateSlices();
389 			}
390 
391 			return maSlices;
392 		}
393 
394 		SdrExtrudePrimitive3D::SdrExtrudePrimitive3D(
395 			const basegfx::B3DHomMatrix& rTransform,
396 			const basegfx::B2DVector& rTextureSize,
397 			const attribute::SdrLineFillShadowAttribute3D& rSdrLFSAttribute,
398 			const attribute::Sdr3DObjectAttribute& rSdr3DObjectAttribute,
399 			const basegfx::B2DPolyPolygon& rPolyPolygon,
400 			double fDepth,
401 			double fDiagonal,
402 			double fBackScale,
403 			bool bSmoothNormals,
404 			bool bSmoothHorizontalNormals,
405 			bool bSmoothLids,
406 			bool bCharacterMode,
407 			bool bCloseFront,
408 			bool bCloseBack)
409 		:	SdrPrimitive3D(rTransform, rTextureSize, rSdrLFSAttribute, rSdr3DObjectAttribute),
410             maCorrectedPolyPolygon(),
411             maSlices(),
412 			maPolyPolygon(rPolyPolygon),
413 			mfDepth(fDepth),
414 			mfDiagonal(fDiagonal),
415 			mfBackScale(fBackScale),
416             mpLastRLGViewInformation(0),
417 			mbSmoothNormals(bSmoothNormals),
418 			mbSmoothHorizontalNormals(bSmoothHorizontalNormals),
419 			mbSmoothLids(bSmoothLids),
420 			mbCharacterMode(bCharacterMode),
421 			mbCloseFront(bCloseFront),
422 			mbCloseBack(bCloseBack)
423 		{
424 			// make sure depth is positive
425 			if(basegfx::fTools::lessOrEqual(getDepth(), 0.0))
426 			{
427 				mfDepth = 0.0;
428 			}
429 
430 			// make sure the percentage value getDiagonal() is between 0.0 and 1.0
431 			if(basegfx::fTools::lessOrEqual(getDiagonal(), 0.0))
432 			{
433 				mfDiagonal = 0.0;
434 			}
435 			else if(basegfx::fTools::moreOrEqual(getDiagonal(), 1.0))
436 			{
437 				mfDiagonal = 1.0;
438 			}
439 
440 			// no close front/back when polygon is not closed
441 			if(getPolyPolygon().count() && !getPolyPolygon().getB2DPolygon(0L).isClosed())
442 			{
443 				mbCloseFront = mbCloseBack = false;
444 			}
445 
446 			// no edge rounding when not closing
447 			if(!getCloseFront() && !getCloseBack())
448 			{
449 				mfDiagonal = 0.0;
450 			}
451 		}
452 
453         SdrExtrudePrimitive3D::~SdrExtrudePrimitive3D()
454         {
455             if(mpLastRLGViewInformation)
456             {
457                 delete mpLastRLGViewInformation;
458             }
459         }
460 
461         bool SdrExtrudePrimitive3D::operator==(const BasePrimitive3D& rPrimitive) const
462 		{
463 			if(SdrPrimitive3D::operator==(rPrimitive))
464 			{
465 				const SdrExtrudePrimitive3D& rCompare = static_cast< const SdrExtrudePrimitive3D& >(rPrimitive);
466 
467 				return (getPolyPolygon() == rCompare.getPolyPolygon()
468 					&& getDepth() == rCompare.getDepth()
469 					&& getDiagonal() == rCompare.getDiagonal()
470 					&& getBackScale() == rCompare.getBackScale()
471 					&& getSmoothNormals() == rCompare.getSmoothNormals()
472 					&& getSmoothHorizontalNormals() == rCompare.getSmoothHorizontalNormals()
473 					&& getSmoothLids() == rCompare.getSmoothLids()
474 					&& getCharacterMode() == rCompare.getCharacterMode()
475 					&& getCloseFront() == rCompare.getCloseFront()
476 					&& getCloseBack() == rCompare.getCloseBack());
477 			}
478 
479 			return false;
480 		}
481 
482 		basegfx::B3DRange SdrExtrudePrimitive3D::getB3DRange(const geometry::ViewInformation3D& /*rViewInformation*/) const
483 		{
484 			// use defaut from sdrPrimitive3D which uses transformation expanded by line width/2
485 			// The parent implementation which uses the ranges of the decomposition would be more
486 			// corrcet, but for historical reasons it is necessary to do the old method: To get
487 			// the range of the non-transformed geometry and transform it then. This leads to different
488 			// ranges where the new method is more correct, but the need to keep the old behaviour
489 			// has priority here.
490 			return get3DRangeFromSlices(getSlices());
491 		}
492 
493         Primitive3DSequence SdrExtrudePrimitive3D::get3DDecomposition(const geometry::ViewInformation3D& rViewInformation) const
494         {
495             if(getSdr3DObjectAttribute().getReducedLineGeometry())
496             {
497                 if(!mpLastRLGViewInformation ||
498                     (getBuffered3DDecomposition().hasElements()
499                         && *mpLastRLGViewInformation != rViewInformation))
500                 {
501 					// conditions of last local decomposition with reduced lines have changed. Remember
502                     // new one and clear current decompositiopn
503     			    ::osl::Mutex m_mutex;
504 					SdrExtrudePrimitive3D* pThat = const_cast< SdrExtrudePrimitive3D* >(this);
505                     pThat->setBuffered3DDecomposition(Primitive3DSequence());
506     				delete pThat->mpLastRLGViewInformation;
507                     pThat->mpLastRLGViewInformation = new geometry::ViewInformation3D(rViewInformation);
508                 }
509             }
510 
511             // no test for buffering needed, call parent
512             return SdrPrimitive3D::get3DDecomposition(rViewInformation);
513         }
514 
515 		// provide unique ID
516 		ImplPrimitrive3DIDBlock(SdrExtrudePrimitive3D, PRIMITIVE3D_ID_SDREXTRUDEPRIMITIVE3D)
517 
518 	} // end of namespace primitive3d
519 } // end of namespace drawinglayer
520 
521 //////////////////////////////////////////////////////////////////////////////
522 // eof
523