QGIS API Documentation  2.11.0-Master
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 
23 // Where has all the code gone?
24 
25 // It's been inlined, so its in the qgsclipper.h file.
26 
27 // But the static members must be initialised outside the class! (or GCC 4 dies)
28 
29 // Qt also does clipping when the coordinates go over +/- 32767
30 // moreover from Qt 4.6, Qt clips also when the width/height of a painter path
31 // is more than 32767. Since we want to avoid clipping by Qt (because it is slow)
32 // we set coordinate limit to less than 32767 / 2
33 const double QgsClipper::MAX_X = 16000;
34 const double QgsClipper::MIN_X = -16000;
35 const double QgsClipper::MAX_Y = 16000;
36 const double QgsClipper::MIN_Y = -16000;
37 
38 const double QgsClipper::SMALL_NUM = 1e-12;
39 
40 const unsigned char* QgsClipper::clippedLineWKB( const unsigned char* wkb, const QgsRectangle& clipExtent, QPolygonF& line )
41 {
42  QgsConstWkbPtr wkbPtr( wkb + 1 );
43 
44  unsigned int wkbType, nPoints;
45 
46  wkbPtr >> wkbType >> nPoints;
47 
48  bool hasZValue = ( wkbType == QGis::WKBLineString25D );
49 
50  double p0x, p0y, p1x = 0.0, p1y = 0.0; //original coordinates
51  double p1x_c, p1y_c; //clipped end coordinates
52  double lastClipX = 0.0, lastClipY = 0.0; //last successfully clipped coords
53 
54  line.clear();
55  line.reserve( nPoints + 1 );
56 
57  for ( unsigned int i = 0; i < nPoints; ++i )
58  {
59  if ( i == 0 )
60  {
61  wkbPtr >> p1x >> p1y;
62  if ( hasZValue )
63  wkbPtr += sizeof( double );
64 
65  continue;
66  }
67  else
68  {
69  p0x = p1x;
70  p0y = p1y;
71 
72  wkbPtr >> p1x >> p1y;
73  if ( hasZValue )
74  wkbPtr += sizeof( double );
75 
76  p1x_c = p1x; p1y_c = p1y;
77  if ( clipLineSegment( clipExtent.xMinimum(), clipExtent.xMaximum(), clipExtent.yMinimum(), clipExtent.yMaximum(),
78  p0x, p0y, p1x_c, p1y_c ) )
79  {
80  bool newLine = line.size() > 0 && ( p0x != lastClipX || p0y != lastClipY );
81  if ( newLine )
82  {
83  //add edge points to connect old and new line
84  connectSeparatedLines( lastClipX, lastClipY, p0x, p0y, clipExtent, line );
85  }
86  if ( line.size() < 1 || newLine )
87  {
88  //add first point
89  line << QPointF( p0x, p0y );
90  }
91 
92  //add second point
93  lastClipX = p1x_c; lastClipY = p1y_c;
94  line << QPointF( p1x_c, p1y_c );
95  }
96  }
97  }
98  return wkbPtr;
99 }
100 
101 void QgsClipper::connectSeparatedLines( double x0, double y0, double x1, double y1,
102  const QgsRectangle& clipRect, QPolygonF& pts )
103 {
104  //test the different edge combinations...
105  if ( qgsDoubleNear( x0, clipRect.xMinimum() ) )
106  {
107  if ( qgsDoubleNear( x1, clipRect.xMinimum() ) )
108  {
109  return;
110  }
111  else if ( qgsDoubleNear( y1, clipRect.yMaximum() ) )
112  {
113  pts << QPointF( clipRect.xMinimum(), clipRect.yMaximum() );
114  return;
115  }
116  else if ( qgsDoubleNear( x1, clipRect.xMaximum() ) )
117  {
118  pts << QPointF( clipRect.xMinimum(), clipRect.yMaximum() );
119  pts << QPointF( clipRect.xMaximum(), clipRect.yMaximum() );
120  return;
121  }
122  else if ( qgsDoubleNear( y1, clipRect.yMinimum() ) )
123  {
124  pts << QPointF( clipRect.xMinimum(), clipRect.yMinimum() );
125  return;
126  }
127  }
128  else if ( qgsDoubleNear( y0, clipRect.yMaximum() ) )
129  {
130  if ( qgsDoubleNear( y1, clipRect.yMaximum() ) )
131  {
132  return;
133  }
134  else if ( qgsDoubleNear( x1, clipRect.xMaximum() ) )
135  {
136  pts << QPointF( clipRect.xMaximum(), clipRect.yMaximum() );
137  return;
138  }
139  else if ( qgsDoubleNear( y1, clipRect.yMinimum() ) )
140  {
141  pts << QPointF( clipRect.xMaximum(), clipRect.yMaximum() );
142  pts << QPointF( clipRect.xMaximum(), clipRect.yMinimum() );
143  return;
144  }
145  else if ( qgsDoubleNear( x1, clipRect.xMinimum() ) )
146  {
147  pts << QPointF( clipRect.xMinimum(), clipRect.yMaximum() );
148  return;
149  }
150  }
151  else if ( qgsDoubleNear( x0, clipRect.xMaximum() ) )
152  {
153  if ( qgsDoubleNear( x1, clipRect.xMaximum() ) )
154  {
155  return;
156  }
157  else if ( qgsDoubleNear( y1, clipRect.yMinimum() ) )
158  {
159  pts << QPointF( clipRect.xMaximum(), clipRect.yMinimum() );
160  return;
161  }
162  else if ( qgsDoubleNear( x1, clipRect.xMinimum() ) )
163  {
164  pts << QPointF( clipRect.xMaximum(), clipRect.yMinimum() );
165  pts << QPointF( clipRect.xMinimum(), clipRect.yMinimum() );
166  return;
167  }
168  else if ( qgsDoubleNear( y1, clipRect.yMaximum() ) )
169  {
170  pts << QPointF( clipRect.xMaximum(), clipRect.yMaximum() );
171  return;
172  }
173  }
174  else if ( qgsDoubleNear( y0, clipRect.yMinimum() ) )
175  {
176  if ( qgsDoubleNear( y1, clipRect.yMinimum() ) )
177  {
178  return;
179  }
180  else if ( qgsDoubleNear( x1, clipRect.xMinimum() ) )
181  {
182  pts << QPointF( clipRect.xMinimum(), clipRect.yMinimum() );
183  return;
184  }
185  else if ( qgsDoubleNear( y1, clipRect.yMaximum() ) )
186  {
187  pts << QPointF( clipRect.xMinimum(), clipRect.yMinimum() );
188  pts << QPointF( clipRect.xMinimum(), clipRect.yMaximum() );
189  return;
190  }
191  else if ( qgsDoubleNear( x1, clipRect.xMaximum() ) )
192  {
193  pts << QPointF( clipRect.xMaximum(), clipRect.yMinimum() );
194  return;
195  }
196  }
197 }
static const double MAX_Y
Definition: qgsclipper.h:64
A rectangle specified with double values.
Definition: qgsrectangle.h:35
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:192
static const unsigned char * clippedLineWKB(const unsigned char *wkb, const QgsRectangle &clipExtent, QPolygonF &line)
Reads a polyline from WKB and clips it to clipExtent.
Definition: qgsclipper.cpp:40
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Definition: qgis.h:350
static const double MIN_X
Definition: qgsclipper.h:63
void clear()
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:197
double xMaximum() const
Get the x maximum value (right side of rectangle)
Definition: qgsrectangle.h:182
void reserve(int size)
static const double MIN_Y
Definition: qgsclipper.h:65
static const double MAX_X
Definition: qgsclipper.h:62
int size() const
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:187