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