Skip to main content

Linear algebra API for AnchorSCAD. Includes GMatrix, GVector, translate, rotate, angle and more.

Project description

anchorscad-linear

A linear algebra library designed specifically for 3D geometric transformations in AnchorSCAD. This library provides specialized matrix and vector operations optimized for working with OpenSCAD's multmatrix transform function.

Key Features

  • Specialized 4x4 matrix operations for 3D transformations (GMatrix)
  • 3D/4D vector operations (GVector)
  • Rotation, translation, and scaling transformations
  • Angle handling in degrees, radians, or sin/cos pairs
  • Plane and line intersection calculations

Core Classes

GVector

A 3D vector class that internally uses homogeneous coordinates (4D with last element always 1).

from anchorscad_lib.linear import GVector, X_AXIS, Y_AXIS, Z_AXIS

# Create vectors
v1 = GVector([1, 2, 3]) # Creates [1, 2, 3, 1]
v2 = GVector([1, 2, 3, 1]) # Same as above

# Vector operations
v3 = v1 + v2 # Addition
v4 = v1 - v2 # Subtraction
v5 = v1 *2 # Scalar multiplication
v6 = v1 / 2 # Scalar division

# Vector methods
length = v1.length() # Vector length
normalized = v1.N # Normalized vector
dot = v1.dot3D(v2) # Dot product
cross = v1.cross3D(v2) # Cross product

GMatrix

A 4x4 matrix class specialized for 3D transformations. The last row is always maintained as [0, 0, 0, 1].

from anchorscad_lib.linear import GMatrix, IDENTITY
# Create matrices
m1 = GMatrix([
    [1, 0, 0, 0], # 4x4 matrix
    [0, 1, 0, 0],
    [0, 0, 1, 0],
    [0, 0, 0, 1]])
# Matrix operations
m2 = m1 * m1 # Matrix multiplication
m3 = ~m1 # Matrix inverse (same as m1.I)
v2 = m1 v1 # Transform vector

Angle

The Angle class and angle function are used to represent and manipulate angles in different units. This can be used to avoid transforming angles say from (sin, cos) to degrees and then back to (sin, cos) to use in a transformation matrix.

from anchorscad_lib.linear import angle

# Create angles
a1 = angle(45) # 45 degrees
a2 = angle(radians=3.14159) # π radians
a3 = angle(sinr_cosr=(0, 1)) # Using sin/cos pair

# Angle operations
a4 = a1 + a2 # Addition
a5 = a1 - a2 # Subtraction
a6 = -a1 # Negation

# Rotations
a7 = a1.rotX() # Rotate around X axis (also rotY and rotZ)
a8 = a1.rotV(v1) # Rotate around vector v1

# Sweep
a9 = a1.sweepRadians(positive_dir=True, non_zero=True) # Sweep angle in the positive direction
a10 = a1.sweepRadians(positive_dir=False, non_zero=True) # Sweep angle in the negative direction

Transformation Functions

Basic Transformations

from anchorscad_lib.linear import translate, scale, rotX, rotY, rotZ
# Translation
t1 = translate([1, 2, 3]) # Translate by vector
t2 = tranX(5) # Translate along X axis
t3 = tranY(5) # Translate along Y axis
t4 = tranZ(5) # Translate along Z axis

# Scaling
s1 = scale([2, 2, 2]) # Scale by vector
s2 = scale(2) # Uniform scale

# Rotation
r1 = rotX(90) # Rotate 90° around X axis
r2 = rotY(45) # Rotate 45° around Y axis
r3 = rotZ(30) # Rotate 30° around Z axis
r4 = rotV(v1) # Rotate around vector v1
r5 = rotVSinCos(v1, sinr, cosr) # Rotate around vector v1 with sin/cos pair

Advanced Transformations

from anchorscad_lib.linear import rotV, rot_to_V, rotAlign, mirror
# Rotate from one vector to another
r5 = rot_to_V(v1, v2) # Creates a rotation matrix that rotates v1 to align with v2

# Align vectors while preserving an axis
r6 = rotAlign(preserve_axis, align_axis, plane_axis)

# Mirror transformations
m1 = mirror(axis) # Mirror matrix for the given axis

Plane and Line Intersection

from anchorscad_lib.linear import *
# Find intersection line of two planes
# Planes are represented as GMatrix objects where the Z axis is the normal to the plane.
line = plane_intersect(plane1, plane2)

# Find intersection point of plane and line
# Line is a GMatrix object where the Z axis is the line direction.
point = plane_line_intersect(plane, line)

# Calculate distances
d1 = distance_between(point1, point2)
d2 = distance_between_point_plane(point, plane)

Constants

  • IDENTITY: Identity matrix
  • X_AXIS, Y_AXIS, Z_AXIS: Standard basis vectors
  • ZERO_VEC: Zero vector
  • MIRROR_X, MIRROR_Y, MIRROR_Z: Mirror matrices
  • ROTX_90, ROTY_90, ROTZ_90: 90° rotation matrices
  • ROTX_180, ROTY_180, ROTZ_180: 180° rotation matrices
  • ROTX_270, ROTY_270, ROTZ_270: 270° rotation matrices

Installation

pip install anchorscad-linear

Usage Example

from anchorscad_lib.linear import *

# Create transformation matrix
transform = (translate([1, 2, 3])   # Translate last
             * rotZ(45)             # Then rotate 45° around Z
             * scale(2))            # Scale first

# Transform a vector
v1 = GVector([1, 0, 0])
v2 = transform * v1

# Chain transformations
result = transform * transform.I * v1 # Should equal v1
assert result.is_approx_equal(v1)

License

This project is licensed under the LGPLv2.1 license. See the LICENSE file for details.

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

anchorscad_linear-0.1.0.tar.gz (26.9 kB view details)

Uploaded Source

Built Distribution

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

anchorscad_linear-0.1.0-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

Details for the file anchorscad_linear-0.1.0.tar.gz.

File metadata

  • Download URL: anchorscad_linear-0.1.0.tar.gz
  • Upload date:
  • Size: 26.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for anchorscad_linear-0.1.0.tar.gz
Algorithm Hash digest
SHA256 819ca283e671134e61d24b6295d0ca53463cddd86a96fa56eb92f059cb60b33d
MD5 8dbcf2d0e992117e69e9acb25cb53e07
BLAKE2b-256 1f7f232978295cab2d71b6c9e8b9a4867fe9b4ee9409d4803ff8043bc68d63c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for anchorscad_linear-0.1.0.tar.gz:

Publisher: publish.yml on owebeeone/anchorscad-linear

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file anchorscad_linear-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for anchorscad_linear-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 24dace289300a3f0b97874baa97aecd810ada57a82f1bdd817d41e53d023a8c9
MD5 7fe7ac225eb574dc63b5b58d4c204ae9
BLAKE2b-256 4291fef4b777d9d5961c022c5015c6d184dbe913eb2d358da1c6a2927ccd7323

See more details on using hashes here.

Provenance

The following attestation bundles were made for anchorscad_linear-0.1.0-py3-none-any.whl:

Publisher: publish.yml on owebeeone/anchorscad-linear

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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