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
00012
00013
00014
00015
00016
00017 #define hashflag 1
00018
00019 #define newhasher 1
00020
00021
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
00033
00034
00035 double nan();
00036
00037
00038
00039 class ADF;
00040 class ADFNode;
00041 class ADFTraversalNode;
00042
00043
00044
00045
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
00078
00079 unsigned short address[3];
00080 };
00081
00082
00083
00084
00085
00086 class ADFSampleHasher
00087 {
00088 public:
00089 int operator()(const ADFSampleAddress &addr) const;
00090 };
00091
00092
00093
00094
00095
00096 class ADFSampleSet
00097 {
00098 public:
00099 bool operator()(const ADFSampleAddress& a1, const ADFSampleAddress& a2) const;
00100 };
00101
00102
00103
00104
00105
00106 class ADFSampleMap
00107 {
00108 public:
00109 bool operator()(const ADFSampleAddress& a1, const ADFSampleAddress& a2) const;
00110 };
00111
00112
00113
00114
00115
00116
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
00134
00135
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;}
00145
00146 ADF(Implicit *m_f, Box<double> *box, double error);
00147 ~ADF();
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);
00151
00152 #if !defined(hashflag)
00153 std::map<ADFSampleAddress, ADFSample, ADFSampleMap> sample_map;
00154 #endif
00155 #if defined(hashflag)
00156 hashmap<ADFSampleAddress, ADFSample, ADFSampleHasher, ADFSampleMap> sample_map;
00157 #endif
00158
00159 virtual bool setChildren(std::vector<Implicit*> children);
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 #ifndef INTERVAL_EVAL_ONLY
00171 virtual double proc(gmVector3 x);
00172 #endif
00173 virtual Intervald proc(Box<double> x);
00174
00175 virtual void procq(gmVector3, double*);
00176 virtual void getq(double*);
00177 virtual void _setq(double*);
00178 virtual int qlen();
00179
00180 virtual void getqname(char** qn);
00181 MAKE_NAME();
00182
00183 void createADF(Implicit *imp, Box<double> *b, double err=0.1);
00184 double length_table[16];
00185 double epsilon;
00186 Box<double> *box;
00187 };
00188
00189
00190
00191
00192
00193
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
00203
00204 class ADFNode : public OctreeNode
00205 {
00206 public:
00207 unsigned char node_type;
00208 };
00209
00210
00211
00212
00213
00214
00215
00216
00217
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
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
00259
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
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