|
SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00007 // missing_desc 00008 /****************************************************************************/ 00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00010 // Copyright (C) 2001-2011 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[ntop]=a[j1]*ntop; 00076 } 00077 return a[n]; /* returns the value n! as a SUMOReal */ 00078 } 00079 00080 /* function to calculate the factorial function for Bernstein basis */ 00081 00082 SUMOReal Ni(int n,int i) { 00083 return factrl(n)/(factrl(i)*factrl(n-i)); 00084 } 00085 00086 /* function to calculate the Bernstein basis */ 00087 00088 SUMOReal Basis(int n,int i,SUMOReal t) { 00089 /* handle the special cases to avoid domain problem with pow */ 00090 const SUMOReal ti = (i == 0) ? 1.0 : pow(t, i); /* this is t^i */ 00091 const SUMOReal tni = (n == i) ? 1.0 : pow(1-t, n-i); /* this is (1-t)^(n-i) */ 00092 return Ni(n,i) * ti * tni; 00093 } 00094 00095 /* Bezier curve subroutine */ 00096 void 00097 bezier(int npts, SUMOReal b[], int cpts, SUMOReal p[]) { 00098 int i; 00099 int j; 00100 int i1; 00101 int icount; 00102 int jcount; 00103 00104 const SUMOReal step = (SUMOReal) 1.0/(cpts -1); 00105 SUMOReal t; 00106 00107 /* calculate the points on the Bezier curve */ 00108 00109 icount = 0; 00110 t = 0; 00111 00112 for (i1 = 1; i1<=cpts; i1++) { /* main loop */ 00113 00114 if ((1.0 - t) < 5e-6) { 00115 t = 1.0; 00116 } 00117 00118 for (j = 1; j <= 3; j++) { /* generate a point on the curve */ 00119 jcount = j; 00120 p[icount+j] = 0.; 00121 for (i = 1; i <= npts; i++) { /* Do x,y,z components */ 00122 p[icount + j] = p[icount + j] + Basis(npts-1,i-1,t)*b[jcount]; 00123 jcount = jcount + 3; 00124 } 00125 } 00126 00127 icount = icount + 3; 00128 t = t + step; 00129 } 00130 } 00131 00132 00133 00134 /****************************************************************************/ 00135