ThreeB 1.1
multispot5.h
Go to the documentation of this file.
00001 /*
00002     This file is part of B-cubed.
00003 
00004     Copyright (C) 2009, 2010, 2011, Edward Rosten and Susan Cox
00005 
00006     B-cubed is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Lesser General Public
00008     License as published by the Free Software Foundation; either
00009     version 3.0 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Lesser General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License     
00017     along with this program.  If not, see <http://www.gnu.org/licenses/>
00018 */
00019 
00020 #ifndef MULTISPOT5_H
00021 #define MULTISPOT5_H
00022 #include <vector>
00023 #include <string>
00024 #include <fstream>
00025 #include <iostream>
00026 #include <tr1/memory>
00027 #include <cvd/image.h>
00028 #include <cvd/byte.h>
00029 #include <TooN/TooN.h>
00030 #include <TooN/so2.h>
00031 
00032 #include "utility.h"
00033 #include "mt19937.h"
00034 
00035 
00036 /// Graphics class for FittingSpots.
00037 /// This abstraction prevents FitSpots from depending on and graphics library.
00038 /// The functions are tied very heavily to the internals of FitSpots.
00039 /// @ingroup gStorm
00040 class FitSpotsGraphics
00041 {
00042     public:
00043         ///Initialize graphics
00044         ///@param size Size of window for display
00045         virtual void init(CVD::ImageRef size)=0;
00046 
00047         ///Draw a bunch of stuff
00048         ///@param spots List of spots to draw
00049         ///@param im Background image
00050         ///@param box Bounding box of region
00051         ///@param N Spot to highlight
00052         ///@param s Extra spot to draw as a cross
00053         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;
00054 
00055         ///Swap buffers if double buffered
00056         virtual void swap()=0;
00057 
00058         ///Draw the pixel mask in an (r,g,b,a) tuple colour
00059         ///@param pix mask
00060         ///@param r red
00061         ///@param g green
00062         ///@param b blue
00063         ///@param a alpha
00064         virtual void draw_pixels(const std::vector<CVD::ImageRef>& pix, float r, float g, float b, float a=1)=0;
00065 
00066         ///Draw a bounding box
00067         ///@param bbox Box corners
00068         virtual void draw_bbox(const BBox& bbox)=0;
00069 
00070         ///Draw a cross
00071         ///@param p Position of cross
00072         ///@param size Size to draw cross
00073         virtual void glDrawCross(const TooN::Vector<2>& p, int size=3)=0;
00074 
00075         ///Desctructor
00076         virtual ~FitSpotsGraphics();
00077 };
00078 
00079 
00080 /// Callback class used by FitSpots to provide enough hooks for a user interface.
00081 /// @ingroup gStorm
00082 class UserInterfaceCallback
00083 {
00084     public:
00085         ///This function is called once per spot in each pass. The idea is to
00086         ///provide a display along the lines of: Iteration #1 optimizing #2% complete
00087         ///@param iteration Iteration number
00088         ///@param pass      Pass number
00089         ///@param spot_num  Spot currently being optimized
00090         ///@param total_spots Total number of spots to be optimized
00091         virtual void per_spot(int iteration, int pass, int spot_num, int total_spots)=0;
00092 
00093         ///This function is called once per spot in the modification phase. The idea is to
00094         ///provide a display along the lines of: Iteration #1 modifying #2% complete
00095         ///@param iteration Iteration number
00096         ///@param pass      Pass number
00097         ///@param spot_num  Spot currently being optimized
00098         ///@param total_spots Total number of spots to be optimized
00099         virtual void per_modification(int iteration, int spot_num, int total_spots)=0;
00100 
00101         ///This function is called once each time PASS data is outputted.
00102         ///It will allow the GUI to build up a reconstruction.
00103         ///@param iteration Iteration number
00104         ///@param pass      Pass number
00105         ///@param spots     Data to be reconstructed
00106         ///@param total_spots Total number of spots to be optimized
00107         virtual void per_pass(int iteration, int pass, const std::vector<TooN::Vector<4> >& spots)=0;
00108 
00109         ///The user wishes to issue a stop instruction to the program (perhaps done via an 
00110         ///asynchronus call to an instance of of UserInterfaceCallback). This function is 
00111         ///called as often as possible and will throw UserIssuedStop when the condition is
00112         ///met.
00113         virtual void perhaps_stop()=0;
00114         
00115         virtual ~UserInterfaceCallback();
00116         struct UserIssuedStop{};
00117 };
00118 
00119 std::auto_ptr<FitSpotsGraphics> null_graphics();
00120 std::auto_ptr<UserInterfaceCallback> null_ui();
00121 
00122 ///Null struct thrown if a parse error is encountered when trying to load a log file.
00123 struct LogFileParseError
00124 {
00125     ///@param s error message to set
00126     LogFileParseError(const std::string &s)
00127     :what(s)
00128     {}
00129     
00130     /// variable holding the parse error error message
00131     std::string what;
00132 };
00133 
00134 ///Internal state (excluding fixed settings) which represents the entire
00135 ///internal state of spot fitting. Used to restart from interruptions.
00136 struct StateParameters{
00137     std::tr1::shared_ptr<MT19937> rng;    ///< Random number generator state
00138     std::vector<TooN::Vector<4> > spots;  ///< Spots positions 
00139     int pass;                             ///< Pass number
00140     int iteration;                        ///< Iteration number
00141     std::vector<CVD::ImageRef> pixels;    ///< Area for analysis
00142 };
00143 
00144 StateParameters generate_state_parameters_ye_olde(const CVD::BasicImage<double>& log_ratios, const std::vector<CVD::Image<float> >& ims, std::vector<CVD::ImageRef> pixels);
00145 
00146 void fit_spots_new(const std::vector<CVD::Image<float> >& ims, StateParameters& p, std::ofstream& save_spots, FitSpotsGraphics&);
00147 void fit_spots_new(const std::vector<CVD::Image<float> >& ims, StateParameters& p, std::ofstream& save_spots, FitSpotsGraphics&, UserInterfaceCallback&);
00148 
00149 StateParameters parse_log_file(std::istream& in);
00150 #endif