Skip to content

Instantly share code, notes, and snippets.

View Eisenwave's full-sized avatar

Jan Schultke Eisenwave

View GitHub Profile
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.util.Random;
/**
* A utility library for performing calculations with colors represented as primitive ARGB integers.
*/
public final class ColorMath {
constexpr size_t indexOf(int x, int y, int stride)
{
return static_cast<size_t>(x + y * stride);
}
template <typename RGB>
void bresenhamDrawLineUnsafe(RGB *buffer, int w, int h, int x0, int y0, int x1, int y1, RGB rgb)
{
const auto dx = x1 - x0, dy = y1 - y0;
const auto dxSign = mve::math::sgn(dx), dySign = mve::math::sgn(dy);
#include <iostream>
#include <vector>
#include <cassert>
#include <set>
#include <tuple>
#include <random>
#include <memory.h>
#include <limits.h>
#include <functional>
[20:24:57] [DBUG] world.cpp@160: Allocated anonymous WorldVoxelObject with id 1ec7bd5b9ba331d1
[20:24:57] [DBUG] voxelio_vox.cpp@98: calling voxelio::vox::Reader::init()
[20:24:57] [SPAM] voxelio_vox.cpp@238: reading MAIN (0self + 29498children = 29498)
[20:24:57] [SPAM] voxelio_vox.cpp@173: skipping SIZE (12self + 0children = 12)
[20:24:57] [SPAM] voxelio_vox.cpp@173: skipping XYZI (84self + 0children = 84)
[20:24:57] [SPAM] voxelio_vox.cpp@173: skipping nTRN (28self + 0children = 28)
[20:24:57] [SPAM] voxelio_vox.cpp@173: skipping nGRP (16self + 0children = 16)
[20:24:57] [SPAM] voxelio_vox.cpp@173: skipping nTRN (43self + 0children = 43)
[20:24:57] [SPAM] voxelio_vox.cpp@173: skipping nSHP (20self + 0children = 20)
[20:24:57] [SPAM] voxelio_vox.cpp@173: skipping LAYR (26self + 0children = 26)
@Eisenwave
Eisenwave / interleave.cpp
Created May 5, 2020 18:33
Functions for interleaving two or three numbers bitwise.
#ifndef UTIL_BITS_HPP
#define UTIL_BITS_HPP
#include <cstddef>
#include <cstdint>
#include <limits>
constexpr uint64_t ileave2(uint32_t x, uint32_t y)
{
constexpr uint64_t MASKS_2[] = {
@Eisenwave
Eisenwave / svo.hpp
Created May 7, 2020 16:08
C++ implementation of a sparse voxel octree
#ifndef SVO_HPP
#define SVO_HPP
#include "common_types.hpp"
#include "vec.hpp"
#include "vec_math.hpp"
#include "util_bits.hpp"
#include "util_common.hpp"
#include <algorithm>
@Eisenwave
Eisenwave / bits.hpp
Last active June 15, 2020 13:24
C++ Bit Manipulation
#ifndef UTIL_BITS_HPP
#define UTIL_BITS_HPP
#include "assert.hpp"
#include "math.hpp"
#include "vec.hpp"
#include <algorithm>
#include <cstddef>
#include <cstdint>
@Eisenwave
Eisenwave / fast_hilbert_to_morton.cpp
Created July 25, 2020 00:03
FastMortonToHilbert explained
uint32_t fastMortonToHilbert3_32(const uint32_t morton, const unsigned iteration) noexcept
{
// one specific permutation table for one specific case of morton <-> hilbert has very interesting properties
// it consists of 3 permutations (rot 1, xor 3, (rot 1, xor 6)) as well as their inverse
// rotations are all to the right
// the first column of this permutation table encodes the xor component (how convenient)
// the rotations simply have to be calculated but are always 0, 1, or 2 as a rotation of 3 is equiv. to 0
constexpr uint32_t permTableXors = 0x5356'0300; // first digit of the 8 permutations, also encodes XOR component
constexpr uint32_t permTableRots = 0x2021'2021; // right-shifts of each permutation
* Long division, unsigned (64/32 ==> 32). This procedure performs unsigned "long division" i.e., division of a 64-bit unsigned dividend by a 32-bit unsigned divisor, producing a 32-bit quotient. In the overflow cases (divide by 0, or quotient exceeds 32 bits), it returns a remainder of 0xFFFFFFFF (an impossible value). The dividend is u1 and u0, with u1 being the most significant word. The divisor is parameter v. The value returned is the quotient. Max line length is 57, to fit in hacker.book. */
#define max(x, y)((x) > (y) ? (x) : (y))
int nlz(unsigned x) {
int n;
if (x == 0) return (32);
n = 0;
if (x <= 0x0000FFFF) {
n = n + 16;
x = x << 16;
@Eisenwave
Eisenwave / divlu.c
Last active August 19, 2020 10:01
THIS IS A MIRROR OF http://www.hackersdelight.org/HDCode/divlu.c WHIS IS NO LONGER VIEWABLE
/* Long division, unsigned (64/32 ==> 32).
This procedure performs unsigned "long division" i.e., division of a
64-bit unsigned dividend by a 32-bit unsigned divisor, producing a
32-bit quotient. In the overflow cases (divide by 0, or quotient
exceeds 32 bits), it returns a remainder of 0xFFFFFFFF (an impossible
value).
The dividend is u1 and u0, with u1 being the most significant word.
The divisor is parameter v. The value returned is the quotient.
Max line length is 57, to fit in hacker.book. */