Skip to content

Instantly share code, notes, and snippets.

@insaneyilin
insaneyilin / welford.h
Created November 29, 2021 11:44
Welford's online algorithm for calculating variance
#pragma once
#include <cmath>
// https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_online_algorithm
template <typename T>
class WelfordMeanStddevCalculator {
public:
WelfordMeanStddevCalculator() = default;
virtual ~WelfordMeanStddevCalculator() = default;
@insaneyilin
insaneyilin / va_args_macro.cc
Last active September 6, 2021 03:11
__VA_ARGS__ usage
#define MODULE_NAME "MY_LIB"
// __VA_ARGS__ 对应宏定义中参数列表的最后一个参数为省略号(也就是三个点)
// 加上##的作用在于,当可变参数的个数为0时,这里的##起到把前面多余的","去掉的作用,否则会编译出错
#define error_print(fmt, ...) printf("[ERROR]["MODULE_NAME"](%s|%d)" fmt, __func__, __LINE__, ##__VA_ARGS__)
// 单个 # 用来把参数转换成字符串
// ## 是个粘合剂,将前后两部分粘合起来,也就是有“字符化”的意思。但是“##”不能随意粘合任意字符,必须是合法的C语言标示符。在单一的宏定义中,最多可以出现一次“#”或“##”预处理操作符。
@insaneyilin
insaneyilin / histogram_dist.cc
Created August 5, 2021 07:52
The Hellinger distance measures the similarity of two probability
// The Hellinger distance measures the similarity of two probability
// distributions Reference: https://en.wikipedia.org/wiki/Hellinger_distance
double HellingerDistance(
const std::vector<double>& histogram1,
const std::vector<double>& histogram2) {
const int histogram_size = histogram1.size();
CHECK_GT(histogram_size, 0);
CHECK_EQ(histogram_size, histogram2.size());
const double mean1 =
std::accumulate(histogram1.begin(), histogram1.end(), 0.0) /
@insaneyilin
insaneyilin / zero_leading_filename_list.cc
Created March 16, 2021 05:57
generate filename list with leading zeros
#include <iostream>
#include <sstream>
#include <iomanip>
int main() {
for (int i = 0; i < 10; ++i) {
std::stringstream ss;
ss << std::setfill('0') << std::setw(8) << std::fixed << i << ".txt";
std::cout << ss.str() << std::endl;
}
@insaneyilin
insaneyilin / pcd2bin.py
Created March 12, 2021 05:50
convert pcd to bin file
import numpy as np
import pypcd
filepath='hesai_pandar40p_1593421778.828716.pcd'
pc = pypcd.PointCloud.from_path(filepath)
points = []
for pt in pc.pc_data:
points.append([pt[0], pt[1], pt[2], 0.0])
@insaneyilin
insaneyilin / least_square_line_fitting.cc
Created January 20, 2021 16:00
C++ least square line fitting
// https://www.varsitytutors.com/hotmath/hotmath_help/topics/line-of-best-fit.html
double CalcLineSlope(const std::vector<double>& x,
const std::vector<double>& y) {
const auto n = x.size();
const auto s_x = std::accumulate(x.begin(), x.end(), 0.0);
const auto s_y = std::accumulate(y.begin(), y.end(), 0.0);
const auto s_xx = std::inner_product(x.begin(), x.end(), x.begin(), 0.0);
const auto s_xy = std::inner_product(x.begin(), x.end(), y.begin(), 0.0);
const auto slope = (n * s_xy - s_x * s_y) / (n * s_xx - s_x * s_x);
@insaneyilin
insaneyilin / std_make_heap_example.cc
Created January 1, 2021 02:39
C++ std::make_heap() std::push_heap() std::pop_heap() example
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
void print_vec(std::vector<int> &vec) {
for (auto x : vec) {
std::cout << x << " ";
}
std::cout << "\n";
@insaneyilin
insaneyilin / opencv_draw_dashed_dotted_line.cc
Created January 1, 2021 02:34
Draw Dashed/Dotted line in OpenCV
// reference: https://ddorobot.tistory.com/entry/OpenCV-Draw-DotDash-Line
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include "opencv2/opencv.hpp"
void DrawDashedLine(cv::Mat& img, cv::Point pt1, cv::Point pt2,
@insaneyilin
insaneyilin / cmp_np_arrays.py
Created December 19, 2020 11:15
compare two numpy arrays
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
{Description}
"""
from __future__ import print_function
import os
import sys
@insaneyilin
insaneyilin / hconcat_images.py
Created July 2, 2020 09:58
Concatenate images with python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import sys
import argparse
import cv2