core/vbl/vbl_bit_array_3d.cxx
Go to the documentation of this file.
00001 // This is core/vbl/vbl_bit_array_3d.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author
00008 // Author: Geoffrey Cross, Oxford RRG
00009 // Created: 17 Jul 99
00010 //
00011 //-----------------------------------------------------------------------------
00012 
00013 #include "vbl_bit_array_3d.h"
00014 
00015 #include <vcl_cstring.h> // for memset()
00016 #include <vcl_cassert.h>
00017 #include <vcl_iostream.h>
00018 #include <vcl_climits.h>
00019 
00020 void vbl_bit_array_3d::put(unsigned int i1, unsigned int i2, unsigned int i3, bool v)
00021 {
00022   unsigned long byteindex;
00023   unsigned char bitindex;
00024 
00025   index(i1, i2, i3, byteindex, bitindex);
00026 
00027   unsigned char mask = (unsigned char)(v?(1<<bitindex):0);
00028   unsigned char nmask = (unsigned char)(~(1<<bitindex));
00029 
00030   data_[byteindex] = mask|(nmask & data_[byteindex]);
00031 }
00032 
00033 void vbl_bit_array_3d::flip(unsigned int i1, unsigned int i2, unsigned int i3)
00034 {
00035   unsigned long byteindex;
00036   unsigned char bitindex;
00037 
00038   index(i1, i2, i3, byteindex, bitindex);
00039 
00040   unsigned char mask = (unsigned char)((data_[byteindex] & (1<<bitindex)) ? 0 : (1<<bitindex));
00041   unsigned char nmask = (unsigned char)(~(1<<bitindex));
00042 
00043   data_[byteindex] = mask|(nmask & data_[byteindex]);
00044 }
00045 
00046 void vbl_bit_array_3d::fill(bool v)
00047 {
00048   register unsigned char temp = v ? ~(unsigned char)0 : 0;
00049   vcl_memset(data_, temp, this->size());
00050 }
00051 
00052 bool vbl_bit_array_3d::operator() (unsigned int i1, unsigned int i2, unsigned int i3) const
00053 {
00054   unsigned long byteindex;
00055   unsigned char bitindex;
00056 
00057   index(i1, i2, i3, byteindex, bitindex);
00058   unsigned char mask = (unsigned char)(1<<bitindex);
00059 
00060   return (data_[byteindex] & mask) != 0;
00061 }
00062 
00063 void vbl_bit_array_3d::index(unsigned x, unsigned y, unsigned z,
00064                              unsigned long & byteindex,
00065                              unsigned char & bitindex) const
00066 {
00067   assert(x<row1_count_ && y<row2_count_ && z<row3_count_);
00068   unsigned long i = (z*row2_count()+y)*row1_count()+x;
00069 
00070   byteindex = i/CHAR_BIT;
00071   bitindex  = (unsigned char)(i%CHAR_BIT);
00072 }
00073 
00074 
00075 ////////////////////////////////////////////////////////////////////////
00076 
00077 vcl_ostream &operator<<(vcl_ostream& os, vbl_bit_array_3d const& bitarray)
00078 {
00079   for (unsigned int i=0; i< bitarray.row3_count(); ++i)
00080   {
00081     for (unsigned int j=0; j< bitarray.row2_count(); ++j)
00082     {
00083       for (unsigned int k=0; k< bitarray.row1_count(); ++k)
00084         os << (bitarray(k,j,i) ? 'x' : '.');
00085       os << vcl_endl;
00086     }
00087     os << vcl_endl;
00088   }
00089   return os;
00090 }
00091 
00092 void vbl_bit_array_3d::construct(unsigned int m, unsigned int n, unsigned int p)
00093 {
00094   // quick return if possible
00095   if (m==0 || n==0 || p==0) { row1_count_=row2_count_=row3_count_=0; data_ = 0; return; }
00096   row1_count_ = m; row2_count_ = n; row3_count_ = p;
00097   data_ = new unsigned char [this->size()];
00098   data_[this->size()-1]=0; // avoids uninitialized data problems in operator==()
00099 }
00100 
00101 //: Copy constructor
00102 vbl_bit_array_3d::vbl_bit_array_3d(vbl_bit_array_3d const& that)
00103   : row1_count_(0), row2_count_(0), row3_count_(0), data_(0)
00104 {
00105   if ( that.data_)
00106   {
00107     construct(that.row1_count_, that.row2_count_, that.row3_count_);
00108     vcl_memcpy(data_, that.data_, this->size());
00109   }
00110 }
00111 
00112 vbl_bit_array_3d::vbl_bit_array_3d(unsigned int m, unsigned int n, unsigned int p, bool v[])
00113 {
00114   construct(m,n,p);
00115   for (unsigned int x=0; x<m; ++x)
00116     for (unsigned int y=0; y<n; ++y)
00117       for (unsigned int z=0; z<p; ++z)
00118         set(x,y,z, v[(n*z+y)*m+x]);
00119 }
00120 
00121 //: Assignment operator
00122 vbl_bit_array_3d& vbl_bit_array_3d::operator=(vbl_bit_array_3d const& that)
00123 {
00124   if (row1_count_ != that.row1_count() ||
00125       row2_count_ != that.row2_count() ||
00126       row3_count_ != that.row3_count())
00127     resize(that.row1_count_, that.row2_count_, that.row3_count_);
00128 
00129   vcl_memcpy(data_, that.data_, this->size());
00130   return *this;
00131 }
00132 
00133 bool vbl_bit_array_3d::operator==(vbl_bit_array_3d const &a) const
00134 {
00135   if (row1_count_ != a.row1_count() ||
00136       row2_count_ != a.row2_count() ||
00137       row3_count_ != a.row3_count())
00138     return false;
00139   return 0 == vcl_memcmp(data_, a.data_, this->size());
00140 }
00141 
00142 unsigned long vbl_bit_array_3d::size() const
00143 {
00144   return (row1_count_*row2_count_*row3_count_+CHAR_BIT-1)/CHAR_BIT;
00145 }