CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyglm

OpenGL Mathematics (GLM) library for Python providing comprehensive vector and matrix manipulation capabilities for graphics programming.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

extensions.mddocs/

Extension Functions

Advanced mathematical and utility functions that extend PyGLM's core capabilities with specialized algorithms and operations for graphics programming, procedural generation, and scientific computing.

Capabilities

Color Space Functions

Functions for converting between different color spaces and gamma correction.

def convertLinearToSRGB(color):
    """
    Convert linear RGB color to sRGB color space.

    Args:
        color: Linear RGB color (vec3 or vec4)

    Returns:
        sRGB color with gamma correction applied

    Example:
        linear_color = glm.vec3(0.5, 0.25, 0.75)
        srgb_color = glm.convertLinearToSRGB(linear_color)
    """

def convertSRGBToLinear(color):
    """
    Convert sRGB color to linear RGB color space.

    Args:
        color: sRGB color (vec3 or vec4)

    Returns:
        Linear RGB color with gamma correction removed

    Example:
        srgb_color = glm.vec3(0.73, 0.54, 0.88)
        linear_color = glm.convertSRGBToLinear(srgb_color)
    """

ULP Functions

Functions for ultra-low precision floating-point operations and comparison.

def next_float(x):
    """
    Return the next representable floating-point value.

    Args:
        x: Input float value (scalar or vector)

    Returns:
        Next representable float value greater than x
    """

def prev_float(x):
    """
    Return the previous representable floating-point value.

    Args:
        x: Input float value (scalar or vector)

    Returns:
        Previous representable float value less than x
    """

def float_distance(x, y):
    """
    Calculate the distance between two floats in ULPs (Units in Last Place).

    Args:
        x: First float value (scalar or vector)
        y: Second float value (same type as x)

    Returns:
        Distance in ULPs between x and y
    """

Rounding Extensions

Advanced rounding functions for alignment and power-of-two operations.

def ceilMultiple(value, multiple):
    """
    Round up to the nearest multiple of a given value.

    Args:
        value: Value to round up (scalar or vector)
        multiple: Multiple to round to (scalar or vector)

    Returns:
        Smallest multiple >= value

    Example:
        ceilMultiple(13, 8)  # 16
        ceilMultiple(glm.vec2(7, 15), 4)  # vec2(8, 16)
    """

def floorMultiple(value, multiple):
    """
    Round down to the nearest multiple of a given value.

    Args:
        value: Value to round down (scalar or vector)
        multiple: Multiple to round to (scalar or vector)

    Returns:
        Largest multiple <= value
    """

def roundMultiple(value, multiple):
    """
    Round to the nearest multiple of a given value.

    Args:
        value: Value to round (scalar or vector)
        multiple: Multiple to round to (scalar or vector)

    Returns:
        Nearest multiple to value
    """

def ceilPowerOfTwo(value):
    """
    Round up to the nearest power of two.

    Args:
        value: Value to round up (scalar or vector)

    Returns:
        Smallest power of two >= value

    Example:
        ceilPowerOfTwo(5)  # 8
        ceilPowerOfTwo(glm.vec2(3, 7))  # vec2(4, 8)
    """

def floorPowerOfTwo(value):
    """
    Round down to the nearest power of two.

    Args:
        value: Value to round down (scalar or vector)

    Returns:
        Largest power of two <= value
    """

def roundPowerOfTwo(value):
    """
    Round to the nearest power of two.

    Args:
        value: Value to round (scalar or vector)

    Returns:
        Nearest power of two to value
    """

Norm Functions

Functions for calculating various mathematical norms and distances.

def l1Norm(vec):
    """
    Calculate L1 norm (Manhattan distance).

    Args:
        vec: Input vector

    Returns:
        Sum of absolute values of components

    Example:
        l1Norm(glm.vec3(3, -4, 5))  # 12 (3 + 4 + 5)
    """

def l2Norm(vec):
    """
    Calculate L2 norm (Euclidean distance).

    Args:
        vec: Input vector

    Returns:
        Square root of sum of squared components (same as length())
    """

def lMaxNorm(vec):
    """
    Calculate L-infinity norm (maximum norm).

    Args:
        vec: Input vector

    Returns:
        Maximum absolute value of components

    Example:
        lMaxNorm(glm.vec3(3, -7, 2))  # 7
    """

def lxNorm(vec, degree):
    """
    Calculate Lx norm for arbitrary degree.

    Args:
        vec: Input vector
        degree: Norm degree (x in Lx)

    Returns:
        x-th root of sum of components raised to x-th power
    """

