Skip to content
Permalink
Browse files
only use one closestSegment method of qgsgeometry
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@5362 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Apr 24, 2006
1 parent 0fadefa commit b1957e6f8f3da5eb8c269dc7a2d5307b4a244026
Showing with 33 additions and 45 deletions.
  1. +16 −3 src/core/qgsgeometry.cpp
  2. +2 −2 src/core/qgsgeometry.h
  3. +15 −40 src/gui/qgsvectorlayer.cpp
@@ -214,7 +214,7 @@ void QgsGeometry::setGeos(geos::Geometry* geos)

}

QgsPoint QgsGeometry::closestVertex(const QgsPoint& point) const
QgsPoint QgsGeometry::closestVertex(const QgsPoint& point, QgsGeometryVertexIndex& atVertex, double& sqrDist) const
{
if(mDirtyWkb)
{
@@ -223,9 +223,10 @@ QgsPoint QgsGeometry::closestVertex(const QgsPoint& point) const

if(mGeometry)
{
int vertexnr;
int vertexcounter = 0;
int wkbType;
double actdist = std::numeric_limits<double>::max();

double x,y;
double *tempx,*tempy;
memcpy(&wkbType, (mGeometry+1), sizeof(int));
@@ -234,6 +235,8 @@ QgsPoint QgsGeometry::closestVertex(const QgsPoint& point) const
case QGis::WKBPoint:
x = *((double *) (mGeometry + 5));
y = *((double *) (mGeometry + 5 + sizeof(double)));
sqrDist = point.sqrDist(x, y);
vertexnr = 0;
break;

case QGis::WKBLineString:
@@ -251,6 +254,7 @@ QgsPoint QgsGeometry::closestVertex(const QgsPoint& point) const
x=*tempx;
y=*tempy;
actdist=point.sqrDist(*tempx,*tempy);
vertexnr = index;
}
ptr+=sizeof(double);
}
@@ -275,8 +279,10 @@ QgsPoint QgsGeometry::closestVertex(const QgsPoint& point) const
x=*tempx;
y=*tempy;
actdist=point.sqrDist(*tempx,*tempy);
vertexnr = vertexcounter;
}
ptr+=sizeof(double);
++vertexcounter;
}
}
}
@@ -296,6 +302,7 @@ QgsPoint QgsGeometry::closestVertex(const QgsPoint& point) const
x=*tempx;
y=*tempy;
actdist=point.sqrDist(*tempx,*tempy);
vertexnr = index;
}
ptr+=(2*sizeof(double));
}
@@ -322,9 +329,10 @@ QgsPoint QgsGeometry::closestVertex(const QgsPoint& point) const
x=*tempx;
y=*tempy;
actdist=point.sqrDist(*tempx,*tempy);
vertexnr = vertexcounter;
}
ptr+=sizeof(double);

