YARP
Yet Another Robot Platform
FakeBot.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #include "FakeBot.h"
11 
12 #include <yarp/sig/all.h>
13 #include <yarp/sig/ImageFile.h>
14 #include <random>
15 
16 using namespace yarp::os;
17 using namespace yarp::sig;
18 using namespace yarp::sig::draw;
19 using namespace yarp::sig::file;
20 using namespace yarp::dev;
21 
22 YARP_LOG_COMPONENT(FAKEBOT, "yarp.devices.FakeBot")
23 
24 
25 #define MAXRND 50000
26 namespace {
27 int rnds[MAXRND];
28 }
29 
30 void FakeBot::init() {
31  int m_w = 640;
32  int m_h = 480;
33  int rr = 30;
34 
35  back.resize(m_w,m_h);
36  PixelRgb w{255,255,255};
37  PixelRgb r{128,128,255};
38  PixelRgb b{0,0,255};
39  IMGFOR(back,x,y) {
40  back(x,y) = r;
41  if (y>=m_h*0.75) {
42  back(x,y) = b;
43  }
44  }
45  for (int i=0; i<=m_w; i+=m_w/10) {
46  addCircle(back,b,i,int(m_h*0.77),rr);
47  }
48 
49  std::default_random_engine randengine;
50  std::uniform_real_distribution<double> udist1(-m_w/4,m_w/4);
51  std::uniform_real_distribution<double> udist2(-int(m_h*0.2),rr);
52  for (int j=0; j<40; j++) {
53  int xx = m_w/2 + udist1(randengine);
54  int yy = int(m_h*0.2) - udist2(randengine);
55  addCircle(back,w,xx,yy,rr);
56  }
57 
58  m_x = (back.width()-this->m_w)/2;
59  m_y = (back.height()-this->m_h)/2;
60  m_dx = m_dy = 0;
61 
62  std::normal_distribution<double> ndist(0,255);
63  for (int & rnd : rnds) {
64  rnd = int(ndist(randengine));
65  }
66 
67  fore.resize(64,64);
68  fore.zero();
69  m_tx = back.width()/2;
70  m_ty = back.height()*0.4;
71  m_tdx = 1;
72  m_tdy = 0;
73 }
74 
75 
76 void scramble(unsigned char& ch, float f) {
77  int x = ch;
78  static int idx = 0;
79  //x += (int)(Random::normal(0,x*f));
80  x += (int)(rnds[idx]*f);
81  idx = (idx+17)%MAXRND;
82  if (x<0) x = 0;
83  if (x>255) x = 255;
84  ch = (unsigned char) x;
85 }
86 
87 
89  std::string backFile = config.check("background",Value("textures/back.ppm"),
90  "background image to use").asString();
91  if (backFile!="") {
92  yarp::sig::file::read(back,backFile);
93  }
94  std::string foreFile = config.check("target",Value("textures/fore.ppm"),
95  "target image to use").asString();
96  if (foreFile!="") {
97  yarp::sig::file::read(fore,foreFile);
98  }
99  noiseLevel = config.check("noise",Value(0.05),
100  "pixel noise level").asFloat64();
101 
102  xScale = config.check("sx",Value(1.0),
103  "scaling for x coordinate").asFloat64();
104  yScale = config.check("sy",Value(1.0),
105  "scaling for y coordinate").asFloat64();
106 
107  lifetime = config.check("lifetime",Value(-1.0),
108  "device should exist for this length of time (in seconds)").asFloat64();
109  if (lifetime>=0) {
110  start();
111  }
112  return true;
113 }
114 
115 
116 // IFrameGrabberImage
118  if (fabs(dpos[0])>0.001||fabs(dpos[0])>0.001) {
119  pos[0] = m_dx+dpos[0];
120  dpos[0] = 0;
121  pos[1] = m_dy+dpos[1];
122  dpos[1] = 0;
123  }
124  pos[0] += vel[0];
125  pos[1] += vel[1];
126  double xx = pos[0];
127  double yy = pos[1];
128  double dx = (xx-m_dx)*5;
129  double dy = (yy-m_dy)*5;
130  m_tx += m_tdx;
131  m_ty += m_tdy;
132  if (m_tdx>0 && m_tx>back.width()*0.75) {
133  m_tdx *= -1;
134  }
135  if (m_tdx<0 && m_tx<back.width()*0.25) {
136  m_tdx *= -1;
137  }
138  dx /= 40;
139  dy /= 40;
140  if (amp[0]>0.5) {
141  m_dx += dx;
142  }
143  if (amp[1]>0.5) {
144  m_dy += dy;
145  }
146  image.resize(m_w,m_h);
147  back.safePixel(-1,-1) = PixelRgb{255,0,0};
148  loc[0] = m_dx;
149  loc[1] = m_dy;
150 
151  double m_dx_scaled = m_dx*xScale;
152  double m_dy_scaled = m_dy*yScale;
153  IMGFOR(image,x,y) {
154  int x0 = int(x+m_x+m_dx_scaled*0.5+0.5);
155  int y0 = int(y+m_y+m_dy_scaled*0.5+0.5);
156  image(x,y) = back.safePixel(x0,y0);
157 
158  if (fore.isPixel(int(x0-m_tx),int(y0-m_ty))) {
159  PixelRgb& pix = fore(int(x0-m_tx),int(y0-m_ty));
160  if (pix.r<200||pix.g>100||pix.b>100) {
161  image(x,y) = pix;
162  }
163  }
164  }
165  IMGFOR(image,x2,y2) {
166  PixelRgb& pix = image(x2,y2);
167  auto f = (float)(noiseLevel);
168  scramble(pix.r,f);
169  scramble(pix.g,f);
170  scramble(pix.b,f);
171  }
172  Time::delay(0.1); // simulated hardware delay, using mutable clock
173  return true;
174 }
175 
176 void FakeBot::run() {
177  if (lifetime>=0) {
178  Time::delay(lifetime);
179  std::exit(0);
180  }
181 }
FakeBot::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: FakeBot.cpp:88
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
yarp::sig::PixelRgb::g
unsigned char g
Definition: Image.h:455
yarp::sig::file::read
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:827
yarp::sig
Signal processing.
Definition: Image.h:25
randengine
std::default_random_engine randengine
Definition: Random.cpp:16
all.h
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
yarp::sig::PixelRgb::b
unsigned char b
Definition: Image.h:456
MAXRND
#define MAXRND
Definition: FakeBot.cpp:25
yarp::sig::ImageOf< yarp::sig::PixelRgb >
yarp::sig::ImageOf::safePixel
T & safePixel(size_t x, size_t y)
Definition: Image.h:679
FakeBot::run
void run() override
Main body of the new thread.
Definition: FakeBot.cpp:176
IMGFOR
#define IMGFOR(img, i, j)
Definition: ImageDraw.h:154
FakeBot::getImage
bool getImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image) override
Get an rgb image from the frame grabber, if required demosaicking/color reconstruction is applied.
Definition: FakeBot.cpp:117
yarp::os::Searchable::check
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
ImageFile.h
yarp::sig::draw
Very basic drawing functions, in case you don't have anything better available.
Definition: ImageDraw.h:26
yarp::sig::file
Image file operations.
Definition: ImageFile.h:24
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::sig::PixelRgb
Packed RGB pixel type.
Definition: Image.h:453
yarp::sig::draw::addCircle
void addCircle(ImageOf< T > &dest, const T &pix, int i, int j, int r)
Definition: ImageDraw.h:43
FAKEBOT
const yarp::os::LogComponent & FAKEBOT()
Definition: FakeBot.cpp:22
FakeBot.h
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:47
yarp::os::Time::delay
void delay(double seconds)
Wait for a certain number of seconds.
Definition: Time.cpp:114
scramble
void scramble(unsigned char &ch, float f)
Definition: FakeBot.cpp:76
yarp::sig::PixelRgb::r
unsigned char r
Definition: Image.h:454