My attempt to determine the maximum value of a point cloud data using the following code proved unsuccessful.
import { PLYLoader } from "three/examples/jsm/loaders/PLYLoader";
let max_x = -Infinity;
function initModel() {
new PLYLoader().load("../data/division/1.ply", (geometry) => {
for (let j = 0; j < geometry.attributes.position.count; j += 3)
max_x = Math.max(max_x, geometry.attributes.position.array[j]);
});
}
function print() {
console.log(max_x);
}
initModel();
print();//resulted in -Infinity
The issue arose because Three.js
loaded the file asynchronously. Attempting to resolve it, I experimented with different solutions, such as adding async
and await
to the functions.
import { PLYLoader } from "three/examples/jsm/loaders/PLYLoader";
let max_x = -Infinity;
async function initModel() {
new PLYLoader().load("../data/division/1.ply", (geometry) => {
for (let j = 0; j < geometry.attributes.position.count; j += 3)
max_x = Math.max(max_x, geometry.attributes.position.array[j]);
});
}
async function print() {
await initModel();
console.log(max_x);
}
print();//still resulted in -Infinity
Despite my efforts, the output remained -Infinity. I sought solutions online, but the examples I found did not involve handling data within the callback function of load
like I did. Surprisingly, using setTimeout()
proved to be effective. Any assistance would be greatly appreciated.