ThreeB 1.1
multispot5.h
Go to the documentation of this file.
00001 #ifndef MULTISPOT5_H
00002 #define MULTISPOT5_H
00003 #include <vector>
00004 #include <string>
00005 #include <fstream>
00006 #include <iostream>
00007 #include <tr1/memory>
00008 #include <cvd/image.h>
00009 #include <cvd/byte.h>
00010 #include <TooN/TooN.h>
00011 #include <TooN/so2.h>
00012 
00013 #include "utility.h"
00014 #include "mt19937.h"
00015 
00016 
00017 /// Graphics class for FittingSpots.
00018 /// This abstraction prevents FitSpots from depending on and graphics library.
00019 /// The functions are tied very heavily to the internals of FitSpots.
00020 /// @ingroup gStorm
00021 class FitSpotsGraphics
00022 {
00023     public:
00024         ///Initialize graphics
00025         ///@param size Size of window for display
00026         virtual void init(CVD::ImageRef size)=0;
00027 
00028         ///Draw a bunch of stuff
00029         ///@param spots List of spots to draw
00030         ///@param im Background image
00031         ///@param box Bounding box of region
00032         ///@param N Spot to highlight
00033         ///@param s Extra spot to draw as a cross
00034         virtual void draw_krap(const std::vector<TooN::Vector<4> >& spots, const CVD::Image<CVD::byte>& im, const BBox& box, int N, TooN::Vector<4> s = TooN::Ones * 1e99)=0;
00035 
00036         ///Swap buffers if double buffered
00037         virtual void swap()=0;
00038 
00039         ///Draw the pixel mask in an (r,g,b,a) tuple colour
00040         ///@param pix mask
00041         ///@param r red
00042         ///@param g green
00043         ///@param b blue
00044         ///@param a alpha
00045         virtual void draw_pixels(const std::vector<CVD::ImageRef>& pix, float r, float g, float b, float a=1)=0;
00046 
00047         ///Draw a bounding box
00048         ///@param bbox Box corners
00049         virtual void draw_bbox(const BBox& bbox)=0;
00050 
00051         ///Draw a cross
00052         ///@param p Position of cross
00053         ///@param size Size to draw cross
00054         virtual void glDrawCross(const TooN::Vector<2>& p, int size=3)=0;
00055 
00056         ///Desctructor
00057         virtual ~FitSpotsGraphics();
00058 };
00059 
00060 
00061 /// Callback class used by FitSpots to provide enough hooks for a user interface.
00062 /// @ingroup gStorm
00063 class UserInterfaceCallback
00064 {
00065     public:
00066         ///This function is called once per spot in each pass. The idea is to
00067         ///provide a display along the lines of: Iteration #1 optimizing #2% complete
00068         ///@param iteration Iteration number
00069         ///@param pass      Pass number
00070         ///@param spot_num  Spot currently being optimized
00071         ///@param total_spots Total number of spots to be optimized
00072         virtual void per_spot(int iteration, int pass, int spot_num, int total_spots)=0;
00073 
00074         ///This function is called once per spot in the modification phase. The idea is to
00075         ///provide a display along the lines of: Iteration #1 modifying #2% complete
00076         ///@param iteration Iteration number
00077         ///@param spot_num  Spot currently being optimized
00078         ///@param total_spots Total number of spots to be optimized
00079         virtual void per_modification(int iteration, int spot_num, int total_spots)=0;
00080 
00081         ///This function is called once each time PASS data is outputted.
00082         ///It will allow the GUI to build up a reconstruction.
00083         ///@param iteration Iteration number
00084         ///@param pass      Pass number
00085         ///@param spots     Data to be reconstructed
00086         virtual void per_pass(int iteration, int pass, const std::vector<TooN::Vector<4> >& spots)=0;
00087 
00088         ///The user wishes to issue a stop instruction to the program (perhaps done via an 
00089         ///asynchronus call to an instance of of UserInterfaceCallback). This function is 
00090         ///called as often as possible and will throw UserIssuedStop when the condition is
00091         ///met.
00092         virtual void perhaps_stop()=0;
00093         
00094         virtual ~UserInterfaceCallback();
00095         struct UserIssuedStop{};
00096 };
00097 
00098 std::auto_ptr<FitSpotsGraphics> null_graphics();
00099 std::auto_ptr<UserInterfaceCallback> null_ui();
00100 
00101 ///Null struct thrown if a parse error is encountered when trying to load a log file.
00102 struct LogFileParseError
00103 {
00104     ///@param s error message to set
00105     LogFileParseError(const std::string &s)
00106     :what(s)
00107     {}
00108     
00109     /// variable holding the parse error error message
00110     std::string what;
00111 };
00112 
00113 ///Internal state (excluding fixed settings) which represents the entire
00114 ///internal state of spot fitting. Used to restart from interruptions.
00115 struct StateParameters{
00116     std::tr1::shared_ptr<MT19937> rng;    ///< Random number generator state
00117     std::vector<TooN::Vector<4> > spots;  ///< Spots positions 
00118     int pass;                             ///< Pass number
00119     int iteration;                        ///< Iteration number
00120     std::vector<CVD::ImageRef> pixels;    ///< Area for analysis
00121 };
00122 
00123 StateParameters generate_state_parameters_ye_olde(const CVD::BasicImage<double>& log_ratios, const std::vector<CVD::Image<float> >& ims, std::vector<CVD::ImageRef> pixels);
00124 
00125 void fit_spots_new(const std::vector<CVD::Image<float> >& ims, StateParameters& p, std::ofstream& save_spots, FitSpotsGraphics&);
00126 void fit_spots_new(const std::vector<CVD::Image<float> >& ims, StateParameters& p, std::ofstream& save_spots, FitSpotsGraphics&, UserInterfaceCallback&);
00127 
00128 StateParameters parse_log_file(std::istream& in);
00129 #endif