BRL-CAD
nurb_util.c
Go to the documentation of this file.
1 /* N U R B _ U T I L . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1994-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 nurb */
21 /** @{ */
22 /** @file primitives/bspline/nurb_util.c
23  *
24  * Utilities for NURB curves and surfaces.
25  *
26  */
27 /** @} */
28 
29 #include "common.h"
30 
31 #include "bio.h"
32 
33 #include "vmath.h"
34 #include "nmg.h"
35 #include "raytrace.h"
36 #include "nurb.h"
37 
38 /**
39  * Create a place holder for a nurb surface.
40  */
41 struct face_g_snurb *
42 rt_nurb_new_snurb(int u_order, int v_order, int n_u, int n_v, int n_rows, int n_cols, int pt_type, struct resource *res)
43 {
44  register struct face_g_snurb * srf;
45  int pnum;
46 
47  if (res) RT_CK_RESOURCE(res);
48 
49  GET_SNURB(srf);
50  srf->order[0] = u_order;
51  srf->order[1] = v_order;
52  srf->dir = RT_NURB_SPLIT_ROW;
53 
54  srf->u.k_size = n_u;
55  srf->v.k_size = n_v;
56  srf->s_size[0] = n_rows;
57  srf->s_size[1] = n_cols;
58  srf->pt_type = pt_type;
59 
60  pnum = sizeof (fastf_t) * n_rows * n_cols * RT_NURB_EXTRACT_COORDS(pt_type);
61 
62  srf->u.knots = (fastf_t *) bu_malloc (
63  n_u * sizeof (fastf_t), "rt_nurb_new_snurb: u kv knot values");
64  srf->v.knots = (fastf_t *) bu_malloc (
65  n_v * sizeof (fastf_t), "rt_nurb_new_snurb: v kv knot values");
66  srf->ctl_points = (fastf_t *) bu_malloc(
67  pnum, "rt_nurb_new_snurb: control mesh points");
68 
69  return srf;
70 }
71 
72 
73 /**
74  * Create a place holder for a new nurb curve.
75  */
76 struct edge_g_cnurb *
77 rt_nurb_new_cnurb(int order, int n_knots, int n_pts, int pt_type)
78 {
79  register struct edge_g_cnurb * crv;
80 
81  GET_CNURB(crv);
82  crv->order = order;
83 
84  crv->k.k_size = n_knots;
85  crv->k.knots = (fastf_t *)
86  bu_malloc(n_knots * sizeof(fastf_t),
87  "rt_nurb_new_cnurb: knot values");
88 
89  crv->c_size = n_pts;
90  crv->pt_type = pt_type;
91 
92  crv->ctl_points = (fastf_t *)
93  bu_malloc(sizeof(fastf_t) * RT_NURB_EXTRACT_COORDS(pt_type) *
94  n_pts,
95  "rt_nurb_new_cnurb: mesh point values");
96 
97  return crv;
98 }
99 
100 
101 /**
102  * Clean up the storage use of an snurb, but don't release the
103  * pointer. Often used by routines that allocate an array of nurb
104  * pointers, or use automatic variables to hold one.
105  */
106 void
107 rt_nurb_clean_snurb(struct face_g_snurb *srf, struct resource *res)
108 {
109  NMG_CK_SNURB(srf);
110 
111  if (res) RT_CK_RESOURCE(res);
112 
113  bu_free((char *)srf->u.knots, "rt_nurb_clean_snurb() u.knots");
114  bu_free((char *)srf->v.knots, "rt_nurb_free_snurb() v.knots");
115  bu_free((char *)srf->ctl_points, "rt_nurb_free_snurb() ctl_points");
116 
117  /* Invalidate the structure */
118  srf->u.knots = (fastf_t *)NULL;
119  srf->v.knots = (fastf_t *)NULL;
120  srf->ctl_points = (fastf_t *)NULL;
121  srf->order[0] = srf->order[1] = -1;
122  srf->l.magic = 0;
123 }
124 
125 
126 void
127 rt_nurb_free_snurb(struct face_g_snurb *srf, struct resource *res)
128 {
129  NMG_CK_SNURB(srf);
130 
131  if (res) RT_CK_RESOURCE(res);
132 
133  /* assume that links to other surface and curves are already
134  * deleted.
135  */
136 
137  bu_free((char *)srf->u.knots, "rt_nurb_free_snurb: u kv knots");
138  bu_free((char *)srf->v.knots, "rt_nurb_free_snurb: v kv knots");
139  bu_free((char *)srf->ctl_points, "rt_nurb_free_snurb: mesh points");
140 
141  srf->l.magic = 0;
142  bu_free((char *)srf, "rt_nurb_free_snurb: snurb struct");
143 }
144 
145 
146 /**
147  * Clean up the storage use of a cnurb, but don't release the pointer.
148  * Often used by routines that allocate an array of nurb pointers, or
149  * use automatic variables to hold one.
150  */
151 void
152 rt_nurb_clean_cnurb(struct edge_g_cnurb *crv)
153 {
154  NMG_CK_CNURB(crv);
155  bu_free((char*)crv->k.knots, "rt_nurb_free_cnurb: knots");
156  bu_free((char*)crv->ctl_points, "rt_nurb_free_cnurb: control points");
157  /* Invalidate the structure */
158  crv->k.knots = (fastf_t *)NULL;
159  crv->ctl_points = (fastf_t *)NULL;
160  crv->c_size = 0;
161  crv->order = -1;
162  crv->l.magic = 0;
163 }
164 
165 
166 /**
167  * Release a cnurb and all the storage that it references.
168  */
169 void
170 rt_nurb_free_cnurb(struct edge_g_cnurb *crv)
171 {
172  NMG_CK_CNURB(crv);
173  bu_free((char*)crv->k.knots, "rt_nurb_free_cnurb: knots");
174  bu_free((char*)crv->ctl_points, "rt_nurb_free_cnurb: control points");
175  crv->l.magic = 0; /* sanity */
176  bu_free((char*)crv, "rt_nurb_free_cnurb: cnurb struct");
177 }
178 
179 
180 void
181 rt_nurb_c_print(const struct edge_g_cnurb *crv)
182 {
183  register fastf_t * ptr;
184  int i, j;
185 
186  NMG_CK_CNURB(crv);
187  bu_log("curve = {\n");
188  bu_log("\tOrder = %d\n", crv->order);
189  bu_log("\tKnot Vector = {\n\t\t");
190 
191  for (i = 0; i < crv->k.k_size; i++)
192  bu_log("%10.8f ", crv->k.knots[i]);
193 
194  bu_log("\n\t}\n");
195  bu_log("\t");
196  rt_nurb_print_pt_type(crv->pt_type);
197  bu_log("\tmesh = {\n");
198  for (ptr = &crv->ctl_points[0], i= 0;
199  i < crv->c_size; i++, ptr += RT_NURB_EXTRACT_COORDS(crv->pt_type))
200  {
201  bu_log("\t\t");
202  for (j = 0; j < RT_NURB_EXTRACT_COORDS(crv->pt_type); j++)
203  bu_log("%4.5f\t", ptr[j]);
204  bu_log("\n");
205 
206  }
207  bu_log("\t}\n}\n");
208 
209 
210 }
211 
212 
213 void
214 rt_nurb_s_print(char *c, const struct face_g_snurb *srf)
215 {
216 
217  bu_log("%s\n", c);
218 
219  bu_log("order %d %d\n", srf->order[0], srf->order[1]);
220 
221  bu_log("u knot vector \n");
222 
223  rt_nurb_pr_kv(&srf->u);
224 
225  bu_log("v knot vector \n");
226 
227  rt_nurb_pr_kv(&srf->v);
228 
229  rt_nurb_pr_mesh(srf);
230 
231 }
232 
233 
234 void
235 rt_nurb_pr_kv(const struct knot_vector *kv)
236 {
237  register fastf_t * ptr = kv->knots;
238  int i;
239 
240  bu_log("[%d]\t", kv->k_size);
241 
242 
243  for (i = 0; i < kv->k_size; i++) {
244  bu_log("%2.5f ", *ptr++);
245  }
246  bu_log("\n");
247 }
248 
249 
250 void
251 rt_nurb_pr_mesh(const struct face_g_snurb *m)
252 {
253  int i, j, k;
254  fastf_t * m_ptr = m->ctl_points;
255  int evp = RT_NURB_EXTRACT_COORDS(m->pt_type);
256 
257  NMG_CK_SNURB(m);
258 
259  bu_log("\t[%d] [%d]\n", m->s_size[0], m->s_size[1]);
260 
261  for (i = 0; i < m->s_size[0]; i++) {
262  for (j =0; j < m->s_size[1]; j++) {
263  bu_log("\t");
264 
265  for (k = 0; k < evp; k++)
266  bu_log("%f ", m_ptr[k]);
267 
268  bu_log("\n");
269  m_ptr += RT_NURB_EXTRACT_COORDS(m->pt_type);
270  }
271  bu_log("\n");
272  }
273 }
274 
275 
276 void
278 {
279  int rat;
280 
281  rat = RT_NURB_IS_PT_RATIONAL(c);
282 
283  if (RT_NURB_EXTRACT_PT_TYPE(c) == RT_NURB_PT_XY)
284  bu_log("Point Type = RT_NURB_PT_XY");
285  else
286  if (RT_NURB_EXTRACT_PT_TYPE(c) == RT_NURB_PT_XYZ)
287  bu_log("Point Type = RT_NURB_PT_XYX");
288  else
289  if (RT_NURB_EXTRACT_PT_TYPE(c) == RT_NURB_PT_UV)
290  bu_log("Point Type = RT_NURB_PT_UV");
291 
292  if (rat)
293  bu_log("W\n");
294  else
295  bu_log("\n");
296 }
297 
298 
299 /*
300  * Local Variables:
301  * mode: C
302  * tab-width: 8
303  * indent-tabs-mode: t
304  * c-file-style: "stroustrup"
305  * End:
306  * ex: shiftwidth=4 tabstop=8
307  */
struct edge_g_cnurb * rt_nurb_new_cnurb(int order, int n_knots, int n_pts, int pt_type)
Definition: nurb_util.c:77
void rt_nurb_free_snurb(struct face_g_snurb *srf, struct resource *res)
Definition: nurb_util.c:127
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
void rt_nurb_clean_snurb(struct face_g_snurb *srf, struct resource *res)
Definition: nurb_util.c:107
struct face_g_snurb * rt_nurb_new_snurb(int u_order, int v_order, int n_u, int n_v, int n_rows, int n_cols, int pt_type, struct resource *res)
Definition: nurb_util.c:42
Header file for the BRL-CAD common definitions.
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
void rt_nurb_print_pt_type(int c)
Definition: nurb_util.c:277
void rt_nurb_s_print(char *c, const struct face_g_snurb *srf)
Definition: nurb_util.c:214
void rt_nurb_pr_mesh(const struct face_g_snurb *m)
Definition: nurb_util.c:251
#define RT_CK_RESOURCE(_p)
Definition: raytrace.h:1490
void rt_nurb_pr_kv(const struct knot_vector *kv)
Definition: nurb_util.c:235
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
void rt_nurb_free_cnurb(struct edge_g_cnurb *crv)
Definition: nurb_util.c:170
void rt_nurb_c_print(const struct edge_g_cnurb *crv)
Definition: nurb_util.c:181
void rt_nurb_clean_cnurb(struct edge_g_cnurb *crv)
Definition: nurb_util.c:152
double fastf_t
Definition: defines.h:300