summaryrefslogtreecommitdiffstats
path: root/src/2geom/geom.cpp
blob: 791e3a681eced64d990d8c32432ca0b508ae7498 (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/**
 *  \brief Various geometrical calculations.
 */

#include <2geom/geom.h>
#include <2geom/point.h>
#include <algorithm>
#include <optional>
#include <2geom/rect.h>

using std::swap;

namespace Geom {

enum IntersectorKind {
    intersects = 0,
    parallel,
    coincident,
    no_intersection
};

/**
 * Finds the intersection of the two (infinite) lines
 * defined by the points p such that dot(n0, p) == d0 and dot(n1, p) == d1.
 *
 * If the two lines intersect, then \a result becomes their point of
 * intersection; otherwise, \a result remains unchanged.
 *
 * This function finds the intersection of the two lines (infinite)
 * defined by n0.X = d0 and x1.X = d1.  The algorithm is as follows:
 * To compute the intersection point use kramer's rule:
 * \verbatim
 * convert lines to form
 * ax + by = c
 * dx + ey = f
 *
 * (
 *  e.g. a = (x2 - x1), b = (y2 - y1), c = (x2 - x1)*x1 + (y2 - y1)*y1
 * )
 *
 * In our case we use:
 *   a = n0.x     d = n1.x
 *   b = n0.y     e = n1.y
 *   c = d0        f = d1
 *
 * so:
 *
 * adx + bdy = cd
 * adx + aey = af
 *
 * bdy - aey = cd - af
 * (bd - ae)y = cd - af
 *
 * y = (cd - af)/(bd - ae)
 *
 * repeat for x and you get:
 *
 * x = (fb - ce)/(bd - ae)                \endverbatim
 *
 * If the denominator (bd-ae) is 0 then the lines are parallel, if the
 * numerators are 0 then the lines coincide.
 *
 * \todo Why not use existing but outcommented code below
 * (HAVE_NEW_INTERSECTOR_CODE)?
 */
IntersectorKind 
line_intersection(Geom::Point const &n0, double const d0,
                  Geom::Point const &n1, double const d1,
                  Geom::Point &result)
{
    double denominator = dot(Geom::rot90(n0), n1);
    double X = n1[Geom::Y] * d0 -
        n0[Geom::Y] * d1;
    /* X = (-d1, d0) dot (n0[Y], n1[Y]) */

    if (denominator == 0) {
        if ( X == 0 ) {
            return coincident;
        } else {
            return parallel;
        }
    }

    double Y = n0[Geom::X] * d1 -
        n1[Geom::X] * d0;

    result = Geom::Point(X, Y) / denominator;

    return intersects;
}



/* ccw exists as a building block */
int
intersector_ccw(const Geom::Point& p0, const Geom::Point& p1,
        const Geom::Point& p2)
/* Determine which way a set of three points winds. */
{
    Geom::Point d1 = p1 - p0;
    Geom::Point d2 = p2 - p0;
    /* compare slopes but avoid division operation */
    double c = dot(Geom::rot90(d1), d2);
    if(c > 0)
        return +1; // ccw - do these match def'n in header?
    if(c < 0)
        return -1; // cw

    /* Colinear [or NaN].  Decide the order. */
    if ( ( d1[0] * d2[0] < 0 )  ||
         ( d1[1] * d2[1] < 0 ) ) {
        return -1; // p2  <  p0 < p1
    } else if ( dot(d1,d1) < dot(d2,d2) ) {
        return +1; // p0 <= p1  <  p2
    } else {
        return 0; // p0 <= p2 <= p1
    }
}

/** Determine whether the line segment from p00 to p01 intersects the
    infinite line passing through p10 and p11. This doesn't find the
    point of intersection, use the line_intersect function above,
    or the segment_intersection interface below.

    \pre neither segment is zero-length; i.e. p00 != p01 and p10 != p11.
*/
bool
line_segment_intersectp(Geom::Point const &p00, Geom::Point const &p01,
                        Geom::Point const &p10, Geom::Point const &p11)
{
    if(p00 == p01) return false;
    if(p10 == p11) return false;

    return ((intersector_ccw(p00, p01, p10) * intersector_ccw(p00, p01, p11)) <= 0 );
}


/** Determine whether two line segments intersect.  This doesn't find
    the point of intersection, use the line_intersect function above,
    or the segment_intersection interface below.

    \pre neither segment is zero-length; i.e. p00 != p01 and p10 != p11.
*/
bool
segment_intersectp(Geom::Point const &p00, Geom::Point const &p01,
                   Geom::Point const &p10, Geom::Point const &p11)
{
    if(p00 == p01) return false;
    if(p10 == p11) return false;

    /* true iff (    (the p1 segment straddles the p0 infinite line)
     *           and (the p0 segment straddles the p1 infinite line) ). */
    return (line_segment_intersectp(p00, p01, p10, p11) &&
            line_segment_intersectp(p10, p11, p00, p01));
}

/** Determine whether \& where a line segments intersects an (infinite) line.

If there is no intersection, then \a result remains unchanged.

\pre neither segment is zero-length; i.e. p00 != p01 and p10 != p11.
**/
IntersectorKind
line_segment_intersect(Geom::Point const &p00, Geom::Point const &p01,
                       Geom::Point const &p10, Geom::Point const &p11,
                       Geom::Point &result)
{
    if(line_segment_intersectp(p00, p01, p10, p11)) {
        Geom::Point n0 = (p01 - p00).ccw();
        double d0 = dot(n0,p00);

        Geom::Point n1 = (p11 - p10).ccw();
        double d1 = dot(n1,p10);
        return line_intersection(n0, d0, n1, d1, result);
    } else {
        return no_intersection;
    }
}


/** Determine whether \& where two line segments intersect.

If the two segments don't intersect, then \a result remains unchanged.

\pre neither segment is zero-length; i.e. p00 != p01 and p10 != p11.
**/
IntersectorKind
segment_intersect(Geom::Point const &p00, Geom::Point const &p01,
                  Geom::Point const &p10, Geom::Point const &p11,
                  Geom::Point &result)
{
    if(segment_intersectp(p00, p01, p10, p11)) {
        Geom::Point n0 = (p01 - p00).ccw();
        double d0 = dot(n0,p00);

        Geom::Point n1 = (p11 - p10).ccw();
        double d1 = dot(n1,p10);
        return line_intersection(n0, d0, n1, d1, result);
    } else {
        return no_intersection;
    }
}

/** Determine whether \& where two line segments intersect.

If the two segments don't intersect, then \a result remains unchanged.

\pre neither segment is zero-length; i.e. p00 != p01 and p10 != p11.
**/
IntersectorKind
line_twopoint_intersect(Geom::Point const &p00, Geom::Point const &p01,
                        Geom::Point const &p10, Geom::Point const &p11,
                        Geom::Point &result)
{
    Geom::Point n0 = (p01 - p00).ccw();
    double d0 = dot(n0,p00);
    
    Geom::Point n1 = (p11 - p10).ccw();
    double d1 = dot(n1,p10);
    return line_intersection(n0, d0, n1, d1, result);
}

// this is used to compare points for std::sort below
static bool
is_less(Point const &A, Point const &B)
{
    if (A[X] < B[X]) {
        return true;
    } else if (A[X] == B[X] && A[Y] < B[Y]) {
        return true;
    } else {
        return false;
    }
}

// TODO: this can doubtlessly be improved
static void
eliminate_duplicates_p(std::vector<Point> &pts)
{
    unsigned int size = pts.size();

    if (size < 2)
        return;

    if (size == 2) {
        if (pts[0] == pts[1]) {
            pts.pop_back();
        }
    } else {
        std::sort(pts.begin(), pts.end(), &is_less);
        if (size == 3) {
            if (pts[0] == pts[1]) {
                pts.erase(pts.begin());
            } else if (pts[1] == pts[2]) {
                pts.pop_back();
            }
        } else {
            // we have size == 4
            if (pts[2] == pts[3]) {
                pts.pop_back();
            }
            if (pts[0] == pts[1]) {
                pts.erase(pts.begin());
            }
        }
    }
}

/** Determine whether \& where an (infinite) line intersects a rectangle.
 *
 * \a c0, \a c1 are diagonal corners of the rectangle and
 * \a p1, \a p1 are distinct points on the line
 *
 * \return A list (possibly empty) of points of intersection.  If two such points (say \a r0 and \a
 * r1) then it is guaranteed that the order of \a r0, \a r1 along the line is the same as the that
 * of \a c0, \a c1 (i.e., the vectors \a r1 - \a r0 and \a p1 - \a p0 point into the same
 * direction).
 */
std::vector<Geom::Point>
rect_line_intersect(Geom::Point const &c0, Geom::Point const &c1,
                    Geom::Point const &p0, Geom::Point const &p1)
{
    using namespace Geom;

    std::vector<Point> results;

    Point A(c0);
    Point C(c1);

    Point B(A[X], C[Y]);
    Point D(C[X], A[Y]);

    Point res;

    if (line_segment_intersect(p0, p1, A, B, res) == intersects) {
        results.push_back(res);
    }
    if (line_segment_intersect(p0, p1, B, C, res) == intersects) {
        results.push_back(res);
    }
    if (line_segment_intersect(p0, p1, C, D, res) == intersects) {
        results.push_back(res);
    }
    if (line_segment_intersect(p0, p1, D, A, res) == intersects) {
        results.push_back(res);
    }

    eliminate_duplicates_p(results);

    if (results.size() == 2) {
        // sort the results so that the order is the same as that of p0 and p1
        Point dir1 (results[1] - results[0]);
        Point dir2 (p1 - p0);
        if (dot(dir1, dir2) < 0) {
            swap(results[0], results[1]);
        }
    }

    return results;
}

/** Determine whether \& where an (infinite) line intersects a rectangle.
 *
 * \a c0, \a c1 are diagonal corners of the rectangle and
 * \a p1, \a p1 are distinct points on the line
 *
 * \return A list (possibly empty) of points of intersection.  If two such points (say \a r0 and \a
 * r1) then it is guaranteed that the order of \a r0, \a r1 along the line is the same as the that
 * of \a c0, \a c1 (i.e., the vectors \a r1 - \a r0 and \a p1 - \a p0 point into the same
 * direction).
 */
std::optional<LineSegment>
rect_line_intersect(Geom::Rect &r,
                    Geom::LineSegment ls)
{
    std::vector<Point> results;
    
    results =  rect_line_intersect(r.min(), r.max(), ls[0], ls[1]);
    if(results.size() == 2) {
        return LineSegment(results[0], results[1]);
    } 
    return std::optional<LineSegment>();
}

std::optional<LineSegment>
rect_line_intersect(Geom::Rect &r,
                    Geom::Line l)
{
    return rect_line_intersect(r, l.segment(0, 1));
}

/**
 * polyCentroid: Calculates the centroid (xCentroid, yCentroid) and area of a polygon, given its
 * vertices (x[0], y[0]) ... (x[n-1], y[n-1]). It is assumed that the contour is closed, i.e., that
 * the vertex following (x[n-1], y[n-1]) is (x[0], y[0]).  The algebraic sign of the area is
 * positive for counterclockwise ordering of vertices in x-y plane; otherwise negative.

 * Returned values: 
    0 for normal execution; 
    1 if the polygon is degenerate (number of vertices < 3);
    2 if area = 0 (and the centroid is undefined).

    * for now we require the path to be a polyline and assume it is closed.
**/

int centroid(std::vector<Geom::Point> const &p, Geom::Point& centroid, double &area) {
    const unsigned n = p.size();
    if (n < 3)
        return 1;
    Geom::Point centroid_tmp(0,0);
    double atmp = 0;
    for (unsigned i = n-1, j = 0; j < n; i = j, j++) {
        const double ai = cross(p[j], p[i]);
        atmp += ai;
        centroid_tmp += (p[j] + p[i])*ai; // first moment.
    }
    area = atmp / 2;
    if (atmp != 0) {
        centroid = centroid_tmp / (3 * atmp);
        return 0;
    }
    return 2;
}

}

/*
  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 :