Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / basegfx / source / polygon / b2dpolygonclipper.cxx
blob6a460430c1684582e65339570db29c2376bf5ab8
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 <osl/diagnose.h>
22 #include <basegfx/polygon/b2dpolygontools.hxx>
23 #include <basegfx/numeric/ftools.hxx>
24 #include <basegfx/matrix/b2dhommatrix.hxx>
25 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
26 #include <basegfx/polygon/b2dpolygoncutandtouch.hxx>
27 #include <basegfx/polygon/b2dpolypolygontools.hxx>
28 #include <basegfx/curve/b2dcubicbezier.hxx>
29 #include <basegfx/tools/rectcliptools.hxx>
30 #include <basegfx/matrix/b2dhommatrixtools.hxx>
32 //////////////////////////////////////////////////////////////////////////////
34 namespace basegfx
36 namespace tools
38 B2DPolyPolygon clipPolygonOnParallelAxis(const B2DPolygon& rCandidate, bool bParallelToXAxis, bool bAboveAxis, double fValueOnOtherAxis, bool bStroke)
40 B2DPolyPolygon aRetval;
42 if(rCandidate.count())
44 const B2DRange aCandidateRange(getRange(rCandidate));
46 if(bParallelToXAxis && fTools::moreOrEqual(aCandidateRange.getMinY(), fValueOnOtherAxis))
48 // completely above and on the clip line. also true for curves.
49 if(bAboveAxis)
51 // add completely
52 aRetval.append(rCandidate);
55 else if(bParallelToXAxis && fTools::lessOrEqual(aCandidateRange.getMaxY(), fValueOnOtherAxis))
57 // completely below and on the clip line. also true for curves.
58 if(!bAboveAxis)
60 // add completely
61 aRetval.append(rCandidate);
64 else if(!bParallelToXAxis && fTools::moreOrEqual(aCandidateRange.getMinX(), fValueOnOtherAxis))
66 // completely right of and on the clip line. also true for curves.
67 if(bAboveAxis)
69 // add completely
70 aRetval.append(rCandidate);
73 else if(!bParallelToXAxis && fTools::lessOrEqual(aCandidateRange.getMaxX(), fValueOnOtherAxis))
75 // completely left of and on the clip line. also true for curves.
76 if(!bAboveAxis)
78 // add completely
79 aRetval.append(rCandidate);
82 else
84 // add cuts with axis to polygon, including bezier segments
85 // Build edge to cut with. Make it a little big longer than needed for
86 // numerical stability. We want to cut against the edge seen as endless
87 // ray here, but addPointsAtCuts() will limit itself to the
88 // edge's range ]0.0 .. 1.0[.
89 const double fSmallExtension((aCandidateRange.getWidth() + aCandidateRange.getHeight()) * (0.5 * 0.1));
90 const B2DPoint aStart(
91 bParallelToXAxis ? aCandidateRange.getMinX() - fSmallExtension : fValueOnOtherAxis,
92 bParallelToXAxis ? fValueOnOtherAxis : aCandidateRange.getMinY() - fSmallExtension);
93 const B2DPoint aEnd(
94 bParallelToXAxis ? aCandidateRange.getMaxX() + fSmallExtension : fValueOnOtherAxis,
95 bParallelToXAxis ? fValueOnOtherAxis : aCandidateRange.getMaxY() + fSmallExtension);
96 const B2DPolygon aCandidate(addPointsAtCuts(rCandidate, aStart, aEnd));
97 const sal_uInt32 nPointCount(aCandidate.count());
98 const sal_uInt32 nEdgeCount(aCandidate.isClosed() ? nPointCount : nPointCount - 1L);
99 B2DCubicBezier aEdge;
100 B2DPolygon aRun;
102 for(sal_uInt32 a(0L); a < nEdgeCount; a++)
104 aCandidate.getBezierSegment(a, aEdge);
105 const B2DPoint aTestPoint(aEdge.interpolatePoint(0.5));
106 const bool bInside(bParallelToXAxis ?
107 fTools::moreOrEqual(aTestPoint.getY(), fValueOnOtherAxis) == bAboveAxis :
108 fTools::moreOrEqual(aTestPoint.getX(), fValueOnOtherAxis) == bAboveAxis);
110 if(bInside)
112 if(!aRun.count() || !aRun.getB2DPoint(aRun.count() - 1).equal(aEdge.getStartPoint()))
114 aRun.append(aEdge.getStartPoint());
117 if(aEdge.isBezier())
119 aRun.appendBezierSegment(aEdge.getControlPointA(), aEdge.getControlPointB(), aEdge.getEndPoint());
121 else
123 aRun.append(aEdge.getEndPoint());
126 else
128 if(bStroke && aRun.count())
130 aRetval.append(aRun);
131 aRun.clear();
136 if(aRun.count())
138 if(bStroke)
140 // try to merge this last and first polygon; they may have been
141 // the former polygon's start/end point
142 if(aRetval.count())
144 const B2DPolygon aStartPolygon(aRetval.getB2DPolygon(0));
146 if(aStartPolygon.count() && aStartPolygon.getB2DPoint(0).equal(aRun.getB2DPoint(aRun.count() - 1)))
148 // append start polygon to aRun, remove from result set
149 aRun.append(aStartPolygon); aRun.removeDoublePoints();
150 aRetval.remove(0);
154 aRetval.append(aRun);
156 else
158 // set closed flag and correct last point (which is added double now).
159 closeWithGeometryChange(aRun);
160 aRetval.append(aRun);
166 return aRetval;
169 B2DPolyPolygon clipPolyPolygonOnParallelAxis(const B2DPolyPolygon& rCandidate, bool bParallelToXAxis, bool bAboveAxis, double fValueOnOtherAxis, bool bStroke)
171 const sal_uInt32 nPolygonCount(rCandidate.count());
172 B2DPolyPolygon aRetval;
174 for(sal_uInt32 a(0L); a < nPolygonCount; a++)
176 const B2DPolyPolygon aClippedPolyPolygon(clipPolygonOnParallelAxis(rCandidate.getB2DPolygon(a), bParallelToXAxis, bAboveAxis, fValueOnOtherAxis, bStroke));
178 if(aClippedPolyPolygon.count())
180 aRetval.append(aClippedPolyPolygon);
184 return aRetval;
187 B2DPolyPolygon clipPolygonOnRange(const B2DPolygon& rCandidate, const B2DRange& rRange, bool bInside, bool bStroke)
189 const sal_uInt32 nCount(rCandidate.count());
190 B2DPolyPolygon aRetval;
192 if(!nCount)
194 // source is empty
195 return aRetval;
198 if(rRange.isEmpty())
200 if(bInside)
202 // nothing is inside an empty range
203 return aRetval;
205 else
207 // everything is outside an empty range
208 return B2DPolyPolygon(rCandidate);
212 const B2DRange aCandidateRange(getRange(rCandidate));
214 if(rRange.isInside(aCandidateRange))
216 // candidate is completely inside given range
217 if(bInside)
219 // nothing to do
220 return B2DPolyPolygon(rCandidate);
222 else
224 // nothing is outside, then
225 return aRetval;
229 if(!bInside)
231 // cutting off the outer parts of filled polygons at parallell
232 // lines to the axes is only possible for the inner part, not for
233 // the outer part which means cutting a hole into the original polygon.
234 // This is because the inner part is a logical AND-operation of
235 // the four implied half-planes, but the outer part is not.
236 // It is possible for strokes, but with creating unnecessary extra
237 // cuts, so using clipPolygonOnPolyPolygon is better there, too.
238 // This needs to be done with the topology knowlegde and is unfurtunately
239 // more expensive, too.
240 const B2DPolygon aClip(createPolygonFromRect(rRange));
242 return clipPolygonOnPolyPolygon(rCandidate, B2DPolyPolygon(aClip), bInside, bStroke);
245 // clip against the four axes of the range
246 // against X-Axis, lower value
247 aRetval = clipPolygonOnParallelAxis(rCandidate, true, bInside, rRange.getMinY(), bStroke);
249 if(aRetval.count())
251 // against Y-Axis, lower value
252 if(1L == aRetval.count())
254 aRetval = clipPolygonOnParallelAxis(aRetval.getB2DPolygon(0L), false, bInside, rRange.getMinX(), bStroke);
256 else
258 aRetval = clipPolyPolygonOnParallelAxis(aRetval, false, bInside, rRange.getMinX(), bStroke);
261 if(aRetval.count())
263 // against X-Axis, higher value
264 if(1L == aRetval.count())
266 aRetval = clipPolygonOnParallelAxis(aRetval.getB2DPolygon(0L), true, !bInside, rRange.getMaxY(), bStroke);
268 else
270 aRetval = clipPolyPolygonOnParallelAxis(aRetval, true, !bInside, rRange.getMaxY(), bStroke);
273 if(aRetval.count())
275 // against Y-Axis, higher value
276 if(1L == aRetval.count())
278 aRetval = clipPolygonOnParallelAxis(aRetval.getB2DPolygon(0L), false, !bInside, rRange.getMaxX(), bStroke);
280 else
282 aRetval = clipPolyPolygonOnParallelAxis(aRetval, false, !bInside, rRange.getMaxX(), bStroke);
288 return aRetval;
291 B2DPolyPolygon clipPolyPolygonOnRange(const B2DPolyPolygon& rCandidate, const B2DRange& rRange, bool bInside, bool bStroke)
293 const sal_uInt32 nPolygonCount(rCandidate.count());
294 B2DPolyPolygon aRetval;
296 if(!nPolygonCount)
298 // source is empty
299 return aRetval;
302 if(rRange.isEmpty())
304 if(bInside)
306 // nothing is inside an empty range
307 return aRetval;
309 else
311 // everything is outside an empty range
312 return rCandidate;
316 if(bInside)
318 for(sal_uInt32 a(0L); a < nPolygonCount; a++)
320 const B2DPolyPolygon aClippedPolyPolygon(clipPolygonOnRange(rCandidate.getB2DPolygon(a), rRange, bInside, bStroke));
322 if(aClippedPolyPolygon.count())
324 aRetval.append(aClippedPolyPolygon);
328 else
330 // for details, see comment in clipPolygonOnRange for the "cutting off
331 // the outer parts of filled polygons at parallell lines" explanations
332 const B2DPolygon aClip(createPolygonFromRect(rRange));
334 return clipPolyPolygonOnPolyPolygon(rCandidate, B2DPolyPolygon(aClip), bInside, bStroke);
337 return aRetval;
340 //////////////////////////////////////////////////////////////////////////////
342 B2DPolyPolygon clipPolyPolygonOnPolyPolygon(const B2DPolyPolygon& rCandidate, const B2DPolyPolygon& rClip, bool bInside, bool bStroke)
344 B2DPolyPolygon aRetval;
346 if(rCandidate.count() && rClip.count())
348 if(bStroke)
350 // line clipping, create line snippets by first adding all cut points and
351 // then marching along the edges and detecting if they are inside or outside
352 // the clip polygon
353 for(sal_uInt32 a(0); a < rCandidate.count(); a++)
355 // add cuts with clip to polygon, including bezier segments
356 const B2DPolygon aCandidate(addPointsAtCuts(rCandidate.getB2DPolygon(a), rClip));
357 const sal_uInt32 nPointCount(aCandidate.count());
358 const sal_uInt32 nEdgeCount(aCandidate.isClosed() ? nPointCount : nPointCount - 1L);
359 B2DCubicBezier aEdge;
360 B2DPolygon aRun;
362 for(sal_uInt32 b(0); b < nEdgeCount; b++)
364 aCandidate.getBezierSegment(b, aEdge);
365 const B2DPoint aTestPoint(aEdge.interpolatePoint(0.5));
366 const bool bIsInside(tools::isInside(rClip, aTestPoint) == bInside);
368 if(bIsInside)
370 if(!aRun.count())
372 aRun.append(aEdge.getStartPoint());
375 if(aEdge.isBezier())
377 aRun.appendBezierSegment(aEdge.getControlPointA(), aEdge.getControlPointB(), aEdge.getEndPoint());
379 else
381 aRun.append(aEdge.getEndPoint());
384 else
386 if(aRun.count())
388 aRetval.append(aRun);
389 aRun.clear();
394 if(aRun.count())
396 // try to merge this last and first polygon; they may have been
397 // the former polygon's start/end point
398 if(aRetval.count())
400 const B2DPolygon aStartPolygon(aRetval.getB2DPolygon(0));
402 if(aStartPolygon.count() && aStartPolygon.getB2DPoint(0).equal(aRun.getB2DPoint(aRun.count() - 1)))
404 // append start polygon to aRun, remove from result set
405 aRun.append(aStartPolygon); aRun.removeDoublePoints();
406 aRetval.remove(0);
410 aRetval.append(aRun);
414 else
416 // area clipping
417 B2DPolyPolygon aMergePolyPolygonA(rClip);
419 // First solve all polygon-self and polygon-polygon intersections.
420 // Also get rid of some not-needed polygons (neutral, no area -> when
421 // no intersections, these are tubes).
422 // Now it is possible to correct the orientations in the cut-free
423 // polygons to values corresponding to painting the PolyPolygon with
424 // a XOR-WindingRule.
425 aMergePolyPolygonA = solveCrossovers(aMergePolyPolygonA);
426 aMergePolyPolygonA = stripNeutralPolygons(aMergePolyPolygonA);
427 aMergePolyPolygonA = correctOrientations(aMergePolyPolygonA);
429 if(!bInside)
431 // if we want to get the outside of the clip polygon, make
432 // it a 'Hole' in topological sense
433 aMergePolyPolygonA.flip();
436 B2DPolyPolygon aMergePolyPolygonB(rCandidate);
438 // prepare 2nd source polygon in same way
439 aMergePolyPolygonB = solveCrossovers(aMergePolyPolygonB);
440 aMergePolyPolygonB = stripNeutralPolygons(aMergePolyPolygonB);
441 aMergePolyPolygonB = correctOrientations(aMergePolyPolygonB);
443 // to clip against each other, concatenate and solve all
444 // polygon-polygon crossovers. polygon-self do not need to
445 // be solved again, they were solved in the preparation.
446 aRetval.append(aMergePolyPolygonA);
447 aRetval.append(aMergePolyPolygonB);
448 aRetval = solveCrossovers(aRetval);
450 // now remove neutral polygons (closed, but no area). In a last
451 // step throw away all polygons which have a depth of less than 1
452 // which means there was no logical AND at their position. For the
453 // not-inside solution, the clip was flipped to define it as 'Hole',
454 // so the removal rule is different here; remove all with a depth
455 // of less than 0 (aka holes).
456 aRetval = stripNeutralPolygons(aRetval);
457 aRetval = stripDispensablePolygons(aRetval, bInside);
461 return aRetval;
464 //////////////////////////////////////////////////////////////////////////////
466 B2DPolyPolygon clipPolygonOnPolyPolygon(const B2DPolygon& rCandidate, const B2DPolyPolygon& rClip, bool bInside, bool bStroke)
468 B2DPolyPolygon aRetval;
470 if(rCandidate.count() && rClip.count())
472 aRetval = clipPolyPolygonOnPolyPolygon(B2DPolyPolygon(rCandidate), rClip, bInside, bStroke);
475 return aRetval;
478 //////////////////////////////////////////////////////////////////////////////
481 * let a plane be defined as
483 * v.n+d=0
485 * and a ray be defined as
487 * a+(b-a)*t=0
489 * substitute and rearranging yields
491 * t = -(a.n+d)/(n.(b-a))
493 * if the denominator is zero, the line is either
494 * contained in the plane or parallel to the plane.
495 * in either case, there is no intersection.
496 * if numerator and denominator are both zero, the
497 * ray is contained in the plane.
500 struct scissor_plane {
501 double nx,ny; // plane normal
502 double d; // [-] minimum distance from origin
503 sal_uInt32 clipmask; // clipping mask, e.g. 1000 1000
508 * polygon clipping rules (straight out of Foley and Van Dam)
509 * ===========================================================
510 * current |next |emit
511 * ____________________________________
512 * inside |inside |next
513 * inside |outside |intersect with clip plane
514 * outside |outside |nothing
515 * outside |inside |intersect with clip plane follwed by next
518 sal_uInt32 scissorLineSegment( ::basegfx::B2DPoint *in_vertex, // input buffer
519 sal_uInt32 in_count, // number of verts in input buffer
520 ::basegfx::B2DPoint *out_vertex, // output buffer
521 scissor_plane *pPlane, // scissoring plane
522 const ::basegfx::B2DRectangle &rR ) // clipping rectangle
524 ::basegfx::B2DPoint *curr;
525 ::basegfx::B2DPoint *next;
527 sal_uInt32 out_count=0;
529 // process all the verts
530 for(sal_uInt32 i=0; i<in_count; i++) {
532 // vertices are relative to the coordinate
533 // system defined by the rectangle.
534 curr = &in_vertex[i];
535 next = &in_vertex[(i+1)%in_count];
537 // perform clipping judgement & mask against current plane.
538 sal_uInt32 clip = pPlane->clipmask & ((getCohenSutherlandClipFlags(*curr,rR)<<4)|getCohenSutherlandClipFlags(*next,rR));
540 if(clip==0) { // both verts are inside
541 out_vertex[out_count++] = *next;
543 else if((clip&0x0f) && (clip&0xf0)) { // both verts are outside
545 else if((clip&0x0f) && (clip&0xf0)==0) { // curr is inside, next is outside
547 // direction vector from 'current' to 'next', *not* normalized
548 // to bring 't' into the [0<=x<=1] intervall.
549 ::basegfx::B2DPoint dir((*next)-(*curr));
551 double denominator = ( pPlane->nx*dir.getX() +
552 pPlane->ny*dir.getY() );
553 double numerator = ( pPlane->nx*curr->getX() +
554 pPlane->ny*curr->getY() +
555 pPlane->d );
556 double t = -numerator/denominator;
558 // calculate the actual point of intersection
559 ::basegfx::B2DPoint intersection( curr->getX()+t*dir.getX(),
560 curr->getY()+t*dir.getY() );
562 out_vertex[out_count++] = intersection;
564 else if((clip&0x0f)==0 && (clip&0xf0)) { // curr is outside, next is inside
566 // direction vector from 'current' to 'next', *not* normalized
567 // to bring 't' into the [0<=x<=1] intervall.
568 ::basegfx::B2DPoint dir((*next)-(*curr));
570 double denominator = ( pPlane->nx*dir.getX() +
571 pPlane->ny*dir.getY() );
572 double numerator = ( pPlane->nx*curr->getX() +
573 pPlane->ny*curr->getY() +
574 pPlane->d );
575 double t = -numerator/denominator;
577 // calculate the actual point of intersection
578 ::basegfx::B2DPoint intersection( curr->getX()+t*dir.getX(),
579 curr->getY()+t*dir.getY() );
581 out_vertex[out_count++] = intersection;
582 out_vertex[out_count++] = *next;
586 return out_count;
589 B2DPolygon clipTriangleListOnRange( const B2DPolygon& rCandidate,
590 const B2DRange& rRange )
592 B2DPolygon aResult;
594 if( !(rCandidate.count()%3) )
596 const int scissor_plane_count = 4;
598 scissor_plane sp[scissor_plane_count];
600 sp[0].nx = +1.0;
601 sp[0].ny = +0.0;
602 sp[0].d = -(rRange.getMinX());
603 sp[0].clipmask = (RectClipFlags::LEFT << 4) | RectClipFlags::LEFT; // 0001 0001
604 sp[1].nx = -1.0;
605 sp[1].ny = +0.0;
606 sp[1].d = +(rRange.getMaxX());
607 sp[1].clipmask = (RectClipFlags::RIGHT << 4) | RectClipFlags::RIGHT; // 0010 0010
608 sp[2].nx = +0.0;
609 sp[2].ny = +1.0;
610 sp[2].d = -(rRange.getMinY());
611 sp[2].clipmask = (RectClipFlags::TOP << 4) | RectClipFlags::TOP; // 0100 0100
612 sp[3].nx = +0.0;
613 sp[3].ny = -1.0;
614 sp[3].d = +(rRange.getMaxY());
615 sp[3].clipmask = (RectClipFlags::BOTTOM << 4) | RectClipFlags::BOTTOM; // 1000 1000
617 // retrieve the number of vertices of the triangulated polygon
618 const sal_uInt32 nVertexCount = rCandidate.count();
620 if(nVertexCount)
622 ////////////////////////////////////////////////////////////////////////
623 ////////////////////////////////////////////////////////////////////////
624 ////////////////////////////////////////////////////////////////////////
626 // Upper bound for the maximal number of vertices when intersecting an
627 // axis-aligned rectangle with a triangle in E2
629 // The rectangle and the triangle are in general position, and have 4 and 3
630 // vertices, respectively.
632 // Lemma: Since the rectangle is a convex polygon ( see
633 // http://mathworld.wolfram.com/ConvexPolygon.html for a definition), and
634 // has no holes, it follows that any straight line will intersect the
635 // rectangle's border line at utmost two times (with the usual
636 // tie-breaking rule, if the intersection exactly hits an already existing
637 // rectangle vertex, that this intersection is only attributed to one of
638 // the adjoining edges). Thus, having a rectangle intersected with
639 // a half-plane (one side of a straight line denotes 'inside', the
640 // other 'outside') will at utmost add _one_ vertex to the resulting
641 // intersection polygon (adding two intersection vertices, and removing at
642 // least one rectangle vertex):
644 // *
645 // +--+-----------------+
646 // | * |
647 // |* |
648 // + |
649 // *| |
650 // * | |
651 // +--------------------+
653 // Proof: If the straight line intersects the rectangle two
654 // times, it does so for distinct edges, i.e. the intersection has
655 // minimally one of the rectangle's vertices on either side of the straight
656 // line (but maybe more). Thus, the intersection with a half-plane has
657 // minimally _one_ rectangle vertex removed from the resulting clip
658 // polygon, and therefore, a clip against a half-plane has the net effect
659 // of adding at utmost _one_ vertex to the resulting clip polygon.
661 // Theorem: The intersection of a rectangle and a triangle results in a
662 // polygon with at utmost 7 vertices.
664 // Proof: The inside of the triangle can be described as the consecutive
665 // intersection with three half-planes. Together with the lemma above, this
666 // results in at utmost 3 additional vertices added to the already existing 4
667 // rectangle vertices.
669 // This upper bound is attained with the following example configuration:
671 // *
672 // ***
673 // ** *
674 // ** *
675 // ** *
676 // ** *
677 // ** *
678 // ** *
679 // ** *
680 // ** *
681 // ** *
682 // ----*2--------3 *
683 // | ** |*
684 // 1* 4
685 // **| *|
686 // ** | * |
687 // **| * |
688 // 7* * |
689 // --*6-----5-----
690 // ** *
691 // **
693 // As we need to scissor all triangles against the
694 // output rectangle we employ an output buffer for the
695 // resulting vertices. the question is how large this
696 // buffer needs to be compared to the number of
697 // incoming vertices. this buffer needs to hold at
698 // most the number of original vertices times '7'. see
699 // figure above for an example. scissoring triangles
700 // with the cohen-sutherland line clipping algorithm
701 // as implemented here will result in a triangle fan
702 // which will be rendered as separate triangles to
703 // avoid pipeline stalls for each scissored
704 // triangle. creating separate triangles from a
705 // triangle fan produces (n-2)*3 vertices where n is
706 // the number of vertices of the original triangle
707 // fan. for the maximum number of 7 vertices of
708 // resulting triangle fans we therefore need 15 times
709 // the number of original vertices.
711 ////////////////////////////////////////////////////////////////////////
712 ////////////////////////////////////////////////////////////////////////
713 ////////////////////////////////////////////////////////////////////////
715 //const size_t nBufferSize = sizeof(vertex)*(nVertexCount*16);
716 //vertex *pVertices = (vertex*)alloca(nBufferSize);
717 //sal_uInt32 nNumOutput = 0;
719 // we need to clip this triangle against the output rectangle
720 // to ensure that the resulting texture coordinates are in
721 // the valid range from [0<=st<=1]. under normal circustances
722 // we could use the BORDERCOLOR renderstate but some cards
723 // seem to ignore this feature.
724 ::basegfx::B2DPoint stack[3];
725 unsigned int clipflag = 0;
727 for(sal_uInt32 nIndex=0; nIndex<nVertexCount; ++nIndex)
729 // rotate stack
730 stack[0] = stack[1];
731 stack[1] = stack[2];
732 stack[2] = rCandidate.getB2DPoint(nIndex);
734 // clipping judgement
735 clipflag |= !(rRange.isInside(stack[2]));
737 if(nIndex > 1)
739 // consume vertices until a single separate triangle has been visited.
740 if(!((nIndex+1)%3))
742 // if any of the last three vertices was outside
743 // we need to scissor against the destination rectangle
744 if(clipflag & 7)
746 ::basegfx::B2DPoint buf0[16];
747 ::basegfx::B2DPoint buf1[16];
749 sal_uInt32 vertex_count = 3;
751 // clip against all 4 planes passing the result of
752 // each plane as the input to the next using a double buffer
753 vertex_count = scissorLineSegment(stack,vertex_count,buf1,&sp[0],rRange);
754 vertex_count = scissorLineSegment(buf1,vertex_count,buf0,&sp[1],rRange);
755 vertex_count = scissorLineSegment(buf0,vertex_count,buf1,&sp[2],rRange);
756 vertex_count = scissorLineSegment(buf1,vertex_count,buf0,&sp[3],rRange);
758 if(vertex_count >= 3)
760 // convert triangle fan back to triangle list.
761 ::basegfx::B2DPoint v0(buf0[0]);
762 ::basegfx::B2DPoint v1(buf0[1]);
763 for(sal_uInt32 i=2; i<vertex_count; ++i)
765 ::basegfx::B2DPoint v2(buf0[i]);
766 aResult.append(v0);
767 aResult.append(v1);
768 aResult.append(v2);
769 v1 = v2;
773 else
775 // the last triangle has not been altered, simply copy to result
776 for(sal_uInt32 i=0; i<3; ++i)
777 aResult.append(stack[i]);
782 clipflag <<= 1;
787 return aResult;
790 //////////////////////////////////////////////////////////////////////////////
792 } // end of namespace tools
793 } // end of namespace basegfx
795 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */