db_inmem.c

Go to the documentation of this file.
00001 /*                     D B _ I N M E M . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** @addtogroup dbio */
00023 
00024 /*@{*/
00025 /** @file db_inmem.c
00026  *
00027  * Routines in support of maintaining geometry in-memory-only.  The
00028  * general process for adding geometry to an inmem database is to
00029  * either: 
00030  *
00031  *   1) call wdb_export_external() providing an external
00032  *   representation of the geometry object and a flag marking it as
00033  *   in-memory (preferred), or
00034  *
00035  *   2) call db_diradd() and mark the directory entry as in-memory via
00036  *   a call to db_inmem() providing an external representation.
00037  *
00038  * Functions -
00039  *      db_open_inmem   open a database marking it as in-memory-only
00040  *      db_create_inmem create an in-memory-only database instance
00041  *      db_inmem        convert existing dir entry to in-memory-only
00042  *
00043  *  Authors -
00044  *      Christopher Sean Morrison
00045  *
00046  *  Source -
00047  *      SECAD/VLD Computing Consortium, Bldg 394
00048  *      The U. S. Army Ballistic Research Laboratory
00049  *      Aberdeen Proving Ground, Maryland  21005-5066
00050  *
00051  */
00052 
00053 #include "common.h"
00054 
00055 #include "machine.h"
00056 #include "vmath.h"
00057 #include "db.h"
00058 #include "raytrace.h"
00059 
00060 
00061 #define DEFAULT_DB_TITLE "Untitled BRL-CAD Database"
00062 
00063 /**
00064  * d b _ o p e n _ i n m e m
00065  *
00066  * "open" an in-memory-only database instance.  this initializes a
00067  * dbip for use, creating an inmem dbi_wdbp as the means to add
00068  * geometry to the directory (use wdb_export_external()).
00069  */
00070 struct db_i *
00071 db_open_inmem(void)
00072 {
00073     register struct db_i *dbip = DBI_NULL;
00074     register int i;
00075 
00076     BU_GETSTRUCT( dbip, db_i );
00077     dbip->dbi_eof = -1L;
00078     dbip->dbi_fd = -1;
00079     dbip->dbi_fp = NULL;
00080     dbip->dbi_mf = NULL;
00081     dbip->dbi_read_only = 1;
00082 
00083     /* Initialize fields */
00084     for( i=0; i<RT_DBNHASH; i++ ) {
00085         dbip->dbi_Head[i] = DIR_NULL;
00086     }
00087 
00088     dbip->dbi_local2base = 1.0;         /* mm */
00089     dbip->dbi_base2local = 1.0;
00090     dbip->dbi_title = bu_strdup(DEFAULT_DB_TITLE);
00091     dbip->dbi_uses = 1;
00092     dbip->dbi_filename = NULL;
00093     dbip->dbi_filepath = NULL;
00094     dbip->dbi_version = 5;
00095 
00096     /* XXX might want/need to stash an ident record so it's valid.
00097      * see db_fwrite_ident();
00098      */
00099 
00100     bu_ptbl_init( &dbip->dbi_clients, 128, "dbi_clients[]" );
00101     dbip->dbi_magic = DBI_MAGIC;                /* Now it's valid */
00102 
00103     /* mark the wdb structure as in-memory. */
00104     dbip->dbi_wdbp = wdb_dbopen(dbip, RT_WDB_TYPE_DB_INMEM);
00105 
00106     return dbip;
00107 }
00108 
00109 
00110 /**
00111  * d b _ c r e a t e _ i n m e m
00112  *
00113  * creates an in-memory-only database.  this is very similar to
00114  * db_open_inmem() with the exception that the this routine adds a
00115  * default _GLOBAL object.
00116  */
00117 struct db_i *
00118 db_create_inmem(void) {
00119     struct db_i *dbip;
00120     struct bu_external obj;
00121     struct bu_attribute_value_set avs;
00122     struct bu_vls units;
00123     struct bu_external attr;
00124     int flags;
00125 
00126     dbip = db_open_inmem();
00127 
00128     RT_CK_DBI(dbip);
00129     RT_CK_WDB(dbip->dbi_wdbp);
00130 
00131 #if 0
00132     /* create the header record? */
00133     db5_export_object3( &out, DB5HDR_HFLAGS_DLI_HEADER_OBJECT,
00134                         NULL, 0, NULL, NULL,
00135                         DB5_MAJORTYPE_RESERVED, 0,
00136                         DB5_ZZZ_UNCOMPRESSED, DB5_ZZZ_UNCOMPRESSED );
00137 #endif
00138 
00139     /* Second, create the attribute-only _GLOBAL object */
00140     bu_vls_init( &units );
00141     bu_vls_printf( &units, "%.25e", dbip->dbi_local2base );
00142     
00143     bu_avs_init( &avs, 4, "db_create_inmem" );
00144     bu_avs_add( &avs, "title", dbip->dbi_title );
00145     bu_avs_add( &avs, "units", bu_vls_addr(&units) );
00146 
00147     db5_export_attributes( &attr, &avs );
00148     db5_export_object3(&obj, DB5HDR_HFLAGS_DLI_APPLICATION_DATA_OBJECT,
00149                        DB5_GLOBAL_OBJECT_NAME, DB5HDR_HFLAGS_HIDDEN_OBJECT, &attr, NULL,
00150                        DB5_MAJORTYPE_ATTRIBUTE_ONLY, 0,
00151                        DB5_ZZZ_UNCOMPRESSED, DB5_ZZZ_UNCOMPRESSED );
00152     flags = DIR_HIDDEN | DIR_NON_GEOM | RT_DIR_INMEM;
00153     wdb_export_external(dbip->dbi_wdbp, &obj, DB5_GLOBAL_OBJECT_NAME, flags, 0);
00154 
00155     bu_free_external( &obj );
00156     bu_free_external( &attr );
00157     bu_avs_free( &avs );
00158     bu_vls_free( &units );
00159 
00160     return dbip;
00161 }
00162 
00163 
00164 /**
00165  *                      D B _ I N M E M
00166  *
00167  *  Transmogrify an existing directory entry to be an in-memory-only
00168  *  one, stealing the external representation from 'ext'.
00169  */
00170 void
00171 db_inmem(struct directory *dp, struct bu_external *ext, int flags, struct db_i *dbip)
00172 {
00173     BU_CK_EXTERNAL(ext);
00174     RT_CK_DIR(dp);
00175 
00176     if( dp->d_flags & RT_DIR_INMEM )
00177         bu_free( dp->d_un.ptr, "db_inmem() ext ptr" );
00178     dp->d_un.ptr = ext->ext_buf;
00179     if( dbip->dbi_version < 5 ) {
00180         dp->d_len = ext->ext_nbytes / 128;      /* DB_MINREC granule size */
00181     } else {
00182         dp->d_len = ext->ext_nbytes;
00183     }
00184     dp->d_flags = flags | RT_DIR_INMEM;
00185 
00186     /* Empty out the external structure, but leave it w/valid magic */
00187     ext->ext_buf = (genptr_t)NULL;
00188     ext->ext_nbytes = 0;
00189 }
00190 
00191 /*@}*/
00192 /*
00193  * Local Variables:
00194  * mode: C
00195  * tab-width: 8
00196  * c-basic-offset: 4
00197  * indent-tabs-mode: t
00198  * End:
00199  * ex: shiftwidth=4 tabstop=8
00200  */

Generated on Mon Sep 18 01:24:49 2006 for BRL-CAD by  doxygen 1.4.6