{"id":27203253,"url":"https://github.com/mattcox/mortoncode","last_synced_at":"2025-04-09T22:29:18.312Z","repository":{"id":285382685,"uuid":"957935766","full_name":"mattcox/MortonCode","owner":"mattcox","description":"A Swift package for computing a Morton Code in N dimensions","archived":false,"fork":false,"pushed_at":"2025-03-31T11:57:52.000Z","size":0,"stargazers_count":0,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-31T12:34:33.954Z","etag":null,"topics":["data-type","package","swift","utility"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mattcox.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-31T11:43:37.000Z","updated_at":"2025-03-31T12:04:47.000Z","dependencies_parsed_at":"2025-03-31T12:53:09.143Z","dependency_job_id":null,"html_url":"https://github.com/mattcox/MortonCode","commit_stats":null,"previous_names":["mattcox/mortoncode"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattcox%2FMortonCode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattcox%2FMortonCode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattcox%2FMortonCode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattcox%2FMortonCode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattcox","download_url":"https://codeload.github.com/mattcox/MortonCode/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248122727,"owners_count":21051329,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-type","package","swift","utility"],"created_at":"2025-04-09T22:29:17.591Z","updated_at":"2025-04-09T22:29:18.290Z","avatar_url":"https://github.com/mattcox.png","language":"Swift","readme":"# MortonCode\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/Swift-orange.svg\" alt=\"Swift\" /\u003e\n    \u003ca href=\"https://swift.org/package-manager\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/swiftpm-compatible-brightgreen.svg?style=flat\" alt=\"Swift Package Manager\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\nWelcome to **MortonCode**, a Swift package for computing a [Morton Code](https://en.wikipedia.org/wiki/Z-order_curve) in N dimensions.\n\nA Morton code (also known as Z-order or Lebesgue curve), is a mapping from many dimensions into one. It ensures locality of information, where two morton codes with a similar value represent a close spatial proximity. It can be very useful for building spatial acceleration structures such as a BVH or an Octree.\n\n## Usage\n\n### Basic Usage\nTo encode a N dimensional morton code, you simply pass integers encoding coordinates to the initializer. The integers represent the bits to interleave into the morton code. For example, for 16-bit unsigned integers `12061` and `31418`:\n\n```swift\nlet morton = try MortonCode\u003cUInt32\u003e(12061, 31418)\nprint(morton.value)  // 786271193\n```\n\nThe coordinates can be decoded from the morton code:\n```swift\nlet coordinates = morton.coordinates(count: 2)\nprint(coordinates)  // [12061, 31418]\n```\n\nThe numeric values used to initialize the morton code must be able to be represented in fewer bits than the morton code size, for example a `UInt32` morton code can encode two `UInt16` values, or four `UInt8` values. A `UInt32` morton code can encode four `UInt16` values, but the numeric value of the `UInt16` must each be able to be stored in 8-bits.\n\n### Higher Dimension Morton Code\nThe value stored in the morton code is based on the value, not necessarily the data type used to initialize it. For example a 64-bit morton code could in theory encode 32 dimensions of data, where each dimension is represented by 2 bits:\n\n```swift\nlet morton = try MortonCode\u003cUInt64\u003e(1, 0, 1, 1, 2, 2, 3, 3, 0, 0, 3, 3, 0, 1, 1, 0, 2, 3, 3, 0, 3, 2, 0, 2, 3, 1, 2, 1, 2, 2, 3, 3)\nprint(morton.value)  // 17705634688369323213\n```\n\n### Encoding Floating Point Values\nWhen working with 2D/3D coordinate systems, mapping floating point coordinates into integers can be a pain. Therefore some helper functions are provided for `RealityKit`, `simd` and `Spatial` APIs. For example to encode a floating point coordinate within the bounds of a bounding box:\n\n```swift\nlet coordinates = SIMD3\u003cFloat\u003e(-2.0, 1.2, 4.7)\nlet bounds = BoundingBox(min: SIMD3\u003cFloat\u003e(-5.0, -5.0, -5.0), max: SIMD3\u003cFloat\u003e(5.0, 5.0, 5.0))\n\t\nlet mortonCode = try MortonCode\u003cUInt64\u003e(coordinates: coordinates, in: bounds)\nprint(mortonCode.value)  //7725758287100141315\n```\n\nNote that when converting the morton code back into coordinates, the integer values will be returned, not the original floating point values.\n\n## Installation\n\nMortonCode is distributed using the [Swift Package Manager](https://swift.org/package-manager). To install it within another Swift package, add it as a dependency within your `Package.swift` manifest:\n\n```swift\nlet package = Package(\n    // . . .\n    dependencies: [\n        .package(url: \"https://github.com/MattCox/MortonCode.git\", branch: \"main\")\n    ],\n    // . . .\n)\n```\n\nIf you’d like to use MortonCode within an iOS, macOS, watchOS or tvOS app, then use Xcode’s `File \u003e Add Packages...` menu command to add it to your project.\n\nImport MortonCode wherever you’d like to use it:\n```swift\nimport MortonCode\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattcox%2Fmortoncode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattcox%2Fmortoncode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattcox%2Fmortoncode/lists"}