Skip to content

Instantly share code, notes, and snippets.

View marmakoide's full-sized avatar

Devert Alexandre marmakoide

View GitHub Profile
@marmakoide
marmakoide / plane-plane-intersection-3d.py
Created May 8, 2018 06:18
Compute the intersection (a line) between two planes in 3d
import numpy
def norm2(X):
return numpy.sqrt(numpy.sum(X ** 2))
def normalized(X):
return X / norm2(X)
'''
Returns the intersection, a line, between the plane A and B
@marmakoide
marmakoide / LICENSE.md
Last active February 15, 2025 13:18
Compute and display a Laguerre-Voronoi diagram (aka power diagram), only relying on a 3d convex hull routine. The Voronoi cells are guaranted to be consistently oriented.

MIT License

Copyright (c) 2021 Devert Alexandre

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

@marmakoide
marmakoide / vmf.py
Created May 11, 2018 14:01
Von Mises Fisher distribution in 3d ie. a spherical analog to an isotropic Gaussian distribution.
import numpy
'''
Pick a point uniformly from the unit circle
'''
def circle_uniform_pick(size, out = None):
if out is None:
out = numpy.empty((size, 2))
angle = 2 * numpy.pi * numpy.random.random(size)
@marmakoide
marmakoide / README.md
Last active May 2, 2023 03:27
Returns N (approximatively) evenly spread points over the unit disc (aka. Vogel's method)

Golden dot spiral on the disc

This code sample demonstrates how to generates a set of points that covers a disc almost evenly, for any number of points. The trick is to use a Fermat's spiral and the golden angle.

42 points 256 points 999 points
dot spiral, 42 points dot spiral, 256 points dot spiral, 999 points
@marmakoide
marmakoide / README.md
Last active August 23, 2018 13:27
Test if a box is intersecting a disc. Works in N-dimensions.

Box/disc intersection test

This code sample demonstrates how to check if a box and a disc (in 3d, a sphere) intersects each other. The code works for 2d and 3d, and beyond, without any modifications.

The actual test is the function box_intersect_disc, the rest of the script is just to provide a visual demonstration of the test.

a sample of the script output

@marmakoide
marmakoide / unit-simplex.py
Last active December 13, 2022 12:49
Computes the coordinates of the regular simplex inscribed in the unit sphere, in N-dimension
import numpy
def get_unit_simplex(N, dtype = float):
# Original algorithm from John Burkardt
A = numpy.full(N, (1. - numpy.sqrt(N + 1)) / N, dtype = dtype)
S = numpy.concatenate([A[:,None].T, numpy.identity(N, dtype = dtype)])
S -= numpy.mean(S, axis = 0)
S /= numpy.sqrt(2.)
return S
@marmakoide
marmakoide / circumsphere.py
Last active January 14, 2019 09:52
Computes the N-dimension circumsphere of M points, returning its center and its radius, assuming M <= N + 1
import numpy
def get_circumsphere(S):
U = S[1:] - S[0]
B = numpy.sqrt(numpy.sum(U ** 2, axis = 1))
U /= B[:,None]
C = numpy.dot(numpy.linalg.solve(numpy.inner(U, U), .5 * B), U)
return C + S[0], numpy.sqrt(numpy.sum(C ** 2))
@marmakoide
marmakoide / quickhull-2d.py
Created January 17, 2019 07:12
A nice, short compact 2d convex hull routine, implementing the Quickhull algorithm
import numpy
def quickhull_2d(S):
def process(P, a, b):
signed_dist = numpy.cross(S[P] - S[a], S[b] - S[a])
K = [i for s, i in zip(signed_dist, P) if s > 0 and i != a and i != b]
if len(K) == 0:
@marmakoide
marmakoide / bridson_algorithm.py
Created November 19, 2019 20:37
An implementation of Bridson's algorithm for blue noise
import numpy
def sample_annulus(r1, r2, k):
theta = numpy.random.rand(k)
theta *= 2 * numpy.pi
R = numpy.sqrt(numpy.random.rand(k))
R *= r2 - r1
R += r1
S = numpy.array([numpy.cos(theta), numpy.sin(theta)]).T
S *= R[:,None]
@marmakoide
marmakoide / hexacosichoron.py
Created February 12, 2020 07:27
Generates the vertices of a unit hexacosichoron
import itertools
from sympy.combinatorics import Permutation
def get_hexacosichoron_vertices():
phi = (1 + 5 ** .5) / 2
U = [.5 * phi, .5, .5 / phi]
permutation_list = [P for P in itertools.permutations(range(4)) if Permutation(P).is_even]
for i in range(16):