KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Friends | List of all members
kjb::Line_segment Class Reference

Class to manipulate a line segment The class is parametrized in terms the position of the centre, its length and orientation. This is thus compatible with the output of the Berkeley edge detector. We store also the start point, the end point and the line parameters describing this line segment (for more details on how the line parameters work, please see the class line). This is redundant information, but it is convenient to have all these parameters precomputed and at hand. More...

#include <gr_line_segment.h>

Inheritance diagram for kjb::Line_segment:
kjb::Readable kjb::Writeable kjb::Collinear_segment_chain kjb::Edge_segment kjb::Model_edge

Public Member Functions

 Line_segment ()
 Constructor without initializations. More...
 
 Line_segment (double icentre_x, double icentre_y, double iorientation, double ilength)
 Constructs a new Line_segment from centre, orientation, length. More...
 
 Line_segment (const Vector &start, const Vector &end)
 Constructs a new Line_segment from a starting and ending point. More...
 
 Line_segment (std::istream &in)
 Constructs this line segment by reading from an input stream. More...
 
 Line_segment (const Line_segment &ls)
 Copy constructor. More...
 
Line_segmentoperator= (const Line_segment &ls)
 Assignment operator. More...
 
bool operator== (const Line_segment &ls) const
 
bool operator< (const Line_segment &ls) const
 
void init_from_slope_and_intercept (double x_start, double x_end, double islope, double iintercept)
 Initialize from x_start, x_end, slope, y_axis intercept. More...
 
void init_vertical_segment (double y_start, double y_end, double x)
 Initialize a vertical Line_segment from y_start, y_end, x coord. More...
 
void init_from_end_points (double x_1, double y_1, double x_2, double y_2)
 Initialize a new Line_segment from position of its end points. More...
 
void init_from_centre_and_orientation (double icentre_x, double icentre_y, double iorientation, double ilength)
 
double get_centre_x () const
 Returns the x-coordinate of the mid-point. More...
 
double get_centre_y () const
 Returns the y-coordinate of the mid-point. More...
 
bool point_outside_segment (const Vector &point) const
 Check whether point is inside the ending points of this segment Note point is collinear with this line segment. More...
 
const kjb::Vectorget_centre () const
 Returns the mid-point. More...
 
double get_start_x () const
 Returns the x-coordinate of the leftmost point of the segment. More...
 
double get_start_y () const
 Returns the y-coordinate of the leftmost point of the segment. More...
 
const kjb::Vectorget_start () const
 Returns the leftmost point of the segment. More...
 
double get_end_x () const
 Returns the x-coordinate of the rightmost point of the segment. More...
 
double get_end_y () const
 Returns the y-coordinate of the rightmost point of the segment. More...
 
const kjb::Vectorget_end () const
 Returns the rightmost point of the segment. More...
 
double get_slope () const
 Returns the slope of the line segment. More...
 
double get_y_intercept () const
 Returns the y-intercept of the line segment. More...
 
double get_x_intercept () const
 Returns the x-intercept of the line segment. More...
 
const Vectorget_line_params () const
 
const Lineget_line () const
 
double get_orientation () const
 Returns the orientation. More...
 
double get_length () const
 Returns the length. More...
 
void read (std::istream &in)
 Reads this Line segment from an input stream. More...
 
void write (std::ostream &out) const
 Writes this Line segment to an output stream. More...
 
void draw (kjb::Image &img, double ir, double ig, double ib, double width=1.0) const
 Draws this line segment. More...
 
void randomly_color (kjb::Image &img, double width=1.0) const
 Randomly colors this line segment on an image. More...
 
bool get_intersection (const Line_segment &line, kjb::Vector &point) const
 Finds the intersection point of the this line_segment and the input Line_segment line. Return false if these two lines are parallel and therefore do not intersect. More...
 
double get_distance_from_point (const kjb::Vector &point) const
 Returns distance between this line segment and the input point. More...
 
double get_angle_between_line (const Line_segment &line) const
 Returns the angle between this line segment and the input line. More...
 
