contrib/brl/bbas/bvgl/bvgl_changes.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 
00004 #include "bvgl_changes.h"
00005 #include "bvgl_change_obj.h"
00006 #include <vil/vil_image_view.h>
00007 #include <vgl/vgl_polygon_scan_iterator.h>
00008 
00009 vil_image_view_base_sptr
00010 bvgl_changes::create_mask_from_objs(unsigned ni, unsigned nj, vcl_string change_type)
00011 {
00012   vil_image_view<vxl_byte>* mask = new vil_image_view<vxl_byte>(ni, nj);
00013   mask->fill(0);
00014 
00015   //index through the polygons and create the boolean mask image
00016   for (unsigned i=0; i<objs_.size(); i++)
00017   {
00018     vgl_polygon<double> v_poly =  objs_[i]->poly();
00019     vgl_polygon_scan_iterator<double> psi(v_poly, false);
00020     for (psi.reset(); psi.next();){
00021       int y = psi.scany();
00022       for (int x = psi.startx(); x<=psi.endx(); ++x)
00023       {
00024         unsigned u = static_cast<unsigned>(x);
00025         unsigned v = static_cast<unsigned>(y);
00026         if (u >= ni || v >= nj)
00027           continue;
00028         if (objs_[i]->type().compare(change_type)==0)
00029           (*mask)(u,v) = 255;
00030         else  // don't care areas
00031           (*mask)(u,v) = 125;
00032       }
00033     }
00034   }
00035 
00036   return mask;
00037 }
00038 
00039 vil_image_view_base_sptr 
00040 bvgl_changes::create_mask_from_objs_all_types(unsigned ni, unsigned nj)
00041 {
00042   vil_image_view<vxl_byte>* mask = new vil_image_view<vxl_byte>(ni, nj);
00043   mask->fill(0);
00044 
00045   //index through the polygons and create the boolean mask image
00046   for (unsigned i=0; i<objs_.size(); i++)
00047   {
00048     vgl_polygon<double> v_poly =  objs_[i]->poly();
00049     vgl_polygon_scan_iterator<double> psi(v_poly, false);
00050     for (psi.reset(); psi.next();){
00051       int y = psi.scany();
00052       for (int x = psi.startx(); x<=psi.endx(); ++x)
00053       {
00054         unsigned u = static_cast<unsigned>(x);
00055         unsigned v = static_cast<unsigned>(y);
00056         if (objs_[i]->type().compare("dont_care")!=0)
00057           (*mask)(u,v) = 255;
00058         else  // don't care areas
00059           (*mask)(u,v) = 125;
00060       }
00061     }
00062   }
00063 
00064   return mask;
00065 }
00066 
00067 void bvgl_changes::add_obj(bvgl_change_obj_sptr obj)
00068 {
00069   objs_.push_back(obj);
00070 }
00071 
00072 void bvgl_changes::remove_obj(bvgl_change_obj_sptr obj)
00073 {
00074   vcl_vector<bvgl_change_obj_sptr>::iterator iter = objs_.begin();
00075   while (iter!=objs_.end()) {
00076     if (*iter == obj) {
00077       objs_.erase(iter);
00078       vcl_cout << "DELETED!" << vcl_endl;
00079       return;
00080     }
00081     iter++;
00082   }
00083   vcl_cout << "Object is no FOUND!" << vcl_endl;
00084 }
00085 
00086 #if 0
00087 void bvgl_changes::xml_read()
00088 {
00089 }
00090 
00091 void bvgl_changes::xml_write()
00092 {
00093 }
00094 #endif // 0
00095 
00096 //: Return IO version number;
00097 unsigned char
00098 bvgl_changes::version(  ) const
00099 {
00100   return 1;
00101 }
00102 
00103 //: binary IO write
00104 void bvgl_changes::b_write(vsl_b_ostream& os)
00105 {
00106   // first write the version number;
00107   unsigned char ver = version();
00108   vsl_b_write(os, ver);
00109 
00110   vsl_b_write(os, img_name_);
00111   vsl_b_write(os, objs_.size());
00112   for (unsigned i = 0; i < objs_.size(); i++) {
00113     objs_[i]->b_write(os);
00114   }
00115 }
00116 
00117 
00118 //: binary IO read
00119 void bvgl_changes::b_read(vsl_b_istream& is)
00120 {
00121   // first read the version number;
00122   unsigned char ver;
00123   vsl_b_read(is, ver);
00124 
00125   switch (ver)
00126   {
00127    case 1:
00128    {
00129     vsl_b_read(is, img_name_);
00130     unsigned size;
00131     vsl_b_read(is, size);
00132     for (unsigned i = 0; i < size; ++i) {
00133       bvgl_change_obj o;
00134       o.b_read(is);
00135       objs_.push_back(new bvgl_change_obj(o));
00136     }
00137     break;
00138    }
00139    default:
00140     vcl_cout << "In bvgl_changes::b_read() -- Unrecognized version number " << ver << vcl_endl;
00141     break;
00142   }
00143 
00144   return;
00145 }
00146 
00147 bvgl_change_obj_sptr
00148 bvgl_changes::obj(unsigned int i)
00149 {
00150   if (i<size())
00151     return objs_[i];
00152   return 0;
00153 }