Flatland
layer.h
1 /*
2  * ______ __ __ __
3  * /\ _ \ __ /\ \/\ \ /\ \__
4  * \ \ \L\ \ __ __ /\_\ \_\ \ \ \____ ___\ \ ,_\ ____
5  * \ \ __ \/\ \/\ \\/\ \ /'_` \ \ '__`\ / __`\ \ \/ /',__\
6  * \ \ \/\ \ \ \_/ |\ \ \/\ \L\ \ \ \L\ \/\ \L\ \ \ \_/\__, `\
7  * \ \_\ \_\ \___/ \ \_\ \___,_\ \_,__/\ \____/\ \__\/\____/
8  * \/_/\/_/\/__/ \/_/\/__,_ /\/___/ \/___/ \/__/\/___/
9  * @copyright Copyright 2017 Avidbots Corp.
10  * @name layer.h
11  * @brief Defines flatland Layer
12  * @author Chunshang Li
13  *
14  * Software License Agreement (BSD License)
15  *
16  * Copyright (c) 2017, Avidbots Corp.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  *
23  * * Redistributions of source code must retain the above copyright
24  * notice, this list of conditions and the following disclaimer.
25  * * Redistributions in binary form must reproduce the above
26  * copyright notice, this list of conditions and the following
27  * disclaimer in the documentation and/or other materials provided
28  * with the distribution.
29  * * Neither the name of the Avidbots Corp. nor the names of its
30  * contributors may be used to endorse or promote products derived
31  * from this software without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
36  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
38  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
39  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
43  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44  * POSSIBILITY OF SUCH DAMAGE.
45  */
46 
47 #ifndef FLATLAND_SERVER_LAYER_H
48 #define FLATLAND_SERVER_LAYER_H
49 
50 #include <Box2D/Box2D.h>
51 #include <flatland_server/body.h>
52 #include <flatland_server/collision_filter_registry.h>
53 #include <flatland_server/entity.h>
54 #include <flatland_server/types.h>
55 #include <yaml-cpp/yaml.h>
56 #include <opencv2/opencv.hpp>
57 #include <string>
58 
59 namespace flatland_server {
60 
65 class Layer : public Entity {
66  public:
67  std::vector<std::string> names_;
68 
69  Body *body_ = nullptr;
71  std::string viz_name_;
72 
88  Layer(b2World *physics_world, CollisionFilterRegistry *cfr,
89  const std::vector<std::string> &names, const Color &color,
90  const Pose &origin, const cv::Mat &bitmap, double occupied_thresh,
91  double resolution, const YAML::Node &properties);
92 
108  Layer(b2World *physics_world, CollisionFilterRegistry *cfr,
109  const std::vector<std::string> &names, const Color &color,
110  const Pose &origin, const std::vector<LineSegment> &line_segments,
111  double scale, const YAML::Node &properties);
112 
122  Layer(b2World *physics_world, CollisionFilterRegistry *cfr,
123  const std::vector<std::string> &names, const Color &color,
124  const YAML::Node &properties);
125 
129  ~Layer();
130 
134  const std::vector<std::string> &GetNames() const;
135 
139  const CollisionFilterRegistry *GetCfr() const;
140 
141  Body *GetBody();
142 
147  EntityType Type() const { return EntityType::LAYER; }
148 
155  void LoadFromBitmap(const cv::Mat &bitmap, double occupied_thresh,
156  double resolution);
157 
161  void DebugVisualize() const override;
162 
166  void DebugOutput() const override;
167 
174  static void ReadLineSegmentsFile(const std::string &file_path,
175  std::vector<LineSegment> &line_segments);
176 
191  static Layer *MakeLayer(b2World *physics_world, CollisionFilterRegistry *cfr,
192  const std::string &map_path,
193  const std::vector<std::string> &names,
194  const Color &color, const YAML::Node &properties);
195 };
196 }; // namespace flatland_server
197 #endif // FLATLAND_SERVER_WORLD_H
EntityType
Defines the type of entity.
Definition: entity.h:64
Definition: collision_filter_registry.h:63
const CollisionFilterRegistry * GetCfr() const
Definition: layer.cpp:119
std::string viz_name_
for visualization
Definition: layer.h:71
Definition: entity.h:61
~Layer()
Destructor for the layer class.
Definition: layer.cpp:115
Definition: body.h:55
Definition: layer.h:65
const std::vector< std::string > & GetNames() const
Definition: layer.cpp:117
std::vector< std::string > names_
list of layer names
Definition: layer.h:67
static Layer * MakeLayer(b2World *physics_world, CollisionFilterRegistry *cfr, const std::string &map_path, const std::vector< std::string > &names, const Color &color, const YAML::Node &properties)
Factory method to instantiate a layer, throws exceptions upon failure.
Definition: layer.cpp:122
Definition: types.h:84
Definition: types.h:110
static void ReadLineSegmentsFile(const std::string &file_path, std::vector< LineSegment > &line_segments)
Read line segments from a file, each line of a file represents a line segment in the form of x1 y1 x2...
Definition: layer.cpp:182
Layer(b2World *physics_world, CollisionFilterRegistry *cfr, const std::vector< std::string > &names, const Color &color, const Pose &origin, const cv::Mat &bitmap, double occupied_thresh, double resolution, const YAML::Node &properties)
Constructor for the Layer class for initialization using a image map file.
Definition: layer.cpp:65
CollisionFilterRegistry * cfr_
collision filter registry
Definition: layer.h:70
void DebugOutput() const override
log debug messages for the layer
Definition: layer.cpp:326
EntityType Type() const
Return the type of entity.
Definition: layer.h:147
Definition: body.h:61
void DebugVisualize() const override
Visualize layer for debugging purposes.
Definition: layer.cpp:309
void LoadFromBitmap(const cv::Mat &bitmap, double occupied_thresh, double resolution)
Load the map by extracting edges from images.
Definition: layer.cpp:214