JWS C++ Library
C++ language utility library
polygon.h
Go to the documentation of this file.
00001 /*
00002  * This work is licensed under a Creative Commons 
00003  * Attribution-Noncommercial-Share Alike 3.0 United States License.
00004  * 
00005  *    http://creativecommons.org/licenses/by-nc-sa/3.0/us/
00006  * 
00007  * You are free:
00008  * 
00009  *    to Share - to copy, distribute, display, and perform the work
00010  *    to Remix - to make derivative works
00011  * 
00012  * Under the following conditions:
00013  * 
00014  *    Attribution. You must attribute the work in the manner specified by the
00015  *    author or licensor (but not in any way that suggests that they endorse you
00016  *    or your use of the work).
00017  * 
00018  *    Noncommercial. You may not use this work for commercial purposes.
00019  * 
00020  *    Share Alike. If you alter, transform, or build upon this work, you may
00021  *    distribute the resulting work only under the same or similar license to
00022  *    this one.
00023  * 
00024  * For any reuse or distribution, you must make clear to others the license
00025  * terms of this work. The best way to do this is by including this header.
00026  * 
00027  * Any of the above conditions can be waived if you get permission from the
00028  * copyright holder.
00029  * 
00030  * Apart from the remix rights granted under this license, nothing in this
00031  * license impairs or restricts the author's moral rights.
00032  */
00033 
00034 
00047 #ifndef POLYGON_H
00048 #define POLYGON_H
00049 
00050 
00051 #include <jwsc++/config.h>
00052 
00053 #include <iostream>
00054 #include <vector>
00055 
00056 #include <inttypes.h>
00057 
00058 #include <jwsc/vector/vector.h>
00059 #include <jwsc/matrix/matrix.h>
00060 
00061 #include "jwsc++/base/cloneable.h"
00062 #include "jwsc++/base/readable.h"
00063 #include "jwsc++/base/writeable.h"
00064 #include "jwsc++/base/exception.h"
00065 #include "jwsc++/graphics/transformable.h"
00066 #include "jwsc++/graphics/renderable.h"
00067 #include "jwsc++/graphics/camera.h"
00068 
00069 
00070 namespace jwscxx {
00071 namespace graphics {
00072 
00073 
00082 class Polygon_f : public jwscxx::base::Cloneable, 
00083                   public jwscxx::base::Readable, 
00084                   public jwscxx::base::Writeable, 
00085                   public Transformable_f, 
00086                   public Wire_renderable, 
00087                   public Wire_occlude_renderable,
00088                   public Solid_renderable
00089 {
00090     public:
00091 
00093         Polygon_f();
00094 
00095 
00097         Polygon_f(uint32_t N);
00098 
00099 
00101         Polygon_f(const Polygon_f& p);
00102 
00103 
00105         Polygon_f(const char* fname) throw (jwscxx::base::Arg_error,
00106                 jwscxx::base::IO_error);
00107 
00108 
00110         Polygon_f(std::istream& in) throw (jwscxx::base::Arg_error,
00111                 jwscxx::base::IO_error);
00112 
00113 
00115         virtual ~Polygon_f();
00116 
00117 
00119         virtual Polygon_f& operator= (const Polygon_f& p);
00120 
00121 
00123         virtual Polygon_f* clone() const;
00124 
00125 
00127         virtual void read(std::istream& in) throw (jwscxx::base::IO_error,
00128                 jwscxx::base::Arg_error);
00129 
00130 
00132         virtual void write(std::ostream& out) const
00133             throw (jwscxx::base::IO_error);
00134 
00135 
00137         virtual void transform(const jwsc::Matrix_f* M) 
00138             throw (jwscxx::base::Arg_error);
00139 
00140         
00142         virtual void wire_render() const;
00143 
00144 
00149         virtual void wire_occlude_render() const;
00150 
00151         
00161         virtual void wire_occluded_silhouette_render(std::vector<bool> & iVector, bool & iBool) const;
00162 
00163 
00165         virtual void solid_render() const;
00166 
00167 
00172         void add_point(jwsc::Vector_f* pt) throw (jwscxx::base::Arg_error);
00173 
00174 
00179         void add_point(float x, float y, float z) 
00180             throw (jwscxx::base::Arg_error);
00181 
00182 
00184         const jwsc::Vector_f* get_normal() const;
00185 
00186 
00188         const jwsc::Vector_f* get_centroid() const;
00189 
00190 
00192         size_t get_num_points() const;
00193 
00194 
00196         const jwsc::Vector_f* get_point(size_t i) const 
00197             throw (jwscxx::base::Arg_error);
00198 
00199 
00201         void flip_normal();
00202 
00203 
00205         void project();
00206 
00207 
00209         uint8_t is_visible(const Camera_f* camera) const;
00210 
00211 
00219         uint8_t is_visible_up_to_epsilon(const Camera_d* camera, float & epsilon) const;
00220 
00221 
00223         uint32_t get_id() const;
00224 
00225 
00227         void set_id(uint32_t id);
00228 
00229 
00230     protected:
00231 
00233         void update_normal();
00234 
00236         void update_centroid();
00237 
00238 
00239     protected:
00240 
00242         std::vector<jwsc::Vector_f*> pts;
00243 
00245         jwsc::Vector_f* normal;
00246 
00248         jwsc::Vector_f* centroid;
00249 
00254         uint8_t normal_flipped;
00255 
00257         uint32_t id;
00258 
00259 
00260     private:
00261 
00263         static uint32_t generate_id();
00264 
00265 };
00266 
00267 
00276 class Polygon_d : public jwscxx::base::Cloneable, 
00277                   public jwscxx::base::Readable, 
00278                   public jwscxx::base::Writeable, 
00279                   public Transformable_d, 
00280                   public Wire_renderable, 
00281                   public Wire_occlude_renderable,
00282                   public Solid_renderable
00283 {
00284     public:
00285 
00287         Polygon_d();
00288 
00289 
00291         Polygon_d(uint32_t N);
00292 
00293 
00295         Polygon_d(const Polygon_d& p);
00296 
00297 
00299         Polygon_d(const char* fname) throw (jwscxx::base::Arg_error,
00300                 jwscxx::base::IO_error);
00301 
00302 
00304         Polygon_d(std::istream& in) throw (jwscxx::base::Arg_error,
00305                 jwscxx::base::IO_error);
00306 
00307 
00309         virtual ~Polygon_d();
00310 
00311 
00313         virtual Polygon_d& operator= (const Polygon_d& p);
00314 
00315 
00317         virtual Polygon_d* clone() const;
00318 
00319 
00321         virtual void read(std::istream& in) throw (jwscxx::base::IO_error,
00322                 jwscxx::base::Arg_error);
00323 
00324 
00326         virtual void write(std::ostream& out) const
00327             throw (jwscxx::base::IO_error);
00328 
00329 
00331         virtual void transform(const jwsc::Matrix_d* M) 
00332             throw (jwscxx::base::Arg_error);
00333 
00334         
00336         virtual void wire_render() const;
00337 
00338 
00343         virtual void wire_occlude_render() const;
00344 
00354         virtual void wire_occluded_silhouette_render(std::vector<bool> & iVector, bool & iBool) const;
00355 
00356 
00358         virtual void solid_render() const;
00359 
00360 
00365         void add_point(jwsc::Vector_d* pt) throw (jwscxx::base::Arg_error);
00366 
00367 
00372         void add_point(double x, double y, double z) 
00373             throw (jwscxx::base::Arg_error);
00374 
00375 
00377         const jwsc::Vector_d* get_normal() const;
00378 
00379 
00381         const jwsc::Vector_d* get_centroid() const;
00382 
00383 
00385         size_t get_num_points() const;
00386 
00387 
00389         const jwsc::Vector_d* get_point(size_t i) const 
00390             throw (jwscxx::base::Arg_error);
00391 
00392 
00394         void flip_normal();
00395 
00396 
00398         void project();
00399 
00400 
00402         uint8_t is_visible(const Camera_d* camera) const;
00403 
00404      
00412         uint8_t is_visible_up_to_epsilon(const Camera_d* camera, double & epsilon) const;
00413 
00414 
00416         uint32_t get_id() const;
00417 
00418 
00420         void set_id(uint32_t id);
00421 
00422 
00423     protected:
00424 
00426         void update_normal();
00427 
00429         void update_centroid();
00430 
00431 
00432     protected:
00433 
00435         std::vector<jwsc::Vector_d*> pts;
00436 
00438         jwsc::Vector_d* normal;
00439 
00441         jwsc::Vector_d* centroid;
00442 
00447         uint8_t normal_flipped;
00448 
00450         uint32_t id;
00451 
00452 
00453     private:
00454 
00456         static uint32_t generate_id();
00457 
00458 };
00459 
00460 
00461 }}
00462 
00463 
00464 #endif