Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / basegfx / source / polygon / b2dpolygonclipper.cxx
blob9d672b4397c05e88614fb8ebd43231c7c1be5bab
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/polygon/b2dpolypolygoncutter.hxx>
24 #include <basegfx/polygon/b2dpolygoncutandtouch.hxx>
25 #include <basegfx/polygon/b2dpolypolygontools.hxx>
26 #include <basegfx/curve/b2dcubicbezier.hxx>
27 #include <basegfx/utils/rectcliptools.hxx>
29 namespace basegfx
31 namespace utils
33 B2DPolyPolygon clipPolygonOnParallelAxis(const B2DPolygon& rCandidate, bool bParallelToXAxis, bool bAboveAxis, double fValueOnOtherAxis, bool bStroke)
35 B2DPolyPolygon aRetval;
37 if(rCandidate.count())
39 const B2DRange aCandidateRange(getRange(rCandidate));
41 if(bParallelToXAxis && fTools::moreOrEqual(aCandidateRange.getMinY(), fValueOnOtherAxis))
43 // completely above and on the clip line. also true for curves.
44 if(bAboveAxis)
46 // add completely
47 aRetval.append(rCandidate);
50 else if(bParallelToXAxis && fTools::lessOrEqual(aCandidateRange.getMaxY(), fValueOnOtherAxis))
52 // completely below and on the clip line. also true for curves.
53 if(!bAboveAxis)
55 // add completely
56 aRetval.append(rCandidate);
59 else if(!bParallelToXAxis && fTools::moreOrEqual(aCandidateRange.getMinX(), fValueOnOtherAxis))
61 // completely right of and on the clip line. also true for curves.
62 if(bAboveAxis)
64 // add completely
65 aRetval.append(rCandidate);
68 else if(!bParallelToXAxis && fTools::lessOrEqual(aCandidateRange.getMaxX(), fValueOnOtherAxis))
70 // completely left of and on the clip line. also true for curves.
71 if(!bAboveAxis)
73 // add completely
74 aRetval.append(rCandidate);
77 else
79 // add cuts with axis to polygon, including bezier segments
80 // Build edge to cut with. Make it a little big longer than needed for
81 // numerical stability. We want to cut against the edge seen as endless
82 // ray here, but addPointsAtCuts() will limit itself to the
83 // edge's range ]0.0 .. 1.0[.
84 const double fSmallExtension((aCandidateRange.getWidth() + aCandidateRange.getHeight()) * (0.5 * 0.1));
85 const B2DPoint aStart(
86 bParallelToXAxis ? aCandidateRange.getMinX() - fSmallExtension : fValueOnOtherAxis,
87 bParallelToXAxis ? fValueOnOtherAxis : aCandidateRange.getMinY() - fSmallExtension);
88 const B2DPoint aEnd(
89 bParallelToXAxis ? aCandidateRange.getMaxX() + fSmallExtension : fValueOnOtherAxis,
90 bParallelToXAxis ? fValueOnOtherAxis : aCandidateRange.getMaxY() + fSmallExtension);
91 const B2DPolygon aCandidate(addPointsAtCuts(rCandidate, aStart, aEnd));
92 const sal_uInt32 nPointCount(aCandidate.count());
93 const sal_uInt32 nEdgeCount(aCandidate.isClosed() ? nPointCount : nPointCount - 1);
94 B2DCubicBezier aEdge;
95 B2DPolygon aRun;
97 for(sal_uInt32 a(0); a < nEdgeCount; a++)
99 aCandidate.getBezierSegment(a, aEdge);
100 const B2DPoint aTestPoint(aEdge.interpolatePoint(0.5));
101 const bool bInside(bParallelToXAxis ?
102 fTools::moreOrEqual(aTestPoint.getY(), fValueOnOtherAxis) == bAboveAxis :
103 fTools::moreOrEqual(aTestPoint.getX(), fValueOnOtherAxis) == bAboveAxis);
105 if(bInside)
107 if(!aRun.count() || !aRun.getB2DPoint(aRun.count() - 1).equal(aEdge.getStartPoint()))
109 aRun.append(aEdge.getStartPoint());
112 if(aEdge.isBezier())
114 aRun.appendBezierSegment(aEdge.getControlPointA(), aEdge.getControlPointB(), aEdge.getEndPoint());
116 else
118 aRun.append(aEdge.getEndPoint());
121 else
123 if(bStroke && aRun.count())
125 aRetval.append(aRun);
126 aRun.clear();
131 if(aRun.count())
133 if(bStroke)
135 // try to merge this last and first polygon; they may have been
136 // the former polygon's start/end point
137 if(aRetval.count())
139 const B2DPolygon aStartPolygon(aRetval.getB2DPolygon(0));
141 if(aStartPolygon.count() && aStartPolygon.getB2DPoint(0).equal(aRun.getB2DPoint(aRun.count() - 1)))
143 // append start polygon to aRun, remove from result set
144 aRun.append(aStartPolygon); aRun.removeDoublePoints();
145 aRetval.remove(0);
149 aRetval.append(aRun);
151 else
153 // set closed flag and correct last point (which is added double now).
154 closeWithGeometryChange(aRun);
155 aRetval.append(aRun);
161 return aRetval;
164 B2DPolyPolygon clipPolyPolygonOnParallelAxis(const B2DPolyPolygon& rCandidate, bool bParallelToXAxis, bool bAboveAxis, double fValueOnOtherAxis, bool bStroke)
166 const sal_uInt32 nPolygonCount(rCandidate.count());
167 B2DPolyPolygon aRetval;
169 for(sal_uInt32 a(0); a < nPolygonCount; a++)
171 const B2DPolyPolygon aClippedPolyPolygon(clipPolygonOnParallelAxis(rCandidate.getB2DPolygon(a), bParallelToXAxis, bAboveAxis, fValueOnOtherAxis, bStroke));
173 if(aClippedPolyPolygon.count())
175 aRetval.append(aClippedPolyPolygon);
179 return aRetval;
182 B2DPolyPolygon clipPolygonOnRange(const B2DPolygon& rCandidate, const B2DRange& rRange, bool bInside, bool bStroke)
184 const sal_uInt32 nCount(rCandidate.count());
185 B2DPolyPolygon aRetval;
187 if(!nCount)
189 // source is empty
190 return aRetval;
193 if(rRange.isEmpty())
195 if(bInside)
197 // nothing is inside an empty range
198 return aRetval;
200 else
202 // everything is outside an empty range
203 return B2DPolyPolygon(rCandidate);
207 const B2DRange aCandidateRange(getRange(rCandidate));
209 if(rRange.isInside(aCandidateRange))
211 // candidate is completely inside given range
212 if(bInside)
214 // nothing to do
215 return B2DPolyPolygon(rCandidate);
217 else
219 // nothing is outside, then
220 return aRetval;
224 if(!bInside)
226 // cutting off the outer parts of filled polygons at parallel
227 // lines to the axes is only possible for the inner part, not for
228 // the outer part which means cutting a hole into the original polygon.
229 // This is because the inner part is a logical AND-operation of
230 // the four implied half-planes, but the outer part is not.
231 // It is possible for strokes, but with creating unnecessary extra
232 // cuts, so using clipPolygonOnPolyPolygon is better there, too.
233 // This needs to be done with the topology knowledge and is unfortunately
234 // more expensive, too.
235 const B2DPolygon aClip(createPolygonFromRect(rRange));
237 return clipPolygonOnPolyPolygon(rCandidate, B2DPolyPolygon(aClip), bInside, bStroke);
240 // clip against the four axes of the range
241 // against X-Axis, lower value
242 aRetval = clipPolygonOnParallelAxis(rCandidate, true, bInside, rRange.getMinY(), bStroke);
244 if(aRetval.count())
246 // against Y-Axis, lower value
247 if(aRetval.count() == 1)
249 aRetval = clipPolygonOnParallelAxis(aRetval.getB2DPolygon(0), false, bInside, rRange.getMinX(), bStroke);
251 else
253 aRetval = clipPolyPolygonOnParallelAxis(aRetval, false, bInside, rRange.getMinX(), bStroke);
256 if(aRetval.count())
258 // against X-Axis, higher value
259 if(aRetval.count() == 1)
261 aRetval = clipPolygonOnParallelAxis(aRetval.getB2DPolygon(0), true, false, rRange.getMaxY(), bStroke);
263 else
265 aRetval = clipPolyPolygonOnParallelAxis(aRetval, true, false, rRange.getMaxY(), bStroke);
268 if(aRetval.count())
270 // against Y-Axis, higher value
271 if(aRetval.count() == 1)
273 aRetval = clipPolygonOnParallelAxis(aRetval.getB2DPolygon(0), false, false, rRange.getMaxX(), bStroke);
275 else
277 aRetval = clipPolyPolygonOnParallelAxis(aRetval, false, false, rRange.getMaxX(), bStroke);
283 return aRetval;
286 B2DPolyPolygon clipPolyPolygonOnRange(const B2DPolyPolygon& rCandidate, const B2DRange& rRange, bool bInside, bool bStroke)
288 const sal_uInt32 nPolygonCount(rCandidate.count());
289 B2DPolyPolygon aRetval;
291 if(!nPolygonCount)
293 // source is empty
294 return aRetval;
297 if(rRange.isEmpty())
299 if(bInside)
301 // nothing is inside an empty range
302 return aRetval;
304 else
306 // everything is outside an empty range
307 return rCandidate;
311 if(bInside)
313 for(sal_uInt32 a(0); a < nPolygonCount; a++)
315 const B2DPolyPolygon aClippedPolyPolygon(clipPolygonOnRange(rCandidate.getB2DPolygon(a), rRange, bInside, bStroke));
317 if(aClippedPolyPolygon.count())
319 aRetval.append(aClippedPolyPolygon);
323 else
325 // for details, see comment in clipPolygonOnRange for the "cutting off
326 // the outer parts of filled polygons at parallel lines" explanations
327 const B2DPolygon aClip(createPolygonFromRect(rRange));
329 return clipPolyPolygonOnPolyPolygon(rCandidate, B2DPolyPolygon(aClip), bInside, bStroke);
332 return aRetval;
335 B2DPolyPolygon clipPolyPolygonOnPolyPolygon(const B2DPolyPolygon& rCandidate, const B2DPolyPolygon& rClip, bool bInside, bool bStroke)
337 B2DPolyPolygon aRetval;
339 if(rCandidate.count() && rClip.count())
341 // one or both are no rectangle - go the hard way and clip PolyPolygon
342 // against PolyPolygon...
343 if(bStroke)
345 // line clipping, create line snippets by first adding all cut points and
346 // then marching along the edges and detecting if they are inside or outside
347 // the clip polygon
348 for(sal_uInt32 a(0); a < rCandidate.count(); a++)
350 // add cuts with clip to polygon, including bezier segments
351 const B2DPolygon aCandidate(addPointsAtCuts(rCandidate.getB2DPolygon(a), rClip));
352 const sal_uInt32 nPointCount(aCandidate.count());
353 const sal_uInt32 nEdgeCount(aCandidate.isClosed() ? nPointCount : nPointCount - 1);
354 B2DCubicBezier aEdge;
355 B2DPolygon aRun;
357 for(sal_uInt32 b(0); b < nEdgeCount; b++)
359 aCandidate.getBezierSegment(b, aEdge);
360 const B2DPoint aTestPoint(aEdge.interpolatePoint(0.5));
361 const bool bIsInside(utils::isInside(rClip, aTestPoint) == bInside);
363 if(bIsInside)
365 if(!aRun.count())
367 aRun.append(aEdge.getStartPoint());
370 if(aEdge.isBezier())
372 aRun.appendBezierSegment(aEdge.getControlPointA(), aEdge.getControlPointB(), aEdge.getEndPoint());
374 else
376 aRun.append(aEdge.getEndPoint());
379 else
381 if(aRun.count())
383 aRetval.append(aRun);
384 aRun.clear();
389 if(aRun.count())
391 // try to merge this last and first polygon; they may have been
392 // the former polygon's start/end point
393 if(aRetval.count())
395 const B2DPolygon aStartPolygon(aRetval.getB2DPolygon(0));
397 if(aStartPolygon.count() && aStartPolygon.getB2DPoint(0).equal(aRun.getB2DPoint(aRun.count() - 1)))
399 // append start polygon to aRun, remove from result set
400 aRun.append(aStartPolygon); aRun.removeDoublePoints();
401 aRetval.remove(0);
405 aRetval.append(aRun);
409 else
411 // check for simplification with ranges if !bStroke (handling as stroke is more simple),
412 // but also only when bInside, else the simplification may lead to recursive calls (see
413 // calls to clipPolyPolygonOnPolyPolygon in clipPolyPolygonOnRange and clipPolygonOnRange)
414 if (bInside && basegfx::utils::isRectangle(rClip))
416 // #i125349# detect if both given PolyPolygons are indeed ranges
417 if (basegfx::utils::isRectangle(rCandidate))
419 // both are rectangle
420 if(rCandidate.getB2DRange().equal(rClip.getB2DRange()))
422 // if both are equal -> no change
423 return rCandidate;
425 else
427 // not equal -> create new intersection from both ranges,
428 // but much cheaper based on the ranges
429 basegfx::B2DRange aIntersectionRange(rCandidate.getB2DRange());
431 aIntersectionRange.intersect(rClip.getB2DRange());
433 if(aIntersectionRange.isEmpty())
435 // no common IntersectionRange -> the clip will be empty
436 return B2DPolyPolygon();
438 else
440 // use common aIntersectionRange as result, convert
441 // to expected utils::PolyPolygon form
442 return basegfx::B2DPolyPolygon(
443 basegfx::utils::createPolygonFromRect(aIntersectionRange));
447 else
449 // rClip is rectangle -> clip rCandidate on rRectangle, use the much
450 // cheaper and numerically more stable clipping against a range
451 return clipPolyPolygonOnRange(rCandidate, rClip.getB2DRange(), bInside, bStroke);
455 // area clipping
457 // First solve all polygon-self and polygon-polygon intersections.
458 // Also get rid of some not-needed polygons (neutral, no area -> when
459 // no intersections, these are tubes).
460 // Now it is possible to correct the orientations in the cut-free
461 // polygons to values corresponding to painting the utils::PolyPolygon with
462 // a XOR-WindingRule.
463 B2DPolyPolygon aMergePolyPolygonA = solveCrossovers(rClip);
464 aMergePolyPolygonA = stripNeutralPolygons(aMergePolyPolygonA);
465 aMergePolyPolygonA = correctOrientations(aMergePolyPolygonA);
467 if(!bInside)
469 // if we want to get the outside of the clip polygon, make
470 // it a 'Hole' in topological sense
471 aMergePolyPolygonA.flip();
475 // prepare 2nd source polygon in same way
476 B2DPolyPolygon aMergePolyPolygonB = solveCrossovers(rCandidate);
477 aMergePolyPolygonB = stripNeutralPolygons(aMergePolyPolygonB);
478 aMergePolyPolygonB = correctOrientations(aMergePolyPolygonB);
480 // to clip against each other, concatenate and solve all
481 // polygon-polygon crossovers. polygon-self do not need to
482 // be solved again, they were solved in the preparation.
483 aRetval.append(aMergePolyPolygonA);
484 aRetval.append(aMergePolyPolygonB);
485 aRetval = solveCrossovers(aRetval);
487 // now remove neutral polygons (closed, but no area). In a last
488 // step throw away all polygons which have a depth of less than 1
489 // which means there was no logical AND at their position. For the
490 // not-inside solution, the clip was flipped to define it as 'Hole',
491 // so the removal rule is different here; remove all with a depth
492 // of less than 0 (aka holes).
493 aRetval = stripNeutralPolygons(aRetval);
494 aRetval = stripDispensablePolygons(aRetval, bInside);
498 return aRetval;
501 B2DPolyPolygon clipPolygonOnPolyPolygon(const B2DPolygon& rCandidate, const B2DPolyPolygon& rClip, bool bInside, bool bStroke)
503 B2DPolyPolygon aRetval;
505 if(rCandidate.count() && rClip.count())
507 aRetval = clipPolyPolygonOnPolyPolygon(B2DPolyPolygon(rCandidate), rClip, bInside, bStroke);
510 return aRetval;
514 * let a plane be defined as
516 * v.n+d=0
518 * and a ray be defined as
520 * a+(b-a)*t=0
522 * substitute and rearranging yields
524 * t = -(a.n+d)/(n.(b-a))
526 * if the denominator is zero, the line is either
527 * contained in the plane or parallel to the plane.
528 * in either case, there is no intersection.
529 * if numerator and denominator are both zero, the
530 * ray is contained in the plane.
533 struct scissor_plane {
534 double nx,ny; // plane normal
535 double d; // [-] minimum distance from origin
536 sal_uInt32 clipmask; // clipping mask, e.g. 1000 1000
541 * polygon clipping rules (straight out of Foley and Van Dam)
542 * ===========================================================
543 * current |next |emit
544 * ____________________________________
545 * inside |inside |next
546 * inside |outside |intersect with clip plane
547 * outside |outside |nothing
548 * outside |inside |intersect with clip plane follwed by next
551 static sal_uInt32 scissorLineSegment( ::basegfx::B2DPoint *in_vertex, // input buffer
552 sal_uInt32 in_count, // number of verts in input buffer
553 ::basegfx::B2DPoint *out_vertex, // output buffer
554 scissor_plane const *pPlane, // scissoring plane
555 const ::basegfx::B2DRectangle &rR ) // clipping rectangle
558 sal_uInt32 out_count=0;
560 // process all the verts
561 for(sal_uInt32 i=0; i<in_count; i++) {
563 // vertices are relative to the coordinate
564 // system defined by the rectangle.
565 ::basegfx::B2DPoint *curr = &in_vertex[i];
566 ::basegfx::B2DPoint *next = &in_vertex[(i+1)%in_count];
568 // perform clipping judgement & mask against current plane.
569 sal_uInt32 clip = pPlane->clipmask & ((getCohenSutherlandClipFlags(*curr,rR)<<4)|getCohenSutherlandClipFlags(*next,rR));
571 if(clip==0) { // both verts are inside
572 out_vertex[out_count++] = *next;
574 else if((clip&0x0f) && (clip&0xf0)) { // both verts are outside
576 else if((clip&0x0f) && (clip&0xf0)==0) { // curr is inside, next is outside
578 // direction vector from 'current' to 'next', *not* normalized
579 // to bring 't' into the [0<=x<=1] interval.
580 ::basegfx::B2DPoint dir((*next)-(*curr));
582 double denominator = pPlane->nx*dir.getX() +
583 pPlane->ny*dir.getY();
584 double numerator = pPlane->nx*curr->getX() +
585 pPlane->ny*curr->getY() +
586 pPlane->d;
587 double t = -numerator/denominator;
589 // calculate the actual point of intersection
590 ::basegfx::B2DPoint intersection( curr->getX()+t*dir.getX(),
591 curr->getY()+t*dir.getY() );
593 out_vertex[out_count++] = intersection;
595 else if((clip&0x0f)==0 && (clip&0xf0)) { // curr is outside, next is inside
597 // direction vector from 'current' to 'next', *not* normalized
598 // to bring 't' into the [0<=x<=1] interval.
599 ::basegfx::B2DPoint dir((*next)-(*curr));
601 double denominator = pPlane->nx*dir.getX() +
602 pPlane->ny*dir.getY();
603 double numerator = pPlane->nx*curr->getX() +
604 pPlane->ny*curr->getY() +
605 pPlane->d;
606 double t = -numerator/denominator;
608 // calculate the actual point of intersection
609 ::basegfx::B2DPoint intersection( curr->getX()+t*dir.getX(),
610 curr->getY()+t*dir.getY() );
612 out_vertex[out_count++] = intersection;
613 out_vertex[out_count++] = *next;
617 return out_count;
620 B2DPolygon clipTriangleListOnRange( const B2DPolygon& rCandidate,
621 const B2DRange& rRange )
623 B2DPolygon aResult;
625 if( !(rCandidate.count()%3) )
627 const int scissor_plane_count = 4;
629 scissor_plane sp[scissor_plane_count];
631 sp[0].nx = +1.0;
632 sp[0].ny = +0.0;
633 sp[0].d = -(rRange.getMinX());
634 sp[0].clipmask = (RectClipFlags::LEFT << 4) | RectClipFlags::LEFT; // 0001 0001
635 sp[1].nx = -1.0;
636 sp[1].ny = +0.0;
637 sp[1].d = +(rRange.getMaxX());
638 sp[1].clipmask = (RectClipFlags::RIGHT << 4) | RectClipFlags::RIGHT; // 0010 0010
639 sp[2].nx = +0.0;
640 sp[2].ny = +1.0;
641 sp[2].d = -(rRange.getMinY());
642 sp[2].clipmask = (RectClipFlags::TOP << 4) | RectClipFlags::TOP; // 0100 0100
643 sp[3].nx = +0.0;
644 sp[3].ny = -1.0;
645 sp[3].d = +(rRange.getMaxY());
646 sp[3].clipmask = (RectClipFlags::BOTTOM << 4) | RectClipFlags::BOTTOM; // 1000 1000
648 // retrieve the number of vertices of the triangulated polygon
649 const sal_uInt32 nVertexCount = rCandidate.count();
651 if(nVertexCount)
653 // Upper bound for the maximal number of vertices when intersecting an
654 // axis-aligned rectangle with a triangle in E2
656 // The rectangle and the triangle are in general position, and have 4 and 3
657 // vertices, respectively.
659 // Lemma: Since the rectangle is a convex polygon ( see
660 // http://mathworld.wolfram.com/ConvexPolygon.html for a definition), and
661 // has no holes, it follows that any straight line will intersect the
662 // rectangle's border line at utmost two times (with the usual
663 // tie-breaking rule, if the intersection exactly hits an already existing
664 // rectangle vertex, that this intersection is only attributed to one of
665 // the adjoining edges). Thus, having a rectangle intersected with
666 // a half-plane (one side of a straight line denotes 'inside', the
667 // other 'outside') will at utmost add _one_ vertex to the resulting
668 // intersection polygon (adding two intersection vertices, and removing at
669 // least one rectangle vertex):
671 // *
672 // +--+-----------------+
673 // | * |
674 // |* |
675 // + |
676 // *| |
677 // * | |
678 // +--------------------+
680 // Proof: If the straight line intersects the rectangle two
681 // times, it does so for distinct edges, i.e. the intersection has
682 // minimally one of the rectangle's vertices on either side of the straight
683 // line (but maybe more). Thus, the intersection with a half-plane has
684 // minimally _one_ rectangle vertex removed from the resulting clip
685 // polygon, and therefore, a clip against a half-plane has the net effect
686 // of adding at utmost _one_ vertex to the resulting clip polygon.
688 // Theorem: The intersection of a rectangle and a triangle results in a
689 // polygon with at utmost 7 vertices.
691 // Proof: The inside of the triangle can be described as the consecutive
692 // intersection with three half-planes. Together with the lemma above, this
693 // results in at utmost 3 additional vertices added to the already existing 4
694 // rectangle vertices.
696 // This upper bound is attained with the following example configuration:
698 // *
699 // ***
700 // ** *
701 // ** *
702 // ** *
703 // ** *
704 // ** *
705 // ** *
706 // ** *
707 // ** *
708 // ** *
709 // ----*2--------3 *
710 // | ** |*
711 // 1* 4
712 // **| *|
713 // ** | * |
714 // **| * |
715 // 7* * |
716 // --*6-----5-----
717 // ** *
718 // **
720 // As we need to scissor all triangles against the
721 // output rectangle we employ an output buffer for the
722 // resulting vertices. the question is how large this
723 // buffer needs to be compared to the number of
724 // incoming vertices. this buffer needs to hold at
725 // most the number of original vertices times '7'. see
726 // figure above for an example. scissoring triangles
727 // with the cohen-sutherland line clipping algorithm
728 // as implemented here will result in a triangle fan
729 // which will be rendered as separate triangles to
730 // avoid pipeline stalls for each scissored
731 // triangle. creating separate triangles from a
732 // triangle fan produces (n-2)*3 vertices where n is
733 // the number of vertices of the original triangle
734 // fan. for the maximum number of 7 vertices of
735 // resulting triangle fans we therefore need 15 times
736 // the number of original vertices.
738 //const size_t nBufferSize = sizeof(vertex)*(nVertexCount*16);
739 //vertex *pVertices = (vertex*)alloca(nBufferSize);
740 //sal_uInt32 nNumOutput = 0;
742 // we need to clip this triangle against the output rectangle
743 // to ensure that the resulting texture coordinates are in
744 // the valid range from [0<=st<=1]. under normal circumstances
745 // we could use the BORDERCOLOR renderstate but some cards
746 // seem to ignore this feature.
747 ::basegfx::B2DPoint stack[3];
748 unsigned int clipflag = 0;
750 for(sal_uInt32 nIndex=0; nIndex<nVertexCount; ++nIndex)
752 // rotate stack
753 stack[0] = stack[1];
754 stack[1] = stack[2];
755 stack[2] = rCandidate.getB2DPoint(nIndex);
757 // clipping judgement
758 clipflag |= unsigned(!(rRange.isInside(stack[2])));
760 if(nIndex > 1)
762 // consume vertices until a single separate triangle has been visited.
763 if(!((nIndex+1)%3))
765 // if any of the last three vertices was outside
766 // we need to scissor against the destination rectangle
767 if(clipflag & 7)
769 ::basegfx::B2DPoint buf0[16];
770 ::basegfx::B2DPoint buf1[16];
772 sal_uInt32 vertex_count = 3;
774 // clip against all 4 planes passing the result of
775 // each plane as the input to the next using a double buffer
776 vertex_count = scissorLineSegment(stack,vertex_count,buf1,&sp[0],rRange);
777 vertex_count = scissorLineSegment(buf1,vertex_count,buf0,&sp[1],rRange);
778 vertex_count = scissorLineSegment(buf0,vertex_count,buf1,&sp[2],rRange);
779 vertex_count = scissorLineSegment(buf1,vertex_count,buf0,&sp[3],rRange);
781 if(vertex_count >= 3)
783 // convert triangle fan back to triangle list.
784 ::basegfx::B2DPoint v0(buf0[0]);
785 ::basegfx::B2DPoint v1(buf0[1]);
786 for(sal_uInt32 i=2; i<vertex_count; ++i)
788 ::basegfx::B2DPoint v2(buf0[i]);
789 aResult.append(v0);
790 aResult.append(v1);
791 aResult.append(v2);
792 v1 = v2;
796 else
798 // the last triangle has not been altered, simply copy to result
799 for(const basegfx::B2DPoint & i : stack)
800 aResult.append(i);
805 clipflag <<= 1;
810 return aResult;
813 } // end of namespace utils
814 } // end of namespace basegfx
816 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */