nurb_util.c

Go to the documentation of this file.
00001 /*                     N U R B _ U T I L . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 1994-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** @addtogroup nurb */
00023 /*@{*/
00024 /** @file nurb_util.c
00025  * Utilities for NURB curves and surfaces.
00026  *
00027  *  Author -
00028  *      Paul Randal Stay
00029  *
00030  *  Source -
00031  *      The U. S. Army Research Laboratory
00032  *      Aberdeen Proving Ground, Maryland  21005-5068  USA
00033  */
00034 /*@}*/
00035 
00036 #ifndef lint
00037 static const char RCSid[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/librt/nurb_util.c,v 14.12 2006/09/16 02:04:25 lbutler Exp $ (ARL)";
00038 #endif
00039 
00040 #include "common.h"
00041 
00042 
00043 
00044 #include <stdio.h>
00045 #include "machine.h"
00046 #include "vmath.h"
00047 #include "nmg.h"
00048 #include "raytrace.h"
00049 #include "nurb.h"
00050 
00051 /* Create a place holder for a nurb surface. */
00052 
00053 struct face_g_snurb *
00054 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)
00055 {
00056         register struct face_g_snurb * srf;
00057         int pnum;
00058 
00059         GET_SNURB(srf);
00060         srf->order[0] = u_order;
00061         srf->order[1] = v_order;
00062         srf->dir = RT_NURB_SPLIT_ROW;
00063 
00064         srf->u.k_size = n_u;
00065         srf->v.k_size = n_v;
00066         srf->s_size[0] = n_rows;
00067         srf->s_size[1] = n_cols;
00068         srf->pt_type = pt_type;
00069 
00070         pnum = sizeof (fastf_t) * n_rows * n_cols * RT_NURB_EXTRACT_COORDS(pt_type);
00071 
00072         srf->u.knots = (fastf_t *) bu_malloc (
00073                         n_u * sizeof (fastf_t ), "rt_nurb_new_snurb: u kv knot values");
00074         srf->v.knots = (fastf_t *) bu_malloc (
00075                         n_v * sizeof (fastf_t ), "rt_nurb_new_snurb: v kv knot values");
00076         srf->ctl_points = ( fastf_t *) bu_malloc(
00077                         pnum, "rt_nurb_new_snurb: control mesh points");
00078 
00079         return srf;
00080 }
00081 
00082 /* Create a place holder for a new nurb curve. */
00083 struct edge_g_cnurb *
00084 rt_nurb_new_cnurb(int order, int n_knots, int n_pts, int pt_type)
00085 {
00086         register struct edge_g_cnurb * crv;
00087 
00088         GET_CNURB(crv);
00089         crv->order = order;
00090 
00091         crv->k.k_size = n_knots;
00092         crv->k.knots = (fastf_t *)
00093                 bu_malloc(n_knots * sizeof(fastf_t),
00094                         "rt_nurb_new_cnurb: knot values");
00095 
00096         crv->c_size = n_pts;
00097         crv->pt_type = pt_type;
00098 
00099         crv->ctl_points = (fastf_t *)
00100                 bu_malloc( sizeof(fastf_t) * RT_NURB_EXTRACT_COORDS(pt_type) *
00101                         n_pts,
00102                         "rt_nurb_new_cnurb: mesh point values");
00103 
00104         return crv;
00105 }
00106 
00107 /*
00108  *                      R T _ N U R B _ C L E A N _ S N U R B
00109  *
00110  *  Clean up the storage use of an snurb, but don't release the pointer.
00111  *  Often used by routines that allocate an array of nurb pointers,
00112  *  or use automatic variables to hold one.
00113  */
00114 void
00115 rt_nurb_clean_snurb(struct face_g_snurb *srf, struct resource *res)
00116 {
00117         NMG_CK_SNURB(srf);
00118 
00119         bu_free( (char *)srf->u.knots, "rt_nurb_clean_snurb() u.knots" );
00120         bu_free( (char *)srf->v.knots, "rt_nurb_free_snurb() v.knots" );
00121         bu_free( (char *)srf->ctl_points, "rt_nurb_free_snurb() ctl_points");
00122 
00123         /* Invalidate the structure */
00124         srf->u.knots = (fastf_t *)NULL;
00125         srf->v.knots = (fastf_t *)NULL;
00126         srf->ctl_points = (fastf_t *)NULL;
00127         srf->order[0] = srf->order[1] = -1;
00128         srf->l.magic = 0;
00129 }
00130 
00131 /*
00132  *                      R T _ N U R B _ F R E E _ S N U R B
00133  */
00134 void
00135 rt_nurb_free_snurb(struct face_g_snurb *srf, struct resource *res)
00136 {
00137         NMG_CK_SNURB(srf);
00138 
00139         /* assume that links to other surface and curves are already deleted */
00140 
00141         bu_free( (char *)srf->u.knots, "rt_nurb_free_snurb: u kv knots" );
00142         bu_free( (char *)srf->v.knots, "rt_nurb_free_snurb: v kv knots" );
00143         bu_free( (char *)srf->ctl_points, "rt_nurb_free_snurb: mesh points");
00144 
00145         srf->l.magic = 0;
00146         bu_free( (char *)srf, "rt_nurb_free_snurb: snurb struct" );
00147 }
00148 
00149 
00150 /*
00151  *                      R T _ N U R B _ C L E A N _ C N U R B
00152  *
00153  *  Clean up the storage use of a cnurb, but don't release the pointer.
00154  *  Often used by routines that allocate an array of nurb pointers,
00155  *  or use automatic variables to hold one.
00156  */
00157 void
00158 rt_nurb_clean_cnurb(struct edge_g_cnurb *crv)
00159 {
00160         NMG_CK_CNURB(crv);
00161         bu_free( (char*)crv->k.knots, "rt_nurb_free_cnurb: knots");
00162         bu_free( (char*)crv->ctl_points, "rt_nurb_free_cnurb: control points");
00163         /* Invalidate the structure */
00164         crv->k.knots = (fastf_t *)NULL;
00165         crv->ctl_points = (fastf_t *)NULL;
00166         crv->c_size = 0;
00167         crv->order = -1;
00168         crv->l.magic = 0;
00169 }
00170 
00171 /*
00172  *                      R T _ N U R B _ F R E E _ C N U R B
00173  *
00174  *  Release a cnurb and all the storage that it references.
00175  */
00176 void
00177 rt_nurb_free_cnurb(struct edge_g_cnurb *crv)
00178 {
00179         NMG_CK_CNURB(crv);
00180         bu_free( (char*)crv->k.knots, "rt_nurb_free_cnurb: knots");
00181         bu_free( (char*)crv->ctl_points, "rt_nurb_free_cnurb: control points");
00182         crv->l.magic = 0;               /* sanity */
00183         bu_free( (char*)crv, "rt_nurb_free_cnurb: cnurb struct");
00184 }
00185 
00186 void
00187 rt_nurb_c_print(const struct edge_g_cnurb *crv)
00188 {
00189         register fastf_t * ptr;
00190         int i,j;
00191 
00192         NMG_CK_CNURB(crv);
00193         bu_log("curve = {\n");
00194         bu_log("\tOrder = %d\n", crv->order);
00195         bu_log("\tKnot Vector = {\n\t\t");
00196 
00197         for( i = 0; i < crv->k.k_size; i++)
00198                 bu_log("%10.8f ", crv->k.knots[i]);
00199 
00200         bu_log("\n\t}\n");
00201         bu_log("\t");
00202         rt_nurb_print_pt_type(crv->pt_type);
00203         bu_log("\tmesh = {\n");
00204         for( ptr = &crv->ctl_points[0], i= 0;
00205                 i < crv->c_size; i++, ptr += RT_NURB_EXTRACT_COORDS(crv->pt_type))
00206         {
00207                 bu_log("\t\t");
00208                 for(j = 0; j < RT_NURB_EXTRACT_COORDS(crv->pt_type); j++)
00209                         bu_log("%4.5f\t", ptr[j]);
00210                 bu_log("\n");
00211 
00212         }
00213         bu_log("\t}\n}\n");
00214 
00215 
00216 }
00217 
00218 void
00219 rt_nurb_s_print(char *c, const struct face_g_snurb *srf)
00220 {
00221 
00222     bu_log("%s\n", c );
00223 
00224     bu_log("order %d %d\n", srf->order[0], srf->order[1] );
00225 
00226     bu_log( "u knot vector \n");
00227 
00228     rt_nurb_pr_kv( &srf->u );
00229 
00230     bu_log( "v knot vector \n");
00231 
00232     rt_nurb_pr_kv( &srf->v );
00233 
00234     rt_nurb_pr_mesh( srf );
00235 
00236 }
00237 
00238 void
00239 rt_nurb_pr_kv(const struct knot_vector *kv)
00240 {
00241     register fastf_t * ptr = kv->knots;
00242     int i;
00243 
00244     bu_log("[%d]\t", kv->k_size );
00245 
00246 
00247     for( i = 0; i < kv->k_size; i++)
00248     {
00249         bu_log("%2.5f  ", *ptr++);
00250     }
00251     bu_log("\n");
00252 }
00253 
00254 void
00255 rt_nurb_pr_mesh(const struct face_g_snurb *m)
00256 {
00257         int i,j,k;
00258         fastf_t * m_ptr = m->ctl_points;
00259         int evp = RT_NURB_EXTRACT_COORDS(m->pt_type);
00260 
00261         NMG_CK_SNURB(m);
00262 
00263         bu_log("\t[%d] [%d]\n", m->s_size[0], m->s_size[1] );
00264 
00265         for( i = 0; i < m->s_size[0]; i++)
00266         {
00267                 for( j =0; j < m->s_size[1]; j++)
00268                 {
00269                         bu_log("\t");
00270 
00271                         for(k = 0; k < evp; k++)
00272                                 bu_log("%f    ", m_ptr[k]);
00273 
00274                         bu_log("\n");
00275                         m_ptr += RT_NURB_EXTRACT_COORDS(m->pt_type);
00276                 }
00277                 bu_log("\n");
00278         }
00279 }
00280 
00281 void
00282 rt_nurb_print_pt_type(int c)
00283 {
00284         int rat;
00285 
00286         rat = RT_NURB_IS_PT_RATIONAL(c);
00287 
00288         if( RT_NURB_EXTRACT_PT_TYPE(c) == RT_NURB_PT_XY)
00289                 bu_log("Point Type = RT_NURB_PT_XY");
00290         else
00291         if( RT_NURB_EXTRACT_PT_TYPE(c) == RT_NURB_PT_XYZ)
00292                 bu_log("Point Type = RT_NURB_PT_XYX");
00293         else
00294         if( RT_NURB_EXTRACT_PT_TYPE(c) == RT_NURB_PT_UV)
00295                 bu_log("Point Type = RT_NURB_PT_UV");
00296 
00297         if( rat )
00298                 bu_log("W\n");
00299         else
00300                 bu_log("\n");
00301 }
00302 
00303 /*
00304  * Local Variables:
00305  * mode: C
00306  * tab-width: 8
00307  * c-basic-offset: 4
00308  * indent-tabs-mode: t
00309  * End:
00310  * ex: shiftwidth=4 tabstop=8
00311  */

Generated on Mon Sep 18 01:24:56 2006 for BRL-CAD by  doxygen 1.4.6