Eclipse SUMO - Simulation of Urban MObility
GNEEdgeType.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-2020 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 /****************************************************************************/
19 /****************************************************************************/
20 #include <config.h>
21 
22 #include <netedit/GNENet.h>
23 #include <netedit/GNEViewNet.h>
24 #include <netedit/GNEViewParent.h>
25 #include <netedit/GNEUndoList.h>
30 
31 #include "GNEEdgeType.h"
32 #include "GNELaneType.h"
33 
34 
35 // ===========================================================================
36 // members methods
37 // ===========================================================================
38 
40  GNENetworkElement(createEdgeFrame->getViewNet()->getNet(), "", GLO_EDGE, SUMO_TAG_TYPE, {}, {}, {}, {}, {}, {}, {}, {}) {
41  // create laneType
42  GNELaneType* laneType = new GNELaneType(this);
43  laneType->incRef("GNEEdgeType::GNEEdgeType(Default)");
44  myLaneTypes.push_back(laneType);
45 }
46 
47 
49  GNENetworkElement(net, net->generateEdgeTypeID(), GLO_EDGE, SUMO_TAG_TYPE, {}, {}, {}, {}, {}, {}, {}, {}) {
50  // create laneType
51  GNELaneType* laneType = new GNELaneType(this);
52  laneType->incRef("GNEEdgeType::GNEEdgeType");
53  myLaneTypes.push_back(laneType);
54 }
55 
56 
57 GNEEdgeType::GNEEdgeType(GNENet* net, const std::string &ID, const NBTypeCont::EdgeTypeDefinition *edgeType) :
58  GNENetworkElement(net, ID, GLO_EDGE, SUMO_TAG_TYPE, {}, {}, {}, {}, {}, {}, {}, {}) {
59  // create laneTypes
60  for (const auto &laneTypeDef : edgeType->laneTypeDefinitions) {
61  GNELaneType* laneType = new GNELaneType(this, laneTypeDef);
62  laneType->incRef("GNEEdgeType::GNEEdgeType(parameters)");
63  myLaneTypes.push_back(laneType);
64  }
65  // copy parameters
66  speed = edgeType->speed;
67  priority = edgeType->priority;
68  permissions = edgeType->permissions;
69  width = edgeType->width;
70  attrs = edgeType->attrs;
71  laneTypeDefinitions = edgeType->laneTypeDefinitions;
72 }
73 
74 
76  // delete laneTypes
77  for (const auto &laneType : myLaneTypes) {
78  laneType->decRef("GNEEdgeType::~GNEEdgeType");
79  if (laneType->unreferenced()) {
80  delete laneType;
81  }
82  }
83 }
84 
85 
86 const std::vector<GNELaneType*>&
88  return myLaneTypes;
89 }
90 
91 
92 int
94  for (int i = 0; i < (int)myLaneTypes.size(); i++) {
95  if (myLaneTypes.at(i) == laneType) {
96  return i;
97  }
98  }
99  return (int)myLaneTypes.size();
100 }
101 
102 
103 void
104 GNEEdgeType::addLaneType(GNELaneType* laneType, const int position) {
105  if (std::find(myLaneTypes.begin(), myLaneTypes.end(), laneType) != myLaneTypes.end()) {
106  throw ProcessError("GNELaneType already inserted");
107  } else {
108  if (position < 0 || position > (int)myLaneTypes.size()) {
109  throw ProcessError("invalid position");
110  } else if (position == (int)myLaneTypes.size()) {
111  myLaneTypes.push_back(laneType);
112  } else {
113  myLaneTypes[position] = laneType;
114  }
115  }
116 }
117 
118 
119 void
121  // get options
122  const OptionsCont& oc = OptionsCont::getOptions();
123  // create new laneType
124  GNELaneType* laneType = new GNELaneType(this);
125  // begin undoList
126  undoList->p_begin("add laneType");
127  // add lane
128  undoList->add(new GNEChange_LaneType(laneType, (int)myLaneTypes.size(), true), true);
129  // set default parameters
130  laneType->setAttribute(SUMO_ATTR_SPEED, toString(oc.getFloat("default.speed")), undoList);
131  laneType->setAttribute(SUMO_ATTR_DISALLOW, oc.getString("default.disallow"), undoList);
133  laneType->setAttribute(GNE_ATTR_PARAMETERS, "", undoList);
134  // end undoList
135  undoList->p_end();
136 }
137 
138 
139 void
141  auto it = std::find(myLaneTypes.begin(), myLaneTypes.end(), laneType);
142  if (it == myLaneTypes.end()) {
143  throw ProcessError("GNELaneType wasn't inserted");
144  } else {
145  myLaneTypes.erase(it);
146  }
147 }
148 
149 
150 void
151 GNEEdgeType::removeLaneType(const int index, GNEUndoList* undoList) {
152  // first check if index is correct
153  if ((myLaneTypes.size() > 1) && (index < (int)myLaneTypes.size())) {
154  // begin undoList
155  undoList->p_begin("remove laneType");
156  // copy laneType values
157  for (int i = index; i < ((int)myLaneTypes.size() - 1); i++) {
158  myLaneTypes.at(i)->copyLaneType(myLaneTypes.at(i+1), undoList);
159  }
160  // remove last lane
161  undoList->add(new GNEChange_LaneType(myLaneTypes.back(), ((int)myLaneTypes.size() - 1), false), true);
162  // end undoList
163  undoList->p_end();
164  }
165 }
166 
167 
168 void
170 }
171 
172 
173 Position
175  // currently unused
176  return Position(0, 0);
177 }
178 
179 
181 GNEEdgeType::getMoveOperation(const double /*shapeOffset*/) {
182  return nullptr;
183 }
184 
185 
186 void
187 GNEEdgeType::removeGeometryPoint(const Position /*clickedPosition*/, GNEUndoList* /*undoList*/) {
188  // nothing to do
189 }
190 
191 
194  return nullptr;
195 }
196 
197 
198 void
199 GNEEdgeType::updateCenteringBoundary(const bool /*updateGrid*/) {
200  // nothing to do
201 }
202 
203 
204 void
206  // nothing to draw
207 }
208 
209 
210 std::string
212  // get options
213  const OptionsCont& oc = OptionsCont::getOptions();
214  switch (key) {
215  case SUMO_ATTR_ID:
216  return getID();
217  case SUMO_ATTR_NUMLANES:
218  return toString(myLaneTypes.size());
219  case SUMO_ATTR_SPEED:
220  if (attrs.count(key) == 0) {
221  return toString(oc.getFloat("default.speed"));
222  } else {
223  return toString(speed);
224  }
225  case SUMO_ATTR_ALLOW:
226  if (attrs.count(SUMO_ATTR_DISALLOW) == 0) {
227  return "all";
228  } else {
230  }
231  case SUMO_ATTR_DISALLOW:
232  if (attrs.count(SUMO_ATTR_DISALLOW) == 0) {
233  return "";
234  } else {
236  }
237  case SUMO_ATTR_WIDTH:
238  if (attrs.count(key) == 0) {
240  } else {
241  return toString(width);
242  }
243  case SUMO_ATTR_PRIORITY:
244  if (attrs.count(key) == 0) {
245  return toString(-1);
246  } else {
247  return toString(priority);
248  }
249  case GNE_ATTR_PARAMETERS:
250  return getParametersStr();
251  default:
252  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
253  }
254 }
255 
256 
257 void
258 GNEEdgeType::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
259  switch (key) {
260  case SUMO_ATTR_ID:
261  case SUMO_ATTR_NUMLANES:
262  case SUMO_ATTR_SPEED:
263  case SUMO_ATTR_ALLOW:
264  case SUMO_ATTR_DISALLOW:
265  case SUMO_ATTR_DISCARD:
266  case SUMO_ATTR_WIDTH:
267  case SUMO_ATTR_PRIORITY:
268  case GNE_ATTR_PARAMETERS:
269  undoList->p_add(new GNEChange_Attribute(this, key, value));
270  break;
271  default:
272  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
273  }
274 }
275 
276 
277 bool
278 GNEEdgeType::isValid(SumoXMLAttr key, const std::string& value) {
279  switch (key) {
280  case SUMO_ATTR_ID:
281  return SUMOXMLDefinitions::isValidNetID(value) && (myNet->retrieveEdgeType(value, false) == nullptr);
282  case SUMO_ATTR_NUMLANES:
283  return canParse<int>(value) && (parse<double>(value) > 0);
284  case SUMO_ATTR_SPEED:
285  if (value.empty()) {
286  return true;
287  } else {
288  return canParse<double>(value) && (parse<double>(value) > 0);
289  }
290  case SUMO_ATTR_ALLOW:
291  case SUMO_ATTR_DISALLOW:
292  if (value.empty()) {
293  return true;
294  } else {
295  return canParseVehicleClasses(value);
296  }
297  case SUMO_ATTR_WIDTH:
298  if (value.empty() || (value == "-1")) {
299  return true;
300  } else {
301  return canParse<double>(value);
302  }
303  case SUMO_ATTR_PRIORITY:
304  return canParse<int>(value);
305  case GNE_ATTR_PARAMETERS:
306  return Parameterised::areParametersValid(value);
307  default:
308  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
309  }
310 }
311 
312 
313 bool
315  return true;
316 }
317 
318 
319 const std::map<std::string, std::string>&
321  return getParametersMap();
322 }
323 
324 // ===========================================================================
325 // private
326 // ===========================================================================
327 
328 void
329 GNEEdgeType::setAttribute(SumoXMLAttr key, const std::string& value) {
330  switch (key) {
331  case SUMO_ATTR_ID:
332  myNet->getAttributeCarriers()->updateID(this, value);
333  break;
334  case SUMO_ATTR_NUMLANES:
335  throw InvalidArgument("Modifying attribute '" + toString(key) + "' of " + getTagStr() + " isn't allowed");
336  break;
337  case SUMO_ATTR_SPEED:
338  if (value.empty()) {
339  attrs.erase(key);
340  } else {
341  attrs.insert(key);
342  speed = parse<double>(value);
343  }
344  break;
345  case SUMO_ATTR_ALLOW:
346  if (value.empty()) {
347  attrs.erase(SUMO_ATTR_DISALLOW);
348  } else {
349  attrs.insert(SUMO_ATTR_DISALLOW);
351  }
352  break;
353  case SUMO_ATTR_DISALLOW:
354  if (value.empty()) {
355  attrs.erase(SUMO_ATTR_DISALLOW);
356  } else {
357  attrs.insert(SUMO_ATTR_DISALLOW);
359  }
360  break;
361  case SUMO_ATTR_DISCARD:
362  if (value.empty()) {
363  attrs.erase(key);
364  } else {
365  attrs.insert(key);
366  discard = parse<bool>(value);
367  }
368  break;
369  case SUMO_ATTR_WIDTH:
370  if (value.empty()) {
371  attrs.erase(key);
372  } else {
373  attrs.insert(key);
374  width = parse<double>(value);
375  }
376  break;
377  case SUMO_ATTR_PRIORITY:
378  if (value.empty()) {
379  attrs.erase(key);
380  } else {
381  attrs.insert(key);
382  priority = parse<int>(value);
383  }
384  break;
385  case GNE_ATTR_PARAMETERS:
386  setParametersStr(value);
387  break;
388  default:
389  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
390  }
391  // update edge selector
392  if (myNet->getViewNet()->getViewParent()->getCreateEdgeFrame()->shown()) {
394  }
395 }
396 
397 
398 void
399 GNEEdgeType::setMoveShape(const GNEMoveResult& /*moveResult*/) {
400  // nothing to do
401 }
402 
403 
404 void
405 GNEEdgeType::commitMoveShape(const GNEMoveResult& /*moveResult*/, GNEUndoList* /*undoList*/) {
406  // nothing to do
407 }
408 
409 /****************************************************************************/
@ GLO_EDGE
an edge
SVCPermissions invertPermissions(SVCPermissions permissions)
negate the given permissions and ensure that only relevant bits are set
const std::string & getVehicleClassNames(SVCPermissions permissions, bool expand)
Returns the ids of the given classes, divided using a ' '.
SVCPermissions parseVehicleClasses(const std::string &allowedS)
Parses the given definition of allowed vehicle classes into the given containers Deprecated classes g...
bool canParseVehicleClasses(const std::string &classes)
Checks whether the given string contains only known vehicle classes.
@ SUMO_TAG_TYPE
type (edge)
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_DISALLOW
@ SUMO_ATTR_ALLOW
@ SUMO_ATTR_SPEED
@ GNE_ATTR_PARAMETERS
parameters "key1=value1|key2=value2|...|keyN=valueN"
@ SUMO_ATTR_PRIORITY
@ SUMO_ATTR_NUMLANES
@ SUMO_ATTR_ID
@ SUMO_ATTR_DISCARD
@ SUMO_ATTR_WIDTH
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
friend class GNEChange_Attribute
declare friend class
const std::string & getTagStr() const
get tag assigned to this object in string format
GNENet * myNet
pointer to net
void refreshEdgeTypeSelector()
refresh edge type selector
EdgeTypeSelector * getEdgeTypeSelector() const
getcustom edge selector
bool isAttributeEnabled(SumoXMLAttr key) const
std::vector< GNELaneType * > myLaneTypes
vector with laneTypes
Definition: GNEEdgeType.h:153
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
GNEEdgeType(GNECreateEdgeFrame *createEdgeFrame)
Constructor for default edge (empty ID)
Definition: GNEEdgeType.cpp:39
Position getPositionInView() const
Returns position of hierarchical element in view.
int getLaneTypeIndex(const GNELaneType *laneType) const
get laneType index
Definition: GNEEdgeType.cpp:93
bool isValid(SumoXMLAttr key, const std::string &value)
void removeGeometryPoint(const Position clickedPosition, GNEUndoList *undoList)
remove geometry point in the clicked position
void commitMoveShape(const GNEMoveResult &moveResult, GNEUndoList *undoList)
commit move shape
GUIGLObjectPopupMenu * getPopUpMenu(GUIMainWindow &app, GUISUMOAbstractView &parent)
Returns an own popup-menu.
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
const std::map< std::string, std::string > & getACParametersMap() const
get parameters map
void updateCenteringBoundary(const bool updateGrid)
update centering boundary (implies change in RTREE)
const std::vector< GNELaneType * > & getLaneTypes() const
get laneTypes
Definition: GNEEdgeType.cpp:87
GNEMoveOperation * getMoveOperation(const double shapeOffset)
get move operation for the given shapeOffset
~GNEEdgeType()
Destructor.
Definition: GNEEdgeType.cpp:75
std::string getAttribute(SumoXMLAttr key) const
void removeLaneType(GNELaneType *laneType)
remove laneType
void addLaneType(GNELaneType *laneType, const int position)
add laneType
void updateGeometry()
update pre-computed geometry information
void setMoveShape(const GNEMoveResult &moveResult)
set move shape
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
move operation
move result
void updateID(GNEAttributeCarrier *AC, const std::string newID)
update ID
A NBNetBuilder extended by visualisation and editing capabilities.
Definition: GNENet.h:40
GNEEdgeType * retrieveEdgeType(const std::string &id, bool failHard=true) const
get edge type by id
Definition: GNENet.cpp:1126
GNENetHelper::AttributeCarriers * getAttributeCarriers() const
retrieve all attribute carriers of Net
Definition: GNENet.cpp:130
GNEViewNet * getViewNet() const
get view net
Definition: GNENet.cpp:2245
const std::string & getID() const
get ID
void decRef(const std::string &debugMsg="")
Decrease reference.
void incRef(const std::string &debugMsg="")
Increarse reference.
bool unreferenced()
check if object ins't referenced
void p_add(GNEChange_Attribute *cmd)
special method, avoid empty changes, always execute
void p_begin(const std::string &description)
Begin undo command sub-group. This begins a new group of commands that are treated as a single comman...
Definition: GNEUndoList.cpp:71
void p_end()
End undo command sub-group. If the sub-group is still empty, it will be deleted; otherwise,...
Definition: GNEUndoList.cpp:78
GNEViewParent * getViewParent() const
get the net object
GNECreateEdgeFrame * getCreateEdgeFrame() const
get frame for NETWORK_CREATEEDGE
The popup menu of a globject.
Stores the information about how to visualize structures.
static const double UNSPECIFIED_WIDTH
unspecified lane width
Definition: NBEdge.h:324
A storage for options typed value containers)
Definition: OptionsCont.h:89
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
static bool areParametersValid(const std::string &value, bool report=false, ParameterisedAttrType attrType=ParameterisedAttrType::STRING, 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".
const std::map< std::string, std::string > & getParametersMap() const
Returns the inner key/value map.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36
static bool isValidNetID(const std::string &value)
whether the given string is a valid id for a network element
edgeType definition
Definition: NBTypeCont.h:80
int priority
The priority of an edge.
Definition: NBTypeCont.h:101
double width
The width of lanes of edges of this edgeType [m].
Definition: NBTypeCont.h:113
double speed
The maximal velocity on an edge in m/s.
Definition: NBTypeCont.h:98
SVCPermissions permissions
List of vehicle edgeTypes that are allowed on this edge.
Definition: NBTypeCont.h:104
std::set< SumoXMLAttr > attrs
The attributes which have been set.
Definition: NBTypeCont.h:136
bool discard
Whether edges of this edgeType shall be discarded.
Definition: NBTypeCont.h:110