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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
|
/**
* \file
* \brief Infinite straight line
*//*
* Authors:
* Marco Cecchetti <mrcekets at gmail.com>
* Krzysztof KosiĆski <tweenk.pl@gmail.com>
* Copyright 2008-2011 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_LINE_H
#define LIB2GEOM_SEEN_LINE_H
#include <cmath>
#include <optional>
#include <2geom/bezier-curve.h> // for LineSegment
#include <2geom/rect.h>
#include <2geom/crossing.h>
#include <2geom/exception.h>
#include <2geom/ray.h>
#include <2geom/angle.h>
#include <2geom/intersection.h>
namespace Geom
{
// class docs in cpp file
class Line
: boost::equality_comparable< Line >
{
private:
Point _initial;
Point _final;
public:
/// @name Creating lines.
/// @{
/** @brief Create a default horizontal line.
* Creates a line with unit speed going in +X direction. */
Line()
: _initial(0,0), _final(1,0)
{}
/** @brief Create a line with the specified inclination.
* @param origin One of the points on the line
* @param angle Angle of the line in mathematical convention */
Line(Point const &origin, Coord angle)
: _initial(origin)
{
Point v;
sincos(angle, v[Y], v[X]);
_final = _initial + v;
}
/** @brief Create a line going through two points.
* The first point will be at time 0, while the second one
* will be at time 1.
* @param a Initial point
* @param b First point */
Line(Point const &a, Point const &b)
: _initial(a)
, _final(b)
{}
/** @brief Create a line based on the coefficients of its equation.
@see Line::setCoefficients() */
Line(double a, double b, double c) {
setCoefficients(a, b, c);
}
/// Create a line by extending a line segment.
explicit Line(LineSegment const &seg)
: _initial(seg.initialPoint())
, _final(seg.finalPoint())
{}
/// Create a line by extending a ray.
explicit Line(Ray const &r)
: _initial(r.origin())
, _final(r.origin() + r.vector())
{}
/// Create a line normal to a vector at a specified distance from origin.
static Line from_normal_distance(Point const &n, Coord c) {
Point start = c * n.normalized();
Line l(start, start + rot90(n));
return l;
}
/** @brief Create a line from origin and unit vector.
* Note that each line direction has two possible unit vectors.
* @param o Point through which the line will pass
* @param v Unit vector of the line's direction */
static Line from_origin_and_vector(Point const &o, Point const &v) {
Line l(o, o + v);
return l;
}
Line* duplicate() const {
return new Line(*this);
}
/// @}
/// @name Retrieve and set the line's parameters.
/// @{
/// Get the line's origin point.
Point origin() const { return _initial; }
/** @brief Get the line's raw direction vector.
* The length of the retrieved vector is equal to the length of a segment parametrized by
* a time interval of length 1. */
Point vector() const { return _final - _initial; }
/** @brief Get the line's normalized direction vector.
* The retrieved vector is normalized to unit length. */
Point versor() const { return (_final - _initial).normalized(); }
/// Angle the line makes with the X axis, in mathematical convention.
Coord angle() const {
Point d = _final - _initial;
double a = std::atan2(d[Y], d[X]);
if (a < 0) a += M_PI;
if (a == M_PI) a = 0;
return a;
}
/** @brief Set the point at zero time.
* The orientation remains unchanged, modulo numeric errors during addition. */
void setOrigin(Point const &p) {
Point d = p - _initial;
_initial = p;
_final += d;
}
/** @brief Set the speed of the line.
* Origin remains unchanged. */
void setVector(Point const &v) {
_final = _initial + v;
}
/** @brief Set the angle the line makes with the X axis.
* Origin remains unchanged. */
void setAngle(Coord angle) {
Point v;
sincos(angle, v[Y], v[X]);
v *= distance(_initial, _final);
_final = _initial + v;
}
/// Set a line based on two points it should pass through.
void setPoints(Point const &a, Point const &b) {
_initial = a;
_final = b;
}
/** @brief Set the coefficients of the line equation.
* The line equation is: \f$ax + by = c\f$. Points that satisfy the equation
* are on the line. */
void setCoefficients(double a, double b, double c);
/** @brief Get the coefficients of the line equation as a vector.
* @return STL vector @a v such that @a v[0] contains \f$a\f$, @a v[1] contains \f$b\f$,
* and @a v[2] contains \f$c\f$. */
std::vector<double> coefficients() const;
/// Get the coefficients of the line equation by reference.
void coefficients(Coord &a, Coord &b, Coord &c) const;
/** @brief Check if the line has more than one point.
* A degenerate line can be created if the line is created from a line equation
* that has no solutions.
* @return True if the line has no points or exactly one point */
bool isDegenerate() const {
return _initial == _final;
}
/// Check if the line is horizontal (y is constant).
bool isHorizontal() const {
return _initial[Y] == _final[Y];
}
/// Check if the line is vertical (x is constant).
bool isVertical() const {
return _initial[X] == _final[X];
}
/** @brief Reparametrize the line so that it has unit speed.
* Note that the direction of the line may also change. */
void normalize() {
// this helps with the nasty case of a line that starts somewhere far
// and ends very close to the origin
if (L2sq(_final) < L2sq(_initial)) {
std::swap(_initial, _final);
}
Point v = _final - _initial;
v.normalize();
_final = _initial + v;
}
/** @brief Return a new line reparametrized for unit speed. */
Line normalized() const {
Point v = _final - _initial;
v.normalize();
Line ret(_initial, _initial + v);
return ret;
}
/// @}
/// @name Evaluate the line as a function.
///@{
Point initialPoint() const {
return _initial;
}
Point finalPoint() const {
return _final;
}
Point pointAt(Coord t) const {
return lerp(t, _initial, _final);;
}
Coord valueAt(Coord t, Dim2 d) const {
return lerp(t, _initial[d], _final[d]);
}
Coord timeAt(Point const &p) const;
/** @brief Get a time value corresponding to a projection of a point on the line.
* @param p Arbitrary point.
* @return Time value corresponding to a point closest to @c p. */
Coord timeAtProjection(Point const& p) const {
if ( isDegenerate() ) return 0;
Point v = vector();
return dot(p - _initial, v) / dot(v, v);
}
/** @brief Find a point on the line closest to the query point.
* This is an alias for timeAtProjection(). */
Coord nearestTime(Point const &p) const {
return timeAtProjection(p);
}
std::vector<Coord> roots(Coord v, Dim2 d) const;
Coord root(Coord v, Dim2 d) const;
/// @}
/// @name Create other objects based on this line.
/// @{
void reverse() {
std::swap(_final, _initial);
}
/** @brief Create a line containing the same points, but in opposite direction.
* @return Line \f$g\f$ such that \f$g(t) = f(1-t)\f$ */
Line reversed() const {
Line result(_final, _initial);
return result;
}
/** @brief Same as segment(), but allocate the line segment dynamically. */
// TODO remove this?
Curve* portion(Coord f, Coord t) const {
LineSegment* seg = new LineSegment(pointAt(f), pointAt(t));
return seg;
}
/** @brief Create a segment of this line.
* @param f Time value for the initial point of the segment
* @param t Time value for the final point of the segment
* @return Created line segment */
LineSegment segment(Coord f, Coord t) const {
return LineSegment(pointAt(f), pointAt(t));
}
/// Return the portion of the line that is inside the given rectangle
std::optional<LineSegment> clip(Rect const &r) const;
/** @brief Create a ray starting at the specified time value.
* The created ray will go in the direction of the line's vector (in the direction
* of increasing time values).
* @param t Time value where the ray should start
* @return Ray starting at t and going in the direction of the vector */
Ray ray(Coord t) {
Ray result;
result.setOrigin(pointAt(t));
result.setVector(vector());
return result;
}
/** @brief Create a derivative of the line.
* The new line will always be degenerate. Its origin will be equal to this
* line's vector. */
Line derivative() const {
Point v = vector();
Line result(v, v);
return result;
}
/// Create a line transformed by an affine transformation.
Line transformed(Affine const& m) const {
Line l(_initial * m, _final * m);
return l;
}
/** @brief Get a unit vector normal to the line.
* If Y grows upwards, then this is the left normal. If Y grows downwards,
* then this is the right normal. */
Point normal() const {
return rot90(vector()).normalized();
}
// what does this do?
Point normalAndDist(double & dist) const {
Point n = normal();
dist = -dot(n, _initial);
return n;
}
/// Compute an affine matrix representing a reflection about the line.
Affine reflection() const {
Point v = versor();
Coord x2 = v[X]*v[X], y2 = v[Y]*v[Y], xy = v[X]*v[Y];
Affine m(x2-y2, 2.*xy,
2.*xy, y2-x2,
_initial[X], _initial[Y]);
m = Translate(-_initial) * m;
return m;
}
/** @brief Compute an affine which transforms all points on the line to zero X or Y coordinate.
* This operation is useful in reducing intersection problems to root-finding problems.
* There are many affines which do this transformation. This function returns one that
* preserves angles, areas and distances - a rotation combined with a translation, and
* additionally moves the initial point of the line to (0,0). This way it works without
* problems even for lines perpendicular to the target, though may in some cases have
* lower precision than e.g. a shear transform.
* @param d Which coordinate of points on the line should be zero after the transformation */
Affine rotationToZero(Dim2 d) const {
Point v = vector();
if (d == X) {
std::swap(v[X], v[Y]);
} else {
v[Y] = -v[Y];
}
Affine m = Translate(-_initial) * Rotate(v);
return m;
}
/** @brief Compute a rotation affine which transforms the line to one of the axes.
* @param d Which line should be the axis */
Affine rotationToAxis(Dim2 d) const {
Affine m = rotationToZero(other_dimension(d));
return m;
}
Affine transformTo(Line const &other) const;
/// @}
std::vector<ShapeIntersection> intersect(Line const &other) const;
std::vector<ShapeIntersection> intersect(Ray const &r) const;
std::vector<ShapeIntersection> intersect(LineSegment const &ls) const;
template <typename T>
Line &operator*=(T const &tr) {
BOOST_CONCEPT_ASSERT((TransformConcept<T>));
_initial *= tr;
_final *= tr;
return *this;
}
bool operator==(Line const &other) const {
if (distance(pointAt(nearestTime(other._initial)), other._initial) != 0) return false;
if (distance(pointAt(nearestTime(other._final)), other._final) != 0) return false;
return true;
}
template <typename T>
friend Line operator*(Line const &l, T const &tr) {
BOOST_CONCEPT_ASSERT((TransformConcept<T>));
Line result(l);
result *= tr;
return result;
}
}; // end class Line
/** @brief Removes intersections outside of the unit interval.
* A helper used to implement line segment intersections.
* @param xs Line intersections
* @param a Whether the first time value has to be in the unit interval
* @param b Whether the second time value has to be in the unit interval
* @return Appropriately filtered intersections */
void filter_line_segment_intersections(std::vector<ShapeIntersection> &xs, bool a=false, bool b=true);
void filter_ray_intersections(std::vector<ShapeIntersection> &xs, bool a=false, bool b=true);
/// @brief Compute distance from point to line.
/// @relates Line
inline
double distance(Point const &p, Line const &line)
{
if (line.isDegenerate()) {
return ::Geom::distance(p, line.initialPoint());
} else {
Coord t = line.nearestTime(p);
return ::Geom::distance(line.pointAt(t), p);
}
}
inline
bool are_near(Point const &p, Line const &line, double eps = EPSILON)
{
return are_near(distance(p, line), 0, eps);
}
inline
bool are_parallel(Line const &l1, Line const &l2, double eps = EPSILON)
{
return are_near(cross(l1.vector(), l2.vector()), 0, eps);
}
/** @brief Test whether two lines are approximately the same.
* This tests for being parallel and the origin of one line being close to the other,
* so it tests whether the images of the lines are similar, not whether the same time values
* correspond to similar points. For example a line from (1,1) to (2,2) and a line from
* (-1,-1) to (0,0) will be the same, because their images match, even though there is
* no time value for which the lines give similar points.
* @relates Line */
inline
bool are_same(Line const &l1, Line const &l2, double eps = EPSILON)
{
return are_parallel(l1, l2, eps) && are_near(l1.origin(), l2, eps);
}
/// Test whether two lines are perpendicular.
/// @relates Line
inline
bool are_orthogonal(Line const &l1, Line const &l2, double eps = EPSILON)
{
return are_near(dot(l1.vector(), l2.vector()), 0, eps);
}
// evaluate the angle between l1 and l2 rotating l1 in cw direction
// until it overlaps l2
// the returned value is an angle in the interval [0, PI[
inline
double angle_between(Line const& l1, Line const& l2)
{
double angle = angle_between(l1.vector(), l2.vector());
if (angle < 0) angle += M_PI;
if (angle == M_PI) angle = 0;
return angle;
}
inline
double distance(Point const &p, LineSegment const &seg)
{
double t = seg.nearestTime(p);
return distance(p, seg.pointAt(t));
}
inline
bool are_near(Point const &p, LineSegment const &seg, double eps = EPSILON)
{
return are_near(distance(p, seg), 0, eps);
}
// build a line passing by _point and orthogonal to _line
inline
Line make_orthogonal_line(Point const &p, Line const &line)
{
Point d = line.vector().cw();
Line l(p, p + d);
return l;
}
// build a line passing by _point and parallel to _line
inline
Line make_parallel_line(Point const &p, Line const &line)
{
Line result(line);
result.setOrigin(p);
return result;
}
// build a line passing by the middle point of _segment and orthogonal to it.
inline
Line make_bisector_line(LineSegment const& _segment)
{
return make_orthogonal_line( middle_point(_segment), Line(_segment) );
}
// build the bisector line of the angle between ray(O,A) and ray(O,B)
inline
Line make_angle_bisector_line(Point const &A, Point const &O, Point const &B)
{
AngleInterval ival(Angle(A-O), Angle(B-O));
Angle bisect = ival.angleAt(0.5);
return Line(O, bisect);
}
// prj(P) = rot(v, Point( rot(-v, P-O)[X], 0 )) + O
inline
Point projection(Point const &p, Line const &line)
{
return line.pointAt(line.nearestTime(p));
}
inline
LineSegment projection(LineSegment const &seg, Line const &line)
{
return line.segment(line.nearestTime(seg.initialPoint()),
line.nearestTime(seg.finalPoint()));
}
inline
std::optional<LineSegment> clip(Line const &l, Rect const &r) {
return l.clip(r);
}
namespace detail
{
OptCrossing intersection_impl(Ray const& r1, Line const& l2, unsigned int i);
OptCrossing intersection_impl( LineSegment const& ls1,
Line const& l2,
unsigned int i );
OptCrossing intersection_impl( LineSegment const& ls1,
Ray const& r2,
unsigned int i );
}
inline
OptCrossing intersection(Ray const& r1, Line const& l2)
{
return detail::intersection_impl(r1, l2, 0);
}
inline
OptCrossing intersection(Line const& l1, Ray const& r2)
{
return detail::intersection_impl(r2, l1, 1);
}
inline
OptCrossing intersection(LineSegment const& ls1, Line const& l2)
{
return detail::intersection_impl(ls1, l2, 0);
}
inline
OptCrossing intersection(Line const& l1, LineSegment const& ls2)
{
return detail::intersection_impl(ls2, l1, 1);
}
inline
OptCrossing intersection(LineSegment const& ls1, Ray const& r2)
{
return detail::intersection_impl(ls1, r2, 0);
}
inline
OptCrossing intersection(Ray const& r1, LineSegment const& ls2)
{
return detail::intersection_impl(ls2, r1, 1);
}
OptCrossing intersection(Line const& l1, Line const& l2);
OptCrossing intersection(Ray const& r1, Ray const& r2);
OptCrossing intersection(LineSegment const& ls1, LineSegment const& ls2);
} // end namespace Geom
#endif // LIB2GEOM_SEEN_LINE_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 :
|