stage.cc
Go to the documentation of this file.
1 // Author: Richard Vaughan
2 
3 #include <FL/Fl_Shared_Image.H>
4 
5 #include "stage.hh"
6 #include "config.h" // results of cmake's system configuration tests
7 #include "file_manager.hh"
8 using namespace Stg;
9 
10 static bool init_called = false;
11 
12 const char* Stg::Version()
13 {
14  return VERSION;
15 }
16 
17 
18 void Stg::Init( int* argc, char** argv[] )
19 {
20  PRINT_DEBUG( "Stg::Init()" );
21 
22  // copy the command line args for controllers to inspect
23  World::args.clear();
24  for( int i=0; i<*argc; i++ )
25  World::args.push_back( (*argv)[i] );
26 
27  // seed the RNG
28  srand48( time(NULL) );
29 
30  if(!setlocale(LC_ALL,"POSIX"))
31  PRINT_WARN("Failed to setlocale(); config file may not be parse correctly\n" );
32 
34 
35  // ask FLTK to load support for various image formats
36  fl_register_images();
37 
38  init_called = true;
39 }
40 
42 {
43  return init_called;
44 }
45 
46 const Color Color::blue( 0,0,1 );
47 const Color Color::red( 1,0,0 );
48 const Color Color::green( 0,1,0 );
49 const Color Color::yellow( 1,1,0 );
50 const Color Color::magenta( 1,0,1 );
51 const Color Color::cyan( 0,1,1 );
52 
53 
54 // static inline uint8_t* pb_get_pixel( Fl_Shared_Image* img,
55 // const unsigned int x,
56 // const unsigned int y )
57 // {
58 // uint8_t* pixels = (uint8_t*)(img->data()[0]);
59 // const unsigned int index = (y * img->w() * img->d()) + (x * img->d());
60 // return( pixels + index );
61 // }
62 
63  // // returns true if the value in the first channel is above threshold
64 // static inline bool pb_pixel_is_set( Fl_Shared_Image* img,
65 // const unsigned int x,
66 // const unsigned int y,
67 // const unsigned int threshold )
68 // {
69 // return( pb_get_pixel( img,x,y )[0] > threshold );
70 // }
71 
72 // set all the pixels in a rectangle
73 static inline void pb_set_rect( Fl_Shared_Image* pb,
74  const unsigned int x, const unsigned int y,
75  const unsigned int rwidth, const unsigned int rheight,
76  const uint8_t val )
77 {
78  const unsigned int bytes_per_sample = 1;
79  const unsigned int depth = pb->d();
80  const unsigned int width = pb->w();
81 
82  for( unsigned int a = y; a < y+rheight; a++ )
83  {
84  // zeroing
85  //uint8_t* pix = pb_get_pixel( pb, x, a );
86  uint8_t* pix = (uint8_t*)(pb->data()[0] + (a*width*depth) + x*depth);
87  memset( pix, val, rwidth * depth * bytes_per_sample );
88  }
89 }
90 
91 
92 static inline bool pixel_is_set( uint8_t* pixels,
93  const unsigned int width,
94  const unsigned int depth,
95  const unsigned int x,
96  const unsigned int y,
97  uint8_t threshold )
98 {
99  return( (pixels + (y*width*depth) + x*depth)[0] > threshold );
100 }
101 
102 int Stg::rotrects_from_image_file( const std::string& filename,
103  std::vector<rotrect_t>& rects )
104 {
105  // TODO: make this a parameter
106  const int threshold = 127;
107 
108  Fl_Shared_Image *img = Fl_Shared_Image::get(filename.c_str());
109  if( img == NULL )
110  {
111  std::cerr << "failed to open file: " << filename << std::endl;
112 
113  assert( img ); // easy access to this point in debugger
114  exit(-1);
115  }
116 
117  //printf( "loaded image %s w %d h %d d %d count %d ld %d\n",
118  // filename, img->w(), img->h(), img->d(), img->count(), img->ld() );
119 
120  const unsigned int width = img->w();
121  const unsigned height = img->h();
122  const unsigned int depth = img->d();
123  uint8_t* pixels = (uint8_t*)img->data()[0];
124 
125  for(unsigned int y = 0; y < height; y++)
126  {
127  for(unsigned int x = 0; x < width; x++)
128  {
129  // skip blank (white) pixels
130  if( pixel_is_set( pixels, width, depth, x, y, threshold) )
131  continue;
132 
133  // a rectangle starts from this point
134  const unsigned int startx = x;
135  const unsigned int starty = y;
136  unsigned int rheight = height; // assume full height for starters
137 
138  // grow the width - scan along the line until we hit an empty (white) pixel
139  for( ; x < width && ! pixel_is_set( pixels, width, depth, x, y, threshold); x++ )
140  {
141  // look down to see how large a rectangle below we can make
142  unsigned int yy = y;
143  //while( ! pb_pixel_is_set(img,x,yy,threshold) && (yy < height-1) )
144  while( ! pixel_is_set( pixels, width, depth, x, yy, threshold) && (yy < height-1) )
145  yy++;
146 
147  // now yy is the depth of a line of non-zero pixels
148  // downward we store the smallest depth - that'll be the
149  // height of the rectangle
150  if( yy-y < rheight ) rheight = yy-y; // shrink the height to fit
151  }
152 
153  // whiten the pixels we have used in this rect
154  pb_set_rect( img, startx, starty, x-startx, rheight, 0xFF );
155 
156  // y-invert all the rectangles because we're using conventional
157  // rather than graphics coordinates. this is much faster than
158  // inverting the original image.
159 
160  rotrect_t latest;// = &(*rects)[(*rect_count)-1];
161  latest.pose.x = startx;
162  latest.pose.y = height-1 - (starty + rheight);
163  latest.pose.a = 0.0;
164  latest.size.x = x - startx;
165  latest.size.y = rheight;
166 
167  assert( latest.pose.x >= 0 );
168  assert( latest.pose.y >= 0 );
169  assert( latest.pose.x <= width );
170  assert( latest.pose.y <= height);
171 
172  rects.push_back( latest );
173 
174  //printf( "rect %d (%.2f %.2f %.2f %.2f %.2f\n",
175  // *rect_count,
176  // latest->x, latest->y, latest->a, latest->w, latest->h );
177 
178  }
179  }
180 
181  if( img ) img->release(); // frees all resources for this image
182  return 0; // ok
183 }
184 
185 /*
186 private static void Floodfill(byte[,] vals, point_int_t q, byte SEED_COLOR, byte COLOR)
187 {
188  int h = vals.GetLength(0);
189  int w = vals.GetLength(1);
190 
191  if (q.Y < 0 || q.Y > h - 1 || q.X < 0 || q.X > w - 1)
192  return;
193 
194  std::stack<Point> stack = new Stack<Point>();
195  stack.Push(q);
196  while (stack.Count > 0)
197  {
198  Point p = stack.Pop();
199  int x = p.X;
200  int y = p.Y;
201  if (y < 0 || y > h - 1 || x < 0 || x > w - 1)
202  continue;
203  byte val = vals[y, x];
204  if (val == SEED_COLOR)
205  {
206  vals[y, x] = COLOR;
207  stack.Push(new Point(x + 1, y));
208  stack.Push(new Point(x - 1, y));
209  stack.Push(new Point(x, y + 1));
210  stack.Push(new Point(x, y - 1));
211  }
212  }
213 }
214 */
215 
216 // POINTS -----------------------------------------------------------
217 
219 {
220  point_t * pts = new point_t[4];
221 
222  pts[0].x = 0;
223  pts[0].y = 0;
224  pts[1].x = 1;
225  pts[1].y = 0;
226  pts[2].x = 1;
227  pts[2].y = 1;
228  pts[3].x = 0;
229  pts[3].y = 1;
230 
231  return pts;
232 }
233 
234 // return a value based on val, but limited minval <= val >= maxval
235 double Stg::constrain( double val, const double minval, const double maxval )
236 {
237  if( val < minval ) return minval;
238  if( val > maxval ) return maxval;
239  return val;
240 }
241 
The Stage library uses its own namespace.
Definition: canvas.hh:8
static const Color red
Definition: stage.hh:217
static std::vector< std::string > args
Definition: stage.hh:826
bool InitDone()
Definition: stage.cc:41
static const Color magenta
Definition: stage.hh:217
meters_t x
Definition: stage.hh:228
void Init(int *argc, char **argv[])
Definition: stage.cc:18
static const Color blue
Definition: stage.hh:217
const char * Version()
Definition: stage.cc:12
void RegisterModels()
Definition: typetable.cc:19
meters_t y
Definition: stage.hh:251
static bool pixel_is_set(uint8_t *pixels, const unsigned int width, const unsigned int depth, const unsigned int x, const unsigned int y, uint8_t threshold)
Definition: stage.cc:92
static int argc
int rotrects_from_image_file(const std::string &filename, std::vector< rotrect_t > &rects)
Definition: stage.cc:102
meters_t y
Definition: stage.hh:473
meters_t x
Definition: stage.hh:473
static const Color cyan
Definition: stage.hh:217
meters_t y
Definition: stage.hh:228
#define PRINT_WARN(m)
Definition: stage.hh:633
static char * argv
static void pb_set_rect(Fl_Shared_Image *pb, const unsigned int x, const unsigned int y, const unsigned int rwidth, const unsigned int rheight, const uint8_t val)
Definition: stage.cc:73
double constrain(double val, double minval, double maxval)
Definition: stage.cc:235
static const Color green
Definition: stage.hh:217
static const Color yellow
Definition: stage.hh:217
radians_t a
rotation about the z axis.
Definition: stage.hh:252
#define PRINT_DEBUG(m)
Definition: stage.hh:666
static bool init_called
Definition: stage.cc:10
point_t * unit_square_points_create()
Definition: stage.cc:218
meters_t x
Definition: stage.hh:251


stage
Author(s): Richard Vaughan , Brian Gerkey , Reed Hedges , Andrew Howard , Toby Collett , Pooya Karimian , Jeremy Asher , Alex Couture-Beil , Geoff Biggs , Rich Mattes , Abbas Sadat
autogenerated on Mon Jun 10 2019 15:06:09