@@ -40,7 +40,8 @@ QgsLineStringV2 *QgsLineStringV2::clone() const
40
40
41
41
void QgsLineStringV2::clear ()
42
42
{
43
- mCoords .clear ();
43
+ mX .clear ();
44
+ mY .clear ();
44
45
mZ .clear ();
45
46
mM .clear ();
46
47
mWkbType = QgsWKBTypes::Unknown;
@@ -149,12 +150,12 @@ QString QgsLineStringV2::asJSON( int precision ) const
149
150
double QgsLineStringV2::length () const
150
151
{
151
152
double length = 0 ;
152
- int size = mCoords .size ();
153
+ int size = mX .size ();
153
154
double dx, dy;
154
155
for ( int i = 1 ; i < size; ++i )
155
156
{
156
- dx = mCoords [i]. x ( ) - mCoords [ i - 1 ]. x ( );
157
- dy = mCoords [i]. y ( ) - mCoords [ i - 1 ]. y ( );
157
+ dx = mX . at ( i ) - mX . at ( i - 1 );
158
+ dy = mY . at ( i ) - mY . at ( i - 1 );
158
159
length += sqrt ( dx * dx + dy * dy );
159
160
}
160
161
return length;
@@ -185,17 +186,18 @@ QgsLineStringV2* QgsLineStringV2::curveToLine() const
185
186
186
187
int QgsLineStringV2::numPoints () const
187
188
{
188
- return mCoords .size ();
189
+ return mX .size ();
189
190
}
190
191
191
192
QgsPointV2 QgsLineStringV2::pointN ( int i ) const
192
193
{
193
- if ( mCoords .size () <= i )
194
+ if ( mX .size () <= i )
194
195
{
195
196
return QgsPointV2 ();
196
197
}
197
198
198
- const QPointF& pt = mCoords .at ( i );
199
+ double x = mX .at ( i );
200
+ double y = mY .at ( i );
199
201
double z = 0 ;
200
202
double m = 0 ;
201
203
@@ -223,7 +225,7 @@ QgsPointV2 QgsLineStringV2::pointN( int i ) const
223
225
{
224
226
t = QgsWKBTypes::PointM;
225
227
}
226
- return QgsPointV2 ( t, pt. x (), pt. y () , z, m );
228
+ return QgsPointV2 ( t, x, y , z, m );
227
229
}
228
230
229
231
void QgsLineStringV2::points ( QList<QgsPointV2>& pts ) const
@@ -241,7 +243,8 @@ void QgsLineStringV2::setPoints( const QList<QgsPointV2>& points )
241
243
if ( points.size () < 1 )
242
244
{
243
245
mWkbType = QgsWKBTypes::Unknown;
244
- mCoords .clear ();
246
+ mX .clear ();
247
+ mY .clear ();
245
248
mZ .clear ();
246
249
mM .clear ();
247
250
return ;
@@ -254,7 +257,8 @@ void QgsLineStringV2::setPoints( const QList<QgsPointV2>& points )
254
257
255
258
setZMTypeFromSubGeometry ( &firstPt, QgsWKBTypes::LineString );
256
259
257
- mCoords .resize ( points.size () );
260
+ mX .resize ( points.size () );
261
+ mY .resize ( points.size () );
258
262
if ( hasZ )
259
263
{
260
264
mZ .resize ( points.size () );
@@ -274,15 +278,15 @@ void QgsLineStringV2::setPoints( const QList<QgsPointV2>& points )
274
278
275
279
for ( int i = 0 ; i < points.size (); ++i )
276
280
{
277
- mCoords [i]. rx () = points[i] .x ();
278
- mCoords [i]. ry () = points[i] .y ();
281
+ mX [i] = points. at ( i ) .x ();
282
+ mY [i] = points. at ( i ) .y ();
279
283
if ( hasZ )
280
284
{
281
- mZ [i] = points[i] .z ();
285
+ mZ [i] = points. at ( i ) .z ();
282
286
}
283
287
if ( hasM )
284
288
{
285
- mM [i] = points[i] .m ();
289
+ mM [i] = points. at ( i ) .m ();
286
290
}
287
291
}
288
292
}
@@ -299,14 +303,15 @@ void QgsLineStringV2::append( const QgsLineStringV2* line )
299
303
setZMTypeFromSubGeometry ( line, QgsWKBTypes::LineString );
300
304
}
301
305
302
- mCoords += line->mCoords ;
306
+ mX += line->mX ;
307
+ mY += line->mY ;
303
308
mZ += line->mZ ;
304
309
mM += line->mM ;
305
310
}
306
311
307
312
void QgsLineStringV2::draw ( QPainter& p ) const
308
313
{
309
- p.drawPolyline ( mCoords );
314
+ p.drawPolyline ( qPolygonF () );
310
315
}
311
316
312
317
void QgsLineStringV2::addToPainterPath ( QPainterPath& path ) const
@@ -317,39 +322,73 @@ void QgsLineStringV2::addToPainterPath( QPainterPath& path ) const
317
322
return ;
318
323
}
319
324
320
- if ( path.isEmpty () || path.currentPosition () != mCoords [ 0 ] )
325
+ if ( path.isEmpty () || path.currentPosition () != QPointF ( mX . at ( 0 ), mY . at ( 0 ) ) )
321
326
{
322
- path.moveTo ( mCoords [ 0 ] );
327
+ path.moveTo ( mX . at ( 0 ), mY . at ( 0 ) );
323
328
}
324
329
325
330
for ( int i = 1 ; i < nPoints; ++i )
326
331
{
327
- path.lineTo ( mCoords [i] );
332
+ path.lineTo ( mX . at ( i ), mY . at ( i ) );
328
333
}
329
334
}
330
335
331
336
void QgsLineStringV2::drawAsPolygon ( QPainter& p ) const
332
337
{
333
- p.drawPolygon ( mCoords );
338
+ p.drawPolygon ( qPolygonF () );
339
+ }
340
+
341
+ QPolygonF QgsLineStringV2::qPolygonF () const
342
+ {
343
+ QPolygonF points;
344
+ for ( int i = 0 ; i < mX .count (); ++i )
345
+ {
346
+ points << QPointF ( mX .at ( i ), mY .at ( i ) );
347
+ }
348
+ return points;
334
349
}
335
350
336
351
void QgsLineStringV2::transform ( const QgsCoordinateTransform& ct, QgsCoordinateTransform::TransformDirection d )
337
352
{
338
- ct.transformPolygon ( mCoords , d );
353
+ double * zArray = mZ .data ();
354
+
355
+ bool hasZ = is3D ();
356
+ int nPoints = numPoints ();
357
+ if ( !hasZ )
358
+ {
359
+ zArray = new double [nPoints];
360
+ for ( int i = 0 ; i < nPoints; ++i )
361
+ {
362
+ zArray[i] = 0 ;
363
+ }
364
+ }
365
+ ct.transformCoords ( nPoints, mX .data (), mY .data (), zArray, d );
366
+ if ( !hasZ )
367
+ {
368
+ delete[] zArray;
369
+ }
370
+
339
371
}
340
372
341
373
void QgsLineStringV2::transform ( const QTransform& t )
342
374
{
343
- mCoords = t.map ( mCoords );
375
+ int nPoints = numPoints ();
376
+ for ( int i = 0 ; i < nPoints; ++i )
377
+ {
378
+ qreal x, y;
379
+ t.map ( mX .at ( i ), mY .at ( i ), &x, &y );
380
+ mX [i] = x; mY [i] = y;
381
+ }
344
382
}
345
383
346
384
bool QgsLineStringV2::insertVertex ( const QgsVertexId& position, const QgsPointV2& vertex )
347
385
{
348
- if ( position.vertex < 0 || position.vertex > mCoords .size () )
386
+ if ( position.vertex < 0 || position.vertex > mX .size () )
349
387
{
350
388
return false ;
351
389
}
352
- mCoords .insert ( position.vertex , QPointF ( vertex.x (), vertex.y () ) );
390
+ mX .insert ( position.vertex , vertex.x () );
391
+ mY .insert ( position.vertex , vertex.y () );
353
392
if ( is3D () )
354
393
{
355
394
mZ .insert ( position.vertex , vertex.z () );
@@ -364,12 +403,12 @@ bool QgsLineStringV2::insertVertex( const QgsVertexId& position, const QgsPointV
364
403
365
404
bool QgsLineStringV2::moveVertex ( const QgsVertexId& position, const QgsPointV2& newPos )
366
405
{
367
- if ( position.vertex < 0 || position.vertex >= mCoords .size () )
406
+ if ( position.vertex < 0 || position.vertex >= mX .size () )
368
407
{
369
408
return false ;
370
409
}
371
- mCoords [position.vertex ]. rx () = newPos.x ();
372
- mCoords [position.vertex ]. ry () = newPos.y ();
410
+ mX [position.vertex ] = newPos.x ();
411
+ mY [position.vertex ] = newPos.y ();
373
412
if ( is3D () && newPos.is3D () )
374
413
{
375
414
mZ [position.vertex ] = newPos.z ();
@@ -384,12 +423,13 @@ bool QgsLineStringV2::moveVertex( const QgsVertexId& position, const QgsPointV2&
384
423
385
424
bool QgsLineStringV2::deleteVertex ( const QgsVertexId& position )
386
425
{
387
- if ( position.vertex >= mCoords .size () || position.vertex < 0 )
426
+ if ( position.vertex >= mX .size () || position.vertex < 0 )
388
427
{
389
428
return false ;
390
429
}
391
430
392
- mCoords .remove ( position.vertex );
431
+ mX .remove ( position.vertex );
432
+ mY .remove ( position.vertex );
393
433
if ( is3D () )
394
434
{
395
435
mZ .remove ( position.vertex );
@@ -409,7 +449,8 @@ void QgsLineStringV2::addVertex( const QgsPointV2& pt )
409
449
setZMTypeFromSubGeometry ( &pt, QgsWKBTypes::LineString );
410
450
}
411
451
412
- mCoords .append ( QPointF ( pt.x (), pt.y () ) );
452
+ mX .append ( pt.x () );
453
+ mY .append ( pt.y () );
413
454
if ( is3D () )
414
455
{
415
456
mZ .append ( pt.z () );
@@ -427,20 +468,22 @@ double QgsLineStringV2::closestSegment( const QgsPointV2& pt, QgsPointV2& segmen
427
468
double testDist = 0 ;
428
469
double segmentPtX, segmentPtY;
429
470
430
- int size = mCoords .size ();
471
+ int size = mX .size ();
431
472
for ( int i = 1 ; i < size; ++i )
432
473
{
433
- const QPointF& prev = mCoords .at ( i - 1 );
434
- const QPointF& currentPt = mCoords .at ( i );
435
- testDist = QgsGeometryUtils::sqrDistToLine ( pt.x (), pt.y (), prev.x (), prev.y (), currentPt.x (), currentPt.y (), segmentPtX, segmentPtY, epsilon );
474
+ double prevX = mX .at ( i - 1 );
475
+ double prevY = mY .at ( i - 1 );
476
+ double currentX = mX .at ( i );
477
+ double currentY = mY .at ( i );
478
+ testDist = QgsGeometryUtils::sqrDistToLine ( pt.x (), pt.y (), prevX, prevY, currentX, currentY, segmentPtX, segmentPtY, epsilon );
436
479
if ( testDist < sqrDist )
437
480
{
438
481
sqrDist = testDist;
439
482
segmentPt.setX ( segmentPtX );
440
483
segmentPt.setY ( segmentPtY );
441
484
if ( leftOf )
442
485
{
443
- *leftOf = ( QgsGeometryUtils::leftOfLine ( segmentPtX, segmentPtY, prev. x (), prev. y () , pt.x (), pt.y () ) < 0 );
486
+ *leftOf = ( QgsGeometryUtils::leftOfLine ( segmentPtX, segmentPtY, prevX, prevY , pt.x (), pt.y () ) < 0 );
444
487
}
445
488
vertexAfter.part = 0 ; vertexAfter.ring = 0 ; vertexAfter.vertex = i;
446
489
}
@@ -464,7 +507,7 @@ void QgsLineStringV2::sumUpArea( double& sum ) const
464
507
int maxIndex = numPoints () - 1 ;
465
508
for ( int i = 0 ; i < maxIndex; ++i )
466
509
{
467
- sum += 0.5 * ( mCoords [i]. x ( ) * mCoords [i+ 1 ]. y ( ) - mCoords [i]. y ( ) * mCoords [i+ 1 ]. x ( ) );
510
+ sum += 0.5 * ( mX . at ( i ) * mY . at ( i + 1 ) - mY . at ( i ) * mX . at ( i + 1 ) );
468
511
}
469
512
}
470
513
@@ -474,13 +517,14 @@ void QgsLineStringV2::importVerticesFromWkb( const QgsConstWkbPtr& wkb )
474
517
bool hasM = isMeasure ();
475
518
int nVertices = 0 ;
476
519
wkb >> nVertices;
477
- mCoords .resize ( nVertices );
520
+ mX .resize ( nVertices );
521
+ mY .resize ( nVertices );
478
522
hasZ ? mZ .resize ( nVertices ) : mZ .clear ();
479
523
hasM ? mM .resize ( nVertices ) : mM .clear ();
480
524
for ( int i = 0 ; i < nVertices; ++i )
481
525
{
482
- wkb >> mCoords [i]. rx () ;
483
- wkb >> mCoords [i]. ry () ;
526
+ wkb >> mX [i];
527
+ wkb >> mY [i];
484
528
if ( hasZ )
485
529
{
486
530
wkb >> mZ [i];
@@ -507,28 +551,34 @@ double QgsLineStringV2::vertexAngle( const QgsVertexId& vertex ) const
507
551
{
508
552
if ( isClosed () )
509
553
{
510
- QPointF previous = mCoords [numPoints () - 1 ];
511
- QPointF current = mCoords [0 ];
512
- QPointF after = mCoords [1 ];
513
- return QgsGeometryUtils::averageAngle ( previous.x (), previous.y (), current.x (), current.y (), after.x (), after.y () );
554
+ double previousX = mX .at ( numPoints () - 1 );
555
+ double previousY = mY .at ( numPoints () - 1 );
556
+ double currentX = mX .at ( 0 );
557
+ double currentY = mY .at ( 0 );
558
+ double afterX = mX .at ( 1 );
559
+ double afterY = mY .at ( 1 );
560
+ return QgsGeometryUtils::averageAngle ( previousX, previousY, currentX, currentY, afterX, afterY );
514
561
}
515
562
else if ( vertex.vertex == 0 )
516
563
{
517
- return QgsGeometryUtils::linePerpendicularAngle ( mCoords [ 0 ]. x ( ), mCoords [ 0 ]. y ( ), mCoords [ 1 ]. x ( ), mCoords [ 1 ]. y ( ) );
564
+ return QgsGeometryUtils::linePerpendicularAngle ( mX . at ( 0 ), mY . at ( 0 ), mX . at ( 1 ), mY . at ( 1 ) );
518
565
}
519
566
else
520
567
{
521
568
int a = numPoints () - 2 ;
522
569
int b = numPoints () - 1 ;
523
- return QgsGeometryUtils::linePerpendicularAngle ( mCoords [a]. x ( ), mCoords [a]. y ( ), mCoords [b]. x ( ), mCoords [b]. y ( ) );
570
+ return QgsGeometryUtils::linePerpendicularAngle ( mX . at ( a ), mY . at ( a ), mX . at ( b ), mY . at ( b ) );
524
571
}
525
572
}
526
573
else
527
574
{
528
- QPointF previous = mCoords [vertex.vertex - 1 ];
529
- QPointF current = mCoords [vertex.vertex ];
530
- QPointF after = mCoords [vertex.vertex + 1 ];
531
- return QgsGeometryUtils::averageAngle ( previous.x (), previous.y (), current.x (), current.y (), after.x (), after.y () );
575
+ double previousX = mX .at ( vertex.vertex - 1 );
576
+ double previousY = mY .at ( vertex.vertex - 1 );
577
+ double currentX = mX .at ( vertex.vertex );
578
+ double currentY = mY .at ( vertex.vertex );
579
+ double afterX = mX .at ( vertex.vertex + 1 );
580
+ double afterY = mY .at ( vertex.vertex + 1 );
581
+ return QgsGeometryUtils::averageAngle ( previousX, previousY, currentX, currentY, afterX, afterY );
532
582
}
533
583
}
534
584
0 commit comments