BRL-CAD
bot.c
Go to the documentation of this file.
1 /* B O T . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1999-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 
21 /** @file libwdb/bot.c
22  *
23  * Support for BOT solid (Bag O'Triangles)
24  *
25  */
26 
27 #include "common.h"
28 
29 #include <string.h>
30 #include <math.h>
31 #include "bio.h"
32 
33 #include "bu/bitv.h"
34 #include "bu/log.h"
35 #include "vmath.h"
36 #include "bn.h"
37 #include "rtgeom.h"
38 #include "raytrace.h"
39 #include "wdb.h"
40 
41 
42 int
44  struct rt_wdb *fp,
45  const char *name,
46  unsigned char mode,
47  unsigned char orientation,
48  unsigned char flags,
49  size_t num_vertices,
50  size_t num_faces,
51  const fastf_t *vertices, /* array of floats for vertices [num_vertices*3] */
52  const int *faces, /* array of ints for faces [num_faces*3] */
53  const fastf_t *thickness, /* array of plate mode thicknesses
54  * (corresponds to array of faces) NULL for
55  * modes RT_BOT_SURFACE and RT_BOT_SOLID.
56  */
57  struct bu_bitv *face_mode, /* a flag for each face indicating
58  * thickness is appended to hit point,
59  * otherwise thickness is centered
60  * about hit point
61  */
62  size_t num_normals, /* number of unit normals in normals array */
63  fastf_t *normals, /* array of floats for normals [num_normals*3] */
64  int *face_normals) /* array of ints (indices into normals array),
65  * must have 3*num_faces entries.
66  */
67 {
68  struct rt_bot_internal *bot;
69  size_t i;
70 
71  if ((num_normals > 0) && (db_version(fp->dbip) < 5)) {
72  bu_log("You are using an old database format which does not support surface normals for BOT primitives\n");
73  bu_log("You are attempting to create a BOT primitive named \"%s\" with surface normals\n", name);
74  bu_log("The surface normals will not be saved\n");
75  bu_log("Please upgrade to the current database format by using \"dbupgrade\"\n");
76  }
77 
78  BU_ALLOC(bot, struct rt_bot_internal);
79  bot->magic = RT_BOT_INTERNAL_MAGIC;
80  bot->mode = mode;
81  bot->orientation = orientation;
82  bot->bot_flags = flags;
83  bot->num_vertices = num_vertices;
84  bot->num_faces = num_faces;
85  bot->vertices = (fastf_t *)bu_calloc(num_vertices * 3, sizeof(fastf_t), "bot->vertices");
86  for (i=0; i<num_vertices*3; i++)
87  bot->vertices[i] = vertices[i];
88  bot->faces = (int *)bu_calloc(num_faces * 3, sizeof(int), "bot->faces");
89  for (i=0; i<num_faces*3; i++)
90  bot->faces[i] = faces[i];
91  if (mode == RT_BOT_PLATE) {
92  bot->thickness = (fastf_t *)bu_calloc(num_faces, sizeof(fastf_t), "bot->thickness");
93  for (i=0; i<num_faces; i++)
94  bot->thickness[i] = thickness[i];
95  bot->face_mode = bu_bitv_dup(face_mode);
96  } else {
97  bot->thickness = (fastf_t *)NULL;
98  bot->face_mode = (struct bu_bitv *)NULL;
99  }
100 
101  if ((num_normals > 0) && (db_version(fp->dbip) > 4)) {
102  bot->num_normals = num_normals;
103  bot->num_face_normals = bot->num_faces;
104  bot->normals = (fastf_t *)bu_calloc(bot->num_normals * 3, sizeof(fastf_t), "BOT normals");
105  bot->face_normals = (int *)bu_calloc(bot->num_faces * 3, sizeof(int), "BOT face normals");
106  memcpy(bot->normals, normals, bot->num_normals * 3 * sizeof(fastf_t));
107  memcpy(bot->face_normals, face_normals, bot->num_faces * 3 * sizeof(int));
108  } else {
109  bot->bot_flags = 0;
110  bot->num_normals = 0;
111  bot->num_face_normals = 0;
112  bot->normals = (fastf_t *)NULL;
113  bot->face_normals = (int *)NULL;
114  }
115 
116  return wdb_export(fp, name, (void *)bot, ID_BOT, mk_conv2mm);
117 }
118 
119 
120 int
122  struct rt_wdb *fp,
123  const char *name,
124  unsigned char mode,
125  unsigned char orientation,
126  unsigned char flags,
127  size_t num_vertices,
128  size_t num_faces,
129  fastf_t *vertices, /* array of floats for vertices [num_vertices*3] */
130  int *faces, /* array of ints for faces [num_faces*3] */
131  fastf_t *thickness, /* array of plate mode thicknesses
132  * (corresponds to array of faces) NULL for
133  * modes RT_BOT_SURFACE and RT_BOT_SOLID.
134  */
135  struct bu_bitv *face_mode) /* a flag for each face indicating
136  * thickness is appended to hit point,
137  * otherwise thickness is centered
138  * about hit point
139  */
140 {
141  return (mk_bot_w_normals(fp, name, mode, orientation, flags, num_vertices, num_faces, vertices,
142  faces, thickness, face_mode, 0, NULL, NULL));
143 }
144 
145 
146 /*
147  * Local Variables:
148  * mode: C
149  * tab-width: 8
150  * indent-tabs-mode: t
151  * c-file-style: "stroustrup"
152  * End:
153  * ex: shiftwidth=4 tabstop=8
154  */
int mk_bot(struct rt_wdb *fp, const char *name, unsigned char mode, unsigned char orientation, unsigned char flags, size_t num_vertices, size_t num_faces, fastf_t *vertices, int *faces, fastf_t *thickness, struct bu_bitv *face_mode)
Definition: bot.c:121
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
#define RT_BOT_INTERNAL_MAGIC
Definition: magic.h:85
struct db_i * dbip
Definition: raytrace.h:1266
Definition: clone.c:90
#define ID_BOT
Bag o' triangles.
Definition: raytrace.h:488
int db_version(struct db_i *dbip)
Definition: db5_scan.c:414
Header file for the BRL-CAD common definitions.
if(share_geom)
Definition: nmg_mod.c:3829
int wdb_export(struct rt_wdb *wdbp, const char *name, void *gp, int id, double local2mm)
Definition: wdb.c:265
#define BU_ALLOC(_ptr, _type)
Definition: malloc.h:223
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
double mk_conv2mm
Conversion factor to mm.
Definition: units.c:37
struct bu_bitv * bu_bitv_dup(const struct bu_bitv *bv)
Definition: bitv.h:105
double fastf_t
Definition: defines.h:300
int mk_bot_w_normals(struct rt_wdb *fp, const char *name, unsigned char mode, unsigned char orientation, unsigned char flags, size_t num_vertices, size_t num_faces, const fastf_t *vertices, const int *faces, const fastf_t *thickness, struct bu_bitv *face_mode, size_t num_normals, fastf_t *normals, int *face_normals)
Definition: bot.c:43
eu1 orientation
Definition: nmg_mod.c:3916