Skip to content

Instantly share code, notes, and snippets.

View pghazanfari's full-sized avatar

Parham Ghazanfari pghazanfari

View GitHub Profile
@pghazanfari
pghazanfari / camera.py
Created August 29, 2019 22:46
numpy Perspective Projection + Look At Matrices
import numpy as np
def perspective_fov(fov, aspect_ratio, near_plane, far_plane):
num = 1.0 / np.tan(fov / 2.0)
num9 = num / aspect_ratio
return np.array([
[num9, 0.0, 0.0, 0.0],
[0.0, num, 0.0, 0.0],
[0.0, 0.0, far_plane / (near_plane - far_plane), -1.0],
[0.0, 0.0, (near_plane * far_plane) / (near_plane - far_plane), 0.0]
@pghazanfari
pghazanfari / surrounding.py
Last active December 11, 2023 14:52
Get Surrounding Elements in a Numpy Array
def surrounding(x, idx, radius=1, fill=0):
"""
Gets surrounding elements from a numpy array
Parameters:
x (ndarray of rank N): Input array
idx (N-Dimensional Index): The index at which to get surrounding elements. If None is specified for a particular axis,
the entire axis is returned.
radius (array-like of rank N or scalar): The radius across each axis. If None is specified for a particular axis,
the entire axis is returned.