SUMO - Simulation of Urban MObility
bezier.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2003-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // missing_desc
17 /****************************************************************************/
18 
19 
20 /* Subroutine to generate a Bezier curve.
21  Copyright (c) 2000 David F. Rogers. All rights reserved.
22 
23  b[] = array containing the defining polygon vertices
24  b[1] contains the x-component of the vertex
25  b[2] contains the y-component of the vertex
26  b[3] contains the z-component of the vertex
27  Basis = function to calculate the Bernstein basis value (see MECG Eq 5-65)
28  cpts = number of points to be calculated on the curve
29  Fractrl = function to calculate the factorial of a number
30  j[] = array containing the basis functions for a single value of t
31  npts = number of defining polygon vertices
32  p[] = array containing the curve points
33  p[1] contains the x-component of the point
34  p[2] contains the y-component of the point
35  p[3] contains the z-component of the point
36  t = parameter value 0 <= t <= 1
37 */
38 
39 // ===========================================================================
40 // included modules
41 // ===========================================================================
42 #include <config.h>
43 
44 #include <cmath>
45 #include <iostream>
46 #include <utils/common/StdDefs.h>
47 #include "PositionVector.h"
48 
49 /* function to calculate the factorial */
50 
51 double factrl(int n) {
52  static int ntop = 6;
53  static double a[33] = {
54  1.0, 1.0, 2.0, 6.0, 24.0, 120.0, 720.0
55  }
56  ; /* fill in the first few values */
57  int j1;
58 
59  if (n < 0) {
60  throw 1;
61  } //cout << "\nNegative factorial in routine FACTRL\n";
62  if (n > 32) {
63  throw 1;
64  } //cout << "\nFactorial value too large in routine FACTRL\n";
65 
66  while (ntop < n) { /* use the precalulated value for n = 0....6 */
67  j1 = ntop++;
68  a[ntop] = a[j1] * ntop;
69  }
70  return a[n]; /* returns the value n! as a double */
71 }
72 
73 /* function to calculate the factorial function for Bernstein basis */
74 
75 double Ni(int n, int i) {
76  return factrl(n) / (factrl(i) * factrl(n - i));
77 }
78 
79 /* function to calculate the Bernstein basis */
80 
81 double Basis(int n, int i, double t) {
82  /* handle the special cases to avoid domain problem with pow */
83  const double ti = (i == 0) ? 1.0 : pow(t, i); /* this is t^i */
84  const double tni = (n == i) ? 1.0 : pow(1 - t, n - i); /* this is (1-t)^(n-i) */
85  return Ni(n, i) * ti * tni;
86 }
87 
88 /* Bezier curve subroutine */
89 void
90 bezier(int npts, double b[], int cpts, double p[]) {
91  int i;
92  int j;
93  int i1;
94  int icount;
95  int jcount;
96 
97  const double step = (double) 1.0 / (cpts - 1);
98  double t;
99 
100  /* calculate the points on the Bezier curve */
101 
102  icount = 0;
103  t = 0;
104 
105  for (i1 = 1; i1 <= cpts; i1++) { /* main loop */
106 
107  if ((1.0 - t) < 5e-6) {
108  t = 1.0;
109  }
110 
111  for (j = 1; j <= 3; j++) { /* generate a point on the curve */
112  jcount = j;
113  p[icount + j] = 0.;
114  for (i = 1; i <= npts; i++) { /* Do x,y,z components */
115  p[icount + j] = p[icount + j] + Basis(npts - 1, i - 1, t) * b[jcount];
116  jcount = jcount + 3;
117  }
118  }
119 
120  icount = icount + 3;
121  t = t + step;
122  }
123 }
124 
125 
127 bezier(const PositionVector& init, int numPoints) {
128  PositionVector ret;
129  double* def = new double[1 + (int)init.size() * 3];
130  for (int i = 0; i < (int)init.size(); ++i) {
131  // starts at index 1
132  def[i * 3 + 1] = init[i].x();
133  def[i * 3 + 2] = init[i].z();
134  def[i * 3 + 3] = init[i].y();
135  }
136  double* ret_buf = new double[numPoints * 3 + 1];
137  bezier((int)init.size(), def, numPoints, ret_buf);
138  delete[] def;
139  Position prev;
140  for (int i = 0; i < (int)numPoints; i++) {
141  Position current(ret_buf[i * 3 + 1], ret_buf[i * 3 + 3], ret_buf[i * 3 + 2]);
142  if (prev != current && !ISNAN(current.x()) && !ISNAN(current.y()) && !ISNAN(current.z())) {
143  ret.push_back(current);
144  }
145  prev = current;
146  }
147  delete[] ret_buf;
148  return ret;
149 }
150 
151 /****************************************************************************/
152 
double z() const
Returns the z-position.
Definition: Position.h:67
double y() const
Returns the y-position.
Definition: Position.h:62
double x() const
Returns the x-position.
Definition: Position.h:57
double Basis(int n, int i, double t)
Definition: bezier.cpp:81
double Ni(int n, int i)
Definition: bezier.cpp:75
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
A list of positions.
T ISNAN(T a)
Definition: StdDefs.h:111
double factrl(int n)
Definition: bezier.cpp:51
void bezier(int npts, double b[], int cpts, double p[])
Definition: bezier.cpp:90