QGIS API Documentation  2.15.0-Master (af20121)
qgsclipper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsclipper.cpp - a class that clips line
3  segments and polygons
4  -------------------
5  begin : March 2004
6  copyright : (C) 2005 by Gavin Macaulay
7  email :
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #include "qgsclipper.h"
20 #include "qgsgeometry.h"
21 #include "qgswkbptr.h"
22 #include "qgslogger.h"
23 
24 // Where has all the code gone?
25 
26 // It's been inlined, so its in the qgsclipper.h file.
27 
28 // But the static members must be initialized outside the class! (or GCC 4 dies)
29 
30 // Qt also does clipping when the coordinates go over +/- 32767
31 // moreover from Qt 4.6, Qt clips also when the width/height of a painter path
32 // is more than 32767. Since we want to avoid clipping by Qt (because it is slow)
33 // we set coordinate limit to less than 32767 / 2
34 const double QgsClipper::MAX_X = 16000;
35 const double QgsClipper::MIN_X = -16000;
36 const double QgsClipper::MAX_Y = 16000;
37 const double QgsClipper::MIN_Y = -16000;
38 
39 const double QgsClipper::SMALL_NUM = 1e-12;
40 
42 {
43  QgsWKBTypes::Type wkbType = wkbPtr.readHeader();
44 
45  int nPoints;
46  wkbPtr >> nPoints;
47 
48  int skipZM = ( QgsWKBTypes::coordDimensions( wkbType ) - 2 ) * sizeof( double );
49 
50  if ( static_cast<int>( nPoints * ( 2 * sizeof( double ) + skipZM ) ) > wkbPtr.remaining() )
51  {
52  QgsDebugMsg( QString( "%1 points exceed wkb length (%2>%3)" ).arg( nPoints ).arg( nPoints * ( 2 * sizeof( double ) + skipZM ) ).arg( wkbPtr.remaining() ) );
53  return QgsConstWkbPtr( nullptr, 0 );
54  }
55 
56  double p0x, p0y, p1x = 0.0, p1y = 0.0; //original coordinates
57  double p1x_c, p1y_c; //clipped end coordinates
58  double lastClipX = 0.0, lastClipY = 0.0; //last successfully clipped coords
59 
60  line.clear();
61  line.reserve( nPoints + 1 );
62 
63  for ( int i = 0; i < nPoints; ++i )
64  {
65  if ( i == 0 )
66  {
67  wkbPtr >> p1x >> p1y;
68  wkbPtr += skipZM;
69  continue;
70  }
71  else
72  {
73  p0x = p1x;
74  p0y = p1y;
75 
76  wkbPtr >> p1x >> p1y;
77  wkbPtr += skipZM;
78 
79  p1x_c = p1x;
80  p1y_c = p1y;
81  if ( clipLineSegment( clipExtent.xMinimum(), clipExtent.xMaximum(), clipExtent.yMinimum(), clipExtent.yMaximum(),
82  p0x, p0y, p1x_c, p1y_c ) )
83  {
84  bool newLine = !line.isEmpty() && ( !qgsDoubleNear( p0x, lastClipX ) || !qgsDoubleNear( p0y, lastClipY ) );
85  if ( newLine )
86  {
87  //add edge points to connect old and new line
88  connectSeparatedLines( lastClipX, lastClipY, p0x, p0y, clipExtent, line );
89  }
90  if ( line.size() < 1 || newLine )
91  {
92  //add first point
93  line << QPointF( p0x, p0y );
94  }
95 
96  //add second point
97  lastClipX = p1x_c;
98  lastClipY = p1y_c;
99  line << QPointF( p1x_c, p1y_c );
100  }
101  }
102  }
103  return wkbPtr;
104 }
105 
106 void QgsClipper::connectSeparatedLines( double x0, double y0, double x1, double y1,
107  const QgsRectangle& clipRect, QPolygonF& pts )
108 {
109  //test the different edge combinations...
110  if ( qgsDoubleNear( x0, clipRect.xMinimum() ) )
111  {
112  if ( qgsDoubleNear( x1, clipRect.xMinimum() ) )
113  {
114  return;
115  }
116  else if ( qgsDoubleNear( y1, clipRect.yMaximum() ) )
117  {
118  pts << QPointF( clipRect.xMinimum(), clipRect.yMaximum() );
119  return;
120  }
121  else if ( qgsDoubleNear( x1, clipRect.xMaximum() ) )
122  {
123  pts << QPointF( clipRect.xMinimum(), clipRect.yMaximum() );
124  pts << QPointF( clipRect.xMaximum(), clipRect.yMaximum() );
125  return;
126  }
127  else if ( qgsDoubleNear( y1, clipRect.yMinimum() ) )
128  {
129  pts << QPointF( clipRect.xMinimum(), clipRect.yMinimum() );
130  return;
131  }
132  }
133  else if ( qgsDoubleNear( y0, clipRect.yMaximum() ) )
134  {
135  if ( qgsDoubleNear( y1, clipRect.yMaximum() ) )
136  {
137  return;
138  }
139  else if ( qgsDoubleNear( x1, clipRect.xMaximum() ) )
140  {
141  pts << QPointF( clipRect.xMaximum(), clipRect.yMaximum() );
142  return;
143  }
144  else if ( qgsDoubleNear( y1, clipRect.yMinimum() ) )
145  {
146  pts << QPointF( clipRect.xMaximum(), clipRect.yMaximum() );
147  pts << QPointF( clipRect.xMaximum(), clipRect.yMinimum() );
148  return;
149  }
150  else if ( qgsDoubleNear( x1, clipRect.xMinimum() ) )
151  {
152  pts << QPointF( clipRect.xMinimum(), clipRect.yMaximum() );
153  return;
154  }
155  }
156  else if ( qgsDoubleNear( x0, clipRect.xMaximum() ) )
157  {
158  if ( qgsDoubleNear( x1, clipRect.xMaximum() ) )
159  {
160  return;
161  }
162  else if ( qgsDoubleNear( y1, clipRect.yMinimum() ) )
163  {
164  pts << QPointF( clipRect.xMaximum(), clipRect.yMinimum() );
165  return;
166  }
167  else if ( qgsDoubleNear( x1, clipRect.xMinimum() ) )
168  {
169  pts << QPointF( clipRect.xMaximum(), clipRect.yMinimum() );
170  pts << QPointF( clipRect.xMinimum(), clipRect.yMinimum() );
171  return;
172  }
173  else if ( qgsDoubleNear( y1, clipRect.yMaximum() ) )
174  {
175  pts << QPointF( clipRect.xMaximum(), clipRect.yMaximum() );
176  return;
177  }
178  }
179  else if ( qgsDoubleNear( y0, clipRect.yMinimum() ) )
180  {
181  if ( qgsDoubleNear( y1, clipRect.yMinimum() ) )
182  {
183  return;
184  }
185  else if ( qgsDoubleNear( x1, clipRect.xMinimum() ) )
186  {
187  pts << QPointF( clipRect.xMinimum(), clipRect.yMinimum() );
188  return;
189  }
190  else if ( qgsDoubleNear( y1, clipRect.yMaximum() ) )
191  {
192  pts << QPointF( clipRect.xMinimum(), clipRect.yMinimum() );
193  pts << QPointF( clipRect.xMinimum(), clipRect.yMaximum() );
194  return;
195  }
196  else if ( qgsDoubleNear( x1, clipRect.xMaximum() ) )
197  {
198  pts << QPointF( clipRect.xMaximum(), clipRect.yMinimum() );
199  return;
200  }
201  }
202 }
static int coordDimensions(Type type)
Returns the coordinate dimension of the geometry type as an integer.
Definition: qgswkbtypes.h:562
static const double MAX_Y
Definition: qgsclipper.h:65
A rectangle specified with double values.
Definition: qgsrectangle.h:35
int remaining() const
Definition: qgswkbptr.h:121
static QgsConstWkbPtr clippedLineWKB(QgsConstWkbPtr wkb, const QgsRectangle &clipExtent, QPolygonF &line)
Reads a polyline from WKB and clips it to clipExtent.
Definition: qgsclipper.cpp:41
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:197
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:352
QgsWKBTypes::Type readHeader() const
Definition: qgswkbptr.cpp:37
static const double MIN_X
Definition: qgsclipper.h:64
void clear()
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:202
double xMaximum() const
Get the x maximum value (right side of rectangle)
Definition: qgsrectangle.h:187
void reserve(int size)
bool isEmpty() const
static const double MIN_Y
Definition: qgsclipper.h:66
static const double MAX_X
Definition: qgsclipper.h:63
int size() const
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:192