QGIS API Documentation 3.99.0-Master (a8882ad4560)
Loading...
Searching...
No Matches
qgscrosssection.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscrosssection.cpp
3 ---------------------
4 begin : January 2026
5 copyright : (C) 2026 by Dominik Cindrić
6 email : viper dot miniq at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgscrosssection.h"
17
19#include "qgslinestring.h"
20#include "qgspolygon.h"
21
23 : mStartPoint( p1 )
24 , mEndPoint( p2 )
25 , mHalfWidth( halfWidth )
26{
27}
28
30{
31 if ( !isValid() )
32 return QgsGeometry();
33
34 QgsVector vec( mEndPoint - mStartPoint );
35 vec = vec.normalized().perpVector();
36
37 QgsGeometry geom;
38 if ( mHalfWidth == 0.0 )
39 {
40 QVector<QgsPointXY> points = { mStartPoint, mEndPoint };
41 geom = QgsGeometry( new QgsLineString( points ) );
42 }
43 else
44 {
45 QVector<QgsPointXY> points = {
46 mStartPoint + vec * mHalfWidth,
47 mEndPoint + vec * mHalfWidth,
48 mEndPoint - vec * mHalfWidth,
49 mStartPoint - vec * mHalfWidth
50 };
51 geom = QgsGeometry( new QgsPolygon( new QgsLineString( points ) ) );
52 }
53
54 if ( ct )
55 {
56 try
57 {
59 }
60 catch ( const QgsCsException & )
61 {
62 geom = QgsGeometry();
63 }
64 }
65 return geom;
66}
67
68void QgsCrossSection::nudgeLeft( double distance )
69{
70 nudge( distance );
71}
72
73void QgsCrossSection::nudgeRight( double distance )
74{
75 nudge( -distance );
76}
77
78void QgsCrossSection::nudge( double distance )
79{
80 if ( !isValid() )
81 return;
82
83 QgsVector vec( mEndPoint - mStartPoint );
84 vec = vec.normalized().perpVector();
85
86 const QgsVector offset = vec * distance;
87 mStartPoint += offset;
88 mEndPoint += offset;
89}
90
92{
93 return ( mStartPoint != mEndPoint ) && ( mHalfWidth > 0.0 );
94}
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2719
Handles coordinate transforms between two coordinate systems.
double halfWidth() const
Returns the half-width of the cross section.
QgsCrossSection()=default
Constructs an invalid cross section.
QgsGeometry asGeometry(const QgsCoordinateTransform *ct=nullptr) const
Returns the cross section extent as a geometry (Polygon or LineString).
void nudgeRight(double distance)
Nudges the cross section to the right by the specified distance.
bool isValid() const
Returns cross section validity.
void nudgeLeft(double distance)
Nudges the cross section to the left by the specified distance.
Custom exception class for Coordinate Reference System related exceptions.
A geometry is the spatial representation of a feature.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
Line string geometry type, with support for z-dimension and m-values.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
Polygon geometry type.
Definition qgspolygon.h:33
Represent a 2-dimensional vector.
Definition qgsvector.h:31
QgsVector normalized() const
Returns the vector's normalized (or "unit") vector (ie same angle but length of 1....
Definition qgsvector.cpp:29
QgsVector perpVector() const
Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise).
Definition qgsvector.h:161