BRL-CAD
vlist.h
Go to the documentation of this file.
1/* V L I S T . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-2023 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 bv_vlist
21 *
22 * @brief
23 * Definitions for handling lists of vectors (really vertices, or
24 * points) and polygons in 3-space. Intended for common handling of
25 * wireframe display information, in the full resolution that is
26 * calculated in.
27 *
28 * On 32-bit machines, BV_VLIST_CHUNK of 35 results in bv_vlist
29 * structures just less than 1k bytes.
30 *
31 * The head of the doubly linked list can be just a "struct bu_list"
32 * head.
33 *
34 * To visit all the elements in the vlist:
35 * for (BU_LIST_FOR(vp, bv_vlist, hp)) {
36 * int i;
37 * int nused = vp->nused;
38 * int *cmd = vp->cmd;
39 * point_t *pt = vp->pt;
40 * for (i = 0; i < nused; i++, cmd++, pt++) {
41 * access(*cmd, *pt);
42 * access(vp->cmd[i], vp->pt[i]);
43 * }
44 * }
45 */
46/** @{ */
47/** @file bv/vlist.h */
48
49#ifndef BV_VLIST_H
50#define BV_VLIST_H
51
52#include "common.h"
53#include <stdio.h> /* for FILE */
54#include "vmath.h"
55#include "bu/magic.h"
56#include "bu/list.h"
57#include "bu/vls.h"
58#include "bv/defines.h"
59
60__BEGIN_DECLS
61
62#define BV_VLIST_CHUNK 35 /**< @brief 32-bit mach => just less than 1k */
63
64struct bv_vlist {
65 struct bu_list l; /**< @brief magic, forw, back */
66 size_t nused; /**< @brief elements 0..nused active */
67 int cmd[BV_VLIST_CHUNK]; /**< @brief VL_CMD_* */
68 point_t pt[BV_VLIST_CHUNK]; /**< @brief associated 3-point/vect */
69};
70#define BV_VLIST_NULL ((struct bv_vlist *)0)
71#define BV_CK_VLIST(_p) BU_CKMAG((_p), BV_VLIST_MAGIC, "bv_vlist")
72
73/* should these be enum? -Erik */
74/* Values for cmd[] */
75#define BV_VLIST_LINE_MOVE 0 /**< @brief specify new line */
76#define BV_VLIST_LINE_DRAW 1 /**< @brief subsequent line vertex */
77#define BV_VLIST_POLY_START 2 /**< @brief pt[] has surface normal */
78#define BV_VLIST_POLY_MOVE 3 /**< @brief move to first poly vertex */
79#define BV_VLIST_POLY_DRAW 4 /**< @brief subsequent poly vertex */
80#define BV_VLIST_POLY_END 5 /**< @brief last vert (repeats 1st), draw poly */
81#define BV_VLIST_POLY_VERTNORM 6 /**< @brief per-vertex normal, for interpolation */
82#define BV_VLIST_TRI_START 7 /**< @brief pt[] has surface normal */
83#define BV_VLIST_TRI_MOVE 8 /**< @brief move to first triangle vertex */
84#define BV_VLIST_TRI_DRAW 9 /**< @brief subsequent triangle vertex */
85#define BV_VLIST_TRI_END 10 /**< @brief last vert (repeats 1st), draw poly */
86#define BV_VLIST_TRI_VERTNORM 11 /**< @brief per-vertex normal, for interpolation */
87#define BV_VLIST_POINT_DRAW 12 /**< @brief Draw a single point */
88#define BV_VLIST_POINT_SIZE 13 /**< @brief specify point pixel size */
89#define BV_VLIST_LINE_WIDTH 14 /**< @brief specify line pixel width */
90#define BV_VLIST_DISPLAY_MAT 15 /**< @brief specify the model matrix */
91#define BV_VLIST_MODEL_MAT 16 /**< @brief specify the display matrix */
92#define BV_VLIST_CMD_MAX 16 /**< @brief Max command number */
93/**
94 * Applications that are going to use BV_ADD_VLIST and BV_GET_VLIST
95 * are required to execute this macro once, on their _free_hd:
96 * BU_LIST_INIT(&_free_hd);
97 *
98 * Note that BV_GET_VLIST and BV_FREE_VLIST are non-PARALLEL.
99 */
100#define BV_GET_VLIST(_free_hd, p) do {\
101 (p) = BU_LIST_FIRST(bv_vlist, (_free_hd)); \
102 if (BU_LIST_IS_HEAD((p), (_free_hd))) { \
103 BU_ALLOC((p), struct bv_vlist); \
104 (p)->l.magic = BV_VLIST_MAGIC; \
105 } else { \
106 BU_LIST_DEQUEUE(&((p)->l)); \
107 } \
108 (p)->nused = 0; \
109 } while (0)
110
111/** Place an entire chain of bv_vlist structs on the freelist _free_hd */
112#define BV_FREE_VLIST(_free_hd, hd) do { \
113 BU_CK_LIST_HEAD((hd)); \
114 BU_LIST_APPEND_LIST((_free_hd), (hd)); \
115 } while (0)
116
117#define BV_ADD_VLIST(_free_hd, _dest_hd, pnt, draw) do { \
118 struct bv_vlist *_vp; \
119 BU_CK_LIST_HEAD(_dest_hd); \
120 _vp = BU_LIST_LAST(bv_vlist, (_dest_hd)); \
121 if (BU_LIST_IS_HEAD(_vp, (_dest_hd)) || _vp->nused >= BV_VLIST_CHUNK) { \
122 BV_GET_VLIST(_free_hd, _vp); \
123 BU_LIST_INSERT((_dest_hd), &(_vp->l)); \
124 } \
125 VMOVE(_vp->pt[_vp->nused], (pnt)); \
126 _vp->cmd[_vp->nused++] = (draw); \
127 } while (0)
128
129/** Change the transformation matrix to display */
130#define BV_VLIST_SET_DISP_MAT(_free_hd, _dest_hd, _ref_pt) do { \
131 struct bv_vlist *_vp; \
132 BU_CK_LIST_HEAD(_dest_hd); \
133 _vp = BU_LIST_LAST(bv_vlist, (_dest_hd)); \
134 if (BU_LIST_IS_HEAD(_vp, (_dest_hd)) || _vp->nused >= BV_VLIST_CHUNK) { \
135 BV_GET_VLIST(_free_hd, _vp); \
136 BU_LIST_INSERT((_dest_hd), &(_vp->l)); \
137 } \
138 VMOVE(_vp->pt[_vp->nused], (_ref_pt)); \
139 _vp->cmd[_vp->nused++] = BV_VLIST_DISPLAY_MAT; \
140 } while (0)
141
142/** Change the transformation matrix to model */
143#define BV_VLIST_SET_MODEL_MAT(_free_hd, _dest_hd) do { \
144 struct bv_vlist *_vp; \
145 BU_CK_LIST_HEAD(_dest_hd); \
146 _vp = BU_LIST_LAST(bv_vlist, (_dest_hd)); \
147 if (BU_LIST_IS_HEAD(_vp, (_dest_hd)) || _vp->nused >= BV_VLIST_CHUNK) { \
148 BV_GET_VLIST(_free_hd, _vp); \
149 BU_LIST_INSERT((_dest_hd), &(_vp->l)); \
150 } \
151 _vp->cmd[_vp->nused++] = BV_VLIST_MODEL_MAT; \
152 } while (0)
153
154/** Set a point size to apply to the vlist elements that follow. */
155#define BV_VLIST_SET_POINT_SIZE(_free_hd, _dest_hd, _size) do { \
156 struct bv_vlist *_vp; \
157 BU_CK_LIST_HEAD(_dest_hd); \
158 _vp = BU_LIST_LAST(bv_vlist, (_dest_hd)); \
159 if (BU_LIST_IS_HEAD(_vp, (_dest_hd)) || _vp->nused >= BV_VLIST_CHUNK) { \
160 BV_GET_VLIST(_free_hd, _vp); \
161 BU_LIST_INSERT((_dest_hd), &(_vp->l)); \
162 } \
163 _vp->pt[_vp->nused][0] = (_size); \
164 _vp->cmd[_vp->nused++] = BV_VLIST_POINT_SIZE; \
165 } while (0)
166
167/** Set a line width to apply to the vlist elements that follow. */
168#define BV_VLIST_SET_LINE_WIDTH(_free_hd, _dest_hd, _width) do { \
169 struct bv_vlist *_vp; \
170 BU_CK_LIST_HEAD(_dest_hd); \
171 _vp = BU_LIST_LAST(bv_vlist, (_dest_hd)); \
172 if (BU_LIST_IS_HEAD(_vp, (_dest_hd)) || _vp->nused >= BV_VLIST_CHUNK) { \
173 BV_GET_VLIST(_free_hd, _vp); \
174 BU_LIST_INSERT((_dest_hd), &(_vp->l)); \
175 } \
176 _vp->pt[_vp->nused][0] = (_width); \
177 _vp->cmd[_vp->nused++] = BV_VLIST_LINE_WIDTH; \
178 } while (0)
179
180
181BV_EXPORT extern size_t bv_vlist_cmd_cnt(struct bv_vlist *vlist);
182BV_EXPORT extern int bv_vlist_bbox(struct bu_list *vlistp, point_t *bmin, point_t *bmax, size_t *length, int *dispmode);
183
184
185
186/**
187 * For plotting, a way of separating plots into separate color vlists:
188 * blocks of vlists, each with an associated color.
189 */
191 uint32_t magic;
192 size_t nused;
193 size_t max;
194 long *rgb; /**< @brief rgb[max] variable size array */
195 struct bu_list *head; /**< @brief head[max] variable size array */
196 struct bu_list *free_vlist_hd; /**< @brief where to get/put free vlists */
197};
198#define BV_CK_VLBLOCK(_p) BU_CKMAG((_p), BV_VLBLOCK_MAGIC, "bv_vlblock")
199
200
201/**
202 * Convert a string to a vlist.
203 *
204 * 'scale' is the width, in mm, of one character.
205 *
206 * @param vhead vhead
207 * @param free_hd source of free vlists
208 * @param string string of chars to be plotted
209 * @param origin lower left corner of 1st char
210 * @param rot Transform matrix (WARNING: may xlate)
211 * @param scale scale factor to change 1x1 char sz
212 *
213 */
214BV_EXPORT extern void bv_vlist_3string(struct bu_list *vhead,
215 struct bu_list *free_hd,
216 const char *string,
217 const point_t origin,
218 const mat_t rot,
219 double scale);
220
221/**
222 * Convert string to vlist in 2D
223 *
224 * A simpler interface, for those cases where the text lies in the X-Y
225 * plane.
226 *
227 * @param vhead vhead
228 * @param free_hd source of free vlists
229 * @param string string of chars to be plotted
230 * @param x lower left corner of 1st char
231 * @param y lower left corner of 1st char
232 * @param scale scale factor to change 1x1 char sz
233 * @param theta degrees ccw from X-axis
234 *
235 */
236BV_EXPORT extern void bv_vlist_2string(struct bu_list *vhead,
237 struct bu_list *free_hd,
238 const char *string,
239 double x,
240 double y,
241 double scale,
242 double theta);
243
244/**
245 * Returns the description of a vlist cmd. Caller is responsible
246 * for freeing the returned string.
247 */
248BV_EXPORT extern const char *bv_vlist_get_cmd_description(int cmd);
249
250/**
251 * Validate an bv_vlist chain for having reasonable values inside.
252 * Calls bu_bomb() if not.
253 *
254 * Returns -
255 * npts Number of point/command sets in use.
256 */
257BV_EXPORT extern size_t bv_ck_vlist(const struct bu_list *vhead);
258
259
260/**
261 * Duplicate the contents of a vlist. Note that the copy may be more
262 * densely packed than the source.
263 */
264BV_EXPORT extern void bv_vlist_copy(struct bu_list *vlists,
265 struct bu_list *dest,
266 const struct bu_list *src);
267
268
269/**
270 * Convert a vlist chain into a blob of network-independent binary,
271 * for transmission to another machine.
272 *
273 * The result is stored in a vls string, so that both the address and
274 * length are available conveniently.
275 */
276BV_EXPORT extern void bv_vlist_export(struct bu_vls *vls,
277 struct bu_list *hp,
278 const char *name);
279
280
281/**
282 * Convert a blob of network-independent binary prepared by
283 * vls_vlist() and received from another machine, into a vlist chain.
284 */
285BV_EXPORT extern void bv_vlist_import(struct bu_list *vlists,
286 struct bu_list *hp,
287 struct bu_vls *namevls,
288 const unsigned char *buf);
289
290BV_EXPORT extern void bv_vlist_cleanup(struct bu_list *hd);
291
292BV_EXPORT extern struct bv_vlblock *bv_vlblock_init(struct bu_list *free_vlist_hd, /* where to get/put free vlists */
293 int max_ent);
294
295BV_EXPORT extern void bv_vlblock_free(struct bv_vlblock *vbp);
296
297BV_EXPORT extern struct bu_list *bv_vlblock_find(struct bv_vlblock *vbp,
298 int r,
299 int g,
300 int b);
301
302
303BV_EXPORT void bv_vlist_rpp(struct bu_list *vlists, struct bu_list *hd, const point_t minn, const point_t maxx);
304
305/**
306 * Output a bv_vlblock object in extended UNIX-plot format, including
307 * color.
308 */
309BV_EXPORT extern void bv_plot_vlblock(FILE *fp,
310 const struct bv_vlblock *vbp);
311
312BV_EXPORT extern void bv_vlblock_to_objs(struct bu_ptbl *out,
313 const char *name_root,
314 struct bv_vlblock *vbp,
315 struct bview *v,
316 struct bv_scene_obj *f,
317 struct bu_list *vlfree);
318
319
320BV_EXPORT extern struct bv_scene_obj *
321bv_vlblock_obj(struct bv_vlblock *vbp, struct bview *v, const char *name);
322
323/**
324 * Output a vlist as an extended 3-D floating point UNIX-Plot file.
325 * You provide the file. Uses libplot3 routines to create the
326 * UNIX-Plot output.
327 */
328BV_EXPORT extern void bv_vlist_to_uplot(FILE *fp,
329 const struct bu_list *vhead);
330
331/** @} */
332
333__END_DECLS
334
335#endif /* BV_VLIST_H */
336
337/*
338 * Local Variables:
339 * mode: C
340 * tab-width: 8
341 * indent-tabs-mode: t
342 * c-file-style: "stroustrup"
343 * End:
344 * ex: shiftwidth=4 tabstop=8
345 */
Header file for the BRL-CAD common definitions.
void float int int float float * theta
Definition: tig.h:167
void int float float float * scale
Definition: tig.h:142
void float float * y
Definition: tig.h:73
void int char int * length
Definition: tig.h:180
void float * x
Definition: tig.h:72
void bv_vlist_export(struct bu_vls *vls, struct bu_list *hp, const char *name)
#define BV_VLIST_CHUNK
32-bit mach => just less than 1k
Definition: vlist.h:62
size_t bv_ck_vlist(const struct bu_list *vhead)
void bv_vlist_cleanup(struct bu_list *hd)
const char * bv_vlist_get_cmd_description(int cmd)
void bv_vlist_3string(struct bu_list *vhead, struct bu_list *free_hd, const char *string, const point_t origin, const mat_t rot, double scale)
void bv_vlblock_to_objs(struct bu_ptbl *out, const char *name_root, struct bv_vlblock *vbp, struct bview *v, struct bv_scene_obj *f, struct bu_list *vlfree)
size_t bv_vlist_cmd_cnt(struct bv_vlist *vlist)
void bv_plot_vlblock(FILE *fp, const struct bv_vlblock *vbp)
struct bv_scene_obj * bv_vlblock_obj(struct bv_vlblock *vbp, struct bview *v, const char *name)
int bv_vlist_bbox(struct bu_list *vlistp, point_t *bmin, point_t *bmax, size_t *length, int *dispmode)
void bv_vlist_rpp(struct bu_list *vlists, struct bu_list *hd, const point_t minn, const point_t maxx)
struct bv_vlblock * bv_vlblock_init(struct bu_list *free_vlist_hd, int max_ent)
void bv_vlist_to_uplot(FILE *fp, const struct bu_list *vhead)
void bv_vlist_import(struct bu_list *vlists, struct bu_list *hp, struct bu_vls *namevls, const unsigned char *buf)
void bv_vlblock_free(struct bv_vlblock *vbp)
struct bu_list * bv_vlblock_find(struct bv_vlblock *vbp, int r, int g, int b)
void bv_vlist_2string(struct bu_list *vhead, struct bu_list *free_hd, const char *string, double x, double y, double scale, double theta)
void bv_vlist_copy(struct bu_list *vlists, struct bu_list *dest, const struct bu_list *src)
fastf_t mat_t[ELEMENTS_PER_MAT]
4x4 matrix
Definition: vmath.h:366
fastf_t point_t[ELEMENTS_PER_POINT]
3-tuple point
Definition: vmath.h:351
Global registry of recognized magic numbers.
Definition: list.h:131
Definition: ptbl.h:53
Definition: vls.h:53
size_t max
Definition: vlist.h:193
long * rgb
rgb[max] variable size array
Definition: vlist.h:194
uint32_t magic
Definition: vlist.h:191
size_t nused
Definition: vlist.h:192
struct bu_list * free_vlist_hd
where to get/put free vlists
Definition: vlist.h:196
struct bu_list * head
head[max] variable size array
Definition: vlist.h:195
Definition: vlist.h:64
size_t nused
elements 0..nused active
Definition: vlist.h:66
struct bu_list l
magic, forw, back
Definition: vlist.h:65
int cmd[BV_VLIST_CHUNK]
VL_CMD_*.
Definition: vlist.h:67
point_t pt[BV_VLIST_CHUNK]
associated 3-point/vect
Definition: vlist.h:68
Definition: defines.h:476
fundamental vector, matrix, quaternion math macros