Path: blob/main/src/netedit/elements/moving/GNEMoveElementVehicle.cpp
185790 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file GNEMoveElementVehicle.cpp14/// @author Pablo Alvarez Lopez15/// @date Oct 202516///17// Class used for elements that can be moved over a edge with two positions18/****************************************************************************/19#include <config.h>2021#include <foreign/fontstash/fontstash.h>22#include <netedit/changes/GNEChange_Attribute.h>23#include <netedit/changes/GNEChange_Connection.h>24#include <netedit/elements/demand/GNEVehicle.h>25#include <netedit/elements/GNEAttributeCarrier.h>26#include <netedit/elements/moving/GNEMoveElement.h>27#include <netedit/elements/network/GNEConnection.h>28#include <netedit/frames/common/GNEMoveFrame.h>29#include <netedit/GNENet.h>30#include <netedit/GNETagProperties.h>31#include <netedit/GNEUndoList.h>32#include <netedit/GNEViewNet.h>33#include <netedit/GNEViewParent.h>34#include <utils/gui/div/GLHelper.h>35#include <utils/gui/div/GUIGlobalViewObjectsHandler.h>36#include <utils/gui/globjects/GLIncludes.h>37#include <utils/gui/globjects/GUIGlObject.h>38#include <utils/options/OptionsCont.h>39#include <utils/vehicle/SUMORouteHandler.h>40#include <utils/xml/NamespaceIDs.h>4142#include "GNEMoveElementVehicle.h"4344// ===========================================================================45// static definitions46// ===========================================================================4748const double GNEMoveElementVehicle::arrivalPositionDiameter = SUMO_const_halfLaneWidth * 0.5;4950// ===========================================================================51// member method definitions52// ===========================================================================5354GNEMoveElementVehicle::GNEMoveElementVehicle(GNEVehicle* vehicle, GNEEdge* fromEdge, GNEEdge* toEdge) :55GNEMoveElement(vehicle),56myVehicle(vehicle) {57// set parents58vehicle->getHierarchicalElement()->setParents<GNEEdge*>({fromEdge, toEdge});59}606162GNEMoveElementVehicle::~GNEMoveElementVehicle() {}636465GNEMoveOperation*66GNEMoveElementVehicle::getMoveOperation() {67// get first and last lanes68const GNELane* firstLane = myVehicle->getFirstPathLane();69const GNELane* lastLane = myVehicle->getLastPathLane();70// check both lanes71if (firstLane && lastLane) {72// get depart and arrival positions (doubles)73const double startPosDouble = myVehicle->getAttributeDouble(SUMO_ATTR_DEPARTPOS);74const double endPosDouble = (myVehicle->getAttributeDouble(SUMO_ATTR_ARRIVALPOS) < 0) ? lastLane->getLaneShape().length2D() : myVehicle->getAttributeDouble(SUMO_ATTR_ARRIVALPOS);75// check if allow change lane76const bool allowChangeLane = myVehicle->getNet()->getViewParent()->getMoveFrame()->getCommonMoveOptions()->getAllowChangeLane();77// obtain diameter78const double diameter = myVehicle->getAttributeDouble(SUMO_ATTR_WIDTH) > myVehicle->getAttributeDouble(SUMO_ATTR_LENGTH) ? myVehicle->getAttributeDouble(SUMO_ATTR_WIDTH) : myVehicle->getAttributeDouble(SUMO_ATTR_LENGTH);79// return move operation depending if we're editing departPos or arrivalPos80if (myVehicle->getNet()->getViewNet()->getPositionInformation().distanceSquaredTo2D(myVehicle->getAttributePosition(GNE_ATTR_PLAN_GEOMETRY_STARTPOS)) < (diameter * diameter)) {81return new GNEMoveOperation(this, firstLane, startPosDouble, lastLane, INVALID_DOUBLE, true, allowChangeLane);82} else if (myVehicle->getNet()->getViewNet()->getPositionInformation().distanceSquaredTo2D(myVehicle->getAttributePosition(GNE_ATTR_PLAN_GEOMETRY_ENDPOS)) < (arrivalPositionDiameter * arrivalPositionDiameter)) {83return new GNEMoveOperation(this, firstLane, INVALID_DOUBLE, lastLane, endPosDouble, false, allowChangeLane);84}85}86// nothing to move87return nullptr;88}899091std::string92GNEMoveElementVehicle::getMovingAttribute(SumoXMLAttr key) const {93return myMovedElement->getCommonAttribute(key);94}959697double98GNEMoveElementVehicle::getMovingAttributeDouble(SumoXMLAttr key) const {99return myMovedElement->getCommonAttributeDouble(key);100}101102103Position104GNEMoveElementVehicle::getMovingAttributePosition(SumoXMLAttr key) const {105return myMovedElement->getCommonAttributePosition(key);106}107108109PositionVector110GNEMoveElementVehicle::getMovingAttributePositionVector(SumoXMLAttr key) const {111return myMovedElement->getCommonAttributePositionVector(key);112}113114115void116GNEMoveElementVehicle::setMovingAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {117myMovedElement->setCommonAttribute(key, value, undoList);118}119120121bool122GNEMoveElementVehicle::isMovingAttributeValid(SumoXMLAttr key, const std::string& value) const {123return myMovedElement->isCommonAttributeValid(key, value);124}125126127void128GNEMoveElementVehicle::setMovingAttribute(SumoXMLAttr key, const std::string& value) {129myMovedElement->setCommonAttribute(key, value);130}131132133void134GNEMoveElementVehicle::removeGeometryPoint(const Position /*clickedPosition*/, GNEUndoList* /*undoList*/) {135// nothing to do here136}137138139void140GNEMoveElementVehicle::setMoveShape(const GNEMoveResult& moveResult) {141if (moveResult.newFirstPos != INVALID_DOUBLE) {142// change depart143myVehicle->departPosProcedure = DepartPosDefinition::GIVEN;144myVehicle->parametersSet |= VEHPARS_DEPARTPOS_SET;145myVehicle->departPos = moveResult.newFirstPos;146// check if depart lane has to be changed147if (moveResult.newFirstLane) {148// set new depart lane149std::string error = "";150myVehicle->parseDepartLane(moveResult.newFirstLane->getID(), myVehicle->getTagStr(), myVehicle->getID(), myVehicle->departLane, myVehicle->departLaneProcedure, error);151// mark parameter as set152myVehicle->parametersSet |= VEHPARS_DEPARTLANE_SET;153}154} else if (moveResult.newLastPos != INVALID_DOUBLE) {155// change arrival156myVehicle->arrivalPosProcedure = ArrivalPosDefinition::GIVEN;157myVehicle->parametersSet |= VEHPARS_ARRIVALPOS_SET;158myVehicle->arrivalPos = moveResult.newFirstPos;159// check if arrival lane has to be changed160if (moveResult.newLastLane) {161// set new arrival lane162std::string error = "";163myVehicle->parseArrivalLane(moveResult.newLastLane->getID(), myVehicle->getTagStr(), myVehicle->getID(), myVehicle->departLane, myVehicle->arrivalLaneProcedure, error);164// mark parameter as set165myVehicle->parametersSet |= VEHPARS_ARRIVALLANE_SET;166}167}168// set lateral offset169myMovingLateralOffset = moveResult.firstLaneOffset;170// update geometry171myVehicle->updateGeometry();172}173174175void176GNEMoveElementVehicle::commitMoveShape(const GNEMoveResult& moveResult, GNEUndoList* undoList) {177// reset lateral offset178myMovingLateralOffset = 0;179// check value180if (moveResult.newFirstPos != INVALID_DOUBLE) {181// begin change attribute182undoList->begin(myVehicle, TLF("departPos of %", myVehicle->getTagStr()));183// now set departPos184myVehicle->setAttribute(SUMO_ATTR_DEPARTPOS, toString(moveResult.newFirstPos), undoList);185// check if depart lane has to be changed186if (moveResult.newFirstLane) {187// set new depart lane188myVehicle->setAttribute(SUMO_ATTR_DEPARTLANE, toString(moveResult.newFirstLane->getIndex()), undoList);189}190} else if (moveResult.newLastPos != INVALID_DOUBLE) {191// begin change attribute192undoList->begin(myVehicle, TLF("arrivalPos of %", myVehicle->getTagStr()));193// now set arrivalPos194myVehicle->setAttribute(SUMO_ATTR_ARRIVALPOS, toString(moveResult.newLastPos), undoList);195// check if arrival lane has to be changed196if (moveResult.newLastLane) {197// set new arrival lane198myVehicle->setAttribute(SUMO_ATTR_ARRIVALLANE, toString(moveResult.newLastLane->getIndex()), undoList);199}200}201// end change attribute202undoList->end();203}204205/****************************************************************************/206207208