Polar Coordinates

Functions for converting between Cartesian and polar coordinate systems.

def euclidean(polar):
    """
    Convert polar coordinates to Cartesian coordinates.

    Args:
        polar: Polar coordinates (vec2: radius, angle)

    Returns:
        Cartesian coordinates (vec2: x, y)

    Example:
        cartesian = glm.euclidean(glm.vec2(5.0, glm.radians(45)))
        # Returns vec2(3.535, 3.535)
    """

def polar(euclidean):
    """
    Convert Cartesian coordinates to polar coordinates.

    Args:
        euclidean: Cartesian coordinates (vec2: x, y)

    Returns:
        Polar coordinates (vec2: radius, angle)

    Example:
        polar_coords = glm.polar(glm.vec2(3, 4))
        # Returns vec2(5.0, 0.927) - radius=5, angle~53°
    """

Vector Orientation

Functions for calculating vector orientations and rotations.

def orientation(forward, up):
    """
    Calculate orientation matrix from forward and up vectors.

    Args:
        forward: Forward direction vector (vec3)
        up: Up direction vector (vec3)

    Returns:
        3x3 orientation matrix

    Example:
        forward = glm.vec3(0, 0, -1)  # Looking down negative Z
        up = glm.vec3(0, 1, 0)        # Y is up
        orient = glm.orientation(forward, up)
    """

Projection Functions

Advanced projection and unprojection functions for 3D graphics.

def project(obj, model, proj, viewport):
    """
    Project 3D coordinates to screen coordinates.

    Args:
        obj: 3D object coordinates (vec3)
        model: Model matrix (mat4)
        proj: Projection matrix (mat4)
        viewport: Viewport parameters (vec4: x, y, width, height)

    Returns:
        Screen coordinates (vec3: x, y, depth)
    """

def projectNO(obj, model, proj, viewport):
    """
    Project 3D coordinates to screen coordinates (NO depth range [-1,1]).

    Args:
        obj: 3D object coordinates (vec3)
        model: Model matrix (mat4)
        proj: Projection matrix (mat4)
        viewport: Viewport parameters (vec4)

    Returns:
        Screen coordinates with NO depth range
    """

def projectZO(obj, model, proj, viewport):
    """
    Project 3D coordinates to screen coordinates (ZO depth range [0,1]).

    Args:
        obj: 3D object coordinates (vec3)
        model: Model matrix (mat4)
        proj: Projection matrix (mat4)
        viewport: Viewport parameters (vec4)

    Returns:
        Screen coordinates with ZO depth range
    """

def unProject(win, model, proj, viewport):
    """
    Unproject screen coordinates to 3D coordinates.

    Args:
        win: Screen coordinates (vec3: x, y, depth)
        model: Model matrix (mat4)
        proj: Projection matrix (mat4)
        viewport: Viewport parameters (vec4)

    Returns:
        3D object coordinates (vec3)
    """

def unProjectNO(win, model, proj, viewport):
    """
    Unproject screen coordinates (NO depth range) to 3D coordinates.

    Args:
        win: Screen coordinates with NO depth range
        model: Model matrix (mat4)
        proj: Projection matrix (mat4)
        viewport: Viewport parameters (vec4)

    Returns:
        3D object coordinates
    """

def unProjectZO(win, model, proj, viewport):
    """
    Unproject screen coordinates (ZO depth range) to 3D coordinates.

    Args:
        win: Screen coordinates with ZO depth range
        model: Model matrix (mat4)
        proj: Projection matrix (mat4)
        viewport: Viewport parameters (vec4)

    Returns:
        3D object coordinates
    """

def pickMatrix(center, delta, viewport):
    """
    Create a picking matrix for selection operations.

    Args:
        center: Center of picking region (vec2)
        delta: Size of picking region (vec2)
        viewport: Viewport parameters (vec4)

    Returns:
        4x4 picking matrix
    """

Euler Angles

Functions for working with Euler angle representations of rotations.

def euler(angles):
    """
    Create rotation matrix from Euler angles.

    Args:
        angles: Euler angles in radians (vec3: pitch, yaw, roll)

    Returns:
        3x3 rotation matrix

    Example:
        angles = glm.vec3(glm.radians(30), glm.radians(45), glm.radians(60))
        rotation_matrix = glm.euler(angles)
    """

