Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

cgul.cpp

Go to the documentation of this file.
00001 /* LIBGUL - Geometry Utility Library
00002  * Copyright (C) 1998-1999 Norbert Irmer
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  */
00019 
00020 #include "stdafx.h"
00021 
00022 #include <iostream>
00023 
00024 #include "gul_types.h"
00025 #include "gunu_mba_approximate.h"
00026 #include "guma_transform.h"
00027 
00028 #if defined(__MINGW32__) || defined(_MSC_VER)
00029   #define GULDECL __declspec(dllexport)
00030   #define GULCALL __cdecl
00031   #define CCALL   __cdecl 
00032 #else
00033   #define GULDECL
00034   #define GULCALL
00035   #define CCALL
00036 #endif
00037 
00038 using gul::Ptr;
00039 using gul::point;
00040 using gul::point2;
00041 
00042 extern "C" {
00043 
00044 /*------------------------------------------------------------------
00045    basic data types, used for import/export
00046 -------------------------------------------------------------------*/
00047 
00048 typedef struct
00049 {
00050   double x,y,z;
00051 }
00052 GULpoint;
00053 
00054 typedef struct
00055 {
00056   double x,y;
00057 }
00058 GULpoint2;
00059 
00060 }
00061 
00062 /*-----------------------------------------------------------------------
00063   convenience functions for copying input/output vectors and matrices
00064 -----------------------------------------------------------------------*/
00065 
00066 inline void set( double *out, const double& in )
00067 {
00068   *out = in;
00069 } 
00070 
00071 inline void set( point<double> *out, const GULpoint& in )
00072 {
00073   out->x = in.x;
00074   out->y = in.y;
00075   out->z = in.z;
00076 }  
00077 
00078 inline void set( point2<double> *out, const GULpoint2& in )
00079 {
00080   out->x = in.x;
00081   out->y = in.y;
00082 }
00083 
00084 inline void set( GULpoint *out, const point<double>& in  )
00085 {
00086   out->x = in.x;
00087   out->y = in.y;
00088   out->z = in.z;
00089 }  
00090 
00091 inline void set( GULpoint2 *out, const point2<double>& in )
00092 {
00093   out->x = in.x;
00094   out->y = in.y;
00095 }  
00096 
00097 /*----------------------------------------------------------------*/
00098 
00099 template< class T1, class T2 >
00100 void copy( int n, const T1 *inP, Ptr<T2>& outP )
00101 {
00102   int i;
00103   outP.reserve_pool(n);
00104   for( i = 0; i < n; i++ )
00105     set( &outP[i], inP[i] );
00106 }
00107 
00108 template< class T1, class T2 >
00109 void copy( int n, const Ptr<T1>& inP, T2 **outP )
00110 {
00111   int i;
00112   *outP = (T2 *)galloc( sizeof(T2) * n );
00113   for( i = 0; i < n; i++ )
00114     set( &(*outP)[i], inP[i] );
00115 }
00116 
00117 /*----------------------------------------------------------------*/
00118 
00119 template< class T1, class T2 >
00120 void copy( int m, int n, const T1 **inP, Ptr< Ptr<T2> >& outP )
00121 {
00122   int i;
00123   outP.reserve_pool(m);
00124   for( i = 0; i < m; i++ )
00125   {
00126     outP[i].reserve_pool(n);
00127     for( j = 0; j < n; j++ )
00128       set( &outP[i][j], inP[i][j] );
00129   }
00130 }
00131 
00132 template< class T1, class T2 >
00133 void copy( int m, int n, const Ptr< Ptr<T1> >& inP, T2 ***outP )
00134 {
00135   int i,j;
00136   *outP = (T2**)galloc( sizeof(T2*) * m );
00137   for( i = 0; i < m; i++ )
00138   {
00139     (*outP)[i] = (T2*)galloc( sizeof(T2) * n );
00140     for( j = 0; j < n; j++ )
00141       set( &(*outP)[i][j], inP[i][j] );
00142   }
00143 }
00144 
00145 /*---------------------------------------------------------------
00146   interface functions for plain C
00147 ---------------------------------------------------------------*/
00148 
00149 extern "C" {
00150 
00151 /*---------------------------------------------------------------------
00152   with this function you can change the functions which are used for
00153   malloc/free of return arrays (for example necessary when using 'dbg_malloc'
00154   in the application, since in the dll the normal malloc is used)
00155 ----------------------------------------------------------------------*/
00156 
00157 static void *(CCALL *GUL_malloc_func)( size_t size ) = 0;
00158 static void (CCALL *GUL_free_func)( void * ) = 0;
00159 
00160 GULDECL void GULCALL GUL_set_malloc_func( void *(CCALL *func)(size_t) )
00161 {
00162   GUL_malloc_func = func;
00163 }
00164 
00165 GULDECL void GULCALL GUL_set_free_func( void (CCALL *func)(void *) )
00166 {
00167   GUL_free_func = func;
00168 }
00169 
00170 void *galloc( size_t size )
00171 {
00172   if( GUL_malloc_func )
00173     return GUL_malloc_func(size);
00174 
00175   return malloc(size);
00176 }
00177 
00178 void gree( void *p )
00179 {
00180   if( GUL_free_func )
00181   {
00182     GUL_free_func(p);
00183     return;
00184   }
00185   free(p);
00186 }
00187 
00188 /*-------------------------------------------------------------------*//**
00189   executes the MBA algorithm and constructs a 3-dimensional surface
00190   from the results (x,y are linear mappings of u and v ).
00191   when 'minimize' is set, the base rectangle in the xy-plane is
00192   chosen so that its area is minimal                                    
00193 
00194   input arrays:
00195 
00196   datPoints  : array of data-points
00197   StdDevs    : standard deviations of the data points (if useStdDevs = 0,
00198                all assumed as beeing one)
00199 
00200   output arrays are reserved by the function and have the dimensions:
00201 
00202   retU     : nu + pu + 2
00203   retV     : nv + pv + 2
00204   retPw    : nv + 1 
00205   retPw[i] : nu + 1
00206                                                                         */
00207 /* ---------------------------------------------------------------------*/
00208 
00209 GULDECL void GULCALL GUL_SurfaceOverXYPlane(
00210           int nDatPoints, GULpoint *datPoints,
00211           int useStdDevs, double *StdDevs,
00212           int minimize, int nIter,
00213           int pu, int pv,
00214           int *ret_nu, double **retU,
00215           int *ret_nv, double **retV,
00216           GULpoint ***retPw )
00217 {
00218   Ptr< point<double> > datP;
00219   Ptr<double> stdD, U, V;
00220   Ptr< Ptr< point<double> > > Pw;
00221 
00222   copy( nDatPoints, datPoints, datP );
00223   if( useStdDevs ) copy( nDatPoints, StdDevs, stdD );
00224 
00225   gunu::SurfaceOverXYPlane< double,point<double> >( 
00226                nDatPoints, datP, (useStdDevs!=0), stdD, (minimize!=0), nIter, pu, pv, 
00227                ret_nu, &U, ret_nv, &V, &Pw );
00228 
00229   copy( U.nElems(), U, retU ); 
00230   copy( V.nElems(), V, retV ); 
00231   copy( Pw.nElems(), Pw[0].nElems(), Pw, retPw );
00232 }
00233 
00234 /*-------------------------------------------------------------------*//**
00235   creates a surface with the MBA algorithm. 'Dat' contains the 3d
00236   values and 'Dom' the locations in the parametric domain 
00237   (output arrays are reserved automatically)                            */
00238 /* ---------------------------------------------------------------------*/
00239 
00240 GULDECL void GULCALL GUL_MBASurface( 
00241           int nDatPoints, GULpoint *datPoints, GULpoint2 *domPoints, 
00242           int nIter, int pu, int pv,
00243           int *ret_nu, double **retU,
00244           int *ret_nv, double **retV,
00245           GULpoint ***retPw )
00246 {
00247   Ptr< point<double> > dat;
00248   Ptr< point2<double> > dom;
00249   Ptr<double> U,V;
00250   Ptr< Ptr< point<double> > > Pw;
00251 
00252   copy( nDatPoints, datPoints, dat );
00253   copy( nDatPoints, domPoints, dom );
00254 
00255   gunu::MBASurface( nDatPoints, dat, dom, nIter, pu, pv, 
00256                     ret_nu, &U, ret_nv, &V, &Pw );
00257 
00258   copy( U.nElems(), U, retU ); 
00259   copy( V.nElems(), V, retV ); 
00260   copy( Pw.nElems(), Pw[0].nElems(), Pw, retPw );
00261 }
00262 
00263 
00264 /*-------------------------------------------------------------------
00265   Calculates a matrix wich transforms points given in a local coordinate
00266   system at a point 'B' into the world coordinate system.
00267   An additional point is required (both in world coordinates
00268   (=P) and local coordinates (=Pb)). 'Ab' must contain the coordinates
00269   of the origin in the local coordinate system at 'B'.
00270   Both coordinate systems must be orthonormal, and memory for 'T' (a 4x4
00271   matrix) must be reserved by the caller.
00272 -------------------------------------------------------------------*/
00273 
00274 GULDECL void GULCALL GUL_CommonCoordinateSystem( 
00275           GULpoint B, GULpoint P, GULpoint Ab, GULpoint Pb,
00276           double **T )
00277 {
00278   point<double> b,p,ab,pb;
00279   Ptr< Ptr<double> > t;
00280   int i,j;
00281 
00282   b.x = B.x;    b.y = B.y;    b.z = B.z;
00283   p.x = P.x;    p.y = P.y;    p.z = P.z;
00284   ab.x = Pb.x;    ab.y = Pb.y;    ab.z = Pb.z;
00285   pb.x = Pb.x;    pb.y = Pb.y;    pb.z = Pb.z;
00286 
00287   t.reserve_pool(4);
00288   for( i = 0; i < 4; i++ )
00289     t[i].reserve_pool(4);
00290 
00291   guma::CommonCoordinateSystem( b, p, ab, pb, t );
00292 
00293   for( i = 0; i < 4; i++ )
00294     for( j = 0; j < 4; j++ )
00295       T[i][j] = t[i][j];
00296 }
00297 
00298 }
00299 

Generated on Mon Jan 21 04:17:17 2002 for GUL 0.6 - Geometry Utility Library by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001