UNPKG

@inweb/viewer-three

Version:

JavaScript library for rendering CAD and BIM files in a browser using Three.js

96 lines (75 loc) 3.48 kB
/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2025, Open Design Alliance (the "Alliance"). // All rights reserved. // // This software and its documentation and related materials are owned by // the Alliance. The software may only be incorporated into application // programs owned by members of the Alliance, subject to a signed // Membership Agreement and Supplemental Software License Agreement with the // Alliance. The structure and organization of this software are the valuable // trade secrets of the Alliance and its suppliers. The software is also // protected by copyright law and international treaty provisions. Application // programs incorporating this software must include the following statement // with their copyright notices: // // This application incorporates Open Design Alliance software pursuant to a // license agreement with Open Design Alliance. // Open Design Alliance Copyright (C) 2002-2025 by Open Design Alliance. // All rights reserved. // // By use of this software, its documentation or related materials, you // acknowledge and accept the above terms. /////////////////////////////////////////////////////////////////////////////// import { ModelUnits } from "./UnitConverter"; // Returns a standard string representation of the display unit. export function getDisplayUnit(units: string) { return (ModelUnits[units] || ModelUnits.Default).symbol; } // ===================== AI-CODE-START ====================== // Source: Claude Sonnet 4.5 // Date: 2025-10-08 // Reviewer: roman.mochalov@opendesign.com // Issue: CLOUD-5963 // Calculates recommended precision for small values (|value| < 1) to avoid showing "0.00". export function calculatePrecision(value: number) { const distance = Math.abs(value); if (distance >= 1000) return 0; if (distance >= 10) return 1; if (distance >= 0.1) return 2; if (distance >= 0.001) return 3; return distance > 0 ? Math.floor(-Math.log10(distance)) + 1 : 2; } // Calculates the minimal distance for the sepecified precision. export function calculateDistance(precision: any): number { let digits: number; if (precision === "Auto") digits = 2; else if (Number.isFinite(precision)) digits = precision; else digits = parseFloat(precision); if (!Number.isFinite(digits) || digits < 0) digits = 2; else if (digits > 10) digits = 10; return Math.pow(10, -digits); } // ===================== AI-CODE-END ====================== // Formats a distance with units. function formatNumber(distance: number, digits: number, precision: any) { let result = distance.toFixed(digits); if (precision === "Auto") result = result.replace(/\.0+$/, "").replace(/\.$/, ""); if (+result !== distance) result = "~ " + result; return result; } export function formatDistance(distance: number, units: string, precision: any = 2) { let digits: number; if (precision === "Auto") digits = calculatePrecision(distance); else if (Number.isFinite(precision)) digits = precision; else digits = parseFloat(precision); if (!Number.isFinite(digits)) digits = 2; else if (digits < 0) digits = 0; else if (digits > 10) digits = 10; if (ModelUnits[units]) { return formatNumber(distance, digits, precision) + " " + ModelUnits[units].symbol; } else if (units) { return formatNumber(distance, digits, precision) + " " + units; } else { return formatNumber(distance, digits, precision); } }