++vertexcounter;
}
}
}
@@ -355,8 +363,10 @@ QgsPoint QgsGeometry::closestVertex(const QgsPoint& point) const
x=*tempx;
y=*tempy;
actdist=point.sqrDist(*tempx,*tempy);
vertexnr = vertexcounter;
}
ptr+=sizeof(double);
++vertexcounter;
}
}
}
@@ -366,6 +376,9 @@ QgsPoint QgsGeometry::closestVertex(const QgsPoint& point) const
default:
break;
}
sqrDist = actdist;
atVertex.clear();
atVertex.push_back(vertexnr);
return QgsPoint(x,y);
}
return QgsPoint(0,0);
@@ -83,9 +83,9 @@ class QgsGeometry {
void setGeos(geos::Geometry* geos);

/**
Returns the vertex closest to the given point
Returns the vertex closest to the given point (and also vertex index and squared distance)
*/
QgsPoint closestVertex(const QgsPoint& point) const;
QgsPoint closestVertex(const QgsPoint& point, QgsGeometryVertexIndex& atVertex, double& sqrDist) const;

/** Insert a new vertex before the given vertex index,
* ring and item (first number is index 0)
@@ -2813,7 +2813,8 @@ bool QgsVectorLayer::snapPoint(QgsPoint& point, double tolerance)
double mindisty=point.y();
QgsFeature* fet;
QgsPoint vertexFeature;//the closest vertex of a feature
double minvertexdist;//the distance between 'point' and 'vertexFeature'
QgsGeometryVertexIndex vindex;
double minsquaredist;

QgsRect selectrect(point.x()-tolerance,point.y()-tolerance,point.x()+tolerance,point.y()+tolerance);
selectrect = inverseProjectRect(selectrect);
@@ -2825,18 +2826,17 @@ bool QgsVectorLayer::snapPoint(QgsPoint& point, double tolerance)
{
if(mChangedGeometries.find(fet->featureId()) != mChangedGeometries.end())//if geometry has been changed, use the new geometry
{
vertexFeature = mChangedGeometries[fet->featureId()].closestVertex(point);
vertexFeature = mChangedGeometries[fet->featureId()].closestVertex(point, vindex, minsquaredist);
}
else
{
vertexFeature=fet->geometry()->closestVertex(point);
vertexFeature=fet->geometry()->closestVertex(point, vindex, minsquaredist);
}
minvertexdist=vertexFeature.sqrDist(point.x(),point.y());
if(minvertexdist<mindist)
if(minsquaredist<mindist)
{
mindistx=vertexFeature.x();
mindisty=vertexFeature.y();
mindist=minvertexdist;
mindist=minsquaredist;
}
}

@@ -2845,31 +2845,29 @@ bool QgsVectorLayer::snapPoint(QgsPoint& point, double tolerance)
{
if(mChangedGeometries.find((*iter)->featureId()) != mChangedGeometries.end())//use the changed geometry
{
vertexFeature = mChangedGeometries[(*iter)->featureId()].closestVertex(point);
vertexFeature = mChangedGeometries[(*iter)->featureId()].closestVertex(point, vindex, minsquaredist);
}
else
{
vertexFeature=(*iter)->geometry()->closestVertex(point);
vertexFeature=(*iter)->geometry()->closestVertex(point, vindex, minsquaredist);
}
minvertexdist=vertexFeature.sqrDist(point.x(),point.y());
if(minvertexdist<mindist)
if(minsquaredist<mindist)
{
mindistx=vertexFeature.x();
mindisty=vertexFeature.y();
mindist=minvertexdist;
mindist=minsquaredist;
}
}

//and also go through the changed geometries, because the spatial filter of the provider did not consider feature changes
for(std::map<int, QgsGeometry>::const_iterator iter = mChangedGeometries.begin(); iter != mChangedGeometries.end(); ++iter)
{
vertexFeature = iter->second.closestVertex(point);
minvertexdist=vertexFeature.sqrDist(point.x(),point.y());
if(minvertexdist<mindist)
vertexFeature = iter->second.closestVertex(point, vindex, minsquaredist);
if(minsquaredist<mindist)
{
mindistx=vertexFeature.x();
mindisty=vertexFeature.y();
mindist=minvertexdist;
mindist=minsquaredist;
}
}

@@ -2891,16 +2889,6 @@ bool QgsVectorLayer::snapVertexWithContext(QgsPoint& point,

QgsPoint origPoint = point;

#ifdef QGISDEBUG
std::cout << "QgsVectorLayer::snapVertexWithContext: Entering."
<< "." << std::endl;
#endif

#ifdef QGISDEBUG
std::cout << "QgsVectorLayer::snapVertexWithContext: Tolerance: " << tolerance << ", dataProvider = '" << dataProvider
<< "'." << std::endl;
#endif

// Sanity checking
if ( tolerance<=0 ||
!dataProvider)
@@ -2938,10 +2926,7 @@ bool QgsVectorLayer::snapVertexWithContext(QgsPoint& point,
feature->setGeometry( mChangedGeometries[ feature->featureId() ] );
}

minDistSegPoint = feature->geometry()->closestVertexWithContext(origPoint,
atVertexTemp,
testSqrDist);

minDistSegPoint = feature->geometry()->closestVertex(origPoint, atVertexTemp, testSqrDist);
if (testSqrDist < minSqrDist)
{
point = minDistSegPoint;
@@ -2951,13 +2936,6 @@ bool QgsVectorLayer::snapVertexWithContext(QgsPoint& point,
snappedFeatureId = feature->featureId();
snappedGeometry = *(feature->geometry());
vertexFound = true;

#ifdef QGISDEBUG
std::cout << "QgsVectorLayer::snapVertexWithContext: minSqrDist reduced to: " << minSqrDist
// << " and beforeVertex " << beforeVertex
<< "." << std::endl;
#endif

}
}

@@ -2972,10 +2950,7 @@ bool QgsVectorLayer::snapVertexWithContext(QgsPoint& point,
iter != mAddedFeatures.end();
++iter)
{
minDistSegPoint = (*iter)->geometry()->closestVertexWithContext(origPoint,
atVertexTemp,
testSqrDist);

minDistSegPoint = (*iter)->geometry()->closestVertex(origPoint, atVertexTemp, testSqrDist);
if (testSqrDist < minSqrDist)
{
point = minDistSegPoint;

0 comments on commit b1957e6

Please sign in to comment.