RVector RSnapIntersection::snap( const RVector& position, RGraphicsView& view, const QMap<REntity::Id, QSet<int> >& candidates, const RBox& queryBox) { RDocument* document = view.getDocument(); if (document==NULL) { return lastSnap; } REntity::Id entityId1 = REntity::INVALID_ID; REntity::Id entityId2 = REntity::INVALID_ID; lastSnap = RVector::invalid; double minDist = RMAXDOUBLE; double dist; QMap<REntity::Id, QSet<int> >::const_iterator it1; for (it1=candidates.begin(); it1!=candidates.end(); it1++) { if (RMouseEvent::hasMouseMoved()) { lastSnap = RVector::invalid; return RVector::invalid; } QSharedPointer<REntity> e1 = document->queryEntityDirect(it1.key()); if (e1.isNull()) { continue; } if (e1->getType()==RS::EntityText || e1->getType()==RS::EntityAttribute || e1->getType()==RS::EntityAttributeDefinition) { continue; } QMap<REntity::Id, QSet<int> >::const_iterator it2; for (it2=it1; it2!=candidates.end(); it2++) { if (RMouseEvent::hasMouseMoved()) { lastSnap = RVector::invalid; return RVector::invalid; } QSharedPointer<REntity> e2 = document->queryEntityDirect(it2.key()); if (e2.isNull()) { continue; } if (e2->getType()==RS::EntityText || e2->getType()==RS::EntityAttribute || e2->getType()==RS::EntityAttributeDefinition) { continue; } QList<RVector> candidates = e1->getIntersectionPoints(*e2, true, queryBox); if (candidates.isEmpty()) { continue; } RVector candidate = position.getClosest(candidates); dist = candidate.getDistanceTo(position); if (dist<minDist) { lastSnap = candidate; minDist = dist; entityId1 = e1->getId(); entityId2 = e2->getId(); } } } if (!lastSnap.isValid()) { lastSnap = position; lastSnap.valid = false; return lastSnap; } else { if (entityId1!=REntity::INVALID_ID) { entityIds.insert(entityId1); } if (entityId2!=REntity::INVALID_ID) { entityIds.insert(entityId2); } return lastSnap; } }
/** * \return Shortest vector from any end point of this shape * to the given point. */ RVector RShape::getVectorFromEndpointTo(const RVector& point) const { QList<RVector> endPoints = getEndPoints(); RVector closest = point.getClosest(endPoints); return point - closest; }