Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SpaceTransform.h
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
13 /*
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public
16 * License as published by the Free Software Foundation; either
17 * version 2 of the License, or (at your option) any later version.
18 *
19 * This software is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public
25 * License along with this software. If not, see
26 * <http://www.gnu.org/licenses/>.
27 *
28 */
29 
30 #ifndef _NONA_SPACETRANSFORM_H
31 #define _NONA_SPACETRANSFORM_H
32 
33 #include <vigra/diff2d.hxx>
34 #include <hugin_math/Matrix3.h>
35 
36 #include <panodata/PanoramaData.h>
37 
38 
39 namespace HuginBase {
40 namespace Nona {
41 
42 
47 {
48  union {
49  double var0;
50  double distance;
51  double shift;
52  };
53  double var1;
54  double var2;
55  double var3;
56  double var4;
57  double var5;
58  double var6;
59  double var7;
61 };
62 
63 
67 typedef void (*trfn)( double x_dest, double y_dest, double* x_src, double* y_src, const _FuncParams &params );
68 
69 
73 typedef struct _fDesc
74 {
75  trfn func; // function to be called
76  _FuncParams param; // parameters to be used
77 } fDescription;
78 
79 
80 
85 {
86 
87  public:
91 
94  ~SpaceTransform();
95 
99  void Init( const SrcPanoImage & img,
100  const vigra::Diff2D & destSize,
102  double destHFOV );
103 
107  void InitInv( const SrcPanoImage & img,
108  const vigra::Diff2D & destSize,
110  double destHFOV );
111 
112 
113  // Different ways to create the transform
114 
115  public:
118  void InitRadialCorrect(const vigra::Size2D & sz, const std::vector<double> & radDist,
119  const hugin_utils::FDiff2D & centerShift);
120 
124  void InitRadialCorrect(const SrcPanoImage & src, int channel=1);
125 
126  void InitInvRadialCorrect(const SrcPanoImage & src, int channel=1);
127 
128  void createTransform(const SrcPanoImage & src, const PanoramaOptions & dest);
129 
130  void createInvTransform(const SrcPanoImage & src, const PanoramaOptions & dest);
131 
132  // create pano -> img transform
133  void createTransform(const PanoramaData & pano, unsigned int imgNr,
134  const PanoramaOptions & dest,
135  vigra::Diff2D srcSize=vigra::Diff2D(0,0));
136 
137  // create image->pano transformation
138  void createInvTransform(const PanoramaData & pano, unsigned int imgNr,
139  const PanoramaOptions & dest,
140  vigra::Diff2D srcSize=vigra::Diff2D(0,0));
141 
142  void createTransform(const vigra::Diff2D & srcSize,
143  const VariableMap & srcVars,
145  const vigra::Diff2D &destSize,
147  double destHFOV);
148 
149  // create image->pano transformation
150  void createInvTransform(const vigra::Diff2D & srcSize,
151  const VariableMap & srcVars,
153  const vigra::Diff2D & destSize,
155  double destHFOV);
156 
157 
158  public:
162  bool transform(hugin_utils::FDiff2D& dest, const hugin_utils::FDiff2D & src) const;
163 
167  bool transformImgCoord(double & x_dest, double & y_dest, double x_src, double y_src) const;
168 
170  {
171  return transformImgCoord(dest.x, dest.y, src.x, src.y);
172  }
173 
174 
175  public:
177  bool isIdentity()
178  {
179  return m_Stack.empty();
180  }
181 
182 
183  private:
185  void AddTransform( trfn function_name, double var0, double var1 = 0.0f, double var2 = 0.0f, double var3 = 0.0f, double var4=0.0f, double var5=0.0f, double var6=0.0f, double var7=0.0f );
186  void AddTransform( trfn function_name, Matrix3 m, double var0, double var1=0.0f, double var2=0.0f, double var3=0.0f);
187 
188 
189  private:
192 
194  double m_srcTX, m_srcTY;
195  double m_destTX, m_destTY;
196 
198  std::vector<fDescription> m_Stack;
199 
200 };
201 
202 
203 
204 
212 template <class VECTOR>
213 void combinePolynom4(const VECTOR & p,
214  const VECTOR & q,
215  VECTOR & c);
216 
217 
221 template <class TRANSFORM>
222 void traceImageOutline(vigra::Size2D sz,
223  TRANSFORM & transf,
224  vigra::Rect2D & inside,
225  vigra::Rect2D & boundingBox);
226 
227 
230 IMPEX double estScaleFactorForFullFrame(const SrcPanoImage & src);
231 
232 
248 double estRadialScaleCrop(const std::vector<double> & coeff, int width, int height);
249 
250 
251 
252 
253 
254 //==============================================================================
255 // template implementations
256 
257 template <class VECTOR>
258 void combinePolynom4(const VECTOR & p, const VECTOR & q, VECTOR & c)
259 {
260  double d3 = p[3]*p[3]*p[3];
261  c[0] = (3*q[1]*p[3]*p[3]*p[2]+q[3]*p[0]+q[2]*(2*p[3]*p[1]+p[2]*p[2])+q[0]*d3*p[3]);
262  c[1] = (2*q[2]*p[3]*p[2]+q[1]*d3+q[3]*p[1]);
263  c[2] = (q[3]*p[2]+q[2]*p[3]*p[3]);
264  c[3] = q[3]*p[3];
265 
266  /*
267  old code for polynoms of up to x^3
268  c[0] = q[0]*(p[2]*(2*p[3]*p[1]+p[2]*p[2])+p[3]*(2*p[3]*p[0]+2*p[2]*p[1])+p[0]*p[3]*p[3]+2*p[1]*p[3]*p[2])
269  +q[1]*(2*p[3]*p[0]+2*p[2]*p[1])+q[2]*p[0];
270 
271  c[1] = q[0]*(p[3]*(2*p[3]*p[1]+p[2]*p[2])+2*p[2]*p[2]*p[3]+p[1]*p[3]*p[3])
272  +q[2]*p[1]+q[1]*(2*p[3]*p[1]+p[2]*p[2]);
273 
274  c[2] = 3*q[0]*p[3]*p[3]*p[2]+2*q[1]*p[3]*p[2]+q[2]*p[2];
275  c[3] = q[0]*p[3]*p[3]*p[3]+q[1]*p[3]*p[3]+q[2]*p[3]+q[3];
276  */
277 }
278 
279 
280 template <class TRANSFORM>
281 void traceImageOutline(vigra::Size2D sz, TRANSFORM & transf, vigra::Rect2D & inside, vigra::Rect2D & boundingBox)
282 {
283  boundingBox = vigra::Rect2D();
284  inside = vigra::Rect2D();
285  int x=0;
286  int y=0;
287  double xd;
288  double yd;
289  transf.transformImgCoord(xd,yd, x,y);
290  // calculate scaling factor that would be required to avoid black borders
291 // double scale = std::max((-sz.x/2)/(xd-sz.x/2), (-sz.y/2)/(yd-sz.y/2));
292  int left = 0;
293  int right = sz.x;
294  int top = 0;
295  int bottom = sz.y;
296  // left
297  for (y=0; y < sz.y; y++) {
298  transf.transformImgCoord(xd,yd, x,y);
299  boundingBox |= vigra::Point2D(hugin_utils::roundi(xd), hugin_utils::roundi(yd));
300  left = std::max(hugin_utils::roundi(xd), left);
301  }
302  // right
303  x = sz.x-1;
304  for (y=0; y < sz.y; y++) {
305  transf.transformImgCoord(xd,yd, x,y);
306  boundingBox |= vigra::Point2D(hugin_utils::roundi(xd), hugin_utils::roundi(yd));
307  right = std::min(hugin_utils::roundi(xd), right);
308  }
309  // bottom
310  y=sz.y-1;
311  for (x=0; x < sz.x; x++) {
312  transf.transformImgCoord(xd,yd, x,y);
313  boundingBox |= vigra::Point2D(hugin_utils::roundi(xd), hugin_utils::roundi(yd));
314  bottom = std::min(hugin_utils::roundi(yd), bottom);
315  }
316  // top
317  y=0;
318  for (x=0; x < sz.x; x++) {
319  transf.transformImgCoord(xd,yd, x,y);
320  boundingBox |= vigra::Point2D(hugin_utils::roundi(xd), hugin_utils::roundi(yd));
321  top = std::max(hugin_utils::roundi(yd), top);
322  }
323  inside.setUpperLeft(vigra::Point2D(left, top));
324  inside.setLowerRight(vigra::Point2D(right, bottom));
325 }
326 
327 
328 
329 } // namespace
330 } // namespace
331 
332 #endif
void traceImageOutline(vigra::Size2D sz, TRANSFORM &transf, vigra::Rect2D &inside, vigra::Rect2D &boundingBox)
Internal function to estimate the image scaling required to avoid black stripes at the image borders...
#define var3
int roundi(T x)
Definition: hugin_math.h:73
void combinePolynom4(const VECTOR &p, const VECTOR &q, VECTOR &c)
combine 4rd degree polynomials
general : Matrix3 is a class for handling 3x3 Matrix manipulation.
Definition: Matrix3.h:37
#define var4
void(* trfn)(double x_dest, double y_dest, double *x_src, double *y_src, const _FuncParams &params)
Transformation function type.
Parameters for transformation calls Can be just one double, two double, 4 double, a matrix...
#define var5
Function descriptor to be executed by exec_function.
#define var0
Model for a panorama.
Definition: PanoramaData.h:81
std::vector< fDescription > m_Stack
vector of transformations
#define IMPEX
Definition: hugin_shared.h:39
std::map< std::string, Variable > VariableMap
bool isIdentity()
returns true if this transform is an identity transform
static T max(T x, T y)
Definition: svm.cpp:65
double estRadialScaleCrop(const std::vector< double > &coeff, int width, int height)
Calculate effective scaling factor.
#define var2
double estScaleFactorForFullFrame(const SrcPanoImage &src)
Calculate effective scaling factor for a given source image.
ProjectionFormat
Projection of final panorama.
All variables of a source image.
Definition: SrcPanoImage.h:194
Panorama image options.
bool m_Initialized
was the class initialized ?
#define var1
static T min(T x, T y)
Definition: svm.cpp:62
struct HuginBase::Nona::_fDesc fDescription
Function descriptor to be executed by exec_function.
bool transformImgCoord(hugin_utils::FDiff2D &dest, const hugin_utils::FDiff2D &src) const