stage.cc
Go to the documentation of this file.
00001 // Author: Richard Vaughan
00002 
00003 #include <FL/Fl_Shared_Image.H>
00004 
00005 #include "stage.hh"
00006 #include "config.h" // results of cmake's system configuration tests
00007 #include "file_manager.hh"
00008 using namespace Stg;
00009 
00010 static bool init_called = false;
00011 
00012 const char* Stg::Version()
00013 { 
00014   return VERSION; 
00015 }
00016 
00017 
00018 void Stg::Init( int* argc, char** argv[] )
00019 {
00020   PRINT_DEBUG( "Stg::Init()" );
00021   
00022   // copy the command line args for controllers to inspect
00023   World::args.clear();
00024   for( int i=0; i<*argc; i++ )
00025     World::args.push_back( (*argv)[i] ); 
00026 
00027   // seed the RNG 
00028   srand48( time(NULL) );
00029 
00030   if(!setlocale(LC_ALL,"POSIX"))
00031     PRINT_WARN("Failed to setlocale(); config file may not be parse correctly\n" );
00032                                                 
00033   RegisterModels();
00034         
00035   // ask FLTK to load support for various image formats
00036   fl_register_images();
00037 
00038   init_called = true;
00039 }
00040 
00041 bool Stg::InitDone()
00042 {
00043   return init_called;
00044 }
00045 
00046 const Color Color::blue( 0,0,1 );
00047 const Color Color::red( 1,0,0 );
00048 const Color Color::green( 0,1,0 );
00049 const Color Color::yellow( 1,1,0 );
00050 const Color Color::magenta( 1,0,1 );
00051 const Color Color::cyan( 0,1,1 );               
00052 
00053 
00054 // static inline uint8_t* pb_get_pixel( Fl_Shared_Image* img, 
00055 //                                   const unsigned int x, 
00056 //                                   const unsigned int y )
00057 // {
00058 //   uint8_t* pixels = (uint8_t*)(img->data()[0]);
00059 //   const unsigned int index = (y * img->w() * img->d()) + (x * img->d());
00060 //   return( pixels + index );
00061 // }
00062 
00063  // // returns true if the value in the first channel is above threshold
00064 // static inline bool pb_pixel_is_set( Fl_Shared_Image* img, 
00065 //                                  const unsigned int x, 
00066 //                                  const unsigned int y, 
00067 //                                  const unsigned int threshold )
00068 // {
00069 //   return( pb_get_pixel( img,x,y )[0] > threshold );
00070 // }
00071 
00072 // set all the pixels in a rectangle 
00073 static inline void pb_set_rect( Fl_Shared_Image* pb, 
00074                                 const unsigned int x, const unsigned int y, 
00075                                 const unsigned int rwidth, const unsigned int rheight, 
00076                                 const uint8_t val )
00077 {
00078   const unsigned int bytes_per_sample = 1;
00079   const unsigned int depth = pb->d();
00080   const unsigned int width = pb->w();
00081   
00082   for( unsigned int a = y; a < y+rheight; a++ )
00083     {   
00084       // zeroing
00085       //uint8_t* pix = pb_get_pixel( pb, x, a );
00086       uint8_t* pix = (uint8_t*)(pb->data()[0] + (a*width*depth) + x*depth);
00087       memset( pix, val, rwidth * depth * bytes_per_sample );
00088     }
00089 }  
00090 
00091 
00092 static inline bool pixel_is_set( uint8_t* pixels,
00093                                  const unsigned int width,
00094                                  const unsigned int depth,
00095                                  const unsigned int x, 
00096                                  const unsigned int y,
00097                                  uint8_t threshold )
00098 {
00099   return( (pixels + (y*width*depth) + x*depth)[0] > threshold );
00100 }
00101 
00102 int Stg::rotrects_from_image_file( const std::string& filename, 
00103                                    std::vector<rotrect_t>& rects )
00104 {
00105   // TODO: make this a parameter
00106   const int threshold = 127;
00107   
00108   Fl_Shared_Image *img = Fl_Shared_Image::get(filename.c_str());
00109   if( img == NULL ) 
00110     {
00111       std::cerr << "failed to open file: " << filename << std::endl;
00112 
00113       assert( img ); // easy access to this point in debugger     
00114       exit(-1); 
00115     }
00116 
00117   //printf( "loaded image %s w %d h %d d %d count %d ld %d\n", 
00118   //  filename, img->w(), img->h(), img->d(), img->count(), img->ld() );
00119 
00120   const unsigned int width = img->w();
00121   const unsigned height = img->h();
00122   const unsigned int depth = img->d();
00123   uint8_t* pixels = (uint8_t*)img->data()[0];
00124 
00125   for(unsigned int y = 0; y < height; y++)
00126     {
00127       for(unsigned int x = 0; x < width; x++)
00128         {
00129           // skip blank (white) pixels
00130           if(  pixel_is_set( pixels, width, depth, x, y, threshold) )
00131             continue;
00132 
00133           // a rectangle starts from this point
00134           const unsigned int startx = x;
00135           const unsigned int starty = y;
00136           unsigned int rheight = height; // assume full height for starters
00137 
00138           // grow the width - scan along the line until we hit an empty (white) pixel
00139             for( ; x < width &&  ! pixel_is_set( pixels, width, depth, x, y, threshold); x++ )
00140             {
00141               // look down to see how large a rectangle below we can make
00142               unsigned int yy  = y;
00143               //while( ! pb_pixel_is_set(img,x,yy,threshold) && (yy < height-1) )
00144               while( !  pixel_is_set( pixels, width, depth, x, yy, threshold) && (yy < height-1) )
00145                   yy++;
00146 
00147               // now yy is the depth of a line of non-zero pixels
00148               // downward we store the smallest depth - that'll be the
00149               // height of the rectangle
00150               if( yy-y < rheight ) rheight = yy-y; // shrink the height to fit
00151             } 
00152 
00153           // whiten the pixels we have used in this rect
00154           pb_set_rect( img, startx, starty, x-startx, rheight, 0xFF );
00155 
00156           //  y-invert all the rectangles because we're using conventional
00157           // rather than graphics coordinates. this is much faster than
00158           // inverting the original image.
00159 
00160           rotrect_t latest;// = &(*rects)[(*rect_count)-1];
00161           latest.pose.x = startx;
00162           latest.pose.y = height-1 - (starty + rheight);
00163           latest.pose.a = 0.0;
00164           latest.size.x = x - startx;
00165           latest.size.y = rheight;
00166 
00167           assert( latest.pose.x >= 0 );
00168           assert( latest.pose.y >= 0 );
00169           assert( latest.pose.x <= width );
00170           assert( latest.pose.y <= height);
00171 
00172           rects.push_back( latest );
00173 
00174           //printf( "rect %d (%.2f %.2f %.2f %.2f %.2f\n", 
00175           //  *rect_count, 
00176           //  latest->x, latest->y, latest->a, latest->w, latest->h );
00177 
00178         }
00179     }
00180 
00181   if( img ) img->release(); // frees all resources for this image
00182   return 0; // ok
00183 }
00184 
00185 /*
00186 private static void Floodfill(byte[,] vals, point_int_t q, byte SEED_COLOR, byte COLOR)
00187 {
00188   int h = vals.GetLength(0);
00189   int w = vals.GetLength(1);
00190 
00191   if (q.Y < 0 || q.Y > h - 1 || q.X < 0 || q.X > w - 1)
00192     return;
00193 
00194   std::stack<Point> stack = new Stack<Point>();
00195   stack.Push(q);
00196   while (stack.Count > 0)
00197     {
00198       Point p = stack.Pop();
00199       int x = p.X;
00200       int y = p.Y;
00201       if (y < 0 || y > h - 1 || x < 0 || x > w - 1)
00202         continue;
00203       byte val = vals[y, x];
00204       if (val == SEED_COLOR)
00205         {
00206           vals[y, x] = COLOR;
00207           stack.Push(new Point(x + 1, y));
00208           stack.Push(new Point(x - 1, y));
00209           stack.Push(new Point(x, y + 1));
00210           stack.Push(new Point(x, y - 1));
00211         }
00212     }
00213 }
00214 */
00215 
00216 // POINTS -----------------------------------------------------------
00217 
00218 point_t* Stg::unit_square_points_create( void )
00219 {
00220   point_t * pts = new point_t[4];
00221 
00222   pts[0].x = 0;
00223   pts[0].y = 0;
00224   pts[1].x = 1;
00225   pts[1].y = 0;
00226   pts[2].x = 1;
00227   pts[2].y = 1;
00228   pts[3].x = 0;
00229   pts[3].y = 1;
00230 
00231   return pts;
00232 }
00233 
00234 // return a value based on val, but limited minval <= val >= maxval  
00235 double Stg::constrain( double val, const double minval, const double maxval )
00236 {
00237   if( val < minval ) return minval;
00238   if( val > maxval ) return maxval;
00239   return val;
00240 }
00241 


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 Thu Aug 27 2015 15:20:57