summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/2geom/include/2geom/conic_section_clipper_impl.h
blob: ee67df18bfdafd18ea6a527447bc27c003616041 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/** @file
 * @brief Conic section clipping with respect to a rectangle
 *//*
 * Authors:
 *      Marco Cecchetti <mrcekets at gmail>
 *
 * Copyright 2009  authors
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (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.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 */

#ifndef LIB2GEOM_SEEN_CONIC_SECTION_CLIPPER_IMPL_H
#define LIB2GEOM_SEEN_CONIC_SECTION_CLIPPER_IMPL_H


#include <2geom/conicsec.h>
#include <2geom/line.h>

#include <list>
#include <map>



#ifdef CLIP_WITH_CAIRO_SUPPORT
    #include <2geom/toys/path-cairo.h>
    #define CLIPPER_CLASS clipper_cr
#else
    #define CLIPPER_CLASS clipper
#endif

//#define CLIPDBG

#ifdef CLIPDBG
#include <2geom/toys/path-cairo.h>
#define DBGINFO(msg) \
    std::cerr << msg << std::endl;
#define DBGPRINT(msg, var) \
    std::cerr << msg << var << std::endl;
#define DBGPRINTIF(cond, msg, var) \
    if (cond)                                                                  \
        std::cerr << msg << var << std::endl;

#define DBGPRINT2(msg1, var1, msg2, var2) \
    std::cerr << msg1 << var1 << msg2 << var2 << std::endl;

#define DBGPRINTCOLL(msg, coll) \
    if (coll.size() != 0)                                                      \
        std::cerr << msg << ":\n";                                             \
    for (size_t i = 0; i < coll.size(); ++i)                                   \
    {                                                                          \
        std::cerr << i << ": " << coll[i] << "\n";                             \
    }

#else
#define DBGINFO(msg)
#define DBGPRINT(msg, var)
#define DBGPRINTIF(cond, msg, var)
#define DBGPRINT2(msg1, var1, msg2, var2)
#define DBGPRINTCOLL(msg, coll)
#endif




namespace Geom
{

class CLIPPER_CLASS
{

  public:

#ifdef CLIP_WITH_CAIRO_SUPPORT
    clipper_cr (cairo_t* _cr, const xAx & _cs, const Rect & _R)
        : cr(_cr), cs(_cs), R(_R)
    {
        DBGPRINT ("CLIP: right side: ", R.right())
        DBGPRINT ("CLIP: top side: ", R.top())
        DBGPRINT ("CLIP: left side: ", R.left())
        DBGPRINT ("CLIP: bottom side: ", R.bottom())
    }
#else
    clipper (const xAx & _cs, const Rect & _R)
        : cs(_cs), R(_R)
    {
    }
#endif

    bool clip (std::vector<RatQuad> & arcs);

    bool found_any_isolated_point() const
    {
        return ( !single_points.empty() );
    }

    const std::vector<Point> & isolated_points() const
    {
        return single_points;
    }


  private:
    bool intersect (std::vector<Point> & crossing_points) const;

    bool are_paired (Point & M, const Point & P1, const Point & P2) const;
    void pairing (std::vector<Point> & paired_points,
                  std::vector<Point> & inner_points,
                  const std::vector<Point> & crossing_points);

    Point find_inner_point_by_bisector_line (const Point & P,
                                             const Point & Q) const;
    Point find_inner_point (const Point & P, const Point & Q) const;

    std::list<Point>::iterator split (std::list<Point> & points,
                                      std::list<Point>::iterator sp,
                                      std::list<Point>::iterator fp) const;
    void rsplit (std::list<Point> & points,
                 std::list<Point>::iterator sp,
                 std::list<Point>::iterator fp,
                 size_t k) const;

    void rsplit (std::list<Point> & points,
                 std::list<Point>::iterator sp,
                 std::list<Point>::iterator fp,
                 double length) const;

