contrib/brl/bseg/brip/brip_roi.cxx
Go to the documentation of this file.
00001 #include "brip_roi.h"
00002 //:
00003 // \file
00004 #include <vsol/vsol_box_2d.h>
00005 #if 0 // later
00006 #include <vgl/vgl_box_2d.h>
00007 #endif
00008 
00009 brip_roi::brip_roi(const unsigned n_image_cols, const unsigned n_image_rows)
00010 {
00011   n_image_cols_ = n_image_cols;
00012   n_image_rows_ = n_image_rows;
00013 }
00014 
00015 void brip_roi::set_image_bounds(const int n_image_cols,
00016                                 const int n_image_rows)
00017 {
00018   n_image_cols_ = n_image_cols;
00019   n_image_rows_ = n_image_rows;
00020 }
00021 
00022 //:expand (or contract) each region of the roi by delta, creating a new roi.
00023 brip_roi::brip_roi(brip_roi const& roi, float delta)
00024   : vbl_ref_count(), n_image_cols_(roi.n_image_cols_),
00025     n_image_rows_(roi.n_image_rows_), regions_(roi.regions_)
00026 {
00027   float tdelta = delta;
00028   if (tdelta < 0) tdelta *= -1.0f; // to guarantee dxmin <= dxmax.
00029   for (vcl_vector<vsol_box_2d_sptr>::iterator rit = regions_.begin();
00030        rit != regions_.end(); rit++)
00031   {
00032     double xmin = (*rit)->get_min_x();
00033     double ymin = (*rit)->get_min_y();
00034     double xmax = (*rit)->get_max_x();
00035     double ymax = (*rit)->get_max_y();
00036 
00037     double dxmin = (xmin-tdelta), dymin = (ymin-tdelta),
00038       dxmax = (xmax+tdelta),  dymax = (ymax+tdelta);
00039 
00040     //Check image bounds and crop appropriately
00041     if (dxmin < 0) dxmin = 0;
00042     if (dymin < 0) dymin = 0;
00043     if (dxmax >= n_image_cols_) dxmax = n_image_cols_-1;
00044     if (dymax >= n_image_rows_) dymax = n_image_rows_-1;
00045 
00046     vsol_box_2d_sptr dbox = new vsol_box_2d();
00047     dbox->add_point(dxmin, dymin);
00048     dbox->add_point(dxmax, dymax);
00049     (*rit)=dbox;
00050   }
00051 }
00052 
00053 vsol_box_2d_sptr brip_roi::clip_to_image_bounds(vsol_box_2d_sptr box)
00054 {
00055   if (!box||!n_image_cols_||!n_image_rows_)
00056     return box;
00057   int x0 = (int)box->get_min_x();
00058   int y0 = (int)box->get_min_y();
00059   int xm = (int)box->get_max_x();
00060   int ym = (int)box->get_max_y();
00061   //clip to image bounds
00062   if (x0 < 0)
00063     x0 = 0;
00064   if ((unsigned int)x0 >= n_image_cols_)
00065     x0 = n_image_cols_-1;
00066   if (y0 < 0)
00067     y0 = 0;
00068   if ((unsigned int)y0 >= n_image_rows_)
00069     y0 = n_image_rows_-1;
00070   if (xm < 0)
00071     xm = 0;
00072   if ((unsigned int)xm >= n_image_cols_)
00073     xm = n_image_cols_-1;
00074   if (ym < 0)
00075     ym = 0;
00076   if ((unsigned int)ym >= n_image_rows_)
00077     ym = n_image_rows_-1;
00078   vsol_box_2d_sptr cbox = new vsol_box_2d();
00079   cbox->add_point(x0, y0);
00080   cbox->add_point(xm, ym);
00081   return cbox;
00082 }
00083 
00084 void brip_roi::add_region(const int x0, const int y0, const int xs, const int ys)
00085 {
00086   vsol_box_2d_sptr reg =  new vsol_box_2d();
00087   reg->add_point(x0, y0);
00088   reg->add_point(x0+xs-1, y0);
00089   reg->add_point(x0+xs-1, y0+ys-1);
00090   reg->add_point(x0, y0+ys-1);
00091   if (!n_image_cols_||!n_image_rows_)
00092   {
00093     regions_.push_back(reg);
00094     return;
00095   }
00096   //need to potentially clip the region
00097   vsol_box_2d_sptr creg =
00098     this->clip_to_image_bounds(reg);
00099   regions_.push_back(creg);
00100 }
00101 
00102 void brip_roi::add_region(const int xc, const int yc, const int radius)
00103 {
00104   vsol_box_2d_sptr reg =  new vsol_box_2d();
00105   reg->add_point(xc-radius, yc-radius);
00106   reg->add_point(xc+radius, yc-radius);
00107   reg->add_point(xc+radius, yc+radius);
00108   reg->add_point(xc-radius, yc+radius);
00109   if (!n_image_cols_||!n_image_rows_)
00110   {
00111     regions_.push_back(reg);
00112     return;
00113   }
00114   //need to potentially clip the region
00115   vsol_box_2d_sptr creg =
00116     this->clip_to_image_bounds(reg);
00117   regions_.push_back(creg);
00118 }
00119 
00120 void brip_roi::add_region(vsol_box_2d_sptr const & box)
00121 {
00122   if (!n_image_cols_||!n_image_rows_)
00123   {
00124     regions_.push_back(box);
00125     return;
00126   }
00127   //need to potentially clip the region
00128   vsol_box_2d_sptr creg =
00129     this->clip_to_image_bounds(box);
00130   regions_.push_back(creg);
00131 }
00132 
00133 //return true if there are no regions or if they are all empty
00134 bool brip_roi::empty() const
00135 {
00136   return n_regions() == 0;
00137 #if 0 // later
00138   for (vcl_vector<vgl_box_2d<int> >::iterator rit = regions_.begin();
00139        rit != regions_.end(); rit++)
00140     if (!(*rit).is_empty())
00141       return false;
00142 #endif
00143 }
00144 
00145 void brip_roi::clip_to_image_bounds()
00146 {
00147   vcl_vector<vsol_box_2d_sptr> temp;
00148   for (vcl_vector<vsol_box_2d_sptr>::iterator rit = regions_.begin();
00149        rit != regions_.end(); rit++)
00150     temp.push_back(this->clip_to_image_bounds(*rit));
00151   regions_ = temp;
00152 }
00153 
00154 int brip_roi::cmin(const unsigned i) const
00155 {
00156   return i >= regions_.size() ? 0 : (int)regions_[i]->get_min_x();
00157 }
00158 
00159 int brip_roi::cmax(const unsigned i) const
00160 {
00161   return i >= regions_.size() ? 0 : (int)regions_[i]->get_max_x();
00162 }
00163 
00164 int brip_roi::rmin(const unsigned i) const
00165 {
00166   return i >= regions_.size() ? 0 : (int)regions_[i]->get_min_y();
00167 }
00168 
00169 int brip_roi::rmax(const unsigned i) const
00170 {
00171   return i >= regions_.size() ? 0 : (int)regions_[i]->get_max_y();
00172 }
00173 
00174 unsigned brip_roi::csize(const unsigned i) const
00175 {
00176   int temp = cmax(i)-cmin(i) + 1;
00177   return temp<0 ? 0 : (unsigned)temp;
00178 }
00179 
00180 unsigned brip_roi::rsize(const unsigned i) const
00181 {
00182   int temp = rmax(i)-rmin(i) + 1;
00183   return temp<0 ? 0 : (unsigned)temp;
00184 }
00185 
00186 unsigned brip_roi::ic(int local_col, unsigned i) const
00187 {
00188   int temp = cmin(i) + local_col;
00189   return temp<0 ? 0 : (unsigned)temp;
00190 }
00191 
00192 unsigned brip_roi::ir(int local_row, unsigned i) const
00193 {
00194   int temp = rmin(i) + local_row;
00195   return temp<0 ? 0 : (unsigned)temp;
00196 }
00197 
00198 unsigned brip_roi::lc(unsigned global_col, unsigned i) const
00199 {
00200   int temp = global_col - cmin(i);
00201   return temp<0 ? 0 : (unsigned)temp;
00202 }
00203 
00204 unsigned brip_roi::lr(unsigned global_row, unsigned i) const
00205 {
00206  int temp = global_row - rmin(i);
00207   return temp<0 ? 0 : (unsigned)temp;
00208 }
00209 
00210 bool brip_roi::remove_region(unsigned i)
00211 {
00212   vcl_cerr << "brip_roi::remove_region(" << i << ") NYI\n";
00213   return false;
00214 }
00215 
00216 vcl_ostream&  operator<<(vcl_ostream& s, brip_roi const& r)
00217 {
00218   s << "brip_roi (regions)\n";
00219   for (unsigned i = 0; i< r.n_regions(); ++i)
00220     s << *(r.region(i)) <<'\n';
00221   return s;
00222 }