OpenGL Mathematics (GLM) library for Python providing comprehensive vector and matrix manipulation capabilities for graphics programming.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Comprehensive vector mathematics providing all the fundamental operations needed for graphics programming, physics simulations, and scientific computing. PyGLM supports vectors from 1 to 4 components in multiple precisions and data types.
PyGLM provides a complete set of vector types covering all common use cases in graphics and scientific computing.
# Boolean vectors
class bvec1:
x: bool
def __init__(self, x: bool = False): ...
class bvec2:
x: bool
y: bool
def __init__(self, x: bool = False, y: bool = False): ...
class bvec3:
x: bool
y: bool
z: bool
def __init__(self, x: bool = False, y: bool = False, z: bool = False): ...
class bvec4:
x: bool
y: bool
z: bool
w: bool
def __init__(self, x: bool = False, y: bool = False, z: bool = False, w: bool = False): ...
# Float32 vectors (default precision)
class vec1:
x: float
def __init__(self, x: float = 0.0): ...
class vec2:
x: float
y: float
def __init__(self, x: float = 0.0, y: float = 0.0): ...
class vec3:
x: float
y: float
z: float
def __init__(self, x: float = 0.0, y: float = 0.0, z: float = 0.0): ...
class vec4:
x: float
y: float
z: float
w: float
def __init__(self, x: float = 0.0, y: float = 0.0, z: float = 0.0, w: float = 0.0): ...
# Double precision vectors
class dvec1:
x: float
def __init__(self, x: float = 0.0): ...
class dvec2:
x: float
y: float
def __init__(self, x: float = 0.0, y: float = 0.0): ...
class dvec3:
x: float
y: float
z: float
def __init__(self, x: float = 0.0, y: float = 0.0, z: float = 0.0): ...
class dvec4:
x: float
y: float
z: float
w: float
def __init__(self, x: float = 0.0, y: float = 0.0, z: float = 0.0, w: float = 0.0): ...
# Signed integer vectors (32-bit)
class ivec1:
x: int
def __init__(self, x: int = 0): ...
class ivec2:
x: int
y: int
def __init__(self, x: int = 0, y: int = 0): ...
class ivec3:
x: int
y: int
z: int
def __init__(self, x: int = 0, y: int = 0, z: int = 0): ...
class ivec4:
x: int
y: int
z: int
w: int
def __init__(self, x: int = 0, y: int = 0, z: int = 0, w: int = 0): ...
# Unsigned integer vectors (32-bit)
class uvec1:
x: int
def __init__(self, x: int = 0): ...
class uvec2:
x: int
y: int
def __init__(self, x: int = 0, y: int = 0): ...
class uvec3:
x: int
y: int
z: int
def __init__(self, x: int = 0, y: int = 0, z: int = 0): ...
class uvec4:
x: int
y: int
z: int
w: int
def __init__(self, x: int = 0, y: int = 0, z: int = 0, w: int = 0): ...class i8vec1: ... # 8-bit signed integer vectors class i8vec2: ... class i8vec3: ... class i8vec4: ...
class i16vec1: ... # 16-bit signed integer vectors class i16vec2: ... class i16vec3: ... class i16vec4: ...
class i64vec1: ... # 64-bit signed integer vectors class i64vec2: ... class i64vec3: ... class i64vec4: ...
class u8vec1: ... # 8-bit unsigned integer vectors class u8vec2: ... class u8vec3: ... class u8vec4: ...
class u16vec1: ... # 16-bit unsigned integer vectors class u16vec2: ... class u16vec3: ... class u16vec4: ...
class u64vec1: ... # 64-bit unsigned integer vectors class u64vec2: ... class u64vec3: ... class u64vec4: ...
class mvec2: ... # 32-bit float mixed vectors class mvec3: ... class mvec4: ...
class dmvec2: ... # Double precision mixed vectors class dmvec3: ... class dmvec4: ...
class imvec2: ... # 32-bit integer mixed vectors class imvec3: ... class imvec4: ...
class umvec2: ... # 32-bit unsigned mixed vectors class umvec3: ... class umvec4: ...
**Type Aliases**: PyGLM provides convenient aliases for common vector types:
- `f32vec*` = `fvec*` = `vec*` - 32-bit float vectors
- `f64vec*` = `dvec*` - 64-bit double vectors
- `i32vec*` = `ivec*` - 32-bit signed integer vectors
- `u32vec*` = `uvec*` - 32-bit unsigned integer vectors
### Geometric Operations
Essential geometric operations for graphics programming including dot products, cross products, normalization, and distance calculations.
```python { .api }
def dot(x, y):
"""
Calculate the dot product of two vectors.
Args:
x: Vector of any type and dimension
y: Vector of same type and dimension as x
Returns:
Scalar value representing the dot product
"""
def cross(x, y):
"""
Calculate the cross product of two 3D vectors.
Args:
x: 3-component vector
y: 3-component vector of same type
Returns:
3-component vector perpendicular to both input vectors
"""
def normalize(x):
"""
Normalize a vector to unit length.
Args:
x: Vector of any type and dimension
Returns:
Vector of same type with length = 1.0
"""
def length(x):
"""
Calculate the Euclidean length of a vector.
Args:
x: Vector of any type and dimension
Returns:
Scalar representing the vector's magnitude
"""
def distance(p0, p1):
"""
Calculate the distance between two points.
Args:
p0: First point as vector
p1: Second point as vector of same type
Returns:
Scalar distance between the points
"""
def faceforward(N, I, Nref):
"""
Orient a vector to point away from a surface.
Args:
N: Vector to orient
I: Incident vector
Nref: Reference vector
Returns:
N if dot(Nref, I) < 0, else -N
"""
def reflect(I, N):
"""
Calculate the reflection direction for an incident vector.
Args:
I: Incident vector
N: Normal vector (should be normalized)
Returns:
Reflection vector
"""
def refract(I, N, eta):
"""
Calculate the refraction direction for an incident vector.
Args:
I: Incident vector (should be normalized)
N: Normal vector (should be normalized)
eta: Ratio of indices of refraction
Returns:
Refraction vector, or zero vector for total internal reflection
"""Component-wise comparison operations that return boolean vectors for element-wise testing.
def lessThan(x, y):
"""
Component-wise less than comparison.
Args:
x: First vector
y: Second vector of same type
Returns:
Boolean vector where each component is True if x[i] < y[i]
"""
def lessThanEqual(x, y):
"""
Component-wise less than or equal comparison.
Args:
x: First vector
y: Second vector of same type
Returns:
Boolean vector where each component is True if x[i] <= y[i]
"""
def greaterThan(x, y):
"""
Component-wise greater than comparison.
Args:
x: First vector
y: Second vector of same type
Returns:
Boolean vector where each component is True if x[i] > y[i]
"""
def greaterThanEqual(x, y):
"""
Component-wise greater than or equal comparison.
Args:
x: First vector
y: Second vector of same type
Returns:
Boolean vector where each component is True if x[i] >= y[i]
"""
def equal(x, y):
"""
Component-wise equality comparison.
Args:
x: First vector
y: Second vector of same type
Returns:
Boolean vector where each component is True if x[i] == y[i]
"""
def notEqual(x, y):
"""
Component-wise inequality comparison.
Args:
x: First vector
y: Second vector of same type
Returns:
Boolean vector where each component is True if x[i] != y[i]
"""Logical operations for boolean vectors including any, all, and component-wise NOT operations.
def any(x):
"""
Test if any component of a boolean vector is True.
Args:
x: Boolean vector of any dimension
Returns:
True if any component is True, False otherwise
"""
def all(x):
"""
Test if all components of a boolean vector are True.
Args:
x: Boolean vector of any dimension
Returns:
True if all components are True, False otherwise
"""
def not_(x):
"""
Component-wise logical NOT operation.
Args:
x: Boolean vector of any dimension
Returns:
Boolean vector with each component logically inverted
"""Linear and smooth interpolation functions for blending between vectors and creating smooth transitions.
def mix(x, y, a):
"""
Linear interpolation between two vectors.
Args:
x: First vector
y: Second vector of same type
a: Interpolation factor (0.0 returns x, 1.0 returns y)
Returns:
Interpolated vector: x * (1-a) + y * a
"""
def smoothstep(edge0, edge1, x):
"""
Smooth Hermite interpolation with smooth transitions.
Args:
edge0: Lower edge of interpolation range
edge1: Upper edge of interpolation range
x: Vector to interpolate
Returns:
Vector with smooth interpolation applied component-wise
"""
def step(edge, x):
"""
Step function returning 0.0 or 1.0 based on comparison.
Args:
edge: Threshold value
x: Vector to compare
Returns:
Vector where each component is 0.0 if x[i] < edge, 1.0 otherwise
"""All vector types support standard arithmetic operations through operator overloading:
v1 + v2 - Component-wise additionv1 - v2 - Component-wise subtractionv * scalar or v1 * v2 - Scalar or component-wise multiplicationv / scalar or v1 / v2 - Scalar or component-wise division-v - Component-wise negationv[i] - Access individual componentslen(v) - Number of componentsfrom pyglm import glm
# Vector creation and basic operations
v1 = glm.vec3(1.0, 2.0, 3.0)
v2 = glm.vec3(4.0, 5.0, 6.0)
v3 = v1 + v2 # vec3(5.0, 7.0, 9.0)
# Geometric operations
cross_prod = glm.cross(v1, v2) # Cross product
dot_prod = glm.dot(v1, v2) # Dot product (32.0)
unit_v1 = glm.normalize(v1) # Unit vector in direction of v1
dist = glm.distance(v1, v2) # Distance between points
# Vector comparisons
comparison = glm.lessThan(v1, v2) # bvec3(True, True, True)
any_less = glm.any(comparison) # True
all_less = glm.all(comparison) # True
# Interpolation
midpoint = glm.mix(v1, v2, 0.5) # Halfway between v1 and v2
smooth = glm.smoothstep(glm.vec3(0), glm.vec3(10), v1)
# Integer vectors for array indices or discrete calculations
indices = glm.ivec3(0, 1, 2)
colors = glm.uvec4(255, 128, 64, 255) # RGBA color
# Boolean vectors for masking
mask = glm.bvec3(True, False, True)Install with Tessl CLI
npx tessl i tessl/pypi-pyglm