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

#include <gr_polygon.h>

Inheritance diagram for kjb::Polygon:
kjb::Abstract_renderable kjb::Readable kjb::Writeable kjb::Renderable kjb::Solid_renderable kjb::Wire_renderable kjb::Wire_occlude_renderable

Public Member Functions

 Polygon ()
 Constructs a polygon with zero vertices. More...
 
 Polygon (unsigned int N)
 Constructs a polygon with N vertices. More...
 
 Polygon (const Polygon &p)
 
 Polygon (const char *fname) throw (Illegal_argument, IO_error)
 Constructs a polygon by reading it from a file. More...
 
 Polygon (std::istream &in) throw (Illegal_argument, IO_error)
 Constructs a polygon by reading it from an input stream. More...
 
virtual ~Polygon ()
 
virtual Polygonoperator= (const Polygon &p)
 
virtual Polygonclone () const
 
virtual void read (std::istream &in) throw (Illegal_argument, IO_error)
 
virtual void write (std::ostream &out) const throw (IO_error)
 
virtual void transform (const Matrix &M) throw (Illegal_argument)
 transforms all the vertices by the input matrix More...
 
void add_point (const Vector &pt) throw (Illegal_argument)
 Adds a point to this polygon. More...
 
void add_point (double x, double y, double z) throw (Illegal_argument)
 Adds a point to this polygon. More...
 
void set_point (double index, double x, double y, double z) throw (Index_out_of_bounds)
 Resets one of the points in this polygon. More...
 
const Vectorget_normal () const
 Returns the normal of this polygon. More...
 
void clear ()
 Clears all the vertices. More...
 
const Vectorget_centroid () const
 returns the centroid of this polygon More...
 
unsigned int get_num_points () const
 returns the number of vertices of this polygon More...
 
const Vectorget_point (unsigned int i) const throw (Illegal_argument)
 returns the ith point of this polygon More...
 
const std::vector< Vector > & get_vertices () const
 returns the vector of vertices More...
 
void flip_normal ()
 Flips the normal of this polygon. More...
 
const Vectorget_edge_first_vertex (unsigned int edge) const throw (Index_out_of_bounds)
 This function is useful when we index a polygon by its edges. In a Polygon with n vertices, 0 is the edge between the first and the second point, n-1 is the edge between the last and the first point. Given an edge index, this function returns the first vertex forming this edge using this convention. More...
 
const Vectorget_edge_second_vertex (unsigned int edge) const throw (Index_out_of_bounds)
 This function is useful when we index a polygon by its edges. In a Polygon with n vertices, 0 is the edge between the first and the second point, n-1 is the edge between the last and the first point. Given an edge index, this function returns the second vertex forming this edge using this convention. More...
 
virtual void wire_render () const
 Renders this polygon as a wireframe. More...
 
virtual void wire_occlude_render () const
 Renders this polygon in the depth buffer. More...
 
virtual void solid_render () const
 Renders this polygon as a solid. More...
 
virtual void project ()
 Projects all the vertices in this polygon onto the image plane. More...
 
virtual void project (const Matrix &M, double width, double height)
 
void fit_plane (Vector &plane_params) const
 Finds the coefficients of the plane that this polygon lies in. More...
 
bool check_convexity () const
 Checks that this polygon is convex. More...
 
double compute_area () const
 Computes the area of this polygon if it is convex. More...
 
void get_all_vertices (std::vector< Vector > &vertices) const
 Stores all of the vertices of this polygon in a vector. More...
 
bool check_polygon_is_right_triangle (double tolerance) const
 Checks if this polygon is a right triangle. More...
 
int get_index_of_longest_edge () const
 Returns the index of the longest edge in the polygon. More...
 
void update ()
 Updates the normal and the centroid of the polygon according to the vertices. More...
 
void get_lines (std::vector< Line3d > &lines) const
 
- Public Member Functions inherited from kjb::Abstract_renderable
virtual ~Abstract_renderable ()
 Deletes this Solid_renderable. More...
 
virtual void render () const
 Renders this object with GL. More...
 
virtual void render_occluded_wireframe () const
 Renders this object with GL as an occluded wire-frame into the depth buffer, to hide unseen lines. More...
 
virtual void silhouette_render (const kjb::Base_gl_interface &, double width) const
 Renders the silhouette of this object. The most basic implementation is to render the occluded wireframe. More...
 
- Public Member Functions inherited from kjb::Renderable
virtual ~Renderable ()
 Deletes this Renderable. More...
 
