Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / basegfx / source / polygon / b2dpolygonclipper.cxx
blob5057fc52d977dc104cb8a7532a61d4b8ca1e1833
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 <basegfx/polygon/b2dpolygonclipper.hxx>
21 #include <basegfx/polygon/b2dpolygontools.hxx>
22 #include <basegfx/numeric/ftools.hxx>
23 #include <basegfx/matrix/b2dhommatrix.hxx>
24 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
25 #include <basegfx/polygon/b2dpolygoncutandtouch.hxx>
26 #include <basegfx/polygon/b2dpolypolygontools.hxx>
27 #include <basegfx/curve/b2dcubicbezier.hxx>
28 #include <basegfx/utils/rectcliptools.hxx>
29 #include <basegfx/matrix/b2dhommatrixtools.hxx>
31 namespace basegfx
33 namespace utils
35 B2DPolyPolygon clipPolygonOnParallelAxis(const B2DPolygon& rCandidate, bool bParallelToXAxis, bool bAboveAxis, double fValueOnOtherAxis, bool bStroke)
37 B2DPolyPolygon aRetval;
39 if(rCandidate.count())
41 const B2DRange aCandidateRange(getRange(rCandidate));
43 if(bParallelToXAxis && fTools::moreOrEqual(aCandidateRange.getMinY(), fValueOnOtherAxis))
45 // completely above and on the clip line. also true for curves.
46 if(bAboveAxis)
48 // add completely
49 aRetval.append(rCandidate);
52 else if(bParallelToXAxis && fTools::lessOrEqual(aCandidateRange.getMaxY(), fValueOnOtherAxis))
54 // completely below and on the clip line. also true for curves.
55 if(!bAboveAxis)
57 // add completely
58 aRetval.append(rCandidate);
61 else if(!bParallelToXAxis && fTools::moreOrEqual(aCandidateRange.getMinX(), fValueOnOtherAxis))
63 // completely right of and on the clip line. also true for curves.
64 if(bAboveAxis)
66 // add completely
67 aRetval.append(rCandidate);
70 else if(!bParallelToXAxis && fTools::lessOrEqual(aCandidateRange.getMaxX(), fValueOnOtherAxis))
72 // completely left of and on the clip line. also true for curves.
73 if(!bAboveAxis)
75 // add completely
76 aRetval.append(rCandidate);
79 else
81 // add cuts with axis to polygon, including bezier segments
82 // Build edge to cut with. Make it a little big longer than needed for
83 // numerical stability. We want to cut against the edge seen as endless
84 // ray here, but addPointsAtCuts() will limit itself to the
85 // edge's range ]0.0 .. 1.0[.
86 const double fSmallExtension((aCandidateRange.getWidth() + aCandidateRange.getHeight()) * (0.5 * 0.1));
87 const B2DPoint aStart(
88 bParallelToXAxis ? aCandidateRange.getMinX() - fSmallExtension : fValueOnOtherAxis,
89 bParallelToXAxis ? fValueOnOtherAxis : aCandidateRange.getMinY() - fSmallExtension);
90 const B2DPoint aEnd(
91 bParallelToXAxis ? aCandidateRange.getMaxX() + fSmallExtension : fValueOnOtherAxis,
92 bParallelToXAxis ? fValueOnOtherAxis : aCandidateRange.getMaxY() + fSmallExtension);
93 const B2DPolygon aCandidate(addPointsAtCuts(rCandidate, aStart, aEnd));
94 const sal_uInt32 nPointCount(aCandidate.count());
95 const sal_uInt32 nEdgeCount(aCandidate.isClosed() ? nPointCount : nPointCount - 1);
96 B2DCubicBezier aEdge;
97 B2DPolygon aRun;
99 for(sal_uInt32 a(0); a < nEdgeCount; a++)
101 aCandidate.getBezierSegment(a, aEdge);
102 const B2DPoint aTestPoint(aEdge.interpolatePoint(0.5));
103 const bool bInside(bParallelToXAxis ?
104 fTools::moreOrEqual(aTestPoint.getY(), fValueOnOtherAxis) == bAboveAxis :
105 fTools::moreOrEqual(aTestPoint.getX(), fValueOnOtherAxis) == bAboveAxis);
107 if(bInside)
109 if(!aRun.count() || !aRun.getB2DPoint(aRun.count() - 1).equal(aEdge.getStartPoint()))
111 aRun.append(aEdge.getStartPoint());
114 if(aEdge.isBezier())
116 aRun.appendBezierSegment(aEdge.getControlPointA(), aEdge.getControlPointB(), aEdge.getEndPoint());
118 else
120 aRun.append(aEdge.getEndPoint());
123 else
125 if(bStroke && aRun.count())
127 aRetval.append(aRun);
128 aRun.clear();
133 if(aRun.count())
135 if(bStroke)
137 // try to merge this last and first polygon; they may have been
138 // the former polygon's start/end point
139 if(aRetval.count())
141 const B2DPolygon aStartPolygon(aRetval.getB2DPolygon(0));
143 if(aStartPolygon.count() && aStartPolygon.getB2DPoint(0).equal(aRun.getB2DPoint(aRun.count() - 1)))
145 // append start polygon to aRun, remove from result set
146 aRun.append(aStartPolygon); aRun.removeDoublePoints();
147 aRetval.remove(0);
151 aRetval.append(aRun);
153 else
155 // set closed flag and correct last point (which is added double now).
156 closeWithGeometryChange(aRun);
157 aRetval.append(aRun);
163 return aRetval;
166 B2DPolyPolygon clipPolyPolygonOnParallelAxis(const B2DPolyPolygon& rCandidate, bool bParallelToXAxis, bool bAboveAxis, double fValueOnOtherAxis, bool bStroke)
168 const sal_uInt32 nPolygonCount(rCandidate.count());
169 B2DPolyPolygon aRetval;
171 for(sal_uInt32 a(0); a < nPolygonCount; a++)
173 const B2DPolyPolygon aClippedPolyPolygon(clipPolygonOnParallelAxis(rCandidate.getB2DPolygon(a), bParallelToXAxis, bAboveAxis, fValueOnOtherAxis, bStroke));
175 if(aClippedPolyPolygon.count())
177 aRetval.append(aClippedPolyPolygon);
181 return aRetval;
184 B2DPolyPolygon clipPolygonOnRange(const B2DPolygon& rCandidate, const B2DRange& rRange, bool bInside, bool bStroke)
186 const sal_uInt32 nCount(rCandidate.count());
187 B2DPolyPolygon aRetval;
189 if(!nCount)
191 // source is empty
192 return aRetval;
195 if(rRange.isEmpty())
197 if(bInside)
199 // nothing is inside an empty range
200 return aRetval;
202 else
204 // everything is outside an empty range
205 return B2DPolyPolygon(rCandidate);
209 const B2DRange aCandidateRange(getRange(rCandidate));
211 if(rRange.isInside(aCandidateRange))
213 // candidate is completely inside given range
214 if(bInside)
216 // nothing to do
217 return B2DPolyPolygon(rCandidate);
219 else
221 // nothing is outside, then
222 return aRetval;
226 if(!bInside)
228 // cutting off the outer parts of filled polygons at parallel
229 // lines to the axes is only possible for the inner part, not for
230 // the outer part which means cutting a hole into the original polygon.
231 // This is because the inner part is a logical AND-operation of
232 // the four implied half-planes, but the outer part is not.
233 // It is possible for strokes, but with creating unnecessary extra
234 // cuts, so using clipPolygonOnPolyPolygon is better there, too.
235 // This needs to be done with the topology knowledge and is unfortunately
236 // more expensive, too.
237 const B2DPolygon aClip(createPolygonFromRect(rRange));
239 return clipPolygonOnPolyPolygon(rCandidate, B2DPolyPolygon(aClip), bInside, bStroke);
242 // clip against the four axes of the range
243 // against X-Axis, lower value
244 aRetval = clipPolygonOnParallelAxis(rCandidate, true, bInside, rRange.getMinY(), bStroke);
246 if(aRetval.count())
248 // against Y-Axis, lower value
249 if(aRetval.count() == 1)
251 aRetval = clipPolygonOnParallelAxis(aRetval.getB2DPolygon(0), false, bInside, rRange.getMinX(), bStroke);
253 else
255 aRetval = clipPolyPolygonOnParallelAxis(aRetval, false, bInside, rRange.getMinX(), bStroke);
258 if(aRetval.count())
260 // against X-Axis, higher value
261 if(aRetval.count() == 1)
263 aRetval = clipPolygonOnParallelAxis(aRetval.getB2DPolygon(0), true, !bInside, rRange.getMaxY(), bStroke);
265 else
267 aRetval = clipPolyPolygonOnParallelAxis(aRetval, true, !bInside, rRange.getMaxY(), bStroke);
270 if(aRetval.count())
272 // against Y-Axis, higher value
273 if(aRetval.count() == 1)
275 aRetval = clipPolygonOnParallelAxis(aRetval.getB2DPolygon(0), false, !bInside, rRange.getMaxX(), bStroke);
277 else
279 aRetval = clipPolyPolygonOnParallelAxis(aRetval, false, !bInside, rRange.getMaxX(), bStroke);
285 return aRetval;
288 B2DPolyPolygon clipPolyPolygonOnRange(const B2DPolyPolygon& rCandidate, const B2DRange& rRange, bool bInside, bool bStroke)
290 const sal_uInt32 nPolygonCount(rCandidate.count());
291 B2DPolyPolygon aRetval;
293 if(!nPolygonCount)
295 // source is empty
296 return aRetval;
299 if(rRange.isEmpty())
301 if(bInside)
303 // nothing is inside an empty range
304 return aRetval;
306 else
308 // everything is outside an empty range
309 return rCandidate;
313 if(bInside)
315 for(sal_uInt32 a(0); a < nPolygonCount; a++)
317 const B2DPolyPolygon aClippedPolyPolygon(clipPolygonOnRange(rCandidate.getB2DPolygon(a), rRange, bInside, bStroke));
319 if(aClippedPolyPolygon.count())
321 aRetval.append(aClippedPolyPolygon);
325 else
327 // for details, see comment in clipPolygonOnRange for the "cutting off
328 // the outer parts of filled polygons at parallel lines" explanations
329 const B2DPolygon aClip(createPolygonFromRect(rRange));
331 return clipPolyPolygonOnPolyPolygon(rCandidate, B2DPolyPolygon(aClip), bInside, bStroke);
334 return aRetval;
337 B2DPolyPolygon clipPolyPolygonOnPolyPolygon(const B2DPolyPolygon& rCandidate, const B2DPolyPolygon& rClip, bool bInside, bool bStroke)
339 B2DPolyPolygon aRetval;
341 if(rCandidate.count() && rClip.count())
343 // one or both are no rectangle - go the hard way and clip PolyPolygon
344 // against PolyPolygon...
345 if(bStroke)
347 // line clipping, create line snippets by first adding all cut points and
348 // then marching along the edges and detecting if they are inside or outside
349 // the clip polygon
350 for(sal_uInt32 a(0); a < rCandidate.count(); a++)
352 // add cuts with clip to polygon, including bezier segments
353 const B2DPolygon aCandidate(addPointsAtCuts(rCandidate.getB2DPolygon(a), rClip));
354 const sal_uInt32 nPointCount(aCandidate.count());
355 const sal_uInt32 nEdgeCount(aCandidate.isClosed() ? nPointCount : nPointCount - 1);
356 B2DCubicBezier aEdge;
357 B2DPolygon aRun;
359 for(sal_uInt32 b(0); b < nEdgeCount; b++)
361 aCandidate.getBezierSegment(b, aEdge);
362 const B2DPoint aTestPoint(aEdge.interpolatePoint(0.5));
363 const bool bIsInside(utils::isInside(rClip, aTestPoint) == bInside);
365 if(bIsInside)
367 if(!aRun.count())
369 aRun.append(aEdge.getStartPoint());
372 if(aEdge.isBezier())
374 aRun.appendBezierSegment(aEdge.getControlPointA(), aEdge.getControlPointB(), aEdge.getEndPoint());
376 else
378 aRun.append(aEdge.getEndPoint());
381 else
383 if(aRun.count())
385 aRetval.append(aRun);
386 aRun.clear();
391 if(aRun.count())
393 // try to merge this last and first polygon; they may have been
394 // the former polygon's start/end point
395 if(aRetval.count())
397 const B2DPolygon aStartPolygon(aRetval.getB2DPolygon(0));
399 if(aStartPolygon.count() && aStartPolygon.getB2DPoint(0).equal(aRun.getB2DPoint(aRun.count() - 1)))
401 // append start polygon to aRun, remove from result set
402 aRun.append(aStartPolygon); aRun.removeDoublePoints();
403 aRetval.remove(0);
407 aRetval.append(aRun);
411 else
413 // check for simplification with ranges if !bStroke (handling as stroke is more simple),
414 // but also only when bInside, else the simplification may lead to recursive calls (see
415 // calls to clipPolyPolygonOnPolyPolygon in clipPolyPolygonOnRange and clipPolygonOnRange)
416 if (bInside && basegfx::utils::isRectangle(rClip))
418 // #i125349# detect if both given PolyPolygons are indeed ranges
419 if (basegfx::utils::isRectangle(rCandidate))
421 // both are rectangle
422 if(rCandidate.getB2DRange().equal(rClip.getB2DRange()))
424 // if both are equal -> no change
425 return rCandidate;
427 else
429 // not equal -> create new intersection from both ranges,
430 // but much cheaper based on the ranges
431 basegfx::B2DRange aIntersectionRange(rCandidate.getB2DRange());
433 aIntersectionRange.intersect(rClip.getB2DRange());
435 if(aIntersectionRange.isEmpty())
437 // no common IntersectionRange -> the clip will be empty
438 return B2DPolyPolygon();
440 else
442 // use common aIntersectionRange as result, convert
443 // to expected utils::PolyPolygon form
444 return basegfx::B2DPolyPolygon(
445 basegfx::utils::createPolygonFromRect(aIntersectionRange));
449 else
451 // rClip is rectangle -> clip rCandidate on rRectangle, use the much
452 // cheaper and numerically more stable clipping against a range
453 return clipPolyPolygonOnRange(rCandidate, rClip.getB2DRange(), bInside, bStroke);
457 // area clipping
458 B2DPolyPolygon aMergePolyPolygonA(rClip);
460 // First solve all polygon-self and polygon-polygon intersections.
461 // Also get rid of some not-needed polygons (neutral, no area -> when
462 // no intersections, these are tubes).
463 // Now it is possible to correct the orientations in the cut-free
464 // polygons to values corresponding to painting the utils::PolyPolygon with
465 // a XOR-WindingRule.
466 aMergePolyPolygonA = solveCrossovers(aMergePolyPolygonA);
467 aMergePolyPolygonA = stripNeutralPolygons(aMergePolyPolygonA);
468 aMergePolyPolygonA = correctOrientations(aMergePolyPolygonA);
470 if(!bInside)
472 // if we want to get the outside of the clip polygon, make
473 // it a 'Hole' in topological sense
474 aMergePolyPolygonA.flip();
477 B2DPolyPolygon aMergePolyPolygonB(rCandidate);
479 // prepare 2nd source polygon in same way
480 aMergePolyPolygonB = solveCrossovers(aMergePolyPolygonB);
481 aMergePolyPolygonB = stripNeutralPolygons(aMergePolyPolygonB);
482 aMergePolyPolygonB = correctOrientations(aMergePolyPolygonB);
484 // to clip against each other, concatenate and solve all
485 // polygon-polygon crossovers. polygon-self do not need to
486 // be solved again, they were solved in the preparation.
487 aRetval.append(aMergePolyPolygonA);
488 aRetval.append(aMergePolyPolygonB);
489 aRetval = solveCrossovers(aRetval);
491 // now remove neutral polygons (closed, but no area). In a last
492 // step throw away all polygons which have a depth of less than 1
493 // which means there was no logical AND at their position. For the
494 // not-inside solution, the clip was flipped to define it as 'Hole',
495 // so the removal rule is different here; remove all with a depth
496 // of less than 0 (aka holes).
497 aRetval = stripNeutralPolygons(aRetval);
498 aRetval = stripDispensablePolygons(aRetval, bInside);
502 return aRetval;
505 B2DPolyPolygon clipPolygonOnPolyPolygon(const B2DPolygon& rCandidate, const B2DPolyPolygon& rClip, bool bInside, bool bStroke)
507 B2DPolyPolygon aRetval;
509 if(rCandidate.count() && rClip.count())
511 aRetval = clipPolyPolygonOnPolyPolygon(B2DPolyPolygon(rCandidate), rClip, bInside, bStroke);
514 return aRetval;
518 * let a plane be defined as
520 * v.n+d=0
522 * and a ray be defined as
524 * a+(b-a)*t=0
526 * substitute and rearranging yields
528 * t = -(a.n+d)/(n.(b-a))
530 * if the denominator is zero, the line is either
531 * contained in the plane or parallel to the plane.
532 * in either case, there is no intersection.
533 * if numerator and denominator are both zero, the
534 * ray is contained in the plane.
537 struct scissor_plane {
538 double nx,ny; // plane normal
539 double d; // [-] minimum distance from origin
540 sal_uInt32 clipmask; // clipping mask, e.g. 1000 1000
545 * polygon clipping rules (straight out of Foley and Van Dam)
546 * ===========================================================
547 * current |next |emit
548 * ____________________________________
549 * inside |inside |next
550 * inside |outside |intersect with clip plane
551 * outside |outside |nothing
552 * outside |inside |intersect with clip plane follwed by next
555 sal_uInt32 scissorLineSegment( ::basegfx::B2DPoint *in_vertex, // input buffer
556 sal_uInt32 in_count, // number of verts in input buffer
557 ::basegfx::B2DPoint *out_vertex, // output buffer
558 scissor_plane const *pPlane, // scissoring plane
559 const ::basegfx::B2DRectangle &rR ) // clipping rectangle
562 sal_uInt32 out_count=0;
564 // process all the verts
565 for(sal_uInt32 i=0; i<in_count; i++) {
567 // vertices are relative to the coordinate
568 // system defined by the rectangle.
569 ::basegfx::B2DPoint *curr = &in_vertex[i];
570 ::basegfx::B2DPoint *next = &in_vertex[(i+1)%in_count];
572 // perform clipping judgement & mask against current plane.
573 sal_uInt32 clip = pPlane->clipmask & ((getCohenSutherlandClipFlags(*curr,rR)<<4)|getCohenSutherlandClipFlags(*next,rR));
575 if(clip==0) { // both verts are inside
576 out_vertex[out_count++] = *next;
578 else if((clip&0x0f) && (clip&0xf0)) { // both verts are outside
580 else if((clip&0x0f) && (clip&0xf0)==0) { // curr is inside, next is outside
582 // direction vector from 'current' to 'next', *not* normalized
583 // to bring 't' into the [0<=x<=1] interval.
584 ::basegfx::B2DPoint dir((*next)-(*curr));
586 double denominator = ( pPlane->nx*dir.getX() +
587 pPlane->ny*dir.getY() );
588 double numerator = ( pPlane->nx*curr->getX() +
589 pPlane->ny*curr->getY() +
590 pPlane->d );
591 double t = -numerator/denominator;
593 // calculate the actual point of intersection
594 ::basegfx::B2DPoint intersection( curr->getX()+t*dir.getX(),
595 curr->getY()+t*dir.getY() );
597 out_vertex[out_count++] = intersection;
599 else if((clip&0x0f)==0 && (clip&0xf0)) { // curr is outside, next is inside
601 // direction vector from 'current' to 'next', *not* normalized
602 // to bring 't' into the [0<=x<=1] interval.
603 ::basegfx::B2DPoint dir((*next)-(*curr));
605 double denominator = ( pPlane->nx*dir.getX() +
606 pPlane->ny*dir.getY() );
607 double numerator = ( pPlane->nx*curr->getX() +
608 pPlane->ny*curr->getY() +
609 pPlane->d );
610 double t = -numerator/denominator;
612 // calculate the actual point of intersection
613 ::basegfx::B2DPoint intersection( curr->getX()+t*dir.getX(),
614 curr->getY()+t*dir.getY() );
616 out_vertex[out_count++] = intersection;
617 out_vertex[out_count++] = *next;
621 return out_count;
624 B2DPolygon clipTriangleListOnRange( const B2DPolygon& rCandidate,
625 const B2DRange& rRange )
627 B2DPolygon aResult;
629 if( !(rCandidate.count()%3) )
631 const int scissor_plane_count = 4;
633 scissor_plane sp[scissor_plane_count];
635 sp[0].nx = +1.0;
636 sp[0].ny = +0.0;
637 sp[0].d = -(rRange.getMinX());
638 sp[0].clipmask = (RectClipFlags::LEFT << 4) | RectClipFlags::LEFT; // 0001 0001
639 sp[1].nx = -1.0;
640 sp[1].ny = +0.0;
641 sp[1].d = +(rRange.getMaxX());
642 sp[1].clipmask = (RectClipFlags::RIGHT << 4) | RectClipFlags::RIGHT; // 0010 0010
643 sp[2].nx = +0.0;
644 sp[2].ny = +1.0;
645 sp[2].d = -(rRange.getMinY());
646 sp[2].clipmask = (RectClipFlags::TOP << 4) | RectClipFlags::TOP; // 0100 0100
647 sp[3].nx = +0.0;
648 sp[3].ny = -1.0;
649 sp[3].d = +(rRange.getMaxY());
650 sp[3].clipmask = (RectClipFlags::BOTTOM << 4) | RectClipFlags::BOTTOM; // 1000 1000
652 // retrieve the number of vertices of the triangulated polygon
653 const sal_uInt32 nVertexCount = rCandidate.count();
655 if(nVertexCount)
657 // Upper bound for the maximal number of vertices when intersecting an
658 // axis-aligned rectangle with a triangle in E2
660 // The rectangle and the triangle are in general position, and have 4 and 3
661 // vertices, respectively.
663 // Lemma: Since the rectangle is a convex polygon ( see
664 // http://mathworld.wolfram.com/ConvexPolygon.html for a definition), and
665 // has no holes, it follows that any straight line will intersect the
666 // rectangle's border line at utmost two times (with the usual
667 // tie-breaking rule, if the intersection exactly hits an already existing
668 // rectangle vertex, that this intersection is only attributed to one of
669 // the adjoining edges). Thus, having a rectangle intersected with
670 // a half-plane (one side of a straight line denotes 'inside', the
671 // other 'outside') will at utmost add _one_ vertex to the resulting
672 // intersection polygon (adding two intersection vertices, and removing at
673 // least one rectangle vertex):
675 // *
676 // +--+-----------------+
677 // | * |
678 // |* |
679 // + |
680 // *| |
681 // * | |
682 // +--------------------+
684 // Proof: If the straight line intersects the rectangle two
685 // times, it does so for distinct edges, i.e. the intersection has
686 // minimally one of the rectangle's vertices on either side of the straight
687 // line (but maybe more). Thus, the intersection with a half-plane has
688 // minimally _one_ rectangle vertex removed from the resulting clip
689 // polygon, and therefore, a clip against a half-plane has the net effect
690 // of adding at utmost _one_ vertex to the resulting clip polygon.
692 // Theorem: The intersection of a rectangle and a triangle results in a
693 // polygon with at utmost 7 vertices.
695 // Proof: The inside of the triangle can be described as the consecutive
696 // intersection with three half-planes. Together with the lemma above, this
697 // results in at utmost 3 additional vertices added to the already existing 4
698 // rectangle vertices.
700 // This upper bound is attained with the following example configuration:
702 // *
703 // ***
704 // ** *
705 // ** *
706 // ** *
707 // ** *
708 // ** *
709 // ** *
710 // ** *
711 // ** *
712 // ** *
713 // ----*2--------3 *
714 // | ** |*
715 // 1* 4
716 // **| *|
717 // ** | * |
718 // **| * |
719 // 7* * |
720 // --*6-----5-----
721 // ** *
722 // **
724 // As we need to scissor all triangles against the
725 // output rectangle we employ an output buffer for the
726 // resulting vertices. the question is how large this
727 // buffer needs to be compared to the number of
728 // incoming vertices. this buffer needs to hold at
729 // most the number of original vertices times '7'. see
730 // figure above for an example. scissoring triangles
731 // with the cohen-sutherland line clipping algorithm
732 // as implemented here will result in a triangle fan
733 // which will be rendered as separate triangles to
734 // avoid pipeline stalls for each scissored
735 // triangle. creating separate triangles from a
736 // triangle fan produces (n-2)*3 vertices where n is
737 // the number of vertices of the original triangle
738 // fan. for the maximum number of 7 vertices of
739 // resulting triangle fans we therefore need 15 times
740 // the number of original vertices.
742 //const size_t nBufferSize = sizeof(vertex)*(nVertexCount*16);
743 //vertex *pVertices = (vertex*)alloca(nBufferSize);
744 //sal_uInt32 nNumOutput = 0;
746 // we need to clip this triangle against the output rectangle
747 // to ensure that the resulting texture coordinates are in
748 // the valid range from [0<=st<=1]. under normal circumstances
749 // we could use the BORDERCOLOR renderstate but some cards
750 // seem to ignore this feature.
751 ::basegfx::B2DPoint stack[3];
752 unsigned int clipflag = 0;
754 for(sal_uInt32 nIndex=0; nIndex<nVertexCount; ++nIndex)
756 // rotate stack
757 stack[0] = stack[1];
758 stack[1] = stack[2];
759 stack[2] = rCandidate.getB2DPoint(nIndex);
761 // clipping judgement
762 clipflag |= unsigned(!(rRange.isInside(stack[2])));
764 if(nIndex > 1)
766 // consume vertices until a single separate triangle has been visited.
767 if(!((nIndex+1)%3))
769 // if any of the last three vertices was outside
770 // we need to scissor against the destination rectangle
771 if(clipflag & 7)
773 ::basegfx::B2DPoint buf0[16];
774 ::basegfx::B2DPoint buf1[16];
776 sal_uInt32 vertex_count = 3;
778 // clip against all 4 planes passing the result of
779 // each plane as the input to the next using a double buffer
780 vertex_count = scissorLineSegment(stack,vertex_count,buf1,&sp[0],rRange);
781 vertex_count = scissorLineSegment(buf1,vertex_count,buf0,&sp[1],rRange);
782 vertex_count = scissorLineSegment(buf0,vertex_count,buf1,&sp[2],rRange);
783 vertex_count = scissorLineSegment(buf1,vertex_count,buf0,&sp[3],rRange);
785 if(vertex_count >= 3)
787 // convert triangle fan back to triangle list.
788 ::basegfx::B2DPoint v0(buf0[0]);
789 ::basegfx::B2DPoint v1(buf0[1]);
790 for(sal_uInt32 i=2; i<vertex_count; ++i)
792 ::basegfx::B2DPoint v2(buf0[i]);
793 aResult.append(v0);
794 aResult.append(v1);
795 aResult.append(v2);
796 v1 = v2;
800 else
802 // the last triangle has not been altered, simply copy to result
803 for(basegfx::B2DPoint & i : stack)
804 aResult.append(i);
809 clipflag <<= 1;
814 return aResult;
817 } // end of namespace utils
818 } // end of namespace basegfx
820 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */