Skip to content

Repository files navigation

OpenSpatial

LicenseCIcodecov

Open-source implementation inspired by Apple's Spatial framework. OpenSpatial is not affiliated with, endorsed by, or maintained by Apple.

Overview

The OpenSpatial module is a lightweight 3D mathematical library that provides a simple API for working with 3D primitives. The API surface mirrors Apple's Spatial framework as closely as possible, so code written against OpenSpatial can be migrated to the official framework with minimal changes.

Goals

OpenSpatial aims to:

  • Provide a fully open-source implementation of common spatial mathematics primitives.
  • Remain API-compatible with Apple's Spatial framework whenever practical.
  • Support Apple and non-Apple platforms equally.
  • Serve as a foundation for graphics, simulation, robotics, game development, and spatial computing projects.

Features

  • Pure Swift 6, no platform-specific dependencies
  • Full Sendable conformance for safe use in Swift Concurrency contexts
  • Codable, Hashable, and Equatable on all value types
  • @frozen structs where applicable for ABI stability
  • Generic arithmetic operators and protocol-driven design (Rotatable3D, Translatable3D, Scalable3D, …)

Available Types

2D Primitives

TypeDescription
Angle2DA geometric angle

3D Primitives

TypeDescription
Point3DA point in 3D space
Size3DA 3D size with width, height, and depth
Rect3DAn axis-aligned 3D bounding box
Pose3DA combined position and orientation
ScaledPose3DA pose extended with a uniform scale factor
Ray3DAn origin and direction defining a ray
Rotation3DA rotation in 3D space (quaternion-backed)
RotationAxis3DA named axis for rotation
Quaternion3DA quaternion for representing rotations
SphericalCoordinates3DA point in spherical coordinates

Transforms

TypeDescription
AffineTransform3DA 4×4 affine transform (translation, rotation, scale, shear)
ProjectiveTransform3DA full 4×4 projective transform

Coordinate Spaces

TypeDescription
CoordinateSpace3DA named 3D coordinate space
CoordinateSpaceValue3DA value associated with a coordinate space
WorldReferenceCoordinateSpaceThe world reference coordinate space

Data Structures & Protocols

TypeDescription
Vector3DA 3D vector
Axis3DA named axis (x, y, z)
EulerAnglesEuler angle representation of a rotation
Primitive3DProtocol for all 3D primitives
Rotatable3DProtocol for types that support rotation
Translatable3DProtocol for types that support translation
Scalable3DProtocol for types that support scaling
Shearable3DProtocol for types that support shearing
Clampable3DProtocol for types that can be clamped to a Rect3D
VolumetricProtocol for types with a volumetric extent

Usage

Working with Points

import OpenSpatial
letorigin=Point3D(x:0, y:0, z:0)lettarget=Point3D(x:3, y:4, z:0)letdistance= origin.distance(to: target) // 5.0
letmoved= origin.translated(by:Vector3D(x:1, y:2, z:3))letscaled= origin.uniformlyScaled(by:2.0)

Working with Rotations and Poses

letrotation=Rotation3D(angle:Angle2D(radians:.pi /4), axis:RotationAxis3D(x:0, y:1, z:0))letpose=Pose3D(position:Point3D(x:1, y:0, z:0), rotation: rotation)letrotatedPoint=Point3D(x:1, y:0, z:0).rotated(by: rotation.quaternion)letinvertedPose= pose.inverse

Applying Transforms

vartransform=AffineTransform3D.identity
transform = transform.concatenating(.init(translation:Vector3D(x:5, y:0, z:0)))lettransformedPoint= point.applying(transform)

Raycasting

letray=Ray3D(origin:Point3D(x:0, y:0, z:-1), direction:Vector3D(x:0, y:0, z:1))letbox=Rect3D(origin:Point3D(x:-0.5, y:-0.5, z:0), size:Size3D(width:1, height:1, depth:1))if ray.intersects(box){print("Hit!")}

Installation

Swift Package Manager

Add the dependency to your Package.swift:

dependencies:[.package(
url:"https://github.com/helbertgs/OpenSpatial.git",
from:"0.1.0")],targets:[.target(
name:"MyTarget",
dependencies:[.product(name:"OpenSpatial",package:"OpenSpatial")]),]

Requirements

ComponentVersion
Swift6.0+

Supported Platforms

The project is continuously tested on:

  • macOS
  • Ubuntu Linux
  • Windows

Additional support is expected to work on:

  • iOS
  • tvOS
  • watchOS
  • WebAssembly (Wasm)
  • Android

Why OpenSpatial?

FeatureOpenSpatialApple Spatial
Open Source
Linux
Windows
Android
Wasm

Use Cases

OpenSpatial can be used for:

  • 3D engines
  • CAD applications
  • Robotics
  • Simulators
  • AR/VR applications
  • Scientific visualization
  • Game development

Documentation

Full API documentation is generated with DocC and available in the docs/ folder and also in this link: OpenSpatial. You can also browse it locally by running:

swift package --disable-sandbox preview-documentation --target OpenSpatial

Contributing

Contributions are welcome!

Before contributing, please review:

License

OpenSpatial is released under the MIT License.