latest version v1.9 - last update 10 Apr 2010 |
Type for computations with lines. More...
#include <ltiFastLine.h>
Public Member Functions | |
tfastLine () | |
tfastLine (const tpoint< T > &theStart, const tpoint< T > &theEnd) | |
template<class U > | |
tfastLine (const tfastLine< U > &other) | |
template<class U > | |
tfastLine (const tline< U > &other) | |
template<class U > | |
tfastLine< T > & | castFrom (const tfastLine< U > &other) |
void | set (const tpoint< T > &theStart, const tpoint< T > &theEnd) |
void | setStart (const tpoint< T > &theStart) |
void | setEnd (const tpoint< T > &theEnd) |
void | invert () |
tfastLine< T > & | copy (const tline< T > &other) |
tfastLine< T > & | copy (const tfastLine< T > &other) |
tfastLine< T > & | operator= (const tfastLine< T > &other) |
tfastLine< T > & | operator= (const tline< T > &other) |
Computation of line attributes | |
void | updateSlope () |
const tpoint< T > & | getDelta () const |
bool | getNormalizedSlope (float &slope) const |
void | getLineAttributes (tpoint< T > &deltaLine, float &nslope, bool &normalSlope) const |
Distance computation | |
T | distanceSqrXPol (const tpoint< T > &c, tpoint< T > &p) const |
T | distanceToXPol (const tpoint< T > &c, tpoint< T > &p) const |
T | distanceSqrXPol (const tpoint< T > &c) const |
T | distanceToXPol (const tpoint< T > &c) const |
Intersections | |
bool | doIntersect (const tfastLine< T > &other) const |
bool | getIntersectionPointXPol (const tfastLine< T > &other, tpoint< T > &p, bool &onThisLine, bool &onOtherLine, bool &colinear) const |
bool | intersect (const trectangle< T > &rect) |
bool | intersect (const tfastLine< T > &other, const trectangle< T > &rect) |
bool | intersectXPol (const trectangle< T > &rect) |
bool | intersectXPol (const tfastLine< T > &other, const trectangle< T > &rect) |
Scaling and Translation operations | |
template<class U > | |
tfastLine< T > & | scale (const U c) |
template<class U > | |
tfastLine< T > | operator* (const U c) const |
template<class U > | |
tfastLine< T > & | operator*= (const U c) |
template<class U > | |
tfastLine< T > & | divide (const U c) |
template<class U > | |
tfastLine< T > | operator/ (const U c) const |
template<class U > | |
tfastLine< T > & | operator/= (const U c) |
tfastLine< T > & | translate (const tpoint< T > &p) |
tfastLine< T > & | translate (const tline< T > &other, const tpoint< T > &p) |
Protected Member Functions | |
void | updateRequest () |
void | ensureCorrectSlope () const |
Protected Attributes | |
dependent attributes | |
bool | uptodate |
tpoint< T > | delta |
bool | invSlope |
float | normSlope |
Type for computations with lines.
A line (or more generally a tfastLine<T>) is represented by a start point and an end point.
The type T correspond to the coordinate type used in both points.
This class stores besides the two delimiting points other derived information that can save some time in some intensive computations involving distances and intersections of lines.
Of course, the class becomes faster, if the operations are done with lines that do not change much in time. If you have thousends of lines and want to compute the intersection among all of them, this class provides a good solution. If you have one line that changes continously and want to compute the intersection with another changing line, the parent class can be even faster, due to the fact that the internal stage do not need to be updated.
lti::tfastLine< T >::tfastLine | ( | ) | [explicit] |
default constructor.
Both points are left uninitialized (this can save some time)
lti::tfastLine< T >::tfastLine | ( | const tpoint< T > & | theStart, | |
const tpoint< T > & | theEnd | |||
) |
constructor with both points
lti::tfastLine< T >::tfastLine | ( | const tfastLine< U > & | other | ) | [inline] |
copy constructor
lti::tfastLine< T >::tfastLine | ( | const tline< U > & | other | ) | [inline] |
copy constructor
tfastLine<T>& lti::tfastLine< T >::castFrom | ( | const tfastLine< U > & | other | ) | [inline] |
tfastLine<T>& lti::tfastLine< T >::copy | ( | const tfastLine< T > & | other | ) | [inline] |
copy operator
Reimplemented from lti::tline< T >.
tfastLine<T>& lti::tfastLine< T >::copy | ( | const tline< T > & | other | ) | [inline] |
copy operator
T lti::tfastLine< T >::distanceSqrXPol | ( | const tpoint< T > & | c | ) | const |
Calculate square of distance to the point c to the infinite line (eXtraPolated) containing this line segment.
c | point to which the minimal distance is searched. |
This method is faster than distanceToXPol (because it does not calculate the square root).
Reimplemented from lti::tline< T >.
T lti::tfastLine< T >::distanceSqrXPol | ( | const tpoint< T > & | c, | |
tpoint< T > & | p | |||
) | const |
Calculate square of distance to the point c to the infinite line (eXtraPolated) containing this line segment.
c | point to which the minimal distance is searched. | |
p | point in the extrapolated line segment with the minimal distance to c. |
This method is faster than distanceToXPol (because it does not calculate the square root).
If not update of state (slope, delta and so on) is required, this method is about 12% faster than the normal one (in tline<T>). If an update is required, the opposite happens, and this method is about 10% slower than the tline<T> one.
Reimplemented from lti::tline< T >.
T lti::tfastLine< T >::distanceToXPol | ( | const tpoint< T > & | c | ) | const [inline] |
Calculate distance to the point c to the infinite line (eXtraPolated) containing this line segment.
Reimplemented from lti::tline< T >.
T lti::tfastLine< T >::distanceToXPol | ( | const tpoint< T > & | c, | |
tpoint< T > & | p | |||
) | const [inline] |
Calculate distance to the point c to the infinite line (eXtraPolated) containing this line segment.
tfastLine<T>& lti::tfastLine< T >::divide | ( | const U | c | ) | [inline] |
divide both points by the given c factor
Reimplemented from lti::tline< T >.
References lti::tline< T >::end, lti::tline< T >::start, and lti::tfastLine< T >::updateRequest().
Referenced by lti::tfastLine< T >::operator/=().
bool lti::tfastLine< T >::doIntersect | ( | const tfastLine< T > & | other | ) | const |
Check if this line segment intersects the other given one.
other | the other line segment to which an intersection is going to be checked. |
void lti::tfastLine< T >::ensureCorrectSlope | ( | ) | const [inline, protected] |
const method called when slope and other attributes need to be updated.
const tpoint<T>& lti::tfastLine< T >::getDelta | ( | ) | const [inline] |
returns a point containing getEnd() - getStart().
bool lti::tfastLine< T >::getIntersectionPointXPol | ( | const tfastLine< T > & | other, | |
tpoint< T > & | p, | |||
bool & | onThisLine, | |||
bool & | onOtherLine, | |||
bool & | colinear | |||
) | const |
Check if this line segment intersects the other given one.
other | the other line segment to which an intersection is going to be checked. | |
p | if there is an intersection between both line segments or between their respective infinite line extrapolations, the intersection point will be written here. | |
onThisLine | if the intersection occurs on a point on the line segment, this parameter will be set to true. Otherwise false. | |
onOtherLine | if the intersection occurs on a point on the other line segment, this parameter will be set to true. | |
colinear | this parameter is set to true in case both line segments are parallel and co-linear. |
true
if the intersection of both infinite extrapolations lay within both line segments (onThisLine and onOtherLine are both set to true
). It returns false
otherwise. Note that even if the return value is false
, the value of the point p is updated to the proper intersection of the two infinite lines containing the segments. If both line segments are parallel and colinear, this method returns true
only if both segments overlap.For parallel line segments following values can be therefore expected:
true
if both segments overlap, false
otherwise. colinear is set to true
.In case the lines are parallel (colinear or not) the point p is leaved unchaged, because the intersection occurs at none or more than one points.
This method is for updated slope values about 25% faster than the normal tline method. When one of the lines needs update both methods need almost the same time.
void lti::tfastLine< T >::getLineAttributes | ( | tpoint< T > & | deltaLine, | |
float & | nslope, | |||
bool & | normalSlope | |||
) | const [inline] |
To avoid extra computations, this method allows you to get three line attributes at once:.
deltaLine | corresponds to end-start. | |
nslope | normalized slope, always in interval -1 to 1. | |
normalSlope | if true, nslope contains the usual definition of slope (deltaLine.y/deltaLine.x), if false, nslope contains (deltaLine.x/deltaLine.y) |
bool lti::tfastLine< T >::getNormalizedSlope | ( | float & | slope | ) | const [inline] |
Get slope of the line ensuring that its absolute value is between -1 and 1.
Let delta=getDelta():
true
, the slope variable will contain usual slope (delta.y/delta.x).This way, there is no risk to become an infinite slope.
bool lti::tfastLine< T >::intersect | ( | const tfastLine< T > & | other, | |
const trectangle< T > & | rect | |||
) | [inline] |
Compute the part of the other line segment which lies within the given rectangle, and leave the result here.
This method assumes, the rectangle is already consistent, i.e. the rect.ul point is in both coordinates smaller than rect.br.
Reimplemented from lti::tline< T >.
bool lti::tfastLine< T >::intersect | ( | const trectangle< T > & | rect | ) |
Compute the part of this line segment which lies within the given rectangle, and leave the result here.
This method assumes, the rectangle is already consistent, i.e. the rect.ul point is in both coordinates smaller than rect.br.
Reimplemented from lti::tline< T >.
bool lti::tfastLine< T >::intersectXPol | ( | const tfastLine< T > & | other, | |
const trectangle< T > & | rect | |||
) | [inline] |
Compute the part of the infinite extrapolated line containing the other line segment which lies within the given rectangle, and leave the result here.
This method assumes, the rectangle is already consistent, i.e. the rect.ul point is in both coordinates smaller than rect.br.
Reimplemented from lti::tline< T >.
bool lti::tfastLine< T >::intersectXPol | ( | const trectangle< T > & | rect | ) |
Compute the part of the infinite extrapolated line containing this line segment which lies within the given rectangle, and leave the result here.
This method assumes, the rectangle is already consistent, i.e. the rect.ul point is in both coordinates smaller than rect.br.
Reimplemented from lti::tline< T >.
void lti::tfastLine< T >::invert | ( | ) | [inline] |
exchange the start and end points, making the previous end a start point and the previous start the end point.
Reimplemented from lti::tline< T >.
tfastLine<T> lti::tfastLine< T >::operator* | ( | const U | c | ) | const [inline] |
create a new line equal this one scaled by the given c factor.
Reimplemented from lti::tline< T >.
References lti::tline< T >::end, and lti::tline< T >::start.
tfastLine<T>& lti::tfastLine< T >::operator*= | ( | const U | c | ) | [inline] |
scale this line by the given c factor.
Reimplemented from lti::tline< T >.
tfastLine<T> lti::tfastLine< T >::operator/ | ( | const U | c | ) | const [inline] |
divide both points by the given c factor
Reimplemented from lti::tline< T >.
References lti::tline< T >::end, and lti::tline< T >::start.
tfastLine<T>& lti::tfastLine< T >::operator/= | ( | const U | c | ) | [inline] |
divide both points of tfastLine<T> by a given factor
Reimplemented from lti::tline< T >.
References lti::tfastLine< T >::divide().
tfastLine<T>& lti::tfastLine< T >::operator= | ( | const tline< T > & | other | ) | [inline] |
operator =
tfastLine<T>& lti::tfastLine< T >::operator= | ( | const tfastLine< T > & | other | ) | [inline] |
operator =
Reimplemented from lti::tline< T >.
tfastLine<T>& lti::tfastLine< T >::scale | ( | const U | c | ) | [inline] |
scale this line by the given c factor.
Reimplemented from lti::tline< T >.
References lti::tline< T >::end, lti::tline< T >::start, and lti::tfastLine< T >::updateRequest().
void lti::tfastLine< T >::set | ( | const tpoint< T > & | theStart, | |
const tpoint< T > & | theEnd | |||
) | [inline] |
general operator to set both points of the line
Reimplemented from lti::tline< T >.
void lti::tfastLine< T >::setEnd | ( | const tpoint< T > & | theEnd | ) | [inline] |
void lti::tfastLine< T >::setStart | ( | const tpoint< T > & | theStart | ) | [inline] |
set the start point.
Reimplemented from lti::tline< T >.
tfastLine<T>& lti::tfastLine< T >::translate | ( | const tline< T > & | other, | |
const tpoint< T > & | p | |||
) | [inline] |
add given point to both ends of the other line and leave the result here.
other | the other line to be tranlated | |
p | the translation factor |
tfastLine<T>& lti::tfastLine< T >::translate | ( | const tpoint< T > & | p | ) | [inline] |
add given point to both ends of this line and leave the result here.
p | the other line to be added to this one |
Reimplemented from lti::tline< T >.
void lti::tfastLine< T >::updateRequest | ( | ) | [inline, protected] |
Method called if a change in the points have been done.
This policy does nothing here.
Referenced by lti::tfastLine< T >::castFrom(), lti::tfastLine< T >::divide(), and lti::tfastLine< T >::scale().
void lti::tfastLine< T >::updateSlope | ( | ) | [inline] |
Update slope can be called to force update of the internal variables containing the line attributes (for example, at a not critical computation time you will want to call this.
)
tpoint<T> lti::tfastLine< T >::delta [mutable, protected] |
delta
bool lti::tfastLine< T >::invSlope [mutable, protected] |
flag indicating if the stored value is slope (false) or 1/slope (true)
float lti::tfastLine< T >::normSlope [mutable, protected] |
value of the slope: always between -1 and 1.
bool lti::tfastLine< T >::uptodate [mutable, protected] |
flag indicating state of the slope