OpenMesh
OpenMesh/Tools/VDPM/VHierarchyWindow.hh
00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                               OpenMesh                                    *
00004  *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openmesh.org                                *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------* 
00008  *  This file is part of OpenMesh.                                           *
00009  *                                                                           *
00010  *  OpenMesh is free software: you can redistribute it and/or modify         * 
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenMesh is distributed in the hope that it will be useful,              *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenMesh.  If not,                                    *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/ 
00034 
00035 /*===========================================================================*\
00036  *                                                                           *             
00037  *   $Revision: 362 $                                                         *
00038  *   $Date: 2011-01-26 10:21:12 +0100 (Mi, 26 Jan 2011) $                   *
00039  *                                                                           *
00040 \*===========================================================================*/
00041 
00042 //=============================================================================
00043 //
00044 //  CLASS newClass
00045 //
00046 //=============================================================================
00047 
00048 #ifndef OPENMESH_VDPROGMESH_VHIERARCHYWINDOWS_HH
00049 #define OPENMESH_VDPROGMESH_VHIERARCHYWINDOWS_HH
00050 
00051 
00052 //== INCLUDES =================================================================
00053 
00054 #include <OpenMesh/Tools/VDPM/VHierarchy.hh>
00055 
00056 
00057 //== FORWARDDECLARATIONS ======================================================
00058 
00059 
00060 //== NAMESPACES ===============================================================
00061 
00062 namespace OpenMesh {
00063 namespace VDPM {
00064 
00065 //== CLASS DEFINITION =========================================================
00066 
00067               
00070 class VHierarchyWindow
00071 {
00072 private:
00073 
00074   // reference of vertex hierarchy
00075   VHierarchy    *vhierarchy_;
00076 
00077   // bits buffer (byte units)
00078   unsigned char *buffer_;
00079   int           buffer_min_;
00080   int           buffer_max_;
00081   int           current_pos_;
00082 
00083   // window (byte units)
00084   int           window_min_;
00085   int           window_max_;
00086   
00087 
00088   // # of right shift (bit units)
00089   unsigned char n_shift_;           // [0, 7]
00090 
00091   unsigned char flag8(unsigned char n_shift) const
00092   { return 0x80 >> n_shift; }  
00093 
00094   unsigned char flag8(VHierarchyNodeHandle _node_handle) const
00095   {
00096     assert(_node_handle.idx() >= 0);
00097     return  0x80 >> (unsigned int) (_node_handle.idx() % 8);
00098   }
00099   int byte_idx(VHierarchyNodeHandle _node_handle) const
00100   {
00101     assert(_node_handle.idx() >= 0);
00102     return  _node_handle.idx() / 8;
00103   }
00104   int buffer_idx(VHierarchyNodeHandle _node_handle) const
00105   { return  byte_idx(_node_handle) - buffer_min_; } 
00106 
00107   bool before_window(VHierarchyNodeHandle _node_handle) const
00108   { return (_node_handle.idx()/8 < window_min_) ? true : false; }
00109 
00110   bool after_window(VHierarchyNodeHandle _node_handle) const
00111   { return (_node_handle.idx()/8 < window_max_) ? false : true; }
00112 
00113   bool underflow(VHierarchyNodeHandle _node_handle) const
00114   { return (_node_handle.idx()/8 < buffer_min_) ? true : false; }
00115 
00116   bool overflow(VHierarchyNodeHandle _node_handle) const
00117   { return (_node_handle.idx()/8 < buffer_max_) ? false : true; }  
00118 
00119   bool update_buffer(VHierarchyNodeHandle _node_handle);
00120 
00121 public:
00122   VHierarchyWindow();
00123   VHierarchyWindow(VHierarchy &_vhierarchy);
00124   ~VHierarchyWindow(void);
00125   
00126   void set_vertex_hierarchy(VHierarchy &_vhierarchy)
00127   { vhierarchy_ = &_vhierarchy; }
00128 
00129   void begin()
00130   {
00131     int new_window_min = window_min_;
00132     for (current_pos_=window_min_-buffer_min_; 
00133          current_pos_ < window_size(); ++current_pos_)
00134     {
00135       if (buffer_[current_pos_] == 0)   
00136         ++new_window_min;
00137       else
00138       {
00139         n_shift_ = 0;
00140         while ((buffer_[current_pos_] & flag8(n_shift_)) == 0)
00141           ++n_shift_;
00142         break;
00143       }
00144     }
00145     window_min_ = new_window_min;
00146   }
00147 
00148   void next()
00149   {
00150     ++n_shift_;
00151     if (n_shift_ == 8)
00152     {
00153       n_shift_ = 0;
00154       ++current_pos_;
00155     }
00156 
00157     while (current_pos_ < window_max_-buffer_min_)
00158     {
00159       if (buffer_[current_pos_] != 0) // if the current byte has non-zero bits
00160       {
00161         while (n_shift_ != 8)
00162         {
00163           if ((buffer_[current_pos_] & flag8(n_shift_)) != 0)
00164             return;                     // find 1 bit in the current byte
00165           ++n_shift_;
00166         }
00167       }
00168       n_shift_ = 0;
00169       ++current_pos_;
00170     }
00171   }
00172   bool end() { return !(current_pos_ < window_max_-buffer_min_); }
00173 
00174   int window_size() const      { return  window_max_ - window_min_; }
00175   int buffer_size() const      { return  buffer_max_ - buffer_min_; }
00176 
00177   VHierarchyNodeHandle node_handle()
00178   {
00179     return  VHierarchyNodeHandle(8*(buffer_min_+current_pos_) + (int)n_shift_);
00180   }
00181 
00182   void activate(VHierarchyNodeHandle _node_handle)
00183   {
00184     update_buffer(_node_handle);
00185     buffer_[buffer_idx(_node_handle)] |= flag8(_node_handle);
00186     window_min_ = std::min(window_min_, byte_idx(_node_handle));
00187     window_max_ = std::max(window_max_, 1+byte_idx(_node_handle));
00188   }
00189 
00190 
00191   void inactivate(VHierarchyNodeHandle _node_handle)
00192   {
00193     if (is_active(_node_handle) != true)  return;
00194     buffer_[buffer_idx(_node_handle)] ^= flag8(_node_handle);
00195   }
00196 
00197 
00198   bool is_active(VHierarchyNodeHandle _node_handle) const
00199   {
00200     if (before_window(_node_handle) == true ||
00201         after_window(_node_handle) == true)
00202       return  false;
00203     return ((buffer_[buffer_idx(_node_handle)] & flag8(_node_handle)) > 0);
00204   }
00205 
00206   void init(VHierarchyNodeHandleContainer &_roots);
00207   void update_with_vsplit(VHierarchyNodeHandle _parent_handle);
00208   void update_with_ecol(VHierarchyNodeHandle _parent_handle);
00209 };
00210 
00211 //=============================================================================
00212 } // namespace VDPM
00213 } // namespace OpenMesh
00214 //=============================================================================
00215 #endif // OPENMESH_VDPROGMESH_VHIERARCHYWINDOWS_HH
00216 //=============================================================================
00217