Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

adf.h

Go to the documentation of this file.
00001 #ifndef SPACEADF_INCLUDED // -*- C++ -*-
00002 #define SPACEADF_INCLUDED
00003 
00004 #ifdef _MSC_VER
00005 #pragma once
00006 #pragma warning(disable : 4786)
00007 #endif
00008 
00009 
00010 /************************************************************************
00011    Adaptively-sampled distance field.
00012    Third-generation:
00013     - re-usable octree-based implementation
00014     - hash table for samples
00015  ************************************************************************/
00016 // controls if use the hashtable or the regular map
00017 #define hashflag 1
00018 // controls if to use the new hasher.
00019 #define newhasher 1
00020 
00021 // replace hash_map by map in STL #include <STLPort/hash_map>
00022 #include "Implicit/Operator/UnaryOp.h"
00023 #include "octree.h"
00024 #include "libgm/gm.h"
00025 #include "hashset.h"
00026 #include "hashmap.h"
00027 #include <map>
00028 #include <set>
00029 #include <limits>
00030 
00031 /**
00032  * nan function for windows
00033  * this was changed after move the code from cygwin to VC++ since the STL is different
00034  */
00035 double nan();
00036 
00037 // Forward declarations.
00038 
00039 class ADF;
00040 class ADFNode;
00041 class ADFTraversalNode;
00042 
00043 /**
00044  * An ADF Sample Address.  Generally this is only used for manipulation
00045  * and maintenance of the address as an ADF is traversed.
00046  */
00047 class ADFSampleAddress
00048 {
00049 public:
00050     void adjust_for_child(int depth, int child);
00051     void adjust_for_corner_sample(int depth, int corner);
00052     void adjust_for_sample(int depth, int sample);
00053     void adjust_coordinate(int coord, int delta, int depth);
00054    
00055     void set_to_root();
00056 
00057     double coord_to_point(ADF *adfp, int coord) const;
00058     void to_point(ADF *adfp, gmVector3 &ret ) const;
00059 
00060     const ADFSampleAddress& operator=(const ADFSampleAddress&  a)
00061     {
00062         address[0] = a.address[0];
00063         address[1] = a.address[1];
00064         address[2] = a.address[2];
00065         return a;
00066     }
00067 
00068     bool operator==(const ADFSampleAddress&  a) const
00069     {
00070         return ( (a.address[0] == address[0])
00071                  && (a.address[1] == address[1])
00072                  && (a.address[2] == address[2]) );
00073     }
00074 
00075 public:
00076     /**
00077      * The x, y, and z components of the address.
00078      */
00079     unsigned short address[3];
00080 };
00081 
00082 
00083 /**
00084  * A hasher for an ADFSampleAddress.
00085  */
00086 class ADFSampleHasher
00087 {
00088 public:
00089     int operator()(const ADFSampleAddress &addr) const;
00090 };
00091 
00092 /**
00093  * A compare operation for an ADFSampleAddress
00094  * used with STL set
00095  */
00096 class ADFSampleSet
00097 {
00098 public:
00099   bool operator()(const ADFSampleAddress& a1, const ADFSampleAddress& a2) const;
00100 };
00101 
00102 /**
00103  * A compare operation for an ADFSampleAddress
00104  * used with STL set
00105  */
00106 class ADFSampleMap
00107 {
00108 public:
00109   bool operator()(const ADFSampleAddress& a1, const ADFSampleAddress& a2) const;
00110 };
00111 
00112 /**
00113  * An ADF Sample.  If the value is NaN, then the sample is invalid.
00114  * Basically, we need a simple wrapper for a double, which allows an
00115  * invalid value, to be produced for addresses which aren't in the
00116  * ADF's sample hash map.
00117  */
00118 class ADFSample
00119 {
00120 public:
00121   ADFSample(double  d) { distance = d; }
00122   ADFSample(const ADFSample&  s) { distance = s.distance; }
00123   ADFSample() { distance = nan(); }
00124 
00125   bool is_valid() const { return (distance != nan()); }
00126 
00127 public:
00128     double distance;
00129 };
00130 
00131 
00132 /**
00133  * An ADF based on an octree.
00134  * The accuracy_isosurface denotes the isosurface below which the tolerance is guaranteed.  Allows only having accurate reconstruction near the surface.
00135  * In this new verion, accuracy_isosurface is not used because the ADF is storing a field, it needs to be accurate every where, including places where the values are not close to zero.
00136  */
00137 class ADF : public Octree, public UnaryOp
00138 {
00139 protected:
00140     virtual void copy_traversal_node(OctreeTraversalNode *dp, OctreeTraversalNode *sp );
00141     virtual void init_traversal_node(OctreeTraversalNode *np, OctreeTraversalNode *pp, int child );
00142 
00143 public:
00144   ADF() { m_f = NULL;}  /// default constructor needed by surface
00145 //  ADF(DistanceFunction *df, double iso = gmGOOGOL);
00146   ADF(Implicit *m_f, Box<double> *box, double error);  /// constructor you normally use.
00147   ~ADF();  /// default destructor
00148 
00149     double get_sample(const ADFSampleAddress &addr, bool create = true);
00150     double get_distance(const gmVector3 &p, gmVector3 *normal = NULL, double calc_normal_threshold = gmGOOGOL);  /// old verion's proc
00151 
00152 #if !defined(hashflag)
00153   std::map<ADFSampleAddress, ADFSample, ADFSampleMap> sample_map;  /// this new std::map replaced the old hashmap used in STLPort
00154 #endif
00155 #if defined(hashflag)
00156   hashmap<ADFSampleAddress, ADFSample, ADFSampleHasher, ADFSampleMap> sample_map;  /// this new std::map replaced the old hashmap used in STLPort
00157 #endif
00158 
00159   virtual bool setChildren(std::vector<Implicit*> children);     /// Sets the operand of this operation.
00160   /* This should be removed. Everywhere the ADF accesses "implicit"
00161    * should be changed to an access of m_f. The member m_f is a pointer
00162    * to the implicit object the operator object actually operates on. 
00163    * The member m_f is already defined in UnaryOp, so it is inherited
00164    * and does not need to be defined here.
00165    * the implicit is called i_m, inhereited from UnaryOp
00166    * DistanceFunction *distance_function;
00167    *  Implicit *implicit; // used to create the ADF.
00168    */
00169 
00170 #ifndef INTERVAL_EVAL_ONLY
00171     virtual double proc(gmVector3 x);    /// Evaluation of function.
00172 #endif
00173     virtual Intervald proc(Box<double> x);
00174 
00175     virtual void procq(gmVector3, double*); /// Evaluation of dFdq.
00176     virtual void getq(double*);          /// Retreives parameters.
00177     virtual void _setq(double*);         /// Assigns parameters.
00178     virtual int qlen();         /// Returns the # of parameters.
00179 
00180   virtual void getqname(char** qn);      /// Retreives parameter names.
00181   MAKE_NAME();  /// factory call
00182 
00183   void createADF(Implicit *imp, Box<double> *b, double err=0.1);  /// calculate the ADF
00184   double length_table[16];  /// precomputered lengths for the data stucture
00185   double epsilon;  /// the error tolerance
00186   Box<double> *box;  /// an array of two gmVector3 that stores the two corner points
00187 };
00188 
00189 
00190 /**
00191  * Node types.  Mostly for denoting the reason a leaf was made a leaf.
00192  * Interior and exterior nodes are outside the accuracy iso-surface and
00193  * entirely interior or exterior to the surface.
00194  */
00195 #define ADF_NODE_INTERNAL (0)
00196 #define ADF_NODE_LEAF     (1)
00197 #define ADF_NODE_INTERIOR (2)
00198 #define ADF_NODE_EXTERIOR (3)
00199 
00200 
00201 /**
00202  * An ADF node.
00203  */
00204 class ADFNode : public OctreeNode
00205 {
00206 public:
00207     unsigned char node_type;
00208 };
00209 
00210 
00211 /**
00212  * Used for temporary storage of ADF nodes during traversal or construction.
00213  * Note that samples are not automatically extracted during traversal, for
00214  * efficiency; extract_samples_from must be called before performing any
00215  * real operations with the node.
00216  * get_local_coord_distance assumes the point is in the local coordinate
00217  * frame of the node.
00218  */
00219 class ADFTraversalNode : public OctreeTraversalNode
00220 {
00221 public:
00222     void extract_samples_from(ADF *adfp);
00223     bool is_reconstruction_valid(ADF *adfp);
00224     double get_distance(const gmVector3 &p);
00225     double get_local_coord_distance(const gmVector3 &p);
00226     void estimate_normal(const gmVector3 &p, gmVector3 *normal);
00227 
00228 
00229 public:
00230     ADFSampleAddress address;
00231     double samples[8];
00232 };
00233 
00234 
00235 /**
00236  * An iterator for use with mesh octrees, used to insert triangles into the octree.
00237  */
00238 class ADFRefiner : public OctreeIterator
00239 {
00240 public:
00241     ADFRefiner() { wants_internal_nodes = false; }
00242 
00243     void refine(ADF *ap, double epsilon);
00244 
00245     virtual bool iterate(OctreeTraversalNode *np);
00246 
00247     
00248 protected:
00249     virtual bool should_split(ADFTraversalNode *np);
00250 
00251 
00252 public:
00253     ADF *adfp;
00254 };
00255 
00256 
00257 /**
00258  * Pruner for an ADF.
00259  * Prunes all unused samples.
00260  */
00261 class ADFPruner : public OctreePruner
00262 {
00263 public:
00264     virtual void prune(Octree *op);
00265     virtual bool is_used(OctreeTraversalNode *np);
00266 
00267 
00268 public:
00269 
00270 #if !defined(hashflag)
00271   std::map<ADFSampleAddress, bool, ADFSampleMap> * used_samplesp;
00272 #endif
00273 #if defined(hashflag)
00274   hashmap<ADFSampleAddress, bool, ADFSampleHasher, ADFSampleMap> * used_samplesp;
00275 #endif
00276 
00277 };
00278 
00279 
00280 /**
00281  * Statistics-gathering for an ADF.
00282  */
00283 class ADFAnalyzer : public OctreeAnalyzer
00284 {
00285 public:
00286     ADFAnalyzer()
00287     {
00288         node_type_countsp[0] = NULL;
00289         node_type_countsp[1] = NULL;
00290         node_type_countsp[2] = NULL;
00291         node_type_countsp[3] = NULL;
00292     }
00293 
00294     ~ADFAnalyzer()
00295     {
00296         if ( node_type_countsp[0] != NULL )
00297         {
00298             delete[] node_type_countsp[0];
00299             delete[] node_type_countsp[1];
00300             delete[] node_type_countsp[2];
00301             delete[] node_type_countsp[3];
00302         }
00303     }
00304 
00305 
00306     virtual bool iterate(OctreeTraversalNode *np);
00307     virtual void report_statistics();
00308     virtual void report_statistics_at_depth(int d);
00309     virtual void reset();
00310 
00311 
00312 public:
00313     int *  node_type_countsp[4];
00314 };
00315 
00316 #endif

Generated on Mon Jun 28 14:58:12 2004 for Advanced Surface Library by doxygen 1.3.4