double find_t (const kjb::Vector &point) const
 
double get_distance_from_point (const kjb::Vector &point, double *perp_dist) const
 Returns distance between this line segment and the input point. More...
 
double get_dx () const
 Returns the dx of this segment. More...
 
double get_dy () const
 Returns the dy of this segment. More...
 
bool is_horizontal () const
 Function object of testing whehter a line is horizontal. More...
 
bool is_vertical () const
 Function object of testing whehter a line is horizontal. More...
 
void get_direction (kjb::Vector &idirection) const
 Returns a vector representing the normalized direction of this line_segment. More...
 
bool is_line_segment_consistent ()
 
bool is_collinear (const Line_segment &ls, double collinear_threshold) const
 
- Public Member Functions inherited from kjb::Readable
virtual ~Readable ()
 Deletes this Readable. More...
 
virtual void read (const char *fname)
 Reads this Readable from a file. More...
 
- Public Member Functions inherited from kjb::Writeable
virtual ~Writeable ()
 Deletes this Writeable. More...
 
virtual void write (const char *fname) const
 Writes this Writeable to a file. More...
 

Static Public Member Functions

static std::pair< Vector, Vectorproject_line_segment_onto_line (const Line_segment &segment, const Line &iline)
 Project a line segment onto a line. More...
 
static bool project_line_segment_onto_line_segment (const Line_segment &segment_project, const Line_segment &segment_target, std::pair< Vector, Vector > &projected_points, double &length_inside, double &length_outside)
 Project a line segment onto a line segment. More...
 
