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