- Public Member Functions inherited from kjb::Solid_renderable
virtual ~Solid_renderable ()
 Deletes this Wire_renderable. More...
 
- Public Member Functions inherited from kjb::Wire_renderable
virtual ~Wire_renderable ()
 Deletes this Wire_renderable. More...
 
- Public Member Functions inherited from kjb::Wire_occlude_renderable
virtual ~Wire_occlude_renderable ()
 Deletes this Wire_occlude_renderable. More...
 
- 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...
 

Protected Member Functions

void update_normal ()
 Recalculates the normal of this polygon. The first three vertices are used. Notice that this is very important, because it determines a sign convention. More...
 
void update_centroid ()
 Recalculates the centroid of this polygon. More...
 

Protected Attributes

std::vector< Vectorpts
 
Vector normal
 
Vector centroid
 
bool normal_flipped
 

Friends

class GL_Polygon_Renderer
 

Additional Inherited Members

- Static Public Member Functions inherited from kjb::Abstract_renderable
static double get_rendering_framework ()
 returns the rendering framework used to render. The only one implemented up to now is OpenGL More...
 
static void set_rendering_framework (unsigned int irf)
 sets the rendering framework used to render. The only one implemented up to now is OpenGL More...
 
- 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 Types inherited from kjb::Abstract_renderable
enum  Rendering_frameworks { RI_OPENGL }
 
- Static Protected Attributes inherited from kjb::Abstract_renderable
static unsigned int _rendering_framework = kjb::Abstract_renderable::RI_OPENGL
 

Constructor & Destructor Documentation

Polygon::Polygon ( )

Constructs a polygon with zero vertices.

The constructed polygon has no points.

Polygon::Polygon ( unsigned int  N)

Constructs a polygon with N vertices.

The constructed polygon has no points, but space is reserved for N of them.

Parameters
NNumber of points that the polygon will contain.
Polygon::Polygon ( const Polygon p)
Parameters
pPolygon to copy into this one.
Polygon::Polygon ( const char *  fname)
throw (Illegal_argument,
IO_error
)

Constructs a polygon by reading it from a file.

The file format is big-endian binary.

Parameters
fnameInput file to read this polygon from.
Exceptions
kjb::IO_errorCould not read from in.
kjb::Illegal_argumentInvalid arguments in file to read from.
Polygon::Polygon ( std::istream &  in)
throw (Illegal_argument,
IO_error
)

Constructs a polygon by reading it from an input stream.

The file format is big-endian binary.

Parameters
inInput stream to read this polygon from.
Exceptions
kjb::IO_errorCould not read from in.
kjb::Illegal_argumentInvalid arguments in file to read from.
Polygon::~Polygon ( )
virtual

Frees all space allocated by this polygon.

Member Function Documentation

void Polygon::add_point ( const Vector pt)
throw (Illegal_argument
)

Adds a point to this polygon.

The point to add must be in homogeneous coordinates (x,y,z,w) and be in the plane containing the points already in the polygon.

Parameters
ptPoint to add.
void Polygon::add_point ( double  x,
double  y,
double  z 
)
throw (Illegal_argument
)

Adds a point to this polygon.

The point to add must be in the plane containing the points already in the polygon.

Parameters
xX-coord of the point to add.
yY-coord of the point to add.
zY-coord of the point to add.
bool Polygon::check_convexity ( ) const

Checks that this polygon is convex.

Checks that the polygon is convex. A polygon is convex if all of its interior angles are less than 180 degrees.

Returns
true if convex, false otherwise.
Exceptions
KJB_errorPolygon has fewer than 3 points.
bool Polygon::check_polygon_is_right_triangle ( double  tolerance) const

Checks if this polygon is a right triangle.

Checks whether or not the polygon is a right triangle.

tolerance one of the angles must be within the range (pi/2 - tolerance, pi/2 + tolerance) to be considered a right angle.

Returns
true if it is a right triangle, false otherwise.
void kjb::Polygon::clear ( )
inline

Clears all the vertices.

Polygon * Polygon::clone ( ) const
virtual
Returns
A new copy of this polygon.
double Polygon::compute_area ( ) const

Computes the area of this polygon if it is convex.

Computes the area of the polygon if it is convex.

Returns
the area of the polygon.
Exceptions
KJB_errorPolygon is not convex.
void Polygon::fit_plane ( Vector plane_params) const

Finds the coefficients of the plane that this polygon lies in.

Finds the coefficients of the plane of the form ax + by + cz + d = 0 that the polygon lies in. The coefficients are stored in the input vector.

plane_params Vector to store the plane coefficients in.