static bool less_than (const Vector &point1, const Vector &point2)
 Returns true if point1.x < point2.x || (point1.x == point2.x && point1.y < point2.y. More...
 
static bool collision_detection (double start_1, double end_1, double start_2, double end_2, int &direction, double &delta)
 
static bool collision_detection_with_direction (double start_1, double end_1, double start_2, double end_2, int &direction, double &delta, int idirection)
 
static double get_overlap (double start_1, double end_1, double start_2, double end_2)
 
- Static Public Member Functions inherited from kjb::Readable
static const char * read_field_value (std::istream &in, const char *field_name, char *field_buf, size_t buf_len, char separator=':')
 Reads a line off in into a buffer and returns a pointer to the field value in the buffer. More...
 
static const char * read_field_value (std::istream &in, const char *field_name, char separator=':')
 Reads a line off in into an internal buffer and returns a pointer to the field value in the buffer. More...
 

Protected Member Functions

void compute_extrema ()
 Computes the extrema of this line segments from centre, orientation, and length. This information is redundant, but it can be very useful to have them pre-computed. More...
 
void compute_line_parameters ()
 

Protected Attributes

kjb::Vector centre
 X coordinate of the centre of this line segment. More...
 
double orientation
 Orientation of this line segment, defined as the angle between the x axis and the segment, positive angle meaning counter clockwise. Ranges between 0 and 180 degrees. Stored in radian. More...
 
double length
 length of the line segment More...
 
kjb::Vector start_point
 
kjb::Vector end_point
 
Line line
 

Static Protected Attributes

static Matrix _line_segment_rotation
 

Friends

std::ostream & operator<< (std::ostream &, const Line_segment &)
 

Detailed Description

Class to manipulate a line segment The class is parametrized in terms the position of the centre, its length and orientation. This is thus compatible with the output of the Berkeley edge detector. We store also the start point, the end point and the line parameters describing this line segment (for more details on how the line parameters work, please see the class line). This is redundant information, but it is convenient to have all these parameters precomputed and at hand.

Constructor & Destructor Documentation

kjb::Line_segment::Line_segment ( )
inline

Constructor without initializations.

kjb::Line_segment::Line_segment ( double  icentre_x,
double  icentre_y,
double  iorientation,
double  ilength 
)

Constructs a new Line_segment from centre, orientation, length.

Parameters
icentre_xthe x coordinate of the centre
icentre_ythe y coordinate of the centre
iorientationthe orientation of the segment, defined as the angle between the x axis and the segment, positive meaning counter clockwise rotation. The value will be converted to the range [0-180] degrees
ilengththe length of the segment
kjb::Line_segment::Line_segment ( const Vector start,
const Vector end 
)

Constructs a new Line_segment from a starting and ending point.

kjb::Line_segment::Line_segment ( std::istream &  in)

Constructs this line segment by reading from an input stream.

kjb::Line_segment::Line_segment ( const Line_segment ls)

Copy constructor.

Parameters
lsthe line segment to copy into this one

Member Function Documentation

bool kjb::Line_segment::collision_detection ( double  start_1,
double  end_1,
double  start_2,
double  end_2,
int &  direction,
double &  delta 
)
static
bool kjb::Line_segment::collision_detection_with_direction ( double  start_1,
double  end_1,
double  start_2,
double  end_2,
int &  direction,
double &  delta,
int  idirection 
)
static
void kjb::Line_segment::compute_extrema ( )
protected

Computes the extrema of this line segments from centre, orientation, and length. This information is redundant, but it can be very useful to have them pre-computed.

Computes the extrema from the line segment parameters. It initializes the extrema so that the segment is aligned with the x axis , centred int the origin and has the correct length. It then rotates it so that it has the correct orientation, and finally translates it to the reight position

void kjb::Line_segment::compute_line_parameters ( )
protected

Computes the slope and the y-axis intercept of this segment This information is redundant, but it can be very useful to have it pre-computed.

Computes the slope and the y-axis intercept of this segment

void kjb::Line_segment::draw ( kjb::Image img,
double  ir,
double  ig,
double  ib,
double  width = 1.0 
) const

Draws this line segment.

Draws the line segment on an image

Parameters
imgthe image
irthe red channel
igthe green channel
ibthe blue channel
widththe width of the segment to be drawn (in pixel)
double kjb::Line_segment::find_t ( const kjb::Vector point) const

If we interpret the end points p1 and p2 of this line segment as the two points defining the parametric equation of a line, given an input point p, this function returns the parameter t such that: p = p2 + t (p2 -p1) Notice that this works even if p is not on the line, its projection on it will be considered.

If we interpret the end points p1 and p2 of this line segment as the two points defining the parametric equation of a line, given an input point p, this function returns the parameter t such that: p = p2 + t (p2 -p1) Notice that this works even if p is not on the line, its projection on it will be considered.

Parameters
pointThe point to compute t for
double kjb::Line_segment::get_angle_between_line ( const Line_segment line_segment) const

Returns the angle between this line segment and the input line.

Calculate the angle between this line segment and the input line_segment.

Returns
the calculated angle
const kjb::Vector& kjb::Line_segment::get_centre ( ) const
inline

Returns the mid-point.

double kjb::Line_segment::get_centre_x ( ) const
inline

Returns the x-coordinate of the mid-point.

double kjb::Line_segment::get_centre_y ( ) const
inline

Returns the y-coordinate of the mid-point.

void kjb::Line_segment::get_direction ( kjb::Vector idirection) const

Returns a vector representing the normalized direction of this line_segment.

Returns a vector representing the normalized direction of this line_segment

Parameters
idirectionThis will contain the line segment direction
double kjb::Line_segment::get_distance_from_point ( const kjb::Vector point) const

Returns distance between this line segment and the input point.

Returns the distance between this line segment and the input point. There are two case: 1) The perpendicular projection of the point onto the line the segment lies on falls onto the line segment. In this case we return this perpendicular distance 2) Otherwise, we return the smallest distance between one of the segment end points and the input point

Parameters
pointthe input point
double kjb::Line_segment::get_distance_from_point ( const kjb::Vector point,
double *  perp_dist 
) const

Returns distance between this line segment and the input point.

Returns the distance between this line segment and the input point. There are two case: 1) The perpendicular projection of the point onto the line the segment lies on falls onto the line segment. In this case we return this perpendicular distance 2) Otherwise, we return the smallest distance between one of the segment end points and the input point

