~shadowrobot/sr-ros-interface/stable

« back to all changes in this revision

Viewing changes to sr_movements/src/movement_from_image.cpp

  • Committer: Ugo Cupcic
  • Date: 2011-09-27 16:23:25 UTC
  • mto: (1.39.1 sr-ros-interface)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: ugo@shadowrobot.com-20110927162325-1864bw3axyhk93g2
sr_movements: This package reads a specified bitmap and send targets by replaying the drawn movement.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file   movement_from_image.cpp
 
3
 * @author Ugo Cupcic <ugo@shadowrobot.com>
 
4
 * @date   Tue Sep 27 10:05:01 2011
 
5
 *
 
6
*
 
7
* Copyright 2011 Shadow Robot Company Ltd.
 
8
*
 
9
* This program is free software: you can redistribute it and/or modify it
 
10
* under the terms of the GNU General Public License as published by the Free
 
11
* Software Foundation, either version 2 of the License, or (at your option)
 
12
* any later version.
 
13
*
 
14
* This program is distributed in the hope that it will be useful, but WITHOUT
 
15
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
16
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
17
* more details.
 
18
*
 
19
* You should have received a copy of the GNU General Public License along
 
20
* with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
*
 
22
 * @brief  Reads a png file and creates a movement from it.
 
23
 *
 
24
 *
 
25
 */
 
26
 
 
27
 
 
28
#include "sr_movements/movement_from_image.hpp"
 
29
 
 
30
#include <iostream>
 
31
 
 
32
namespace shadowrobot
 
33
{
 
34
  MovementFromImage::MovementFromImage(std::string image_path)
 
35
    : PartialMovement()
 
36
  {
 
37
    png::image< png::rgb_pixel > image( image_path );
 
38
 
 
39
    generate_movement(image);
 
40
  }
 
41
 
 
42
  MovementFromImage::~MovementFromImage()
 
43
  {}
 
44
 
 
45
 
 
46
  void MovementFromImage::generate_movement( png::image<png::rgb_pixel> image )
 
47
  {
 
48
    png::byte empty_byte(0);
 
49
    double img_height = static_cast<double>( image.get_width() );
 
50
    for (size_t y = 0; y < image.get_height(); ++y)
 
51
    {
 
52
      bool no_pixel = true;
 
53
      for (size_t x = 0; x < image.get_width(); ++x)
 
54
      {
 
55
        if( (image[x][y].red == empty_byte) && (image[x][y].green == empty_byte)
 
56
            && (image[x][y].blue == empty_byte) )
 
57
        {
 
58
          no_pixel = false;
 
59
          steps.push_back( 1.0 - static_cast<double>(x) / img_height);
 
60
          break;
 
61
        }
 
62
      }
 
63
      if(no_pixel)
 
64
      {
 
65
        //not sending any targets for this point.
 
66
        steps.push_back( -1.0 );
 
67
      }
 
68
    }
 
69
  }
 
70
}
 
71
 
 
72
/* For the emacs weenies in the crowd.
 
73
Local Variables:
 
74
   c-basic-offset: 2
 
75
End:
 
76
*/
 
77