summaryrefslogtreecommitdiffstats
path: root/binfilter/bf_sch/source/core/sch_calculat.cxx
blob: 6e28ea74a512e3d2a376f17130dfe470e3d7434e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

// header for Point, Rectangle
// header for Polygon
// header for DBG_ASSERT
// header for XPolygon, XPolyPolygon
#include <bf_svx/xpoly.hxx>
// header for Line
#include <tools/line.hxx>
// for performance measurement
#include <rtl/logfile.hxx>

// Note: Enable the following to skip points in the resulting spline
// poly-polygon, if they have equal x-values rather than identical points.
// Unitl now, I think there are situations where the output might differ, if you
// do so, so it's not enabled by default.

// #define SPLINE_OPTIMIZE_POINTS

#include "calculat.hxx"

#include <algorithm>
#include <functional>
namespace binfilter {

using namespace ::std;


/*N*/ void SchCalculationHelper::IntersectPolygonWithRectangle( const XPolygon& rPolygon, const Rectangle& rRectangle, XPolyPolygon& aResult )
/*N*/ {
/*N*/   RTL_LOGFILE_CONTEXT_AUTHOR( context, "sch", "bm93744", "SchCalculationHelper::IntersectPolygonWithRectangle");
/*N*/
/*N*/   aResult.Clear();
/*N*/
/*N*/   if( rRectangle.IsInside( rPolygon.GetBoundRect() ) )
/*N*/   {
/*N*/       aResult.Insert( rPolygon );
/*N*/         OSL_TRACE( "IntersectPolygonWithRectangle: result has %d polygons", aResult.Count() );
/*N*/       return;
/*N*/   }
/*N*/
/*N*/     Point aFrom;
/*N*/     Point aTo;
/*N*/   USHORT nCount = rPolygon.GetPointCount();
/*N*/
/*N*/     // set last point to a position outside the rectangle, such that the first
/*N*/     // time clip2d returns true, the comparison to last will always yield false
/*N*/     Point aLast (rRectangle.TopLeft());
/*N*/     aLast.Move (-1, -1);
/*N*/     XPolygon aCurrentPoly;
/*N*/     USHORT nIdx = 0;
/*N*/
/*N*/   for (USHORT i=1; i<nCount; i++)
/*N*/   {
/*N*/       aFrom = rPolygon[i-1];
/*N*/       aTo = rPolygon[i];
/*N*/       if (clip2d (aFrom, aTo, rRectangle))
/*N*/       {
/*N*/             // compose an XPolygon of as many consecutive points as possible
/*N*/             if (aFrom == aLast)
/*N*/             {
/*N*/                 if (aTo != aFrom)
/*N*/                     aCurrentPoly.Insert (nIdx++, aTo, XPOLY_NORMAL);
/*N*/             }
/*N*/             else
/*N*/             {
/*N*/                 // create an XPolygon and put it into the XPolyPolygon
/*N*/                 if (aCurrentPoly.GetPointCount() > 0)
/*N*/                     aResult.Insert (aCurrentPoly, XPOLYPOLY_APPEND);
/*N*/
/*N*/                 // start new sequence
/*N*/               aCurrentPoly.SetPointCount (0);
/*N*/                 aCurrentPoly.Insert (0, aFrom, XPOLY_NORMAL);
/*N*/                 nIdx = 1;
/*N*/                 if (aTo != aFrom)
/*N*/                     aCurrentPoly.Insert (nIdx++, aTo, XPOLY_NORMAL);
/*N*/             }
/*N*/
/*N*/             aLast = aTo;
/*N*/         }
/*N*/     }
/*N*/     if (aCurrentPoly.GetPointCount() > 0)
/*N*/         aResult.Insert (aCurrentPoly, XPOLYPOLY_APPEND);
/*N*/
/*N*/     OSL_TRACE( "IntersectPolygonWithRectangle: result has %d polygons", aResult.Count() );
/*N*/ }





/*N*/ BOOL  SchCalculationHelper::clip2d    (Point & rPoint0,
/*N*/                                       Point & rPoint1,
/*N*/                                       const Rectangle & rRectangle)
/*N*/ {
/*N*/   //  Direction vector of the line.
/*N*/   Point   aD = rPoint1 - rPoint0;
/*N*/
/*N*/   if (aD.X()==0 && aD.Y()==0 && rRectangle.IsInside (rPoint0))
/*N*/   {
/*N*/       //  Degenerate case of a zero length line.
/*N*/       return TRUE;
/*N*/   }
/*N*/   else
/*N*/   {
/*N*/       //  Values of the line parameter where the line enters resp. leaves the rectangle.
/*N*/       double  fTE = 0,
/*N*/               fTL = 1;
/*N*/
/*N*/       //  Test wether at least a part lies in the four half-planes with respect to
/*N*/       //  the rectangles four edges.
/*N*/       if (CLIPt (aD.X(), rRectangle.Left() - rPoint0.X(), fTE, fTL))
/*N*/           if (CLIPt (-aD.X(), rPoint0.X() - rRectangle.Right(), fTE, fTL))
/*N*/               if (CLIPt (aD.Y(), rRectangle.Top() - rPoint0.Y(), fTE, fTL))
/*N*/                   if (CLIPt (-aD.Y(), rPoint0.Y() - rRectangle.Bottom(), fTE, fTL))
/*N*/                   {
/*N*/                       //  At least a part is visible.
/*N*/                       if (fTL < 1)
/*N*/                       {
/*N*/                           //  ::com::pute the new end point.
/*N*/                           rPoint1.X() = (long)(rPoint0.X() + fTL * aD.X() + 0.5);
/*N*/                           rPoint1.Y() = (long)(rPoint0.Y() + fTL * aD.Y() + 0.5);
/*N*/                       }
/*N*/                       if (fTE > 0)
/*N*/                       {
/*N*/                           //  ::com::pute the new starting point.
/*N*/                           rPoint0.X() = (long)(rPoint0.X() + fTE * aD.X() + 0.5);
/*N*/                           rPoint0.Y() = (long)(rPoint0.Y() + fTE * aD.Y() + 0.5);
/*N*/                       }
/*N*/                       return TRUE;
/*N*/                   }
/*N*/
/*N*/       //  Line is not visible.
/*N*/       return FALSE;
/*N*/   }
/*N*/ }




/*N*/ BOOL  SchCalculationHelper::CLIPt (double fDenom,
/*N*/                                   double fNum,
/*N*/                                   double & fTE,
/*N*/                                   double & fTL)
/*N*/ {
/*N*/   double  fT;
/*N*/
/*N*/   if (fDenom > 0)             //  Intersection enters: PE
/*N*/   {
/*N*/       fT = fNum / fDenom;     //  Parametric value at the intersection.
/*N*/       if (fT > fTL)           //  fTE and fTL crossover
/*N*/           return FALSE;       //    therefore reject the line.
/*N*/       else if (fT > fTE)      //  A new fTE has been found.
/*N*/           fTE = fT;
/*N*/   }
/*N*/   else if (fDenom < 0)        //  Intersection leaves: PL
/*N*/   {
/*N*/       fT = fNum / fDenom;     //  Parametric Value at the intersection.
/*N*/       if (fT < fTE)           //  fTE and fTL crossover
/*N*/           return FALSE;       //    therefore reject the line.
/*N*/       else if (fT < fTL)      //  A new fTL has been found.
/*N*/           fTL = fT;
/*N*/   }
/*N*/   else if (fNum > 0)
/*N*/       return FALSE;           //  Line lies on the outside of the edge.
/*N*/
/*N*/   return TRUE;
/*N*/ }


// --------------------------------------------------------------------------------

// Calculation of Splines


// ----------------------------------------

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */