Skip to main content

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.

Metadata

Release files for anchorscad-linear 0.1.1

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

Source distribution (sdist)

Source distribution for anchorscad-linear 0.1.1
File Size Uploaded Provenance Actions
anchorscad_linear-0.1.1.tar.gz 26.9 kB With provenance Details for anchorscad_linear-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for anchorscad-linear 0.1.1
File Interpreter ABI Platform Actions
anchorscad_linear-0.1.1-py3-none-any.whl Python 3 none any Details for anchorscad_linear-0.1.1-py3-none-any.whl

Total release size: 49.8 kB

Release files / anchorscad_linear-0.1.1.tar.gz

Download URL anchorscad_linear-0.1.1.tar.gz
Size 26.9 kB
Tags Source
SHA-256 checksum
How to use checksums
075bec6bca4ba4d888614b6769cf686cd5ab60a22c2d360f2794697bc94a858a
BLAKE2b-256 checksum
How to use checksums
751c993e219c23e36e83b5b67cbfe6086352413d2889d6bfeba249e86256f60a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 19, 2025.

Transparency log

Release files / anchorscad_linear-0.1.1-py3-none-any.whl

Download URL anchorscad_linear-0.1.1-py3-none-any.whl
Size 22.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
304c3cc8c42c1eec6794589717007d1364832f5449533acd1ea409d303d52eab
BLAKE2b-256 checksum
How to use checksums
ef716874e99714ca63b8e2c386d4dafecde7a7b7f16487fc61845bf531c6b446
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 19, 2025.

Transparency log

Security for anchorscad-linear 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for anchorscad_linear-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 19, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files Full provenance

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