stage_internal.h

Go to the documentation of this file.
00001 #ifndef _STAGE_INTERNAL_H
00002 #define _STAGE_INTERNAL_H
00003 
00004 // internal function declarations that are not part of the external
00005 // interface to Stage
00006 
00007 #include "stage.h"
00008 #include "math.h" // for lrint() in macros
00009 
00010 #if INCLUDE_GNOME
00011 #include "gnome.h"
00012 #endif
00013 
00014 
00025 #include <glib.h>
00026 
00027 #ifdef __cplusplus
00028 extern "C" {
00029 #endif 
00030   
00032   typedef struct 
00033   {
00034     stg_rtk_canvas_t* canvas;
00035     
00036     // Gtk stuff
00037     GtkWidget* frame;    
00038     GtkWidget *layout;
00039     GtkWidget *menu_bar;
00040     //GtkWidget* scrolled_win;
00041   
00042     // The status bar widget
00043     GtkStatusbar *status_bar;
00044     GtkProgressBar *perf_bar;
00045     GtkProgressBar *rt_bar;
00046     GtkLabel *clock_label;
00047 
00048 #if INCLUDE_GNOME
00049     // temporary and experimental - will get moved away from here soon - rtv
00050     GnomeCanvas* gcanvas;
00051     double zoom;
00052 #endif
00053 
00054     stg_world_t* world; // every window shows a single world
00055     
00056     // stg_rtk doesn't support status bars, so we'll use gtk directly
00057     GtkStatusbar* statusbar;
00058     GtkLabel* timelabel;
00059     
00060     int wf_section; // worldfile section for load/save
00061     
00062     stg_rtk_fig_t* bg; // background
00063     stg_rtk_fig_t* matrix;
00064     stg_rtk_fig_t* matrix_tree;
00065     stg_rtk_fig_t* poses;
00066 
00067     gboolean show_matrix;  
00068     gboolean fill_polygons;
00069     gboolean show_geom;
00070     gboolean show_polygons;
00071     gboolean show_grid;
00072 
00073     int frame_series;
00074     int frame_index;
00075     int frame_callback_tag;
00076     int frame_interval;
00077     int frame_format;
00078 
00079     stg_model_t* selection_active;
00080 
00081     GList* toggle_list;
00082     
00083   } gui_window_t;
00084 
00085   void gui_startup( int* argc, char** argv[] ); 
00086   void gui_poll( void );
00087   void gui_shutdown( void );
00088 
00089   void gui_load( gui_window_t* win, int section );
00090   void gui_save( gui_window_t* win );
00091   
00092   gui_window_t* gui_world_create( stg_world_t* world );
00093   void gui_world_destroy( stg_world_t* world );
00094   int gui_world_update( stg_world_t* world );
00095   void stg_world_add_model( stg_world_t* world, stg_model_t* mod  );
00096   void gui_world_geom( stg_world_t* world );
00097 
00098   void gui_model_create( stg_model_t* model );
00099   void gui_model_destroy( stg_model_t* model );
00100   void gui_model_display_pose( stg_model_t* mod, char* verb );
00101   void gui_model_features( stg_model_t* mod );
00102   void gui_model_geom( stg_model_t* model );
00103   void gui_model_mouse(stg_rtk_fig_t *fig, int event, int mode);
00104   void gui_model_nose( stg_model_t* model );
00105   void gui_window_menus_create( gui_window_t* win );
00106   void gui_window_menus_destroy( gui_window_t* win );
00107 
00108   // callback functions that handle property changes, mostly for drawing stuff in the GUI  
00109   int gui_model_polygons( stg_model_t* mod, void* userp );
00110   int gui_model_grid( stg_model_t* mod, void* userp );
00111   int gui_model_move( stg_model_t* mod, void* userp );
00112   int gui_model_mask( stg_model_t* mod, void* userp );
00113   int gui_model_lines( stg_model_t* mod, void* userp );
00114 
00115 
00116   void gui_add_view_item( const gchar *name,
00117                           const gchar *label,
00118                           const gchar *tooltip,
00119                           GCallback callback,
00120                           gboolean  is_active,
00121                           void* userdata );
00122 
00123   void gui_add_tree_item( stg_model_t* mod );
00124 
00125 
00128   typedef struct
00129   {
00130     stg_model_callback_t callback;
00131     void* arg;
00132   } stg_cbarg_t;
00133 
00134   // callback functions
00135   //typedef void(*func_init_t)(struct _stg_model*);
00136   typedef int(*func_update_t)(struct _stg_model*);
00137   typedef int(*func_startup_t)(struct _stg_model*);
00138   typedef int(*func_shutdown_t)(struct _stg_model*);
00139   typedef void(*func_load_t)(struct _stg_model*);
00140   typedef void(*func_save_t)(struct _stg_model*);
00141   
00142   typedef struct
00143   {
00144     stg_model_t* mod;
00145     void* member;
00146     char* name;
00147     stg_model_callback_t callback_on;
00148     stg_model_callback_t callback_off;
00149     void* arg_on; // argument to callback_on
00150     void* arg_off; // argument to callback_off
00151     //int default_state; // disabled = 0
00152     GtkAction* action; // action associated with this toggle, may be NULL
00153     char* path;
00154   } stg_property_toggle_args_t;
00155     
00156   
00157   typedef struct {
00158     const char* keyword;
00159     stg_model_initializer_t initializer;
00160   } stg_type_record_t;
00161 
00162   struct _stg_model
00163   {
00164     stg_id_t id; // used as hash table key
00165     stg_world_t* world; // pointer to the world in which this model exists
00166     char token[STG_TOKEN_MAX]; // automatically-generated unique ID string
00167     stg_type_record_t* typerec;
00168 
00169     struct _stg_model *parent; // the model that owns this one, possibly NULL
00170 
00171     GPtrArray* children; // the models owned by this model
00172 
00173     // a datalist can contain arbitrary named data items. Can be used
00174     // by derived model types to store properties, and for user code
00175     // to associate arbitrary items with a model.
00176     //GData* props;
00177 
00178     // a datalist of stg_rtk_figs, indexed by name (string)
00179     GData* figs; 
00180 
00181 #if INCLUDE_GNOME
00182     GnomeCanvasGroup* grp;
00183     GnomeCanvasGroup* cgrp;
00184 #endif
00185 
00186     stg_pose_t pose;
00187     stg_velocity_t velocity;
00188     stg_polygon_t* polygons;
00189     size_t polygons_count;
00190     stg_watts_t watts; //< power consumed by this model
00191     stg_color_t color;
00192     stg_kg_t mass;
00193     stg_geom_t geom;
00194     int laser_return;
00195     int obstacle_return;
00196     int blob_return;
00197     int gripper_return;
00198     int ranger_return;
00199     int fiducial_return;
00200     int boundary;
00201     stg_meters_t map_resolution;
00202     stg_bool_t stall;
00203 
00204     int gui_nose;
00205     int gui_grid;
00206     int gui_outline;
00207     int gui_mask;
00208 
00209     GHashTable* callbacks;
00210        
00211     // the number of children of each type is counted so we can
00212     // automatically generate names for them
00213     int child_type_count[256];
00214 
00215     int subs;     // the number of subscriptions to this model
00216 
00217     stg_msec_t interval; // time between updates in ms
00218     stg_msec_t interval_elapsed; // time since last update in ms
00219 
00220     stg_bool_t disabled; // if non-zero, the model is disabled
00221     
00222     // type-dependent functions for this model, implementing simple
00223     // polymorphism
00224     //stg_model_initializer_t initializer;
00225     func_startup_t f_startup;
00226     func_shutdown_t f_shutdown;
00227     func_update_t f_update;
00228     func_load_t f_load;
00229     func_save_t f_save;
00230 
00231     void *data, *cmd, *cfg;
00232     size_t data_len, cmd_len, cfg_len;
00233     
00234     // XX experimental
00235     stg_polyline_t* lines;
00236     size_t lines_count;
00237     
00239     // allow exclusive access to this model
00240     // pthread_mutex_t mutex;
00241 
00242     // end experimental    
00243   };
00244   
00245 
00246   // internal functions
00247   
00248   int _model_update( stg_model_t* mod );
00249   int _model_startup( stg_model_t* mod );
00250   int _model_shutdown( stg_model_t* mod );
00251 
00252   void model_change( stg_model_t* mod, void* address );
00253 
00254   void stg_model_update_velocity( stg_model_t* model );
00255   int stg_model_update_pose( stg_model_t* model );
00256   void stg_model_energy_consume( stg_model_t* mod, stg_watts_t rate );
00257   void stg_model_map( stg_model_t* mod, gboolean render );
00258   void stg_model_map_with_children( stg_model_t* mod, gboolean render );
00259   
00260   stg_rtk_fig_t* stg_model_prop_fig_create( stg_model_t* mod, 
00261                                     stg_rtk_fig_t* array[],
00262                                     stg_id_t propid, 
00263                                     stg_rtk_fig_t* parent,
00264                                     int layer );
00265 
00266   void stg_model_render_geom( stg_model_t* mod );
00267   void stg_model_render_pose( stg_model_t* mod );
00268   void stg_model_render_polygons( stg_model_t* mod );
00269   
00270   int stg_fig_clear_cb(  stg_model_t* mod, char* name, 
00271                          void* data, size_t len, void* userp );
00272   
00273   stg_rtk_fig_t* stg_model_fig_create( stg_model_t* mod, 
00274                                        const char* figname, 
00275                                        const char* parentname,
00276                                        int layer );
00277   
00278   stg_rtk_fig_t* stg_model_get_fig( stg_model_t* mod, const char* figname );
00279   void stg_model_fig_clear( stg_model_t* mod, const char* figname );
00280          
00281   // defines a simulated world
00282   struct _stg_world
00283   {
00284     stg_id_t id; 
00285     
00286     GHashTable* models; 
00287     GHashTable* models_by_name; 
00288     
00289     stg_meters_t width; 
00290     stg_meters_t height; 
00291 
00295     int child_type_count[256];
00296     
00297     struct _stg_matrix* matrix; 
00298 
00299     char* token; 
00300 
00301     stg_msec_t sim_time; 
00302     stg_msec_t sim_interval; 
00303    
00306     stg_msec_t wall_interval;
00307 
00308     stg_msec_t wall_last_update; 
00309     
00314     stg_msec_t real_interval_measured;
00315 
00316     double ppm; 
00317 
00318     gboolean paused; 
00319    
00320     gboolean destroy; 
00321 
00322     gui_window_t* win; 
00323 
00324     int subs; 
00325   };
00326 
00327   // ROTATED RECTANGLES -------------------------------------------------
00328 
00335   typedef struct
00336   {
00337     stg_pose_t pose;
00338     stg_size_t size;
00339   } stg_rotrect_t; // rotated rectangle
00340   
00344   void stg_rotrects_normalize( stg_rotrect_t* rects, int num );
00345   
00351   int stg_rotrects_from_image_file( const char* filename, 
00352                                     stg_rotrect_t** rects,
00353                                     int* rect_count,
00354                                     int* widthp, int* heightp );
00355   
00356 
00360   stg_polygon_t* stg_polygons_from_rotrects( stg_rotrect_t* rects, size_t count,
00361                                              double width, double height );
00362 
00366   // MATRIX  -----------------------------------------------------------------------
00367   
00375   typedef struct stg_cell
00376   {
00377     void* data;
00378     double x, y;
00379     double size;
00380     
00381     // bounding box
00382     double xmin,ymin,xmax,ymax;
00383     
00384     stg_rtk_fig_t* fig; // for debugging
00385 
00386     struct stg_cell* children[4];
00387     struct stg_cell* parent;
00388   } stg_cell_t;
00389   
00392   stg_cell_t* stg_cell_locate( stg_cell_t* cell, double x, double y );
00393   
00394   void stg_cell_unrender( stg_cell_t* cell );
00395   void stg_cell_render( stg_cell_t* cell );
00396   void stg_cell_render_tree( stg_cell_t* cell );
00397   void stg_cell_unrender_tree( stg_cell_t* cell );
00398 
00400   typedef struct _stg_matrix
00401   {
00402     double ppm; // pixels per meter (1/resolution)
00403     double width, height;
00404     
00405     // A quad tree of cells. Each leaf node contains a list of
00406     // pointers to objects located at that cell
00407     stg_cell_t* root;
00408     
00409     // hash table stores all the pointers to objects rendered in the
00410     // quad tree, each associated with a list of cells in which it is
00411     // rendered. This allows us to remove objects from the tree
00412     // without doing the geometry again
00413     GHashTable* ptable;
00414 
00415     /* TODO */
00416     // lists of cells that have changed recently. This is used by the
00417     // GUI to render cells very quickly, and could also be used by devices
00418     //GSList* cells_changed;
00419     
00420     // debug figure. if this is non-NULL, debug info is drawn here
00421     stg_rtk_fig_t* fig;
00422 
00423     // todo - record a timestamp for matrix mods so devices can see if
00424     //the matrix has changed since they last peeked into it
00425     // stg_msec_t last_mod_time;
00426   } stg_matrix_t;
00427   
00428 
00431   stg_matrix_t* stg_matrix_create( double ppm, double width, double height );
00432   
00436   void stg_matrix_destroy( stg_matrix_t* matrix );
00437   
00440   void stg_matrix_clear( stg_matrix_t* matrix );
00441   
00444   void* stg_matrix_cell_get( stg_matrix_t* matrix, int r, double x, double y);
00445 
00448   void stg_matrix_cell_append(  stg_matrix_t* matrix, 
00449                                 double x, double y, void* object );
00450   
00453   void stg_matrix_remove_obect( stg_matrix_t* matrix, void* object );
00454   
00457   void stg_matrix_cell_remove(  stg_matrix_t* matrix,
00458                                 double x, double y, void* object );
00459   
00463   void stg_matrix_rectangle( stg_matrix_t* matrix,
00464                              double px, double py, double pth,
00465                              double dx, double dy, 
00466                              void* object );
00467   
00470   void stg_matrix_line( stg_matrix_t* matrix, 
00471                         double x1, double y1, 
00472                         double x2, double y2,
00473                         void* object );
00474 
00477   typedef struct
00478   {
00479     stg_meters_t x1, y1, x2, y2;
00480   } stg_line_t;
00481   
00482 
00485   void stg_matrix_lines( stg_matrix_t* matrix, 
00486                          stg_line_t* lines, int num_lines,
00487                          void* object );
00488     
00491   void stg_matrix_polygons( stg_matrix_t* matrix,
00492                             double x, double y, double a,
00493                             stg_polygon_t* polys, int num_polys,
00494                             void* object );
00495   
00497   void stg_matrix_polylines( stg_matrix_t* matrix,
00498                              double x, double y, double a,
00499                              stg_polyline_t* polylines, int num_polylines,
00500                              void* object );
00501 
00504   void stg_matrix_remove_object( stg_matrix_t* matrix, void* object );
00505 
00506 
00507   // RAYTRACE ITERATORS -------------------------------------------------------------
00508     
00509   typedef struct
00510   {
00511     double x, y, a;
00512     double cosa, sina, tana;
00513     double range;
00514     double max_range;
00515     double* incr;
00516 
00517     GSList* models;
00518     int index;
00519     stg_matrix_t* matrix;    
00520   
00521   } itl_t;
00522   
00523   typedef enum { PointToPoint=0, PointToBearingRange } itl_mode_t;
00524   
00525   typedef int(*stg_itl_test_func_t)(stg_model_t* finder, stg_model_t* found );
00526   
00527   itl_t* itl_create( double x, double y, double a, double b, 
00528                      stg_matrix_t* matrix, itl_mode_t pmode );
00529   
00530   void itl_destroy( itl_t* itl );
00531   void itl_raytrace( itl_t* itl );
00532   
00533   stg_model_t* itl_first_matching( itl_t* itl, 
00534                                    stg_itl_test_func_t func, 
00535                                    stg_model_t* finder );
00536 
00544   // C wrappers for C++ worldfile functions
00545   void wf_warn_unused( void );
00546   int wf_property_exists( int section, char* token );
00547   int wf_read_int( int section, char* token, int def );
00548   double wf_read_length( int section, char* token, double def );
00549   double wf_read_angle( int section, char* token, double def );
00550   double wf_read_float( int section, char* token, double def );
00551   const char* wf_read_tuple_string( int section, char* token, int index, char* def );
00552   double wf_read_tuple_float( int section, char* token, int index, double def );
00553   double wf_read_tuple_length( int section, char* token, int index, double def );
00554   double wf_read_tuple_angle( int section, char* token, int index, double def );
00555   const char* wf_read_string( int section, char* token, char* def );
00556 
00557   void wf_write_int( int section, char* token, int value );
00558   void wf_write_length( int section, char* token, double value );
00559   void wf_write_angle( int section, char* token, double value );
00560   void wf_write_float( int section, char* token, double value );
00561   void wf_write_tuple_string( int section, char* token, int index, char* value );
00562   void wf_write_tuple_float( int section, char* token, int index, double value );
00563   void wf_write_tuple_length( int section, char* token, int index, double value );
00564   void wf_write_tuple_angle( int section, char* token, int index, double value );
00565   void wf_write_string( int section, char* token, char* value );
00566 
00567   void wf_save( void );
00568   void wf_load( char* path );
00569   int wf_section_count( void );
00570   const char* wf_get_section_type( int section );
00571   int wf_get_parent_section( int section );
00572   const char* wf_get_filename( void);
00573 
00576   // CALLBACK WRAPPERS ------------------------------------------------------------
00577 
00578   // callback wrappers for other functions
00579   void model_update_cb( gpointer key, gpointer value, gpointer user );
00580   void model_print_cb( gpointer key, gpointer value, gpointer user );
00581   void model_destroy_cb( gpointer mod );
00582   
00583 
00585 // end of libstage_internal documentation  
00586 
00587 
00588 // end documentation group stage
00594 // TODO - some of this needs to be implemented, the rest junked.
00595 
00596 /*   //  -------------------------------------------------------------- */
00597 
00598 /*   // standard energy consumption of some devices in W. */
00599 /*   // */
00600 /*   // The MOTIONKG value is a hack to approximate cost of motion, as */
00601 /*   // Stage does not yet have an acceleration model. */
00602 /*   // */
00603 /* #define STG_ENERGY_COST_LASER 20.0 // 20 Watts! (LMS200 - from SICK web site) */
00604 /* #define STG_ENERGY_COST_FIDUCIAL 10.0 // 10 Watts */
00605 /* #define STG_ENERGY_COST_RANGER 0.5 // 500mW (estimate) */
00606 /* #define STG_ENERGY_COST_MOTIONKG 10.0 // 10 Watts per KG when moving  */
00607 /* #define STG_ENERGY_COST_BLOB 4.0 // 4W (estimate) */
00608 
00609 /*   typedef struct */
00610 /*   { */
00611 /*     stg_joules_t joules; // current energy stored in Joules/1000 */
00612 /*     stg_watts_t watts; // current power expenditure in mW (mJoules/sec) */
00613 /*     int charging; // 1 if we are receiving energy, -1 if we are */
00614 /*     // supplying energy, 0 if we are neither charging nor */
00615 /*     // supplying energy. */
00616 /*     stg_meters_t range; // the range that our charging probe hit a charger */
00617 /*   } stg_energy_data_t; */
00618 
00619 /*   typedef struct */
00620 /*   { */
00621 /*     stg_joules_t capacity; // maximum energy we can store (we start fully charged) */
00622 /*     stg_meters_t probe_range; // the length of our recharge probe */
00623 /*     //stg_pose_t probe_pose; // TODO - the origin of our probe */
00624 
00625 /*     stg_watts_t give_rate; // give this many Watts to a probe that hits me (possibly 0) */
00626   
00627 /*     stg_watts_t trickle_rate; // this much energy is consumed or */
00628 /*     // received by this device per second as a */
00629 /*     // baseline trickle. Positive values mean */
00630 /*     // that the device is just burning energy */
00631 /*     // stayting alive, which is appropriate */
00632 /*     // for most devices. Negative values mean */
00633 /*     // that the device is receiving energy */
00634 /*     // from the environment, simulating a */
00635 /*     // solar cell or some other ambient energy */
00636 /*     // collector */
00637 
00638 /*   } stg_energy_config_t; */
00639 
00640 
00641 /*   // BLINKENLIGHT ------------------------------------------------------------ */
00642 
00643 /*   // a number of milliseconds, used for example as the blinkenlight interval */
00644 /* #define STG_LIGHT_ON UINT_MAX */
00645 /* #define STG_LIGHT_OFF 0 */
00646 
00647   //typedef int stg_interval_ms_t;
00648 
00649 
00650 /*   // TOKEN ----------------------------------------------------------------------- */
00651 /*   // token stuff for parsing worldfiles - this may one day replace
00652 the worldfile c++ code */
00653 
00654 /* #define CFG_OPEN '(' */
00655 /* #define CFG_CLOSE ')' */
00656 /* #define STR_OPEN '\"' */
00657 /* #define STR_CLOSE '\"' */
00658 /* #define TPL_OPEN '[' */
00659 /* #define TPL_CLOSE ']' */
00660 
00661 /*   typedef enum { */
00662 /*     STG_T_NUM = 0, */
00663 /*     STG_T_BOOLEAN, */
00664 /*     STG_T_MODELPROP, */
00665 /*     STG_T_WORLDPROP, */
00666 /*     STG_T_NAME, */
00667 /*     STG_T_STRING, */
00668 /*     STG_T_KEYWORD, */
00669 /*     STG_T_CFG_OPEN, */
00670 /*     STG_T_CFG_CLOSE, */
00671 /*     STG_T_TPL_OPEN, */
00672 /*     STG_T_TPL_CLOSE, */
00673 /*   } stg_token_type_t; */
00674 
00675 
00676 
00677 
00678 /* typedef struct stg_token  */
00679 /* { */
00680 /*   char* token; ///< the text of the token */
00681 /*   stg_token_type_t type; ///< the type of the token */
00682 /*   unsigned int line; ///< the line on which the token appears */
00683   
00684 /*   struct stg_token* next; ///< linked list support */
00685 /*   struct stg_token* child; ///< tree support */
00686   
00687 /* } stg_token_t; */
00688 
00689 /*   stg_token_t* stg_token_next( stg_token_t* tokens ); */
00690 /*   /// print [token] formatted for a human reader, with a string [prefix] */
00691 /*   void stg_token_print( char* prefix,  stg_token_t* token ); */
00692 
00693 /*   /// print a token array suitable for human reader */
00694 /*   void stg_tokens_print( stg_token_t* tokens ); */
00695 /*   void stg_tokens_free( stg_token_t* tokens ); */
00696   
00697 /*   /// create a new token structure from the arguments */
00698 /*   stg_token_t* stg_token_create( const char* token, stg_token_type_t type, int line ); */
00699 
00700 /*   /// add a token to a list */
00701 /*   stg_token_t* stg_token_append( stg_token_t* head, */
00702 /*                               char* token, stg_token_type_t type, int line ); */
00703 
00704 /*   const char* stg_token_type_string( stg_token_type_t type ); */
00705 
00706 /*   const char* stg_model_type_string( stg_model_type_t type ); */
00707   
00708 /*   //  functions for parsing worldfiles */
00709 /*   stg_token_t* stg_tokenize( FILE* wf ); */
00710 /*   //stg_world_t* sc_load_worldblock( stg_client_t* cli, stg_token_t** tokensptr ); */
00711 /*   //stg_model_t* sc_load_modelblock( stg_world_t* world, stg_model_t* parent, */
00712 /*   //                    stg_token_t** tokensptr ); */
00713 
00714 
00715 
00716 
00717 #ifdef __cplusplus
00718 }
00719 #endif 
00720 
00721 
00722 #endif // _STAGE_INTERNAL_H
00723 

Generated on Mon Feb 27 23:30:51 2006 for Stage by  doxygen 1.4.4