source: rrlib_highgui_wrapper/tWindow.cpp

17.03 tip
Last change on this file was 11:12e9dc5c65a0, checked in by Tobias Föhst <foehst@…>, 8 years ago

Added and updated license information

File size: 14.6 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    tWindow.cpp
23 *
24 * \author  Tobias Foehst
25 *
26 * \date    2010-12-26
27 *
28 */
29//----------------------------------------------------------------------
30#include "rrlib/highgui_wrapper/tWindow.h"
31
32//----------------------------------------------------------------------
33// External includes (system with <>, local with "")
34//----------------------------------------------------------------------
35#include <highgui.h>
36
37//----------------------------------------------------------------------
38// Internal includes with ""
39//----------------------------------------------------------------------
40
41//----------------------------------------------------------------------
42// Debugging
43//----------------------------------------------------------------------
44#include <cassert>
45
46//----------------------------------------------------------------------
47// Namespace usage
48//----------------------------------------------------------------------
49
50//----------------------------------------------------------------------
51// Namespace declaration
52//----------------------------------------------------------------------
53namespace rrlib
54{
55namespace highgui
56{
57
58//----------------------------------------------------------------------
59// Forward declarations / typedefs / enums
60//----------------------------------------------------------------------
61
62//----------------------------------------------------------------------
63// Const values
64//----------------------------------------------------------------------
65const unsigned int cNUMBER_OF_COLORS = 13;
66const struct
67{
68  unsigned char red;
69  unsigned char green;
70  unsigned char blue;
71} cPREDEFINED_COLORS[cNUMBER_OF_COLORS] =
72{
73  {   0,   0,   0 }, // black
74  { 255,   0,   0 }, // red
75  {   0, 255,   0 }, // green
76  {   0,   0, 255 }, // blue
77  {   0, 255, 255 }, //
78  { 255,   0, 255 }, //
79  { 255, 255,   0 }, //
80  { 127,   0,   0 }, // dark red
81  {   0, 127,   0 }, // dark green
82  {   0,   0, 127 }, // dark blue
83  {   0, 127, 127 }, // dark
84  { 127,   0, 127 }, // dark
85  { 127, 127,   0 }  // dark
86};
87
88//----------------------------------------------------------------------
89// Implementation
90//----------------------------------------------------------------------
91
92//----------------------------------------------------------------------
93// tWindow constructors
94//----------------------------------------------------------------------
95tWindow::tWindow(const std::string &name, unsigned int width, unsigned int height, int min_x, int min_y)
96  : name(name),
97    width(width),
98    height(height),
99    min_x(min_x),
100    min_y(min_y),
101    image(0),
102    r(0), g(0), b(0)
103{
104  cvNamedWindow(this->name.c_str(), CV_WINDOW_AUTOSIZE);
105  cvMoveWindow(this->name.c_str(), 0, 0);
106  image = cvCreateImage(cvSize(this->width, this->height), IPL_DEPTH_8U, 3);
107  this->Clear();
108}
109
110//----------------------------------------------------------------------
111// tWindow destructor
112//----------------------------------------------------------------------
113tWindow::~tWindow()
114{
115  cvReleaseImage(&this->image);
116  cvDestroyWindow(this->name.c_str());
117}
118
119//----------------------------------------------------------------------
120// tWindow NameToWindowMap
121//----------------------------------------------------------------------
122std::map<std::string, tWindow *> &tWindow::NameToWindowMap()
123{
124  static std::map<std::string, tWindow *> static_result;
125  return static_result;
126}
127
128//----------------------------------------------------------------------
129// tWindow GetInstance
130//----------------------------------------------------------------------
131tWindow &tWindow::GetInstance(const std::string &name, unsigned int width, unsigned int height, int min_x, int min_y)
132{
133  if (NameToWindowMap().find(name) == NameToWindowMap().end())
134  {
135    NameToWindowMap()[name] = new tWindow(name, width, height, min_x, min_y);
136  }
137  return *NameToWindowMap()[name];
138}
139
140tWindow &tWindow::GetInstance(const std::string &name, unsigned int width, unsigned int height)
141{
142  if (NameToWindowMap().find(name) == NameToWindowMap().end())
143  {
144    NameToWindowMap()[name] = new tWindow(name, width, height);
145  }
146  return *NameToWindowMap()[name];
147}
148
149tWindow &tWindow::GetInstance(const std::string &name)
150{
151  assert(NameToWindowMap().find(name) != NameToWindowMap().end());
152  return *NameToWindowMap()[name];
153}
154
155tWindow &tWindow::GetInstance()
156{
157  assert(!NameToWindowMap().empty());
158  return *NameToWindowMap().begin()->second;
159}
160
161//----------------------------------------------------------------------
162// tWindow ReleaseAllInstances
163//----------------------------------------------------------------------
164void tWindow::ReleaseAllInstances()
165{
166  for (std::map<std::string, tWindow *>::iterator it = NameToWindowMap().begin(); it != NameToWindowMap().end(); it++)
167  {
168    delete it->second;
169  }
170  NameToWindowMap().clear();
171}
172
173//----------------------------------------------------------------------
174// tWindow Render
175//----------------------------------------------------------------------
176void tWindow::Render(unsigned int delay) const
177{
178  cvShowImage(this->name.c_str(), this->image);
179
180  if (!delay)
181  {
182    std::cout << "[Press key to continue or q to quit]" << std::endl;
183  }
184
185  int key = cvWaitKey(delay);
186
187  if (key == 113 || key == 1048689)
188  {
189    exit(EXIT_SUCCESS);
190  }
191}
192
193//----------------------------------------------------------------------
194// tWindow SetColor
195//----------------------------------------------------------------------
196void tWindow::SetColor(unsigned char r, unsigned char g, unsigned char b)
197{
198  this->r = r;
199  this->g = g;
200  this->b = b;
201}
202
203void tWindow::SetColor(unsigned int index)
204{
205  index %= cNUMBER_OF_COLORS;
206  this->SetColor(cPREDEFINED_COLORS[index].red, cPREDEFINED_COLORS[index].green, cPREDEFINED_COLORS[index].blue);
207}
208
209//----------------------------------------------------------------------
210// tWindow DrawPoint
211//----------------------------------------------------------------------
212void tWindow::DrawPoint(unsigned int x, unsigned int y)
213{
214  assert(x <= this->width && y <= this->height);
215  y = this->height - y;
216  (this->image->imageData + y * this->image->widthStep + x * this->image->nChannels)[0] = this->b;
217  (this->image->imageData + y * this->image->widthStep + x * this->image->nChannels)[1] = this->g;
218  (this->image->imageData + y * this->image->widthStep + x * this->image->nChannels)[2] = this->r;
219}
220
221void tWindow::DrawPoint(unsigned int x, unsigned int y, unsigned char r, unsigned char g, unsigned char b)
222{
223  this->SetColor(r, g, b);
224  this->DrawPoint(x, y);
225}
226
227//----------------------------------------------------------------------
228// tWindow DrawPointNormalized
229//----------------------------------------------------------------------
230void tWindow::DrawPointNormalized(double x, double y)
231{
232  this->DrawPoint(static_cast<unsigned int>(x * this->width), static_cast<unsigned int>(y * this->height));
233}
234
235void tWindow::DrawPointNormalized(double x, double y, unsigned char r, unsigned char g, unsigned char b)
236{
237  this->SetColor(r, g, b);
238  this->DrawPointNormalized(x, y);
239}
240
241//----------------------------------------------------------------------
242// tWindow DrawPointShifted
243//----------------------------------------------------------------------
244void tWindow::DrawPointShifted(double x, double y)
245{
246  this->DrawPoint(x - this->min_x, y - this->min_y);
247}
248
249void tWindow::DrawPointShifted(double x, double y, unsigned char r, unsigned char g, unsigned char b)
250{
251  this->SetColor(r, g, b);
252  this->DrawPointShifted(x, y);
253}
254
255//----------------------------------------------------------------------
256// tWindow DrawLine
257//----------------------------------------------------------------------
258void tWindow::DrawLine(unsigned int start_x, unsigned int start_y, unsigned int stop_x, unsigned int stop_y)
259{
260  start_y = this->height - start_y;
261  stop_y = this->height - stop_y;
262  cvLine(this->image, cvPoint(start_x, start_y), cvPoint(stop_x, stop_y), cvScalar(this->b, this->g, this->r, 0));
263}
264
265void tWindow::DrawLine(unsigned int start_x, unsigned int start_y, unsigned int stop_x, unsigned int stop_y, unsigned char r, unsigned char g, unsigned char b)
266{
267  this->SetColor(r, g, b);
268  this->DrawLine(start_x, start_y, stop_x, stop_y);
269}
270
271//----------------------------------------------------------------------
272// tWindow DrawLineNormalized
273//----------------------------------------------------------------------
274void tWindow::DrawLineNormalized(double start_x, double start_y, double stop_x, double stop_y)
275{
276  this->DrawLine(static_cast<unsigned int>(start_x * this->width), static_cast<unsigned int>(start_y * this->height),
277                 static_cast<unsigned int>(stop_x * this->width), static_cast<unsigned int>(stop_y * this->height));
278}
279
280void tWindow::DrawLineNormalized(double start_x, double start_y, double stop_x, double stop_y, unsigned char r, unsigned char g, unsigned char b)
281{
282  this->SetColor(r, g, b);
283  this->DrawLineNormalized(start_x, start_y, stop_x, stop_y);
284}
285
286//----------------------------------------------------------------------
287// tWindow DrawLineShifted
288//----------------------------------------------------------------------
289void tWindow::DrawLineShifted(double start_x, double start_y, double stop_x, double stop_y)
290{
291  this->DrawLine(start_x - this->min_x, start_y - this->min_y, stop_x - this->min_x, stop_y - this->min_y);
292}
293
294void tWindow::DrawLineShifted(double start_x, double start_y, double stop_x, double stop_y, unsigned char r, unsigned char g, unsigned char b)
295{
296  this->SetColor(r, g, b);
297  this->DrawLineShifted(start_x, start_y, stop_x, stop_y);
298}
299
300//----------------------------------------------------------------------
301// tWindow DrawRectangle
302//----------------------------------------------------------------------
303void tWindow::DrawRectangle(unsigned int min_x, unsigned int min_y, unsigned int max_x, unsigned int max_y)
304{
305  min_y = this->height - min_y;
306  max_y = this->height - max_y;
307  cvRectangle(this->image, cvPoint(min_x, min_y), cvPoint(max_x, max_y), cvScalar(this->b, this->g, this->r, 0));
308}
309
310void tWindow::DrawRectangle(unsigned int min_x, unsigned int min_y, unsigned int max_x, unsigned int max_y, unsigned char r, unsigned char g, unsigned char b)
311{
312  this->SetColor(r, g, b);
313  this->DrawRectangle(min_x, min_y, max_x, max_y);
314}
315
316//----------------------------------------------------------------------
317// tWindow DrawRectangleNormalized
318//----------------------------------------------------------------------
319void tWindow::DrawRectangleNormalized(double min_x, double min_y, double max_x, double max_y)
320{
321  this->DrawRectangle(static_cast<unsigned int>(min_x * this->width), static_cast<unsigned int>(min_y * this->height),
322                      static_cast<unsigned int>(max_x * this->width), static_cast<unsigned int>(max_y * this->height));
323}
324
325void tWindow::DrawRectangleNormalized(double min_x, double min_y, double max_x, double max_y, unsigned char r, unsigned char g, unsigned char b)
326{
327  this->SetColor(r, g, b);
328  this->DrawRectangleNormalized(min_x, min_y, max_x, max_y);
329}
330
331//----------------------------------------------------------------------
332// tWindow DrawRectangleShifted
333//----------------------------------------------------------------------
334void tWindow::DrawRectangleShifted(double min_x, double min_y, double max_x, double max_y)
335{
336  this->DrawRectangle(min_x - this->min_x, min_y - this->min_y, max_x - this->min_x, max_y - this->min_y);
337}
338
339void tWindow::DrawRectangleShifted(double min_x, double min_y, double max_x, double max_y, unsigned char r, unsigned char g, unsigned char b)
340{
341  this->SetColor(r, g, b);
342  this->DrawRectangleShifted(min_x, min_y, max_x, max_y);
343}
344
345//----------------------------------------------------------------------
346// tWindow DrawCircle
347//----------------------------------------------------------------------
348void tWindow::DrawCircle(unsigned int x, unsigned int y, unsigned int radius, bool filled)
349{
350  y = this->height - y;
351  cvCircle(this->image, cvPoint(x, y), radius, cvScalar(this->b, this->g, this->r, 0), filled ? -1 : 1);
352}
353
354void tWindow::DrawCircle(unsigned int x, unsigned int y, unsigned int radius, bool filled, unsigned char r, unsigned char g, unsigned char b)
355{
356  this->SetColor(r, g, b);
357  this->DrawCircle(x, y, radius, filled);
358}
359
360//----------------------------------------------------------------------
361// tWindow DrawCircleNormalized
362//----------------------------------------------------------------------
363void tWindow::DrawCircleNormalized(double x, double y, double radius, bool filled)
364{
365  this->DrawCircle(static_cast<unsigned int>(x * this->width), static_cast<unsigned int>(y * this->height),
366                   static_cast<unsigned int>(radius * sqrt(this->width * this->height)), filled);
367}
368
369void tWindow::DrawCircleNormalized(double x, double y, double radius, bool filled, unsigned char r, unsigned char g, unsigned char b)
370{
371  this->SetColor(r, g, b);
372  this->DrawCircleNormalized(x, y, radius, filled);
373}
374
375//----------------------------------------------------------------------
376// tWindow DrawCircleShifted
377//----------------------------------------------------------------------
378void tWindow::DrawCircleShifted(double x, double y, double radius, bool filled)
379{
380  this->DrawCircle(x - this->min_x, y - this->min_y, radius, filled);
381}
382
383void tWindow::DrawCircleShifted(double x, double y, double radius, bool filled, unsigned char r, unsigned char g, unsigned char b)
384{
385  this->SetColor(r, g, b);
386  this->DrawCircleShifted(x, y, radius, filled);
387}
388
389//----------------------------------------------------------------------
390// tWindow Clear
391//----------------------------------------------------------------------
392void tWindow::Clear()
393{
394  size_t n = this->image->height * this->image->width * this->image->nChannels;
395  for (size_t i = 0; i < n; i++)
396  {
397    this->image->imageData[i] = 255;
398  }
399  this->SetColor(0, 0, 0);
400}
401
402//----------------------------------------------------------------------
403// End of namespace declaration
404//----------------------------------------------------------------------
405}
406}
Note: See TracBrowser for help on using the repository browser.