update emoji autocorrect entries from po-files
[LibreOffice.git] / basegfx / source / polygon / b2dpolygonclipper.cxx
blob73eb7851ce5c4b7898d2a98b50bc0025f894813b
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 namespace basegfx
34 namespace tools
36 B2DPolyPolygon clipPolygonOnParallelAxis(const B2DPolygon& rCandidate, bool bParallelToXAxis, bool bAboveAxis, double fValueOnOtherAxis, bool bStroke)
38 B2DPolyPolygon aRetval;
40 if(rCandidate.count())
42 const B2DRange aCandidateRange(getRange(rCandidate));
44 if(bParallelToXAxis && fTools::moreOrEqual(aCandidateRange.getMinY(), fValueOnOtherAxis))
46 // completely above and on the clip line. also true for curves.
47 if(bAboveAxis)
49 // add completely
50 aRetval.append(rCandidate);
53 else if(bParallelToXAxis && fTools::lessOrEqual(aCandidateRange.getMaxY(), fValueOnOtherAxis))
55 // completely below and on the clip line. also true for curves.
56 if(!bAboveAxis)
58 // add completely
59 aRetval.append(rCandidate);
62 else if(!bParallelToXAxis && fTools::moreOrEqual(aCandidateRange.getMinX(), fValueOnOtherAxis))
64 // completely right of and on the clip line. also true for curves.
65 if(bAboveAxis)
67 // add completely
68 aRetval.append(rCandidate);
71 else if(!bParallelToXAxis && fTools::lessOrEqual(aCandidateRange.getMaxX(), fValueOnOtherAxis))
73 // completely left of and on the clip line. also true for curves.
74 if(!bAboveAxis)
76 // add completely
77 aRetval.append(rCandidate);
80 else
82 // add cuts with axis to polygon, including bezier segments
83 // Build edge to cut with. Make it a little big longer than needed for
84 // numerical stability. We want to cut against the edge seen as endless
85 // ray here, but addPointsAtCuts() will limit itself to the
86 // edge's range ]0.0 .. 1.0[.
87 const double fSmallExtension((aCandidateRange.getWidth() + aCandidateRange.getHeight()) * (0.5 * 0.1));
88 const B2DPoint aStart(
89 bParallelToXAxis ? aCandidateRange.getMinX() - fSmallExtension : fValueOnOtherAxis,
90 bParallelToXAxis ? fValueOnOtherAxis : aCandidateRange.getMinY() - fSmallExtension);
91 const B2DPoint aEnd(
92 bParallelToXAxis ? aCandidateRange.getMaxX() + fSmallExtension : fValueOnOtherAxis,
93 bParallelToXAxis ? fValueOnOtherAxis : aCandidateRange.getMaxY() + fSmallExtension);
94 const B2DPolygon aCandidate(addPointsAtCuts(rCandidate, aStart, aEnd));
95 const sal_uInt32 nPointCount(aCandidate.count());
96 const sal_uInt32 nEdgeCount(aCandidate.isClosed() ? nPointCount : nPointCount - 1L);
97 B2DCubicBezier aEdge;
98 B2DPolygon aRun;
100 for(sal_uInt32 a(0L); a < nEdgeCount; a++)
102 aCandidate.getBezierSegment(a, aEdge);
103 const B2DPoint aTestPoint(aEdge.interpolatePoint(0.5));
104 const bool bInside(bParallelToXAxis ?
105 fTools::moreOrEqual(aTestPoint.getY(), fValueOnOtherAxis) == bAboveAxis :
106 fTools::moreOrEqual(aTestPoint.getX(), fValueOnOtherAxis) == bAboveAxis);
108 if(bInside)
110 if(!aRun.count() || !aRun.getB2DPoint(aRun.count() - 1).equal(aEdge.getStartPoint()))
112 aRun.append(aEdge.getStartPoint());
115 if(aEdge.isBezier())
117 aRun.appendBezierSegment(aEdge.getControlPointA(), aEdge.getControlPointB(), aEdge.getEndPoint());
119 else
121 aRun.append(aEdge.getEndPoint());
124 else
126 if(bStroke && aRun.count())
128 aRetval.append(aRun);
129 aRun.clear();
134 if(aRun.count())
136 if(bStroke)
138 // try to merge this last and first polygon; they may have been
139 // the former polygon's start/end point
140 if(aRetval.count())
142 const B2DPolygon aStartPolygon(aRetval.getB2DPolygon(0));
144 if(aStartPolygon.count() && aStartPolygon.getB2DPoint(0).equal(aRun.getB2DPoint(aRun.count() - 1)))
146 // append start polygon to aRun, remove from result set
147 aRun.append(aStartPolygon); aRun.removeDoublePoints();
148 aRetval.remove(0);
152 aRetval.append(aRun);
154 else
156 // set closed flag and correct last point (which is added double now).
157 closeWithGeometryChange(aRun);
158 aRetval.append(aRun);
164 return aRetval;
167 B2DPolyPolygon clipPolyPolygonOnParallelAxis(const B2DPolyPolygon& rCandidate, bool bParallelToXAxis, bool bAboveAxis, double fValueOnOtherAxis, bool bStroke)
169 const sal_uInt32 nPolygonCount(rCandidate.count());
170 B2DPolyPolygon aRetval;
172 for(sal_uInt32 a(0L); a < nPolygonCount; a++)
174 const B2DPolyPolygon aClippedPolyPolygon(clipPolygonOnParallelAxis(rCandidate.getB2DPolygon(a), bParallelToXAxis, bAboveAxis, fValueOnOtherAxis, bStroke));
176 if(aClippedPolyPolygon.count())
178 aRetval.append(aClippedPolyPolygon);
182 return aRetval;
185 B2DPolyPolygon clipPolygonOnRange(const B2DPolygon& rCandidate, const B2DRange& rRange, bool bInside, bool bStroke)
187 const sal_uInt32 nCount(rCandidate.count());
188 B2DPolyPolygon aRetval;
190 if(!nCount)
192 // source is empty
193 return aRetval;
196 if(rRange.isEmpty())
198 if(bInside)
200 // nothing is inside an empty range
201 return aRetval;
203 else
205 // everything is outside an empty range
206 return B2DPolyPolygon(rCandidate);
210 const B2DRange aCandidateRange(getRange(rCandidate));
212 if(rRange.isInside(aCandidateRange))
214 // candidate is completely inside given range
215 if(bInside)
217 // nothing to do
218 return B2DPolyPolygon(rCandidate);
220 else
222 // nothing is outside, then
223 return aRetval;
227 if(!bInside)
229 // cutting off the outer parts of filled polygons at parallell
230 // lines to the axes is only possible for the inner part, not for
231 // the outer part which means cutting a hole into the original polygon.
232 // This is because the inner part is a logical AND-operation of
233 // the four implied half-planes, but the outer part is not.
234 // It is possible for strokes, but with creating unnecessary extra
235 // cuts, so using clipPolygonOnPolyPolygon is better there, too.
236 // This needs to be done with the topology knowlegde and is unfurtunately
237 // more expensive, too.
238 const B2DPolygon aClip(createPolygonFromRect(rRange));
240 return clipPolygonOnPolyPolygon(rCandidate, B2DPolyPolygon(aClip), bInside, bStroke);
243 // clip against the four axes of the range
244 // against X-Axis, lower value
245 aRetval = clipPolygonOnParallelAxis(rCandidate, true, bInside, rRange.getMinY(), bStroke);
247 if(aRetval.count())
249 // against Y-Axis, lower value
250 if(1L == aRetval.count())
252 aRetval = clipPolygonOnParallelAxis(aRetval.getB2DPolygon(0L), false, bInside, rRange.getMinX(), bStroke);
254 else
256 aRetval = clipPolyPolygonOnParallelAxis(aRetval, false, bInside, rRange.getMinX(), bStroke);
259 if(aRetval.count())
261 // against X-Axis, higher value
262 if(1L == aRetval.count())
264 aRetval = clipPolygonOnParallelAxis(aRetval.getB2DPolygon(0L), true, !bInside, rRange.getMaxY(), bStroke);
266 else
268 aRetval = clipPolyPolygonOnParallelAxis(aRetval, true, !bInside, rRange.getMaxY(), bStroke);
271 if(aRetval.count())
273 // against Y-Axis, higher value
274 if(1L == aRetval.count())
276 aRetval = clipPolygonOnParallelAxis(aRetval.getB2DPolygon(0L), false, !bInside, rRange.getMaxX(), bStroke);
278 else
280 aRetval = clipPolyPolygonOnParallelAxis(aRetval, false, !bInside, rRange.getMaxX(), bStroke);
286 return aRetval;
289 B2DPolyPolygon clipPolyPolygonOnRange(const B2DPolyPolygon& rCandidate, const B2DRange& rRange, bool bInside, bool bStroke)
291 const sal_uInt32 nPolygonCount(rCandidate.count());
292 B2DPolyPolygon aRetval;
294 if(!nPolygonCount)
296 // source is empty
297 return aRetval;
300 if(rRange.isEmpty())
302 if(bInside)
304 // nothing is inside an empty range
305 return aRetval;
307 else
309 // everything is outside an empty range
310 return rCandidate;
314 if(bInside)
316 for(sal_uInt32 a(0L); a < nPolygonCount; a++)
318 const B2DPolyPolygon aClippedPolyPolygon(clipPolygonOnRange(rCandidate.getB2DPolygon(a), rRange, bInside, bStroke));
320 if(aClippedPolyPolygon.count())
322 aRetval.append(aClippedPolyPolygon);
326 else
328 // for details, see comment in clipPolygonOnRange for the "cutting off
329 // the outer parts of filled polygons at parallell lines" explanations
330 const B2DPolygon aClip(createPolygonFromRect(rRange));
332 return clipPolyPolygonOnPolyPolygon(rCandidate, B2DPolyPolygon(aClip), bInside, bStroke);
335 return aRetval;
338 B2DPolyPolygon clipPolyPolygonOnPolyPolygon(const B2DPolyPolygon& rCandidate, const B2DPolyPolygon& rClip, bool bInside, bool bStroke)
340 B2DPolyPolygon aRetval;
342 if(rCandidate.count() && rClip.count())
344 // one or both are no rectangle - go the hard way and clip PolyPolygon
345 // against PolyPolygon...
346 if(bStroke)
348 // line clipping, create line snippets by first adding all cut points and
349 // then marching along the edges and detecting if they are inside or outside
350 // the clip polygon
351 for(sal_uInt32 a(0); a < rCandidate.count(); a++)
353 // add cuts with clip to polygon, including bezier segments
354 const B2DPolygon aCandidate(addPointsAtCuts(rCandidate.getB2DPolygon(a), rClip));
355 const sal_uInt32 nPointCount(aCandidate.count());
356 const sal_uInt32 nEdgeCount(aCandidate.isClosed() ? nPointCount : nPointCount - 1L);
357 B2DCubicBezier aEdge;
358 B2DPolygon aRun;
360 for(sal_uInt32 b(0); b < nEdgeCount; b++)
362 aCandidate.getBezierSegment(b, aEdge);
363 const B2DPoint aTestPoint(aEdge.interpolatePoint(0.5));
364 const bool bIsInside(tools::isInside(rClip, aTestPoint) == bInside);
366 if(bIsInside)
368 if(!aRun.count())
370 aRun.append(aEdge.getStartPoint());
373 if(aEdge.isBezier())
375 aRun.appendBezierSegment(aEdge.getControlPointA(), aEdge.getControlPointB(), aEdge.getEndPoint());
377 else
379 aRun.append(aEdge.getEndPoint());
382 else
384 if(aRun.count())
386 aRetval.append(aRun);
387 aRun.clear();
392 if(aRun.count())
394 // try to merge this last and first polygon; they may have been
395 // the former polygon's start/end point
396 if(aRetval.count())
398 const B2DPolygon aStartPolygon(aRetval.getB2DPolygon(0));
400 if(aStartPolygon.count() && aStartPolygon.getB2DPoint(0).equal(aRun.getB2DPoint(aRun.count() - 1)))
402 // append start polygon to aRun, remove from result set
403 aRun.append(aStartPolygon); aRun.removeDoublePoints();
404 aRetval.remove(0);
408 aRetval.append(aRun);
412 else
414 // check for simplification with ranges if !bStroke (handling as stroke is more simple),
415 // but also only when bInside, else the simplification may lead to recursive calls (see
416 // calls to clipPolyPolygonOnPolyPolygon in clipPolyPolygonOnRange and clipPolygonOnRange)
417 if(bInside)
419 // #i125349# detect if both given PolyPolygons are indeed ranges
420 bool bBothRectangle(false);
422 if(basegfx::tools::isRectangle(rCandidate))
424 if(basegfx::tools::isRectangle(rClip))
426 // both are ranges
427 bBothRectangle = true;
429 else
431 // rCandidate is rectangle -> clip rClip on rRectangle, use the much
432 // cheaper and numerically more stable clipping against a range
433 // This simplification (exchanging content and clip) is valid
434 // since we do a logical AND operation
435 return clipPolyPolygonOnRange(rClip, rCandidate.getB2DRange(), bInside, bStroke);
438 else if(basegfx::tools::isRectangle(rClip))
440 if(basegfx::tools::isRectangle(rCandidate))
442 // both are ranges
443 bBothRectangle = true;
445 else
447 // rClip is rectangle -> clip rCandidate on rRectangle, use the much
448 // cheaper and numerically more stable clipping against a range
449 return clipPolyPolygonOnRange(rCandidate, rClip.getB2DRange(), bInside, bStroke);
453 if(bBothRectangle)
455 // both are rectangle
456 if(rCandidate.getB2DRange().equal(rClip.getB2DRange()))
458 // if both are equal -> no change
459 return rCandidate;
461 else
463 // not equal -> create new intersection from both ranges,
464 // but much cheaper based on the ranges
465 basegfx::B2DRange aIntersectionRange(rCandidate.getB2DRange());
467 aIntersectionRange.intersect(rClip.getB2DRange());
469 if(aIntersectionRange.isEmpty())
471 // no common IntersectionRange -> the clip will be empty
472 return B2DPolyPolygon();
474 else
476 // use common aIntersectionRange as result, convert
477 // to expected tools::PolyPolygon form
478 return basegfx::B2DPolyPolygon(
479 basegfx::tools::createPolygonFromRect(aIntersectionRange));
485 // area clipping
486 B2DPolyPolygon aMergePolyPolygonA(rClip);
488 // First solve all polygon-self and polygon-polygon intersections.
489 // Also get rid of some not-needed polygons (neutral, no area -> when
490 // no intersections, these are tubes).
491 // Now it is possible to correct the orientations in the cut-free
492 // polygons to values corresponding to painting the tools::PolyPolygon with
493 // a XOR-WindingRule.
494 aMergePolyPolygonA = solveCrossovers(aMergePolyPolygonA);
495 aMergePolyPolygonA = stripNeutralPolygons(aMergePolyPolygonA);
496 aMergePolyPolygonA = correctOrientations(aMergePolyPolygonA);
498 if(!bInside)
500 // if we want to get the outside of the clip polygon, make
501 // it a 'Hole' in topological sense
502 aMergePolyPolygonA.flip();
505 B2DPolyPolygon aMergePolyPolygonB(rCandidate);
507 // prepare 2nd source polygon in same way
508 aMergePolyPolygonB = solveCrossovers(aMergePolyPolygonB);
509 aMergePolyPolygonB = stripNeutralPolygons(aMergePolyPolygonB);
510 aMergePolyPolygonB = correctOrientations(aMergePolyPolygonB);
512 // to clip against each other, concatenate and solve all
513 // polygon-polygon crossovers. polygon-self do not need to
514 // be solved again, they were solved in the preparation.
515 aRetval.append(aMergePolyPolygonA);
516 aRetval.append(aMergePolyPolygonB);
517 aRetval = solveCrossovers(aRetval);
519 // now remove neutral polygons (closed, but no area). In a last
520 // step throw away all polygons which have a depth of less than 1
521 // which means there was no logical AND at their position. For the
522 // not-inside solution, the clip was flipped to define it as 'Hole',
523 // so the removal rule is different here; remove all with a depth
524 // of less than 0 (aka holes).
525 aRetval = stripNeutralPolygons(aRetval);
526 aRetval = stripDispensablePolygons(aRetval, bInside);
530 return aRetval;
533 B2DPolyPolygon clipPolygonOnPolyPolygon(const B2DPolygon& rCandidate, const B2DPolyPolygon& rClip, bool bInside, bool bStroke)
535 B2DPolyPolygon aRetval;
537 if(rCandidate.count() && rClip.count())
539 aRetval = clipPolyPolygonOnPolyPolygon(B2DPolyPolygon(rCandidate), rClip, bInside, bStroke);
542 return aRetval;
546 * let a plane be defined as
548 * v.n+d=0
550 * and a ray be defined as
552 * a+(b-a)*t=0
554 * substitute and rearranging yields
556 * t = -(a.n+d)/(n.(b-a))
558 * if the denominator is zero, the line is either
559 * contained in the plane or parallel to the plane.
560 * in either case, there is no intersection.
561 * if numerator and denominator are both zero, the
562 * ray is contained in the plane.
565 struct scissor_plane {
566 double nx,ny; // plane normal
567 double d; // [-] minimum distance from origin
568 sal_uInt32 clipmask; // clipping mask, e.g. 1000 1000
573 * polygon clipping rules (straight out of Foley and Van Dam)
574 * ===========================================================
575 * current |next |emit
576 * ____________________________________
577 * inside |inside |next
578 * inside |outside |intersect with clip plane
579 * outside |outside |nothing
580 * outside |inside |intersect with clip plane follwed by next
583 sal_uInt32 scissorLineSegment( ::basegfx::B2DPoint *in_vertex, // input buffer
584 sal_uInt32 in_count, // number of verts in input buffer
585 ::basegfx::B2DPoint *out_vertex, // output buffer
586 scissor_plane *pPlane, // scissoring plane
587 const ::basegfx::B2DRectangle &rR ) // clipping rectangle
590 sal_uInt32 out_count=0;
592 // process all the verts
593 for(sal_uInt32 i=0; i<in_count; i++) {
595 // vertices are relative to the coordinate
596 // system defined by the rectangle.
597 ::basegfx::B2DPoint *curr = &in_vertex[i];
598 ::basegfx::B2DPoint *next = &in_vertex[(i+1)%in_count];
600 // perform clipping judgement & mask against current plane.
601 sal_uInt32 clip = pPlane->clipmask & ((getCohenSutherlandClipFlags(*curr,rR)<<4)|getCohenSutherlandClipFlags(*next,rR));
603 if(clip==0) { // both verts are inside
604 out_vertex[out_count++] = *next;
606 else if((clip&0x0f) && (clip&0xf0)) { // both verts are outside
608 else if((clip&0x0f) && (clip&0xf0)==0) { // curr is inside, next is outside
610 // direction vector from 'current' to 'next', *not* normalized
611 // to bring 't' into the [0<=x<=1] intervall.
612 ::basegfx::B2DPoint dir((*next)-(*curr));
614 double denominator = ( pPlane->nx*dir.getX() +
615 pPlane->ny*dir.getY() );
616 double numerator = ( pPlane->nx*curr->getX() +
617 pPlane->ny*curr->getY() +
618 pPlane->d );
619 double t = -numerator/denominator;
621 // calculate the actual point of intersection
622 ::basegfx::B2DPoint intersection( curr->getX()+t*dir.getX(),
623 curr->getY()+t*dir.getY() );
625 out_vertex[out_count++] = intersection;
627 else if((clip&0x0f)==0 && (clip&0xf0)) { // curr is outside, next is inside
629 // direction vector from 'current' to 'next', *not* normalized
630 // to bring 't' into the [0<=x<=1] intervall.
631 ::basegfx::B2DPoint dir((*next)-(*curr));
633 double denominator = ( pPlane->nx*dir.getX() +
634 pPlane->ny*dir.getY() );
635 double numerator = ( pPlane->nx*curr->getX() +
636 pPlane->ny*curr->getY() +
637 pPlane->d );
638 double t = -numerator/denominator;
640 // calculate the actual point of intersection
641 ::basegfx::B2DPoint intersection( curr->getX()+t*dir.getX(),
642 curr->getY()+t*dir.getY() );
644 out_vertex[out_count++] = intersection;
645 out_vertex[out_count++] = *next;
649 return out_count;
652 B2DPolygon clipTriangleListOnRange( const B2DPolygon& rCandidate,
653 const B2DRange& rRange )
655 B2DPolygon aResult;
657 if( !(rCandidate.count()%3) )
659 const int scissor_plane_count = 4;
661 scissor_plane sp[scissor_plane_count];
663 sp[0].nx = +1.0;
664 sp[0].ny = +0.0;
665 sp[0].d = -(rRange.getMinX());
666 sp[0].clipmask = (RectClipFlags::LEFT << 4) | RectClipFlags::LEFT; // 0001 0001
667 sp[1].nx = -1.0;
668 sp[1].ny = +0.0;
669 sp[1].d = +(rRange.getMaxX());
670 sp[1].clipmask = (RectClipFlags::RIGHT << 4) | RectClipFlags::RIGHT; // 0010 0010
671 sp[2].nx = +0.0;
672 sp[2].ny = +1.0;
673 sp[2].d = -(rRange.getMinY());
674 sp[2].clipmask = (RectClipFlags::TOP << 4) | RectClipFlags::TOP; // 0100 0100
675 sp[3].nx = +0.0;
676 sp[3].ny = -1.0;
677 sp[3].d = +(rRange.getMaxY());
678 sp[3].clipmask = (RectClipFlags::BOTTOM << 4) | RectClipFlags::BOTTOM; // 1000 1000
680 // retrieve the number of vertices of the triangulated polygon
681 const sal_uInt32 nVertexCount = rCandidate.count();
683 if(nVertexCount)
685 // Upper bound for the maximal number of vertices when intersecting an
686 // axis-aligned rectangle with a triangle in E2
688 // The rectangle and the triangle are in general position, and have 4 and 3
689 // vertices, respectively.
691 // Lemma: Since the rectangle is a convex polygon ( see
692 // http://mathworld.wolfram.com/ConvexPolygon.html for a definition), and
693 // has no holes, it follows that any straight line will intersect the
694 // rectangle's border line at utmost two times (with the usual
695 // tie-breaking rule, if the intersection exactly hits an already existing
696 // rectangle vertex, that this intersection is only attributed to one of
697 // the adjoining edges). Thus, having a rectangle intersected with
698 // a half-plane (one side of a straight line denotes 'inside', the
699 // other 'outside') will at utmost add _one_ vertex to the resulting
700 // intersection polygon (adding two intersection vertices, and removing at
701 // least one rectangle vertex):
703 // *
704 // +--+-----------------+
705 // | * |
706 // |* |
707 // + |
708 // *| |
709 // * | |
710 // +--------------------+
712 // Proof: If the straight line intersects the rectangle two
713 // times, it does so for distinct edges, i.e. the intersection has
714 // minimally one of the rectangle's vertices on either side of the straight
715 // line (but maybe more). Thus, the intersection with a half-plane has
716 // minimally _one_ rectangle vertex removed from the resulting clip
717 // polygon, and therefore, a clip against a half-plane has the net effect
718 // of adding at utmost _one_ vertex to the resulting clip polygon.
720 // Theorem: The intersection of a rectangle and a triangle results in a
721 // polygon with at utmost 7 vertices.
723 // Proof: The inside of the triangle can be described as the consecutive
724 // intersection with three half-planes. Together with the lemma above, this
725 // results in at utmost 3 additional vertices added to the already existing 4
726 // rectangle vertices.
728 // This upper bound is attained with the following example configuration:
730 // *
731 // ***
732 // ** *
733 // ** *
734 // ** *
735 // ** *
736 // ** *
737 // ** *
738 // ** *
739 // ** *
740 // ** *
741 // ----*2--------3 *
742 // | ** |*
743 // 1* 4
744 // **| *|
745 // ** | * |
746 // **| * |
747 // 7* * |
748 // --*6-----5-----
749 // ** *
750 // **
752 // As we need to scissor all triangles against the
753 // output rectangle we employ an output buffer for the
754 // resulting vertices. the question is how large this
755 // buffer needs to be compared to the number of
756 // incoming vertices. this buffer needs to hold at
757 // most the number of original vertices times '7'. see
758 // figure above for an example. scissoring triangles
759 // with the cohen-sutherland line clipping algorithm
760 // as implemented here will result in a triangle fan
761 // which will be rendered as separate triangles to
762 // avoid pipeline stalls for each scissored
763 // triangle. creating separate triangles from a
764 // triangle fan produces (n-2)*3 vertices where n is
765 // the number of vertices of the original triangle
766 // fan. for the maximum number of 7 vertices of
767 // resulting triangle fans we therefore need 15 times
768 // the number of original vertices.
770 //const size_t nBufferSize = sizeof(vertex)*(nVertexCount*16);
771 //vertex *pVertices = (vertex*)alloca(nBufferSize);
772 //sal_uInt32 nNumOutput = 0;
774 // we need to clip this triangle against the output rectangle
775 // to ensure that the resulting texture coordinates are in
776 // the valid range from [0<=st<=1]. under normal circustances
777 // we could use the BORDERCOLOR renderstate but some cards
778 // seem to ignore this feature.
779 ::basegfx::B2DPoint stack[3];
780 unsigned int clipflag = 0;
782 for(sal_uInt32 nIndex=0; nIndex<nVertexCount; ++nIndex)
784 // rotate stack
785 stack[0] = stack[1];
786 stack[1] = stack[2];
787 stack[2] = rCandidate.getB2DPoint(nIndex);
789 // clipping judgement
790 clipflag |= unsigned(!(rRange.isInside(stack[2])));
792 if(nIndex > 1)
794 // consume vertices until a single separate triangle has been visited.
795 if(!((nIndex+1)%3))
797 // if any of the last three vertices was outside
798 // we need to scissor against the destination rectangle
799 if(clipflag & 7)
801 ::basegfx::B2DPoint buf0[16];
802 ::basegfx::B2DPoint buf1[16];
804 sal_uInt32 vertex_count = 3;
806 // clip against all 4 planes passing the result of
807 // each plane as the input to the next using a double buffer
808 vertex_count = scissorLineSegment(stack,vertex_count,buf1,&sp[0],rRange);
809 vertex_count = scissorLineSegment(buf1,vertex_count,buf0,&sp[1],rRange);
810 vertex_count = scissorLineSegment(buf0,vertex_count,buf1,&sp[2],rRange);
811 vertex_count = scissorLineSegment(buf1,vertex_count,buf0,&sp[3],rRange);
813 if(vertex_count >= 3)
815 // convert triangle fan back to triangle list.
816 ::basegfx::B2DPoint v0(buf0[0]);
817 ::basegfx::B2DPoint v1(buf0[1]);
818 for(sal_uInt32 i=2; i<vertex_count; ++i)
820 ::basegfx::B2DPoint v2(buf0[i]);
821 aResult.append(v0);
822 aResult.append(v1);
823 aResult.append(v2);
824 v1 = v2;
828 else
830 // the last triangle has not been altered, simply copy to result
831 for(sal_uInt32 i=0; i<3; ++i)
832 aResult.append(stack[i]);
837 clipflag <<= 1;
842 return aResult;
845 } // end of namespace tools
846 } // end of namespace basegfx
848 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */