QGIS API Documentation  3.4.15-Madeira (e83d02e274)
Bezier3D.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Bezier3D.cpp
3  ------------
4  copyright : (C) 2004 by Marco Hugentobler
5  email : [email protected]
6  ***************************************************************************/
7 
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "Bezier3D.h"
18 #include "qgslogger.h"
19 #include "Vector3D.h"
20 #include "MathUtils.h"
21 
22 void Bezier3D::calcFirstDer( float t, Vector3D *v )
23 {
24  if ( v && mControlPoly )
25  {
26  v->setX( 0 );
27  v->setY( 0 );
28  v->setZ( 0 );
29 
30  if ( mControlPoly->count() < 2 )
31  {
32  return;
33  }
34 
35  for ( int n = 1; n <= int( mControlPoly->count() - 1 ); n++ )
36  {
37  double bernst = MathUtils::calcBernsteinPoly( mControlPoly->count() - 2, n - 1, t );
38  v->setX( v->getX() + ( ( *mControlPoly )[n]->x() - ( *mControlPoly )[n - 1]->x() )*bernst );
39  v->setY( v->getY() + ( ( *mControlPoly )[n]->y() - ( *mControlPoly )[n - 1]->y() )*bernst );
40  v->setZ( v->getZ() + ( ( *mControlPoly )[n]->z() - ( *mControlPoly )[n - 1]->z() )*bernst );
41  }
42  v->setX( v->getX() * ( mControlPoly->count() - 1 ) );
43  v->setY( v->getY() * ( mControlPoly->count() - 1 ) );
44  v->setZ( v->getZ() * ( mControlPoly->count() - 1 ) );
45  }
46 
47  else
48  {
49  QgsDebugMsg( QStringLiteral( "warning: null pointer" ) );
50  }
51 }
52 
53 void Bezier3D::calcPoint( float t, QgsPoint *p )
54 {
55 
56  if ( p && mControlPoly )
57  {
58  p->setX( 0 );
59  p->setY( 0 );
60  p->setZ( 0 );
61 
62  for ( int n = 1; n <= int( mControlPoly->count() ); n++ )
63  {
64  double bernst = MathUtils::calcBernsteinPoly( mControlPoly->count() - 1, n - 1, t );
65  p->setX( p->x() + ( *mControlPoly )[n - 1]->x()*bernst );
66  p->setY( p->y() + ( *mControlPoly )[n - 1]->y()*bernst );
67  p->setZ( p->z() + ( *mControlPoly )[n - 1]->z()*bernst );
68  }
69  }
70 
71  else
72  {
73  QgsDebugMsg( QStringLiteral( "warning: null pointer" ) );
74  }
75 }
76 
77 void Bezier3D::calcSecDer( float t, Vector3D *v )
78 {
79  if ( v && mControlPoly )
80  {
81  v->setX( 0 );
82  v->setY( 0 );
83  v->setZ( 0 );
84 
85  int nodes = mControlPoly->count();
86  if ( nodes < 3 )
87  {
88  return;
89  }
90 
91  for ( int n = 1; n <= int( nodes - 2 ); n++ )
92  {
93  double bernst = MathUtils::calcBernsteinPoly( nodes - 3, n - 1, t );
94  v->setX( v->getX() + ( ( *mControlPoly )[n + 1]->x() - 2 * ( *mControlPoly )[n]->x() + ( *mControlPoly )[n - 1]->x() )*bernst );
95  v->setY( v->getY() + ( ( *mControlPoly )[n + 1]->y() - 2 * ( *mControlPoly )[n]->y() + ( *mControlPoly )[n - 1]->y() )*bernst );
96  v->setZ( v->getZ() + ( ( *mControlPoly )[n + 1]->z() - 2 * ( *mControlPoly )[n]->z() + ( *mControlPoly )[n - 1]->z() )*bernst );
97  }
98  v->setX( v->getX()*MathUtils::faculty( nodes - 1 ) / MathUtils::faculty( nodes - 3 ) );
99  v->setY( v->getY()*MathUtils::faculty( nodes - 1 ) / MathUtils::faculty( nodes - 3 ) );
100  v->setZ( v->getZ()*MathUtils::faculty( nodes - 1 ) / MathUtils::faculty( nodes - 3 ) );
101  }
102 
103  else
104  {
105  QgsDebugMsg( QStringLiteral( "warning: null pointer" ) );
106  }
107 }
108 
109 
110 void Bezier3D::changeDirection()//does this work correctly? more testing is needed.
111 {
112  if ( mControlPoly )
113  {
114  QgsPoint **pointer = new QgsPoint*[mControlPoly->count()];//create an array to temporarily store pointer to the control points
115  for ( int i = 0; i < mControlPoly->count(); i++ )//store the points
116  {
117  pointer[i] = ( *mControlPoly )[i];
118  }
119 
120  for ( int i = 0; i < mControlPoly->count(); i++ )
121  {
122  mControlPoly->insert( i, pointer[( mControlPoly->count() - 1 ) - i] );
123  }
124  delete [] pointer;
125  }
126 
127  else
128  {
129  QgsDebugMsg( QStringLiteral( "warning: null pointer" ) );
130  }
131 }
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
void setX(double x)
Sets the x-component of the vector.
Definition: Vector3D.h:103
QVector< QgsPoint * > * mControlPoly
MControlPoly stores the points of the control polygon.
double y
Definition: qgspoint.h:42
void setZ(double z)
Sets the point&#39;s z-coordinate.
Definition: qgspoint.h:237
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
void changeDirection() override
Changes the order of control points.
Definition: Bezier3D.cpp:110
void calcPoint(float t, QgsPoint *p) override
Calculates the point on the curve and assigns it to p.
Definition: Bezier3D.cpp:53
void calcSecDer(float t, Vector3D *v) override
Calculates the second derivative and assigns it to v.
Definition: Bezier3D.cpp:77
Class Vector3D represents a 3D-Vector, capable to store x-,y- and z-coordinates in double values...
Definition: Vector3D.h:33
void setZ(double z)
Sets the z-component of the vector.
Definition: Vector3D.h:113
void calcFirstDer(float t, Vector3D *v) override
Calculates the first derivative and assigns it to v.
Definition: Bezier3D.cpp:22
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
double getY() const
Returns the y-component of the vector.
Definition: Vector3D.h:93
double getX() const
Returns the x-component of the vector.
Definition: Vector3D.h:88
void setX(double x)
Sets the point&#39;s x-coordinate.
Definition: qgspoint.h:213
void setY(double y)
Sets the point&#39;s y-coordinate.
Definition: qgspoint.h:224
void setY(double y)
Sets the y-component of the vector.
Definition: Vector3D.h:108
double ANALYSIS_EXPORT calcBernsteinPoly(int n, int i, double t)
Calculates the value of a Bernstein polynomial.
Definition: MathUtils.cpp:101
double z
Definition: qgspoint.h:43
double getZ() const
Returns the z-component of the vector.
Definition: Vector3D.h:98
int ANALYSIS_EXPORT faculty(int n)
Faculty function.
Definition: MathUtils.cpp:221
double x
Definition: qgspoint.h:41