1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file    GeomConvHelper.cpp
11 /// @author  Daniel Krajzewicz
12 /// @author  Jakob Erdmann
13 /// @author  Michael Behrisch
14 /// @date    Sept 2003
15 /// @version $Id$
16 ///
17 // Some helping functions for geometry parsing
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <string>
27 #include <sstream>
28 #include <utils/geom/PositionVector.h>
29 #include <utils/common/MsgHandler.h>
30 #include <utils/common/StringTokenizer.h>
31 #include <utils/common/StringUtils.h>
32 #include "GeomConvHelper.h"
33 
34 
35 // ===========================================================================
36 // method definitions
37 // ===========================================================================
38 PositionVector
parseShapeReporting(const std::string & shpdef,const std::string & objecttype,const char * objectid,bool & ok,bool allowEmpty,bool report)39 GeomConvHelper::parseShapeReporting(const std::string& shpdef, const std::string& objecttype,
40                                     const char* objectid, bool& ok, bool allowEmpty, bool report) {
41     if (shpdef == "") {
42         if (!allowEmpty) {
43             emitError(report, "Shape", objecttype, objectid, "the shape is empty");
44             ok = false;
45         }
46         return PositionVector();
47     }
48     StringTokenizer st(shpdef, " ");
49     PositionVector shape;
50     while (st.hasNext()) {
51         StringTokenizer pos(st.next(), ",");
52         if (pos.size() != 2 && pos.size() != 3) {
53             emitError(report, "Shape", objecttype, objectid, "the position is neither x,y nor x,y,z");
54             ok = false;
55             return PositionVector();
56         }
57         try {
58             double x = StringUtils::toDouble(pos.next());
59             double y = StringUtils::toDouble(pos.next());
60             if (pos.size() == 2) {
61                 shape.push_back(Position(x, y));
62             } else {
63                 double z = StringUtils::toDouble(pos.next());
64                 shape.push_back(Position(x, y, z));
65             }
66         } catch (NumberFormatException&) {
67             emitError(report, "Shape", objecttype, objectid, "not numeric position entry");
68             ok = false;
69             return PositionVector();
70         } catch (EmptyData&) {
71             emitError(report, "Shape", objecttype, objectid, "empty position entry");
72             ok = false;
73             return PositionVector();
74         }
75     }
76     return shape;
77 }
78 
79 
80 Boundary
parseBoundaryReporting(const std::string & def,const std::string & objecttype,const char * objectid,bool & ok,bool report)81 GeomConvHelper::parseBoundaryReporting(const std::string& def, const std::string& objecttype,
82                                        const char* objectid, bool& ok, bool report) {
83     StringTokenizer st(def, ",");
84     if (st.size() != 4) {
85         emitError(report, "Bounding box", objecttype, objectid, "mismatching entry number");
86         ok = false;
87         return Boundary();
88     }
89     try {
90         double xmin = StringUtils::toDouble(st.next());
91         double ymin = StringUtils::toDouble(st.next());
92         double xmax = StringUtils::toDouble(st.next());
93         double ymax = StringUtils::toDouble(st.next());
94         return Boundary(xmin, ymin, xmax, ymax);
95     } catch (NumberFormatException&) {
96         emitError(report, "Shape", objecttype, objectid, "not numeric entry");
97     } catch (EmptyData&) {
98         emitError(report, "Shape", objecttype, objectid, "empty entry");
99     }
100     ok = false;
101     return Boundary();
102 }
103 
104 
105 void
emitError(bool report,const std::string & what,const std::string & objecttype,const char * objectid,const std::string & desc)106 GeomConvHelper::emitError(bool report, const std::string& what, const std::string& objecttype,
107                           const char* objectid, const std::string& desc) {
108     if (!report) {
109         return;
110     }
111     std::ostringstream oss;
112     oss << what << " of ";
113     if (objectid == nullptr) {
114         oss << "a(n) ";
115     }
116     oss << objecttype;
117     if (objectid != nullptr) {
118         oss << " '" << objectid << "'";
119     }
120     oss << " is broken: " << desc << ".";
121     WRITE_ERROR(oss.str());
122 }
123 
124 
125 
126 /****************************************************************************/
127 
128