Permalink
Browse files

cleaning up

  • Loading branch information...
1 parent 324b825 commit c57874bdad0827a6150ace65bd98d6d5a8b08b13 @rtv committed Oct 2, 2011
Showing with 129 additions and 99 deletions.
  1. +3 −5 libstage/block.cc
  2. +126 −92 libstage/stage.cc
  3. +0 −2 libstage/stage.hh
View
8 libstage/block.cc
@@ -24,8 +24,7 @@ Block::Block( Model* mod,
color( color ),
inherit_color( inherit_color ),
wheel(wheel),
- rendered_cells(),
- gpts()
+ rendered_cells()
{
assert( mod );
canonicalize_winding(this->pts);
@@ -42,8 +41,7 @@ Block::Block( Model* mod,
color(),
inherit_color(true),
wheel(),
- rendered_cells(),
- gpts()
+ rendered_cells()
{
assert(mod);
assert(wf);
@@ -211,7 +209,7 @@ void Block::Map( unsigned int layer )
}
// now calculate the global pixel coords of the block vertices
- gpts.clear();
+ PointIntVec gpts;
mod->LocalToPixels( mpts, gpts );
// and render this block's polygon into the world
View
218 libstage/stage.cc
@@ -22,17 +22,17 @@ void Stg::Init( int* argc, char** argv[] )
// copy the command line args for controllers to inspect
World::args.clear();
for( int i=0; i<*argc; i++ )
- World::args.push_back( (*argv)[i] );
+ World::args.push_back( (*argv)[i] );
// seed the RNG
srand48( time(NULL) );
if(!setlocale(LC_ALL,"POSIX"))
- PRINT_WARN("Failed to setlocale(); config file may not be parse correctly\n" );
+ PRINT_WARN("Failed to setlocale(); config file may not be parse correctly\n" );
RegisterModels();
- // ask FLTK to load support for various image formats
+ // ask FLTK to load support for various image formats
fl_register_images();
init_called = true;
@@ -52,8 +52,8 @@ const Color Color::cyan( 0,1,1 );
static inline uint8_t* pb_get_pixel( Fl_Shared_Image* img,
- const unsigned int x,
- const unsigned int y )
+ const unsigned int x,
+ const unsigned int y )
{
uint8_t* pixels = (uint8_t*)(img->data()[0]);
const unsigned int index = (y * img->w() * img->d()) + (x * img->d());
@@ -62,27 +62,27 @@ static inline uint8_t* pb_get_pixel( Fl_Shared_Image* img,
// set all the pixels in a rectangle
static inline void pb_set_rect( Fl_Shared_Image* pb,
- const unsigned int x, const unsigned int y,
- const unsigned int width, const unsigned int height,
- const uint8_t val )
+ const unsigned int x, const unsigned int y,
+ const unsigned int width, const unsigned int height,
+ const uint8_t val )
{
const unsigned int bytes_per_sample = 1;
const unsigned int num_samples = pb->d();
for( unsigned int a = y; a < y+height; a++ )
- for( unsigned int b = x; b < x+width; b++ )
- {
- // zeroing
- uint8_t* pix = pb_get_pixel( pb, b, a );
- memset( pix, val, num_samples * bytes_per_sample );
- }
+ for( unsigned int b = x; b < x+width; b++ )
+ {
+ // zeroing
+ uint8_t* pix = pb_get_pixel( pb, b, a );
+ memset( pix, val, num_samples * bytes_per_sample );
+ }
}
// returns true if the value in the first channel is above threshold
static inline bool pb_pixel_is_set( Fl_Shared_Image* img,
- const unsigned int x,
- const unsigned int y,
- const unsigned int threshold )
+ const unsigned int x,
+ const unsigned int y,
+ const unsigned int threshold )
{
return( pb_get_pixel( img,x,y )[0] > threshold );
}
@@ -94,10 +94,13 @@ int Stg::rotrects_from_image_file( const std::string& filename,
const int threshold = 127;
Fl_Shared_Image *img = Fl_Shared_Image::get(filename.c_str());
- if( img == NULL ) {
- std::cerr << "failed to open file: " << filename << std::endl;
- assert( img );
- }
+ if( img == NULL )
+ {
+ std::cerr << "failed to open file: " << filename << std::endl;
+
+ assert( img ); // easy access to this point in debugger
+ exit(-1);
+ }
//printf( "loaded image %s w %d h %d d %d count %d ld %d\n",
// filename, img->w(), img->h(), img->d(), img->count(), img->ld() );
@@ -106,82 +109,113 @@ int Stg::rotrects_from_image_file( const std::string& filename,
const unsigned height = img->h();
for(unsigned int y = 0; y < height; y++)
+ {
+ for(unsigned int x = 0; x < width; x++)
{
- for(unsigned int x = 0; x < width; x++)
- {
- // skip blank (white) pixels
- if( pb_pixel_is_set( img,x,y, threshold) )
- continue;
-
- // a rectangle starts from this point
- unsigned int startx = x;
- unsigned int starty = y;
- unsigned int rheight = height; // assume full height for starters
-
- // grow the width - scan along the line until we hit an empty (white) pixel
- for( ; x < width && ! pb_pixel_is_set(img,x,y,threshold); x++ )
- {
- // handle horizontal cropping
- //double ppx = x * sx;
- //if (ppx < this->crop_ax || ppx > this->crop_bx)
- //continue;
-
- // look down to see how large a rectangle below we can make
- unsigned int yy = y;
- while( ! pb_pixel_is_set(img,x,yy,threshold) && (yy < height-1) )
- {
- // handle vertical cropping
- //double ppy = (this->image->height - yy) * sy;
- //if (ppy < this->crop_ay || ppy > this->crop_by)
- //continue;
-
- yy++;
- }
-
- // now yy is the depth of a line of non-zero pixels
- // downward we store the smallest depth - that'll be the
- // height of the rectangle
- if( yy-y < rheight ) rheight = yy-y; // shrink the height to fit
- }
-
- // whiten the pixels we have used in this rect
- pb_set_rect( img, startx, starty, x-startx, rheight, 0xFF );
-
- // y-invert all the rectangles because we're using conventional
- // rather than graphics coordinates. this is much faster than
- // inverting the original image.
-
- rotrect_t latest;// = &(*rects)[(*rect_count)-1];
- latest.pose.x = startx;
- latest.pose.y = height-1 - (starty + rheight);
- latest.pose.a = 0.0;
- latest.size.x = x - startx;
- latest.size.y = rheight;
-
- assert( latest.pose.x >= 0 );
- assert( latest.pose.y >= 0 );
- assert( latest.pose.x <= width );
- assert( latest.pose.y <= height);
-
- rects.push_back( latest );
- //assert( latest->size.x > 0 );
- //assert( latest->size.y > 0 );
-
- // if( latest->size.x < 1 || latest->size.y < 1 )
- // printf( "p [%.2f %.2f] s [%.2f %.2f]\n",
- // latest->pose.x, latest->pose.y, latest->size.x, latest->size.y );
-
- //printf( "rect %d (%.2f %.2f %.2f %.2f %.2f\n",
- // *rect_count,
- // latest->x, latest->y, latest->a, latest->w, latest->h );
-
- }
+ // skip blank (white) pixels
+ if( pb_pixel_is_set( img,x,y, threshold) )
+ continue;
+
+ // a rectangle starts from this point
+ const unsigned int startx = x;
+ const unsigned int starty = y;
+ unsigned int rheight = height; // assume full height for starters
+
+ // grow the width - scan along the line until we hit an empty (white) pixel
+ for( ; x < width && ! pb_pixel_is_set(img,x,y,threshold); x++ )
+ {
+ // handle horizontal cropping
+ //double ppx = x * sx;
+ //if (ppx < this->crop_ax || ppx > this->crop_bx)
+ //continue;
+
+ // look down to see how large a rectangle below we can make
+ unsigned int yy = y;
+ while( ! pb_pixel_is_set(img,x,yy,threshold) && (yy < height-1) )
+ {
+ // handle vertical cropping
+ //double ppy = (this->image->height - yy) * sy;
+ //if (ppy < this->crop_ay || ppy > this->crop_by)
+ //continue;
+
+ yy++;
+ }
+
+ // now yy is the depth of a line of non-zero pixels
+ // downward we store the smallest depth - that'll be the
+ // height of the rectangle
+ if( yy-y < rheight ) rheight = yy-y; // shrink the height to fit
+ }
+
+ // whiten the pixels we have used in this rect
+ pb_set_rect( img, startx, starty, x-startx, rheight, 0xFF );
+
+ // y-invert all the rectangles because we're using conventional
+ // rather than graphics coordinates. this is much faster than
+ // inverting the original image.
+
+ rotrect_t latest;// = &(*rects)[(*rect_count)-1];
+ latest.pose.x = startx;
+ latest.pose.y = height-1 - (starty + rheight);
+ latest.pose.a = 0.0;
+ latest.size.x = x - startx;
+ latest.size.y = rheight;
+
+ assert( latest.pose.x >= 0 );
+ assert( latest.pose.y >= 0 );
+ assert( latest.pose.x <= width );
+ assert( latest.pose.y <= height);
+
+ rects.push_back( latest );
+ //assert( latest->size.x > 0 );
+ //assert( latest->size.y > 0 );
+
+ // if( latest->size.x < 1 || latest->size.y < 1 )
+ // printf( "p [%.2f %.2f] s [%.2f %.2f]\n",
+ // latest->pose.x, latest->pose.y, latest->size.x, latest->size.y );
+
+ //printf( "rect %d (%.2f %.2f %.2f %.2f %.2f\n",
+ // *rect_count,
+ // latest->x, latest->y, latest->a, latest->w, latest->h );
+
}
+ }
if( img ) img->release(); // frees all resources for this image
return 0; // ok
}
+/*
+private static void Floodfill(byte[,] vals, point_int_t q, byte SEED_COLOR, byte COLOR)
+{
+ int h = vals.GetLength(0);
+ int w = vals.GetLength(1);
+
+ if (q.Y < 0 || q.Y > h - 1 || q.X < 0 || q.X > w - 1)
+ return;
+
+ std::stack<Point> stack = new Stack<Point>();
+ stack.Push(q);
+ while (stack.Count > 0)
+ {
+ Point p = stack.Pop();
+ int x = p.X;
+ int y = p.Y;
+ if (y < 0 || y > h - 1 || x < 0 || x > w - 1)
+ continue;
+ byte val = vals[y, x];
+ if (val == SEED_COLOR)
+ {
+ vals[y, x] = COLOR;
+ stack.Push(new Point(x + 1, y));
+ stack.Push(new Point(x - 1, y));
+ stack.Push(new Point(x, y + 1));
+ stack.Push(new Point(x, y - 1));
+ }
+ }
+}
+*/
+
// POINTS -----------------------------------------------------------
point_t* Stg::unit_square_points_create( void )
@@ -204,10 +238,10 @@ point_t* Stg::unit_square_points_create( void )
double Stg::constrain( double val, const double minval, const double maxval )
{
if( val < minval )
- return minval;
+ return minval;
if( val > maxval )
- return maxval;
+ return maxval;
return val;
}
View
2 libstage/stage.hh
@@ -1277,8 +1277,6 @@ namespace Stg
UnMapping them very quickly. */
CellPtrVec rendered_cells[2];
- PointIntVec gpts;
-
/** find the position of a block's point in model coordinates
(m) */
point_t BlockPointToModelMeters( const point_t& bpt );

0 comments on commit c57874b

Please sign in to comment.