contrib/gel/vtol/vtol_intensity_face.cxx
Go to the documentation of this file.
00001 // This is gel/vtol/vtol_intensity_face.cxx
00002 #include "vtol_intensity_face.h"
00003 //:
00004 // \file
00005 
00006 #include <vcl_vector.h>
00007 #include <vnl/vnl_matrix.h>
00008 #include <vdgl/vdgl_digital_region.h>
00009 #include <vsol/vsol_curve_2d.h>
00010 #include <vsol/vsol_curve_2d_sptr.h>
00011 #include <vtol/vtol_one_chain.h>
00012 #include <vtol/vtol_one_chain_sptr.h>
00013 #include <vtol/vtol_edge.h>
00014 #include <vtol/vtol_edge_2d.h>
00015 #include <vtol/vtol_face_2d.h>
00016 
00017 //---------------------------------------------------------------
00018 //: Constructors
00019 //
00020 //----------------------------------------------------------------
00021 
00022 //: A constructor from an existing face
00023 vtol_intensity_face::vtol_intensity_face(vtol_face_2d_sptr const& f)
00024   : vtol_face_2d(f),
00025     region_(new vdgl_digital_region())
00026 {
00027 }
00028 
00029 //: Copy constructor
00030 vtol_intensity_face::vtol_intensity_face(vtol_intensity_face_sptr const& f)
00031   : vtol_face_2d(f->cast_to_face_2d()),
00032     region_(new vdgl_digital_region(f->Npix(), f->Xj(), f->Yj(), f->Ij()))
00033 {
00034 }
00035 
00036 //----------------------------------------------------------------
00037 //: A similar constructor using previously formed vtol_one_chain(s)
00038 //    The list is structured with the outer boundary as the first
00039 //    element of one_chains and the interior hole boundaries as
00040 //    the remaining elements of the list.
00041 vtol_intensity_face::vtol_intensity_face(one_chain_list & one_chains)
00042   : vtol_face_2d(one_chains), region_(new vdgl_digital_region())
00043 {
00044 }
00045 
00046 //: Uses given 2-d vtol_one_chain(s) (not deep copy) with intensity information from dr.
00047 vtol_intensity_face::vtol_intensity_face(vcl_vector<vtol_one_chain_sptr>* chains, vdgl_digital_region const& dr)
00048   : vtol_face_2d(*chains), region_(new vdgl_digital_region(dr.Npix(), dr.Xj(), dr.Yj(), dr.Ij()))
00049 {
00050 }
00051 
00052 //-------------------------------------------------------------------
00053 //: Constructor from various vtol_face_2d class/subclasses.
00054 //    These constructors carry out a deep copy of the vtol_face_2d. Adjacent
00055 //    face topology is lost.
00056 
00057 vtol_intensity_face::vtol_intensity_face(vtol_face_2d_sptr const& face, int npts, float const* xp, float const* yp,
00058                                          unsigned short const* pix)
00059   :vtol_face_2d(face),
00060    region_(new vdgl_digital_region(npts, xp, yp, pix))
00061 {
00062 }
00063 
00064 //Default Destructor
00065 vtol_intensity_face::~vtol_intensity_face()
00066 {
00067   delete region_;
00068 }
00069 
00070 
00071 //---------------------------------------------------------------------------
00072 //: Clone `this': creation of a new object and initialization
00073 // See Prototype pattern
00074 //---------------------------------------------------------------------------
00075 vsol_spatial_object_2d* vtol_intensity_face::clone() const
00076 {
00077   return new vtol_intensity_face(vtol_intensity_face_sptr(const_cast<vtol_intensity_face*>(this)));
00078 }
00079 
00080 //----------------------------------------------------
00081 //: A local method for the moment matrix.
00082 //    The scatter matrix is defined
00083 //    with a coordinate origin at the centroid while the moment matrix is
00084 //    in the face coordinate system
00085 //
00086 vnl_matrix<double> vtol_intensity_face::MomentMatrix()
00087 {
00088   vnl_matrix<double> M(3,3);
00089 
00090   M(0,0) = X2()+(Ix()*Ix()/Npix()); M(0,1) = XY()+(Ix()*Iy()/Npix()); M(0,2) = Ix();
00091   M(1,0) = M(0,1);                  M(1,1) = Y2()+(Iy()*Iy()/Npix()); M(1,2) = Iy();
00092   M(2,0) = Ix();                    M(2,1) = Iy();                    M(2,2) = Npix();
00093   return M;
00094 }
00095 
00096 //-----------------------------------------------------------
00097 //: Compute the total face boundary perimeter.
00098 //    Uses the length of EdgelChain(s)
00099 // MPP 5/9/2003
00100 // Resurrected from #ifdef'd block below for intensity face attributes
00101 double vtol_intensity_face::perimeter()
00102 {
00103   edge_list edges; this->edges(edges);
00104   double  p = 0.0;
00105 
00106   for (edge_list::iterator eit = edges.begin(); eit != edges.end(); eit++)
00107   {
00108     vsol_curve_2d_sptr  c = (*eit)->cast_to_edge_2d()->curve();
00109     if (c)
00110       p += c->length();
00111   }
00112   return p;
00113 }
00114 
00115 #if 0 //nobody appears to call these methods
00116 //-----------------------------------------------
00117 //: The extrema of the face along a line of given orientation
00118 //
00119 void vtol_intensity_face::extrema(vcl_vector<float>& orientation,
00120                                   float& fmin, float& fmax)
00121 {
00122   //Iterate through the pixels of the face and determine the
00123   //extrema of the face projection.
00124   float fmin = vnl_numeric_traits<float>::maxval,
00125         fmax = -vnl_numeric_traits<float>::maxval;
00126   for (this->reset(); this->next();)
00127   {
00128     //The coordinates of each region pixel
00129     float xi = float(this->X())+.5, yi = float(this->Y())+.5;
00130     //Project onto the orientation direction
00131     float c = orientation.x(), s = orientation.y();
00132     float w = (xi*c + yi*s);
00133     //Update the extrema
00134     if (w<fmin) fmin = w;
00135     if (w>fmax) fmax = w;
00136   }
00137 }
00138 
00139 bool vtol_intensity_face::TaggedTransform(CoolTransform const& t)
00140 {
00141   if (this->GetId())
00142     return true;
00143   //First transform the boundary of the vtol_face_2d
00144   if (!vtol_face_2d::TaggedTransform(t))//this sets the vtol_face_2d Id
00145     return false;
00146   //Then transform the interior pixels, no Id change
00147   return vdgl_digital_region::Transform(t);
00148 }
00149 
00150 //-----------------------------------------------------------
00151 //: Compute the histogram of the intensities of the adjacent faces
00152 //
00153 Histogram_ref vtol_intensity_face::GetAdjacentRegionHistogram()
00154 {
00155   //Get the adjacent faces
00156   float mini=this->get_min(), maxi=this->get_max();
00157   vcl_vector<vtol_face_2d*> adj_faces;
00158   this->GetAdjacentFaces(adj_faces);
00159   //iterate through and collect intensity bounds
00160   iface_list afs;
00161   for (vcl_vector<vtol_face_2d*>::iterator fit = adj_faces.begin();
00162        fit != adj_faces.end(); fit++)
00163   {
00164     vtol_intensity_face_sptr af= (*fit)->cast_to_intensity_face();
00165     if (!af)
00166       continue;
00167     afs.push_back(af);
00168     if (af->get_min() < mini) mini = af->get_min();
00169     if (af->get_max() > maxi) maxi = af->get_max();
00170   }
00171   //The histogram of the adjacent regions
00172   Histogram_ref ha = new Histogram(100, mini, maxi);
00173   for (iface_iterator ifit = afs.begin(); ifit != afs.end(); ifit++)
00174     for ((*ifit)->reset(); (*ifit)->next();)
00175       ha->UpCount((*ifit)->I());
00176 
00177   return ha;
00178 }
00179 
00180 //-----------------------------------------------------------
00181 //: Compute the mean intensity of adjacent Face(s)
00182 //
00183 float vtol_intensity_face::GetAdjacentRegionMean()
00184 {
00185     //Get the adjacent faces
00186   vcl_vector<vtol_face_2d*> adj_faces;
00187   this->adjacent_faces(adj_faces);
00188   //iterate through and accumulate the mean
00189   float area =0;
00190   float mean = 0;
00191   for (vcl_vector<vtol_face_2d*>::iterator fit = adj_faces.begin();
00192        fit != adj_faces.end(); fit++)
00193   {
00194     vtol_intensity_face_sptr af= (*fit)->cast_to_intensity_face();
00195     if (!af)
00196       continue;
00197     float n = af->Npix();
00198     area += n;
00199     mean += n*af->Io();
00200   }
00201   if (!area)
00202     return 0;
00203   mean/=area;
00204   return mean;
00205 }
00206 #endif // 0