~jaspervdg/+junk/aem-diffusion-curves

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
/**
 * \file
 * \brief  Infinite Straight Ray
 *
 * Copyright 2008  Marco Cecchetti <mrcekets at gmail.com>
 *
 * 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 _2GEOM_RAY_H_
#define _2GEOM_RAY_H_

#include <2geom/point.h>
#include <2geom/bezier-curve.h> // for LineSegment
#include <2geom/exception.h>

#include <vector>


namespace Geom
{

class Ray
{
public:
	Ray()
		: m_origin(0,0), m_versor(1,0)
	{
	}

	Ray(Point const& _origin, Coord angle )
		: m_origin(_origin), m_versor(std::cos(angle), std::sin(angle))
	{
	}

	Ray(Point const& A, Point const& B)
	{
		setBy2Points(A, B);
	}

	Point origin() const
	{
		return m_origin;
	}

	Point versor() const
	{
		return m_versor;
	}

	void origin(Point const& _point)
	{
		m_origin = _point;
	}

	void versor(Point const& _versor)
	{
		m_versor = _versor;
	}

	Coord angle() const
	{
		double a = std::atan2(m_versor[Y], m_versor[X]);
		if (a < 0) a += 2*M_PI;
		return a;
	}

	void angle(Coord _angle)
	{
		m_versor[X] = std::cos(_angle);
		m_versor[Y] = std::sin(_angle);
	}

	void setBy2Points(Point const& A, Point const& B)
	{
		m_origin = A;
		m_versor = B - A;
		if ( are_near(m_versor, Point(0,0)) )
			m_versor = Point(0,0);
		else
			m_versor.normalize();
	}

	bool isDegenerate() const
	{
		return ( m_versor[X] == 0 && m_versor[Y] == 0 );
	}

	Point pointAt(Coord t) const
	{
		if (t < 0)	THROW_RANGEERROR("Ray::pointAt, negative t value passed");
		return m_origin + m_versor * t;
	}

	Coord valueAt(Coord t, Dim2 d) const
	{
		if (t < 0)
			THROW_RANGEERROR("Ray::valueAt, negative t value passed");
		if (d < 0 || d > 1)
			THROW_RANGEERROR("Ray::valueAt, dimension argument out of range");
		return m_origin[d] + m_versor[d] * t;
	}

	std::vector<Coord> roots(Coord v, Dim2 d) const
	{
		if (d < 0 || d > 1)
			THROW_RANGEERROR("Ray::roots, dimension argument out of range");
		std::vector<Coord> result;
		if ( m_versor[d] != 0 )
		{
			double t = (v - m_origin[d]) / m_versor[d];
			if (t >= 0)	result.push_back(t);
		}
		// TODO: else ?
		return result;
	}

	// require are_near(_point, *this)
	// on the contrary the result value is meaningless
	Coord timeAt(Point const& _point) const
	{
		Coord t;
		if ( m_versor[X] != 0 )
		{
			t = (_point[X] - m_origin[X]) / m_versor[X];
		}
		else if ( m_versor[Y] != 0 )
		{
			t = (_point[Y] - m_origin[Y]) / m_versor[Y];
		}
		else // degenerate case
		{
			t = 0;
		}
		return t;
	}

	Coord nearestPoint(Point const& _point) const
	{
		if ( isDegenerate() ) return 0;
		double t = dot( _point - m_origin, m_versor );
		if (t < 0) t = 0;
		return t;
	}

	Ray reverse() const
	{
		Ray result;
		result.origin(m_origin);
		result.versor(-m_versor);
		return result;
	}

	Curve* portion(Coord  f, Coord t) const
	{
		LineSegment* seg = new LineSegment(pointAt(f), pointAt(t));
		return seg;
	}

	LineSegment segment(Coord  f, Coord t) const
	{
		return LineSegment(pointAt(f), pointAt(t));
	}

	Ray transformed(Matrix const& m) const
	{
		return Ray(m_origin * m, (m_origin + m_versor) * m);
	}

private:
	Point m_origin;
	Point m_versor;

};  // end class ray

inline
double distance(Point const& _point, Ray const& _ray)
{
	double t = _ray.nearestPoint(_point);
	return distance(_point, _ray.pointAt(t));
}

inline
bool are_near(Point const& _point, Ray const& _ray, double eps = EPSILON)
{
	return are_near(distance(_point, _ray), 0, eps);
}

inline
bool are_same(Ray const& r1, Ray const& r2, double eps = EPSILON)
{
	return are_near(r1.versor(), r2.versor(), eps)
			&& are_near(r1.origin(), r2.origin(), eps);
}

// evaluate the angle between r1 and r2 rotating r1 in cw or ccw direction on r2
// the returned value is an angle in the interval [0, 2PI[
inline
double angle_between(Ray const& r1, Ray const& r2, bool cw = true)
{
	double angle = angle_between(r1.versor(), r2.versor());
	if (angle < 0) angle += 2*M_PI;
	if (!cw) angle = 2*M_PI - angle;
	return angle;
}


inline
Ray make_angle_bisector_ray(Ray const& r1, Ray const& r2)
{
    if ( !are_near(r1.origin(), r2.origin()) )
    {
        THROW_RANGEERROR("passed rays have not the same origin");
    }

    Point M = middle_point(r1.pointAt(1), r2.pointAt(1) );
    if (angle_between(r1, r2) > M_PI)  M = 2 * r1.origin() - M;
    return Ray(r1.origin(), M);
}


}  // end namespace Geom



#endif /*_2GEOM_RAY_H_*/


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