Exceptions
KJB_errorPolygon has fewer than 3 points.
void Polygon::flip_normal ( )

Flips the normal of this polygon.

The normal vector is created by crossing a vector made from the first two points with a vector made with the first and last points. This function flips ths order of that operation and reverses the direction of the normal.

void Polygon::get_all_vertices ( std::vector< Vector > &  vertices) const

Stores all of the vertices of this polygon in a vector.

vertices A vector of Vectors to store the vertex points of the polygon in.

const Vector& kjb::Polygon::get_centroid ( ) const
inline

returns the centroid of this polygon

const Vector& kjb::Polygon::get_edge_first_vertex ( unsigned int  edge) const
throw (Index_out_of_bounds
)
inline

This function is useful when we index a polygon by its edges. In a Polygon with n vertices, 0 is the edge between the first and the second point, n-1 is the edge between the last and the first point. Given an edge index, this function returns the first vertex forming this edge using this convention.

const Vector& kjb::Polygon::get_edge_second_vertex ( unsigned int  edge) const
throw (Index_out_of_bounds
)
inline

This function is useful when we index a polygon by its edges. In a Polygon with n vertices, 0 is the edge between the first and the second point, n-1 is the edge between the last and the first point. Given an edge index, this function returns the second vertex forming this edge using this convention.

int Polygon::get_index_of_longest_edge ( ) const

Returns the index of the longest edge in the polygon.

Returns
an int representing the index of the longest edge in the polygon.
void Polygon::get_lines ( std::vector< Line3d > &  lines) const
const Vector& kjb::Polygon::get_normal ( ) const
inline

Returns the normal of this polygon.

unsigned int kjb::Polygon::get_num_points ( ) const
inline

returns the number of vertices of this polygon

const Vector& kjb::Polygon::get_point ( unsigned int  i) const
throw (Illegal_argument
)
inline

returns the ith point of this polygon

const std::vector<Vector>& kjb::Polygon::get_vertices ( ) const
inline

returns the vector of vertices

Polygon & Polygon::operator= ( const Polygon p)
virtual

Performs a deep copy of the points in p into this polygon.

Parameters
pPolygon to copy into this one.
Returns
A reference to this polygon.
void Polygon::project ( )
virtual

Projects all the vertices in this polygon onto the image plane.

void Polygon::project ( const Matrix M,
double  width,
double  height 
)
virtual
void Polygon::read ( std::istream &  in)
throw (Illegal_argument,
IO_error
)
virtual
Parameters
inInput stream to read the members of this polygon from.
Exceptions
kjb::IO_errorCould not read from in.
kjb::Illegal_argumentInvalid arguments to read from the file.

Implements kjb::Readable.

void kjb::Polygon::set_point ( double  index,
double  x,
double  y,
double  z 
)
throw (Index_out_of_bounds
)
inline

Resets one of the points in this polygon.

void Polygon::solid_render ( ) const
virtual

Renders this polygon as a solid.

Implements kjb::Solid_renderable.

void Polygon::transform ( const Matrix M)
throw (Illegal_argument
)
virtual

transforms all the vertices by the input matrix

Parameters
MHomogeneous transformation matrix to transform this polygon by.
void kjb::Polygon::update ( )
inline

Updates the normal and the centroid of the polygon according to the vertices.

void Polygon::update_centroid ( )
protected

Recalculates the centroid of this polygon.

Does nothing if this polygon has zero points. Otherwise computes the centroid of the polygon

void Polygon::update_normal ( )
protected

Recalculates the normal of this polygon. The first three vertices are used. Notice that this is very important, because it determines a sign convention.

Does nothing if this polygon has fewer than 3 points. The update is done by crossing the vectors created from the first, second and third points in this polygon.

void Polygon::wire_occlude_render ( ) const
virtual

Renders this polygon in the depth buffer.

Implements kjb::Wire_occlude_renderable.

void Polygon::wire_render ( ) const
virtual

Renders this polygon as a wireframe.

Rendering

Implements kjb::Wire_renderable.

void Polygon::write ( std::ostream &  out) const
throw (IO_error
)
virtual

The file format is big-endian binary.

Parameters
outOutput stream to write the members of this polygon to.
Exceptions
kjb::IO_errorCould not write to out.

Implements kjb::Writeable.

Friends And Related Function Documentation

friend class GL_Polygon_Renderer
friend

Member Data Documentation

Vector kjb::Polygon::centroid
protected
Vector kjb::Polygon::normal
protected
bool kjb::Polygon::normal_flipped
protected
std::vector<Vector> kjb::Polygon::pts
protected

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