Eclipse SUMO - Simulation of Urban MObility
GNETAZSourceSink.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2022 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 //
19 /****************************************************************************/
20 #include <config.h>
21 
22 #include <netedit/GNENet.h>
23 #include <netedit/GNEUndoList.h>
24 #include <netedit/GNEViewNet.h>
29 
30 #include "GNETAZSourceSink.h"
31 
32 
33 // ===========================================================================
34 // member method definitions
35 // ===========================================================================
36 
38  GNETAZElement("", net, GLO_TAZ, tag,
39 {}, {}, {}, {}, {}, {}, {}, {},
40 std::map<std::string, std::string>()),
41 myDepartWeight(0) {
42  // reset default values
44 }
45 
46 
47 GNETAZSourceSink::GNETAZSourceSink(SumoXMLTag sourceSinkTag, GNETAZElement* TAZParent, GNEEdge* edge, double departWeight) :
48  GNETAZElement(TAZParent, TAZParent->getNet(), GLO_TAZ, sourceSinkTag,
49 {}, {edge}, {}, {}, {}, {TAZParent}, {}, {},
50 std::map<std::string, std::string>()),
51 myDepartWeight(departWeight) {
52  //check that this is a TAZ Source OR a TAZ Sink
53  if ((sourceSinkTag != SUMO_TAG_TAZSOURCE) && (sourceSinkTag != SUMO_TAG_TAZSINK)) {
54  throw InvalidArgument("Invalid TAZ Child Tag");
55  }
56 }
57 
58 
60 
61 
62 const PositionVector&
64  return getParentTAZElements().front()->getTAZElementShape();
65 }
66 
67 
68 void
70  // open source/sink tag
71  device.openTag(myTagProperty.getTag());
72  // write source/sink attributes
73  device.writeAttr(SUMO_ATTR_ID, getParentEdges().front()->getID());
75  // close tag
76  device.closeTag();
77 }
78 
79 
80 double
82  return myDepartWeight;
83 }
84 
85 
86 void
88  // This TAZElement doesn't own a geometry
89 }
90 
91 
94  return getParentTAZElements().at(0)->getPositionInView();
95 }
96 
97 
98 double
100  return 1;
101 }
102 
103 
104 Boundary
106  return getParentEdges().front()->getCenteringBoundary();
107 }
108 
109 
110 void
111 GNETAZSourceSink::splitEdgeGeometry(const double /*splitPosition*/, const GNENetworkElement* /*originalElement*/, const GNENetworkElement* /*newElement*/, GNEUndoList* /*undoList*/) {
112  // geometry of this element cannot be splitted
113 }
114 
115 
116 std::string
118  return getParentTAZElements().at(0)->getID();
119 }
120 
121 
124  GUIGLObjectPopupMenu* ret = new GUIGLObjectPopupMenu(app, parent, *this);
125  // build header
126  buildPopupHeader(ret, app);
127  // build menu command for center button and copy cursor position to clipboard
129  buildPositionCopyEntry(ret, false);
130  // buld menu commands for names
131  GUIDesigns::buildFXMenuCommand(ret, "Copy " + getTagStr() + " name to clipboard", nullptr, ret, MID_COPY_NAME);
132  GUIDesigns::buildFXMenuCommand(ret, "Copy " + getTagStr() + " typed name to clipboard", nullptr, ret, MID_COPY_TYPED_NAME);
133  new FXMenuSeparator(ret);
134  // build selection and show parameters menu
137  return ret;
138 }
139 
140 
141 void
143  // Currently This TAZElement isn't drawn
144 }
145 
146 
147 std::string
149  switch (key) {
150  case SUMO_ATTR_ID:
151  return getID();
152  case SUMO_ATTR_EDGE:
153  return getParentEdges().front()->getID();
154  case SUMO_ATTR_WEIGHT:
155  return toString(myDepartWeight);
156  case GNE_ATTR_PARENT:
157  return getParentTAZElements().at(0)->getID();
158  case GNE_ATTR_PARAMETERS:
159  return getParametersStr();
160  case GNE_ATTR_TAZCOLOR: {
161  // obtain max and min weight source
162  double maxWeightSource = getParentTAZElements().at(0)->getAttributeDouble(GNE_ATTR_MAX_SOURCE);
163  double minWeightSource = getParentTAZElements().at(0)->getAttributeDouble(GNE_ATTR_MIN_SOURCE);
164  // avoid division between zero
165  if ((maxWeightSource - minWeightSource) == 0) {
166  return "0";
167  } else {
168  // calculate percentage relative to the max and min weight
169  double percentage = (myDepartWeight - minWeightSource) / (maxWeightSource - minWeightSource);
170  // convert percentage to a value between [0-9] (because we have only 10 colors)
171  if (percentage >= 1) {
172  return "9";
173  } else if (percentage < 0) {
174  return "0";
175  } else {
176  return toString((int)(percentage * 10));
177  }
178  }
179  }
180  default:
181  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
182  }
183 }
184 
185 double
187  switch (key) {
188  case SUMO_ATTR_WEIGHT:
189  return myDepartWeight;
190  default:
191  throw InvalidArgument(getTagStr() + " doesn't have a double attribute of type '" + toString(key) + "'");
192  }
193 }
194 
195 
196 Position
198  throw InvalidArgument(getTagStr() + " doesn't have a double attribute of type '" + toString(key) + "'");
199 }
200 
201 
202 void
203 GNETAZSourceSink::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
204  // this TAZElement is the only that can edit a variable directly, see GNEAdditionalHandler::buildTAZEdge(...)
205  if (undoList == nullptr) {
206  setAttribute(key, value);
207  } else {
208  if (value == getAttribute(key)) {
209  return; //avoid needless changes, later logic relies on the fact that attributes have changed
210  }
211  switch (key) {
212  case SUMO_ATTR_ID:
213  case SUMO_ATTR_WEIGHT:
214  case GNE_ATTR_PARAMETERS:
215  undoList->changeAttribute(new GNEChange_Attribute(this, key, value));
216  break;
217  default:
218  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
219  }
220  }
221 }
222 
223 
224 bool
225 GNETAZSourceSink::isValid(SumoXMLAttr key, const std::string& value) {
226  switch (key) {
227  case SUMO_ATTR_ID:
229  (myNet->getAttributeCarriers()->retrieveTAZElement(myTagProperty.getTag(), value, false) == nullptr);
230  case SUMO_ATTR_WEIGHT:
231  return canParse<double>(value) && (parse<double>(value) >= 0);
232  case GNE_ATTR_PARAMETERS:
233  return Parameterised::areParametersValid(value);
234  default:
235  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
236  }
237 }
238 
239 
240 
241 bool
243  switch (key) {
244  case SUMO_ATTR_EDGE:
245  return false;
246  default:
247  return true;
248  }
249 }
250 
251 
252 std::string
254  return getTagStr();
255 }
256 
257 
258 std::string
260  return getTagStr() + ": " + getAttribute(SUMO_ATTR_WEIGHT);
261 }
262 
263 // ===========================================================================
264 // private
265 // ===========================================================================
266 
267 void
268 GNETAZSourceSink::setAttribute(SumoXMLAttr key, const std::string& value) {
269  switch (key) {
270  case SUMO_ATTR_ID:
271  // update microsimID
272  setMicrosimID(value);
273  break;
274  case SUMO_ATTR_WEIGHT:
275  myDepartWeight = parse<double>(value);
276  break;
277  case GNE_ATTR_PARAMETERS:
278  setParametersStr(value);
279  break;
280  default:
281  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
282  }
283 }
284 
285 
286 /****************************************************************************/
@ MID_COPY_TYPED_NAME
Copy typed object name - popup entry.
Definition: GUIAppEnum.h:413
@ MID_COPY_NAME
Copy object name - popup entry.
Definition: GUIAppEnum.h:411
@ GLO_TAZ
Traffic Assignment Zones (TAZs)
SumoXMLTag
Numbers representing SUMO-XML - element names.
@ SUMO_TAG_TAZSINK
a sink within a district (connection road)
@ SUMO_TAG_TAZSOURCE
a source within a district (connection road)
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ GNE_ATTR_MAX_SOURCE
max source (used only by TAZs)
@ SUMO_ATTR_EDGE
@ GNE_ATTR_TAZCOLOR
Color of TAZSources/TAZSinks.
@ GNE_ATTR_PARENT
parent of an additional element
@ GNE_ATTR_PARAMETERS
parameters "key1=value1|key2=value2|...|keyN=valueN"
@ SUMO_ATTR_WEIGHT
@ SUMO_ATTR_ID
@ GNE_ATTR_MIN_SOURCE
min source (used only by TAZs)
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
friend class GNEChange_Attribute
declare friend class
const std::string & getTagStr() const
get tag assigned to this object in string format
void resetDefaultValues()
reset attribute carrier to their default values
GNENet * myNet
pointer to net
const GNETagProperties & myTagProperty
reference to tagProperty associated with this attribute carrier
A road/street connecting two junctions (netedit-version)
Definition: GNEEdge.h:53
const std::vector< GNEEdge * > & getParentEdges() const
get parent edges
const std::vector< GNETAZElement * > & getParentTAZElements() const
get parent TAZElements
GNETAZElement * retrieveTAZElement(SumoXMLTag type, const std::string &id, bool hardFail=true) const
Returns the named TAZElement.
A NBNetBuilder extended by visualisation and editing capabilities.
Definition: GNENet.h:42
GNENetHelper::AttributeCarriers * getAttributeCarriers() const
get all attribute carriers used in this net
Definition: GNENet.cpp:125
GNEViewNet * getViewNet() const
get view net
Definition: GNENet.cpp:1964
An Element which don't belongs to GNENet but has influency in the simulation.
Definition: GNETAZElement.h:45
const std::string & getID() const
get ID
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
void updateGeometry()
update pre-computed geometry information
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform TAZElement changes
void splitEdgeGeometry(const double splitPosition, const GNENetworkElement *originalElement, const GNENetworkElement *newElement, GNEUndoList *undoList)
split geometry
void writeTAZElement(OutputDevice &device) const
writte TAZElement element into a xml file
Boundary getCenteringBoundary() const
Returns the boundary to which the view shall be centered in order to show the object.
std::string getAttribute(SumoXMLAttr key) const
inherited from GNEAttributeCarrier
double getDepartWeight() const
get depart weight
~GNETAZSourceSink()
destructor
GUIGLObjectPopupMenu * getPopUpMenu(GUIMainWindow &app, GUISUMOAbstractView &parent)
Returns an own popup-menu.
double myDepartWeight
depart Weight
std::string getPopUpID() const
get PopPup ID (Used in AC Hierarchy)
GNETAZSourceSink(SumoXMLTag tag, GNENet *net)
default Constructor
std::string getHierarchyName() const
get Hierarchy Name (Used in AC Hierarchy)
std::string getParentName() const
Returns the name of the parent object.
double getExaggeration(const GUIVisualizationSettings &s) const
return exaggeration asociated with this GLObject
double getAttributeDouble(SumoXMLAttr key) const
const PositionVector & getTAZElementShape() const
get TAZ Shape
bool isAttributeEnabled(SumoXMLAttr key) const
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
Position getPositionInView() const
Returns position of additional in view.
Position getAttributePosition(SumoXMLAttr key) const
SumoXMLTag getTag() const
get Tag vinculated with this attribute Property
void changeAttribute(GNEChange_Attribute *change)
special method for change attributes, avoid empty changes, always execute
void buildSelectionACPopupEntry(GUIGLObjectPopupMenu *ret, GNEAttributeCarrier *AC)
Builds an entry which allows to (de)select the object.
Definition: GNEViewNet.cpp:432
static FXMenuCommand * buildFXMenuCommand(FXComposite *p, const std::string &text, FXIcon *icon, FXObject *tgt, FXSelector sel)
build menu command
Definition: GUIDesigns.cpp:42
The popup menu of a globject.
void buildPositionCopyEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to copy the cursor position if geo projection is used,...
void buildShowParamsPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to open the parameter window.
virtual void setMicrosimID(const std::string &newID)
Changes the microsimID of the object.
void buildCenterPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to center to the object.
void buildPopupHeader(GUIGLObjectPopupMenu *ret, GUIMainWindow &app, bool addSeparator=true)
Builds the header.
Stores the information about how to visualize structures.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:248
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
static bool areParametersValid(const std::string &value, bool report=false, const std::string kvsep="=", const std::string sep="|")
check if given string can be parsed to a parameters map "key1=value1|key2=value2|....
void setParametersStr(const std::string &paramsString, const std::string kvsep="=", const std::string sep="|")
set the inner key/value map in string format "key1=value1|key2=value2|...|keyN=valueN"
std::string getParametersStr(const std::string kvsep="=", const std::string sep="|") const
Returns the inner key/value map in string format "key1=value1|key2=value2|...|keyN=valueN".
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
A list of positions.
static bool isValidAdditionalID(const std::string &value)
whether the given string is a valid id for an additional object