src/screen.hpp

Go to the documentation of this file.
00001 //
00002 // Ephi - simulation of magnetic fields and particles
00003 // Copyright (C) 2007 Indrek Mandre <indrek(at)mare.ee>
00004 // For more information please see http://www.mare.ee/indrek/ephi/
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License along
00017 // with this program; if not, write to the Free Software Foundation, Inc.,
00018 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019 //
00020 
00021 #ifndef __screen_hpp__
00022 #define __screen_hpp__
00023 
00024 #include <string>
00025 
00026 #include "math3d.hpp"
00027 
00029 struct color3
00030 {
00031   color3() { }
00032   color3(const vect3d& v) :
00033       b((unsigned char)prec2double(v.z * 255)),
00034       g((unsigned char)prec2double(v.y * 255)),
00035       r((unsigned char)prec2double(v.x * 255)) { }
00036   color3(unsigned char r, unsigned char g, unsigned char b) : b(b), g(g), r(r) { }
00037 
00038   friend bool operator== (const color3&l, const color3&r) { return l.r == r.r && l.g == r.g && l.b == r.b; }
00039   friend bool operator!= (const color3&l, const color3&r) { return !(l == r); }
00040 
00041   unsigned char b, g, r;
00042 } ATTRIBUTE_PACKED;
00043 
00045 class Screen
00046 {
00047 public:
00048   typedef color3 color;
00049 
00050   static const color WHITE;
00051   static const color RED;
00052   static const color GREEN;
00053   static const color BLUE;
00054   static const color BLACK;
00055 
00062   Screen (size_t w, size_t h, prec_t sw, prec_t sh, const color& bg = WHITE);
00063   Screen (const Screen& copy);
00064   ~Screen ();
00065 
00066   Screen& operator= (const Screen& copy);
00067 
00068   void clear(const color& c = WHITE);
00069 
00071   void draw_axes (prec_t interval = 0.0, const color& c = BLUE);
00072 
00074   void set_pixel (size_t x, size_t y, const color& c);
00076   void set_pixel (const vect3d& p, const color& c);
00078   void set_pixel_merge (size_t x, size_t y, prec_t factor, const color& c);
00079 
00081   void translate (const vect3d& p, size_t& out_x, size_t& out_y);
00083   void translate (const vect3d& p, prec_t& out_x, prec_t& out_y, prec_t& out_z);
00085   void reverse (size_t x, size_t y, vect3d& out_p);
00086 
00087   prec_t get_dx () { return dx; }
00088   prec_t get_dy () { return dy; }
00089   prec_t get_dw () { return dx; }
00090   prec_t get_dh () { return dy; }
00091 
00093   void write (const char *path);
00094 
00096   void write (const std::string& path) { write (path.c_str()); }
00097 
00098   prec_t get_space_width () { return space_width; }
00099 
00100   size_t get_width () const { return width; }
00101   size_t get_height () const { return height; }
00102 
00103   enum eplane { UNDEFINED = 0, PLANE_XY = 1, PLANE_YZ, PLANE_ZX };
00105   void set_axis (eplane plane, prec_t distance = 0.0);
00107   void set_axis (const vect3d& pos0, const vect3d& xaxix, const vect3d& yaxis);
00109   void set_perspective (bool p) { is_perspective = p; }
00111   void set_zbuffer (bool enable);
00112 
00114   void draw_line (const vect3d& p0, const vect3d& p1, const color& c);
00115 
00117   void draw_cross (const vect3d& p0, const color& c, prec_t len = 0.05);
00118 
00119   const vect3d& get_center () const { return center; }
00120 
00121 private:
00122   size_t width, height;
00123   prec_t space_width, space_height;
00124   prec_t dw, dh;
00125   prec_t dx, dy;
00126   color *smap;
00127   prec_t *zbuf;
00128   vect3d center, normal, xdir, ydir;
00129   prec_t plane_d;
00130   eplane plane_type;
00131   bool is_perspective;
00132 
00133   void set_pixel_prec (prec_t x, prec_t y, const color& c);
00134 };
00135 
00136 #endif // !__screen_hpp__
00137 

Generated on Thu Dec 6 20:31:14 2007 for Ephi by  doxygen 1.5.0