PointCloudLoader.js
2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { FileLoader, Loader, Vector3 } from 'three';
class PointCloudLoader extends Loader {
constructor(manager) {
super(manager);
}
load(url, onLoad, onProgress, onError) {
const loader = new FileLoader(this.manager);
loader.setPath(this.path);
loader.setResponseType('arraybuffer');
loader.setRequestHeader(this.requestHeader);
loader.setWithCredentials(this.withCredentials);
loader.load(
url,
function (data) {
try {
onLoad(parsePointCloudData(data));
} catch (e) {
handleLoadError(e, url, onError);
}
},
onProgress,
function (error) {
handleLoadError(error, url, onError);
}
);
}
parse(data) {
function ensureString(buffer) {
return typeof buffer !== 'string' ? new TextDecoder().decode(buffer) : buffer;
}
const textData = ensureString(data);
let pointCloudData = textData.split(/\s/);
let index = 0;
const vertices = [];
pointCloudData = pointCloudData.filter((el) => el.length > 0);
while (index < pointCloudData.length) {
if (pointCloudData[index] && pointCloudData[index + 1] && pointCloudData[index + 2]) {
vertices.push(
new Vector3(
Number(pointCloudData[index]),
Number(pointCloudData[index + 1]),
Number(pointCloudData[index + 2])
)
);
}
index += 3; // Assuming each point has 4 units of data
}
return {
type: 'PtsModel',
vertices
};
}
}
function parsePointCloudData(data) {
return new PointCloudLoader().parse(data);
}
function handleLoadError(error, url, onError) {
if (onError) {
onError(error);
} else {
console.error(error);
}
new PointCloudLoader().manager.itemError(url);
}
export { PointCloudLoader };