@immugio/three-math-extensions 0.0.3 → 0.0.4

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.
@@ -0,0 +1,28 @@
1
+ name: Build
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ pull_request:
7
+ branches: [ master ]
8
+
9
+ jobs:
10
+ build:
11
+
12
+ runs-on: ubuntu-latest
13
+
14
+ strategy:
15
+ matrix:
16
+ node-version: [16.x]
17
+
18
+ steps:
19
+ - uses: actions/checkout@v3
20
+
21
+ - name: Use Node.js ${{ matrix.node-version }}
22
+ uses: actions/setup-node@v3
23
+
24
+ with:
25
+ node-version: ${{ matrix.node-version }}
26
+ - run: npm ci
27
+ - run: npm run build --if-present
28
+ - run: npm test
@@ -0,0 +1,51 @@
1
+ name: Publish to NPM
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - '[0-9]+.[0-9]+.[0-9]+'
7
+ jobs:
8
+ build:
9
+ runs-on: ubuntu-latest
10
+
11
+ strategy:
12
+ matrix:
13
+ node-version: [16.x]
14
+
15
+ steps:
16
+ - uses: actions/checkout@v2
17
+ - name: Use Node.js ${{ matrix.node-version }}
18
+ uses: actions/setup-node@v1
19
+ with:
20
+ node-version: ${{ matrix.node-version }}
21
+ - uses: actions/cache@v2
22
+ with:
23
+ path: node_modules
24
+ key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
25
+ restore-keys: |
26
+ ${{ runner.os }}-node-
27
+
28
+ - name: Install Dependencies
29
+ run: npm install
30
+
31
+ - name: Building
32
+ run: npm run build
33
+
34
+ - name: Publish to NPM
35
+ run: |
36
+ npm config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN
37
+ npm publish
38
+ env:
39
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
40
+ - name: Package lib
41
+ run: npm pack
42
+
43
+ - name: Release
44
+ uses: docker://antonyurchenko/git-release:v3
45
+ env:
46
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
47
+ ALLOW_EMPTY_CHANGELOG: "false"
48
+ ALLOW_TAG_PREFIX: "true"
49
+ with:
50
+ args: |
51
+ three-math-extensions-*.tgz
package/CHANGELOG.md ADDED
@@ -0,0 +1,34 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
9
+
10
+ ## 0.0.4 - 2022-11-21
11
+
12
+ ### Commits
13
+
14
+ - Automatic publish with change log [`a70c99d`](https://github.com/Immugio/three-math-extensions/commit/a70c99d0d42e60411ce01bcdb8dd4f3f489bb949)
15
+ - Switch to npm [`ed47a17`](https://github.com/Immugio/three-math-extensions/commit/ed47a1787522db3bde5a2112bfd02480fe2edc54)
16
+ - Initial commit [`3d1bf9e`](https://github.com/Immugio/three-math-extensions/commit/3d1bf9ef015570830007c9be99140c8c8d760d7f)
17
+ - Add Line2D [`9a1dd0f`](https://github.com/Immugio/three-math-extensions/commit/9a1dd0f58352b7b25828693c688aa4770e95c174)
18
+ - Readme, npm info [`dda4a28`](https://github.com/Immugio/three-math-extensions/commit/dda4a282a71a00308dcae858ffe53d67d4185be8)
19
+ - Run tests in CI on commit [`e3f77ca`](https://github.com/Immugio/three-math-extensions/commit/e3f77ca76e25f4d99eef0130e5779b5e7c5aec6b)
20
+ - Remove babel.config.js [`c9a1e16`](https://github.com/Immugio/three-math-extensions/commit/c9a1e1607cffec8a3d74adcca644320a79ca4c43)
21
+ - Initial changelog [`6c98aa6`](https://github.com/Immugio/three-math-extensions/commit/6c98aa6ad631cf5e73d32dd76276f99b3ba5089f)
22
+ - Release 0.0.4 [`bdac840`](https://github.com/Immugio/three-math-extensions/commit/bdac840f33261a3f39ef33bc05c80772169cfe87)
23
+ - Add test badge [`37c62e8`](https://github.com/Immugio/three-math-extensions/commit/37c62e809bdcdfc63f7fe135469fd131f190f950)
24
+ - Edit publish script condition [`dc77d89`](https://github.com/Immugio/three-math-extensions/commit/dc77d8978d87daab8732d6429ad1b01ba0ca6f97)
25
+ - CI to use node version 16 [`dc42650`](https://github.com/Immugio/three-math-extensions/commit/dc426508939de922cc0316b7652bbd09aeed4610)
26
+ - Action rename [`26a1f01`](https://github.com/Immugio/three-math-extensions/commit/26a1f014827faab2c02d30ca8ed18dac8f9ff8af)
27
+ - Release 0.0.4 - tag pattern 4 [`ab9ad4e`](https://github.com/Immugio/three-math-extensions/commit/ab9ad4e49477a991482ed6d4d3a3ca4687a70b44)
28
+ - Release 0.0.4 - tag pattern 3 [`d7d4c8f`](https://github.com/Immugio/three-math-extensions/commit/d7d4c8f546e6550868ddd06a5213fdec6c68bcd2)
29
+ - Release 0.0.4 - tag pattern 2 [`0dcf801`](https://github.com/Immugio/three-math-extensions/commit/0dcf80190bd67a3ca93d45131a403201ab39e00f)
30
+ - Release 0.0.4 - tag pattern [`e7f2f76`](https://github.com/Immugio/three-math-extensions/commit/e7f2f76aa247e1c51f0522c300a7aaf07253039c)
31
+ - Release 0.0.5 [`c80d6bd`](https://github.com/Immugio/three-math-extensions/commit/c80d6bd12c25ddad0f67610cdf5c804d5759f084)
32
+ - Status badge update [`370843e`](https://github.com/Immugio/three-math-extensions/commit/370843ed0a28b024761555572b7d51ccbdb4b77d)
33
+ - Run tests in CI on commit [`aee8c55`](https://github.com/Immugio/three-math-extensions/commit/aee8c556ae1cab9025dbe0984dd17278fe6275a1)
34
+ - Add Line2D to index [`a5fb6bd`](https://github.com/Immugio/three-math-extensions/commit/a5fb6bdeee5d9f07f325bfffc31ef96f0ce167d1)
package/README.md CHANGED
@@ -1 +1,3 @@
1
- Set of utilities for 2d and 3d line math built on top of three.js
1
+ # Set of utilities for 2d and 3d line math built on top of three.js
2
+
3
+ [![Build](https://github.com/Immugio/three-math-extensions/actions/workflows/build.yml/badge.svg)](https://github.com/Immugio/three-math-extensions/actions/workflows/build.yml)
package/cjs/Line2D.js CHANGED
@@ -18,14 +18,23 @@ class Line2D {
18
18
  static fromPoints(p1, p2, index = 0) {
19
19
  return new Line2D(new Vec2_1.Vec2(p1.x, p1.y), new Vec2_1.Vec2(p2.x, p2.y), index);
20
20
  }
21
- static fromPolygon(polygon) {
22
- if (polygon[0].x === polygon[polygon.length - 1].x && polygon[0].y === polygon[polygon.length - 1].y) {
23
- polygon = polygon.slice(0, polygon.length - 1);
21
+ /**
22
+ * Creates a polygon formed by an array of lines from points provided.
23
+ * The polygon will only be closed if either
24
+ * 1) the first and last points are the same or 2) `forceClosedPolygon` is true.
25
+ */
26
+ static fromPolygon(polygon, forceClosedPolygon = false) {
27
+ if (!polygon || polygon.length < 2) {
28
+ return [];
24
29
  }
25
- return polygon.map((p, i) => {
26
- const next = polygon[(i + 1) % polygon.length];
27
- return Line2D.fromPoints(p, next, i);
28
- });
30
+ if (forceClosedPolygon && (polygon[0].x !== polygon.at(-1).x || polygon[0].y !== polygon.at(-1).y)) {
31
+ polygon = [...polygon, polygon[0]];
32
+ }
33
+ const lines = [];
34
+ for (let i = 0; i < polygon.length - 1; i++) {
35
+ lines.push(Line2D.fromPoints(polygon[i], polygon[i + 1], i));
36
+ }
37
+ return lines;
29
38
  }
30
39
  static fromLength(length) {
31
40
  return Line2D.fromCoordinates(-length / 2, 0, length / 2, 0);
@@ -101,6 +110,14 @@ class Line2D {
101
110
  get length() {
102
111
  return this.start.distanceTo(this.end);
103
112
  }
113
+ /**
114
+ * Set the length of this line. Center and direction remain unchanged.
115
+ * @param length
116
+ */
117
+ setLength(length) {
118
+ this.length = length;
119
+ return this;
120
+ }
104
121
  /**
105
122
  * Returns the start and end points of the line as an array.
106
123
  * Endpoints are not cloned.
package/cjs/Line3D.js CHANGED
@@ -13,6 +13,24 @@ class Line3D extends three_1.Line3 {
13
13
  static fromPoints(start, end) {
14
14
  return new Line3D(new Vec3_1.Vec3(start.x, start.y, start.z), new Vec3_1.Vec3(end.x, end.y, end.z));
15
15
  }
16
+ /**
17
+ * Creates a polygon formed by an array of lines from points provided.
18
+ * The polygon will only be closed if either
19
+ * 1) the first and last points are the same or 2) `forceClosedPolygon` is true.
20
+ */
21
+ static fromPolygon(polygon, forceClosedPolygon = false) {
22
+ if (!polygon || polygon.length < 2) {
23
+ return [];
24
+ }
25
+ if (forceClosedPolygon && (polygon[0].x !== polygon.at(-1).x || polygon[0].y !== polygon.at(-1).y || polygon[0].z !== polygon.at(-1).z)) {
26
+ polygon = [...polygon, polygon[0]];
27
+ }
28
+ const lines = [];
29
+ for (let i = 0; i < polygon.length - 1; i++) {
30
+ lines.push(Line3D.fromPoints(polygon[i], polygon[i + 1]));
31
+ }
32
+ return lines;
33
+ }
16
34
  /**
17
35
  * Returns lines that are the result of clipping this line by the @other line.
18
36
  * Clips must be parallel to this line.
package/cjs/Vec2.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Vec2 = void 0;
4
4
  const three_1 = require("three");
5
+ const Vec3_1 = require("./Vec3");
5
6
  class Vec2 extends three_1.Vector2 {
6
7
  static fromPoint(point) {
7
8
  return new Vec2(point.x, point.y);
@@ -15,5 +16,8 @@ class Vec2 extends three_1.Vector2 {
15
16
  }
16
17
  return this;
17
18
  }
19
+ in3DSpace(z = 0) {
20
+ return new Vec3_1.Vec3(this.x, z, this.y);
21
+ }
18
22
  }
19
23
  exports.Vec2 = Vec2;
package/package.json CHANGED
@@ -1,29 +1,46 @@
1
1
  {
2
2
  "name": "@immugio/three-math-extensions",
3
- "version": "0.0.3",
4
- "description": "Set of utilities for 2d and 3d line math built on top of threejs",
5
- "author": "Jan Mikeska",
3
+ "version": "0.0.4",
4
+ "description": "Set of utilities for 2d and 3d line math built on top of three.js",
5
+ "author": "Jan Mikeska <janmikeska@gmail.com>",
6
6
  "license": "ISC",
7
+ "keywords": [
8
+ "threejs",
9
+ "three",
10
+ "math"
11
+ ],
12
+ "bugs": {
13
+ "url": "https://github.com/Immugio/three-math-extensions/issues"
14
+ },
15
+ "homepage": "https://github.com/Immugio/three-math-extensions#readme",
7
16
  "sideEffects": false,
8
17
  "source": "src/index.ts",
9
18
  "main": "cjs/index.js",
10
19
  "module": "esm/index.js",
11
20
  "types": "types/index.d.ts",
21
+ "auto-changelog": {
22
+ "commitLimit": false,
23
+ "template": "keepachangelog"
24
+ },
12
25
  "scripts": {
13
26
  "test": "npx jest",
14
27
  "build:esm": "tsc",
15
28
  "build:cjs": "tsc -p tsconfig-cjs.json",
16
29
  "clean": "rimraf types cjs esm",
17
- "build": "npm run clean && npm run build:esm && npm run build:cjs"
30
+ "build": "npm run clean && npm run build:esm && npm run build:cjs",
31
+ "preversion": "npm run clean && npm run build && npm run test",
32
+ "version": "auto-changelog -p && git add CHANGELOG.md",
33
+ "postversion": "git push && git push --tags"
18
34
  },
19
35
  "devDependencies": {
20
36
  "@types/jest": "^29.2.1",
21
- "@types/three": "0.130.1",
22
37
  "@types/offscreencanvas": "2019.7.0",
38
+ "@types/three": "0.130.1",
39
+ "auto-changelog": "^2.4.0",
23
40
  "jest": "^29.2.2",
41
+ "rimraf": "^3.0.2",
24
42
  "ts-jest": "^29.0.0",
25
- "typescript": "4.7.4",
26
- "rimraf": "^3.0.2"
43
+ "typescript": "4.7.4"
27
44
  },
28
45
  "peerDependencies": {
29
46
  "three": "^0.130.1"
@@ -31,14 +48,5 @@
31
48
  "repository": {
32
49
  "type": "git",
33
50
  "url": "git+https://github.com/Immugio/three-math-extensions.git"
34
- },
35
- "keywords": [
36
- "threejs",
37
- "three",
38
- "math"
39
- ],
40
- "bugs": {
41
- "url": "https://github.com/Immugio/three-math-extensions/issues"
42
- },
43
- "homepage": "https://github.com/Immugio/three-math-extensions#readme"
51
+ }
44
52
  }
package/src/Line2D.ts CHANGED
@@ -15,15 +15,26 @@ export class Line2D {
15
15
  return new Line2D(new Vec2(p1.x, p1.y), new Vec2(p2.x, p2.y), index);
16
16
  }
17
17
 
18
- public static fromPolygon(polygon: Point2[]): Line2D[] {
19
- if (polygon[0].x === polygon[polygon.length - 1].x && polygon[0].y === polygon[polygon.length - 1].y) {
20
- polygon = polygon.slice(0, polygon.length - 1);
18
+ /**
19
+ * Creates a polygon formed by an array of lines from points provided.
20
+ * The polygon will only be closed if either
21
+ * 1) the first and last points are the same or 2) `forceClosedPolygon` is true.
22
+ */
23
+ public static fromPolygon(polygon: Point2[], forceClosedPolygon: boolean = false): Line2D[] {
24
+ if (!polygon || polygon.length < 2) {
25
+ return [];
21
26
  }
22
27
 
23
- return polygon.map((p, i) => {
24
- const next = polygon[(i + 1) % polygon.length];
25
- return Line2D.fromPoints(p, next, i);
26
- });
28
+ if (forceClosedPolygon && (polygon[0].x !== polygon.at(-1).x || polygon[0].y !== polygon.at(-1).y)) {
29
+ polygon = [...polygon, polygon[0]];
30
+ }
31
+
32
+ const lines: Line2D[] = [];
33
+ for (let i = 0; i < polygon.length - 1; i++) {
34
+ lines.push(Line2D.fromPoints(polygon[i], polygon[i + 1], i));
35
+ }
36
+
37
+ return lines;
27
38
  }
28
39
 
29
40
  public static fromLength(length: number): Line2D {
@@ -113,6 +124,15 @@ export class Line2D {
113
124
  return this.start.distanceTo(this.end);
114
125
  }
115
126
 
127
+ /**
128
+ * Set the length of this line. Center and direction remain unchanged.
129
+ * @param length
130
+ */
131
+ public setLength(length: number): this {
132
+ this.length = length;
133
+ return this;
134
+ }
135
+
116
136
  /**
117
137
  * Returns the start and end points of the line as an array.
118
138
  * Endpoints are not cloned.
package/src/Line3D.ts CHANGED
@@ -19,6 +19,28 @@ export class Line3D extends Line3 {
19
19
  return new Line3D(new Vec3(start.x, start.y, start.z), new Vec3(end.x, end.y, end.z));
20
20
  }
21
21
 
22
+ /**
23
+ * Creates a polygon formed by an array of lines from points provided.
24
+ * The polygon will only be closed if either
25
+ * 1) the first and last points are the same or 2) `forceClosedPolygon` is true.
26
+ */
27
+ public static fromPolygon(polygon: Point3[], forceClosedPolygon: boolean = false): Line3D[] {
28
+ if (!polygon || polygon.length < 2) {
29
+ return [];
30
+ }
31
+
32
+ if (forceClosedPolygon && (polygon[0].x !== polygon.at(-1).x || polygon[0].y !== polygon.at(-1).y || polygon[0].z !== polygon.at(-1).z)) {
33
+ polygon = [...polygon, polygon[0]];
34
+ }
35
+
36
+ const lines: Line3D[] = [];
37
+ for (let i = 0; i < polygon.length - 1; i++) {
38
+ lines.push(Line3D.fromPoints(polygon[i], polygon[i + 1]));
39
+ }
40
+
41
+ return lines;
42
+ }
43
+
22
44
  /**
23
45
  * Returns lines that are the result of clipping this line by the @other line.
24
46
  * Clips must be parallel to this line.
package/src/Vec2.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  import { Vector2 } from "three";
2
+ import { Vec3 } from "./Vec3";
3
+ import { Point2 } from "./Point2";
2
4
 
3
5
  export class Vec2 extends Vector2 {
4
6
 
5
- public static fromPoint(point: { x: number, y: number }): Vec2 {
7
+ public static fromPoint(point: Point2): Vec2 {
6
8
  return new Vec2(point.x, point.y);
7
9
  }
8
10
 
@@ -15,4 +17,8 @@ export class Vec2 extends Vector2 {
15
17
  }
16
18
  return this;
17
19
  }
20
+
21
+ public in3DSpace(z: number = 0): Vec3 {
22
+ return new Vec3(this.x, z, this.y);
23
+ }
18
24
  }
@@ -3,7 +3,7 @@ import { Vector2 } from "three";
3
3
  import { Vec2 } from "../Vec2";
4
4
  import { Point2 } from "../Point2";
5
5
 
6
- describe("Line", () => {
6
+ describe("Line2D", () => {
7
7
  it("should be created", () => {
8
8
  const start = new Vec2(1, 2);
9
9
  const end = new Vec2(3, 4);
@@ -27,6 +27,35 @@ describe("Line", () => {
27
27
  expect(line).toEqual(new Line2D(new Vec2(1, 2), new Vec2(3, 4), 10));
28
28
  });
29
29
 
30
+ it("should create a single line polygon from a 2 points polygon", () => {
31
+ const start = new Vec2(1, 2);
32
+ const end = new Vec2(3, 4);
33
+ const lines = Line2D.fromPolygon([start, end]);
34
+ expect(lines.length).toEqual(1);
35
+ expect(lines[0]).toEqual(new Line2D(start, end));
36
+ });
37
+
38
+ it("should create a 2 lines polygon from a 3 points polygon", () => {
39
+ const p1 = new Vec2(1, 2);
40
+ const p2 = new Vec2(3, 4);
41
+ const p3 = new Vec2(5, 2);
42
+ const lines = Line2D.fromPolygon([p1, p2, p3]);
43
+ expect(lines.length).toEqual(2);
44
+ expect(lines[0]).toEqual(new Line2D(p1, p2, 0));
45
+ expect(lines[1]).toEqual(new Line2D(p2, p3, 1));
46
+ });
47
+
48
+ it("should create a closed 3 lines polygon from a 3 points polygon", () => {
49
+ const p1 = new Vec2(1, 2);
50
+ const p2 = new Vec2(3, 4);
51
+ const p3 = new Vec2(5, 2);
52
+ const lines = Line2D.fromPolygon([p1, p2, p3], true);
53
+ expect(lines.length).toEqual(3);
54
+ expect(lines[0]).toEqual(new Line2D(p1, p2, 0));
55
+ expect(lines[1]).toEqual(new Line2D(p2, p3, 1));
56
+ expect(lines[2]).toEqual(new Line2D(p3, p1, 2));
57
+ });
58
+
30
59
  test("center should return the center of the line", () => {
31
60
  const line = Line2D.fromCoordinates(-2, -2, 2, 2);
32
61
  expect(line.center.x).toEqual(0);
@@ -10,6 +10,35 @@ describe("Line3d", () => {
10
10
  expect(line.end).toEqual(new Vec3(4, 5, 6));
11
11
  });
12
12
 
13
+ it("should create a single line polygon from a 2 points polygon", () => {
14
+ const start = new Vec3(1, 2, 20);
15
+ const end = new Vec3(3, 4, 20);
16
+ const lines = Line3D.fromPolygon([start, end]);
17
+ expect(lines.length).toEqual(1);
18
+ expect(lines[0]).toEqual(new Line3D(start, end));
19
+ });
20
+
21
+ it("should create a 2 lines polygon from a 3 points polygon", () => {
22
+ const p1 = new Vec3(1, 2, 20);
23
+ const p2 = new Vec3(3, 4, 20);
24
+ const p3 = new Vec3(5, 2, 20);
25
+ const lines = Line3D.fromPolygon([p1, p2, p3]);
26
+ expect(lines.length).toEqual(2);
27
+ expect(lines[0]).toEqual(new Line3D(p1, p2));
28
+ expect(lines[1]).toEqual(new Line3D(p2, p3));
29
+ });
30
+
31
+ it("should create a closed 3 lines polygon from a 3 points polygon", () => {
32
+ const p1 = new Vec3(1, 2, 20);
33
+ const p2 = new Vec3(3, 4, 20);
34
+ const p3 = new Vec3(5, 2, 20);
35
+ const lines = Line3D.fromPolygon([p1, p2, p3], true);
36
+ expect(lines.length).toEqual(3);
37
+ expect(lines[0]).toEqual(new Line3D(p1, p2));
38
+ expect(lines[1]).toEqual(new Line3D(p2, p3));
39
+ expect(lines[2]).toEqual(new Line3D(p3, p1));
40
+ });
41
+
13
42
  it("should return the expected center", () => {
14
43
  const line = defaultLine();
15
44
  expect(line.center).toEqual(new Vec3(0, 0, 0));