Orfeo Toolbox  3.16
otbCurves2DWidget.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #include "otbCurves2DWidget.h"
19 
20 namespace otb
21 {
22 Curves2DWidget::Curves2DWidget() : m_Curves(), m_XAxisLabel("X axis"), m_YAxisLabel("Y axis"), m_Margins(),
23  m_Extent(), m_AxisOrigin(), m_AxisLength(),
24  m_GridOrigin(), m_GridSpacing(), m_ZeroCrossingAxis(true),
25  m_AutoScale(true), m_AxisColor(), m_GridColor(), m_ScaleAxeYDefault(true),
26  m_SpaceToScreenTransform()
27 {
28  // Build the curves list
30 
31  // Prefer a white background
32  ColorType white;
33  white.Fill(1);
34  this->SetBackgroundColor(white);
35 
36  // Default colors
37  m_AxisColor.Fill(0.);
38  m_GridColor.Fill(0.9);
39 
40  // Default margin
41  m_Margins.Fill(20);
42 
43  // Default values
44  m_AxisOrigin.Fill(-100.);
45  m_AxisLength.Fill(200);
46  m_GridOrigin.Fill(-100);
47  m_GridSpacing.Fill(10.);
48 
49  // The list of curves
51 
52  // The space to screen transform
54 }
55 
57 {}
58 
59 unsigned int Curves2DWidget::AddCurve(CurveType * curve)
60 {
61  m_Curves->PushBack(curve);
62  return m_Curves->Size() - 1;
63 }
64 
65 void Curves2DWidget::RemoveCurve(unsigned int idx)
66 {
67  m_Curves->Erase(idx);
68 }
69 
71 {
72  unsigned int size = m_Curves->Size();
73 
74  for (unsigned int j = 0; j < size; ++j)
75  {
76  if (m_Curves->GetNthElement(j)->GetId() == id)
77  {
78  m_Curves->Erase(j);
79  break;
80  }
81  }
82 }
83 
86 {
87  for (unsigned int j = 0; j < m_Curves->Size(); ++j)
88  {
89  if (m_Curves->GetNthElement(j)->GetId() == id)
90  {
91  return m_Curves->GetNthElement(j);
92  }
93  }
94 
95  itkExceptionMacro(<< "No curve found with ID " << id);
96 }
97 
99 {
100  m_Curves->Clear();
101 }
102 
104 {
105  return m_Curves->Size();
106 }
107 
109 {
110  // Call superclass implementation for gl init
112 
113  // Call the before rendering method for each curve
114  CurveListType::Iterator it = m_Curves->Begin();
115  while (it != m_Curves->End())
116  {
117  it.Get()->BeforeRendering();
118  ++it;
119  }
120  if (m_AutoScale)
121  {
122  this->AutoScale();
123  }
124 
125  // Update the space to screen transform
127 
128  // Render the grid
129  this->RenderGrid();
130  // Render the axis
131  this->RenderAxis();
132  // Render the curves
133  this->RenderCurves();
134 }
135 
137 {
138 
139  if (m_Curves->Size() < 1)
140  {
141  // No curves available, retrun
142  return;
143  }
144  PointType min, max, newMin, newMax;
145 
146  // Get min and max from the first curve
147  CurveListType::Iterator it = m_Curves->Begin();
148  min = it.Get()->GetMinimum();
149  max = it.Get()->GetMaximum();
150 
151  // Get the absolute min/max
152  while (it != m_Curves->End())
153  {
154  newMin = it.Get()->GetMinimum();
155  newMax = it.Get()->GetMaximum();
156 
157  if (newMin[0] < min[0]) min[0] = newMin[0];
158  if (newMin[1] < min[1]) min[1] = newMin[1];
159  if (newMax[0] > max[0]) max[0] = newMax[0];
160  if (newMax[1] > max[1]) max[1] = newMax[1];
161 
162  ++it;
163  }
164 
165  // Autoscale
166  m_AxisOrigin = min;
167  m_AxisLength = max - min;
169  m_GridOrigin = min;
170 }
171 
173 {
174  // Update Extent
175  RegionType::IndexType extentIndex;
176  RegionType::SizeType extentSize;
177 
178  // Extent index
179  extentIndex[0] = m_Margins[0];
180  extentIndex[1] = m_Margins[1];
181 
182  // Extent size
183  extentSize[0] = this->w() - 2 * m_Margins[0];
184  extentSize[1] = this->h() - 2 * m_Margins[1];
185 
186  // Set the display extent
187  m_Extent.SetIndex(extentIndex);
188  m_Extent.SetSize(extentSize);
189 
190  // Set the matrix
192  matrix.Fill(0);
193  matrix(0, 0) = (static_cast<double>(m_Extent.GetSize()[0])) / m_AxisLength[0];
194  matrix(1, 1) = (static_cast<double>(m_Extent.GetSize()[1])) / m_AxisLength[1];
195  m_SpaceToScreenTransform->SetMatrix(matrix);
196 
197  // Set the translation
199  translation[0] = m_Extent.GetIndex()[0] - m_AxisOrigin[0];
200  translation[1] = m_Extent.GetIndex()[1] - m_AxisOrigin[1];
201  m_SpaceToScreenTransform->SetTranslation(translation);
202 
203  // Set the center
205 }
206 
208 {
209 
210  // Check if zero crossing axis is possible
211  double xAxisYPos = static_cast<double>(m_Margins[0]);
212  double yAxisXPos = static_cast<double>(m_Margins[1]);
213 
214  PointType spaceCenter;
215  spaceCenter.Fill(0.);
216  PointType screenCenter = m_SpaceToScreenTransform->TransformPoint(spaceCenter);
217 
218  // Update zero crossing coords
219  if (m_ZeroCrossingAxis)
220  {
221 
222  if (m_AxisOrigin[0] < 0 && m_AxisOrigin[0] + m_AxisLength[0] > 0)
223  {
224  yAxisXPos = screenCenter[0];
225  }
226  if (m_AxisOrigin[1] < 0 && m_AxisOrigin[1] + m_AxisLength[1] > 0)
227  {
228  xAxisYPos = screenCenter[1];
229  }
230  }
231  glBegin(GL_LINES);
232  glColor4d(m_AxisColor[0], m_AxisColor[1], m_AxisColor[2], m_AxisColor[3]);
233  glVertex2d(m_Margins[0], yAxisXPos);
234  glVertex2d(this->w() - m_Margins[0], yAxisXPos);
235  glVertex2d(xAxisYPos, m_Margins[1]);
236  glVertex2d(xAxisYPos, this->h() - m_Margins[1]);
237  glEnd();
238 
239  gl_font(FL_COURIER_BOLD, 10);
240  //Draw the y axis legend
241  PointType screenLabelPosition;
242  screenLabelPosition[0] = yAxisXPos + 10;
243  screenLabelPosition[1] = this->h() - m_Margins[1] - 10;
244  gl_draw(m_YAxisLabel.c_str(), (float) screenLabelPosition[0], (float) screenLabelPosition[1]);
245 
246  //Draw the x axis legend
247  screenLabelPosition[0] = this->w() - m_Margins[0] - 5 * m_XAxisLabel.size();
248  screenLabelPosition[1] = xAxisYPos - 10;
249  gl_draw(m_XAxisLabel.c_str(), (float) screenLabelPosition[0], (float) screenLabelPosition[1]);
250 
251  // Draw the title of the curve
252  screenLabelPosition[0] = this->w() - m_Margins[0] - 7 * m_Title.size();
253  screenLabelPosition[1] = this->h() - m_Margins[1] - 10;
254  gl_draw(m_Title.c_str(), (float) screenLabelPosition[0], (float) screenLabelPosition[1]);
255 }
256 
258 {
259  // Get the grid screen origin
260  PointType screenGridOrigin = m_SpaceToScreenTransform->TransformPoint(m_GridOrigin);
261  // Get the grid screen spacing
262  VectorType screenGridSpacing = m_SpaceToScreenTransform->TransformVector(m_GridSpacing);
263 
264  // Render the grid
265  glBegin(GL_LINES);
266  glColor4d(m_GridColor[0], m_GridColor[1], m_GridColor[2], m_GridColor[3]);
267  double pos = screenGridOrigin[0];
268  while (pos <= this->w() - m_Margins[0])
269  {
270  glVertex2d(pos, m_Margins[1]);
271  glVertex2d(pos, this->h() - m_Margins[1]);
272  pos += screenGridSpacing[0];
273  }
274 
275  pos = screenGridOrigin[1];
276 
277  while (pos <= this->h() - m_Margins[1])
278  {
279  glVertex2d(m_Margins[0], pos);
280  glVertex2d(this->w() - m_Margins[0], pos);
281  pos += screenGridSpacing[1];
282  }
283 
284  glEnd();
285 
286  // Display the vertical grid index (Y axis)
287  pos = screenGridOrigin[1];
288 
289  int scale;
291  {
292  scale = 0;
293  }
294  else
295  scale = m_GridOrigin[1];
296 
297  std::ostringstream oss;
298  gl_font(FL_COURIER_BOLD, 8);
299  glColor4d(0, 0, 0, 0.5);
300 
301  while(pos <= this->h()-m_Margins[1])
302  {
303  oss<<scale;
304 
305  gl_draw(oss.str().c_str(), (float)m_Margins[0]-20, (float)pos);
306  pos+=screenGridSpacing[1];
307  scale += m_GridSpacing[1];
308  oss.str("");
309  }
310 
311  // Display the vertical grid index (X axis)
312  scale = m_GridOrigin[0];
313  pos = screenGridOrigin[0];
314  while(pos <= this->w()-m_Margins[0])
315  {
316  oss<<scale;
317 
318  gl_draw(oss.str().c_str(), (float)pos, m_Margins[1]-5);
319  pos+=screenGridSpacing[0];
320  scale += m_GridSpacing[0];
321  oss.str("");
322  }
323 
324 
325 }
326 
328 {
329  for (CurveListType::Iterator it = m_Curves->Begin();
330  it != m_Curves->End(); ++it)
331  {
332  if (it.Get()->GetVisible())
333  {
334  it.Get()->Render(m_Extent, m_SpaceToScreenTransform);
335  }
336  }
337 }
338 
339 void Curves2DWidget::PrintSelf(std::ostream& os, itk::Indent indent) const
340 {
341  Superclass::PrintSelf(os, indent);
342 }
343 } // end namespace otb

Generated at Sun Feb 3 2013 00:19:56 for Orfeo Toolbox with doxygen 1.8.1.1