UNPKG

ply-js

Version:

A TypeScript port based on python-plyfile for reading and writing .ply files

140 lines (139 loc) 5.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PlyListProperty = exports.PlyProperty = void 0; /* * This file is part of python-plyfile (original work Copyright © 2014-2025 Darsh Ranjan * and plyfile authors). TypeScript port © 2025 Gustavo Diogo Silva (GitHub: GustavoDiogo). * * This program is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with this * program. If not, see <http://www.gnu.org/licenses/>. */ const utils_1 = require("./utils"); const utils_2 = require("./utils"); class PlyProperty { constructor(name, valDtype) { this._name = String(name); this.valDtype = valDtype; } get name() { return this._name; } get valDtype() { return this._valDtypeCode; } set valDtype(val) { this._valDtypeCode = (0, utils_1.lookupType)(val); } dtype(byteOrder = '=') { return `${byteOrder}${this._valDtypeCode}`; // keep Python-esque flavor } _fromFields(fields) { const next = fields.next(); if (next.done) throw new Error('StopIteration'); const value = Number(next.value); if (Number.isNaN(value)) throw new Error('ValueError'); return value; } *_toFields(value) { yield value; } _readBin(view, offset, byteOrder) { const code = this._valDtypeCode; if (process.env.PLY_DEBUG === '1') { // eslint-disable-next-line no-console console.debug(`PlyProperty._readBin: prop=${this._name} code=${code} offset=${offset} byteOrder=${byteOrder}`); } const { values, next } = (0, utils_2.readArray)(view, offset, 1, code, byteOrder); if (values.length < 1) throw new Error('StopIteration'); return { value: values[0], next }; } _writeBin(value, byteOrder) { return (0, utils_2.writeArray)([value], this._valDtypeCode, byteOrder); } toString() { const valStr = utils_1.dataTypeReverse[this._valDtypeCode]; return `property ${valStr} ${this.name}`; } } exports.PlyProperty = PlyProperty; class PlyListProperty extends PlyProperty { constructor(name, lenDtype, valDtype) { super(name, valDtype); this.lenDtype = lenDtype; } get lenDtype() { return this._lenDtypeCode; } set lenDtype(v) { this._lenDtypeCode = (0, utils_1.lookupType)(v); } // For parity: list dtype for field storage is always object/variant in JS dtype() { return '|O'; } listDtype(byteOrder = '=') { const bo = byteOrder === '=' ? utils_2.nativeByteOrder : byteOrder; return [`${bo}${this._lenDtypeCode}`, `${bo}${this.valDtype}`]; } _fromFields(fields) { const first = fields.next(); if (first.done) throw new Error('StopIteration'); const n = Number(first.value); if (!Number.isFinite(n)) throw new Error('ValueError'); const out = []; for (let i = 0; i < n; i++) { const item = fields.next(); if (item.done) throw new Error('StopIteration'); const v = Number(item.value); if (Number.isNaN(v)) throw new Error('ValueError'); out.push(v); } return out; } *_toFields(value) { yield value.length; for (const v of value) yield v; } _readListAndAdvance(view, offset, byteOrder) { const [lenCode, valCode] = this.listDtype(byteOrder); if (process.env.PLY_DEBUG === '1') { // eslint-disable-next-line no-console console.debug(`PlyListProperty._readListAndAdvance: prop=${this.name} lenCode=${lenCode} valCode=${valCode} offset=${offset}`); } const lenRead = (0, utils_2.readArray)(view, offset, 1, lenCode.slice(1), byteOrder); if (lenRead.values.length < 1) throw new Error('StopIteration'); const n = lenRead.values[0]; const valRead = (0, utils_2.readArray)(view, lenRead.next, n, valCode.slice(1), byteOrder); if (valRead.values.length < n) throw new Error('StopIteration'); return { list: valRead.values, next: valRead.next }; } _readBin(view, offset, byteOrder) { const { list, next } = this._readListAndAdvance(view, offset, byteOrder); return { value: list, next }; } _writeBin(value, byteOrder) { const [lenCode, valCode] = this.listDtype(byteOrder); const list = value; const lenBuf = (0, utils_2.writeArray)([list.length], lenCode.slice(1), byteOrder); const valBuf = (0, utils_2.writeArray)(list, valCode.slice(1), byteOrder); return Buffer.concat([lenBuf, valBuf]); } toString() { const lenStr = utils_1.dataTypeReverse[this.lenDtype]; const valStr = utils_1.dataTypeReverse[this.valDtype]; return `property list ${lenStr} ${valStr} ${this.name}`; } } exports.PlyListProperty = PlyListProperty;