Bin
2025-12-17 d616898802dfe7e5dd648bcf53c6d1f86b6d3642
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
/* eslint-disable */
 
export function Livewire() {}
 
this.mousedown = (event) => {
  // first time
  if (!self.started) {
    self.started = true;
    self.x0 = event._x;
    self.y0 = event._y;
    // clear vars
    clearPaths();
    clearParentPoints();
    // do the training from the first point
    const p = new dwv.math.FastPoint2D(event._x, event._y);
    scissors.doTraining(p);
    // add the initial point to the path
    const p0 = new dwv.math.Point2D(event._x, event._y);
    path.addPoint(p0);
    path.addControlPoint(p0);
  } else {
    // final point: at 'tolerance' of the initial point
    if (Math.abs(event._x - self.x0) < tolerance && Math.abs(event._y - self.y0) < tolerance) {
      // draw
      self.mousemove(event);
      // listen
      command.onExecute = fireEvent;
      command.onUndo = fireEvent;
      // debug
      // save command in undo stack
      app.addToUndoStack(command);
      // set flag
      self.started = false;
    }
    // anchor point
    else {
      path = currentPath;
      clearParentPoints();
      const pn = new dwv.math.FastPoint2D(event._x, event._y);
      scissors.doTraining(pn);
      path.addControlPoint(currentPath.getPoint(0));
    }
  }
};
 
this.mousemove = (event) => {
  if (!self.started) {
    return;
  }
  // set the point to find the path to
  let p = new dwv.math.FastPoint2D(event._x, event._y);
  scissors.setPoint(p);
  // do the work
  let results = 0;
  let stop = false;
  while (!parentPoints[p.y][p.x] && !stop) {
    results = scissors.doWork();
 
    if (results.length === 0) {
      stop = true;
    } else {
      // fill parents
      for (let i = 0; i < results.length - 1; i += 2) {
        const _p = results[i];
        const _q = results[i + 1];
        parentPoints[_p.y][_p.x] = _q;
      }
    }
  }
 
  // get the path
  currentPath = new dwv.math.Path();
  stop = false;
  while (p && !stop) {
    currentPath.addPoint(new dwv.math.Point2D(p.x, p.y));
    if (!parentPoints[p.y]) {
      stop = true;
    } else {
      if (!parentPoints[p.y][p.x]) {
        stop = true;
      } else {
        p = parentPoints[p.y][p.x];
      }
    }
  }
  currentPath.appenPath(path);
 
  // remove previous draw
  if (shapeGroup) {
    shapeGroup.destroy();
  }
  // create shape
  const factory = new dwv.tool.RoiFactory();
  shapeGroup = factory.create(currentPath.pointArray, self.style);
  shapeGroup.id(dwv.math.guid());
 
  // get the position group
  const posGroup = app.getDrawController().getCurrentPosGroup();
  // add shape group to position group
  posGroup.add(shapeGroup);
 
  // draw shape command
  command = new dwv.tool.DrawGroupCommand(shapeGroup, "livewire", app.getDrawController().getDrawLayer());
  // draw
  command.execute();
};
 
this.dblclick = (/*event*/) => {
  // save command in undo stack
  app.addToUndoStack(command);
  // set flag
  self.started = false;
};