Skip to main content

python-vector-swizzling

The vector_swizzling library builds on top of numpy to provide flexible and intuitive vector manipulation with swizzling capabilities, designed to resemble GLSL vector handling for 2D, 3D, and 4D vectors.

Overview

SVec is a base class for representing vectors with a numpy array and a lookup table allowing swizzling. Swizzling lets you access components in different combinations or orders, similar to GLSL's swizzling. See Swizzling on Wikipedia for more information.

Classes and Structure

The main SVec class has three subclasses for specific dimensions:

  • SVec2: for 2D vectors.
  • SVec3: for 3D vectors.
  • SVec4: for 4D vectors.

These subclasses come with dimension-specific operations, such as srotate for SVec2 and scross for SVec3.

Component Lookup Table

The swizzling table maps to the following indices:

  • 'x' or 'r' = 0
  • 'y' or 'g' = 1
  • 'z' or 'b' = 2
  • 'w' or 'a' = 3

This table lets you use letters from both Cartesian (x, y, z, w) and color (r, g, b, a) spaces interchangeably.

Usage Examples

Declaring Vectors

You can declare vectors in different ways:

# Standard declaration
a = SVec2(1, 2)
b = SVec3(1, 2, 3)
c = SVec4(1, 2, 3, 4)
d = SVec4(4, 3, 2, 1)

# Using list as components
a = SVec2([1, 2])
b = SVec3([1, 2], 3)
c = SVec4(1, 2, [3, 4])
d = SVec4([4, 3, 2], 1)

# Using vectors as components
b = SVec3(a, 3)
c = SVec4(b, 4)`
d = SVec4(c.wzy, 1)

Swizzling Vectors

You can assign to and operate on swizzled vectors, as well passing them as function arguments:

# Assigning and operating on swizzled vectors
b.xy = a.xy + b.yx

# Swizzling with RGBA space
d.xyzw = d.rgba

# Swizzling an SVec2 into an SVec3 and calling scross
cross_vec = scross(a.xyx, b)

Swizzling quirks

This module allows higher dimensional swizzles and swizzle chaining:

# Swizzle an SVec4 into a 7D SVec and call a dimension agnostic function on it
normalized_7D_vector = snormalize(d.xyzwxyz)

# Swizzle chaining is permitted but only works for the first 4 components, just like GLSL
d.xyzwxyz.xyzw
            #^ You run out of swizzles here

Vectors as lists

Vectors are just cool lists, so you can use them as such

# Iterate over vector components
for i in d.xyzwxyz:
    print(i)

# Assign to a component with its index
d[3] = 1

Metadata

Release files for vector-swizzling 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vector-swizzling 0.2.0
File Size Uploaded
vector_swizzling-0.2.0.tar.gz 23.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for vector-swizzling 0.2.0
File Interpreter ABI Platform
vector_swizzling-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 28.2 kB

Release files / vector_swizzling-0.2.0.tar.gz

Download URL vector_swizzling-0.2.0.tar.gz
Size 23.6 kB
Tags Source
SHA-256 checksum
How to use checksums
89bae7d9402431526b1d9b8d838b10688185a71a0ed78919ab32f0f1a028cf5d
BLAKE2b-256 checksum
How to use checksums
b8dc411068fd61d39a6bdcaf3ce5365f0e05d2a9e23235a034b71e9d4ffd7dc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release files / vector_swizzling-0.2.0-py3-none-any.whl

Download URL vector_swizzling-0.2.0-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
188a591434456a8bb1bbf8cd49ca30bd61ecb782a7167a4877f167254224c5e7
BLAKE2b-256 checksum
How to use checksums
1d9ae21d8464d1122510940a582e5a1aa4220117a38a56118a96a0b0843dedee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page