dymokomi/luce-ply
Source of release 0.1.0
## The luce-ply export: PLY for Luce. Loading PLY files (ASCII and binary,
## any elements) as luce-geocore GeometrySets, and saving GeometrySets as
## PLY. Everything here is a thin entry point; the work is in reader.lucb,
## compressed.lucb and writer.lucb.
import interop
import memory
from luce_geocore.geocore import GeometrySet
from luce_geocore.kernel import adopt_set
from header import Format, parse_header
from warnings import Warnings
import reader
import writer
import source
import errors
## The last load's or save's warnings on this thread, up to 4 KiB of whole lines.
local var last_warnings: u8[4096]
local var last_length: usize
pub struct Ply:
let reserved: u8 = 0
## The PLY file at `path` as a GeometrySet: a mesh when it has faces,
## else a point cloud, with every property kept as an attribute (see
## reader.lucb for the names). 3D Gaussian splat files, plain or
## PlayCanvas compressed, keep the raw 3DGS names with their spherical
## harmonics as one interleaved `f_rest` array. What was left out is in
## `warnings()`.
pub static func load(path: str) -> interop.Reference[GeometrySet]!:
var data = try source.read(path)
defer data.release()
var warnings = Warnings()
defer warnings.close()
let geometry = try reader.decode(data.value, &warnings)
remember(&warnings)
return try adopt_set(geometry)
## Write `geometry` to `path` as PLY: `format` is "binary" (little-endian,
## the default), "binary_big_endian" or "ascii". Splat clouds with the
## raw 3DGS names are written in the reference 3DGS layout. What was left
## out is in `warnings()`.
pub static func save(geometry: interop.Reference[GeometrySet], path: str, format: str = "binary") -> !:
var encoding = Format.little_endian
if format == "ascii": encoding = Format.ascii
elif format == "binary_big_endian": encoding = Format.big_endian
elif format != "binary" and format != "binary_little_endian": error(errors.unsupported, "PLY formats are binary, binary_big_endian and ascii")
var warnings = Warnings()
defer warnings.close()
let data = try writer.encode(try geometry.get(), encoding, &warnings)
defer free(data) in memory.heap
try source.write(path, data)
remember(&warnings)
## The header of the PLY file at `path`, from `ply` to `end_header`.
pub static func header(path: str) -> interop.Owned[str]!:
var data = try source.read(path)
defer data.release()
var parsed = try parse_header(data.value)
defer parsed.close()
return try interop.copy_text((str)data.value[..<parsed.body])
## The warnings of this thread's last load or save, one per line.
pub static func warnings() -> interop.Owned[str]!:
return try interop.copy_text((str)last_warnings[..<last_length])
func remember(warnings: const Warnings*):
let text = warnings.text.view()
let length = text.length if text.length < last_warnings.length else last_warnings.length
memory.copy(last_warnings[..<length], text[..<length], length)
last_length = length