GlobalReg.h
上传用户:kellyonhid
上传日期:2013-10-12
资源大小:932k
文件大小:8k
源码类别:

3D图形编程

开发平台:

Visual C++

  1. //############################################################
  2. // GlobalReg.h
  3. // Kari Pulli
  4. // Tue Jun 23 11:54:00 PDT 1998
  5. // 
  6. // Global registration of several scans simultaneously
  7. //############################################################
  8. #ifndef _GLOBALREG_H_
  9. #define _GLOBALREG_H_
  10. #include <iostream.h>
  11. #include <hash_map.h>
  12. #include <hash_set.h>
  13. #include <vector.h>
  14. #include <rope.h>
  15. #include <string>
  16. #include <time.h>
  17. #include "Random.h"
  18. #include "Xform.h"
  19. #include "Pnt3.h"
  20. #include "absorient.h"
  21. #include "TbObj.h"
  22. #include "Bbox.h"
  23. class GlobalReg {
  24. private:
  25.   typedef Xform<float> XF_F;
  26.   float         ftol;
  27.   
  28.   std::string   gr_dir;
  29.   std::string   gr_auto_dir;
  30.   char          *cyber_raw_name;
  31.   //
  32.   // definitions for a hash_multimap
  33.   // it stores pointers to view transformations
  34.   // and points from those views (in current world coordinates)
  35.   //
  36.  public:
  37.   class mapEntry {
  38.   private:
  39.     mapEntry(const mapEntry &me);
  40.     mapEntry &operator=(const mapEntry &me);
  41.   public:
  42.     TbObj   *xfa, *xfb;         // current transforms
  43.     vector<Pnt3> ptsa, ptsb;    // points in world coordinates
  44.     Xform<float> rel_xf;
  45.     float   maxErr;  // point-to-point errors after globalreg
  46.     float   avgErr;
  47.     float   rmsErr;
  48.     float   pw_point_rmsErr;
  49.     float   pw_plane_rmsErr;
  50.     bool    manually_aligned;
  51.     enum  { qual_Unknown, qual_Bad, qual_Fair, qual_Good };
  52.     int     quality_grade; // 0..3 = unknown, bad, fair, good
  53.     time_t  modifyDate;
  54.     
  55.     mapEntry(TbObj *xa, TbObj *xb, 
  56.      const vector<Pnt3> &a, const vector<Pnt3> &b,
  57.      Xform<float> rxf, 
  58.      int max_pairs = 0)
  59.       : xfa(xa), xfb(xb), ptsa(a), ptsb(b),
  60.       manually_aligned(false), rel_xf(rxf)
  61.       { 
  62. if (ptsa.size() > max_pairs) {
  63.   // too many pairs, remove some
  64.   int n_left  = ptsa.size();
  65.   int end     = n_left;
  66.   int allowed = max_pairs;
  67.   Random rnd;
  68.   while (n_left && allowed) {
  69.     if (rnd() < float(allowed) / float(n_left)) {
  70.       // keep
  71.       n_left--;  allowed--;
  72.     } else {
  73.       // don't keep
  74.       n_left--;  end--;
  75.       ptsa[n_left] = ptsa[end];
  76.       ptsb[n_left] = ptsb[end];
  77.     }
  78.   }
  79.   if (max_pairs) {
  80.     // clean the vectors
  81.     ptsa.erase(&ptsa[max_pairs], ptsa.end());
  82.     ptsb.erase(&ptsb[max_pairs], ptsb.end());
  83.   }
  84. }
  85. //cout << "constructing " << int(this) << endl;
  86. time (&modifyDate);
  87.       }
  88.     ~mapEntry(void)
  89.       {
  90. //cout << "destructing " << int(this) << endl;
  91.       }
  92.     const Pnt3 *pts(TbObj *x)
  93.       {
  94. if (x == xfa) return &ptsa[0];
  95. if (x == xfb) return &ptsb[0];
  96. assert(0);
  97. return NULL;
  98.       }
  99.     int getQuality (void)
  100.       {
  101. if (quality_grade > qual_Good) return qual_Good;
  102. if (quality_grade < qual_Unknown) return qual_Unknown;
  103. return quality_grade;
  104.       }
  105.     
  106.     // return max distant, count, and sum of distants
  107.     void stats(void);
  108.     // try writing the mapEntry data into a single file
  109.     void export_to_file(const std::string &gr_dir);
  110.     // try writing the mapEntry data into a single file
  111.     // for CyberScans, raw data
  112.     void export_cyber_raw(const char *fname);
  113.   };
  114.   struct equal_tbobj {
  115.     bool operator()(const TbObj *a, const TbObj *b) const
  116.       { return a==b; }
  117.   };
  118.   struct hash_tbobj {
  119.     size_t operator()(const TbObj *a) const
  120.       { return (unsigned long) a; }
  121.   };
  122.  private:
  123.   //hash_multimap<Key, Data, HashFcn, EqualKey, Alloc>
  124.   typedef
  125.   hash_multimap<TbObj *,mapEntry*, hash_tbobj,equal_tbobj> HMM;
  126.   typedef hash_set<TbObj *,hash_tbobj,equal_tbobj> HS;
  127.   typedef HMM::iterator ITT;
  128.   typedef HS::iterator  ITS;
  129.   HMM hmm;
  130.   HS  all_scans;
  131.   // When a scan is changed in some way, it gets "dirty".
  132.   // Globalreg only aligns dirty scans to their neighbors
  133.   // (though the others may get aligned if their neighbors
  134.   // move enough).
  135.   // The set is cleaned after align_group().
  136.   HS  dirty_scans;
  137.   // a volume bucket is defines a space in the bbox of the
  138.   // collection of scans which we'll throw points into
  139.   // in order to area-normalize the number of points
  140.   // considered in the alignment
  141.   typedef struct {
  142.     vector<Pnt3> points;
  143.     Bbox volume;
  144.   } VolumeBucket;
  145.   // helpers to normalize_points
  146.   bool should_delete_point(Pnt3 pt, VolumeBucket *vb, 
  147.    int bucketInd);
  148.   int init_vol_buckets(VolumeBucket **vb, 
  149.        Bbox worldBbox, 
  150.        TbObj *scanToMoveTo,
  151.        float bucket_side = 1.0);
  152.   int find_bucket(Pnt3 pt, VolumeBucket *vb, 
  153.   float bucket_side, int numBuckets);
  154.   void  get_pts(TbObj *x, vector<Pnt3> &P, vector<Pnt3> &Q,
  155. TbObj* partner = NULL);
  156.   void  get_pts_within_group(TbObj *x,
  157.      vector<Pnt3> &P, vector<Pnt3> &Q,
  158.      vector<TbObj*> &group);
  159.   void  evaluate(const vector<TbObj*> &group);
  160.   float evaluate(TbObj *);
  161.   void  dump(void);
  162.   void         move_back(const vector<TbObj*> &group,
  163.  const vector<XF_F> &old_xf);
  164.   Xform<float> compute_xform(vector<Pnt3> &P, vector<Pnt3> &Q);
  165.   void align_one_to_others(TbObj* one, TbObj* two = NULL);
  166.   void align_group(const vector<TbObj*> &group);
  167.   bool getPairError(TbObj* a, TbObj* b,
  168.     float &pointError,
  169.     float &planeError,
  170.     float &globalError,
  171.     bool  &manual);
  172.   bool initial_import_done;
  173.   mapEntry* import(const std::string &fname, bool manual = false);
  174.   void unlink_gr_files(TbObj *a, TbObj *b, bool only_auto = false);
  175. public:
  176.   // constructor/destructor
  177.   GlobalReg (void);
  178.   ~GlobalReg(void);
  179.   void initial_import(void);
  180.   void perform_import(void);
  181.   // manipulate pairs
  182.   mapEntry* addPair(TbObj *a, TbObj *b, 
  183.     const vector<Pnt3> &ap, const vector<Pnt3> &nrma,
  184.     const vector<Pnt3> &bp, const vector<Pnt3> &nrmb,
  185.     Xform<float> rel_xf = Xform<float>(),
  186.     bool manually_aligned = false,
  187.     int  max_pairs = 0,
  188.     bool save = true,
  189.     int saveQual = mapEntry::qual_Unknown);
  190.   mapEntry* addPair(TbObj *a, TbObj *b, 
  191.     const vector<Pnt3> &ap,
  192.     const vector<Pnt3> &bp,
  193.     float pw_point_rmsErr,
  194.     float pw_plane_rmsErr,
  195.     Xform<float> rel_xf = Xform<float>(),
  196.     bool manually_aligned = false,
  197.     int  max_pairs = 0,
  198.     bool save = true,
  199.     int saveQual = mapEntry::qual_Unknown);
  200.   bool markPairQuality (TbObj *a, TbObj* b,
  201. int saveQual = mapEntry::qual_Unknown);
  202.   int  getPairQuality (TbObj *a, TbObj* b);
  203.   void deletePair(TbObj *a, TbObj *b, bool deleteFile = false);
  204.   void deleteAllPairs(TbObj *a, bool deleteFile = false);
  205.   void deleteAutoPairs(float thr, TbObj *a = NULL);
  206.   void showPointpairCount(TbObj *a, TbObj *b = NULL);
  207.   //void recalcPairwiseErrors(void);
  208.   // align: if you pass scanToMove = NULL (default), all scans 
  209.   // are aligned to all others.
  210.   // If you pass an object as scanToMove, only pairs
  211.   // involving that scan are considered, and for each alignment
  212.   // it will be moved toward the other scan, which should have
  213.   // the effect of aligning it with a group of other 
  214.   // self-registered scans without moving any of them.
  215.   // we'll need the worldBbox so we can compute buckets for area-
  216.   // normalization (the second align fn)
  217.   void align(float ftol,
  218.      TbObj* scanToMove = NULL,
  219.      TbObj* scanToMoveTo = NULL);
  220.    void align(float ftol, Bbox worldBbox,
  221.      TbObj* scanToMove = NULL,
  222.      TbObj* scanToMoveTo = NULL);
  223.    void normalize_samples(Bbox worldBbox, TbObj *scanToMoveTo);
  224.   // status and debugging info
  225.   bool pairRegistered(TbObj *a, TbObj *b,
  226.       bool &manual,
  227.       bool transitiveAllowed = false);
  228.   int   count_partners(TbObj *mesh);
  229.   crope list_partners (TbObj *mesh, 
  230.        bool transitiveAllowed = false);
  231.   std::string dump_meshpairs(int   choice,
  232.      float listThresh = FLT_MAX);
  233.   void dump_connected_groups (void);
  234.   enum ERRMETRIC { errmetric_max, errmetric_avg, errmetric_rms,
  235.    errmetric_pnt, errmetric_pln };
  236.   bool getPairingSummary (TbObj* mesh,     // return info on this mesh's pairs:
  237.   ERRMETRIC metric,// using this field for err
  238.   int count[3],    // total/man/auto
  239.   float err[3],    // min/avg/max
  240.   int quality[4]); // unknown/bad/fair/good
  241.   bool getPairingStats (TbObj* from, TbObj* to,  // between these two meshes
  242. bool& bManual,
  243. int& iQuality,
  244. int& nPoints,
  245. time_t& date,
  246. float err[5]);
  247. };
  248. #endif