Skip to content

Commit

Permalink
add defaultdict data structure for c++ && use SFINAE for get color
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyendt committed Jul 10, 2024
1 parent 288526e commit 9660b3f
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 32 deletions.
1 change: 1 addition & 0 deletions cw_tools/src/cw_all.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
#include "cw_logic.h"
#include "cw_saver.h"
#include "cw_string.h"
#include "cw_datastructure.h"
61 changes: 61 additions & 0 deletions cw_tools/src/cw_datastructure.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once

#include <unordered_map>
#include <utility> // for std::move

namespace cw {

template<typename Key, typename Value, typename Hash = std::hash<Key>>
class DefaultDict {
public:
// Default constructor
DefaultDict() : default_value(Value()) {}

// Constructor with default value
DefaultDict(const Value& default_value) : default_value(default_value) {}

// Copy constructor
DefaultDict(const DefaultDict& other) = default;

// Move constructor
DefaultDict(DefaultDict&& other) noexcept = default;

// Copy assignment
DefaultDict& operator=(const DefaultDict& other) = default;

// Move assignment
DefaultDict& operator=(DefaultDict&& other) noexcept = default;

// Operator []
Value& operator[](const Key& key) {
return data[key];
}

const Value& operator[](const Key& key) const {
auto it = data.find(key);
if (it != data.end()) {
return it->second;
} else {
return default_value;
}
}

std::size_t size() const {
return data.size();
}

void clear() {
data.clear();
}

// Iterator support
auto begin() { return data.begin(); }
auto end() { return data.end(); }
auto begin() const { return data.begin(); }
auto end() const { return data.end(); }

private:
std::unordered_map<Key, Value, Hash> data;
Value default_value;
};
}
115 changes: 83 additions & 32 deletions cw_tools/src/cw_plot.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,93 @@
#include <opencv2/opencv.hpp>

namespace cw {
inline std::vector<cv::Scalar> colors = {
cv::Scalar(255, 0, 0), // 红色
cv::Scalar(0, 255, 0), // 绿色
cv::Scalar(0, 0, 255), // 蓝色
cv::Scalar(255, 255, 0), // 黄色
cv::Scalar(255, 0, 255), // 洋红
cv::Scalar(0, 255, 255), // 青色
cv::Scalar(128, 0, 0), // 栗色
cv::Scalar(128, 128, 0), // 橄榄色
cv::Scalar(0, 128, 0), // 暗绿色
cv::Scalar(128, 0, 128), // 紫色
cv::Scalar(0, 128, 128), // 水鸭色
cv::Scalar(0, 0, 128), // 海军蓝
cv::Scalar(255, 165, 0), // 橙色
cv::Scalar(255, 20, 147), // 深粉色
cv::Scalar(75, 0, 130), // 靛色
cv::Scalar(173, 216, 230),// 浅蓝色
cv::Scalar(139, 69, 19), // 巧克力色
cv::Scalar(255, 192, 203),// 粉色
cv::Scalar(255, 218, 185),// 桃色
cv::Scalar(47, 79, 79), // 暗灰色
cv::Scalar(105, 105, 105),// 灰色
cv::Scalar(220, 20, 60), // 猩红
cv::Scalar(0, 255, 127), // 春绿色
cv::Scalar(255, 105, 180) // 热粉色
};

inline cv::Scalar get_random_color() {
namespace {
inline const std::vector<cv::Scalar> cv_colors = {
cv::Scalar(255, 0, 0), // 红色
cv::Scalar(0, 255, 0), // 绿色
cv::Scalar(0, 0, 255), // 蓝色
cv::Scalar(255, 255, 0), // 黄色
cv::Scalar(255, 0, 255), // 洋红
cv::Scalar(0, 255, 255), // 青色
cv::Scalar(128, 0, 0), // 栗色
cv::Scalar(128, 128, 0), // 橄榄色
cv::Scalar(0, 128, 0), // 暗绿色
cv::Scalar(128, 0, 128), // 紫色
cv::Scalar(0, 128, 128), // 水鸭色
cv::Scalar(0, 0, 128), // 海军蓝
cv::Scalar(255, 165, 0), // 橙色
cv::Scalar(255, 20, 147), // 深粉色
cv::Scalar(75, 0, 130), // 靛色
cv::Scalar(173, 216, 230),// 浅蓝色
cv::Scalar(139, 69, 19), // 巧克力色
cv::Scalar(255, 192, 203),// 粉色
cv::Scalar(255, 218, 185),// 桃色
cv::Scalar(47, 79, 79), // 暗灰色
cv::Scalar(105, 105, 105),// 灰色
cv::Scalar(220, 20, 60), // 猩红
cv::Scalar(0, 255, 127), // 春绿色
cv::Scalar(255, 105, 180) // 热粉色
};

inline const std::vector<Eigen::Vector3i> eigen_colors = {
Eigen::Vector3i(255, 0, 0), // 红色
Eigen::Vector3i(0, 255, 0), // 绿色
Eigen::Vector3i(0, 0, 255), // 蓝色
Eigen::Vector3i(255, 255, 0), // 黄色
Eigen::Vector3i(255, 0, 255), // 洋红
Eigen::Vector3i(0, 255, 255), // 青色
Eigen::Vector3i(128, 0, 0), // 栗色
Eigen::Vector3i(128, 128, 0), // 橄榄色
Eigen::Vector3i(0, 128, 0), // 暗绿色
Eigen::Vector3i(128, 0, 128), // 紫色
Eigen::Vector3i(0, 128, 128), // 水鸭色
Eigen::Vector3i(0, 0, 128), // 海军蓝
Eigen::Vector3i(255, 165, 0), // 橙色
Eigen::Vector3i(255, 20, 147), // 深粉色
Eigen::Vector3i(75, 0, 130), // 靛色
Eigen::Vector3i(173, 216, 230),// 浅蓝色
Eigen::Vector3i(139, 69, 19), // 巧克力色
Eigen::Vector3i(255, 192, 203),// 粉色
Eigen::Vector3i(255, 218, 185),// 桃色
Eigen::Vector3i(47, 79, 79), // 暗灰色
Eigen::Vector3i(105, 105, 105),// 灰色
Eigen::Vector3i(220, 20, 60), // 猩红
Eigen::Vector3i(0, 255, 127), // 春绿色
Eigen::Vector3i(255, 105, 180) // 热粉色
};

template <typename T, typename Enable = void>
struct ColorTraits;

template <typename T>
struct ColorTraits<T, std::enable_if_t<std::is_same_v<T, cv::Scalar>>> {
static const std::vector<cv::Scalar>& colors() {
return cv_colors;
}
};

template <typename T>
struct ColorTraits<T, std::enable_if_t<std::is_same_v<T, Eigen::Vector3i>>> {
static const std::vector<Eigen::Vector3i>& colors() {
return eigen_colors;
}
};

}

template <typename ColorType>
inline ColorType get_color(int index) {
auto colors = ColorTraits<ColorType>::colors();
return colors[index % colors.size()];
}

template <typename ColorType>
inline ColorType get_random_color() {
static std::random_device rd;
static std::mt19937 gen(rd());
auto colors = ColorTraits<ColorType>::colors();
std::uniform_int_distribution<> dis(0, colors.size() - 1);
int rand_int = dis(gen);
return colors[rand_int];
}

inline cv::Scalar get_color(int index) {
return colors[index % colors.size()];
}
}

0 comments on commit 9660b3f

Please sign in to comment.