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-2026 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#include <config.h>
21
24#include <netedit/GNENet.h>
26#include <netedit/GNEUndoList.h>
28
29#include "GNEMoveElementShape.h"
30
31// ===========================================================================
32// static members
33// ===========================================================================
34
36
37// ===========================================================================
38// Method definitions
39// ===========================================================================
40
42 GNEMoveElement(element),
43 myMovingShape(EMPTY_SHAPE) {
44}
45
46
48 GNEMoveElement(element),
49 myMovingShape(shape),
50 myAlwaysClosed(alwaysClosed) {
51}
52
53
54GNEMoveElementShape::GNEMoveElementShape(GNEAttributeCarrier* element, PositionVector& shape, const Position& position, const bool alwaysClosed) :
55 GNEMoveElement(element),
56 myCenterPosition(position),
57 myMovingShape(shape),
58 myAlwaysClosed(alwaysClosed) {
59}
60
61
63
64
67 // get snap radius
69 // check if we're moving center or shape
71 // move entire shape
72 return new GNEMoveOperation(this, myCenterPosition);
74 // move entire shape
75 return new GNEMoveOperation(this, myMovingShape);
76 } else {
77 // calculate move shape operation
79 }
80}
81
82
83void
85 // get original shape
87 // check shape size
88 if (shape.size() > 3) {
89 // obtain index
90 int index = shape.indexOfClosest(clickedPosition);
91 // get last index
92 const int lastIndex = ((int)shape.size() - 1);
93 // get snap radius
95 // check if we have to create a new index
96 if ((index != -1) && shape[index].distanceSquaredTo2D(clickedPosition) < (snap_radius * snap_radius)) {
97 // check if we're deleting the first point
98 if ((index == 0) || (index == lastIndex)) {
99 // remove both geometry point
100 shape.erase(shape.begin() + lastIndex);
101 shape.erase(shape.begin());
102 // close shape
103 shape.closePolygon();
104 } else {
105 // remove geometry point
106 shape.erase(shape.begin() + index);
107 }
108 // commit new shape
109 undoList->begin(myMovedElement, TLF("remove geometry point of %", myMovedElement->getTagStr()));
111 undoList->end();
112 }
113 }
114}
115
116
117std::string
121
122
123double
127
128
131 if (key == SUMO_ATTR_CENTER) {
132 return myCenterPosition;
133 } else {
135 }
136}
137
138
143
144
145void
146GNEMoveElementShape::setMovingAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
147 myMovedElement->setCommonAttribute(key, value, undoList);
148}
149
150
151bool
152GNEMoveElementShape::isMovingAttributeValid(SumoXMLAttr key, const std::string& value) const {
153 return myMovedElement->isCommonAttributeValid(key, value);
154}
155
156
157void
159 if (key == SUMO_ATTR_CENTER) {
160 myCenterPosition = GNEAttributeCarrier::parse<Position>(value);
161 } else {
163 }
164}
165
166
167void
169 if (key == SUMO_ATTR_CENTER) {
170 myCenterPosition = value;
171 } else {
172 throw InvalidArgument(myMovedElement->getTagStr() + " doesn't have a position attribute of type '" + toString(key) + "'");
173 }
174}
175
176
177void
180 // update new center
181 myCenterPosition = moveResult.shapeToUpdate.front();
183 // update new shape and center
185 myMovingShape = moveResult.shapeToUpdate;
186 } else {
187 // get lastIndex
188 const int lastIndex = (int)moveResult.shapeToUpdate.size() - 1;
189 // update new shape
190 myMovingShape = moveResult.shapeToUpdate;
191 // adjust first and last position
192 if (moveResult.geometryPointsToMove.front() == 0) {
193 myMovingShape[lastIndex] = moveResult.shapeToUpdate[0];
194 } else if (moveResult.geometryPointsToMove.front() == lastIndex) {
195 myMovingShape[0] = moveResult.shapeToUpdate[lastIndex];
196 }
198 }
199 // update geometry
201}
202
203
204void
207 // commit center
208 undoList->begin(myMovedElement, TLF("moving center of %", myMovedElement->getTagStr()));
210 undoList->end();
212 // calculate offset between old and new shape
213 Position newCenter = myCenterPosition;
214 newCenter.add(moveResult.shapeToUpdate.getCentroid() - myMovingShape.getCentroid());
215 // commit new shape and center
216 undoList->begin(myMovedElement, TLF("moving entire shape of %", myMovedElement->getTagStr()));
219 }
221 undoList->end();
222 } else {
223 // get lastIndex
224 const int lastIndex = (int)moveResult.shapeToUpdate.size() - 1;
225 // close shapeToUpdate
226 auto closedShape = moveResult.shapeToUpdate;
227 // adjust first and last position
228 if (moveResult.geometryPointsToMove.front() == 0) {
229 closedShape[lastIndex] = moveResult.shapeToUpdate[0];
230 } else if (moveResult.geometryPointsToMove.front() == lastIndex) {
231 closedShape[0] = moveResult.shapeToUpdate[lastIndex];
232 }
233 // commit new shape
234 undoList->begin(myMovedElement, TLF("editing shape of %", myMovedElement->getTagStr()));
236 undoList->end();
237 }
238}
239
240/****************************************************************************/
#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:49
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
GNEMoveOperation * getMoveOperation() override
get move operation
const bool myAlwaysClosed
check if shape must be always closed
void setMoveShape(const GNEMoveResult &moveResult) override
set move shape
void setMovingAttributePosition(SumoXMLAttr key, const Position &value)
set moving attribute position (needed to avoid precision conversion problems)
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
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)
GNEViewParent * getViewParent() const
get view parent (used for simplify code)
Definition GNENet.cpp:150
GNEViewNet * getViewNet() const
get view net (used for simplify code)
Definition GNENet.cpp:144
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...
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