KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gr_polygon.h
Go to the documentation of this file.
1 
2 /* $Id: gr_polygon.h 18278 2014-11-25 01:42:10Z ksimek $ */
3 
4 /* =========================================================================== *
5 |
6 | Copyright (c) 1994-2008 by Kobus Barnard (author).
7 |
8 | Personal and educational use of this code is granted, provided that this
9 | header is kept intact, and that the authorship is not misrepresented, that
10 | its use is acknowledged in publications, and relevant papers are cited.
11 |
12 | For other use contact the author (kobus AT cs DOT arizona DOT edu).
13 |
14 | Please note that the code in this file has not necessarily been adequately
15 | tested. Naturally, there is no guarantee of performance, support, or fitness
16 | for any particular task. Nonetheless, I am interested in hearing about
17 | problems that you encounter.
18 |
19 * =========================================================================== */
20 
21 #ifndef POLYGON_H_INCLUDED
22 #define POLYGON_H_INCLUDED
23 
24 #include <vector>
25 #include "m_cpp/m_matrix.h"
26 #include "m_cpp/m_vector.h"
27 #include <gr_cpp/gr_renderable.h>
28 
29 #include "l_cpp/l_readable.h"
30 #include "l_cpp/l_writeable.h"
31 
32 namespace kjb {
33 
34 typedef std::pair<kjb::Vector, kjb::Vector> Line3d;
35 
36 class Polygon : public Abstract_renderable, public Readable, public Writeable
37 {
38 public:
39 
41  Polygon();
42 
44  Polygon(unsigned int N);
45 
46  Polygon(const Polygon& p);
47 
49  Polygon(const char* fname) throw (Illegal_argument, IO_error);
50 
52  Polygon(std::istream& in) throw (Illegal_argument, IO_error);
53 
54  virtual ~Polygon();
55 
56  virtual Polygon& operator=(const Polygon& p);
57 
58  virtual Polygon* clone() const;
59 
60  virtual void read(std::istream& in) throw (Illegal_argument, IO_error);
61 
62  virtual void write(std::ostream& out) const throw (IO_error);
63 
65  virtual void transform(const Matrix& M) throw (Illegal_argument);
66 
68  void add_point(const Vector& pt) throw (Illegal_argument);
69 
71  void add_point(double x, double y, double z) throw (Illegal_argument);
72 
74  inline void set_point(double index, double x, double y, double z) throw(Index_out_of_bounds)
75  {
76  (pts[index])(0) = x;
77  (pts[index])(1) = y;
78  (pts[index])(2) = z;
79  }
80 
82  inline const Vector& get_normal() const
83  {
84  return normal;
85  }
86 
88  inline void clear() {pts.clear();}
89 
91  inline const Vector& get_centroid() const
92  {
93  return centroid;
94  }
95 
97  inline unsigned int get_num_points() const
98  {
99  return pts.size();
100  }
101 
103  inline const Vector& get_point(unsigned int i) const throw (Illegal_argument)
104  {
105  if(i >= pts.size())
106  {
108  }
109 
110  return pts[i];
111  }
112 
114  inline const std::vector<Vector> & get_vertices() const
115  {
116  return pts;
117  }
118 
120  void flip_normal();
121 
127  inline const Vector& get_edge_first_vertex(unsigned int edge) const throw (Index_out_of_bounds)
128  {
129  if(edge >= get_num_points())
130  {
131  throw Index_out_of_bounds("Edge index out of bounds, cannot get first vertex");
132  }
133 
134  return pts[edge];
135  }
136 
142  inline const Vector& get_edge_second_vertex(unsigned int edge) const throw (Index_out_of_bounds)
143  {
144  unsigned int n_pts = get_num_points();
145  if(edge >= n_pts)
146  {
147  throw Index_out_of_bounds("Edge index out of bounds, cannot get second vertex");
148  }
149 
150  return pts[ ( ( edge + 1 ) % n_pts ) ];
151  }
152 
155  virtual void wire_render() const;
157  virtual void wire_occlude_render() const;
159  virtual void solid_render() const;
161  virtual void project();
162 
163  virtual void project(const Matrix & M, double width, double height);
164 
166  void fit_plane(Vector & plane_params) const;
167 
169  bool check_convexity() const;
170 
172  double compute_area() const;
173 
175  void get_all_vertices(std::vector<Vector> & vertices) const;
176 
178  bool check_polygon_is_right_triangle(double tolerance) const;
179 
181  int get_index_of_longest_edge() const;
182 
184  void update()
185  {
186  update_normal();
187  update_centroid();
188  }
189 
190  void get_lines(std::vector<Line3d> & lines) const;
191 
192 protected:
193 
196  void update_normal();
197 
199  void update_centroid();
200 
201 protected:
202 
203  std::vector<Vector> pts;
206 
207  //Used to determine whether the normal of this polygon is flipped
209 
210 private:
211 
212  friend class GL_Polygon_Renderer;
213 };
214 
215 }
216 
217 #endif
218 
unsigned int get_num_points() const
returns the number of vertices of this polygon
Definition: gr_polygon.h:97
Abstract class to write this object to an output stream.
Definition: l_writeable.h:41
virtual void wire_render() const
Renders this polygon as a wireframe.
Definition: gr_polygon.cpp:646
Definition for the Matrix class, a thin wrapper on the KJB Matrix struct and its related functionalit...
bool normal_flipped
Definition: gr_polygon.h:208
function lines
Definition: APPgetLargeConnectedEdges.m:1
double compute_area() const
Computes the area of this polygon if it is convex.
Definition: gr_polygon.cpp:821
Abstract class to read this object from an input stream.
Definition: l_readable.h:39
const Vector & get_edge_second_vertex(unsigned int edge) const
This function is useful when we index a polygon by its edges. In a Polygon with n vertices...
Definition: gr_polygon.h:142
virtual Polygon * clone() const
Definition: gr_polygon.cpp:144
Object thrown when an index argument exceeds the size of a container.
Definition: l_exception.h:399
height
Definition: APPgetLargeConnectedEdges.m:33
#define KJB_THROW(ex)
Definition: l_exception.h:46
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
int get_index_of_longest_edge() const
Returns the index of the longest edge in the polygon.
Definition: gr_polygon.cpp:934
bool check_convexity() const
Checks that this polygon is convex.
Definition: gr_polygon.cpp:757
virtual void project()
Projects all the vertices in this polygon onto the image plane.
Definition: gr_polygon.cpp:688
void get_lines(std::vector< Line3d > &lines) const
Definition: gr_polygon.cpp:959
const Vector & get_normal() const
Returns the normal of this polygon.
Definition: gr_polygon.h:82
Abstract class to render this object with GL.
Definition: gr_renderable.h:151
bool check_polygon_is_right_triangle(double tolerance) const
Checks if this polygon is a right triangle.
Definition: gr_polygon.cpp:877
Vector centroid
Definition: gr_polygon.h:205
virtual void read(std::istream &in)
Definition: gr_polygon.cpp:156
void fit_plane(Vector &plane_params) const
Finds the coefficients of the plane that this polygon lies in.
Definition: gr_polygon.cpp:725
x
Definition: APPgetLargeConnectedEdges.m:100
const Vector & get_centroid() const
returns the centroid of this polygon
Definition: gr_polygon.h:91
Definition: gr_polygon_renderer.h:32
Polygon()
Constructs a polygon with zero vertices.
Definition: gr_polygon.cpp:31
virtual void transform(const Matrix &M)
transforms all the vertices by the input matrix
Definition: gr_polygon.cpp:413
virtual void wire_occlude_render() const
Renders this polygon in the depth buffer.
Definition: gr_polygon.cpp:660
Definition: gr_polygon.h:36
virtual void write(std::ostream &out) const
Definition: gr_polygon.cpp:286
void update()
Updates the normal and the centroid of the polygon according to the vertices.
Definition: gr_polygon.h:184
void get_all_vertices(std::vector< Vector > &vertices) const
Stores all of the vertices of this polygon in a vector.
Definition: gr_polygon.cpp:860
std::vector< Vector > pts
Definition: gr_polygon.h:203
Vector normal
Definition: gr_polygon.h:204
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
virtual ~Polygon()
Definition: gr_polygon.cpp:95
std::pair< kjb::Vector, kjb::Vector > Line3d
Definition: gr_polygon.h:34
void add_point(const Vector &pt)
Adds a point to this polygon.
Definition: gr_polygon.cpp:462
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
void flip_normal()
Flips the normal of this polygon.
Definition: gr_polygon.cpp:506
Object thrown when input or output fails.
Definition: l_exception.h:496
const Vector & get_edge_first_vertex(unsigned int edge) const
This function is useful when we index a polygon by its edges. In a Polygon with n vertices...
Definition: gr_polygon.h:127
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
const Vector & get_point(unsigned int i) const
returns the ith point of this polygon
Definition: gr_polygon.h:103
virtual Polygon & operator=(const Polygon &p)
Definition: gr_polygon.cpp:107
const std::vector< Vector > & get_vertices() const
returns the vector of vertices
Definition: gr_polygon.h:114
void set_point(double index, double x, double y, double z)
Resets one of the points in this polygon.
Definition: gr_polygon.h:74
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
virtual void solid_render() const
Renders this polygon as a solid.
Definition: gr_polygon.cpp:674
void update_centroid()
Recalculates the centroid of this polygon.
Definition: gr_polygon.cpp:554
void clear()
Clears all the vertices.
Definition: gr_polygon.h:88
void update_normal()
Recalculates the normal of this polygon. The first three vertices are used. Notice that this is very ...
Definition: gr_polygon.cpp:519