vee_grid_force.h

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 
00003 /* COPYRIGHT
00004  *
00005  * This file is part of the Visual Effects Engine - VEE
00006  *
00007  * Read the "VEE-LICENSE" file for the license.
00008  *
00009  * Authors & Copyright:   
00010  *
00011  * Tommi Ilmonen, Tuukka Heikura, Marko Myllymaa and 
00012  * Janne Kontkanen 2001-2004
00013  *
00014  * Additional copyrights: Tekes 2003-2004
00015  *
00016  * firstname.lastname@hut.fi
00017  *
00018  */
00019 
00020 #ifndef VEE_GRID_FORCE_H
00021 #define VEE_GRID_FORCE_H
00022 
00023 #include <vee_force_object.h>
00024 #include <vee_reference_object.h>
00025 #include <vee_vector4.h>
00026 
00027 #include <mj_array3d.h>
00028 
00029 #define VEE_GRID_SENTINEL -1.11e+10f
00030 
00031 namespace Solar
00032 {
00033   class Model;
00034 }
00035 
00036 typedef Solar::Model Solar_Model;
00037 
00038 class GEE_Group;
00039 class GEE_Io;
00040 
00041 class VEE_VectorGridCell {
00042 public:
00043   void clear() { m_gradient.clear(); m_distance = -1.0e+10; }
00044   
00045   void add(const VEE_VectorGridCell &c) 
00046   { m_gradient += c.m_gradient; }
00047   
00048   void scale(float s) { m_gradient *= s; }
00049   
00050   VEE_Vector3 m_gradient;
00052   float       m_distance;
00053 };
00054 
00057 class VEE_VectorGrid 
00058 {
00059 public:
00060 
00061   class JanneGrid3;
00062 
00063   VEE_VectorGrid();
00064   virtual ~VEE_VectorGrid();
00065   
00066   VEE_VectorGridCell getGradient(const VEE_Vector3 &) const;
00067   VEE_VectorGridCell interpolateGradient(const VEE_Vector3 &) const;
00068 
00069   const float & scale() const { return m_scale; }
00070 
00071   float meanDistance();
00072   float meanGradientLength();
00073 
00074   bool createFromModel(Solar_Model *m, 
00075            uint division,
00076            int blurRepeats,
00077            int blurRadius,
00078            int pad = -1);
00079 
00080   bool createFromModel(GEE_Group *m, 
00081            uint division,
00082            int blurRepeats,
00083            int blurRadius,
00084            int pad = -1);
00085 
00086   bool createFromModel2(Solar_Model *m, 
00087       uint division,
00088       int blurRepeats,
00089       int blurRadius);
00090 
00091   bool writeGridToFile(const char *);
00092   bool readGridFromFile(const char *);
00093   bool writeAsciiFile(const char *, int selectLayer = -1) const;
00094 
00095   static bool fromModelToFile(const char * fieldFile,
00096             const char * solarModel, 
00097             int division,
00098             int blurRepeats,
00099             int blurRadius,
00100             int method);
00101 
00102   static void setDebug(bool b) { m_debug = b; }
00103 
00104   typedef MJ_Array3D<VEE_VectorGridCell> grid3d;
00105 
00106   grid3d & grid() { return m_field; }
00107 
00108   int layers() const { return m_field.layers(); }
00109   int rows() const { return m_field.rows(); }
00110   int cols() const { return m_field.cols(); }
00111 
00112 protected:
00113   bool createFromGrid(JanneGrid3 *,
00114           const VEE_Vector3 & low, 
00115           const VEE_Vector3 & high,
00116           uint division,
00117           int blurRepeats,
00118           int blurRadius,
00119           int pad);
00120 
00121   bool createFromGrid2(JanneGrid3 *,
00122            const VEE_Vector3 & low, 
00123            const VEE_Vector3 & high,
00124            uint division,
00125            int blurRepeats,
00126            int blurRadius);
00127 
00128   void setInsides(JanneGrid3 *);
00129 
00130   grid3d m_field;
00131 
00134   float  m_scale;
00135 
00136   static bool m_debug;
00137 };
00138 
00141 
00142 /*
00143 class VEE_VectorGridCompressed
00144 {
00145 public:
00146   VEE_VectorGridCompressed();
00147   ~VEE_VectorGridCompressed();
00148 
00149   
00150   void build(VEE_VectorGrid * source, 
00151        uint firstDivision = 8, 
00152        float maximumVectorSpread = 0.2);
00153 
00154 protected:
00155 
00156   class Branch {
00157   public:
00158 
00159     Branch() {}
00160     ~Branch() {}
00161     
00162     MJ_Array3D<Leaf>  m_leafs;
00163     Fluid::Vector4<char> 
00164   };
00165 };
00166 
00167 */
00168 
00171 
00178 class VEE_VectorGridTree
00179 {
00180 public:
00181   VEE_VectorGridTree();
00182   ~VEE_VectorGridTree();
00183 
00184   uint bytesUsed() const;
00185   uint lowestVoxels() const;
00186   uint totalVoxelDim() const { return m_totalVoxelDim; }
00187   float voxelSize() const { return m_voxelSize; }
00188 
00189   void build(VEE_VectorGrid * source, 
00190        uint firstDivision = 8, 
00191        float maximumVectorSpread = 0.2);
00192 
00193   bool write(const char * filename);
00194   bool read(const char * filename);
00195 
00196   bool write(GEE_Io *);
00197   bool read(GEE_Io *);
00198 
00199   const VEE_Vector4 & getPlane(const VEE_Vector3 &loc);
00200 
00201   uint validLeafCount() const;
00202 
00203 protected:
00204 
00205   void resize(uint firstDivision, uint secondDivision);
00206 
00207   class Leaf
00208   {
00209   public:
00210     Leaf() : m_plane(0, 0, 0, VEE_GRID_SENTINEL) {}
00211     ~Leaf() {}
00212 
00213     void clear() { m_plane.make(0, 0, 0, VEE_GRID_SENTINEL); }
00214 
00215     VEE_Vector4 m_plane;
00216   };
00217 
00218   class Branch
00219   {
00220   public:
00221     Branch();
00222 
00223     ~Branch();
00224 
00225     void resize(uint layers, uint rows, uint cols)
00226     { 
00227       m_leafs.resize(layers, rows, cols); 
00228     }
00229 
00230     bool tryCollapse(float maximumVectorSpread);
00231     
00232     bool isEmpty() const { return m_leafs.data() == 0; }
00233 
00234     uint bytesUsed() const;
00235 
00236     bool write(GEE_Io *);
00237     bool read(GEE_Io *);
00238 
00239     // MJ_Array3D<Branch> m_children;
00240     MJ_Array3D<Leaf>   m_leafs;
00241 
00242     Leaf               m_topLeaf;
00243   };
00244 
00245   MJ_Array3D<Branch> m_tree;
00246 
00247   Leaf m_zeroLeaf;
00248 
00250   float m_voxelSize;
00251   uint  m_totalVoxelDim;
00252   uint  m_bottomDivision;
00253 };
00254 
00257 
00261 class VEE_GridForceBase : public VEE_ForceObject
00262 {
00263 public:
00264   VEE_GridForceBase();
00265   virtual ~VEE_GridForceBase();
00266 
00267   virtual void getForce(VEE_Vector3 *force,
00268       const VEE_Vector3 &velocity,
00269       const VEE_Vector3 &location);
00270   virtual void boundingBoxOfForce(VEE_BoundingBox *box);
00271 
00272   virtual bool setValue(const char *, VEE_Value *);
00273   virtual VEE_Variable getVariable(const char *) const;
00274 
00275   void setRotation(const VEE_Matrix3 & r)
00276   { m_rotation = r; m_rotationInv = r; m_rotationInv.transpose(); }
00277 
00278   const VEE_Matrix3 & rotation() { return m_rotation; }
00279 
00280 protected:
00281 
00282   float       m_scale;
00283   VEE_Matrix3 m_rotation;
00284   VEE_Matrix3 m_rotationInv;
00285 
00286   float       m_magnitude;
00289   float       m_tangentForce;
00290   float       m_tangentGrab;
00291   float       m_bounce;
00292   float       m_drag;
00293   float       m_dragPower;
00294 
00295   float       m_bounceDistance;
00296   float       m_flowDistance;
00297   float       m_dragDistance;
00298 
00299   float       m_frameTime;
00300 
00301   bool        m_impactMethod;
00302   bool        m_onlyImpact;
00303 };
00304 
00307 
00311 class VEE_GridForce : public VEE_GridForceBase
00312 {
00313 public:
00314   VEE_GridForce();
00315   virtual ~VEE_GridForce();
00316 
00317   void getForce(VEE_Vector3 *force,
00318     const VEE_Vector3 &velocity,
00319                 const VEE_Vector3 &location);
00320   void boundingBoxOfForce(VEE_BoundingBox *box);
00321 
00322   virtual void update(float);
00323 
00324   VEE_Object * clone() const;
00325 
00326 
00327   void setGrid(const VEE_VectorGrid &g) { m_grid = g; }
00328 
00329   VEE_VectorGrid & grid() { return (*m_grid); }
00330 
00331   float totalScale() const 
00332   { return (*m_grid).scale() * m_scale; }
00333 
00334 protected:
00335 
00336   void recursiveImpact(VEE_SceneBox *, VEE_BoundingBox *);
00337 
00338   VEE_ReferenceObject<VEE_VectorGrid> m_grid;
00339 
00340 };
00341 
00344 
00348 class VEE_GridForce2 : public VEE_GridForceBase
00349 {
00350 public:
00351   VEE_GridForce2();
00352   virtual ~VEE_GridForce2();
00353 
00354   void getForce(VEE_Vector3 *force,
00355     const VEE_Vector3 &velocity,
00356                 const VEE_Vector3 &location);
00357   void boundingBoxOfForce(VEE_BoundingBox *box);
00358 
00359   virtual void update(float);
00360 
00361   VEE_Object * clone() const;
00362 
00363 
00364   void setGrid(const VEE_VectorGridTree &g) { m_grid = g; }
00365 
00366   VEE_VectorGridTree & grid() { return (*m_grid); }
00367 
00368   float totalScale() const 
00369   { return m_scale; }
00370 
00371 protected:
00372 
00373   void recursiveImpact(VEE_SceneBox *, VEE_BoundingBox *);
00374 
00375   VEE_ReferenceObject<VEE_VectorGridTree> m_grid;
00376 
00377 };
00378 
00379 
00380 
00381 #endif

Generated on Mon Mar 12 21:09:00 2007 for VEE - The Visual Effects Engine by  doxygen 1.4.6