Skip to content

Instantly share code, notes, and snippets.

View mtao's full-sized avatar

Michael Tao mtao

View GitHub Profile
@mtao
mtao / make_unique_remove_void.cpp
Created October 16, 2023 00:06
Getting unique types + removing void from a sequence of types
#include <tuple>
#include <type_traits>
template <typename T, typename U>
struct concatenate_tuple {};
template <typename... Ts, typename... Us>
struct concatenate_tuple<std::tuple<Ts...>, std::tuple<Us...>> {
using type = std::tuple<Ts..., Us...>;
};
template <typename T, typename U>
@mtao
mtao / concate_VF_meshes.cpp
Created October 9, 2023 22:38
concatenates IGL style std::vectors.
#include <Eigen/Core>
#include <numeric>
#include <tuple>
#include <iterator>
#include <algorithm>
#include <iostream>
template <typename BeginIt, typename EndIt>
auto vstack_iter(BeginIt beginit, EndIt endit) {
using CDerived = typename std::decay_t<decltype(*beginit)>;
@mtao
mtao / cache_derived_return_values.cpp
Last active October 8, 2023 23:50
Generic caching of multiple return types when switching between compile time and runtime polymorphism
#include <spdlog/spdlog.h>
#include <functional>
#include <map>
#include <type_traits>
#include <utility>
#include <variant>
// Say you have a few heavy data structures derived from a single base class
// that has a value/enum that can be used to identify which derived class you have.
@mtao
mtao / igl_concepts_example.cpp
Last active December 18, 2022 22:20
IGL + Concepts = Cleaner function declarations
#include <Eigen/Core>
#include <concepts>
#include <iostream>
// This example code shows how concepts could be used to lighten the syntax used
// in declaring igl style function input/outputs. In particular, inputs are
// typically derived from MatrixBase (which can be accessed directly) and
// outputs are derived from PlainObjectBase (which has memory to store values).
//
// Below there are three examples:
@mtao
mtao / brightness.sh
Created December 15, 2020 03:52
Updates the brightness of a screen using xrandr.
#!/bin/bash
# Changes brightness as a float value in [0,1], using +N -N induces a relative
# change in brightness
# Usage:
# brightness.sh .8 # set brightness to .8
# brightness.sh -.1 # decrement brightness by .1
# brightness.sh +.1 # increment brightness by .1
# helper to do floating point evaluation
#include <iostream>
#include <Eigen/Dense>
#include <concepts>
namespace Eigen {
template <int D, typename Derived>
inline auto& get(Eigen::DenseCoeffsBase<Derived, WriteAccessors>& p) { return p.coeffRef(D); }
template <int D, typename Derived>
inline const auto get(const Eigen::DenseCoeffsBase<Derived,ReadOnlyAccessors>& p) { return p.coeff(D); }
@mtao
mtao / easy_caching.cpp
Created June 9, 2020 00:03
a quick/dirty implementation of a return value caching mechanism
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <tuple>
// all caching classes need to have a basic inventory of dirty flags and a tool
// to flush them.
class CachableType {
template <typename RetType, typename... Args>
@mtao
mtao / igl_mesh_read comments_results.txt
Last active May 30, 2020 01:41
some relatively naive performance attempts on igl's mesh loader
compiled with `clang++ -O3 -std=c++17 -march=native igl_mesh_read_test.cpp -o igl_mesh_read_test`
Hit something unexpected (unless i'm doing something stupid). In particular, the cost of pre-reading a file to pre-allocate the `vector<vector<double>>` data outweighs the cost of reading in a mesh. Changing the data type that the loader assumes is of size 3 to `vector<array<double,3>` increased performance. I thought that increasing the size of the heap objects being allocated would make pre-sizing more important but it didn't; in fact my preliminary tests said that it made results even slower. My methodology of using `reserve()` then `.push_back` could be faulty (perhaps `resize()` -> `value[idx] = val` will actually work better?
Format of the results are "igl base impl || vector file scan || array file scan || just with array"
File /home/mtao/Downloads/20131013_Venus2.obj took 2576 || 2733 || 2701 || 2503.33 ms/load (7728 || 8200 || 8105 || 7510ms total)
File /home/mtao/git/quartet/meshes/block.obj took 61
@mtao
mtao / eigen_ref.cpp
Last active March 16, 2023 15:07
a little explanation of what Eigen::Ref can be used for
#include <Eigen/Dense>
#include <cassert>
#include <iostream>
// the ideal way to implement a cross product function is to depend on eigen's
// builtin
template <typename XDerived, typename YDerived>
auto cross_t(const Eigen::MatrixBase<XDerived>& x,
const Eigen::MatrixBase<YDerived>& y) {
return x.cross(y);
}
@mtao
mtao / Pimpl.cpp
Last active May 5, 2020 19:10
A simple example of the Pimpl pattern using unique_ptr to store the object. This example is intended to show that although I want to hide the implementation, `unique_ptr` wants to call `A_impl::~A_impl` which isn't available from the header and that this issue can be ameliorated by defining `A::~A() = default;` in the implementation
#include "A.h"
#include <iostream>
class A_impl {
public:
void f() const {
std::cout << "F!" << std::endl;
}
};
A::A(): _obj{std::make_unique<A_impl>()} {}