  private:
#ifdef CLIP_WITH_CAIRO_SUPPORT
    cairo_t* cr;
#endif
    const xAx & cs;
    const Rect & R;
    std::vector<Point> single_points;
};




/*
 *  Given two point "P", "Q" on the conic section the method computes
 *  a third point inner to the arc with end-point "P", "Q".
 *  The new point is found by intersecting the conic with the bisector line
 *  of the PQ line segment.
 */
inline
Point CLIPPER_CLASS::find_inner_point_by_bisector_line (const Point & P,
                                                  const Point & Q) const
{
    DBGPRINT ("CLIP: find_inner_point_by_bisector_line: P = ", P)
    DBGPRINT ("CLIP: find_inner_point_by_bisector_line: Q = ", Q)
    Line bl = make_bisector_line (LineSegment (P, Q));
    std::vector<double> rts = cs.roots (bl);
    //DBGPRINT ("CLIP: find_inner_point: rts.size = ", rts.size())
    double t;
    if (rts.size() == 0)
    {
        THROW_LOGICALERROR ("clipper::find_inner_point_by_bisector_line: "
                            "no conic-bisector line intersection point");
    }
    if (rts.size() == 2)
    {
        // we suppose that the searched point is the nearest
        // to the line segment PQ
        t = (std::fabs(rts[0]) < std::fabs(rts[1])) ? rts[0] : rts[1];
    }
    else
    {
        t = rts[0];
    }
    return bl.pointAt (t);
}


/*
 *  Given two point "P", "Q" on the conic section the method computes
 *  a third point inner to the arc with end-point "P", "Q".
 *  The new point is found by intersecting the conic with the line
 *  passing through the middle point of the PQ line segment and
 *  the intersection point of the tangent lines at points P and Q.
 */
inline
Point CLIPPER_CLASS::find_inner_point (const Point & P, const Point & Q) const
{

    Line l1 = cs.tangent (P);
    Line l2 = cs.tangent (Q);
    Line l;
    // in case we fail to find a crossing point we fall back to the bisector
    // method
    try
    {
        OptCrossing oc = intersection(l1, l2);
        if (!oc)
        {
            return find_inner_point_by_bisector_line (P, Q);
        }
        l.setPoints (l1.pointAt (oc->ta), middle_point (P, Q));
    }
    catch (Geom::InfiniteSolutions const &e)
    {
        return find_inner_point_by_bisector_line (P, Q);
    }

    std::vector<double> rts = cs.roots (l);
    double t;
    if (rts.size() == 0)
    {
        return find_inner_point_by_bisector_line (P, Q);
    }
    // the line "l" origin is set to the tangent crossing point so in case
    // we find two intersection points only the nearest belongs to the given arc
    // pay attention: in case we are dealing with an hyperbola (remember that
    // end points are on the same branch, because they are paired) the tangent
    // crossing point belongs to the angle delimited by hyperbola asymptotes
    // and containing the given hyperbola branch, so the previous statement is
    // still true
    if (rts.size() == 2)
    {
        t = (std::fabs(rts[0]) < std::fabs(rts[1])) ? rts[0] : rts[1];
    }
    else
    {
        t = rts[0];
    }
    return l.pointAt (t);
}


/*
 *  Given a list of points on the conic section, and given two consecutive
 *  points belonging to the list and passed by two list iterators, the method
 *  finds a new point that is inner to the conic arc which has the two passed
 *  points as initial and final point. This new point is inserted into the list
 *  between the two passed points and an iterator pointing to the new point
 *  is returned.
 */
inline
std::list<Point>::iterator CLIPPER_CLASS::split (std::list<Point> & points,
                                           std::list<Point>::iterator sp,
                                           std::list<Point>::iterator fp) const
{
    Point new_point = find_inner_point (*sp, *fp);
    std::list<Point>::iterator ip = points.insert (fp, new_point);
    //std::cerr << "CLIP: split: [" << *sp << ", " << *ip << ", "
    //          << *fp << "]" << std::endl;
    return ip;
}


/*
 *  Given a list of points on the conic section, and given two consecutive
 *  points belonging to the list and passed by two list iterators, the method
 *  recursively finds new points that are inner to the conic arc which has
 *  the two passed points as initial and final point. The recursion stop after
 *  "k" recursive calls. These new points are inserted into the list between
 *  the two passed points, and in the order we cross them going from
 *  the initial to the final arc point.
 */
inline
void CLIPPER_CLASS::rsplit (std::list<Point> & points,
                      std::list<Point>::iterator sp,
                      std::list<Point>::iterator fp,
                      size_t k) const
{
    if (k == 0)
    {
        //DBGINFO("CLIP: split: no further split")
        return;
    }

    std::list<Point>::iterator ip = split (points, sp, fp);
    --k;
    rsplit (points, sp, ip, k);
    rsplit (points, ip, fp, k);
}


/*
 *  Given a list of points on the conic section, and given two consecutive
 *  points belonging to the list and passed by two list iterators, the method
 *  recursively finds new points that are inner to the conic arc which has
 *  the two passed points as initial and final point. The recursion stop when
 *  the max distance between the new computed inner point and the two passed
 *  arc end-points is less then the value specified by the "length" parameter.
 *  These new points are inserted into the list between the two passed points,
 *  and in the order we cross them going from the initial to the final arc point.
 */
inline
void CLIPPER_CLASS::rsplit (std::list<Point> & points,
                      std::list<Point>::iterator sp,
                      std::list<Point>::iterator fp,
                      double length) const
{
    std::list<Point>::iterator ip = split (points, sp, fp);
    double d1 = distance (*sp, *ip);
    double d2 = distance (*ip, *fp);
    double mdist = std::max (d1, d2);

    if (mdist < length)
    {
        //DBGINFO("CLIP: split: no further split")
        return;
    }

    // they have to be called both to keep the number of points in the list
    // in the form 2k+1 where k are the sub-arcs the initial arc is split in.
    rsplit (points, sp, ip, length);
    rsplit (points, ip, fp, length);
}


} // end namespace Geom

#endif // LIB2GEOM_SEEN_CONIC_SECTION_CLIPPER_IMPL_H

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :