Skip to main content

A versatile vector operations module with swizzling capabilities for 2D, 3D, and 4D vectors.

Project description

python-vector-swizzling

The vector_swizzling library provides 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 list of components 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vector_swizzling-0.1.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vector_swizzling-0.1.1-py3-none-any.whl (2.3 kB view details)

Uploaded Python 3

File details

Details for the file vector_swizzling-0.1.1.tar.gz.

File metadata

  • Download URL: vector_swizzling-0.1.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.7

File hashes

Hashes for vector_swizzling-0.1.1.tar.gz
Algorithm Hash digest
SHA256 bcd816858fd6a282977fd7d3b7c6792e8d9866bb2be612c92fc86b452793fae7
MD5 e2c417c728795af54a7e15b0fb7141a2
BLAKE2b-256 b199672d212668faed6d72d117f84353b56d54e81d4e232b6d35c84d27b828be

See more details on using hashes here.

File details

Details for the file vector_swizzling-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for vector_swizzling-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 025d18f6f1337e4b8b473b72455aadb2dad485bc9c787a21dbd7fd1a67ed87a9
MD5 abf23f41e69b0a281664ef7854db95f0
BLAKE2b-256 fb7a5dd9b13f886355544c580d0bac6a21026ed9e931cf7222454558fd2afc26

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page