src/scene.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 __scene_hpp__
00022 #define __scene_hpp__
00023 
00024 #include <vector>
00025 
00026 #include "math3d.hpp"
00027 #include "utils.hpp"
00028 #include "screen.hpp"
00029 #include "threading.hpp"
00030 
00031 class ElectroDynamics;
00032 class Screen;
00033 
00040 class Scene
00041 {
00042 #ifndef SWIG
00043   template <class T>
00044   struct value_map
00045   {
00046     value_map () : data(0) { }
00047 
00048     void init (size_t w, size_t h)
00049     {
00050       delete [] data;
00051       this->w = w;
00052       this->h = h;
00053       data = new T [w * h];
00054     }
00055 
00056     void clear ()
00057     {
00058       memset (data, 0, sizeof (T) * w * h);
00059     }
00060 
00061     bool valid (size_t x, size_t y) const
00062     {
00063       return x < w && y < h;
00064     }
00065 
00066     T get (size_t x, size_t y) const
00067     {
00068       if ( !valid(x, y) )
00069         {
00070           T tmp;
00071           tmp = 0;
00072           return tmp;
00073         }
00074       return data[y * w + x];
00075     }
00076 
00077     void set (size_t x, size_t y, T value) const
00078     {
00079       data[y * w + x] = value;
00080     }
00081 
00082     bool valid (const vect3d& v) const { return valid(v.x, v.y); }
00083     T get (const vect3d& v) const { return get(v.x, v.y); }
00084 
00085     bool valid (prec_t x, prec_t y) const
00086     {
00087       return valid ((size_t)floor(x), (size_t)floor(y));
00088     }
00089 
00090     T get (prec_t x, prec_t y) const
00091     {
00092       return get ((size_t)floor(x), (size_t)floor(y));
00093     }
00094 
00095     ~value_map ()
00096     {
00097       delete [] data;
00098     }
00099 
00100     operator T* () { return data; }
00101     operator const T* () const { return data; }
00102 
00103     size_t w, h;
00104     T *data;
00105   };
00106 #endif
00107 
00108 public:
00109   Scene (ElectroDynamics& dynamics);
00110   ~Scene ();
00111 
00112   enum ecalc_t { CALC_BFIELD = 1, CALC_EFIELD, CALC_POTENTIAL };
00113 
00115   void calc (Screen& screen, ecalc_t ct = CALC_BFIELD);
00117   void render_map (Screen &screen);
00119   void render_lic (Screen &screen);
00121   void render_lic_animation (Screen &screen, const char *name_prefix, int frames);
00122 
00127   void set_coloring (size_t levels = 0, bool use_log = false);
00128 
00129   const Screen::color& cmap (prec_t bf);
00130 
00140   bool render_bline (Screen &screen, const vect3d& pos, const Screen::color& color, prec_t limit_dist = -1,
00141       const vect3d& limit_pos = vect3d(0, 0, 0), prec_t limit_pos_radius = 0.0, bool backwards = false);
00142 
00145   void calibrate_bline_step (const vect3d& pos, prec_t step);
00146 
00148   void render_bline_range (Screen &screen, const vect3d& pos0, const vect3d& pos1,
00149       const Screen::color& color, prec_t first_factor = 1.0, prec_t dist_limit = -1,
00150       const vect3d& line_end = vect3d(0, 0, 0), prec_t line_end_radius = 0.0);
00151 
00153   bool find_untouching_bline (vect3d& result, const vect3d& pos0, vect3d& pos1,
00154       prec_t limit_dist = -1, prec_t limit_pos_radius = 0.0);
00155 
00157   prec_t get_fmin() { return min_bf; }
00159   prec_t get_fmax() { return max_bf; }
00161   prec_t get_fadd() { return bfadd; }
00162 
00164   void calibrate_field (prec_t min, prec_t max, prec_t add = 0)
00165   { min_bf = min; max_bf = max; bfadd = add; update_levels(); }
00166 
00167 private:
00168   ElectroDynamics& dynamics;
00169   value_map<vect3d> vmap;
00170   value_map<vect3d> tmap; // translated and normalized vmap
00171   value_map<prec_t> smap; // scalar map
00172   prec_t min_bf;
00173   prec_t max_bf;
00174   std::vector<Screen::color> colors;
00175   bool use_log;
00176   size_t levels;
00177   prec_t max_level;
00178   prec_t factor;
00179   prec_t range;
00180   bool has_data;
00181   prec_t bfadd;
00182 
00183   struct calctask : Task
00184   {
00185     int part;
00186     prec_t min_f, max_f;
00187     Scene *scene;
00188     ecalc_t ct;
00189     Screen *screen;
00190 
00191     void execute () { scene->run_calctask (this); }
00192   };
00193   friend struct calctask;
00194 
00195   struct lictask : Task
00196   {
00197     int part;
00198     Scene *scene;
00199     Screen *screen;
00200     value_map<prec_t> *noise;
00201 
00202     void execute () { scene->run_lictask (this); }
00203   };
00204   friend struct lictask;
00205 
00206   void run_calctask (calctask *task);
00207   void run_lictask (lictask *task);
00208 
00209   prec_t step, mbf;
00210 
00211   void lic_integrate (const value_map<prec_t>& noise, const vect3d& pos,
00212       prec_t& total_ds, prec_t& total_c, bool neg = false);
00213 public:
00214   static prec_t hf (prec_t a, prec_t b, prec_t phi);
00215 private:
00216   void lic_integrate_hf (const value_map<prec_t>& noise, const vect3d& pos, prec_t phi,
00217       prec_t& total_ds, prec_t& total_c, bool neg = false);
00218   void update_levels ();
00219 };
00220 
00221 #endif // !__scene_hpp__
00222 

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