bezier.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // missing_desc
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 /* Subroutine to generate a Bezier curve.
00022     Copyright (c) 2000 David F. Rogers. All rights reserved.
00023 
00024     b[]        = array containing the defining polygon vertices
00025                   b[1] contains the x-component of the vertex
00026                   b[2] contains the y-component of the vertex
00027                   b[3] contains the z-component of the vertex
00028     Basis      = function to calculate the Bernstein basis value (see MECG Eq 5-65)
00029     cpts       = number of points to be calculated on the curve
00030     Fractrl    = function to calculate the factorial of a number
00031     j[]        = array containing the basis functions for a single value of t
00032     npts       = number of defining polygon vertices
00033     p[]        = array containing the curve points
00034                  p[1] contains the x-component of the point
00035                  p[2] contains the y-component of the point
00036                  p[3] contains the z-component of the point
00037     t          = parameter value 0 <= t <= 1
00038 */
00039 
00040 // ===========================================================================
00041 // included modules
00042 // ===========================================================================
00043 #ifdef _MSC_VER
00044 #include <windows_config.h>
00045 #else
00046 #include <config.h>
00047 #endif
00048 
00049 #include <math.h>
00050 #include <iostream>
00051 
00052 #ifdef CHECK_MEMORY_LEAKS
00053 #include <foreign/nvwa/debug_new.h>
00054 #endif // CHECK_MEMORY_LEAKS
00055 
00056 /* function to calculate the factorial */
00057 
00058 SUMOReal factrl(int n) {
00059     static int ntop=6;
00060     static SUMOReal a[33]= {
00061         1.0,1.0,2.0,6.0,24.0,120.0,720.0
00062     }
00063     ; /* fill in the first few values */
00064     int j1;
00065 
00066     if (n < 0) {
00067         throw 1;
00068     } //cout << "\nNegative factorial in routine FACTRL\n" ;
00069     if (n > 32) {
00070         throw 1;
00071     } //cout << "\nFactorial value too large in routine FACTRL\n";
00072 
00073     while (ntop < n) { /* use the precalulated value for n = 0....6 */
00074         j1 = ntop++;
00075         a[n]=a[j1]*ntop;
00076     }
00077     return a[n]; /* returns the value n! as a SUMORealing point number */
00078 }
00079 
00080 /* function to calculate the factorial function for Bernstein basis */
00081 
00082 SUMOReal Ni(int n,int i) {
00083     SUMOReal ni;
00084     ni = factrl(n)/(factrl(i)*factrl(n-i));
00085     return ni;
00086 }
00087 
00088 /* function to calculate the Bernstein basis */
00089 
00090 SUMOReal Basis(int n,int i,SUMOReal t) {
00091     SUMOReal basis;
00092     SUMOReal ti; /* this is t^i */
00093     SUMOReal tni; /* this is (1 - t)^i */
00094 
00095     /* handle the special cases to avoid domain problem with pow */
00096 
00097     if (t==0. && i == 0) ti=1.0;
00098     else ti = pow(t,i);
00099     if (n==i && t==1.) tni=1.0;
00100     else tni = pow((1-t),(n-i));
00101     basis = Ni(n,i)*ti*tni; /* calculate Bernstein basis function */
00102     return basis;
00103 }
00104 
00105 /* Bezier curve subroutine */
00106 void
00107 bezier(int npts, SUMOReal b[], int cpts, SUMOReal p[]) {
00108     int i;
00109     int j;
00110     int i1;
00111     int icount;
00112     int jcount;
00113 
00114     SUMOReal step;
00115     SUMOReal t;
00116 
00117     SUMOReal factrl(int);
00118     SUMOReal Ni(int,int);
00119     SUMOReal Basis(int,int,SUMOReal);
00120 
00121     /*    calculate the points on the Bezier curve */
00122 
00123     icount = 0;
00124     t = 0;
00125     step = (SUMOReal) 1.0/(cpts -1);
00126 
00127     for (i1 = 1; i1<=cpts; i1++) { /* main loop */
00128 
00129         if ((1.0 - t) < 5e-6) t = 1.0;
00130 
00131         for (j = 1; j <= 3; j++) { /* generate a point on the curve */
00132             jcount = j;
00133             p[icount+j] = 0.;
00134             for (i = 1; i <= npts; i++) { /* Do x,y,z components */
00135                 p[icount + j] = p[icount + j] + Basis(npts-1,i-1,t)*b[jcount];
00136                 jcount = jcount + 3;
00137             }
00138         }
00139 
00140         icount = icount + 3;
00141         t = t + step;
00142     }
00143 }
00144 
00145 
00146 
00147 /****************************************************************************/
00148 

Generated on Wed May 5 00:06:28 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6