Skip to content

Commit

Permalink
Change protractor api
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmartel committed Jul 18, 2024
1 parent da87501 commit b181fa2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 62 deletions.
7 changes: 3 additions & 4 deletions src/dicom/dicomSpatialCoordinate.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,10 @@ export function getScoordFromShape(shape) {
];
scoord.graphicType = GraphicTypes.polyline;
} else if (shape instanceof Protractor) {
const pointList = shape.getPointList();
scoord.graphicData = [];
for (const point of pointList) {
scoord.graphicData.push(point.getX().toString());
scoord.graphicData.push(point.getY().toString());
for (let i = 0; i < 3; ++i) {
scoord.graphicData.push(shape.getPoint(i).getX().toString());
scoord.graphicData.push(shape.getPoint(i).getY().toString());
}
scoord.graphicType = GraphicTypes.polyline;
} else if (shape instanceof Circle) {
Expand Down
29 changes: 10 additions & 19 deletions src/math/protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,17 @@ export class Protractor {
*
* @type {Point2D[]}
*/
#pointArray;
#points;

/**
* @param {Point2D[]} pointArray The list of Point2D that make
* @param {Point2D[]} points The list of Point2D that make
* the protractor.
*/
constructor(pointArray) {
if (pointArray.length > 3) {
constructor(points) {
if (points.length > 3) {
throw new Error('Too many points for a protractor');
}
this.#pointArray = pointArray.slice(0, 3);
}

/**
* Get the point list.
*
* @returns {Point2D[]} The list.
*/
getPointList() {
return this.#pointArray;
this.#points = points.slice(0, 3);
}

/**
Expand All @@ -47,7 +38,7 @@ export class Protractor {
* @returns {Point2D|undefined} The Point2D at the given index.
*/
getPoint(index) {
return this.#pointArray[index];
return this.#points[index];
}

/**
Expand All @@ -56,7 +47,7 @@ export class Protractor {
* @returns {number} The length of the path.
*/
getLength() {
return this.#pointArray.length;
return this.#points.length;
}

/**
Expand All @@ -68,9 +59,9 @@ export class Protractor {
*/
quantify(_viewController, _flags) {
const quant = {};
if (this.#pointArray.length === 3) {
const line0 = new Line(this.#pointArray[0], this.#pointArray[1]);
const line1 = new Line(this.#pointArray[1], this.#pointArray[2]);
if (this.#points.length === 3) {
const line0 = new Line(this.#points[0], this.#points[1]);
const line1 = new Line(this.#points[1], this.#points[2]);
let angle = getAngle(line0, line1);
if (angle > 180) {
angle = 360 - angle;
Expand Down
85 changes: 46 additions & 39 deletions src/tools/protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class ProtractorFactory {
* @returns {Konva.Group} The Konva group.
*/
createShapeGroup(annotation, style) {
const path = annotation.mathShape;
const protractor = annotation.mathShape;

// konva group
const group = new Konva.Group();
Expand All @@ -84,7 +84,7 @@ export class ProtractorFactory {
// konva shape
group.add(this.#createShape(annotation, style));

if (path.getLength() === this.getNPoints()) {
if (protractor.getLength() === this.getNPoints()) {
// extras
const extras = this.#createShapeExtras(annotation, style);
for (const extra of extras) {
Expand Down Expand Up @@ -219,13 +219,12 @@ export class ProtractorFactory {
*/
updateAnnotationOnTranslation(annotation, translation) {
// math shape
const path = annotation.mathShape;
const pointList = path.getPointList();
const protractor = annotation.mathShape;
const newPointList = [];
for (const point of pointList) {
for (let i = 0; i < 3; ++i) {
newPointList.push(new Point2D(
point.getX() + translation.x,
point.getY() + translation.y
protractor.getPoint(i).getX() + translation.x,
protractor.getPoint(i).getY() + translation.y
));
}
annotation.mathShape = new Protractor(newPointList);
Expand Down Expand Up @@ -315,12 +314,11 @@ export class ProtractorFactory {
* @returns {Konva.Line} The konva shape.
*/
#createShape(annotation, style) {
const path = annotation.mathShape;
const pointList = path.getPointList();
const protractor = annotation.mathShape;
const points = [];
for (const point of pointList) {
points.push(point.getX());
points.push(point.getY());
for (let i = 0; i < 3; ++i) {
points.push(protractor.getPoint(i).getX());
points.push(protractor.getPoint(i).getY());
}

// konva line
Expand All @@ -332,13 +330,16 @@ export class ProtractorFactory {
name: 'shape'
});

if (path.getLength() === this.getNPoints()) {
if (protractor.getLength() === this.getNPoints()) {
// larger hitfunc
kshape.hitFunc(function (context) {
context.beginPath();
context.moveTo(pointList[0].getX(), pointList[0].getY());
context.lineTo(pointList[1].getX(), pointList[1].getY());
context.lineTo(pointList[2].getX(), pointList[2].getY());
context.moveTo(
protractor.getPoint(0).getX(), protractor.getPoint(0).getY());
context.lineTo(
protractor.getPoint(1).getX(), protractor.getPoint(1).getY());
context.lineTo(
protractor.getPoint(2).getX(), protractor.getPoint(2).getY());
context.closePath();
context.fillStrokeShape(kshape);
});
Expand All @@ -355,10 +356,11 @@ export class ProtractorFactory {
* @returns {Array} The konva shape extras.
*/
#createShapeExtras(annotation, style) {
const path = annotation.mathShape;
const pointList = path.getPointList();
const line0 = new Line(pointList[0], pointList[1]);
const line1 = new Line(pointList[1], pointList[2]);
const protractor = annotation.mathShape;
const line0 = new Line(
protractor.getPoint(0), protractor.getPoint(1));
const line1 = new Line(
protractor.getPoint(1), protractor.getPoint(2));

let angle = getAngle(line0, line1);
let inclination = line0.getInclination();
Expand All @@ -376,8 +378,8 @@ export class ProtractorFactory {
strokeScaleEnabled: false,
angle: angle,
rotation: -inclination,
x: pointList[1].getX(),
y: pointList[1].getY(),
x: protractor.getPoint(1).getX(),
y: protractor.getPoint(1).getY(),
name: 'shape-arc'
});

Expand All @@ -391,10 +393,11 @@ export class ProtractorFactory {
* @returns {Point2D} The position.
*/
#getDefaultLabelPosition(annotation) {
const path = annotation.mathShape;
const pointList = path.getPointList();
const line0 = new Line(pointList[0], pointList[1]);
const line1 = new Line(pointList[1], pointList[2]);
const protractor = annotation.mathShape;
const line0 = new Line(
protractor.getPoint(0), protractor.getPoint(1));
const line1 = new Line(
protractor.getPoint(1), protractor.getPoint(2));

const midX =
(line0.getMidpoint().getX() + line1.getMidpoint().getX()) / 2;
Expand Down Expand Up @@ -469,10 +472,11 @@ export class ProtractorFactory {
* @param {Style} _style The application style.
*/
#updateShape(annotation, anchor, _style) {
const path = annotation.mathShape;
const pointList = path.getPointList();
const line0 = new Line(pointList[0], pointList[1]);
const line1 = new Line(pointList[1], pointList[2]);
const protractor = annotation.mathShape;
const line0 = new Line(
protractor.getPoint(0), protractor.getPoint(1));
const line1 = new Line(
protractor.getPoint(1), protractor.getPoint(2));

// parent group
const group = anchor.getParent();
Expand All @@ -491,12 +495,12 @@ export class ProtractorFactory {
kline.position({x: 0, y: 0});
// update shape
kline.points([
pointList[0].getX(),
pointList[0].getY(),
pointList[1].getX(),
pointList[1].getY(),
pointList[2].getX(),
pointList[2].getY()
protractor.getPoint(0).getX(),
protractor.getPoint(0).getY(),
protractor.getPoint(1).getX(),
protractor.getPoint(1).getY(),
protractor.getPoint(2).getX(),
protractor.getPoint(2).getY()
]);

// associated arc
Expand Down Expand Up @@ -554,9 +558,12 @@ export class ProtractorFactory {
// larger hitfunc
kline.hitFunc(function (context) {
context.beginPath();
context.moveTo(pointList[0].getX(), pointList[0].getY());
context.lineTo(pointList[1].getX(), pointList[1].getY());
context.lineTo(pointList[2].getX(), pointList[2].getY());
context.moveTo(
protractor.getPoint(0).getX(), protractor.getPoint(0).getY());
context.lineTo(
protractor.getPoint(1).getX(), protractor.getPoint(1).getY());
context.lineTo(
protractor.getPoint(2).getX(), protractor.getPoint(2).getY());
context.closePath();
context.fillStrokeShape(kline);
});
Expand Down

0 comments on commit b181fa2

Please sign in to comment.