From e55035b24d67c5ac790aa48a922c2ba3fd69d1de Mon Sep 17 00:00:00 2001 From: Vincent Gerber Date: Wed, 6 Nov 2024 08:54:14 +0100 Subject: [PATCH] fix: Fixed pose string precision --- src/lib/converters.ts | 4 ++-- test/utils.test.ts | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/lib/converters.ts b/src/lib/converters.ts index ec6ab56..bff0ad7 100644 --- a/src/lib/converters.ts +++ b/src/lib/converters.ts @@ -52,9 +52,9 @@ export function poseToWandelscriptString( pose.orientation?.z ?? 0, ] - const positionValues = position.map((v) => parseFloat(v.toFixed(1))) + const positionValues = position.map((v) => v.toFixed(1)) // Rotation needs more precision since it's in radians - const rotationValues = orientation.map((v) => parseFloat(v.toFixed(4))) + const rotationValues = orientation.map((v) => v.toFixed(4)) return `(${positionValues.concat(rotationValues).join(", ")})` } diff --git a/test/utils.test.ts b/test/utils.test.ts index ebcae27..c96f35e 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -3,11 +3,20 @@ import { expect, test } from "vitest" import { poseToWandelscriptString } from "../dist" -test("utility functions", async () => { +test("pose string has right precision", async () => { expect( poseToWandelscriptString({ position: { x: 1, y: 2, z: 3 }, orientation: { x: 4, y: 5, z: 6 }, }), - ).toBe("(1, 2, 3, 4, 5, 6)") + ).toBe("(1.0, 2.0, 3.0, 4.0000, 5.0000, 6.0000)") +}) + +test("pose string rounds correctly", async () => { + expect( + poseToWandelscriptString({ + position: { x: 1, y: 2.22, z: 3.556 }, + orientation: { x: 2, y: 3.3333, z: 1.55559 }, + }), + ).toBe("(1.0, 2.2, 3.6, 2.0000, 3.3333, 1.5556)") })