contrib/mul/mcal/mcal_single_basis_cost.h
Go to the documentation of this file.
00001 #ifndef mcal_single_basis_cost_h
00002 #define mcal_single_basis_cost_h
00003 //:
00004 // \file
00005 // \author Tim Cootes
00006 // \brief Base for objects which compute a cost function for one basis direction
00007 
00008 #include <vsl/vsl_fwd.h>
00009 #include <vcl_string.h>
00010 #include <vcl_memory.h>
00011 #include <vnl/vnl_vector.h>
00012 #include <vcl_iosfwd.h>
00013 
00014 //: Base for objects which compute a cost function for one basis direction.
00015 // We assume that the component analysis aims to choose a set of orthogonal
00016 // directions which minimise a cost function which can we expressed
00017 // as a sum of terms, one per basis vector.
00018 //
00019 // Objects derived from this class are responsible for computing that
00020 // component of the total cost function.
00021 //
00022 // Some cost functions only require the variance of the projection of the
00023 // data onto the direction of interest (about the origin).  In that case
00024 // the can_use_variance() should return true, and the cost_from_variance()
00025 // function implemented.
00026 class mcal_single_basis_cost
00027 {
00028  public:
00029 
00030   //: Dflt ctor
00031   mcal_single_basis_cost();
00032 
00033   //: Destructor
00034   virtual ~mcal_single_basis_cost();
00035 
00036   //: Returns true if cost can be computed from the variance, rather than requiring all the data
00037   virtual bool can_use_variance() const =0;
00038 
00039   //: Compute component of the cost function from given basis vector
00040   // \param[in] unit_basis   Unit vector defining basis direction
00041   // \param[in] projections  Projections of the dataset onto this basis vector
00042   virtual double cost(const vnl_vector<double>& unit_basis,
00043                       const vnl_vector<double>& projections) =0;
00044 
00045   //: Compute component of the cost function from given basis vector
00046   // Only relevant if can_use_variance() is true.  This allows more
00047   // efficient function evaluation in that case.
00048   // \param[in] unit_basis Unit vector defining basis direction
00049   // \param[in] variance   Variance of projections of the dataset onto this basis vector
00050   virtual double cost_from_variance(const vnl_vector<double>& unit_basis,
00051                                     double variance) =0;
00052 
00053   //: Version number for I/O
00054   short version_no() const;
00055 
00056   //: Name of the class
00057   virtual vcl_string is_a() const = 0;
00058 
00059   //: Create a copy on the heap and return base class pointer
00060   virtual mcal_single_basis_cost* clone() const = 0;
00061 
00062   //: Print class to os
00063   virtual void print_summary(vcl_ostream& os) const =0;
00064 
00065   //: Save class to binary file stream
00066   virtual void b_write(vsl_b_ostream& bfs) const=0;
00067 
00068   //: Load class from binary file stream
00069   virtual void b_read(vsl_b_istream& bfs)=0;
00070 
00071   //: Read initialisation settings from a stream.
00072   // The default implementation merely checks that no properties have
00073   // been specified.
00074   virtual void config_from_stream(vcl_istream &);
00075 
00076   //: Create a concrete mcal_single_basis_cost object, from a text specification.
00077   static vcl_auto_ptr<mcal_single_basis_cost> create_from_stream(vcl_istream &is);
00078 };
00079 
00080 //: Allows derived class to be loaded by base-class pointer
00081 //  A loader object exists which is invoked by calls
00082 //  of the form "vsl_b_read(bfs,base_ptr);".  This loads derived class
00083 //  objects from the disk, places them on the heap and
00084 //  returns a base class pointer.
00085 //  In order to work the loader object requires
00086 //  an instance of each derived class that might be
00087 //  found.  This function gives the model class to
00088 //  the appropriate loader.
00089 void vsl_add_to_binary_loader(const mcal_single_basis_cost& b);
00090 
00091 //: Binary file stream output operator for class reference
00092 void vsl_b_write(vsl_b_ostream& bfs, const mcal_single_basis_cost& b);
00093 
00094 //: Binary file stream input operator for class reference
00095 void vsl_b_read(vsl_b_istream& bfs, mcal_single_basis_cost& b);
00096 
00097 //: Stream output operator for class reference
00098 vcl_ostream& operator<<(vcl_ostream& os,const mcal_single_basis_cost& b);
00099 
00100 //: Stream output operator for class pointer
00101 vcl_ostream& operator<<(vcl_ostream& os,const mcal_single_basis_cost* b);
00102 
00103 #endif // mcal_single_basis_cost_h