Editing Example db walk tree

Warning: You are not logged in.

Your IP address will be recorded in this page's edit history.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
 
[[category:Code examples]]
 
[[category:Code examples]]
 +
 +
<blockquote>
 +
'''This example is for current stable release 7.24.X. In latest release (7.26.1) the function prototype for db_walk_tree has changed, the corresponding changes to the code are: [[Db_walk_tree|Latest db_walk_tree()]]'''
 +
</blockquote>
 +
 
This is a simple example program that shows how to walk a geometry database while performing some operation on geometry as it is encountered.  In this particular example, we iterate just over regions and simply count them:
 
This is a simple example program that shows how to walk a geometry database while performing some operation on geometry as it is encountered.  In this particular example, we iterate just over regions and simply count them:
  
<source lang="C">
 
  
 +
<source lang="c">
 
#include "common.h"
 
#include "common.h"
 
#include "raytrace.h"
 
#include "raytrace.h"
  
int incr_region(struct db_tree_state *tsp, struct db_full_path *pathp, const struct rt_comb_internal *combp, void *data)
+
int incr_region(struct db_tree_state *tsp, struct db_full_path *pathp, const struct rt_comb_internal *combp, genptr_t data)
 
{
 
{
 
   int *counter = (int*)data;
 
   int *counter = (int*)data;
Line 15: Line 20:
 
}
 
}
  
int main(int argc, char *argv[]){
+
int
 
+
main(int argc, char *argv[])
 +
{
 
   struct db_i *dbip;
 
   struct db_i *dbip;
 
   int counter = 0;
 
   int counter = 0;
Line 36: Line 42:
  
 
   bu_log("Database title is:\n%s\n", dbip->dbi_title);
 
   bu_log("Database title is:\n%s\n", dbip->dbi_title);
//  bu_log("Units: %s\n", bu_units_string(dbip->dbi_local2base));
+
  bu_log("Units: %s\n", bu_units_string(dbip->dbi_local2base));
  
 
   if (db_lookup(dbip, argv[2], 1) == NULL) {
 
   if (db_lookup(dbip, argv[2], 1) == NULL) {
Line 48: Line 54:
  
 
   db_walk_tree(dbip, 1, (const char **)argv+2, 1, &state, incr_region, NULL, NULL, &counter);
 
   db_walk_tree(dbip, 1, (const char **)argv+2, 1, &state, incr_region, NULL, NULL, &counter);
 
 
  
 
   bu_log("counter is %d\n", counter);
 
   bu_log("counter is %d\n", counter);
  return 0;
 
  
 +
  return 0;
 
}
 
}
 +
 
</source>
 
</source>
  
 
If we compile this program and run it on a sample geometry database, we get the following:
 
If we compile this program and run it on a sample geometry database, we get the following:
  
<source lang="bash">
 
# you may need to replace /usr/brlcad with the path to where BRL-CAD is installed on your system
 
$ gcc -o db_walk_tree_example -I/usr/brlcad/include -I/usr/brlcad/include/brlcad db_walk_tree_example.c -L/usr/brlcad/lib -lrt -lbu
 
  
$ ./db_walk_tree_example db/ktank.g tank
+
<source lang="bash">
 +
$ gcc db_walk_tree_example.c -L/usr/brlcad/lib -lrt -lbu -I/usr/brlcad/include -I/usr/brlcad/include/brlcad
 +
$ ./a.out db/ktank.g tank
 
Database title is:
 
Database title is:
 
Keith's Tank
 
Keith's Tank
 
+
Units: in
 
...incrementing...
 
...incrementing...
 
<... trimmed output ...>
 
<... trimmed output ...>
 
...incrementing...
 
...incrementing...
counter is 83
+
counter is 80
 +
 
 
</source>
 
</source>

Please note that all contributions to BRL-CAD may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see BRL-CAD:Copyrights for details). Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)