Skip to main content

A 2D/3D geometry library for CAD/CAM applications.

Project description

raygeo

PyPI CI

A high-performance 2D/3D geometry library for Python, built in Rust with PyO3.

raygeo provides vector path construction, polygon boolean operations, curve fitting, path transformations, and geometric queries — all backed by a native Rust extension.

Installation

pip install raygeo

Requires Python 3.10+ and a compatible platform (Linux, Windows, macOS Intel, or macOS Apple Silicon). Pre-compiled wheels are available on PyPI.

Quick Start

Building Paths

The Geometry class is the core abstraction. It stores a vector path as a sequence of move, line, arc, and cubic Bezier commands:

from raygeo import Geometry

# Create a 10x10 square
g = Geometry()
g.move_to(0, 0)
g.line_to(10, 0)
g.line_to(10, 10)
g.line_to(0, 10)
g.close_path()

print(g.area())    # 100.0
print(g.rect())    # (0.0, 0.0, 10.0, 10.0)
print(g.is_closed())  # True

You can also create paths from point lists:

triangle = Geometry.from_points([(0, 0), (10, 0), (5, 8.66)])

Accessing Raw Data

Internally, geometry data is stored as an (N, 8) NumPy float64 array. Each row represents one command:

import numpy as np
from raygeo import COL_TYPE, COL_X, COL_Y, CMD_TYPE_LINE

data = g.data  # numpy array, shape (N, 8)
print(data[:, COL_X])  # all x coordinates

The 8 columns are:

Column Index Description
COL_TYPE 0 Command type (move/line/arc/bezier)
COL_X 1 X coordinate
COL_Y 2 Y coordinate
COL_Z 3 Z coordinate
COL_I / COL_C1X 4 Arc center offset X / Bezier control 1 X
COL_J / COL_C1Y 5 Arc center offset Y / Bezier control 1 Y
COL_CW / COL_C2X 6 Arc clockwise flag / Bezier control 2 X
— / COL_C2Y 7 (unused for arcs) / Bezier control 2 Y

Arcs and Bezier Curves

g = Geometry()
g.move_to(0, 0)
g.arc_to(10, 0, i=5, j=0, clockwise=False)  # semicircular arc
g.close_path()

# Bezier curves
g2 = Geometry()
g2.move_to(0, 0)
g2.bezier_to(10, 0, c1x=3, c1y=5, c2x=7, c2y=5)

# Convert arcs to Bezier curves (for non-uniform scaling)
g3 = Geometry()
g3.move_to(0, 0)
g3.arc_to_as_bezier(10, 0, i=5, j=0)
g3.upgrade_to_scalable()

Path Analysis

print(g.distance())       # total path length
print(g.area())           # signed enclosed area
print(g.rect())           # bounding box (x_min, y_min, x_max, y_max)
print(g.is_closed())      # path closure check
print(g.segments())       # split into sub-paths

# Find closest point on path
result = g.find_closest_point(5, 5)  # (segment_index, distance, (px, py))

# Point and tangent at parameter t on a segment
pos, tangent = g.get_point_and_tangent_at(segment_index=0, t=0.5)

Transformations

import numpy as np
from raygeo import Geometry

g = Geometry.from_points([(0, 0), (10, 0), (10, 10), (0, 10)])

# Offset (grow/shrink)
grown = g.grow(2.0)   # offset outward by 2 units
shrunk = g.grow(-1.0)  # offset inward by 1 unit

# Affine transform (4x4 matrix)
matrix = [
    [1, 0, 0, 5],  # translate x by 5
    [0, 1, 0, 3],  # translate y by 3
    [0, 0, 1, 0],
    [0, 0, 0, 1],
]
g.transform(matrix)

# Map geometry into a frame
mapped = g.map_to_frame(
    origin=(0, 0),
    p_width=(100, 0),
    p_height=(0, 100),
)

g.flip_x()  # negate all x coordinates
g.flip_y()  # negate all y coordinates

Contour Operations

# Split into separate closed contours
contours = g.split_into_contours()

# Split into disconnected components
components = g.split_into_components()

# Separate holes from solids
inner, outer = g.split_inner_and_outer_contours()

# Remove shared edges between sub-paths
outer_only = g.remove_inner_edges()

Polygon Operations

The geo.shape.polygon submodule provides polygon-specific operations powered by Clipper2:

from raygeo import Geometry
from raygeo.geo.shape.polygon import (
    get_polygon_area,
    get_polygon_bounds,
    offset_polygon,
    get_polygons_union,
    get_polygons_intersection,
    get_polygons_difference,
    is_point_inside_polygon,
    polygons_intersect,
    get_polygon_convex_hull,
)

square = [(0, 0), (10, 0), (10, 10), (0, 10)]
circle_approx = [(5 + 5 * math.cos(a), 5 + 5 * math.sin(a))
                 for a in [i * math.pi / 20 for i in range(40)]]

get_polygon_area(square)                # 100.0
get_polygon_bounds(square)              # (0.0, 0.0, 10.0, 10.0)
is_point_inside_polygon((5, 5), square) # True

# Boolean operations
union = get_polygons_union([square, circle_approx])
intersection = get_polygons_intersection([square, circle_approx])
difference = get_polygons_difference([square], [circle_approx])

