BRL-CAD
generic.c
Go to the documentation of this file.
1 /* G E N E R I C . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1989-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 g_ */
21 /** @{ */
22 /** @file primitives/generic.c
23  *
24  * Generic routines applicable across most primitives
25  *
26  */
27 /** @} */
28 
29 #include "common.h"
30 
31 #include <string.h>
32 
33 
34 #include "bn.h"
35 #include "raytrace.h"
36 
37 
38 /**
39  * Apply a 4x4 transformation matrix to the internal form of a solid.
40  *
41  * If "free" flag is non-zero, storage for the original solid is
42  * released. If "os" is same as "is", storage for the original solid
43  * is overwritten with the new, transformed solid.
44  *
45  * Returns -
46  * -1 FAIL
47  * 0 OK
48  */
49 int
51  struct rt_db_internal *op,
52  const mat_t mat,
53  struct rt_db_internal *ip,
54  int release,
55  struct db_i *dbip,
56  struct resource *resp)
57 {
58  struct bu_external ext;
59  int id;
60  struct bu_attribute_value_set avs;
61 
63  RT_CK_DBI(dbip);
64  RT_CK_RESOURCE(resp);
65 
66  memset(&avs, 0, sizeof(struct bu_attribute_value_set));
67 
68  id = ip->idb_type;
69  BU_EXTERNAL_INIT(&ext);
70  /* Scale change on export is 1.0 -- no change */
71  switch (db_version(dbip)) {
72  case 4:
73  if (OBJ[id].ft_export4(&ext, ip, 1.0, dbip, resp) < 0) {
74  bu_log("rt_generic_xform(): %s export failure\n",
75  OBJ[id].ft_name);
76  return -1; /* FAIL */
77  }
78  if ((release || op == ip)) rt_db_free_internal(ip);
79 
81  if (OBJ[id].ft_import4(op, &ext, mat, dbip, resp) < 0) {
82  bu_log("rt_generic_xform(): solid import failure\n");
83  return -1; /* FAIL */
84  }
85  break;
86  case 5:
87  if (OBJ[id].ft_export5(&ext, ip, 1.0, dbip, resp) < 0) {
88  bu_log("rt_generic_xform(): %s export failure\n",
89  OBJ[id].ft_name);
90  return -1; /* FAIL */
91  }
92 
93  if ((release || op == ip)) {
94  if (ip->idb_avs.magic == BU_AVS_MAGIC) {
95  /* grab the attributes before they are lost
96  * by rt_db_free_internal or RT_DB_INTERNAL_INIT
97  */
98  bu_avs_init(&avs, ip->idb_avs.count, "avs");
99  bu_avs_merge(&avs, &ip->idb_avs);
100  }
102  }
103 
105 
106  if (!release && op != ip) {
107  /* just copy the attributes from ip to op */
108  if (ip->idb_avs.magic == BU_AVS_MAGIC) {
109  bu_avs_init(&op->idb_avs, ip->idb_avs.count, "avs");
110  bu_avs_merge(&op->idb_avs, &ip->idb_avs);
111  }
112  } else if (avs.magic == BU_AVS_MAGIC) {
113  /* put the saved attributes in the output */
114  bu_avs_init(&op->idb_avs, avs.count, "avs");
115  bu_avs_merge(&op->idb_avs, &avs);
116  bu_avs_free(&avs);
117  }
118 
119  if (OBJ[id].ft_import5(op, &ext, mat, dbip, resp) < 0) {
120  bu_log("rt_generic_xform(): solid import failure\n");
121  return -1; /* FAIL */
122  }
123  break;
124  }
125 
126  bu_free_external(&ext);
127 
128  RT_CK_DB_INTERNAL(op);
129  return 0; /* OK */
130 }
131 
132 
133 /**
134  * This is the generic routine to be listed in OBJ[].ft_get
135  * for those solid types which are fully described by their
136  * ft_parsetab entry.
137  *
138  * 'attr' is specified to retrieve only one attribute, rather than
139  * all. Example: "db get ell.s B" to get only the B vector.
140  */
141 int
142 rt_generic_get(struct bu_vls *logstr, const struct rt_db_internal *intern, const char *attr)
143 {
144  register const struct bu_structparse *sp = NULL;
145  register const struct rt_functab *ftp;
146 
147  RT_CK_DB_INTERNAL(intern);
148  ftp = intern->idb_meth;
149  RT_CK_FUNCTAB(ftp);
150 
151  sp = ftp->ft_parsetab;
152  if (!sp) {
153  bu_vls_printf(logstr,
154  "%s {a Tcl output routine for this type of object has not yet been implemented}",
155  ftp->ft_label);
156  return BRLCAD_ERROR;
157  }
158 
159  if (attr == (char *)0) {
160  struct bu_vls str = BU_VLS_INIT_ZERO;
161 
162  /* Print out solid type and all attributes */
163  bu_vls_printf(logstr, "%s", ftp->ft_label);
164  while (sp->sp_name != NULL) {
165  bu_vls_printf(logstr, " %s", sp->sp_name);
166 
167  bu_vls_trunc(&str, 0);
168  bu_vls_struct_item(&str, sp,
169  (char *)intern->idb_ptr, ' ');
170 
171  if (sp->sp_count < 2)
172  bu_vls_printf(logstr, " %s", bu_vls_addr(&str));
173  else {
174  bu_vls_printf(logstr, " {");
175  bu_vls_printf(logstr, "%s", bu_vls_addr(&str));
176  bu_vls_printf(logstr, "} ");
177  }
178 
179  ++sp;
180  }
181 
182  bu_vls_free(&str);
183  } else {
184  if (bu_vls_struct_item_named(logstr, sp, attr, (char *)intern->idb_ptr, ' ') < 0) {
185  bu_vls_printf(logstr,
186  "Objects of type %s do not have a %s attribute.",
187  ftp->ft_label, attr);
188  return BRLCAD_ERROR;
189  }
190  }
191 
192  return BRLCAD_OK;
193 }
194 
195 
196 /**
197  * This one assumes that making all the parameters null is fine.
198  */
199 void
200 rt_generic_make(const struct rt_functab *ftp, struct rt_db_internal *intern)
201 {
202  intern->idb_type = ftp - OBJ;
203  intern->idb_major_type = DB5_MAJORTYPE_BRLCAD;
204  BU_ASSERT(&OBJ[intern->idb_type] == ftp);
205 
206  intern->idb_meth = ftp;
207  intern->idb_ptr = bu_calloc(1, (unsigned int)ftp->ft_internal_size, "rt_generic_make");
208  *((uint32_t *)(intern->idb_ptr)) = ftp->ft_internal_magic;
209 }
210 
211 
212 /**
213  * For those solids entirely defined by their parsetab. Invoked via
214  * OBJ[].ft_adjust()
215  */
216 int
217 rt_generic_adjust(struct bu_vls *logstr, struct rt_db_internal *intern, int argc, const char **argv)
218 {
219  const struct rt_functab *ftp;
220 
221  RT_CK_DB_INTERNAL(intern);
222  ftp = intern->idb_meth;
223  RT_CK_FUNCTAB(ftp);
224 
225  if (ftp->ft_parsetab == (struct bu_structparse *)NULL) {
226  bu_vls_printf(logstr, "%s type objects do not yet have a 'db put' (adjust) handler.", ftp->ft_label);
227  return BRLCAD_ERROR;
228  }
229 
230  return bu_structparse_argv(logstr, argc, argv, ftp->ft_parsetab, (char *)intern->idb_ptr, NULL);
231 }
232 
233 
234 /**
235  * Invoked via OBJ[].ft_form() on solid types which are
236  * fully described by their bu_structparse table in ft_parsetab.
237  */
238 int
239 rt_generic_class(const struct soltab *UNUSED(s), const vect_t UNUSED(v0), const vect_t UNUSED(v2), const struct bn_tol *UNUSED(b))
240 {
241  return 0;
242 }
243 
244 /**
245  * Invoked via OBJ[].ft_form() on solid types which are
246  * fully described by their bu_structparse table in ft_parsetab.
247  */
248 int
249 rt_generic_form(struct bu_vls *logstr, const struct rt_functab *ftp)
250 {
251  RT_CK_FUNCTAB(ftp);
252 
253  if (ftp->ft_parsetab) {
255  return BRLCAD_OK;
256  }
257  bu_vls_printf(logstr,
258  "%s is a valid object type, but a 'form' routine has not yet been implemented.",
259  ftp->ft_label);
260 
261  return BRLCAD_ERROR;
262 }
263 
264 
265 /*
266  * Local Variables:
267  * mode: C
268  * tab-width: 8
269  * indent-tabs-mode: t
270  * c-file-style: "stroustrup"
271  * End:
272  * ex: shiftwidth=4 tabstop=8
273  */
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
const char * sp_name
Definition: parse.h:140
uint32_t ft_internal_magic
RT_XXX_INTERNAL_MAGIC.
Definition: raytrace.h:2178
if lu s
Definition: nmg_mod.c:3860
void bu_avs_merge(struct bu_attribute_value_set *dest, const struct bu_attribute_value_set *src)
Definition: avs.c:154
void bu_vls_trunc(struct bu_vls *vp, int len)
Definition: vls.c:198
int db_version(struct db_i *dbip)
Definition: db5_scan.c:414
void bu_free_external(struct bu_external *ep)
Header file for the BRL-CAD common definitions.
int rt_generic_form(struct bu_vls *logstr, const struct rt_functab *ftp)
Definition: generic.c:249
#define BU_ASSERT(_equation)
Definition: defines.h:216
char ft_label[9]
Definition: raytrace.h:2044
int rt_generic_get(struct bu_vls *logstr, const struct rt_db_internal *intern, const char *attr)
Definition: generic.c:142
int idb_major_type
Definition: raytrace.h:192
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
int rt_generic_class(const struct soltab *s, const vect_t v0, const vect_t v2, const struct bn_tol *b)
Definition: generic.c:239
void * memset(void *s, int c, size_t n)
#define RT_CK_DB_INTERNAL(_p)
Definition: raytrace.h:207
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
int bu_vls_struct_item_named(struct bu_vls *vp, const struct bu_structparse *sdp, const char *name, const char *base, int sep_char)
Definition: parse.c:1203
#define RT_DB_INTERNAL_INIT(_p)
Definition: raytrace.h:199
const struct rt_functab * idb_meth
for ft_ifree(), etc.
Definition: raytrace.h:194
const struct bu_structparse * ft_parsetab
rt_xxx_parse
Definition: raytrace.h:2176
#define BRLCAD_OK
Definition: defines.h:71
struct bu_attribute_value_set idb_avs
Definition: raytrace.h:196
#define UNUSED(parameter)
Definition: common.h:239
uint32_t magic
Definition: avs.h:84
Support for uniform tolerances.
Definition: tol.h:71
void bu_avs_init(struct bu_attribute_value_set *avp, size_t len, const char *str)
Definition: avs.c:47
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
int bu_structparse_argv(struct bu_vls *str, int argc, const char **argv, const struct bu_structparse *desc, char *base, void *data)
Definition: parse.c:2522
#define RT_CK_DBI(_p)
Definition: raytrace.h:829
void rt_generic_make(const struct rt_functab *ftp, struct rt_db_internal *intern)
Definition: generic.c:200
void * idb_ptr
Definition: raytrace.h:195
void bu_vls_struct_item(struct bu_vls *vp, const struct bu_structparse *sdp, const char *base, int sep_char)
Definition: parse.c:1097
const struct rt_functab OBJ[]
Definition: table.c:159
#define RT_CK_RESOURCE(_p)
Definition: raytrace.h:1490
size_t sp_count
Definition: parse.h:139
size_t ft_internal_size
sizeof(struct rt_xxx_internal)
Definition: raytrace.h:2177
void bu_structparse_get_terse_form(struct bu_vls *logstr, const struct bu_structparse *sp)
Definition: parse.c:2483
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define BU_EXTERNAL_INIT(_p)
Definition: parse.h:229
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
Definition: vls.h:56
#define BRLCAD_ERROR
Definition: defines.h:72
int rt_generic_xform(struct rt_db_internal *op, const mat_t mat, struct rt_db_internal *ip, int release, struct db_i *dbip, struct resource *resp)
Definition: generic.c:50
void bu_avs_free(struct bu_attribute_value_set *avp)
Definition: avs.c:235
int rt_generic_adjust(struct bu_vls *logstr, struct rt_db_internal *intern, int argc, const char **argv)
Definition: generic.c:217
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216