Skip to content

Instantly share code, notes, and snippets.

@Unril
Unril / EigenOpenMeshPlugin.h
Last active June 14, 2018 06:54
Using Eigen::Vector3f in OpenMesh mesh
EIGEN_STRONG_INLINE Matrix &normalize() {
*this /= norm();
return *this;
}
EIGEN_STRONG_INLINE Scalar length() const { return norm(); }
EIGEN_STRONG_INLINE Matrix &vectorize(Scalar v) {
fill(v);
return *this;
@Unril
Unril / DllWrapper.hpp
Last active December 27, 2015 22:39
Simple helper class to import addresses of the exported function or variable from dll via WinApi LoadLibrary and GetProcAddress.
#pragma once
#include <stdexcept>
#include <cassert>
#include <Windows.h>
template<typename Signature, char *(*GetFunctionName)(), HMODULE &(*GetDllHandle)()>
struct DllFunction {
DllFunction() : _functionPtr(reinterpret_cast<Signature*>(GetProcAddress(GetDllHandle(), GetFunctionName()))) {
if (_functionPtr == NULL) throw std::runtime_error("Dll function cannot be loaded.");
public static void CircularShiftLeft(ref uint value, int shift, int sizeInBits = BitsInInt32) {
Contract.Requires(sizeInBits > 0 && sizeInBits <= BitsInInt32);
Contract.Requires(shift <= sizeInBits && shift >= 0);
uint mask = UInt32.MaxValue >> (BitsInInt32 - sizeInBits);
uint masked = value & mask;
uint shifted = (masked << shift) | (masked >> (sizeInBits - shift));
value = (value & ~mask) | (shifted & mask);
}
public static void CircularShiftRight(ref uint value, int shift, int sizeInBits = BitsInInt32) {
@Unril
Unril / gist:5823360
Created June 20, 2013 14:46
StructureToByteArray and ByteArrayToStructure
public static byte[] StructureToByteArray<T>(T obj) {
int len = Marshal.SizeOf(typeof (T));
var arr = new byte[len];
IntPtr ptr = IntPtr.Zero;
try {
ptr = Marshal.AllocHGlobal(len);
Marshal.StructureToPtr(obj, ptr, true);
Marshal.Copy(ptr, arr, 0, len);
}
finally {
@Unril
Unril / binder.cs
Created May 15, 2013 12:37
RadioButtonBinder
public static class RadioButtonBinder {
public static readonly DependencyProperty ValueProperty =
DependencyProperty.RegisterAttached("Value", typeof (object), typeof (RadioButtonBinder),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.NotDataBindable, ValueChanged));
public static readonly DependencyProperty BindProperty =
DependencyProperty.RegisterAttached("Bind", typeof (object), typeof (RadioButtonBinder),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, BindChanged));
private static bool _react = true;