# Offset
inflated = offset_polygon(square, 2.0)

# NumPy variants are also available (suffixed with _numpy)
import numpy as np
sq_np = np.array(square)
get_polygon_area(sq_np)  # also works with numpy arrays

Curve Fitting

from raygeo import Geometry

# Simplify a path
g.simplify(tolerance=0.1)

# Convert curves to line segments
g.linearize(tolerance=0.01)

# Fit arcs and beziers to linear data
g.fit_arcs(tolerance=0.5)
g.fit_curves(tolerance=0.5, beziers=True, arcs=True)

# Convert geometry to polygons
polygons = g.to_polygons(tolerance=0.01)

Self-Intersection Detection

g.has_self_intersections()          # check for self-intersections
g.intersects_with(other_geometry)   # check intersection with another geometry
g.encloses(other_geometry)          # check if this fully encloses another

Serialization

# Dump to dict (JSON-safe)
data = g.dump()

# Load from dict
g2 = Geometry.load(data)

# Pickle support (via __reduce_ex__)
import pickle
g3 = pickle.loads(pickle.dumps(g))

Documentation

Full API documentation is generated from the source type stubs and published with the RayForge Developer Docs.

Development

Prerequisites

  • Rust toolchain (via rustup)
  • Python 3.10+ with maturin and pytest:
pip install maturin pytest

Build and Test

maturin develop
pytest tests/ -v

Running Rust Tests

cargo test

License

MIT

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

raygeo-0.5.0.tar.gz (271.3 kB view details)

Uploaded Source

Built Distributions

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

raygeo-0.5.0-cp311-abi3-win_amd64.whl (1.0 MB view details)

Uploaded CPython 3.11+Windows x86-64

raygeo-0.5.0-cp311-abi3-manylinux_2_35_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.35+ x86-64

raygeo-0.5.0-cp311-abi3-macosx_11_0_arm64.whl (1.1 MB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

raygeo-0.5.0-cp311-abi3-macosx_10_12_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.11+macOS 10.12+ x86-64

File details

Details for the file raygeo-0.5.0.tar.gz.

File metadata

  • Download URL: raygeo-0.5.0.tar.gz
  • Upload date:
  • Size: 271.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for raygeo-0.5.0.tar.gz
Algorithm Hash digest
SHA256 3203b6f5eaf6021c9314ed73a48f9f4ce120bdfb204a26e42be993bf0b3e4739
MD5 b1dad540caa64366b989b872efe61a7f
BLAKE2b-256 a2fe65ff7cc84b4ac901d6c7650d40acfddaa913fd080a45674c91d2ce93a6b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for raygeo-0.5.0.tar.gz:

Publisher: release.yml on barebaric/raygeo

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

File details

Details for the file raygeo-0.5.0-cp311-abi3-win_amd64.whl.

File metadata

  • Download URL: raygeo-0.5.0-cp311-abi3-win_amd64.whl
  • Upload date:
  • Size: 1.0 MB
  • Tags: CPython 3.11+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for raygeo-0.5.0-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 97e6f44d66c15306b0392e8ff04c3d71135e5ade8f850c0611fa7e75def6e8d5
MD5 49a9f272291b259fcc70170f4c81ec5d
BLAKE2b-256 4170fc3f13ef34002042ae83adaa6455c3fd68206bfe3d7084f81ba0c9661025

See more details on using hashes here.

Provenance

The following attestation bundles were made for raygeo-0.5.0-cp311-abi3-win_amd64.whl:

Publisher: release.yml on barebaric/raygeo

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

File details

Details for the file raygeo-0.5.0-cp311-abi3-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for raygeo-0.5.0-cp311-abi3-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 1d4612513bcbda8a203614e87795bba78ed26c196b0e05b2ffe5de6dc6761a99
MD5 6b4612cc44d1be0ad4451b5339decd80
BLAKE2b-256 2939136663debbb626d27124594b83ca7f050a4cbdfbfcb3ca986b8f5dc7b03b

See more details on using hashes here.

Provenance

The following attestation bundles were made for raygeo-0.5.0-cp311-abi3-manylinux_2_35_x86_64.whl:

Publisher: release.yml on barebaric/raygeo

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

File details

Details for the file raygeo-0.5.0-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for raygeo-0.5.0-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8c52528fe842491246eb0a08640247cd16c43b04728c63e0a2af8b48aea29ef9
MD5 221adf55b8ed915cc27d9491d8a4105f
BLAKE2b-256 b75281b526d8cac4225de725c8bede241fedb4cb55f98ada6864e21ea87c4b8e

See more details on using hashes here.

Provenance

The following attestation bundles were made for raygeo-0.5.0-cp311-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on barebaric/raygeo

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

File details

Details for the file raygeo-0.5.0-cp311-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for raygeo-0.5.0-cp311-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b5d984a3e2a07f8055e15f4a27202f475937ae2c3d6c24e45ff1de813356a9b1
MD5 7f7c73e35daf9a92a534657c1d2edaf9
BLAKE2b-256 42538f8110d7b0a57b6d8c9cc29c7345182f84a5e761a52f76ba0717cb6e2248

See more details on using hashes here.

Provenance

The following attestation bundles were made for raygeo-0.5.0-cp311-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on barebaric/raygeo

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