Skip to content

Commit

Permalink
only use one closestSegment method of qgsgeometry
Browse files Browse the repository at this point in the history
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 b1957e6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 45 deletions.
19 changes: 16 additions & 3 deletions src/core/qgsgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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));
Expand All @@ -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:
Expand All @@ -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);
}
Expand All @@ -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;
}
}
}
Expand All @@ -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));
}
Expand All @@ -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;
}
}
}
Expand Down Expand Up @@ -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;
}
}
}
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 15 additions & 40 deletions src/gui/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}

Expand All @@ -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;
}
}

Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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

}
}

Expand All @@ -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;
Expand Down

0 comments on commit b1957e6

Please sign in to comment.