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-2017 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 #include <utils/common/StdDefs.h>
54 #include "PositionVector.h"
55 
56 /* function to calculate the factorial */
57 
58 double factrl(int n) {
59  static int ntop = 6;
60  static double a[33] = {
61  1.0, 1.0, 2.0, 6.0, 24.0, 120.0, 720.0
62  }
63  ; /* fill in the first few values */
64  int j1;
65 
66  if (n < 0) {
67  throw 1;
68  } //cout << "\nNegative factorial in routine FACTRL\n";
69  if (n > 32) {
70  throw 1;
71  } //cout << "\nFactorial value too large in routine FACTRL\n";
72 
73  while (ntop < n) { /* use the precalulated value for n = 0....6 */
74  j1 = ntop++;
75  a[ntop] = a[j1] * ntop;
76  }
77  return a[n]; /* returns the value n! as a double */
78 }
79 
80 /* function to calculate the factorial function for Bernstein basis */
81 
82 double Ni(int n, int i) {
83  return factrl(n) / (factrl(i) * factrl(n - i));
84 }
85 
86 /* function to calculate the Bernstein basis */
87 
88 double Basis(int n, int i, double t) {
89  /* handle the special cases to avoid domain problem with pow */
90  const double ti = (i == 0) ? 1.0 : pow(t, i); /* this is t^i */
91  const double tni = (n == i) ? 1.0 : pow(1 - t, n - i); /* this is (1-t)^(n-i) */
92  return Ni(n, i) * ti * tni;
93 }
94 
95 /* Bezier curve subroutine */
96 void
97 bezier(int npts, double b[], int cpts, double p[]) {
98  int i;
99  int j;
100  int i1;
101  int icount;
102  int jcount;
103 
104  const double step = (double) 1.0 / (cpts - 1);
105  double t;
106 
107  /* calculate the points on the Bezier curve */
108 
109  icount = 0;
110  t = 0;
111 
112  for (i1 = 1; i1 <= cpts; i1++) { /* main loop */
113 
114  if ((1.0 - t) < 5e-6) {
115  t = 1.0;
116  }
117 
118  for (j = 1; j <= 3; j++) { /* generate a point on the curve */
119  jcount = j;
120  p[icount + j] = 0.;
121  for (i = 1; i <= npts; i++) { /* Do x,y,z components */
122  p[icount + j] = p[icount + j] + Basis(npts - 1, i - 1, t) * b[jcount];
123  jcount = jcount + 3;
124  }
125  }
126 
127  icount = icount + 3;
128  t = t + step;
129  }
130 }
131 
132 
134 bezier(const PositionVector& init, int numPoints) {
135  PositionVector ret;
136  double* def = new double[1 + (int)init.size() * 3];
137  for (int i = 0; i < (int)init.size(); ++i) {
138  // starts at index 1
139  def[i * 3 + 1] = init[i].x();
140  def[i * 3 + 2] = init[i].z();
141  def[i * 3 + 3] = init[i].y();
142  }
143  double* ret_buf = new double[numPoints * 3 + 1];
144  bezier((int)init.size(), def, numPoints, ret_buf);
145  delete[] def;
146  Position prev;
147  for (int i = 0; i < (int)numPoints; i++) {
148  Position current(ret_buf[i * 3 + 1], ret_buf[i * 3 + 3], ret_buf[i * 3 + 2]);
149  if (prev != current && !ISNAN(current.x()) && !ISNAN(current.y()) && !ISNAN(current.z())) {
150  ret.push_back(current);
151  }
152  prev = current;
153  }
154  delete[] ret_buf;
155  return ret;
156 }
157 
158 /****************************************************************************/
159 
double z() const
Returns the z-position.
Definition: Position.h:73
double y() const
Returns the y-position.
Definition: Position.h:68
double x() const
Returns the x-position.
Definition: Position.h:63
double Basis(int n, int i, double t)
Definition: bezier.cpp:88
double Ni(int n, int i)
Definition: bezier.cpp:82
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
T ISNAN(T a)
Definition: StdDefs.h:105
double factrl(int n)
Definition: bezier.cpp:58
void bezier(int npts, double b[], int cpts, double p[])
Definition: bezier.cpp:97