nurb_poly.c

Go to the documentation of this file.
00001 /*                     N U R B _ P O L Y . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 1990-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_poly.c
00025  *     Returns two polygons from a NURB surface.
00026  *     Asumes that the surface is flat.
00027  *
00028  * Author -
00029  *     Paul R. Stay
00030  *
00031  * Source -
00032  *     SECAD/VLD Computing Consortium, Bldg 394
00033  *     The U.S. Army Ballistic Research Laboratory
00034  *     Aberdeen Proving Ground, Maryland 21005
00035  *
00036  */
00037 /*@}*/
00038 
00039 #include "common.h"
00040 
00041 
00042 
00043 #include <stdio.h>
00044 
00045 #include "machine.h"
00046 #include "vmath.h"
00047 #include "raytrace.h"
00048 #include "nurb.h"
00049 
00050 /* Algorithm -
00051  * From the four corners of the surface, return the two parts split by the
00052  * diagonal from the first and third corner point making sure Homogeneous
00053  * points are divided.
00054  */
00055 
00056 struct rt_nurb_poly *
00057 rt_nurb_to_poly(struct face_g_snurb *srf)
00058 {
00059         int     coords = srf->pt_type;
00060         fastf_t          * p1, *p2, *p3, *p4;
00061         fastf_t         uv1[2], uv2[2], uv3[2], uv4[2];
00062         struct rt_nurb_poly *p, *p_head;
00063 
00064         /* Extract the four corners from the mesh */
00065 
00066         p1 = srf->ctl_points;
00067         p2 = srf->ctl_points + coords * srf->s_size[1];
00068         p3 = srf->ctl_points + (coords * srf->s_size[1] *
00069             (srf->s_size[0] - 1)) +
00070             ((srf->s_size[1] - 1) * coords);
00071         p4 = srf->ctl_points + (coords * srf->s_size[1] *
00072             (srf->s_size[0] - 1));
00073 
00074         /* If the point is rational then divide out the w component */
00075         if ( RT_NURB_IS_PT_RATIONAL(srf->pt_type)) {
00076                 int     w_index;
00077 
00078                 if ( RT_NURB_EXTRACT_PT_TYPE( srf->pt_type) == RT_NURB_PT_XY)
00079                         w_index = 2;
00080                 else if ( RT_NURB_EXTRACT_PT_TYPE( srf->pt_type) == RT_NURB_PT_UV)
00081                         w_index = 2;
00082                 else if ( RT_NURB_EXTRACT_PT_TYPE( srf->pt_type) == RT_NURB_PT_XYZ)
00083                         w_index = 3;
00084                 else /* assume the forth coordinate */
00085                         w_index = 3;
00086 
00087                 p1[0] = p1[0] / p1[w_index];
00088                 p2[0] = p2[0] / p1[w_index];
00089                 p3[0] = p3[0] / p1[w_index];
00090                 p4[0] = p4[0] / p1[w_index];
00091         }
00092 
00093         uv1[0] = srf->u.knots[0];
00094         uv1[1] = srf->v.knots[0];
00095 
00096         uv2[0] = srf->u.knots[srf->u.k_size -1];
00097         uv2[1] = srf->v.knots[0];
00098 
00099         uv3[0] = srf->u.knots[srf->u.k_size -1];
00100         uv3[1] = srf->v.knots[srf->v.k_size -1];
00101 
00102         uv4[0] = srf->u.knots[0];
00103         uv4[1] = srf->v.knots[srf->v.k_size -1];
00104 
00105         p = rt_nurb_mk_poly(p1, p2, p3, uv1, uv2, uv3 );
00106         p_head = p;
00107         p = rt_nurb_mk_poly(p3, p4, p1, uv3, uv4, uv1 );
00108         p->next = p_head;
00109         p_head = p;
00110 
00111         return p_head;
00112 }
00113 
00114 
00115 struct rt_nurb_poly *
00116 rt_nurb_mk_poly(fastf_t *v1, fastf_t *v2, fastf_t *v3, fastf_t *uv1, fastf_t *uv2, fastf_t *uv3)
00117 {
00118         struct rt_nurb_poly *p;
00119 
00120         p = (struct rt_nurb_poly *) bu_malloc( sizeof( struct rt_nurb_poly ),
00121             "rt_nurb_mk_poly: rt_nurb_poly struct" );
00122 
00123         p->next = (struct rt_nurb_poly *) 0;
00124 
00125         VMOVE( p->ply[0], v1);
00126         VMOVE( p->ply[1], v2);
00127         VMOVE( p->ply[2], v3);
00128 
00129         p->uv[0][0] = uv1[0];
00130         p->uv[0][1] = uv1[1];
00131         p->uv[1][0] = uv2[0];
00132         p->uv[1][1] = uv2[1];
00133         p->uv[2][0] = uv3[0];
00134         p->uv[2][1] = uv3[1];
00135 
00136         return p;
00137 }
00138 
00139 /*
00140  * Local Variables:
00141  * mode: C
00142  * tab-width: 8
00143  * c-basic-offset: 4
00144  * indent-tabs-mode: t
00145  * End:
00146  * ex: shiftwidth=4 tabstop=8
00147  */

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