@abi-software/scaffoldvuer 1.0.0 → 1.1.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,5 @@
1
+ import { THREE } from 'zincjs';
2
+
1
3
  export const createListFromPrimitives = (primitives, list) => {
2
4
  if (primitives) {
3
5
  let id = "";
@@ -14,6 +16,77 @@ export const createListFromPrimitives = (primitives, list) => {
14
16
  return list;
15
17
  }
16
18
 
19
+ const getDistance = (point1, point2) => {
20
+ const dist0 = point1[0] - point2[0];
21
+ const dist1 = point1[1] - point2[1];
22
+ const dist2 = point1[2] - point2[2];
23
+ return Math.sqrt(dist0 * dist0 + dist1 * dist1 + dist2 * dist2);
24
+ }
25
+
26
+ export const getEditableLines = (event) => {
27
+ const zincObjects = event.zincObjects;
28
+ if (zincObjects.length > 0 && zincObjects[0]) {
29
+ const zincObject = zincObjects[0];
30
+ if (zincObject.isEditable && zincObject.isLines2) {
31
+ const info = event.identifiers[0].extraData.intersected;
32
+ if (info && info.faceIndex > -1) {
33
+ const v = zincObject.getVerticesByFaceIndex(info.faceIndex);
34
+ const p = event.identifiers[0].extraData.intersected.pointOnLine;
35
+ if (v.length > 1) {
36
+ const dist0 = getDistance(v[0], [p.x, p.y, p.z]);
37
+ const dist1 = getDistance(v[1], [p.x, p.y, p.z]);
38
+ if (dist0 > dist1) {
39
+ return { zincObject, faceIndex: info.faceIndex, vertexIndex: info.faceIndex * 2 + 1, point: v[0]};
40
+ } else {
41
+ return { zincObject, faceIndex: info.faceIndex, vertexIndex: info.faceIndex * 2, point: v[1]};
42
+ }
43
+ }
44
+ }
45
+ }
46
+ }
47
+ return undefined;
48
+ }
49
+
50
+ export const moveLine = (zincObject, faceIndex, unit) => {
51
+ if (zincObject && unit !== 0.0) {
52
+ if (zincObject.isEditable && zincObject.isLines2) {
53
+ if (faceIndex > -1) {
54
+ const v = zincObject.getVerticesByFaceIndex(faceIndex);
55
+ let d = [v[0][0] - v[1][0], v[0][1] - v[1][1], v[0][2] - v[1][2]];
56
+ const mag = Math.sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
57
+ for (let i = 0; i < 3; i++) {
58
+ d[i] = d[i] / mag * unit;
59
+ v[0][i] = v[0][i] + d[i];
60
+ v[1][i] = v[1][i] + d[i];
61
+ }
62
+ zincObject.editVertice(v, faceIndex * 2);
63
+ zincObject.boundingBoxUpdateRequired = true;
64
+ }
65
+ }
66
+ }
67
+ return undefined;
68
+ }
69
+
70
+ export const updateBoundingBox = (geometry, scene) => {
71
+ const box = scene.getBoundingBox();
72
+ const dim = new THREE.Vector3().subVectors(box.max, box.min);
73
+ const boxGeo = new THREE.BoxGeometry(dim.x, dim.y, dim.z);
74
+ dim.addVectors(box.min, box.max).multiplyScalar( 0.5 );
75
+ const positions = boxGeo.getAttribute("position");
76
+ const count = positions.count;
77
+ const vertices = [];
78
+ for (let i = 0; i < count; i++) {
79
+ vertices[i] = [
80
+ positions.array[i * 3],
81
+ positions.array[i * 3 + 1],
82
+ positions.array[i * 3 + 2]
83
+ ];
84
+ }
85
+ geometry.editVertices(vertices , 0);
86
+ geometry.setPosition(dim.x, dim.y, dim.z);
87
+ boxGeo.dispose();
88
+ }
89
+
17
90
  export const extractAllFullPaths = (item, list) => {
18
91
  let nodeName = "";
19
92
  if (item.isRegion) {
@@ -144,3 +217,122 @@ export const getObjectsFromAnnotations = (scene, annotations) => {
144
217
  }
145
218
  return returned;
146
219
  }
220
+
221
+ const getCoordinatesForAnnotationFeature = (zincObject) => {
222
+ const mesh = zincObject.getMorph();
223
+ let attr = 'position';
224
+ if (zincObject.isLines2) {
225
+ attr = 'instanceStart';
226
+ }
227
+ const coords = [];
228
+ let vIndex = 0;
229
+ const position = mesh.geometry.getAttribute( attr );
230
+ for (let i = 0; i < zincObject.drawRange; i++) {
231
+ coords.push([
232
+ position.array[vIndex++],
233
+ position.array[vIndex++],
234
+ position.array[vIndex++],
235
+ ]);
236
+ }
237
+ return coords;
238
+ }
239
+
240
+ const createNewAnnotationsWithFeatures = (zincObject, region, group, scaffoldUrl, comment) => {
241
+ let type = undefined;
242
+ if (zincObject.isPointset) {
243
+ type = "MultiPoint";
244
+ } else if (zincObject.isLines2) {
245
+ type = "MultiLineString";
246
+ }
247
+ if (type) {
248
+ const coords = getCoordinatesForAnnotationFeature(zincObject);
249
+ const featureID = encodeURIComponent(region + group);
250
+ const userAnnotation = {
251
+ resource: encodeURIComponent(scaffoldUrl),
252
+ item: {
253
+ "id": featureID,
254
+ },
255
+ body: {
256
+ evidence: [],
257
+ comment: comment,
258
+ },
259
+ feature: {
260
+ "id": featureID,
261
+ "properties": {
262
+ "drawn": true,
263
+ "label": "Drawn annotation"
264
+ },
265
+ "geometry": {
266
+ "coordinates": coords,
267
+ "type": type
268
+ }
269
+ },
270
+ }
271
+ return userAnnotation;
272
+ }
273
+ }
274
+
275
+ /*
276
+ * Add/Update drawn annotations to the server.
277
+ */
278
+ export const addUserAnnotationWithFeature = (service, userToken, zincObject, region, group, scaffoldUrl, action) => {
279
+ if (service && service.currentUser) {
280
+ const annotation = createNewAnnotationsWithFeatures(zincObject, region, group, scaffoldUrl, action);
281
+ console.log(annotation)
282
+ if (annotation) {
283
+ annotation.creator = {...service.currentUser};
284
+ if (!annotation.creator.orcid) annotation.creator.orcid = '0000-0000-0000-0000';
285
+ service.addAnnotation(userToken, annotation)
286
+ .then((response) => {
287
+ if (!response.annotationId) {
288
+ console.log('There is a problem with the submission, please try again later');
289
+ }
290
+ })
291
+ .catch(() => {
292
+ console.log('There is a problem with the submission, please try again later');
293
+ })
294
+ }
295
+ }
296
+ }
297
+
298
+ /*
299
+ * Get the drawn annotation stored on the annotation server
300
+ */
301
+ export const getDrawnAnnotations = async (service, userToken, scaffoldUrl) => {
302
+ const resource = encodeURIComponent(scaffoldUrl);
303
+ return await service.drawnFeatures(userToken, resource);
304
+ }
305
+
306
+ /*
307
+ * Convert features store in annotation server into primitives
308
+ */
309
+ export const annotationFeaturesToPrimitives = (scene, features) => {
310
+ if (scene) {
311
+ features.forEach((feature) => {
312
+ console.log(feature)
313
+ const geometry = feature.geometry;
314
+ const regionGroup = decodeURIComponent(feature.id);
315
+ const last = regionGroup.lastIndexOf('/');
316
+ const region = regionGroup.substring(0, last);
317
+ const group = regionGroup.substring(last + 1);
318
+ let object = undefined;
319
+ if (geometry.type === "MultiPoint") {
320
+ object = scene.createPoints(
321
+ region,
322
+ group,
323
+ geometry.coordinates,
324
+ undefined,
325
+ 0x0022ee,
326
+ );
327
+ } else if (geometry.type === "MultiLineString") {
328
+ object = scene.createLines(
329
+ region,
330
+ group,
331
+ geometry.coordinates,
332
+ 0x00ee22,
333
+ );
334
+ }
335
+ if (object) object.zincObject.isEditable = true;
336
+ });
337
+ }
338
+ }