Parameters
pointthe input point
Returns
the perpendicular distance between the line the segment lies on and the point. If we are in case (1) these two distances are the same
double kjb::Line_segment::get_dx ( ) const
inline

Returns the dx of this segment.

double kjb::Line_segment::get_dy ( ) const
inline

Returns the dy of this segment.

const kjb::Vector& kjb::Line_segment::get_end ( ) const
inline

Returns the rightmost point of the segment.

double kjb::Line_segment::get_end_x ( ) const
inline

Returns the x-coordinate of the rightmost point of the segment.

double kjb::Line_segment::get_end_y ( ) const
inline

Returns the y-coordinate of the rightmost point of the segment.

bool kjb::Line_segment::get_intersection ( const Line_segment _line,
kjb::Vector point_p 
) const

Finds the intersection point of the this line_segment and the input Line_segment line. Return false if these two lines are parallel and therefore do not intersect.

Parameters
lineinput Line_segment
pointthe intersection point

Finds the intersection between the line this line_segment lies ont and the line the input line_segment lies on. Return false if these two lines are parallel and therefore do not intersect

Parameters
_lineThe input line_segment
point_pWill contain the intersection point
Returns
false if the lines do not intesect
double kjb::Line_segment::get_length ( ) const
inline

Returns the length.

const Line& kjb::Line_segment::get_line ( ) const
inline
const Vector& kjb::Line_segment::get_line_params ( ) const
inline
double kjb::Line_segment::get_orientation ( ) const
inline

Returns the orientation.

double kjb::Line_segment::get_overlap ( double  start_1,
double  end_1,
double  start_2,
double  end_2 
)
static
double kjb::Line_segment::get_slope ( ) const
inline

Returns the slope of the line segment.

If the segment is vertical, the slope tends to infinity.

const kjb::Vector& kjb::Line_segment::get_start ( ) const
inline

Returns the leftmost point of the segment.

double kjb::Line_segment::get_start_x ( ) const
inline

Returns the x-coordinate of the leftmost point of the segment.

double kjb::Line_segment::get_start_y ( ) const
inline

Returns the y-coordinate of the leftmost point of the segment.

double kjb::Line_segment::get_x_intercept ( ) const
inline

Returns the x-intercept of the line segment.

If the segment is horizontal there is not such interception.

double kjb::Line_segment::get_y_intercept ( ) const
inline

Returns the y-intercept of the line segment.

If the segment is vertical there is not such interception.

void kjb::Line_segment::init_from_centre_and_orientation ( double  icentre_x,
double  icentre_y,
double  iorientation,
double  ilength 
)
Parameters
icentre_xthe x coordinate of the centre
icentre_ythe y coordinate of the centre
iorientationthe orientation of the segment, defined as the angle between the x axis and the segment, positive meaning counter clockwise rotation. The value will be converted to the range [0-180] degrees
ilengththe length of the segment
void kjb::Line_segment::init_from_end_points ( double  x_1,
double  y_1,
double  x_2,
double  y_2 
)

Initialize a new Line_segment from position of its end points.

Parameters
x_1the x coordinate of the first end point
y_1the y coordinate of the first end point
x_2the x coordinate of the second end point
y_2the y coordinate of the second end point
void kjb::Line_segment::init_from_slope_and_intercept ( double  x_start,
double  x_end,
double  islope,
double  iintercept 
)

Initialize from x_start, x_end, slope, y_axis intercept.

Notice that a vertical segment cannot be initialized with this function.

Parameters
x_startthe x coordinate of the first endpoint
x_endthe x coordinate of the second endpoint
imslope of the line segment
iby intercept
void kjb::Line_segment::init_vertical_segment ( double  y_start,
double  y_end,
double  x 
)

Initialize a vertical Line_segment from y_start, y_end, x coord.

This function inits this line segment as a vertical segment defined by the x location and a start and end point along the y direction.

