Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
ray.h
Go to the documentation of this file.
1/*
5 * Copyright 2008 Marco Cecchetti <mrcekets at gmail.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it either under the terms of the GNU Lesser General Public
9 * License version 2.1 as published by the Free Software Foundation
10 * (the "LGPL") or, at your option, under the terms of the Mozilla
11 * Public License Version 1.1 (the "MPL"). If you do not alter this
12 * notice, a recipient may use your version of this file under either
13 * the MPL or the LGPL.
14 *
15 * You should have received a copy of the LGPL along with this library
16 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * You should have received a copy of the MPL along with this library
19 * in the file COPYING-MPL-1.1
20 *
21 * The contents of this file are subject to the Mozilla Public License
22 * Version 1.1 (the "License"); you may not use this file except in
23 * compliance with the License. You may obtain a copy of the License at
24 * http://www.mozilla.org/MPL/
25 *
26 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28 * the specific language governing rights and limitations.
29 */
30
31#ifndef LIB2GEOM_SEEN_RAY_H
32#define LIB2GEOM_SEEN_RAY_H
33
34#include <vector>
35#include <2geom/point.h>
36#include <2geom/bezier-curve.h> // for LineSegment
37#include <2geom/exception.h>
38#include <2geom/math-utils.h>
39#include <2geom/transforms.h>
40#include <2geom/angle.h>
41
42namespace Geom
43{
44
53class Ray {
54private:
57
58public:
59 Ray() : _origin(0,0), _vector(1,0) {}
62 {
64 }
65 Ray(Point const& A, Point const& B) {
66 setPoints(A, B);
67 }
68 Point origin() const { return _origin; }
69 Point vector() const { return _vector; }
70 Point versor() const { return _vector.normalized(); }
71 void setOrigin(Point const &o) { _origin = o; }
72 void setVector(Point const& v) { _vector = v; }
73 Coord angle() const { return std::atan2(_vector[Y], _vector[X]); }
74 void setAngle(Coord a) { sincos(a, _vector[Y], _vector[X]); }
75 void setPoints(Point const &a, Point const &b) {
76 _origin = a;
77 _vector = b - a;
78 if (are_near(_vector, Point(0,0)) )
79 _vector = Point(0,0);
80 else
82 }
83 bool isDegenerate() const {
84 return ( _vector[X] == 0 && _vector[Y] == 0 );
85 }
86 Point pointAt(Coord t) const {
87 return _origin + _vector * t;
88 }
89 Coord valueAt(Coord t, Dim2 d) const {
90 return _origin[d] + _vector[d] * t;
91 }
92 std::vector<Coord> roots(Coord v, Dim2 d) const {
93 std::vector<Coord> result;
94 if ( _vector[d] != 0 ) {
95 double t = (v - _origin[d]) / _vector[d];
96 if (t >= 0) result.push_back(t);
97 } else if (_vector[(d+1)%2] == v) {
98 THROW_INFINITESOLUTIONS();
99 }
100 return result;
101 }
102 Coord nearestTime(Point const& point) const {
103 if ( isDegenerate() ) return 0;
104 double t = dot(point - _origin, _vector);
105 if (t < 0) t = 0;
106 return t;
107 }
108 Ray reverse() const {
109 Ray result;
111 result.setVector(-_vector);
112 return result;
113 }
114 Curve *portion(Coord f, Coord t) const {
115 return new LineSegment(pointAt(f), pointAt(t));
116 }
118 return LineSegment(pointAt(f), pointAt(t));
119 }
120 Ray transformed(Affine const& m) const {
121 return Ray(_origin * m, (_origin + _vector) * m);
122 }
123}; // end class Ray
124
125inline
126double distance(Point const& _point, Ray const& _ray) {
127 double t = _ray.nearestTime(_point);
128 return ::Geom::distance(_point, _ray.pointAt(t));
129}
130
131inline
132bool are_near(Point const& _point, Ray const& _ray, double eps = EPSILON) {
133 return are_near(distance(_point, _ray), 0, eps);
134}
135
136inline
137bool are_same(Ray const& r1, Ray const& r2, double eps = EPSILON) {
138 return are_near(r1.vector(), r2.vector(), eps)
139 && are_near(r1.origin(), r2.origin(), eps);
140}
141
142// evaluate the angle between r1 and r2 rotating r1 in cw or ccw direction on r2
143// the returned value is an angle in the interval [0, 2PI[
144inline
145double angle_between(Ray const& r1, Ray const& r2, bool cw = true) {
146 double angle = angle_between(r1.vector(), r2.vector());
147 if (angle < 0) angle += 2*M_PI;
148 if (!cw) angle = 2*M_PI - angle;
149 return angle;
150}
151
166inline
167Ray make_angle_bisector_ray(Ray const& r1, Ray const& r2, bool cw = true)
168{
169 if ( !are_near(r1.origin(), r2.origin()) )
170 {
171 THROW_RANGEERROR("passed rays do not have the same origin");
172 }
173
174 Ray bisector(r1.origin(), r1.origin() + r1.vector() * Rotate(angle_between(r1, r2) / 2.0));
175
176 return (cw ? bisector : bisector.reverse());
177}
178
179} // end namespace Geom
180
181#endif // LIB2GEOM_SEEN_RAY_H
182
183/*
184 Local Variables:
185 mode:c++
186 c-file-style:"stroustrup"
187 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
188 indent-tabs-mode:nil
189 fill-column:99
190 End:
191*/
192// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Defines the different types of exceptions that 2geom can throw.
Cartesian point / 2D vector and related operations.
Various trigoniometric helper functions.
Bezier curve.
3x3 matrix representing an affine transformation.
Definition affine.h:70
Abstract continuous curve on a plane defined on [0,1].
Definition curve.h:78
Two-dimensional point that doubles as a vector.
Definition point.h:66
void normalize()
Normalize the vector representing the point.
Definition point.cpp:96
Point normalized() const
Definition point.h:121
Straight ray from a specific point to infinity.
Definition ray.h:53
Point _vector
Definition ray.h:56
Ray reverse() const
Definition ray.h:108
std::vector< Coord > roots(Coord v, Dim2 d) const
Definition ray.h:92
void setAngle(Coord a)
Definition ray.h:74
Ray transformed(Affine const &m) const
Definition ray.h:120
void setPoints(Point const &a, Point const &b)
Definition ray.h:75
Coord nearestTime(Point const &point) const
Definition ray.h:102
Point origin() const
Definition ray.h:68
LineSegment segment(Coord f, Coord t) const
Definition ray.h:117
Ray(Point const &origin, Coord angle)
Definition ray.h:60
Curve * portion(Coord f, Coord t) const
Definition ray.h:114
Ray(Point const &A, Point const &B)
Definition ray.h:65
void setVector(Point const &v)
Definition ray.h:72
Coord valueAt(Coord t, Dim2 d) const
Definition ray.h:89
void setOrigin(Point const &o)
Definition ray.h:71
Point pointAt(Coord t) const
Definition ray.h:86
Point vector() const
Definition ray.h:69
bool isDegenerate() const
Definition ray.h:83
Ray()
Definition ray.h:59
Point _origin
Definition ray.h:55
Point versor() const
Definition ray.h:70
Coord angle() const
Definition ray.h:73
Rotation around the origin.
Definition transforms.h:187
Css & result
BezierCurveN< 1 > LineSegment
Line segment.
Dim2
2D axis enumeration (X or Y).
Definition coord.h:48
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
constexpr Coord EPSILON
Default "acceptably small" value.
Definition coord.h:84
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
Low level math functions and compatibility wrappers.
Various utility functions.
Definition affine.h:22
void sincos(double angle, double &sin_, double &cos_)
Simultaneously compute a sine and a cosine of the same angle.
Definition math-utils.h:89
Ray make_angle_bisector_ray(Ray const &r1, Ray const &r2, bool cw=true)
Returns the angle bisector for the two given rays.
Definition ray.h:167
bool are_same(Ray const &r1, Ray const &r2, double eps=EPSILON)
Definition ray.h:137
Angle distance(Angle const &a, Angle const &b)
Definition angle.h:163
double angle_between(Line const &l1, Line const &l2)
Definition line.h:456
T dot(D2< T > const &a, D2< T > const &b)
Definition d2.h:355
bool are_near(Affine const &a1, Affine const &a2, Coord eps=EPSILON)
Affine transformation classes.