def eulerAngles(quat):
    """
    Extract Euler angles from quaternion.

    Args:
        quat: Input quaternion

    Returns:
        Euler angles in radians (vec3: pitch, yaw, roll)

    Example:
        q = glm.angleAxis(glm.radians(45), glm.vec3(0, 1, 0))
        angles = glm.eulerAngles(q)
    """

def yaw(quat):
    """
    Extract yaw angle from quaternion.

    Args:
        quat: Input quaternion

    Returns:
        Yaw angle in radians
    """

def pitch(quat):
    """
    Extract pitch angle from quaternion.

    Args:
        quat: Input quaternion

    Returns:
        Pitch angle in radians
    """

def roll(quat):
    """
    Extract roll angle from quaternion.

    Args:
        quat: Input quaternion

    Returns:
        Roll angle in radians
    """

Usage Examples

from pyglm import glm

# === Color Space Conversion ===

# Convert from linear RGB to sRGB for display
linear_color = glm.vec3(0.5, 0.25, 0.75)
display_color = glm.convertLinearToSRGB(linear_color)

# Convert sRGB texture data to linear for calculations
texture_color = glm.vec3(0.73, 0.54, 0.88)
linear_texture = glm.convertSRGBToLinear(texture_color)

# === Rounding for Memory Alignment ===

# Align buffer size to 16-byte boundaries
buffer_size = 137
aligned_size = glm.ceilMultiple(buffer_size, 16)  # 144

# Round texture dimensions to power of two
width, height = 300, 200
pow2_width = glm.ceilPowerOfTwo(width)   # 512
pow2_height = glm.ceilPowerOfTwo(height) # 256

# === Norm Calculations ===

vector = glm.vec3(3, -4, 5)
manhattan_dist = glm.l1Norm(vector)  # 12 (|3| + |-4| + |5|)
euclidean_dist = glm.l2Norm(vector)  # ~7.07 (same as glm.length())
max_component = glm.lMaxNorm(vector) # 5 (max of |3|, |-4|, |5|)

# === Coordinate System Conversion ===

# Convert 2D Cartesian to polar
cartesian_point = glm.vec2(3, 4)
polar_coords = glm.polar(cartesian_point)  # vec2(5.0, 0.927)
radius = polar_coords.x    # 5.0
angle = polar_coords.y     # ~0.927 radians (~53 degrees)

# Convert back to Cartesian
back_to_cartesian = glm.euclidean(polar_coords)  # vec2(3, 4)

# === 3D Projection ===

# Project 3D world coordinates to screen coordinates
world_pos = glm.vec3(1, 2, -5)
model_matrix = glm.mat4()  # Identity
view_matrix = glm.lookAt(glm.vec3(0, 0, 0), glm.vec3(0, 0, -1), glm.vec3(0, 1, 0))
proj_matrix = glm.perspective(glm.radians(45), 1.33, 0.1, 100.0)
viewport = glm.vec4(0, 0, 800, 600)  # x, y, width, height

# Combine model and view
modelview = view_matrix * model_matrix
screen_pos = glm.project(world_pos, modelview, proj_matrix, viewport)

# Unproject screen coordinates back to world space
world_pos_back = glm.unProject(screen_pos, modelview, proj_matrix, viewport)

# === Euler Angle Rotation ===

# Create rotation from Euler angles
pitch = glm.radians(30)  # Around X-axis
yaw = glm.radians(45)    # Around Y-axis
roll = glm.radians(60)   # Around Z-axis

euler_angles = glm.vec3(pitch, yaw, roll)
rotation_matrix = glm.euler(euler_angles)

# Convert to quaternion for smooth interpolation
rotation_quat = glm.quat_cast(rotation_matrix)

# Extract individual angles from quaternion
extracted_yaw = glm.yaw(rotation_quat)
extracted_pitch = glm.pitch(rotation_quat)
extracted_roll = glm.roll(rotation_quat)

# Get all angles at once
all_angles = glm.eulerAngles(rotation_quat)

# === Picking for Mouse Selection ===

# Create picking matrix for mouse selection
mouse_pos = glm.vec2(400, 300)  # Mouse position
pick_size = glm.vec2(5, 5)      # 5x5 pixel selection area
viewport = glm.vec4(0, 0, 800, 600)

picking_matrix = glm.pickMatrix(mouse_pos, pick_size, viewport)
# Multiply with projection matrix for actual picking projection
pick_proj = proj_matrix * picking_matrix

Install with Tessl CLI

npx tessl i tessl/pypi-pyglm

docs

extensions.md

index.md

math-functions.md

matrices.md

quaternions.md

random-noise.md

transformations.md

utilities.md

vectors.md

tile.json