From 191ba733b14c2bf7e213563b6ffd900513c6d821 Mon Sep 17 00:00:00 2001 From: feixyz10 Date: Sat, 27 Feb 2021 17:55:29 +0800 Subject: [PATCH 1/2] add factory ... --- common/macro/macros.h | 23 +++++++++-------------- common/math/fitting.h | 14 -------------- common/pcl/pcl_utils.h | 3 +-- common/registerer/registerer.cc | 0 common/registerer/registerer.h | 19 +++++++++++++++++++ common/time/timer.cc | 4 ++++ common/time/timer.h | 4 +--- 7 files changed, 34 insertions(+), 33 deletions(-) create mode 100644 common/registerer/registerer.cc create mode 100644 common/registerer/registerer.h diff --git a/common/macro/macros.h b/common/macro/macros.h index b7108ec..96a2e1f 100644 --- a/common/macro/macros.h +++ b/common/macro/macros.h @@ -12,18 +12,13 @@ classname &operator=(const classname &) = delete; // adapted form baidu apollo cyber/common/macros.h -#define DECLARE_SINGLETON(classname) \ - public: \ - static classname *Instance() { \ - static std::unique_ptr instance{nullptr}; \ - if (!instance) { \ - static std::once_flag flag; \ - std::call_once(flag, \ - [&] { instance.reset(new (std::nothrow) classname()); }); \ - } \ - return instance.get(); \ - } \ - \ - private: \ - classname() = default; \ +#define DECLARE_SINGLETON(classname) \ + public: \ + static classname *Instance() { \ + static classname instance; \ + return &instance; \ + } \ + \ + private: \ + classname() = default; \ DISALLOW_COPY_AND_ASSIGN(classname) diff --git a/common/math/fitting.h b/common/math/fitting.h index 9c2e756..ebd7030 100644 --- a/common/math/fitting.h +++ b/common/math/fitting.h @@ -74,18 +74,4 @@ Eigen::Vector4d FitPlane(const pcl::PointCloud &cloud, return coeffs; } -// template -// Eigen::Vector4d FitPlane(const pcl::PointCloud &cloud) { -// Eigen::MatrixX3f A(cloud.size(), 3); // NOLINT -// Eigen::VectorXf b(cloud.size()); -// b.setConstant(-1.0); -// size_t i = 0; -// for (const auto &p : cloud) { -// A.row(i++) << p.x, p.y, p.z; -// } -// Eigen::Vector3f sol = A.colPivHouseholderQr().solve(b); -// Eigen::Vector4d coeff(sol(0), sol(1), sol(2), 1.0); -// return coeff / sol.norm(); -// } - } // namespace common \ No newline at end of file diff --git a/common/pcl/pcl_utils.h b/common/pcl/pcl_utils.h index fe64b4c..9ff213b 100644 --- a/common/pcl/pcl_utils.h +++ b/common/pcl/pcl_utils.h @@ -62,7 +62,6 @@ template inline void TransformPointCloud(const Pose3d &pose, const pcl::PointCloud &cloud_in, pcl::PointCloud *const cloud_out) { - ACHECK(cloud_out); pcl::transformPointCloud(cloud_in, *cloud_out, pose.TransMat().cast()); } @@ -78,7 +77,7 @@ void RemovePoints(const pcl::PointCloud &cloud_in, } size_t j = 0; for (size_t i = 0; i < cloud_in.size(); ++i) { - const auto pt = cloud_in.points[i]; + const auto &pt = cloud_in.points[i]; if (check(pt)) { if (removed_indices) removed_indices->push_back(i); continue; diff --git a/common/registerer/registerer.cc b/common/registerer/registerer.cc new file mode 100644 index 0000000..e69de29 diff --git a/common/registerer/registerer.h b/common/registerer/registerer.h new file mode 100644 index 0000000..ccf8205 --- /dev/null +++ b/common/registerer/registerer.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include +#include + +#include "common/log/log.h" +#include "common/macro/macros.h" + +template +class AbstractFactory { + virtual ~AbstractFactory() = default; + virtual std::shared_ptr Create() = 0; +}; + +template +class ConcreteFactory { + virtual std::shared_ptr Create() = 0; +}; \ No newline at end of file diff --git a/common/time/timer.cc b/common/time/timer.cc index 057ae25..45c8305 100644 --- a/common/time/timer.cc +++ b/common/time/timer.cc @@ -4,6 +4,10 @@ namespace common { +void Timer::Tic() { + start_ = std::chrono::system_clock::now(); +} + double Timer::Toc(char unit) { ACHECK(unit == 's' || unit == 'm' || unit == 'u') << "Only 's'(second), 'm'(millisecond) and 'u'(microsecond) are " diff --git a/common/time/timer.h b/common/time/timer.h index 833f2b3..5e66930 100644 --- a/common/time/timer.h +++ b/common/time/timer.h @@ -13,9 +13,7 @@ class Timer { Tic(); } - void Tic() { - start_ = std::chrono::system_clock::now(); - } + void Tic(); // unit: 's' = second, 'm' = millisecond, 'u' = microsecond double Toc(char unit = 'm'); From 8e70b81e192f8a76e7698dec7010e772931e30f6 Mon Sep 17 00:00:00 2001 From: feixyz10 Date: Mon, 1 Mar 2021 17:36:57 +0800 Subject: [PATCH 2/2] add factor registerer --- common/registerer/registerer.cc | 0 common/registerer/registerer.h | 60 ++++++++++++++++++++++--- ggg.txt | 42 ----------------- oh_my_loam/configs/config.yaml | 2 +- oh_my_loam/extractor/extractor_VLP16.cc | 3 ++ oh_my_loam/oh_my_loam.cc | 6 +-- 6 files changed, 62 insertions(+), 51 deletions(-) delete mode 100644 common/registerer/registerer.cc delete mode 100644 ggg.txt diff --git a/common/registerer/registerer.cc b/common/registerer/registerer.cc deleted file mode 100644 index e69de29..0000000 diff --git a/common/registerer/registerer.h b/common/registerer/registerer.h index ccf8205..20919d5 100644 --- a/common/registerer/registerer.h +++ b/common/registerer/registerer.h @@ -5,15 +5,65 @@ #include #include "common/log/log.h" -#include "common/macro/macros.h" + +namespace common { template class AbstractFactory { + public: virtual ~AbstractFactory() = default; - virtual std::shared_ptr Create() = 0; + virtual BaseClass *Create() = 0; }; template -class ConcreteFactory { - virtual std::shared_ptr Create() = 0; -}; \ No newline at end of file +class ConcreteFactory : public AbstractFactory { + public: + BaseClass *Create() override { + return new DerivedClass; + } +}; + +template +inline auto &GetFactoryMap() { + static std::map>> map; + return map; +} + +template +class Registerer { + public: + template + static void Register(const std::string &derived_class_name) { + static_assert(std::is_base_of::value, ""); + auto &factory_map = GetFactoryMap(); + if (factory_map.find(derived_class_name) != factory_map.end()) { + return; + } + factory_map[derived_class_name].reset( + new ConcreteFactory); + } + + static BaseClass *NewInstance(const std::string &derived_class_name) { + auto &factory_map = GetFactoryMap(); + auto iter = factory_map.find(derived_class_name); + if (iter == factory_map.end()) { + AFATAL << "Class not registered: " << derived_class_name << "."; + return nullptr; + } + return iter->second->Create(); + } + + static bool IsRegistered(const std::string &derived_class_name) { + auto &factory_map = GetFactoryMap(); + return factory_map.find(derived_class_name) != factory_map.end(); + } +}; + +} // namespace common + +#define REGISTER_CLASS(base_class, derived_class) \ + namespace { \ + __attribute__((constructor)) void Register##derived_class() { \ + ::common::Registerer::Register(#derived_class); \ + } \ + } // namespace \ No newline at end of file diff --git a/ggg.txt b/ggg.txt deleted file mode 100644 index 3ecd0d1..0000000 --- a/ggg.txt +++ /dev/null @@ -1,42 +0,0 @@ -scan line number 16 -line resolution 0.200000 plane resolution 0.400000 -Mapping 10 Hz -... logging to /home/liufei/.ros/log/8dd723e0-69e0-11eb-931b-4cedfbbfbb36/roslaunch-liufei-fabu-4816.log -Checking log directory for disk usage. This may take awhile. -Press Ctrl-C to interrupt -Done checking log file disk usage. Usage is <1GB. -]2;/home/liufei/catkin_ws/src/A-LOAM/launch/aloam_velodyne_VLP_16.launch -started roslaunch server http://liufei-fabu:38301/ - -SUMMARY -======== - -PARAMETERS - * /mapping_line_resolution: 0.2 - * /mapping_plane_resolution: 0.4 - * /mapping_skip_frame: 1 - * /minimum_range: 0.3 - * /rosdistro: kinetic - * /rosversion: 1.12.16 - * /scan_line: 16 - -NODES - / - alaserMapping (aloam_velodyne/alaserMapping) - alaserOdometry (aloam_velodyne/alaserOdometry) - ascanRegistration (aloam_velodyne/ascanRegistration) - rviz (rviz/rviz) - -ROS_MASTER_URI=http://localhost:11311 -]2;/home/liufei/catkin_ws/src/A-LOAM/launch/aloam_velodyne_VLP_16.launch http://localhost:11311 -process[ascanRegistration-1]: started with pid [4854] -process[alaserOdometry-2]: started with pid [4855] -process[alaserMapping-3]: started with pid [4856] -process[rviz-4]: started with pid [4857] -[rviz-4] killing on exit -[alaserMapping-3] killing on exit -[alaserOdometry-2] killing on exit -[ascanRegistration-1] killing on exit -shutting down processing monitor... -... shutting down processing monitor complete -done diff --git a/oh_my_loam/configs/config.yaml b/oh_my_loam/configs/config.yaml index 147f537..93f46c7 100644 --- a/oh_my_loam/configs/config.yaml +++ b/oh_my_loam/configs/config.yaml @@ -1,5 +1,5 @@ # global configs -lidar: VPL16 +lidar: VLP16 log_to_file: false log_path: /data/log/oh_my_loam vis: true diff --git a/oh_my_loam/extractor/extractor_VLP16.cc b/oh_my_loam/extractor/extractor_VLP16.cc index dc419d7..171f8ca 100644 --- a/oh_my_loam/extractor/extractor_VLP16.cc +++ b/oh_my_loam/extractor/extractor_VLP16.cc @@ -1,6 +1,7 @@ #include "extractor_VLP16.h" #include "common/math/math_utils.h" +#include "common/registerer/registerer.h" namespace oh_my_loam { @@ -10,4 +11,6 @@ int ExtractorVLP16::GetScanID(const common::Point &pt) const { return static_cast(std::round(theta / 2.0) + 1.e-5); }; +REGISTER_CLASS(Extractor, ExtractorVLP16) + } // namespace oh_my_loam \ No newline at end of file diff --git a/oh_my_loam/oh_my_loam.cc b/oh_my_loam/oh_my_loam.cc index 4a8238c..09278a8 100644 --- a/oh_my_loam/oh_my_loam.cc +++ b/oh_my_loam/oh_my_loam.cc @@ -2,9 +2,8 @@ #include -#include "common/common.h" #include "common/pcl/pcl_utils.h" -#include "oh_my_loam/extractor/extractor_VLP16.h" +#include "common/registerer/registerer.h" namespace oh_my_loam { @@ -15,7 +14,8 @@ const double kPointMinDist = 0.5; bool OhMyLoam::Init() { config_ = common::YAMLConfig::Instance()->config(); is_vis_ = config_["vis"].as(); - extractor_.reset(new ExtractorVLP16); + extractor_.reset(common::Registerer::NewInstance( + "Extractor" + config_["lidar"].as())); if (!extractor_->Init()) { AERROR << "Failed to initialize extractor"; return false;