-
Notifications
You must be signed in to change notification settings - Fork 0
/
LabelsClusterExtension.js
160 lines (142 loc) · 5.85 KB
/
LabelsClusterExtension.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
class LabelsClusterExtension extends Autodesk.Viewing.Extension {
constructor(viewer, options) {
super(viewer, options);
this._button = null;
this._changeInProgress = false;
//treshold in pixels
this.treshold = 80;
this._onObjectTreeCreated = (ev) => this.onModelLoaded(ev.model);
}
async onModelLoaded(model) {
this.dataVizExtn = await this.viewer.getExtension("Autodesk.DataVisualization");
await this.prepareDataViz();
this.viewer.addEventListener(Autodesk.Viewing.CAMERA_CHANGE_EVENT, this.groupAndRenderLabels.bind(this));
}
groupAndRenderLabels() {
if (!this._changeInProgress) {
this._changeInProgress = true;
this.scheduleChange();
}
}
scheduleChange() {
setTimeout(() => {
let indexGroups;
if (this._button.getState() == Autodesk.Viewing.UI.Button.State.ACTIVE) {
indexGroups = this.findIndexGroups();
}
else {
indexGroups = document.labelsJSON.labelsPositions.map((p, i) => [i]);
}
this.replaceLabels(9999, indexGroups);
this._changeInProgress = false;
}, 200);
}
findIndexGroups() {
let indexGroups = [];
for (let i = 0; i < document.labelsJSON.labelsPositions.length; i++) {
let currentPosition = this.viewer.worldToClient(document.labelsJSON.labelsPositions[i]);
const currentIndexColumn = Math.floor(currentPosition.x / this.treshold);
const currentIndexRow = Math.floor(currentPosition.y / this.treshold);
if (!indexGroups[currentIndexRow]) {
indexGroups[currentIndexRow] = [];
}
if (!indexGroups[currentIndexRow][currentIndexColumn]) {
indexGroups[currentIndexRow][currentIndexColumn] = [];
}
indexGroups[currentIndexRow][currentIndexColumn].push(i);
}
return indexGroups.flat();
}
async load() {
this.viewer.addEventListener(Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT, this._onObjectTreeCreated);
return true;
}
replaceLabels(dbId, indexGroups) {
let DataVizCore = Autodesk.DataVisualization.Core;
this.dataVizExtn.removeAllViewables();
this.viewableData = new DataVizCore.ViewableData();
this.viewableData.spriteSize = 32;
for (let indexGroup of indexGroups) {
dbId++;
let spritePoint;
let spriteStyle;
if (indexGroup.length > 1) {
spritePoint = this.findMiddlePoint(indexGroup);
spriteStyle = this.pointStyles[1];
}
else {
spritePoint = document.labelsJSON.labelsPositions[indexGroup[0]];
spriteStyle = this.pointStyles[0];
}
let viewable = new DataVizCore.SpriteViewable(spritePoint, spriteStyle, dbId);
this.viewableData.addViewable(viewable);
}
this.viewableData.finish().then(() => {
this.dataVizExtn.addViewables(this.viewableData);
});
}
findMiddlePoint(indexGroup) {
let positions = indexGroup.map(index => document.labelsJSON.labelsPositions[index]);
let middleX = positions.map(p => p.x).reduce((acc, cur) => acc + cur, 0) / indexGroup.length;
let middleY = positions.map(p => p.y).reduce((acc, cur) => acc + cur, 0) / indexGroup.length;
let middleZ = positions.map(p => p.z).reduce((acc, cur) => acc + cur, 0) / indexGroup.length;
return { x: middleX, y: middleY, z: middleZ };
}
async prepareDataViz() {
this.dataVizExtn = this.viewer.getExtension("Autodesk.DataVisualization");
let DataVizCore = Autodesk.DataVisualization.Core;
this.viewableData = new DataVizCore.ViewableData();
this.viewableData.spriteSize = 32; // Sprites as points of size 24 x 24 pixels
let viewableType = DataVizCore.ViewableType.SPRITE;
let pointsColor = new THREE.Color(0xffffff);
let onePointIconUrl = "https://img.icons8.com/ios/50/null/1-circle.png";
// let multiplePointsIconUrl = "https://img.icons8.com/external-vitaliy-gorbachev-lineal-vitaly-gorbachev/50/null/external-numbers-support-vitaliy-gorbachev-lineal-vitaly-gorbachev.png";
let multiplePointsIconUrl = "https://raw.githubusercontent.com/autodesk-platform-services/aps-labelscluster-sample/main/assets/cluster.svg";
let onePointStyle = new DataVizCore.ViewableStyle(viewableType, pointsColor, onePointIconUrl);
let multiplePointsStyle = new DataVizCore.ViewableStyle(viewableType, pointsColor, multiplePointsIconUrl);
this.pointStyles = [
onePointStyle,
multiplePointsStyle
];
}
unload() {
if (this._button) {
this.removeToolbarButton(this._button);
this._button = null;
}
return true;
}
onToolbarCreated() {
this._button = this.createToolbarButton('labelscluster-button', 'https://img.icons8.com/ios/30/null/combine.png', 'Labels Cluster');
this._button.onClick = () => {
if (this._button.getState() == Autodesk.Viewing.UI.Button.State.ACTIVE) {
this._button.setState(Autodesk.Viewing.UI.Button.State.INACTIVE);
} else {
this._button.setState(Autodesk.Viewing.UI.Button.State.ACTIVE);
}
};
}
createToolbarButton(buttonId, buttonIconUrl, buttonTooltip) {
let group = this.viewer.toolbar.getControl('labels-toolbar-group');
if (!group) {
group = new Autodesk.Viewing.UI.ControlGroup('labels-toolbar-group');
this.viewer.toolbar.addControl(group);
}
const button = new Autodesk.Viewing.UI.Button(buttonId);
button.setToolTip(buttonTooltip);
group.addControl(button);
const icon = button.container.querySelector('.adsk-button-icon');
if (icon) {
icon.style.backgroundImage = `url(${buttonIconUrl})`;
icon.style.backgroundSize = `24px`;
icon.style.backgroundRepeat = `no-repeat`;
icon.style.backgroundPosition = `center`;
}
return button;
}
removeToolbarButton(button) {
const group = this.viewer.toolbar.getControl('labels-toolbar-group');
group.removeControl(button);
}
}
Autodesk.Viewing.theExtensionManager.registerExtension('LabelsClusterExtension', LabelsClusterExtension);