00001
00002
00003 #include <FL/Fl_Shared_Image.H>
00004
00005 #include "stage.hh"
00006 #include "config.h"
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
00023 World::args.clear();
00024 for( int i=0; i<*argc; i++ )
00025 World::args.push_back( (*argv)[i] );
00026
00027
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
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
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
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
00085
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
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 );
00114 exit(-1);
00115 }
00116
00117
00118
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
00130 if( pixel_is_set( pixels, width, depth, x, y, threshold) )
00131 continue;
00132
00133
00134 const unsigned int startx = x;
00135 const unsigned int starty = y;
00136 unsigned int rheight = height;
00137
00138
00139 for( ; x < width && ! pixel_is_set( pixels, width, depth, x, y, threshold); x++ )
00140 {
00141
00142 unsigned int yy = y;
00143
00144 while( ! pixel_is_set( pixels, width, depth, x, yy, threshold) && (yy < height-1) )
00145 yy++;
00146
00147
00148
00149
00150 if( yy-y < rheight ) rheight = yy-y;
00151 }
00152
00153
00154 pb_set_rect( img, startx, starty, x-startx, rheight, 0xFF );
00155
00156
00157
00158
00159
00160 rotrect_t latest;
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
00175
00176
00177
00178 }
00179 }
00180
00181 if( img ) img->release();
00182 return 0;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
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
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