Parameters
y_startthe y coordinate of the bottom of this segment
y_endthe y coordinate of the first endpoint
xthe x coordinate of the second endpoint
bool kjb::Line_segment::is_collinear ( const Line_segment ls,
double  collinear_threshold 
) const
bool kjb::Line_segment::is_horizontal ( ) const
inline

Function object of testing whehter a line is horizontal.

bool kjb::Line_segment::is_line_segment_consistent ( )

For debug purposes, checks this line segment is consistent

For debugging purposes. It checks that the line segment parameters are consistent

bool kjb::Line_segment::is_vertical ( ) const
inline

Function object of testing whehter a line is horizontal.

bool kjb::Line_segment::less_than ( const Vector point1,
const Vector point2 
)
static

Returns true if point1.x < point2.x || (point1.x == point2.x && point1.y < point2.y.

bool kjb::Line_segment::operator< ( const Line_segment ls) const
inline
Line_segment & kjb::Line_segment::operator= ( const Line_segment ls)

Assignment operator.

Parameters
lsthe line segment to assign to this one
bool kjb::Line_segment::operator== ( const Line_segment ls) const
Parameters
lsthe line segment to compare to this one
bool kjb::Line_segment::point_outside_segment ( const Vector point) const

Check whether point is inside the ending points of this segment Note point is collinear with this line segment.

std::pair< Vector, Vector > kjb::Line_segment::project_line_segment_onto_line ( const Line_segment segment,
const Line iline 
)
static

Project a line segment onto a line.

Returns
The projected two ending points of the line segment
std::pair<Vector, Vector> The projected two ending points of the line segment

(not fully tested)

bool kjb::Line_segment::project_line_segment_onto_line_segment ( const Line_segment segment_project,
const Line_segment segment_target,
std::pair< Vector, Vector > &  projected_points,
double &  length_inside,
double &  length_outside 
)
static

Project a line segment onto a line segment.

Parameters
segment_projectThe line segment that is being projected
segment_targetThe line segment that is being projected to
projected_pointsThe projected two ending points of the line segment.
Returns
True If both projected_points are inside the ending points of segment_target, False If one or both the projected_points are outside the ending points the segment_target.
Parameters
segment_projectThe line segment that is being projected
segment_targetThe line segment that is being projected to
projected_pointsThe projected two ending points of the line segment
Returns
True If both projected_points are inside the ending points of segment_target, False If one or both the projected_points are outside the ending points the segment_target
void kjb::Line_segment::randomly_color ( kjb::Image img,
double  width = 1.0 
) const

Randomly colors this line segment on an image.

Draws the line segment using a random color

Parameters
imgthe image
widththe width of the segment to be drawn (in pixel)
void kjb::Line_segment::read ( std::istream &  in)
virtual

Reads this Line segment from an input stream.

Reads this Line_segment from an input stream.

Implements kjb::Readable.

Reimplemented in kjb::Model_edge.

void kjb::Line_segment::write ( std::ostream &  out) const
virtual

Writes this Line segment to an output stream.

Writes this Line_segment to an output stream.

Implements kjb::Writeable.

Reimplemented in kjb::Model_edge.

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  ,
const Line_segment  
)
friend

Member Data Documentation

Matrix kjb::Line_segment::_line_segment_rotation
staticprotected
kjb::Vector kjb::Line_segment::centre
protected

X coordinate of the centre of this line segment.

kjb::Vector kjb::Line_segment::end_point
protected
double kjb::Line_segment::length
protected

length of the line segment

Line kjb::Line_segment::line
protected

Line parameters. The following parameters are redundant, but it can be helpful to have them pre-computed.

double kjb::Line_segment::orientation
protected

Orientation of this line segment, defined as the angle between the x axis and the segment, positive angle meaning counter clockwise. Ranges between 0 and 180 degrees. Stored in radian.

kjb::Vector kjb::Line_segment::start_point
protected

Start and end point of the line segment. The following fields are redundant, but it can be helpful to have them pre-computed.


The documentation for this class was generated from the following files: