BRL-CAD
dir.c
Go to the documentation of this file.
1 /* D I R . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1985-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @addtogroup dbio */
21 /** @{ */
22 /** @file librt/dir.c
23  *
24  * Ray Tracing program, GED database directory manager.
25  *
26  */
27 
28 #include "common.h"
29 
30 #include <string.h>
31 #include "bio.h"
32 
33 #include "vmath.h"
34 #include "raytrace.h"
35 
36 
37 /**
38  * Builds a directory of the object names.
39  *
40  * Allocate and initialize information for this instance of an RT
41  * model database.
42  *
43  * Returns -
44  * (struct rt_i *) Success
45  * RTI_NULL Fatal Error
46  */
47 struct rt_i *
48 rt_dirbuild(const char *filename, char *buf, int len)
49 {
50  register struct rt_i *rtip;
51  register struct db_i *dbip; /* Database instance ptr */
52 
53  if (rt_uniresource.re_magic == 0)
55 
56  if ((dbip = db_open(filename, DB_OPEN_READONLY)) == DBI_NULL)
57  return RTI_NULL; /* FAIL */
58  RT_CK_DBI(dbip);
59 
60  if (db_dirbuild(dbip) < 0) {
61  db_close(dbip);
62  return RTI_NULL; /* FAIL */
63  }
64 
65  rtip = rt_new_rti(dbip); /* clones dbip */
66  db_close(dbip); /* releases original dbip */
67 
68  if (buf != (char *)NULL)
69  bu_strlcpy(buf, dbip->dbi_title, len);
70 
71  return rtip; /* OK */
72 }
73 
74 
75 int
77  struct rt_db_internal *ip,
78  const struct directory *dp,
79  const struct db_i *dbip,
80  const mat_t mat,
81  struct resource *resp)
82 {
83  struct bu_external ext;
84  int id;
85  int ret;
86 
88 
89  if (dbip->dbi_version > 4)
90  return rt_db_get_internal5(ip, dp, dbip, mat, resp);
91 
92  BU_EXTERNAL_INIT(&ext);
93 
94  if (db_get_external(&ext, dp, dbip) < 0)
95  return -2; /* FAIL */
96 
97  if (dp->d_flags & RT_DIR_COMB) {
98  id = ID_COMBINATION;
99  } else {
100  /* As a convenience to older ft_import4 routines */
101  if (mat == NULL) mat = bn_mat_identity;
102  id = rt_id_solid(&ext);
103  }
104 
105  /* ip is already initialized and should not be re-initialized */
106  ret = -1;
107  if (OBJ[id].ft_import4) {
108  ret = OBJ[id].ft_import4(ip, &ext, mat, dbip, resp);
109  }
110  if (ret < 0) {
111  bu_log("rt_db_get_internal(%s): import failure\n",
112  dp->d_namep);
114  bu_free_external(&ext);
115  return -1; /* FAIL */
116  }
117  bu_free_external(&ext);
118  RT_CK_DB_INTERNAL(ip);
119  ip->idb_meth = &OBJ[id];
120 
121  /* prior to version 5, there are no attributes ... */
123 
124  /* ... but this isn't the whole story: */
125  if (id == ID_COMBINATION) {
126  const struct rt_comb_internal *comb = (const struct rt_comb_internal *)ip->idb_ptr;
127  RT_CK_COMB(comb);
128 
129  db5_sync_comb_to_attr(&ip->idb_avs, comb);
130  }
131 
132  return id; /* OK */
133 }
134 
135 int
137  struct directory *dp,
138  struct db_i *dbip,
139  struct rt_db_internal *ip,
140  struct resource *resp)
141 {
142  struct bu_external ext;
143  int ret;
144 
145  RT_CK_DB_INTERNAL(ip);
146 
147  if (db_version(dbip) > 4)
148  return rt_db_put_internal5(dp, dbip, ip, resp,
149  DB5_MAJORTYPE_BRLCAD);
150 
151  BU_EXTERNAL_INIT(&ext);
152 
153  /* Scale change on export is 1.0 -- no change */
154  ret = -1;
155  if (ip->idb_meth->ft_export4) {
156  ret = ip->idb_meth->ft_export4(&ext, ip, 1.0, dbip, resp);
157  }
158  if (ret < 0) {
159  bu_log("rt_db_put_internal(%s): solid export failure\n",
160  dp->d_namep);
162  bu_free_external(&ext);
163  return -2; /* FAIL */
164  }
166 
167  if (db_put_external(&ext, dp, dbip) < 0) {
168  bu_free_external(&ext);
169  return -1; /* FAIL */
170  }
171 
172  bu_free_external(&ext);
173  return 0; /* OK */
174 }
175 
176 int
178  FILE *fp,
179  const char *name,
180  const struct rt_db_internal *ip,
181  double conv2mm)
182 {
183  struct bu_external ext;
184  int ret;
185 
186  RT_CK_DB_INTERNAL(ip);
187  RT_CK_FUNCTAB(ip->idb_meth);
188 
189  BU_EXTERNAL_INIT(&ext);
190 
191  ret = -1;
192  if (ip->idb_meth->ft_export4) {
193  ret = ip->idb_meth->ft_export4(&ext, ip, conv2mm, NULL /*dbip*/, &rt_uniresource);
194  }
195  if (ret < 0) {
196  bu_log("rt_file_put_internal(%s): solid export failure\n",
197  name);
198  bu_free_external(&ext);
199  return -2; /* FAIL */
200  }
201  BU_CK_EXTERNAL(&ext);
202 
203  if (db_fwrite_external(fp, name, &ext) < 0) {
204  bu_log("rt_fwrite_internal(%s): db_fwrite_external() error\n",
205  name);
206  bu_free_external(&ext);
207  return -3;
208  }
209  bu_free_external(&ext);
210  return 0;
211 
212 }
213 
214 
215 void
217 {
218  RT_CK_DB_INTERNAL(ip);
219 
220  /* meth is not required since may be asked to free something that
221  * was never set.
222  */
223  if (ip->idb_meth) {
224  RT_CK_FUNCTAB(ip->idb_meth);
225  if (ip->idb_ptr && ip->idb_meth->ft_ifree) {
226  ip->idb_meth->ft_ifree(ip);
227  }
228  }
229 
230  if (ip->idb_ptr) {
231  ip->idb_ptr = NULL; /* sanity. Should be handled by INIT, below */
232  }
233  if (ip->idb_avs.magic == BU_AVS_MAGIC) {
234  bu_avs_free(&ip->idb_avs);
235  }
237 }
238 
239 int
241  struct db_i *dbip,
242  const char *obj_name,
243  struct directory **dpp,
244  struct rt_db_internal *ip,
245  int noisy,
246  struct resource *resp)
247 {
248  struct directory *dp;
249 
250  if (obj_name == (char *) 0) {
251  if (noisy == LOOKUP_NOISY)
252  bu_log("rt_db_lookup_internal() No object specified\n");
253  return ID_NULL;
254  }
255  if ((dp = db_lookup(dbip, obj_name, noisy)) == RT_DIR_NULL)
256  return ID_NULL;
257  if (rt_db_get_internal(ip, dp, dbip, (matp_t) NULL, resp) < 0) {
258  if (noisy == LOOKUP_NOISY)
259  bu_log("rt_db_lookup_internal() Failed to get internal form of object '%s'\n",
260  dp->d_namep);
261  return ID_NULL;
262  }
263 
264  *dpp = dp;
265  return ip->idb_type;
266 }
267 
268 
269 /** @} */
270 /*
271  * Local Variables:
272  * mode: C
273  * tab-width: 8
274  * indent-tabs-mode: t
275  * c-file-style: "stroustrup"
276  * End:
277  * ex: shiftwidth=4 tabstop=8
278  */
char * d_namep
pointer to name string
Definition: raytrace.h:859
char filename[MAXLENGTH]
Definition: human.c:105
Definition: raytrace.h:800
#define RT_CK_FUNCTAB(_p)
Definition: raytrace.h:2242
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
#define BU_AVS_MAGIC
Definition: magic.h:46
void bu_avs_init_empty(struct bu_attribute_value_set *avp)
Definition: avs.c:36
void(* ft_ifree)(struct rt_db_internal *)
Definition: raytrace.h:2159
const mat_t bn_mat_identity
Matrix and vector functionality.
Definition: mat.c:46
Definition: clone.c:90
int rt_db_lookup_internal(struct db_i *dbip, const char *obj_name, struct directory **dpp, struct rt_db_internal *ip, int noisy, struct resource *resp)
Definition: dir.c:240
int(* ft_import4)(struct rt_db_internal *, const struct bu_external *, const mat_t, const struct db_i *, struct resource *)
Definition: raytrace.h:2145
struct directory * db_lookup(const struct db_i *, const char *name, int noisy)
Definition: db_lookup.c:153
int db_version(struct db_i *dbip)
Definition: db5_scan.c:414
#define RT_CK_COMB(_p)
Definition: raytrace.h:955
#define ID_NULL
Unused.
Definition: raytrace.h:458
void bu_free_external(struct bu_external *ep)
Header file for the BRL-CAD common definitions.
#define ID_COMBINATION
Combination Record.
Definition: raytrace.h:499
#define RTI_NULL
Definition: raytrace.h:1830
int db_fwrite_external(FILE *fp, const char *name, struct bu_external *ep)
Definition: db_io.c:337
void rt_init_resource(struct resource *resp, int cpu_num, struct rt_i *rtip)
Definition: prep.c:585
int(* ft_export4)(struct bu_external *, const struct rt_db_internal *, double, const struct db_i *, struct resource *)
Definition: raytrace.h:2152
uint32_t re_magic
Magic number.
Definition: raytrace.h:1441
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
char * dbi_title
title from IDENT rec
Definition: raytrace.h:809
#define RT_CK_DB_INTERNAL(_p)
Definition: raytrace.h:207
int db_get_external(struct bu_external *ep, const struct directory *dp, const struct db_i *dbip)
#define RT_DB_INTERNAL_INIT(_p)
Definition: raytrace.h:199
const struct rt_functab * idb_meth
for ft_ifree(), etc.
Definition: raytrace.h:194
#define bu_strlcpy(dst, src, size)
Definition: str.h:60
struct bu_attribute_value_set idb_avs
Definition: raytrace.h:196
void db_close(struct db_i *dbip)
int rt_fwrite_internal(FILE *fp, const char *name, const struct rt_db_internal *ip, double conv2mm)
Definition: dir.c:177
int rt_id_solid(struct bu_external *ep)
Definition: table.c:2325
uint32_t magic
Definition: avs.h:84
int rt_db_put_internal(struct directory *dp, struct db_i *dbip, struct rt_db_internal *ip, struct resource *resp)
Definition: dir.c:136
struct rt_i * rt_new_rti(struct db_i *dbip)
Definition: prep.c:58
#define RT_CK_DBI(_p)
Definition: raytrace.h:829
void db5_sync_comb_to_attr(struct bu_attribute_value_set *avs, const struct rt_comb_internal *comb)
Definition: db5_attr.c:447
int rt_db_get_internal(struct rt_db_internal *ip, const struct directory *dp, const struct db_i *dbip, const mat_t mat, struct resource *resp)
Definition: dir.c:76
void * idb_ptr
Definition: raytrace.h:195
#define RT_DIR_COMB
combination
Definition: raytrace.h:884
const struct rt_functab OBJ[]
Definition: table.c:159
#define DBI_NULL
Definition: raytrace.h:827
#define RT_DIR_NULL
Definition: raytrace.h:875
#define LOOKUP_NOISY
Definition: raytrace.h:892
#define BU_EXTERNAL_INIT(_p)
Definition: parse.h:229
#define DB_OPEN_READONLY
Definition: raytrace.h:3550
struct db_i * db_open(const char *name, const char *mode)
Definition: db_open.c:59
struct rt_i * rt_dirbuild(const char *filename, char *buf, int len)
Definition: dir.c:48
int dbi_version
PRIVATE: use db_version()
Definition: raytrace.h:824
int rt_db_get_internal5(struct rt_db_internal *ip, const struct directory *dp, const struct db_i *dbip, const mat_t mat, struct resource *resp)
Definition: db5_io.c:961
int db_put_external(struct bu_external *ep, struct directory *dp, struct db_i *dbip)
Definition: db_io.c:281
#define BU_CK_EXTERNAL(_p)
Definition: parse.h:224
int db_dirbuild(struct db_i *dbip)
Definition: db5_scan.c:301
int d_flags
flags
Definition: raytrace.h:869
int rt_db_put_internal5(struct directory *dp, struct db_i *dbip, struct rt_db_internal *ip, struct resource *resp, const int major)
Definition: db5_io.c:789
void bu_avs_free(struct bu_attribute_value_set *avp)
Definition: avs.c:235
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216