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#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
133
134
139
140
141void
142GNEMoveElementShape::setMovingAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
143 return myMovedElement->setCommonAttribute(key, value, undoList);
144}
145
146
147bool
148GNEMoveElementShape::isMovingAttributeValid(SumoXMLAttr key, const std::string& value) const {
149 return myMovedElement->isCommonAttributeValid(key, value);
150}
151
152
153void
155 return myMovedElement->setCommonAttribute(key, value);
156}
157
158
159void
162 // update new center
163 myCenterPosition = moveResult.shapeToUpdate.front();
165 // update new shape and center
167 myMovingShape = moveResult.shapeToUpdate;
168 } else {
169 // get lastIndex
170 const int lastIndex = (int)moveResult.shapeToUpdate.size() - 1;
171 // update new shape
172 myMovingShape = moveResult.shapeToUpdate;
173 // adjust first and last position
174 if (moveResult.geometryPointsToMove.front() == 0) {
175 myMovingShape[lastIndex] = moveResult.shapeToUpdate[0];
176 } else if (moveResult.geometryPointsToMove.front() == lastIndex) {
177 myMovingShape[0] = moveResult.shapeToUpdate[lastIndex];
178 }
180 }
181 // update geometry
183}
184
185
186void
189 // commit center
190 undoList->begin(myMovedElement, TLF("moving center of %", myMovedElement->getTagStr()));
192 undoList->end();
194 // calculate offset between old and new shape
195 Position newCenter = myCenterPosition;
196 newCenter.add(moveResult.shapeToUpdate.getCentroid() - myMovingShape.getCentroid());
197 // commit new shape and center
198 undoList->begin(myMovedElement, TLF("moving entire shape of %", myMovedElement->getTagStr()));
201 }
203 undoList->end();
204 } else {
205 // get lastIndex
206 const int lastIndex = (int)moveResult.shapeToUpdate.size() - 1;
207 // close shapeToUpdate
208 auto closedShape = moveResult.shapeToUpdate;
209 // adjust first and last position
210 if (moveResult.geometryPointsToMove.front() == 0) {
211 closedShape[lastIndex] = moveResult.shapeToUpdate[0];
212 } else if (moveResult.geometryPointsToMove.front() == lastIndex) {
213 closedShape[0] = moveResult.shapeToUpdate[lastIndex];
214 }
215 // commit new shape
216 undoList->begin(myMovedElement, TLF("editing shape of %", myMovedElement->getTagStr()));
218 undoList->end();
219 }
220}
221
222/****************************************************************************/
#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
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 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)
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