Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GraphTools.cpp
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
10 /* This program 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  * 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 #include "panoinc_WX.h"
27 #include "GraphTools.h"
29 #include "base_wx/wxutils.h"
30 
31 namespace wxGraphTools
32 {
33 IMPLEMENT_CLASS(GraphPopupWindow, wxPopupTransientWindow)
34 
35 GraphPopupWindow::GraphPopupWindow(wxWindow* parent, wxBitmap bitmap) : wxPopupTransientWindow(parent)
36 {
37  m_bitmapControl = new wxStaticBitmap(this, wxID_ANY, bitmap);
38  m_bitmapControl->Bind(wxEVT_LEFT_DOWN, &GraphPopupWindow::OnLeftDown, this);
39  m_bitmapControl->Bind(wxEVT_RIGHT_DOWN, &GraphPopupWindow::OnRightDown, this);
40  wxBoxSizer* topsizer = new wxBoxSizer(wxHORIZONTAL);
41  topsizer->Add(m_bitmapControl, wxEXPAND);
42  SetSizerAndFit(topsizer);
43 };
44 
45 void GraphPopupWindow::OnLeftDown(wxMouseEvent &e)
46 {
47  Dismiss();
48 };
49 
50 void GraphPopupWindow::OnRightDown(wxMouseEvent &e)
51 {
52  wxConfigBase* config = wxConfigBase::Get();
53  wxFileDialog dlg(GetParent(),
54  _("Save graph"),
55  config->Read("/actualPath", wxEmptyString), wxEmptyString,
56  _("Bitmap (*.bmp)|*.bmp|PNG-File (*.png)|*.png"),
57  wxFD_SAVE | wxFD_OVERWRITE_PROMPT, wxDefaultPosition);
58  dlg.SetDirectory(config->Read("/actualPath", wxEmptyString));
59  dlg.SetFilterIndex(config->Read("/lastImageTypeIndex", 0l));
60  if (dlg.ShowModal() == wxID_OK)
61  {
62  config->Write("/actualPath", dlg.GetDirectory()); // remember for later
63  wxFileName filename(dlg.GetPath());
64  int imageType = dlg.GetFilterIndex();
65  config->Write("/lastImageTypeIndex", imageType);
66  if (!filename.HasExt())
67  {
68  switch (imageType)
69  {
70  case 1:
71  filename.SetExt("png");
72  break;
73  case 0:
74  default:
75  filename.SetExt("bmp");
76  break;
77  };
78  };
79  if (filename.FileExists())
80  {
81  if (!hugin_utils::AskUserOverwrite(filename.GetFullPath(), _("Hugin"), GetParent()))
82  {
83  return;
84  }
85  }
86  switch (imageType)
87  {
88  case 1:
89  m_bitmapControl->GetBitmap().SaveFile(filename.GetFullPath(), wxBITMAP_TYPE_PNG);
90  break;
91  case 0:
92  default:
93  m_bitmapControl->GetBitmap().SaveFile(filename.GetFullPath(), wxBITMAP_TYPE_BMP);
94  break;
95  };
96  };
97 };
98 
99 /*help class to draw charts */
100 Graph::Graph(int graphWidth, int graphHeight, wxColour backgroundColour)
101 {
102  m_width = graphWidth;
103  m_height = graphHeight;
104  //create bitmap
105  m_bitmap = new wxBitmap(m_width, m_height);
106  m_dc.SelectObject(*m_bitmap);
107  m_dc.SetBackground(wxBrush(backgroundColour));
108  m_dc.Clear();
109  //draw border
110  m_dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)));
111  m_dc.SetBrush(*wxTRANSPARENT_BRUSH);
112  m_dc.DrawRectangle(0, 0, m_width, m_height);
113  SetChartArea(0, 0, m_width, m_height);
114  SetChartDisplay(0, 0, 1, 1);
115 };
116 
119 {
120  m_dc.SelectObject(wxNullBitmap);
121 };
122 
124 void Graph::SetChartArea(int left, int top, int right, int bottom)
125 {
126  m_dc.DestroyClippingRegion();
127  m_left = left;
128  m_top = top;
129  m_right = right;
130  m_bottom = bottom;
131  m_dc.SetClippingRegion(m_left - 1, m_top - 1, m_right - m_left + 2, m_bottom - m_top + 2);
132 };
133 
135 void Graph::SetChartDisplay(double xmin, double ymin, double xmax, double ymax)
136 {
137  m_xmin = xmin;
138  m_ymin = ymin;
139  m_xmax = xmax;
140  m_ymax = ymax;
141 };
142 
144 void Graph::DrawGrid(size_t linesX, size_t linesY)
145 {
146  m_dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)));
147  for (size_t i = 0; i < linesX; i++)
148  {
149  int x = TransformX((double)i*(m_xmax - m_xmin) / (linesX - 1) + m_xmin);
150  m_dc.DrawLine(x, m_top, x, m_bottom);
151  };
152  for (size_t i = 0; i < linesY; i++)
153  {
154  int y = TransformY((double)i*(m_ymax - m_ymin) / (linesY - 1) + m_ymin);
155  m_dc.DrawLine(m_left, y, m_right, y);
156  };
157 };
158 
160 void Graph::DrawLine(std::vector<hugin_utils::FDiff2D> points, wxColour colour, int penWidth)
161 {
162  if (points.size() < 2)
163  {
164  return;
165  };
166  wxPoint *polygonPoints = new wxPoint[points.size()];
167  for (size_t i = 0; i < points.size(); i++)
168  {
169  polygonPoints[i].x = TransformX(points[i].x);
170  polygonPoints[i].y = TransformY(points[i].y);
171  };
172  m_dc.SetPen(wxPen(colour, penWidth));
173  m_dc.DrawLines(points.size(), polygonPoints);
174  delete[]polygonPoints;
175 };
176 
177 const wxBitmap Graph::GetGraph() const
178 {
179  return *m_bitmap;
180 };
181 
182 int Graph::TransformX(double x)
183 {
184  return hugin_utils::round((x - m_xmin) / (m_xmax - m_xmin)*(m_right - m_left) + m_left);
185 };
186 
187 int Graph::TransformY(double y)
188 {
189  return hugin_utils::round(m_bottom - (y - m_ymin) / (m_ymax - m_ymin)*(m_bottom - m_top));
190 };
191 
193 {
194 #define NRPOINTS 100
196  opts.setHFOV(srcImage.getHFOV());
198  opts.setWidth(srcImage.getWidth());
199  opts.setHeight(srcImage.getHeight());
201  transform.createTransform(srcImage, opts);
202 
203  const double minLength = std::min<double>(srcImage.getWidth(), srcImage.getHeight()) / 2.0;
204  const double maxR = std::sqrt(static_cast<double>(srcImage.getWidth()*srcImage.getWidth() + srcImage.getHeight()*srcImage.getHeight())) / (2.0*minLength);
205  //draw graph
206  Graph graph(300, 200, wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
207  graph.SetChartArea(10, 10, 290, 190);
208  graph.SetChartDisplay(0, -0.1, maxR, 0.1);
209  graph.DrawGrid(6, 7);
210  std::vector<hugin_utils::FDiff2D> points;
211  // draw helper lines
212  // help line at r=1
213  points.push_back(hugin_utils::FDiff2D(1, -0.1));
214  points.push_back(hugin_utils::FDiff2D(1, 0.1));
215  graph.DrawLine(points, wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT), 1);
216  points.clear();
217  // helper line at r=width/height (for landscape images), maximal radius in horizontal (or vertical) direction
218  // chart goes to r for diagonal
219  const double rMaxHorzVert = std::max<double>(srcImage.getWidth(), srcImage.getHeight()) / (2.0*minLength);
220  points.push_back(hugin_utils::FDiff2D(rMaxHorzVert, -0.1));
221  points.push_back(hugin_utils::FDiff2D(rMaxHorzVert, 0.1));
222  graph.DrawLine(points, wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT), 1);
223  points.clear();
224  // now draw distortion graph
225  double r = 0;
226  for (size_t i = 0; i < NRPOINTS; i++)
227  {
228  double x, y;
229  if (transform.transform(x, y, r*minLength, 0))
230  {
231  points.push_back(hugin_utils::FDiff2D(r, x / minLength - r));
232  };
233  r += maxR / (NRPOINTS - 1);
234  };
235  graph.DrawLine(points, wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT), 2);
236  // return bitmap
237  return graph.GetGraph();
238 };
239 
240 } // namespace
void OnLeftDown(wxMouseEvent &e)
Definition: GraphTools.cpp:45
bool AskUserOverwrite(const wxString &filename, const wxString &caption, wxWindow *parent)
ask user if the given file should be overwritten, return true if the user confirmed the overwritting ...
Definition: wxutils.cpp:233
wxBitmap * m_bitmap
Definition: GraphTools.h:81
#define NRPOINTS
void setHeight(unsigned int h)
set panorama height
wxStaticBitmap * m_bitmapControl
Definition: GraphTools.h:46
simple popup to show graph
Definition: GraphTools.h:38
wxMemoryDC m_dc
Definition: GraphTools.h:82
int getHeight() const
Get the height of the image in pixels.
Definition: SrcPanoImage.h:276
vigra::pair< typename ROIImage< Image, Mask >::image_const_traverser, typename ROIImage< Image, Mask >::ImageConstAccessor > srcImage(const ROIImage< Image, Mask > &img)
Definition: ROIImage.h:300
void DrawGrid(size_t linesX, size_t linesY)
draws the grid with linesX lines in x-direction and linexY lines in y-direction
Definition: GraphTools.cpp:144
bool transform(double &x_dest, double &y_dest, double x_src, double y_src) const
excecute transform
double round(double x)
Definition: hugin_math.h:50
~Graph()
destructor
Definition: GraphTools.cpp:118
void OnRightDown(wxMouseEvent &e)
Definition: GraphTools.cpp:50
int getWidth() const
Get the width of the image in pixels.
Definition: SrcPanoImage.h:266
int TransformX(double x)
Definition: GraphTools.cpp:182
void SetChartArea(int left, int top, int right, int bottom)
set where to draw the chart on the bitmap
Definition: GraphTools.cpp:124
void SetChartDisplay(double xmin, double ymin, double xmax, double ymax)
set the real dimension of the chart
Definition: GraphTools.cpp:135
void DrawLine(std::vector< hugin_utils::FDiff2D > points, wxColour colour, int penWidth=1)
draws a line with the coordinates given in points
Definition: GraphTools.cpp:160
void setHFOV(double h, bool keepView=true)
set the horizontal field of view.
help class to draw charts
Definition: GraphTools.h:51
include file for the hugin project
Holds transformations for Image -&gt; Pano and the other way.
int TransformY(double y)
Definition: GraphTools.cpp:187
wxBitmap GetDistortionGraph(const HuginBase::SrcPanoImage &srcImage)
return wxBitmap with graph of distortion for given SrcPanoImage
Definition: GraphTools.cpp:192
const wxBitmap GetGraph() const
Definition: GraphTools.cpp:177
All variables of a source image.
Definition: SrcPanoImage.h:194
void setProjection(ProjectionFormat f)
set the Projection format and adjust the hfov/vfov if nessecary
Panorama image options.
Graph(int graphWidth, int graphHeight, wxColour backgroundColour)
constructors, set size and background colour of resulting bitmap
Definition: GraphTools.cpp:100
void createTransform(const vigra::Diff2D &srcSize, VariableMap srcVars, Lens::LensProjectionFormat srcProj, const vigra::Diff2D &destSize, PanoramaOptions::ProjectionFormat destProj, const std::vector< double > &destProjParam, double destHFOV, const vigra::Diff2D &origSrcSize)
initialize pano-&gt;image transformation
void setWidth(unsigned int w, bool keepView=true)
set panorama width keep the HFOV, if keepView=true