line.h revision b63c6af7aa7f869de42730cf0ffd8b6077d778c5
/**
* \file
* \brief Infinite straight line
*//*
* Authors:
* Marco Cecchetti <mrcekets at gmail.com>
* Krzysztof KosiĆski <tweenk.pl@gmail.com>
* Copyright 2008-2011 Authors
*
* 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
*
* 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 <boost/optional.hpp>
{
// class docs in cpp file
{
/// @name Creating lines.
/// @{
/** @brief Create a default horizontal line.
* Creates a line with unit speed going in +X direction. */
Line()
{}
/** @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 */
{
Point 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 */
: _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.
{}
/// Create a line by extending a ray.
{}
/// Create a line normal to a vector at a specified distance from origin.
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 */
Line l(o, o + v);
return l;
}
}
/// @}
/// @name Retrieve and set the line's parameters.
/// @{
/// Get the line's origin point.
/** @brief Get the line's raw direction vector.
* The retrieved vector is normalized to unit length. */
/** @brief Get the line's normalized direction vector.
* The retrieved vector is normalized to unit length. */
/// Angle the line makes with the X axis, in mathematical convention.
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. */
_initial = p;
_final += d;
}
/** @brief Set the speed of the line.
* Origin remains unchanged. */
}
/** @brief Set the angle the line makes with the X axis.
* Origin remains unchanged. */
Point v;
}
/// Set a line based on two points it should pass through.
_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$. */
/// Get the coefficients of the line equation by reference.
/** @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 {
}
/// Check if the line is horizontal (y is constant).
bool isHorizontal() const {
}
/// Check if the line is vertical (x is constant).
bool isVertical() const {
}
/** @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
}
v.normalize();
}
/** @brief Return a new line reparametrized for unit speed. */
Line normalized() const {
v.normalize();
return ret;
}
/// @}
/// @name Evaluate the line as a function.
///@{
Point initialPoint() const {
return _initial;
}
Point finalPoint() const {
return _final;
}
}
}
/** @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. */
if ( isDegenerate() ) return 0;
}
/** @brief Find a point on the line closest to the query point.
* This is an alias for timeAtProjection(). */
return timeAtProjection(p);
}
/// @}
/// @name Create other objects based on this line.
/// @{
void reverse() {
}
/** @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$ */
return result;
}
/** @brief Same as segment(), but allocate the line segment dynamically. */
// TODO remove this?
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 */
}
/// Return the portion of the line that is inside the given rectangle
/** @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 */
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 {
return result;
}
/// Create a line transformed by an affine transformation.
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. */
}
// what does this do?
return n;
}
/// Compute an affine matrix representing a reflection about the line.
Affine reflection() const {
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
* additionaly 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 */
if (d == X) {
} else {
v[Y] = -v[Y];
}
return m;
}
/** @brief Compute a rotation affine which transforms the line to one of the axes.
* @param d Which line should be the axis */
return m;
}
/// @}
return *this;
}
return true;
}
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);
/// @brief Compute distance from point to line.
/// @relates Line
inline
{
if (line.isDegenerate()) {
} else {
}
}
inline
{
}
inline
{
}
/** @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 the the same, because their images match, even though there is
* no time value for which the lines give similar points.
* @relates Line */
inline
{
}
/// Test whether two lines are perpendicular.
/// @relates Line
inline
{
}
// 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
{
return angle;
}
inline
{
double t = seg.nearestTime(p);
}
inline
{
}
// build a line passing by _point and orthogonal to _line
inline
{
Line l(p, p + d);
return l;
}
// build a line passing by _point and parallel to _line
inline
{
return result;
}
// build a line passing by the middle point of _segment and orthogonal to it.
inline
{
}
// build the bisector line of the angle between ray(O,A) and ray(O,B)
inline
{
}
// prj(P) = rot(v, Point( rot(-v, P-O)[X], 0 )) + O
inline
{
}
inline
{
}
inline
return l.clip(r);
}
{
unsigned int i );
unsigned int i );
}
inline
{
}
inline
{
}
inline
{
}
inline
{
}
inline
{
}
inline
{
}
} // 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 :