SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bezier.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // missing_desc
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 /* Subroutine to generate a Bezier curve.
25  Copyright (c) 2000 David F. Rogers. All rights reserved.
26 
27  b[] = array containing the defining polygon vertices
28  b[1] contains the x-component of the vertex
29  b[2] contains the y-component of the vertex
30  b[3] contains the z-component of the vertex
31  Basis = function to calculate the Bernstein basis value (see MECG Eq 5-65)
32  cpts = number of points to be calculated on the curve
33  Fractrl = function to calculate the factorial of a number
34  j[] = array containing the basis functions for a single value of t
35  npts = number of defining polygon vertices
36  p[] = array containing the curve points
37  p[1] contains the x-component of the point
38  p[2] contains the y-component of the point
39  p[3] contains the z-component of the point
40  t = parameter value 0 <= t <= 1
41 */
42 
43 // ===========================================================================
44 // included modules
45 // ===========================================================================
46 #ifdef _MSC_VER
47 #include <windows_config.h>
48 #else
49 #include <config.h>
50 #endif
51 
52 #include <math.h>
53 #include <iostream>
54 
55 #ifdef CHECK_MEMORY_LEAKS
56 #include <foreign/nvwa/debug_new.h>
57 #endif // CHECK_MEMORY_LEAKS
58 
59 /* function to calculate the factorial */
60 
61 SUMOReal factrl(int n) {
62  static int ntop = 6;
63  static SUMOReal a[33] = {
64  1.0, 1.0, 2.0, 6.0, 24.0, 120.0, 720.0
65  }
66  ; /* fill in the first few values */
67  int j1;
68 
69  if (n < 0) {
70  throw 1;
71  } //cout << "\nNegative factorial in routine FACTRL\n";
72  if (n > 32) {
73  throw 1;
74  } //cout << "\nFactorial value too large in routine FACTRL\n";
75 
76  while (ntop < n) { /* use the precalulated value for n = 0....6 */
77  j1 = ntop++;
78  a[ntop] = a[j1] * ntop;
79  }
80  return a[n]; /* returns the value n! as a SUMOReal */
81 }
82 
83 /* function to calculate the factorial function for Bernstein basis */
84 
85 SUMOReal Ni(int n, int i) {
86  return factrl(n) / (factrl(i) * factrl(n - i));
87 }
88 
89 /* function to calculate the Bernstein basis */
90 
91 SUMOReal Basis(int n, int i, SUMOReal t) {
92  /* handle the special cases to avoid domain problem with pow */
93  const SUMOReal ti = (i == 0) ? 1.0 : pow(t, i); /* this is t^i */
94  const SUMOReal tni = (n == i) ? 1.0 : pow(1 - t, n - i); /* this is (1-t)^(n-i) */
95  return Ni(n, i) * ti * tni;
96 }
97 
98 /* Bezier curve subroutine */
99 void
100 bezier(int npts, SUMOReal b[], int cpts, SUMOReal p[]) {
101  int i;
102  int j;
103  int i1;
104  int icount;
105  int jcount;
106 
107  const SUMOReal step = (SUMOReal) 1.0 / (cpts - 1);
108  SUMOReal t;
109 
110  /* calculate the points on the Bezier curve */
111 
112  icount = 0;
113  t = 0;
114 
115  for (i1 = 1; i1 <= cpts; i1++) { /* main loop */
116 
117  if ((1.0 - t) < 5e-6) {
118  t = 1.0;
119  }
120 
121  for (j = 1; j <= 3; j++) { /* generate a point on the curve */
122  jcount = j;
123  p[icount + j] = 0.;
124  for (i = 1; i <= npts; i++) { /* Do x,y,z components */
125  p[icount + j] = p[icount + j] + Basis(npts - 1, i - 1, t) * b[jcount];
126  jcount = jcount + 3;
127  }
128  }
129 
130  icount = icount + 3;
131  t = t + step;
132  }
133 }
134 
135 
136 
137 /****************************************************************************/
138