Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

bmrf_arc.cxx

Go to the documentation of this file.
00001 // This is brl/bseg/bmrf/bmrf_arc.cxx
00002 #include "bmrf_arc.h"
00003 //:
00004 // \file
00005 
00006 #include <vsl/vsl_binary_io.h>
00007 #include <vbl/io/vbl_io_smart_ptr.h>
00008 #include <bmrf/bmrf_node.h>
00009 #include <bmrf/bmrf_epi_seg_compare.h>
00010 #include <bmrf/bmrf_gamma_func.h>
00011 #include <bmrf/bmrf_epi_transform.h>
00012 
00013 
00014 //: Constructor
00015 bmrf_arc::bmrf_arc()
00016   : from_(NULL), to_(NULL), probability_(-1.0),
00017     min_alpha_(0.0), max_alpha_(0.0),
00018     gamma_(0.0), inv_gamma_(0.0),
00019     avg_intensity_error_(0.0), induced_match_error_(0.0),
00020     gamma_func_(NULL)
00021 {
00022 }
00023 
00024 
00025 //: Copy constructor
00026 bmrf_arc::bmrf_arc(bmrf_arc const& a)
00027   : vbl_ref_count(), from_(a.from_), to_(a.to_), probability_(a.probability_),
00028     min_alpha_(a.min_alpha_), max_alpha_(a.max_alpha_),
00029     gamma_(a.gamma_), inv_gamma_(a.inv_gamma_),
00030     avg_intensity_error_(a.avg_intensity_error_), induced_match_error_(a.induced_match_error_),
00031     gamma_func_(NULL)
00032 {
00033 }
00034 
00035 
00036 //: Constructor
00037 bmrf_arc::bmrf_arc( const bmrf_node_sptr& f, const bmrf_node_sptr& t)
00038   : from_(f.ptr()), to_(t.ptr()), probability_(-1.0),
00039     min_alpha_(0.0), max_alpha_(0.0),
00040     gamma_(0.0), inv_gamma_(0.0),
00041     avg_intensity_error_(0.0), induced_match_error_(0.0),
00042     gamma_func_(NULL)
00043 {
00044   if ( from_ && to_ ) {
00045     if (from_->frame_num() != to_->frame_num())
00046       time_init();
00047   }
00048 }
00049 
00050 
00051 //: Produce a new arc which is the reverse of this one efficiently
00052 bmrf_arc_sptr
00053 bmrf_arc::reverse() const
00054 {
00055   bmrf_arc_sptr rev_arc = new bmrf_arc(*this);
00056   rev_arc->to_ = this->from_;
00057   rev_arc->from_ = this->to_;
00058   rev_arc->probability_ = -1.0;
00059   rev_arc->gamma_ = this->inv_gamma_;
00060   rev_arc->inv_gamma_ = this->gamma_;
00061   rev_arc->gamma_func_ = NULL;
00062 
00063   double t = double(rev_arc->time_step());
00064 
00065   bmrf_epi_seg_sptr ep1 = rev_arc->from_->epi_seg();
00066   bmrf_epi_seg_sptr ep2 = rev_arc->to_->epi_seg();
00067   bmrf_gamma_func_sptr gamma_inv = new bmrf_const_gamma_func(rev_arc->inv_gamma_);
00068   bmrf_epi_seg_sptr xform_seg = bmrf_epi_transform(ep2, gamma_inv, -t);
00069   rev_arc->induced_match_error_ = bmrf_match_error(ep1, xform_seg);
00070 
00071   return rev_arc;
00072 }
00073 
00074 
00075 //: Return the probability of this arc
00076 double
00077 bmrf_arc::probability()
00078 {
00079   if (from_->probability_ < 0.0)
00080     from_->compute_probability();
00081   return probability_;
00082 }
00083 
00084 
00085 //: The change in time spanned by this arc
00086 int
00087 bmrf_arc::time_step() const
00088 {
00089   if ( !to_ || !from_ )
00090     return 0;
00091   return to_->frame_num() - from_->frame_num();
00092 }
00093 
00094 
00095 //: Binary save bmrf_arc to stream.
00096 void
00097 bmrf_arc::b_write( vsl_b_ostream& ) const
00098 {
00099   // Nothing to write
00100 }
00101 
00102 
00103 //: Binary load bmrf_arc from stream.
00104 void
00105 bmrf_arc::b_read( vsl_b_istream& )
00106 {
00107   // Nothing to read
00108 }
00109 
00110 
00111 //: Compute the alpha range and intensity comparison
00112 void
00113 bmrf_arc::time_init()
00114 {
00115   if ( from_->epi_seg()->n_pts() <= 0 ||
00116        to_->epi_seg()->n_pts() <= 0 )
00117     return;
00118 
00119   bmrf_epi_seg_sptr ep1 = from_->epi_seg();
00120   bmrf_epi_seg_sptr ep2 = to_->epi_seg();
00121 
00122   min_alpha_ = bmrf_min_alpha(ep1, ep2);
00123   max_alpha_ = bmrf_max_alpha(ep1, ep2);
00124 
00125   avg_intensity_error_ = bmrf_intensity_error(ep1, ep2);
00126 
00127   int t = time_step();
00128   double dist_ratio = bmrf_avg_distance_ratio(ep1, ep2);
00129   gamma_ = (1.0 - dist_ratio) / t;
00130 
00131   dist_ratio = bmrf_avg_distance_ratio(ep2, ep1);
00132   inv_gamma_ = (1.0 - dist_ratio) / -t;
00133 
00134   bmrf_gamma_func_sptr gamma_inv = new bmrf_const_gamma_func(inv_gamma_);
00135   bmrf_epi_seg_sptr xform_seg = bmrf_epi_transform(ep2, gamma_inv, double(-t));
00136   induced_match_error_ = bmrf_match_error(ep1, xform_seg);
00137 }
00138 
00139 
00140 //: Return the piecewise linear gamma function fit to the pair
00141 bmrf_gamma_func_sptr 
00142 bmrf_arc::gamma_func(){ 
00143   if(!gamma_func_)
00144     gamma_func_ = new bmrf_pwl_gamma_func( from_->epi_seg(), 
00145                                            to_->epi_seg(), 
00146                                            double(time_step()) );
00147   return gamma_func_; 
00148 }
00149 
00150 
00151 //-----------------------------------------------------------------------------------------
00152 // External functions
00153 //-----------------------------------------------------------------------------------------
00154 
00155 
00156 //: Binary save bmrf_arc \p a to stream.
00157 void
00158 vsl_b_write(vsl_b_ostream &os, const bmrf_arc* a)
00159 {
00160   if (a==0) {
00161     vsl_b_write(os, false); // Indicate null pointer stored
00162   }
00163   else{
00164     vsl_b_write(os,true); // Indicate non-null pointer stored
00165     a->b_write(os);
00166   }
00167 }
00168 
00169 
00170 //: Binary load bmrf_arc \p a from stream.
00171 void
00172 vsl_b_read(vsl_b_istream &is, bmrf_arc* &a)
00173 {
00174   delete a;
00175   bool not_null_ptr;
00176   vsl_b_read(is, not_null_ptr);
00177   if (not_null_ptr) {
00178     a = new bmrf_arc();
00179     a->b_read(is);
00180   }
00181   else
00182     a = 0;
00183 }
00184 
00185 
00186 //: Print an ASCII summary of a bmrf_arc to the stream (NYI)
00187 void
00188 vsl_print_summary(vcl_ostream &os, const bmrf_arc* )
00189 {
00190   os << "bmrf_arc{}";
00191 }
00192 

Generated on Thu Jan 10 14:51:52 2008 for contrib/brl/bseg/bmrf by  doxygen 1.4.4