-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathcomponent.ts
More file actions
407 lines (342 loc) · 10.8 KB
/
component.ts
File metadata and controls
407 lines (342 loc) · 10.8 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**
* @file Component
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @private
*/
import { Vector3, Quaternion, Matrix4, Euler, Box3 } from 'three'
import { Signal } from 'signals'
import { defaults, createParams } from '../utils'
import { generateUUID } from '../math/math-utils'
import Annotation, { AnnotationParams } from '../component/annotation'
import ComponentControls from '../controls/component-controls'
import { makeRepresentation } from '../representation/representation-utils'
import RepresentationElement from './representation-element'
import Stage from '../stage/stage'
import Viewer from '../viewer/viewer'
const _m = new Matrix4()
const _v = new Vector3()
export const ComponentDefaultParameters = {
name: '',
status: '',
visible: true
}
export type ComponentParameters = typeof ComponentDefaultParameters
export interface ComponentSignals {
representationAdded: Signal // when a representation is added
representationRemoved: Signal // when a representation is removed
visibilityChanged: Signal // on visibility change
matrixChanged: Signal // on matrix change
statusChanged: Signal // on status change
nameChanged: Signal // on name change
disposed: Signal // on dispose
}
/**
* Base class for components
*/
abstract class Component {
/**
* Events emitted by the component
*/
readonly signals: ComponentSignals = {
representationAdded: new Signal(),
representationRemoved: new Signal(),
visibilityChanged: new Signal(),
matrixChanged: new Signal(),
statusChanged: new Signal(),
nameChanged: new Signal(),
disposed: new Signal()
}
readonly parameters: ComponentParameters
get defaultParameters () { return ComponentDefaultParameters }
readonly uuid: string
readonly viewer: Viewer
reprList: RepresentationElement[] = []
annotationList: Annotation[] = []
matrix = new Matrix4()
position = new Vector3()
quaternion = new Quaternion()
scale = new Vector3(1, 1, 1)
transform = new Matrix4()
controls: ComponentControls
/**
* @param {Stage} stage - stage object the component belongs to
* @param {ComponentParameters} params - parameter object
*/
constructor (readonly stage: Stage, readonly object: any, params: Partial<ComponentParameters> = {}) {
this.parameters = createParams(params, this.defaultParameters)
this.uuid = generateUUID()
this.viewer = stage.viewer
this.controls = new ComponentControls(this)
}
abstract get type (): string
get name () { return this.parameters.name }
get status () { return this.parameters.status }
get visible () { return this.parameters.visible }
/**
* Set position transform
*
* @example
* // translate by 25 angstrom along x axis
* component.setPosition([ 25, 0, 0 ]);
*
* @param {Vector3|Array} p - the coordinates
* @return {Component} this object
*/
setPosition (p: [number, number, number]|Vector3) {
if (Array.isArray(p)) {
this.position.fromArray(p)
} else {
this.position.copy(p)
}
this.updateMatrix()
return this
}
/**
* Set rotation transform
*
* @example
* // rotate by 2 degree radians on x axis
* component.setRotation( [ 2, 0, 0 ] );
*
* @param {Quaternion|Euler|Array} r - the rotation
* @return {Component} this object
*/
setRotation (r: [number, number, number]|Euler|Quaternion) {
if (Array.isArray(r)) {
if (r.length === 3) {
const e = new Euler().fromArray(r)
this.quaternion.setFromEuler(e)
} else {
this.quaternion.fromArray(r)
}
} else if (r instanceof Euler) {
this.quaternion.setFromEuler(r)
} else {
this.quaternion.copy(r)
}
this.updateMatrix()
return this
}
/**
* Set scale transform
*
* @example
* // scale by factor of two
* component.setScale( 2 );
*
* @param {Number} s - the scale
* @return {Component} this object
*/
setScale (s: number) {
this.scale.set(s, s, s)
this.updateMatrix()
return this
}
/**
* Set general transform. Is applied before and in addition
* to the position, rotation and scale transformations
*
* @example
* component.setTransform( matrix );
*
* @param {Matrix4} m - the matrix
* @return {Component} this object
*/
setTransform (m: Matrix4) {
this.transform.copy(m)
this.updateMatrix()
return this
}
updateMatrix () {
const c = this.getCenterUntransformed(_v)
this.matrix.makeTranslation(-c.x, -c.y, -c.z)
_m.makeRotationFromQuaternion(this.quaternion)
this.matrix.premultiply(_m)
_m.makeScale(this.scale.x, this.scale.y, this.scale.z)
this.matrix.premultiply(_m)
const p = this.position
_m.makeTranslation(p.x + c.x, p.y + c.y, p.z + c.z)
this.matrix.premultiply(_m)
this.matrix.premultiply(this.transform)
this.reprList.forEach(repr => {
repr.setParameters({ matrix: this.matrix })
})
this.stage.viewer.updateBoundingBox()
this.signals.matrixChanged.dispatch(this.matrix)
}
/**
* Add an anotation object
* @param {Vector3} position - the 3d position
* @param {String|Element} content - the HTML content
* @param {Object} [params] - parameters
* @param {Integer} params.offsetX - 2d offset in x direction
* @param {Integer} params.offsetY - 2d offset in y direction
* @return {Annotation} the added annotation object
*/
addAnnotation (position: Vector3, content: string|HTMLElement, params: AnnotationParams) {
const annotation = new Annotation(this, position, content, params)
this.annotationList.push(annotation)
return annotation
}
/**
* Iterator over each annotation and executing the callback
* @param {Function} callback - function to execute
* @return {undefined}
*/
eachAnnotation (callback: (a: Annotation) => void) {
this.annotationList.slice().forEach(callback)
}
/**
* Remove the give annotation from the component
* @param {Annotation} annotation - the annotation to remove
* @return {undefined}
*/
removeAnnotation (annotation: Annotation) {
const idx = this.annotationList.indexOf(annotation)
if (idx !== -1) {
this.annotationList.splice(idx, 1)
annotation.dispose()
}
}
/**
* Remove all annotations from the component
* @return {undefined}
*/
removeAllAnnotations () {
this.eachAnnotation(annotation => annotation.dispose())
this.annotationList.length = 0
}
/**
* Add a new representation to the component
* @param {String} type - the name of the representation
* @param {Object} object - the object on which the representation should be based
* @param {RepresentationParameters} [params] - representation parameters
* @return {RepresentationElement} the created representation wrapped into
* a representation element object
*/
protected _addRepresentation (type: string, object: any, params: any, hidden = false) { // TODO
const p = params || {}
const sp = this.stage.getParameters() as any // TODO
p.matrix = this.matrix.clone()
p.quality = p.quality || sp.quality
p.disableImpostor = defaults(p.disableImpostor, !sp.impostor)
p.useWorker = defaults(p.useWorker, sp.workerDefault)
p.visible = defaults(p.visible, true)
const p2 = Object.assign({}, p, { visible: this.parameters.visible && p.visible })
const repr = makeRepresentation(type, object, this.viewer, p2)
const reprElem = new RepresentationElement(this.stage, repr, p, this)
if (!hidden) {
this.reprList.push(reprElem)
this.signals.representationAdded.dispatch(reprElem)
}
return reprElem
}
abstract addRepresentation (type: any, params: any): any
addBufferRepresentation (buffer: any, params: any) { // TODO
return this._addRepresentation.call(this, 'buffer', buffer, params)
}
hasRepresentation (repr: RepresentationElement) {
return this.reprList.indexOf(repr) !== -1
}
/**
* Iterator over each representation and executing the callback
* @param {Function} callback - function to execute
* @return {undefined}
*/
eachRepresentation (callback: (repr: RepresentationElement) => void) {
this.reprList.slice().forEach(callback)
}
/**
* Removes a representation component
* @param {RepresentationElement} repr - the representation element
* @return {undefined}
*/
removeRepresentation (repr: RepresentationElement) {
const idx = this.reprList.indexOf(repr)
if (idx !== -1) {
this.reprList.splice(idx, 1)
repr.dispose()
this.signals.representationRemoved.dispatch(repr)
}
}
updateRepresentations (what: any) { // TODO
this.reprList.forEach(repr => repr.update(what))
this.stage.viewer.requestRender()
}
/**
* Removes all representation components
* @return {undefined}
*/
removeAllRepresentations () {
this.eachRepresentation(repr => repr.dispose())
}
dispose () {
this.removeAllAnnotations()
this.removeAllRepresentations()
delete this.annotationList
delete this.reprList
this.signals.disposed.dispatch()
}
/**
* Set the visibility of the component, including added representations
* @param {Boolean} value - visibility flag
* @return {Component} this object
*/
setVisibility (value: boolean) {
this.parameters.visible = value
this.eachRepresentation((repr: RepresentationElement) => repr.updateVisibility())
this.eachAnnotation((annotation: Annotation) => annotation.updateVisibility())
this.signals.visibilityChanged.dispatch(value)
return this
}
setStatus (value: string) {
this.parameters.status = value
this.signals.statusChanged.dispatch(value)
return this
}
setName (value: string) {
this.parameters.name = value
this.signals.nameChanged.dispatch(value)
return this
}
/**
* @return {Box3} the component's bounding box
*/
getBox (...args: any[]) {
return this.getBoxUntransformed(...args)
.clone().applyMatrix4(this.matrix)
}
/**
* @return {Vector3} the component's center position
*/
getCenter (...args: any[]) {
return this.getCenterUntransformed(...args)
.clone().applyMatrix4(this.matrix)
}
getZoom (...args: any[]) {
return this.stage.getZoomForBox(this.getBox(...args))
}
/**
* @abstract
* @return {Box3} the untransformed component's bounding box
*/
getBoxUntransformed (...args: any[]): Box3 {
return new Box3()
}
getCenterUntransformed (...args: any[]) {
return this.getBoxUntransformed().getCenter(new Vector3())
}
/**
* Automatically center and zoom the component
* @param {Integer} [duration] - duration of the animation, defaults to 0
* @return {undefined}
*/
autoView (duration?: number) {
this.stage.animationControls.zoomMove(
this.getCenter(),
this.getZoom(),
defaults(duration, 0)
)
}
}
export default Component