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