QGIS API Documentation 3.41.0-Master (af5edcb665c)
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#include "qgslogger.h"
19#include "Vector3D.h"
20#include "MathUtils.h"
21
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 const 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 QgsDebugError( QStringLiteral( "warning: null pointer" ) );
50 }
51}
52
53void Bezier3D::calcPoint( float t, QgsPoint *p )
54{
55 if ( p && mControlPoly )
56 {
57 p->setX( 0 );
58 p->setY( 0 );
59 p->setZ( 0 );
60
61 for ( int n = 1; n <= int( mControlPoly->count() ); n++ )
62 {
63 const double bernst = MathUtils::calcBernsteinPoly( mControlPoly->count() - 1, n - 1, t );
64 p->setX( p->x() + ( *mControlPoly )[n - 1]->x() * bernst );
65 p->setY( p->y() + ( *mControlPoly )[n - 1]->y() * bernst );
66 p->setZ( p->z() + ( *mControlPoly )[n - 1]->z() * bernst );
67 }
68 }
69
70 else
71 {
72 QgsDebugError( QStringLiteral( "warning: null pointer" ) );
73 }
74}
75
76void Bezier3D::calcSecDer( float t, Vector3D *v )
77{
78 if ( v && mControlPoly )
79 {
80 v->setX( 0 );
81 v->setY( 0 );
82 v->setZ( 0 );
83
84 const int nodes = mControlPoly->count();
85 if ( nodes < 3 )
86 {
87 return;
88 }
89
90 for ( int n = 1; n <= int( nodes - 2 ); n++ )
91 {
92 const double bernst = MathUtils::calcBernsteinPoly( nodes - 3, n - 1, t );
93 v->setX( v->getX() + ( ( *mControlPoly )[n + 1]->x() - 2 * ( *mControlPoly )[n]->x() + ( *mControlPoly )[n - 1]->x() ) * bernst );
94 v->setY( v->getY() + ( ( *mControlPoly )[n + 1]->y() - 2 * ( *mControlPoly )[n]->y() + ( *mControlPoly )[n - 1]->y() ) * bernst );
95 v->setZ( v->getZ() + ( ( *mControlPoly )[n + 1]->z() - 2 * ( *mControlPoly )[n]->z() + ( *mControlPoly )[n - 1]->z() ) * bernst );
96 }
97 v->setX( v->getX() * MathUtils::faculty( nodes - 1 ) / MathUtils::faculty( nodes - 3 ) );
98 v->setY( v->getY() * MathUtils::faculty( nodes - 1 ) / MathUtils::faculty( nodes - 3 ) );
99 v->setZ( v->getZ() * MathUtils::faculty( nodes - 1 ) / MathUtils::faculty( nodes - 3 ) );
100 }
101
102 else
103 {
104 QgsDebugError( QStringLiteral( "warning: null pointer" ) );
105 }
106}
107
108
109void Bezier3D::changeDirection() //does this work correctly? more testing is needed.
110{
111 if ( mControlPoly )
112 {
113 QgsPoint **pointer = new QgsPoint *[mControlPoly->count()]; //create an array to temporarily store pointer to the control points
114 for ( int i = 0; i < mControlPoly->count(); i++ ) //store the points
115 {
116 pointer[i] = ( *mControlPoly )[i];
117 }
118
119 for ( int i = 0; i < mControlPoly->count(); i++ )
120 {
121 mControlPoly->insert( i, pointer[( mControlPoly->count() - 1 ) - i] );
122 }
123 delete[] pointer;
124 }
125
126 else
127 {
128 QgsDebugError( QStringLiteral( "warning: null pointer" ) );
129 }
130}
void calcPoint(float t, QgsPoint *p) override
Calculates the point on the curve and assigns it to p.
Definition Bezier3D.cpp:53
void calcFirstDer(float t, Vector3D *v) override
Calculates the first derivative and assigns it to v.
Definition Bezier3D.cpp:22
void changeDirection() override
Changes the order of control points.
Definition Bezier3D.cpp:109
void calcSecDer(float t, Vector3D *v) override
Calculates the second derivative and assigns it to v.
Definition Bezier3D.cpp:76
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:343
void setX(double x)
Sets the point's x-coordinate.
Definition qgspoint.h:332
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:356
double y
Definition qgspoint.h:53
Class Vector3D represents a 3D-Vector, capable to store x-,y- and z-coordinates in double values.
Definition Vector3D.h:36
void setX(double x)
Sets the x-component of the vector.
Definition Vector3D.h:105
double getY() const
Returns the y-component of the vector.
Definition Vector3D.h:95
double getX() const
Returns the x-component of the vector.
Definition Vector3D.h:90
void setY(double y)
Sets the y-component of the vector.
Definition Vector3D.h:110
double getZ() const
Returns the z-component of the vector.
Definition Vector3D.h:100
void setZ(double z)
Sets the z-component of the vector.
Definition Vector3D.h:115
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:38