Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PerspectiveImageCtrl.h
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
10 /* This is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This software is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this software. If not, see
22  * <http://www.gnu.org/licenses/>.
23  *
24  */
25 
26 #ifndef PerspectiveImageCtrl_H
27 #define PerspectiveImageCtrl_H
28 
29 #include <base_wx/wxImageCache.h>
30 #include <panoinc.h>
31 #include <lcms2.h>
32 
34 const int PerspectiveOffset = 200;
36 const int maxSelectionDistance = 10;
38 const int polygonPointSize = 3;
39 
43 class PerspectiveImageCtrl : public wxScrolledWindow
44 {
45 public:
46  // simple struct for lines
47  class Line
48  {
49  public:
51  // constructor for fast creation
52  Line() : start(hugin_utils::FDiff2D()), end(hugin_utils::FDiff2D()) {};
54  {
55  start = p1;
56  end = p2;
57  };
58  // swap start and end point
59  void Mirror()
60  {
62  start = end;
63  end = temp;
64  };
65  };
66 
68  bool Create(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxTAB_TRAVERSAL, const wxString& name = "panel");
69 
75 
77  void setOriginalMode();
78  bool IsOriginalShown() const { return !m_showRemappedImage; };
80  void SetRemappedMode(const HuginBase::Panorama& pano);
82  void setImage(const std::string& filename, ImageRotation rot);
83  void ChangeRotation(ImageRotation newRot);
84 
86  void OnMouseMove(wxMouseEvent& mouse);
88  void OnLeftMouseDown(wxMouseEvent& mouse);
90  void OnLeftMouseUp(wxMouseEvent& mouse);
92  void OnRightMouseUp(wxMouseEvent& mouse);
94  void OnMiddleMouseDown(wxMouseEvent& mouse);
96  void OnMiddleMouseUp(wxMouseEvent& mouse);
99  void OnCaptureLost(wxMouseCaptureLostEvent& e);
101  void OnKillFocus(wxFocusEvent& e);
102 
107  void setScale(double factor);
109  double getScale() const;
110 
114  void SetLineColour(wxColour newColour);
115 
117  void OnPaint(wxPaintEvent& e);
119  void SetRectMode(bool newMode);
121  HuginBase::CPVector GetControlPoints(const unsigned int index);
122 
124  ImageCache::EntryPtr getCachedImage() { return m_img; };
126  void AddLines(const HuginBase::CPVector& lines);
127 protected:
129  void OnSize(wxSizeEvent& e);
130 
132  double getScaleFactor() const;
134  double calcAutoScaleFactor(wxSize size);
136  void rescaleImage();
138  void UpdateVirtualSize();
139 
140 private:
143  HuginBase::ControlPoint GetCP(const unsigned int index, const hugin_utils::FDiff2D& p1, const hugin_utils::FDiff2D& p2);
145  void ClipPos(hugin_utils::FDiff2D& pos);
151  bool IsInsideRect(const hugin_utils::FDiff2D& p);
156  int GetNearestLinePoint(const hugin_utils::FDiff2D& p, bool& isStart);
158  int GetNearestLine(const hugin_utils::FDiff2D& p);
160  void GenerateRemappedImage(const unsigned int newWidth, const unsigned int newHeight);
161  //draw the given polygon
162  void DrawRect(wxDC& dc, const std::vector<hugin_utils::FDiff2D>& rect);
163  // draw lines
164  void DrawLines(wxDC& dc, const std::vector<Line>& lines);
165 
166  //scaled bitmap
167  wxBitmap m_bitmap;
169  wxBitmap m_remappedImg;
170 
171  //filename of current editing file
172  std::string m_imageFilename;
173  // stores rotation of image
175  // size of displayed (probably scaled) image
176  wxSize m_imageSize;
177  // size of real image
178  wxSize m_realSize;
181  int scale(int x) const
182  {
183  return (int)(x * getScaleFactor() + 0.5);
184  };
185  double scale(double x) const
186  {
187  return x * getScaleFactor();
188  };
190  int transform(int x) const
191  {
192  return (int)((x + PerspectiveOffset) * getScaleFactor() + 0.5);
193  };
194  double transform(double x) const
195  {
196  return (x + PerspectiveOffset) * getScaleFactor();
197  };
198  wxPoint transform(const hugin_utils::FDiff2D& p) const
199  {
200  wxPoint r;
201  r.x = transform(p.x);
202  r.y = transform(p.y);
203  return r;
204  };
206  int invtransform(int x) const
207  {
208  return (int)(x / getScaleFactor() - PerspectiveOffset + 0.5);
209  };
210  double invtransform(double x) const
211  {
212  return (x / getScaleFactor() - PerspectiveOffset);
213  };
214  hugin_utils::FDiff2D invtransform(const wxPoint& p) const
215  {
217  r.x = invtransform(p.x);
218  r.y = invtransform(p.y);
219  return r;
220  };
221  // rotate coordinate to fit possibly rotated image display
222  // useful for drawing something on the rotated display
223  template <class T>
224  T applyRot(const T& p) const
225  {
226  switch (m_imgRotation) {
227  case ROT0:
228  return p;
229  break;
230  case ROT90:
231  return T(m_realSize.GetHeight() - 1 - p.y, p.x);
232  break;
233  case ROT180:
234  return T(m_realSize.GetWidth() - 1 - p.x, m_realSize.GetHeight() - 1 - p.y);
235  break;
236  case ROT270:
237  return T(p.y, m_realSize.GetWidth() - 1 - p.x);
238  break;
239  default:
240  return p;
241  break;
242  }
243  }
244  // rotate coordinate to fit possibly rotated image display
245  // useful for converting rotated display coordinates to image coordinates
246  template <class T>
247  T applyRotInv(const T& p) const
248  {
249  switch (m_imgRotation) {
250  case ROT90:
251  return T(p.y, m_realSize.GetHeight() - 1 - p.x);
252  break;
253  case ROT180:
254  return T(m_realSize.GetWidth() - 1 - p.x, m_realSize.GetHeight() - 1 - p.y);
255  break;
256  case ROT270:
257  return T(m_realSize.GetWidth() - 1 - p.y, p.x);
258  break;
259  case ROT0:
260  default:
261  return p;
262  break;
263  }
264  }
265 
268  {
269  NO_IMAGE = 0, // no image selected
270  SHOW_IMAGE, // image loaded and rectangle
271  POINT_MOVING, // dragging points of the rectangle
272  SEGMENT_MOVING, // dragging line of the rectangle
273  RECT_MOVING, // dragging the whole rectangle
274  LINE_CREATE, // create a new line
275  LINE_POINT_MOVING, // dragging one point of the line
276  LINE_MOVING // dragging one line
277  };
279 
280  double m_scaleFactor{ 1.0 };
281  bool m_fitToWindow{ false };
282  bool m_showRemappedImage{ false };
283  bool m_showRect{ true };
284 
286  ImageCache::EntryPtr m_img;
289 
290  // positions of mouse drag
291  wxPoint m_dragStartPos;
292  wxPoint m_currentPos;
293  int m_movingPoint{ -1 };
294  // variable for saving scrolling state
295  bool m_middleMouseScroll{ false };
296  wxPoint m_scrollPos;
297 
298  // colours for different parts
300  // coordinates of the rectangle
301  std::vector<hugin_utils::FDiff2D> m_rectPoints, m_rectPointsStartDrag;
302  std::vector<Line> m_lines, m_currentLine;
304 
305  DECLARE_DYNAMIC_CLASS(PerspectiveImageCtrl)
306 };
307 
308 #endif // PerspectiveImageCtrl_H
void rescaleImage()
rescale the image
ImageCache::EntryPtr m_img
image cache entry for current image
void OnRightMouseUp(wxMouseEvent &mouse)
event handler when right mouse button is released
PerspectiveEditorState
different states of the editor
void OnSize(wxSizeEvent &e)
handler called when size of control was changed
void ChangeRotation(ImageRotation newRot)
std::vector< hugin_utils::FDiff2D > m_rectPoints
include file for the hugin project
int GetNearestLine(const hugin_utils::FDiff2D &p)
return index of nearest line or -1 if the point is too far from all lines
void setScale(double factor)
set the scaling factor for mask editing display.
void SetRectMode(bool newMode)
set line or rect mode
T applyRot(const T &p) const
void OnMiddleMouseDown(wxMouseEvent &mouse)
event handler for middle mouse button, start scrolling
represents a control point
Definition: ControlPoint.h:38
double calcAutoScaleFactor(wxSize size)
calculate new scale factor for this image
void DrawRect(wxDC &dc, const std::vector< hugin_utils::FDiff2D > &rect)
void ClipPos(hugin_utils::FDiff2D &pos)
clip the given pos to image size + offset
void OnPaint(wxPaintEvent &e)
drawing routine
void OnMouseMove(wxMouseEvent &mouse)
event handler when mouse is moving
bool Create(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxTAB_TRAVERSAL, const wxString &name="panel")
creates the control
double getScaleFactor() const
get scale factor (calculates factor when fit to window is active)
HuginBase::ControlPoint GetCP(const unsigned int index, const hugin_utils::FDiff2D &p1, const hugin_utils::FDiff2D &p2)
return the HuginBase::ControlPoint, using index as image index, p1 and p2 as position, take image rotation into account to decide if control point is horizontal or vertical
void setImage(const std::string &filename, ImageRotation rot)
set the current image and mask list, this loads also the image from cache
void SetRemappedMode(const HuginBase::Panorama &pano)
set the panorama object for remapping, the mouse handler are deactivated
int GetNearestRectanglePoint(const hugin_utils::FDiff2D &p)
return index of nearest rectangle point or -1 if the point is too far from rectangle ...
Model for a panorama.
Definition: Panorama.h:152
void AddLines(const HuginBase::CPVector &lines)
add the lines to the list
const int polygonPointSize
half size of markers
int scale(int x) const
helper function for scale, offset and rotation
const int PerspectiveOffset
size of border at all sides
void DrawLines(wxDC &dc, const std::vector< Line > &lines)
HuginBase::CPVector GetControlPoints(const unsigned int index)
return list of control points
void SetLineColour(wxColour newColour)
sets the colour for the lines
int transform(int x) const
convert image coordinate to screen coordinates, considers additional added border ...
std::vector< Line > m_lines
image previewer for perspective correction
double scale(double x) const
TDiff2D< double > FDiff2D
Definition: hugin_math.h:155
T applyRotInv(const T &p) const
PerspectiveEditorState m_editorState
ImageCache::EntryPtr getCachedImage()
return pointer to ImageCache
void OnLeftMouseDown(wxMouseEvent &mouse)
event handler when left mouse button is pressed
void OnLeftMouseUp(wxMouseEvent &mouse)
event handler when right mouse button is released
bool IsInsideRect(const hugin_utils::FDiff2D &p)
return true, if the point p is inside the rect
void setOriginalMode()
show the original images with selected zoom ration, the mouse handlers are activated ...
void GenerateRemappedImage(const unsigned int newWidth, const unsigned int newHeight)
generates the remapped image suitable for wxBitmap
int invtransform(int x) const
translate screen coordinates to image coordinates, considers additional added border ...
void OnCaptureLost(wxMouseCaptureLostEvent &e)
event handler, when mouse capture is lost, e.g.
ImageRotation
image rotation.
double transform(double x) const
Line(const hugin_utils::FDiff2D &p1, const hugin_utils::FDiff2D &p2)
int GetNearestRectangleLine(const hugin_utils::FDiff2D &p)
return index of nearest rectangle line or -1 if the point is too far from the rectangle lines ...
wxPoint transform(const hugin_utils::FDiff2D &p) const
std::vector< Line > m_currentLine
void OnMiddleMouseUp(wxMouseEvent &mouse)
event handler for middle mouse button, end scrolling
hugin_utils::FDiff2D invtransform(const wxPoint &p) const
const int maxSelectionDistance
maximal distance for selection of one point
std::vector< ControlPoint > CPVector
Definition: ControlPoint.h:99
std::vector< hugin_utils::FDiff2D > m_rectPointsStartDrag
void OnKillFocus(wxFocusEvent &e)
event handler, when editor lost focus, mainly cancels creating new polygon
double invtransform(double x) const
void UpdateVirtualSize()
update the virtual size of the control, necessary for correctly display the scrollbars ...
HuginBase::Panorama m_pano
HuginBase::Panorama object for calculation of remapped image.
wxBitmap m_remappedImg
the remapped image as wxBitmap
ImageRotation getCurrentRotation()
returns the current rotation of displayed image
double getScale() const
return scale factor, 0 for autoscale
int GetNearestLinePoint(const hugin_utils::FDiff2D &p, bool &isStart)
return index of nearest line or -1 if the point to far from line