Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GNEMoveElementShape.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
18// Class used for shape elements that can be moved over view
19/****************************************************************************/
20
23#include <netedit/GNENet.h>
25#include <netedit/GNEUndoList.h>
27
28#include "GNEMoveElementShape.h"
29
30// ===========================================================================
31// static members
32// ===========================================================================
33
35
36// ===========================================================================
37// Method definitions
38// ===========================================================================
39
41 GNEMoveElement(element),
42 myMovingShape(EMPTY_SHAPE) {
43}
44
45
47 GNEMoveElement(element),
48 myMovingShape(shape),
49 myAlwaysClosed(alwaysClosed) {
50}
51
52
53GNEMoveElementShape::GNEMoveElementShape(GNEAttributeCarrier* element, PositionVector& shape, const Position& position, const bool alwaysClosed) :
54 GNEMoveElement(element),
55 myCenterPosition(position),
56 myMovingShape(shape),
57 myAlwaysClosed(alwaysClosed) {
58}
59
60
62
63
66 // get snap radius
68 // check if we're moving center or shape
70 // move entire shape
71 return new GNEMoveOperation(this, myCenterPosition);
73 // move entire shape
74 return new GNEMoveOperation(this, myMovingShape);
75 } else {
76 // calculate move shape operation
78 }
79}
80
81
82void
84 // get original shape
86 // check shape size
87 if (shape.size() > 3) {
88 // obtain index
89 int index = shape.indexOfClosest(clickedPosition);
90 // get last index
91 const int lastIndex = ((int)shape.size() - 1);
92 // get snap radius
94 // check if we have to create a new index
95 if ((index != -1) && shape[index].distanceSquaredTo2D(clickedPosition) < (snap_radius * snap_radius)) {
96 // check if we're deleting the first point
97 if ((index == 0) || (index == lastIndex)) {
98 // remove both geometry point
99 shape.erase(shape.begin() + lastIndex);
100 shape.erase(shape.begin());
101 // close shape
102 shape.closePolygon();
103 } else {
104 // remove geometry point
105 shape.erase(shape.begin() + index);
106 }
107 // commit new shape
108 undoList->begin(myMovedElement, TLF("remove geometry point of %", myMovedElement->getTagStr()));
110 undoList->end();
111 }
112 }
113}
114
115
116std::string
120
121
122double
126
127
132
133
138
139
140void
141GNEMoveElementShape::setMovingAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
142 return myMovedElement->setCommonAttribute(key, value, undoList);
143}
144
145
146bool
147GNEMoveElementShape::isMovingAttributeValid(SumoXMLAttr key, const std::string& value) const {
148 return myMovedElement->isCommonAttributeValid(key, value);
149}
150
151
152void
154 return myMovedElement->setCommonAttribute(key, value);
155}
156
157
158void
161 // update new center
162 myCenterPosition = moveResult.shapeToUpdate.front();
164 // update new shape and center
166 myMovingShape = moveResult.shapeToUpdate;
167 } else {
168 // get lastIndex
169 const int lastIndex = (int)moveResult.shapeToUpdate.size() - 1;
170 // update new shape
171 myMovingShape = moveResult.shapeToUpdate;
172 // adjust first and last position
173 if (moveResult.geometryPointsToMove.front() == 0) {
174 myMovingShape[lastIndex] = moveResult.shapeToUpdate[0];
175 } else if (moveResult.geometryPointsToMove.front() == lastIndex) {
176 myMovingShape[0] = moveResult.shapeToUpdate[lastIndex];
177 }
179 }
180 // update geometry
182}
183
184
185void
188 // commit center
189 undoList->begin(myMovedElement, TLF("moving center of %", myMovedElement->getTagStr()));
191 undoList->end();
193 // calculate offset between old and new shape
194 Position newCenter = myCenterPosition;
195 newCenter.add(moveResult.shapeToUpdate.getCentroid() - myMovingShape.getCentroid());
196 // commit new shape and center
197 undoList->begin(myMovedElement, TLF("moving entire shape of %", myMovedElement->getTagStr()));
200 }
202 undoList->end();
203 } else {
204 // get lastIndex
205 const int lastIndex = (int)moveResult.shapeToUpdate.size() - 1;
206 // close shapeToUpdate
207 auto closedShape = moveResult.shapeToUpdate;
208 // adjust first and last position
209 if (moveResult.geometryPointsToMove.front() == 0) {
210 closedShape[lastIndex] = moveResult.shapeToUpdate[0];
211 } else if (moveResult.geometryPointsToMove.front() == lastIndex) {
212 closedShape[0] = moveResult.shapeToUpdate[lastIndex];
213 }
214 // commit new shape
215 undoList->begin(myMovedElement, TLF("editing shape of %", myMovedElement->getTagStr()));
217 undoList->end();
218 }
219}
220
221/****************************************************************************/
#define TLF(string,...)
Definition MsgHandler.h:306
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_SHAPE
edge: the shape in xml-definition
@ SUMO_ATTR_CENTER
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
double getCommonAttributeDouble(SumoXMLAttr key) const
PositionVector getCommonAttributePositionVector(SumoXMLAttr key) const
void setCommonAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
const std::string & getTagStr() const
get tag assigned to this object in string format
Position getCommonAttributePosition(SumoXMLAttr key) const
const GNETagProperties * getTagProperty() const
get tagProperty associated with this Attribute Carrier
GNENet * getNet() const
get pointer to net
bool isCommonAttributeValid(SumoXMLAttr key, const std::string &value) const
virtual void updateGeometry()=0
update pre-computed geometry information
std::string getCommonAttribute(SumoXMLAttr key) const
virtual GUIGlObject * getGUIGlObject()=0
get GUIGlObject associated with this AttributeCarrier
static void changeAttribute(GNEAttributeCarrier *AC, SumoXMLAttr key, const std::string &value, GNEUndoList *undoList, const bool force=false)
change attribute
GNEAttributeCarrier * myMovedElement
pointer to element
GNEMoveOperation * getEditShapeOperation(const GUIGlObject *obj, const PositionVector originalShape, const bool maintainShapeClosed)
calculate move shape operation
const bool myAlwaysClosed
check if shape must be always closed
void setMoveShape(const GNEMoveResult &moveResult) override
set move shape
void setMovingAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList) override
set moving attribute (using undo-list)
void commitMoveShape(const GNEMoveResult &moveResult, GNEUndoList *undoList) override
commit move shape
double getMovingAttributeDouble(SumoXMLAttr key) const override
get moving attribute double
Position getMovingAttributePosition(SumoXMLAttr key) const override
get moving attribute position
PositionVector & myMovingShape
reference to Shape
void removeGeometryPoint(const Position clickedPosition, GNEUndoList *undoList) override
remove geometry point in the clicked position
GNEMoveOperation * getMoveOperation()
get move operation
std::string getMovingAttribute(SumoXMLAttr key) const override
get moving attribute
PositionVector getMovingAttributePositionVector(SumoXMLAttr key) const override
get moving attribute position vector
static PositionVector EMPTY_SHAPE
empty position vector
Position myCenterPosition
center position
bool isMovingAttributeValid(SumoXMLAttr key, const std::string &value) const override
check if the given moving attribute is valid
GNEMoveElementShape(GNEAttributeCarrier *element)
constructor
bool getMoveWholePolygons() const
check if option "move whole polygons" is enabled
NetworkMoveOptions * getNetworkMoveOptions() const
get network mode options
const GNEMoveOperation::OperationType operationType
move operation
std::vector< int > geometryPointsToMove
shape points to move (of shapeToMove)
PositionVector shapeToUpdate
shape to update (edited in moveElement)
GNEViewNet * getViewNet() const
get view net
Definition GNENet.cpp:2193
bool hasAttribute(SumoXMLAttr attr) const
check if current TagProperties owns the attribute "attr"
void end()
End undo command sub-group. If the sub-group is still empty, it will be deleted; otherwise,...
void begin(GUIIcon icon, const std::string &description)
Begin undo command sub-group with current supermode. This begins a new group of commands that are tre...
GNEViewParent * getViewParent() const
get the net object
GNEMoveFrame * getMoveFrame() const
get frame for move elements
const GUIVisualizationSettings & getVisualisationSettings() const
get visualization settings (read only)
virtual Position getPositionInformation() const
Returns the cursor's x/y position within the network.
GUIVisualizationNeteditSizeSettings neteditSizeSettings
netedit size settings
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
double distanceSquaredTo2D(const Position &p2) const
returns the square of the distance to another position (Only using x and y positions)
Definition Position.h:278
void add(const Position &pos)
Adds the given position to this one.
Definition Position.h:129
A list of positions.
void closePolygon()
ensures that the last position equals the first
int indexOfClosest(const Position &p, bool twoD=false) const
Position getCentroid() const
Returns the centroid (closes the polygon if unclosed)
static const double polygonGeometryPointRadius
moving geometry point radius