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-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 #ifndef BN_VLIST_H
22 #define BN_VLIST_H
23 
24 #include "common.h"
25 #include "vmath.h"
26 #include "bu/magic.h"
27 #include "bu/list.h"
28 #include "bn/defines.h"
29 
31 
32 /*----------------------------------------------------------------------*/
33 /* @file vlist.h */
34 /** @addtogroup vlist */
35 /** @{ */
36 
37 /**
38  * @brief
39  * Definitions for handling lists of vectors (really vertices, or
40  * points) and polygons in 3-space. Intended for common handling of
41  * wireframe display information, in the full resolution that is
42  * calculated in.
43  */
44 
45 #define BN_VLIST_CHUNK 35 /**< @brief 32-bit mach => just less than 1k */
46 
47 /**
48  * Definitions for handling lists of vectors (really vertices, or
49  * points) and polygons in 3-space. Intended for common handling of
50  * wireframe display information, in the full resolution that is
51  * calculated in.
52  *
53  * On 32-bit machines, BN_VLIST_CHUNK of 35 results in bn_vlist
54  * structures just less than 1k bytes.
55  *
56  * The head of the doubly linked list can be just a "struct bu_list"
57  * head.
58  *
59  * To visit all the elements in the vlist:
60  * for (BU_LIST_FOR(vp, bn_vlist, hp)) {
61  * register int i;
62  * register int nused = vp->nused;
63  * register int *cmd = vp->cmd;
64  * register point_t *pt = vp->pt;
65  * for (i = 0; i < nused; i++, cmd++, pt++) {
66  * access(*cmd, *pt);
67  * access(vp->cmd[i], vp->pt[i]);
68  * }
69  * }
70  */
71 struct bn_vlist {
72  struct bu_list l; /**< @brief magic, forw, back */
73  size_t nused; /**< @brief elements 0..nused active */
74  int cmd[BN_VLIST_CHUNK]; /**< @brief VL_CMD_* */
75  point_t pt[BN_VLIST_CHUNK]; /**< @brief associated 3-point/vect */
76 };
77 #define BN_VLIST_NULL ((struct bn_vlist *)0)
78 #define BN_CK_VLIST(_p) BU_CKMAG((_p), BN_VLIST_MAGIC, "bn_vlist")
79 
80 /* should these be enum? -Erik */
81 /* Values for cmd[] */
82 #define BN_VLIST_LINE_MOVE 0
83 #define BN_VLIST_LINE_DRAW 1
84 #define BN_VLIST_POLY_START 2 /**< @brief pt[] has surface normal */
85 #define BN_VLIST_POLY_MOVE 3 /**< @brief move to first poly vertex */
86 #define BN_VLIST_POLY_DRAW 4 /**< @brief subsequent poly vertex */
87 #define BN_VLIST_POLY_END 5 /**< @brief last vert (repeats 1st), draw poly */
88 #define BN_VLIST_POLY_VERTNORM 6 /**< @brief per-vertex normal, for interpolation */
89 #define BN_VLIST_TRI_START 7 /**< @brief pt[] has surface normal */
90 #define BN_VLIST_TRI_MOVE 8 /**< @brief move to first triangle vertex */
91 #define BN_VLIST_TRI_DRAW 9 /**< @brief subsequent triangle vertex */
92 #define BN_VLIST_TRI_END 10 /**< @brief last vert (repeats 1st), draw poly */
93 #define BN_VLIST_TRI_VERTNORM 11 /**< @brief per-vertex normal, for interpolation */
94 #define BN_VLIST_POINT_DRAW 12 /**< @brief Draw a single point */
95 #define BN_VLIST_POINT_SIZE 13 /**< @brief specify point pixel size */
96 #define BN_VLIST_LINE_WIDTH 14 /**< @brief specify line pixel width */
97 #define BN_VLIST_CMD_MAX 14 /**< @brief Max command number */
98 
99 /**
100  * Applications that are going to use BN_ADD_VLIST and BN_GET_VLIST
101  * are required to execute this macro once, on their _free_hd:
102  * BU_LIST_INIT(&_free_hd);
103  *
104  * Note that BN_GET_VLIST and BN_FREE_VLIST are non-PARALLEL.
105  */
106 #define BN_GET_VLIST(_free_hd, p) {\
107  (p) = BU_LIST_FIRST(bn_vlist, (_free_hd)); \
108  if (BU_LIST_IS_HEAD((p), (_free_hd))) { \
109  BU_ALLOC((p), struct bn_vlist); \
110  (p)->l.magic = BN_VLIST_MAGIC; \
111  } else { \
112  BU_LIST_DEQUEUE(&((p)->l)); \
113  } \
114  (p)->nused = 0; \
115  }
116 
117 /** Place an entire chain of bn_vlist structs on the freelist _free_hd */
118 #define BN_FREE_VLIST(_free_hd, hd) { \
119  BU_CK_LIST_HEAD((hd)); \
120  BU_LIST_APPEND_LIST((_free_hd), (hd)); \
121  }
122 
123 #define BN_ADD_VLIST(_free_hd, _dest_hd, pnt, draw) { \
124  register struct bn_vlist *_vp; \
125  BU_CK_LIST_HEAD(_dest_hd); \
126  _vp = BU_LIST_LAST(bn_vlist, (_dest_hd)); \
127  if (BU_LIST_IS_HEAD(_vp, (_dest_hd)) || _vp->nused >= BN_VLIST_CHUNK) { \
128  BN_GET_VLIST(_free_hd, _vp); \
129  BU_LIST_INSERT((_dest_hd), &(_vp->l)); \
130  } \
131  VMOVE(_vp->pt[_vp->nused], (pnt)); \
132  _vp->cmd[_vp->nused++] = (draw); \
133  }
134 
135 /** Set a point size to apply to the vlist elements that follow. */
136 #define BN_VLIST_SET_POINT_SIZE(_free_hd, _dest_hd, _size) { \
137  struct bn_vlist *_vp; \
138  BU_CK_LIST_HEAD(_dest_hd); \
139  _vp = BU_LIST_LAST(bn_vlist, (_dest_hd)); \
140  if (BU_LIST_IS_HEAD(_vp, (_dest_hd)) || _vp->nused >= BN_VLIST_CHUNK) { \
141  BN_GET_VLIST(_free_hd, _vp); \
142  BU_LIST_INSERT((_dest_hd), &(_vp->l)); \
143  } \
144  _vp->pt[_vp->nused][0] = (_size); \
145  _vp->cmd[_vp->nused++] = BN_VLIST_POINT_SIZE; \
146 }
147 
148 /** Set a line width to apply to the vlist elements that follow. */
149 #define BN_VLIST_SET_LINE_WIDTH(_free_hd, _dest_hd, _width) { \
150  struct bn_vlist *_vp; \
151  BU_CK_LIST_HEAD(_dest_hd); \
152  _vp = BU_LIST_LAST(bn_vlist, (_dest_hd)); \
153  if (BU_LIST_IS_HEAD(_vp, (_dest_hd)) || _vp->nused >= BN_VLIST_CHUNK) { \
154  BN_GET_VLIST(_free_hd, _vp); \
155  BU_LIST_INSERT((_dest_hd), &(_vp->l)); \
156  } \
157  _vp->pt[_vp->nused][0] = (_width); \
158  _vp->cmd[_vp->nused++] = BN_VLIST_LINE_WIDTH; \
159 }
160 
161 
162 BN_EXPORT extern int bn_vlist_cmd_cnt(struct bn_vlist *vlist);
163 BN_EXPORT extern int bn_vlist_bbox(struct bn_vlist *vp, point_t *bmin, point_t *bmax);
164 
165 /**
166  * For plotting, a way of separating plots into separate color vlists:
167  * blocks of vlists, each with an associated color.
168  */
169 struct bn_vlblock {
170  uint32_t magic;
171  size_t nused;
172  size_t max;
173  long *rgb; /**< @brief rgb[max] variable size array */
174  struct bu_list *head; /**< @brief head[max] variable size array */
175  struct bu_list *free_vlist_hd; /**< @brief where to get/put free vlists */
176 };
177 #define BN_CK_VLBLOCK(_p) BU_CKMAG((_p), BN_VLBLOCK_MAGIC, "bn_vlblock")
178 
179 
180 /** @file libbn/font.c
181  *
182  */
183 
184 /**
185  * Convert a string to a vlist.
186  *
187  * 'scale' is the width, in mm, of one character.
188  *
189  * @param vhead
190  * @param free_hd source of free vlists
191  * @param string string of chars to be plotted
192  * @param origin lower left corner of 1st char
193  * @param rot Transform matrix (WARNING: may xlate)
194  * @param scale scale factor to change 1x1 char sz
195  *
196  */
197 BN_EXPORT extern void bn_vlist_3string(struct bu_list *vhead,
198  struct bu_list *free_hd,
199  const char *string,
200  const point_t origin,
201  const mat_t rot,
202  double scale);
203 
204 /**
205  * Convert string to vlist in 2D
206  *
207  * A simpler interface, for those cases where the text lies in the X-Y
208  * plane.
209  *
210  * @param vhead
211  * @param free_hd source of free vlists
212  * @param string string of chars to be plotted
213  * @param x lower left corner of 1st char
214  * @param y lower left corner of 1st char
215  * @param scale scale factor to change 1x1 char sz
216  * @param theta degrees ccw from X-axis
217  *
218  */
219 BN_EXPORT extern void bn_vlist_2string(struct bu_list *vhead,
220  struct bu_list *free_hd,
221  const char *string,
222  double x,
223  double y,
224  double scale,
225  double theta);
226 
227 /** @} */
228 
230 
231 #endif /* BN_VLIST_H */
232 /** @} */
233 /*
234  * Local Variables:
235  * mode: C
236  * tab-width: 8
237  * indent-tabs-mode: t
238  * c-file-style: "stroustrup"
239  * End:
240  * ex: shiftwidth=4 tabstop=8
241  */
size_t nused
elements 0..nused active
Definition: vlist.h:73
struct bu_list l
magic, forw, back
Definition: vlist.h:72
Definition: list.h:118
int cmd[BN_VLIST_CHUNK]
VL_CMD_*.
Definition: vlist.h:74
int bn_vlist_cmd_cnt(struct bn_vlist *vlist)
Definition: vlist.c:34
size_t max
Definition: vlist.h:172
Header file for the BRL-CAD common definitions.
long * rgb
rgb[max] variable size array
Definition: vlist.h:173
struct bu_list * head
head[max] variable size array
Definition: vlist.h:174
uint32_t magic
Definition: vlist.h:170
#define __BEGIN_DECLS
Definition: common.h:73
void bn_vlist_2string(struct bu_list *vhead, struct bu_list *free_hd, const char *string, double x, double y, double scale, double theta)
Definition: font.c:111
void bn_vlist_3string(struct bu_list *vhead, struct bu_list *free_hd, const char *string, const point_t origin, const mat_t rot, double scale)
Definition: vlist.h:71
#define __END_DECLS
Definition: common.h:74
struct bu_list * free_vlist_hd
where to get/put free vlists
Definition: vlist.h:175
#define BN_VLIST_CHUNK
Definitions for handling lists of vectors (really vertices, or points) and polygons in 3-space...
Definition: vlist.h:45
point_t pt[BN_VLIST_CHUNK]
associated 3-point/vect
Definition: vlist.h:75
size_t nused
Definition: vlist.h:171
int bn_vlist_bbox(struct bn_vlist *vp, point_t *bmin, point_t *bmax)
Definition: vlist.c:52