Fawkes API  Fawkes Development Version
generator.cpp
1 
2 /***************************************************************************
3  * generator.cpp - navgraph generator interface and base class
4  *
5  * Created: Thu Mar 16 11:00:22 2017
6  * Copyright 2015-2017 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version. A runtime exception applies to
13  * this software (see LICENSE.GPL_WRE file mentioned below for details).
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
21  */
22 
23 #include <core/exception.h>
24 #include <navgraph/generators/generator.h>
25 
26 namespace fawkes {
27 
28 /** @class NavGraphGenerator <navgraph/generators/generator.h>
29  * Base class for navgraph generators.
30  * This class cannot be instantiated and used directly, rather it defines the
31  * general interface of specific generators.
32  *
33  * To implementers of generators this provides some basic interface to
34  * define a bounding box, a near threshold, and to add obstacles. Note that
35  * these features may not be used by some generators. See the respective
36  * documentation.
37  *
38  * If a bounding box is provided, it shall be defined befined on the ground
39  * plane in a right-handed coordinate system (the same in which all obstacles
40  * are defined) by two points P1 and P2. The point P1 shall be the "lower left"
41  * corner and P2 the "upper right" corner, i.e., P1 < P2, as in P1.x < P2.x and
42  * P1.y < P2.y.
43  *
44  * @fn void NavGraphGenerator::compute(fawkes::LockPtr<fawkes::NavGraph> graph) = 0
45  * Compute graph.
46  * @param graph the resulting nodes and edges will be added to this graph.
47  * The graph shall *not* be cleared automatically. The generator shall
48  * lock the graph as necessary.
49  *
50  * @author Tim Niemueller
51  */
52 
53 /** Default constructor.
54  * Disabled bounding box, set near threshold to 1cm.
55  */
57 : bbox_enabled_(false),
58  bbox_p1_x_(0.),
59  bbox_p1_y_(0.),
60  bbox_p2_x_(0.),
61  bbox_p2_y_(0.),
62  near_threshold_(0.01)
63 {
64 }
65 
66 /** Parametrized constructor.
67  * @param params parameters
68  */
69 NavGraphGenerator::NavGraphGenerator(const std::map<std::string, std::string> params)
70 : bbox_enabled_(false),
71  bbox_p1_x_(0.),
72  bbox_p1_y_(0.),
73  bbox_p2_x_(0.),
74  bbox_p2_y_(0.),
75  near_threshold_(0.01),
76  params_(params)
77 {
78 }
79 
80 /** Destructor. */
82 {
83 }
84 
85 /** Generate a new name
86  * @param i number parameter for point name, will be incremented by one
87  * @return string with a new point name
88  */
89 std::string
90 NavGraphGenerator::genname(unsigned int &i)
91 {
92  char *name;
93  if (asprintf(&name, "V_%02u", ++i) != -1) {
94  std::string rv = name;
95  free(name);
96  return rv;
97  } else {
98  throw Exception("Failed to create node name");
99  }
100 }
101 
102 /** Set bounding box.
103  * Setting a bounding box will cause compute() to ignore any edge with
104  * a vertex out of the given bounding box area.
105  * @param bbox_p1_x X coordinate of first (lower) bounding box point
106  * @param bbox_p1_y y coordinate of first (lower) bounding box point
107  * @param bbox_p2_x X coordinate of second (upper) bounding box point
108  * @param bbox_p2_y y coordinate of second (upper) bounding box point
109  */
110 void
112  float bbox_p1_y,
113  float bbox_p2_x,
114  float bbox_p2_y)
115 {
116  bbox_enabled_ = true;
117  bbox_p1_x_ = bbox_p1_x;
118  bbox_p1_y_ = bbox_p1_y;
119  bbox_p2_x_ = bbox_p2_x;
120  bbox_p2_y_ = bbox_p2_y;
121 }
122 
123 /** Set distance threshold for considering nodes to be the same.
124  * @param near_threshold distance threshold for which to consider
125  * nodes to be the same if the distance is smaller than this
126  * threshold.
127  */
128 void
129 NavGraphGenerator::set_near_threshold(float near_threshold)
130 {
131  near_threshold_ = near_threshold;
132 }
133 
134 /** Add an obstacle point.
135  * An obstacle point will be the representative for a Voronoi
136  * face in the newly generated graph.
137  * @param x X coordinate of point
138  * @param y Y coordinate of point
139  */
140 void
141 NavGraphGenerator::add_obstacle(float x, float y)
142 {
143  obstacles_.push_back(std::make_pair(x, y));
144 }
145 
146 } // end of namespace fawkes
fawkes::NavGraphGenerator::set_bounding_box
virtual void set_bounding_box(float bbox_p1_x, float bbox_p1_y, float bbox_p2_x, float bbox_p2_y)
Set bounding box.
Definition: generator.cpp:114
fawkes::NavGraphGenerator::~NavGraphGenerator
virtual ~NavGraphGenerator()
Destructor.
Definition: generator.cpp:84
fawkes::NavGraphGenerator::bbox_p2_y_
float bbox_p2_y_
Y part of P2 for bounding box.
Definition: generator.h:57
fawkes::NavGraphGenerator::add_obstacle
virtual void add_obstacle(float x, float y)
Add an obstacle point.
Definition: generator.cpp:144
fawkes::NavGraphGenerator::bbox_p1_y_
float bbox_p1_y_
Y part of P1 for bounding box.
Definition: generator.h:55
fawkes::NavGraphGenerator::bbox_enabled_
bool bbox_enabled_
True if bounding box requested, false otherwise.
Definition: generator.h:53
fawkes::NavGraphGenerator::near_threshold_
float near_threshold_
distance threshold when to consider two nodes to be the same
Definition: generator.h:58
fawkes::NavGraphGenerator::NavGraphGenerator
NavGraphGenerator()
Default constructor.
Definition: generator.cpp:59
fawkes
fawkes::NavGraphGenerator::obstacles_
std::list< std::pair< float, float > > obstacles_
Obstacles to consider during navgraph generation.
Definition: generator.h:61
fawkes::NavGraphGenerator::genname
static std::string genname(unsigned int &i)
Generate a new name.
Definition: generator.cpp:93
fawkes::NavGraphGenerator::bbox_p1_x_
float bbox_p1_x_
X part of P1 for bounding box.
Definition: generator.h:54
fawkes::NavGraphGenerator::bbox_p2_x_
float bbox_p2_x_
X part of P2 for bounding box.
Definition: generator.h:56
fawkes::NavGraphGenerator::set_near_threshold
virtual void set_near_threshold(float near_threshold)
Set distance threshold for considering nodes to be the same.
Definition: generator.cpp:132
fawkes::Exception
Definition: exception.h:39