QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
Bezier3D.cpp
Go to the documentation of this file.
1/***************************************************************************
2 Bezier3D.cpp
3 ------------
4 copyright : (C) 2004 by Marco Hugentobler
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
19#include "MathUtils.h"
20#include "Vector3D.h"
21#include "qgslogger.h"
22
24{
25 if ( v && mControlPoly )
26 {
27 v->setX( 0 );
28 v->setY( 0 );
29 v->setZ( 0 );
30
31 if ( mControlPoly->count() < 2 )
32 {
33 return;
34 }
35
36 for ( int n = 1; n <= int( mControlPoly->count() - 1 ); n++ )
37 {
38 const double bernst = MathUtils::calcBernsteinPoly( mControlPoly->count() - 2, n - 1, t );
39 v->setX( v->getX() + ( ( *mControlPoly )[n]->x() - ( *mControlPoly )[n - 1]->x() ) * bernst );
40 v->setY( v->getY() + ( ( *mControlPoly )[n]->y() - ( *mControlPoly )[n - 1]->y() ) * bernst );
41 v->setZ( v->getZ() + ( ( *mControlPoly )[n]->z() - ( *mControlPoly )[n - 1]->z() ) * bernst );
42 }
43 v->setX( v->getX() * ( mControlPoly->count() - 1 ) );
44 v->setY( v->getY() * ( mControlPoly->count() - 1 ) );
45 v->setZ( v->getZ() * ( mControlPoly->count() - 1 ) );
46 }
47
48 else
49 {
50 QgsDebugError( QStringLiteral( "warning: null pointer" ) );
51 }
52}
53
54void Bezier3D::calcPoint( float t, QgsPoint *p )
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 const 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 QgsDebugError( QStringLiteral( "warning: null pointer" ) );
74 }
75}
76
77void 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 const 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 const 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 QgsDebugError( QStringLiteral( "warning: null pointer" ) );
106 }
107}
108
109
110void 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 QgsDebugError( QStringLiteral( "warning: null pointer" ) );
130 }
131}
void calcPoint(float t, QgsPoint *p) override
Calculates the point on the curve and assigns it to p.
Definition Bezier3D.cpp:54
void calcFirstDer(float t, Vector3D *v) override
Calculates the first derivative and assigns it to v.
Definition Bezier3D.cpp:23
void changeDirection() override
Changes the order of control points.
Definition Bezier3D.cpp:110
void calcSecDer(float t, Vector3D *v) override
Calculates the second derivative and assigns it to v.
Definition Bezier3D.cpp:77
QVector< QgsPoint * > * mControlPoly
MControlPoly stores the points of the control polygon.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
void setY(double y)
Sets the point's y-coordinate.
Definition qgspoint.h:337
void setX(double x)
Sets the point's x-coordinate.
Definition qgspoint.h:326
double z
Definition qgspoint.h:54
double x
Definition qgspoint.h:52
void setZ(double z)
Sets the point's z-coordinate.
Definition qgspoint.h:350
double y
Definition qgspoint.h:53
Represents a 3D-Vector, capable of storing x, y and z-coordinates in double values.
Definition Vector3D.h:37
void setX(double x)
Sets the x-component of the vector.
Definition Vector3D.h:106
double getY() const
Returns the y-component of the vector.
Definition Vector3D.h:96
double getX() const
Returns the x-component of the vector.
Definition Vector3D.h:91
void setY(double y)
Sets the y-component of the vector.
Definition Vector3D.h:111
double getZ() const
Returns the z-component of the vector.
Definition Vector3D.h:101
void setZ(double z)
Sets the z-component of the vector.
Definition Vector3D.h:116
double ANALYSIS_EXPORT calcBernsteinPoly(int n, int i, double t)
Calculates the value of a Bernstein polynomial.
int ANALYSIS_EXPORT faculty(int n)
Faculty function.
#define QgsDebugError(str)
Definition qgslogger.h:57