Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
geometry.h
Go to the documentation of this file.
1/*
2 * vim: ts=4 sw=4 et tw=0 wm=0
3 *
4 * libavoid - Fast, Incremental, Object-avoiding Line Router
5 *
6 * Copyright (C) 2004-2011 Monash University
7 *
8 * --------------------------------------------------------------------
9 * Much of the code in this module is based on code published with
10 * and/or described in "Computational Geometry in C" (Second Edition),
11 * Copyright (C) 1998 Joseph O'Rourke <orourke@cs.smith.edu>
12 * --------------------------------------------------------------------
13 * The segmentIntersectPoint function is based on code published and
14 * described in Franklin Antonio, Faster Line Segment Intersection,
15 * Graphics Gems III, p. 199-202, code: p. 500-501.
16 * --------------------------------------------------------------------
17 *
18 * This library is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Lesser General Public
20 * License as published by the Free Software Foundation; either
21 * version 2.1 of the License, or (at your option) any later version.
22 * See the file LICENSE.LGPL distributed with the library.
23 *
24 * Licensees holding a valid commercial license may use this file in
25 * accordance with the commercial license agreement provided with the
26 * library.
27 *
28 * This library is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31 *
32 * Author(s): Michael Wybrow
33*/
34
35
36#ifndef AVOID_GEOMETRY_H
37#define AVOID_GEOMETRY_H
38
39#include "libavoid/geomtypes.h"
40#include "libavoid/assertions.h"
41
42namespace Avoid {
43
44
45extern double euclideanDist(const Point& a, const Point& b);
46extern double manhattanDist(const Point& a, const Point& b);
47extern double totalLength(const Polygon& poly);
48extern double angle(const Point& a, const Point& b, const Point& c);
49extern bool segmentIntersect(const Point& a, const Point& b,
50 const Point& c, const Point& d);
51extern bool segmentShapeIntersect(const Point& e1, const Point& e2,
52 const Point& s1, const Point& s2, bool& seenIntersectionAtEndpoint);
53extern bool inPoly(const Polygon& poly, const Point& q, bool countBorder = true);
54extern bool inPolyGen(const PolygonInterface& poly, const Point& q);
55extern bool inValidRegion(bool IgnoreRegions, const Point& a0,
56 const Point& a1, const Point& a2, const Point& b);
57extern int cornerSide(const Point &c1, const Point &c2, const Point &c3,
58 const Point& p);
59extern bool pointOnLine(const Point& a, const Point& b, const Point& c,
60 const double tolerance = 0.0);
61extern bool colinear(const Point& a, const Point& b, const Point& c,
62 const double tolerance = 0.0);
63// To be used only when the points are known to be colinear.
64extern bool inBetween(const Point& a, const Point& b, const Point& c);
65
66
67// Direction from vector.
68// Looks at the position of point c from the directed segment ab and
69// returns the following:
70// 1 counterclockwise
71// 0 collinear
72// -1 clockwise
73//
74// Based on the code of 'AreaSign'.
75//
76// The 'maybeZero' argument can be used to adjust the tolerance of the
77// function. It will be most accurate when 'maybeZero' == 0.0, the default.
78//
79static inline int vecDir(const Point& a, const Point& b, const Point& c,
80 const double maybeZero = 0.0)
81{
82 COLA_ASSERT(maybeZero >= 0);
83
84 double area2 = ((b.x - a.x) * (c.y - a.y)) -
85 ((c.x - a.x) * (b.y - a.y));
86
87 if (area2 < (-maybeZero))
88 {
89 return -1;
90 }
91 else if (area2 > maybeZero)
92 {
93 return 1;
94 }
95 return 0;
96}
97
98// Finds the projection point of (a,b) onto (a,c)
99static inline Point projection(const Point& a, const Point& b, const Point& c)
100{
101 double ux = c.x - a.x,
102 uy = c.y - a.y,
103 vx = b.x - a.x,
104 vy = b.y - a.y,
105 scalarProj = ux * vx + uy * vy;
106 scalarProj /= ux * ux + uy * uy;
107 Point p;
108 p.x = scalarProj * ux + a.x;
109 p.y = scalarProj * uy + a.y;
110 return p;
111}
112
113// Line Segment Intersection
114// Original code by Franklin Antonio
115//
116static const int DONT_INTERSECT = 0;
117static const int DO_INTERSECT = 1;
118static const int PARALLEL = 3;
119extern int segmentIntersectPoint(const Point& a1, const Point& a2,
120 const Point& b1, const Point& b2, double *x, double *y);
121extern int rayIntersectPoint(const Point& a1, const Point& a2,
122 const Point& b1, const Point& b2, double *x, double *y);
123extern double rotationalAngle(const Point& p);
124
125
126}
127
128
129#endif
pair< double, double > Point
Definition parser.cpp:7
The Point class defines a point in the plane.
Definition geomtypes.h:53
double y
The y position.
Definition geomtypes.h:109
double x
The x position.
Definition geomtypes.h:107
double c[8][4]
Contains the interface for various geometry types and classes.
libavoid: Object-avoiding orthogonal and polyline connector routing library.
double manhattanDist(const Point &a, const Point &b)
Definition geometry.cpp:302
int segmentIntersectPoint(const Point &a1, const Point &a2, const Point &b1, const Point &b2, double *x, double *y)
Definition geometry.cpp:476
bool inPoly(const Polygon &poly, const Point &q, bool countBorder)
Definition geometry.cpp:349
bool inBetween(const Point &a, const Point &b, const Point &c)
Definition geometry.cpp:55
static const int DO_INTERSECT
Definition geometry.h:117
static Point projection(const Point &a, const Point &b, const Point &c)
Definition geometry.h:99
static const int DONT_INTERSECT
Definition geometry.h:116
bool colinear(const Point &a, const Point &b, const Point &c, const double tolerance)
Definition geometry.cpp:79
double angle(const Point &a, const Point &b, const Point &c)
Definition geometry.cpp:330
static const int PARALLEL
Definition geometry.h:118
bool segmentShapeIntersect(const Point &e1, const Point &e2, const Point &s1, const Point &s2, bool &seenIntersectionAtEndpoint)
Definition geometry.cpp:166
double euclideanDist(const Point &a, const Point &b)
Definition geometry.cpp:292
int rayIntersectPoint(const Point &a1, const Point &a2, const Point &b1, const Point &b2, double *x, double *y)
Definition geometry.cpp:576
static int vecDir(const Point &a, const Point &b, const Point &c, const double maybeZero=0.0)
Definition geometry.h:79
bool inValidRegion(bool IgnoreRegions, const Point &a0, const Point &a1, const Point &a2, const Point &b)
Definition geometry.cpp:199
double rotationalAngle(const Point &p)
Definition geometry.cpp:611
bool pointOnLine(const Point &a, const Point &b, const Point &c, const double tolerance)
Definition geometry.cpp:105
int cornerSide(const Point &c1, const Point &c2, const Point &c3, const Point &p)
Definition geometry.cpp:261
double totalLength(const Polygon &poly)
Definition geometry.cpp:319
bool inPolyGen(const PolygonInterface &argpoly, const Point &q)
Definition geometry.cpp:380
bool segmentIntersect(const Point &a, const Point &b, const Point &c, const Point &d)
Definition geometry.cpp:132