Skip to content

Instantly share code, notes, and snippets.

View facontidavide's full-sized avatar
🤓
Developing

Davide Faconti facontidavide

🤓
Developing
View GitHub Profile
@facontidavide
facontidavide / point_converter.hpp
Last active September 20, 2023 18:42
Convert any point 3D from one type to another.
#include <type_traits>
// Magically converts any representation of a point in 3D
// (type with x, y and z) to another one. Works with:
//
// - pcl::PointXYZ, pcl::PointXYZI, pcl::PointXYZRGB, etc
// - Eigen::Vector3d, Eigen::Vector3f
// - custom type with x,y,z fields.
// - arrays or vectors with 3 elements.
@facontidavide
facontidavide / rosPointCloud2ToPCL.hpp
Last active December 22, 2022 10:12
Drop in replacement for pcl::fromROSMsg
# include <pcl_conversions/pcl_conversions.h>
namespace pcl_df
{
// Usage: substitute pcl::fromROSMsg with pcl_df::fromROSMsg in your code and you have done.
// You should experience a considerable speedup.
template<typename T>
void fromROSMsg(const sensor_msgs::PointCloud2 &cloud, pcl::PointCloud<T> &pcl_cloud)
@facontidavide
facontidavide / string_ref_sso.h
Last active November 18, 2021 03:33
Unmutable, string reference with "small object optimization".
/**
* @brief Super simple, unmutable, string_view with
* small string optimization.
* If the string is 15 bytes or less, it is copied, otherwise,
* StringRef stores a not-owning reference.
*/
class StringRef
{
private:
@facontidavide
facontidavide / nano_serialization.hpp
Created January 20, 2021 18:54
Not much worse than boost::serialization
struct SizeStream{
unsigned size;
SizeStream(): size(0) {}
template <typename T>
typename std::enable_if<std::is_arithmetic<T>::value, SizeStream&>::type
operator <<(T& op)
{
size += sizeof(op);
@facontidavide
facontidavide / benchmark_scripting.cpp
Last active February 11, 2020 08:29
Lua vs QT JS
#include <iostream>
#include <QJSEngine>
#include "sol.hpp" //https://github.com/ThePhD/sol2
#include <benchmark/benchmark.h>
static void QmlFunction(benchmark::State& state) {
QJSEngine js;
js.evaluate("function calc(time, value){ return value*2 }");
QJSValue func = js.evaluate("calc");
@facontidavide
facontidavide / benchmark_pcl_filter.cpp
Last active January 10, 2020 11:02
Show how horribly slow is pcl::ConditionalRemoval
#include <benchmark/benchmark.h>
#include <pcl/common/centroid.h>
#include <pcl/common/common.h>
#include <pcl/filters/conditional_removal.h>
#include <pcl/io/pcd_io.h>
#include <pcl/pcl_base.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
using namespace pcl;
@facontidavide
facontidavide / copy_on_write.h
Created January 22, 2019 11:38
Copy on Write utility (from stlab)
/*
Copyright 2013 Adobe
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
/**************************************************************************************************/
#ifndef STLAB_COPY_ON_WRITE_HPP
#define STLAB_COPY_ON_WRITE_HPP
@facontidavide
facontidavide / problematic_miltithreading.cpp
Created January 9, 2019 11:03
This simple multithreading C++ may sometimes crash (SIGSEGV) or remain blocked forever inside waitStart. Can you figure out why?
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
// The purpose of the this apparently fine application is to make you appreciate thread santizers
// https://clang.llvm.org/docs/ThreadSanitizer.html
class Foo{
@facontidavide
facontidavide / unordered_with_string_view.cpp
Last active June 6, 2023 22:22
Use std::string_view to find() an element in std::unordered_map<string,T>, avoiding potential memory allocations
#include <iostream>
#include <cstring>
#include <unordered_map>
// https://github.com/martinmoene/string-view-lite
#include "string_view.hpp"
template <typename Value>
class StringMap: public std::unordered_map<std::string, Value>
{
public:
@facontidavide
facontidavide / signal.cpp
Last active July 16, 2020 12:46
Super Simple Signal Slot implementation in C++ 11
#include <stdio.h>
#include <memory>
#include <functional>
#include <list>
/**
* Super simple Signal/Slop implementation, AKA "Observable pattern".
* The subscriber is active until it goes out of scope or Subscriber::reset() is called.
*/
template <typename... CallableArgs>