Skip to content

shBLOCK/spatium

Repository files navigation

🚀Spatium: a fast python spatial math library

PyPI Version Python Version
Language Python Implementation Code style: black
Codegen Tests Release Benchmark Charts

⚙️Main features

  • Fast Pure Cython Implementation
    • ~16x faster than pure python(3.12) implementation
  • Spatial Math
    • Vector
      • Operators +, -, *, /, @(dot), ^(cross), |(distance) ...
      • Fast (compile-time) swizzling (e.g. Vec3(1, 2, 3).zxy)
      • Flexible constructor (e.g. Vec3(Vec2(1, 2), 3))
      • Iterating and unpacking (e.g. x, y, z = Vec3(1, 2, 3))
      • Works with other libraries (pygame, numpy, ...)
    • Transform
    • Double-precision floats
  • Pythonic & GLSL-like interface
  • Custom code generation
    • IDE-friendly stubs

Please refer to the wiki for more details

📈Benchmark

Benchmark Results

🔧Implementation details

  • Codegen!
    Custom code generation is used throughout this library.
    Every swizzle pattern and constructor are implemented as individual methods and properties (For performance reasons).
    As a result, code generation is required so that I don't have to maintain 50,000+ lines of code by hand...
    Besides, it also handles vector classes of every dimension and type (e.g. Vec2 Vec3 Vec2i Vec3i) are generated from the same template, so a lot of repetitive code is avoided.
    There's also a stub generator that generates stub files from the cython code generated by codegen.

Credits

  • This library is partially inspired by Godot's math library.

Notes

  • This library was originally named GdMath, as I originally used this to bridge Python and Godot and focus on gamedev.

As development went on, I realized that it has become a versatile spatial math library which is also one of the fastest, I also found it useful in many projects.
Thus, I've decided to rename it to Spatium (since Spatial was taken), before it (possibly) gets used by more people.