Skip to content

Commit d370d8d

Browse files
committed
wip2
1 parent c95afba commit d370d8d

6 files changed

Lines changed: 29 additions & 20 deletions

File tree

src/__tests__/bounding-box.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ describe('BoundingBox', () => {
1010

1111
it('should calculate the correct center coordinates', () => {
1212
const bbox = new BoundingBox();
13-
bbox.update(0, 0, 0);
14-
bbox.update(10, 10, 10);
13+
bbox.update(new GCodeVector3(0, 0, 0));
14+
bbox.update(new GCodeVector3(10, 10, 10));
1515

1616
const center = bbox.center as GCodeVector3;
1717
expect(center).toBeInstanceOf(GCodeVector3);
1818
expect(center.x).toBe(5);
1919
expect(center.y).toBe(5);
2020
expect(center.z).toBe(5);
2121

22-
bbox.update(-5, -5, -5);
22+
bbox.update(new GCodeVector3(-5, -5, -5));
2323
const newCenter = bbox.center as GCodeVector3;
2424
expect(newCenter.x).toBe(2.5);
2525
expect(newCenter.y).toBe(2.5);

src/__tests__/job.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Job } from '../job';
33
import { PathType, Path } from '../path';
44
import { State } from '../state';
55
import { LayersIndexer } from '../indexers';
6+
import { GCodeVector3 } from '../types';
67

78
test('it has an initial state', () => {
89
const job = new Job();
@@ -367,7 +368,7 @@ describe('.finishPath', () => {
367368
const job = new Job();
368369
const path = new Path(PathType.Extrusion, 0.6, 0.2, 0);
369370

370-
path.addPoint(0, 0, 0);
371+
path.addPoint(new GCodeVector3(0, 0, 0));
371372

372373
job.inprogressPath = path;
373374
job.finishPath();
@@ -389,7 +390,7 @@ describe('.finishPath', () => {
389390
const job = new Job();
390391
const path = new Path(PathType.Extrusion, 0.6, 0.2, 0);
391392

392-
path.addPoint(0, 0, 0);
393+
path.addPoint(new GCodeVector3(0, 0, 0));
393394

394395
job.inprogressPath = path;
395396
job.finishPath();
@@ -431,7 +432,7 @@ describe('.resumeLastPath', () => {
431432

432433
function append_path(job: Job, travelType, points: [number, number, number][], tool: number = 0): Path {
433434
const path = new Path(travelType, 0.6, 0.2, tool || job.state.tool);
434-
points.forEach((point: [number, number, number]) => path.addPoint(...point));
435+
points.forEach((point: [number, number, number]) => path.addPoint(new GCodeVector3(point[0], point[1], point[2])));
435436
job.addPath(path);
436437
return path;
437438
}

src/bounding-box.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ export class BoundingBox {
99
* @param y - The Y coordinate.
1010
* @param z - The Z coordinate.
1111
*/
12-
public update(x: number, y: number, z: number): void {
13-
this.min.min(new GCodeVector3(x, y, z));
14-
this.max.max(new GCodeVector3(x, y, z));
12+
public update(point: GCodeVector3): void {
13+
this.min.min(point);
14+
this.max.max(point);
1515
}
1616

1717
/**

src/interpreter.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Path, PathType } from './path';
22
import { GCodeCommand } from './gcode-parser';
33
import { Job } from './job';
4+
import { GCodeVector3 } from './types';
45

56
// eslint-disable-next-line no-unused-vars
67
type Method = (...args: unknown[]) => unknown;
@@ -113,9 +114,9 @@ export class Interpreter {
113114
state.y = y ?? state.y;
114115
state.z = z ?? state.z;
115116

116-
currentPath.addPoint(state.x, state.y, state.z);
117+
currentPath.addPoint(state.toGCodeVector3());
117118
if (pathType === PathType.Extrusion) {
118-
job.boundingBox.update(state.x, state.y, state.z);
119+
job.boundingBox.update(state.toGCodeVector3());
119120
}
120121
}
121122

@@ -220,19 +221,19 @@ export class Interpreter {
220221
px = centerX + arcRadius * Math.cos(currentAngle);
221222
py = centerY + arcRadius * Math.sin(currentAngle);
222223
pz += zStep;
223-
currentPath.addPoint(px, py, pz);
224+
currentPath.addPoint(new GCodeVector3(px, py, pz));
224225
if (pathType === PathType.Extrusion) {
225-
job.boundingBox.update(px, py, pz);
226+
job.boundingBox.update(new GCodeVector3(px, py, pz));
226227
}
227228
}
228229

229230
state.x = x || state.x;
230231
state.y = y || state.y;
231232
state.z = z || state.z;
232233

233-
currentPath.addPoint(state.x, state.y, state.z);
234+
currentPath.addPoint(state.toGCodeVector3());
234235
if (pathType === PathType.Extrusion) {
235-
job.boundingBox.update(state.x, state.y, state.z);
236+
job.boundingBox.update(state.toGCodeVector3());
236237
}
237238
}
238239

@@ -371,7 +372,7 @@ export class Interpreter {
371372
private breakPath(job: Job, newType: PathType): Path {
372373
job.finishPath();
373374
const currentPath = new Path(newType, 0.6, 0.2, job.state.tool);
374-
currentPath.addPoint(job.state.x, job.state.y, job.state.z);
375+
currentPath.addPoint(job.state.toGCodeVector3());
375376
job.inprogressPath = currentPath;
376377
return currentPath;
377378
}

src/path.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { BufferGeometry, Vector3 } from 'three';
33
import { ExtrusionGeometry } from './extrusion-geometry';
44
import { LineSegmentsGeometry } from 'three/examples/jsm/lines/LineSegmentsGeometry.js';
5+
import { GCodeVector3 } from './types';
56

67
/**
78
* Type of path movement
@@ -64,8 +65,8 @@ export class Path {
6465
* @param y - Y coordinate
6566
* @param z - Z coordinate
6667
*/
67-
addPoint(x: number, y: number, z: number): void {
68-
this._vertices.push(x, y, z);
68+
addPoint(point: GCodeVector3): void {
69+
this._vertices.push(point.x, point.y, point.z);
6970
}
7071

7172
/**
@@ -75,7 +76,7 @@ export class Path {
7576
* @param z - Z coordinate to check
7677
* @returns True if the point matches the last point in the path
7778
*/
78-
checkLineContinuity(x: number, y: number, z: number): boolean {
79+
checkLineContinuity(point: GCodeVector3): boolean {
7980
if (this._vertices.length < 3) {
8081
return false;
8182
}
@@ -84,7 +85,7 @@ export class Path {
8485
const lastY = this._vertices[this._vertices.length - 2];
8586
const lastZ = this._vertices[this._vertices.length - 1];
8687

87-
return x === lastX && y === lastY && z === lastZ;
88+
return point.x === lastX && point.y === lastY && point.z === lastZ;
8889
}
8990

9091
/**

src/state.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GCodeVector3 } from './types';
2+
13
/**
24
* Represents the current state of the print job
35
* @remarks
@@ -26,4 +28,8 @@ export class State {
2628
Object.assign(state, { x: 0, y: 0, z: 0, e: 0, tool: 0, units: 'mm' });
2729
return state;
2830
}
31+
32+
public toGCodeVector3(): GCodeVector3 {
33+
return new GCodeVector3(this.x, this.y, this.z);
34+
}
2935
}

0 commit comments

Comments
 (0)