QGIS API Documentation 3.99.0-Master (d270888f95f)
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
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28{
29 if ( v && mControlPoly )
30 {
31 v->setX( 0 );
32 v->setY( 0 );
33 v->setZ( 0 );
34
35 if ( mControlPoly->count() < 2 )
36 {
37 return;
38 }
39
40 for ( int n = 1; n <= int( mControlPoly->count() - 1 ); n++ )
41 {
42 const double bernst = MathUtils::calcBernsteinPoly( mControlPoly->count() - 2, n - 1, t );
43 v->setX( v->getX() + ( ( *mControlPoly )[n]->x() - ( *mControlPoly )[n - 1]->x() ) * bernst );
44 v->setY( v->getY() + ( ( *mControlPoly )[n]->y() - ( *mControlPoly )[n - 1]->y() ) * bernst );
45 v->setZ( v->getZ() + ( ( *mControlPoly )[n]->z() - ( *mControlPoly )[n - 1]->z() ) * bernst );
46 }
47 v->setX( v->getX() * ( mControlPoly->count() - 1 ) );
48 v->setY( v->getY() * ( mControlPoly->count() - 1 ) );
49 v->setZ( v->getZ() * ( mControlPoly->count() - 1 ) );
50 }
51
52 else
53 {
54 QgsDebugError( u"warning: null pointer"_s );
55 }
56}
57
58void Bezier3D::calcPoint( float t, QgsPoint *p )
59{
60 if ( p && mControlPoly )
61 {
62 p->setX( 0 );
63 p->setY( 0 );
64 p->setZ( 0 );
65
66 for ( int n = 1; n <= int( mControlPoly->count() ); n++ )
67 {
68 const double bernst = MathUtils::calcBernsteinPoly( mControlPoly->count() - 1, n - 1, t );
69 p->setX( p->x() + ( *mControlPoly )[n - 1]->x() * bernst );
70 p->setY( p->y() + ( *mControlPoly )[n - 1]->y() * bernst );
71 p->setZ( p->z() + ( *mControlPoly )[n - 1]->z() * bernst );
72 }
73 }
74
75 else
76 {
77 QgsDebugError( u"warning: null pointer"_s );
78 }
79}
80
81void Bezier3D::calcSecDer( float t, Vector3D *v )
82{
83 if ( v && mControlPoly )
84 {
85 v->setX( 0 );
86 v->setY( 0 );
87 v->setZ( 0 );
88
89 const int nodes = mControlPoly->count();
90 if ( nodes < 3 )
91 {
92 return;
93 }
94
95 for ( int n = 1; n <= int( nodes - 2 ); n++ )
96 {
97 const double bernst = MathUtils::calcBernsteinPoly( nodes - 3, n - 1, t );
98 v->setX( v->getX() + ( ( *mControlPoly )[n + 1]->x() - 2 * ( *mControlPoly )[n]->x() + ( *mControlPoly )[n - 1]->x() ) * bernst );
99 v->setY( v->getY() + ( ( *mControlPoly )[n + 1]->y() - 2 * ( *mControlPoly )[n]->y() + ( *mControlPoly )[n - 1]->y() ) * bernst );
100 v->setZ( v->getZ() + ( ( *mControlPoly )[n + 1]->z() - 2 * ( *mControlPoly )[n]->z() + ( *mControlPoly )[n - 1]->z() ) * bernst );
101 }
102 v->setX( v->getX() * MathUtils::faculty( nodes - 1 ) / MathUtils::faculty( nodes - 3 ) );
103 v->setY( v->getY() * MathUtils::faculty( nodes - 1 ) / MathUtils::faculty( nodes - 3 ) );
104 v->setZ( v->getZ() * MathUtils::faculty( nodes - 1 ) / MathUtils::faculty( nodes - 3 ) );
105 }
106
107 else
108 {
109 QgsDebugError( u"warning: null pointer"_s );
110 }
111}
112
113
114void Bezier3D::changeDirection() //does this work correctly? more testing is needed.
115{
116 if ( mControlPoly )
117 {
118 QgsPoint **pointer = new QgsPoint *[mControlPoly->count()]; //create an array to temporarily store pointer to the control points
119 for ( int i = 0; i < mControlPoly->count(); i++ ) //store the points
120 {
121 pointer[i] = ( *mControlPoly )[i];
122 }
123
124 for ( int i = 0; i < mControlPoly->count(); i++ )
125 {
126 mControlPoly->insert( i, pointer[( mControlPoly->count() - 1 ) - i] );
127 }
128 delete[] pointer;
129 }
130
131 else
132 {
133 QgsDebugError( u"warning: null pointer"_s );
134 }
135}
void calcPoint(float t, QgsPoint *p) override
Calculates the point on the curve and assigns it to p.
Definition Bezier3D.cpp:58
void calcFirstDer(float t, Vector3D *v) override
Calculates the first derivative and assigns it to v.
Definition Bezier3D.cpp:27
void changeDirection() override
Changes the order of control points.
Definition Bezier3D.cpp:114
void calcSecDer(float t, Vector3D *v) override
Calculates the second derivative and assigns it to v.
Definition Bezier3D.cpp:81
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:53
void setY(double y)
Sets the point's y-coordinate.
Definition qgspoint.h:341
void setX(double x)
Sets the point's x-coordinate.
Definition qgspoint.h:330
double z
Definition qgspoint.h:58
double x
Definition qgspoint.h:56
void setZ(double z)
Sets the point's z-coordinate.
Definition qgspoint.h:354
double y
Definition qgspoint.h:57
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:59