Skip to content

Commit fe4dc8e

Browse files
authored
Update to v0.2.0 (#37)
2 parents 704ddd0 + 7a74731 commit fe4dc8e

File tree

7 files changed

+430
-42
lines changed

7 files changed

+430
-42
lines changed

package-lock.json

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "levelz-js",
3-
"version": "0.1.2",
3+
"version": "0.2.0",
44
"description": "JavaScript Bindings for the LevelZ File Format",
55
"main": "index.js",
66
"scripts": {

src/coordinate.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ export class Coordinate2D extends Coordinate {
114114
* @static
115115
* @param {string} str The string to convert
116116
* @returns {Coordinate2D} The 2D Coordinate
117+
* @throws {SyntaxError} If the string is invalid.
118+
* @example
119+
* Coordinate2D.fromString('[1, 2]')
117120
*/
118121
static fromString(str) {
119122
if (!str || typeof(str) !== 'string')
@@ -204,6 +207,8 @@ export class Coordinate3D extends Coordinate {
204207
* @static
205208
* @param {string} str The string to convert
206209
* @returns {Coordinate3D} The 3D Coordinate
210+
* @example
211+
* Coordinate3D.fromString('[1, 2, 3]')
207212
*/
208213
static fromString(str) {
209214
if (!str || typeof(str) !== 'string')

src/matrix.js

Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
import { Dimension, Coordinate, Coordinate2D, Coordinate3D } from './coordinate.js';
2+
3+
/**
4+
* Represents a coordinate matrix.
5+
* @abstract
6+
* @classdesc Abstract Coordinate Matrix for representation purposes
7+
*/
8+
export class CoordinateMatrix {
9+
constructor() {
10+
if (new.target === CoordinateMatrix)
11+
throw new TypeError('Cannot instantiate abstract class')
12+
}
13+
14+
/**
15+
* Gets the iterator for this CoordinateMatrix.
16+
* @returns {Iterator<Coordinate>} The iterator for this CoordinateMatrix.
17+
*/
18+
[Symbol.iterator]() {
19+
return this.coordinates.values()
20+
}
21+
22+
/**
23+
* Returns the dimension of this CoordinateMatrix.
24+
* @returns {Dimension} The dimension of this CoordinateMatrix.
25+
*/
26+
get dimension() {
27+
throw new Error('Abstract method')
28+
}
29+
30+
/**
31+
* Returns the coordinates of this CoordinateMatrix.
32+
* @returns {Array<Coordinate>} The coordinates of this CoordinateMatrix.
33+
*/
34+
get coordinates() {
35+
throw new Error('Abstract method')
36+
}
37+
}
38+
39+
export class CoordinateMatrix2D extends CoordinateMatrix {
40+
41+
/**
42+
* The minimum x value of this matrix.
43+
* @type {number}
44+
*/
45+
minX;
46+
47+
/**
48+
* The maximum x value of this matrix.
49+
* @type {number}
50+
*/
51+
maxX;
52+
53+
/**
54+
* The minimum y value of this matrix.
55+
* @type {number}
56+
*/
57+
minY;
58+
59+
/**
60+
* The maximum y value of this matrix.
61+
* @type {number}
62+
*/
63+
maxY;
64+
65+
/**
66+
* The starting coordinate of this matrix.
67+
* @type {Coordinate2D}
68+
*/
69+
start;
70+
71+
/**
72+
* Constructs a CoordinateMatrix2D.
73+
* @constructor
74+
* @param {number} minX The minimum x value.
75+
* @param {number} maxX The maximum x value.
76+
* @param {number} minY The minimum y value.
77+
* @param {number} maxY The maximum y value.
78+
* @param {Coordinate2D} start The starting coordinate.
79+
* @example
80+
* new CoordinateMatrix2D(0, 1, 0, 1, new Coordinate2D(0, 0))
81+
*/
82+
constructor(minX, maxX, minY, maxY, start) {
83+
super();
84+
85+
if (minX > maxX) throw new SyntaxError(`minX cannot be greater than maxX: ${minX} > ${maxX}`);
86+
if (minY > maxY) throw new SyntaxError(`minY cannot be greater than maxY: ${minY} > ${maxY}`);
87+
88+
this.minX = minX;
89+
this.maxX = maxX;
90+
this.minY = minY;
91+
this.maxY = maxY;
92+
this.start = start;
93+
}
94+
95+
get dimension() {
96+
return Dimension.TWO;
97+
}
98+
99+
get coordinates() {
100+
const coords = [];
101+
102+
for (let x = this.minX; x <= this.maxX; x++) {
103+
for (let y = this.minY; y <= this.maxY; y++) {
104+
coords.push(new Coordinate2D(x, y));
105+
}
106+
}
107+
108+
return coords;
109+
}
110+
111+
/**
112+
* Gets the string representation of this CoordinateMatrix2D.
113+
* @returns {string} The string representation of this CoordinateMatrix2D.
114+
*/
115+
toString() {
116+
return `(${this.minX}, ${this.maxX}, ${this.minY}, ${this.maxY})^${this.start.toString()}`;
117+
}
118+
119+
// Statics
120+
121+
/**
122+
* Converts a string to a CoordinateMatrix2D.
123+
* @static
124+
* @param {string} str The string to convert.
125+
* @returns {CoordinateMatrix2D} The parsed CoordinateMatrix2D.
126+
* @throws {SyntaxError} If the string is invalid.
127+
* @example
128+
* CoordinateMatrix2D.fromString('(0, 1, 0, 1)^[0, 0]')
129+
*/
130+
static fromString(str) {
131+
const split = str.split(/\^/)
132+
if (split.length !== 2) throw new SyntaxError(`Invalid 3D matrix string: ${str}`)
133+
134+
const coords = split[1].replace(/[\[\]\s]/g, "").split(/,/)
135+
const matrix = split[0].replace(/[()\s]/g, "").split(/,/)
136+
137+
if (coords.length !== 2) throw new SyntaxError(`Invalid 2D point: ${str}`)
138+
if (matrix.length !== 4) throw new SyntaxError(`Invalid 2D matrix: ${str}`)
139+
140+
const cx = parseFloat(coords[0]), cy = parseFloat(coords[1])
141+
142+
const x1 = parseInt(matrix[0]), x2 = parseInt(matrix[1])
143+
const y1 = parseInt(matrix[2]), y2 = parseInt(matrix[3])
144+
145+
if (x1 > x2) throw new SyntaxError(`minX cannot be greater than maxX: ${str}`)
146+
if (y1 > y2) throw new SyntaxError(`minY cannot be greater than maxY: ${str}`)
147+
148+
return new CoordinateMatrix2D(x1, x2, y1, y2, new Coordinate2D(cx, cy))
149+
}
150+
151+
}
152+
153+
/**
154+
* Represents a 3D coordinate matrix.
155+
*/
156+
export class CoordinateMatrix3D extends CoordinateMatrix {
157+
158+
/**
159+
* The minimum x value of this matrix.
160+
* @type {number}
161+
*/
162+
minX;
163+
164+
/**
165+
* The maximum x value of this matrix.
166+
* @type {number}
167+
*/
168+
maxX;
169+
170+
/**
171+
* The minimum y value of this matrix.
172+
* @type {number}
173+
*/
174+
minY;
175+
176+
/**
177+
* The maximum y value of this matrix.
178+
* @type {number}
179+
*/
180+
maxY;
181+
182+
/**
183+
* The minimum z value of this matrix.
184+
* @type {number}
185+
*/
186+
minZ;
187+
188+
/**
189+
* The maximum z value of this matrix.
190+
* @type {number}
191+
*/
192+
maxZ;
193+
194+
/**
195+
* The starting coordinate of this matrix.
196+
* @type {Coordinate3D}
197+
*/
198+
start;
199+
200+
/**
201+
* Constructs a CoordinateMatrix3D.
202+
* @constructor
203+
* @param {number} minX The minimum x value.
204+
* @param {number} maxX The maximum x value.
205+
* @param {number} minY The minimum y value.
206+
* @param {number} maxY The maximum y value.
207+
* @param {number} minZ The minimum z value.
208+
* @param {number} maxZ The maximum z value.
209+
* @param {Coordinate3D} start The starting coordinate.
210+
* @example
211+
* new CoordinateMatrix3D(0, 1, 0, 1, 0, 1, new Coordinate3D(0, 0, 0))
212+
*/
213+
constructor(minX, maxX, minY, maxY, minZ, maxZ, start) {
214+
super();
215+
this.minX = minX;
216+
this.maxX = maxX;
217+
this.minY = minY;
218+
this.maxY = maxY;
219+
this.minZ = minZ;
220+
this.maxZ = maxZ;
221+
this.start = start;
222+
}
223+
224+
get dimension() {
225+
return Dimension.THREE;
226+
}
227+
228+
get coordinates() {
229+
const coords = [];
230+
231+
for (let x = this.minX; x <= this.maxX; x++) {
232+
for (let y = this.minY; y <= this.maxY; y++) {
233+
for (let z = this.minZ; z <= this.maxZ; z++) {
234+
coords.push(new Coordinate3D(x, y, z));
235+
}
236+
}
237+
}
238+
239+
return coords;
240+
}
241+
242+
/**
243+
* Gets the string representation of this CoordinateMatrix3D.
244+
* @returns {string} The string representation of this CoordinateMatrix3D.
245+
*/
246+
toString() {
247+
return `(${this.minX}, ${this.maxX}, ${this.minY}, ${this.maxY}, ${this.minZ}, ${this.maxZ})^${this.start.toString()}`;
248+
}
249+
250+
// Statics
251+
252+
/**
253+
* Converts a string to a CoordinateMatrix3D.
254+
* @static
255+
* @param {string} str The string to convert.
256+
* @returns {CoordinateMatrix3D} The parsed CoordinateMatrix3D.
257+
* @throws {SyntaxError} If the string is invalid.
258+
* @example
259+
* CoordinateMatrix3D.fromString('(0, 1, 0, 1, 0, 1)^[0, 0, 0]')
260+
*/
261+
static fromString(str) {
262+
const split = str.split(/\^/)
263+
if (split.length !== 2) throw new SyntaxError(`Invalid 3D matrix string: ${str}`)
264+
265+
const coords = split[1].replace(/[\[\]\s]/g, "").split(/,/)
266+
const matrix = split[0].replace(/[()\s]/g, "").split(/,/)
267+
268+
if (coords.length !== 3) throw new SyntaxError(`Invalid 2D point: ${str}`)
269+
if (matrix.length !== 6) throw new SyntaxError(`Invalid 2D matrix: ${str}`)
270+
271+
const cx = parseFloat(coords[0]), cy = parseFloat(coords[1]), cz = parseFloat(coords[2])
272+
273+
const x1 = parseInt(matrix[0]), x2 = parseInt(matrix[1])
274+
const y1 = parseInt(matrix[2]), y2 = parseInt(matrix[3])
275+
const z1 = parseInt(matrix[4]), z2 = parseInt(matrix[5])
276+
277+
if (x1 > x2) throw new SyntaxError(`minX cannot be greater than maxX: ${str}`)
278+
if (y1 > y2) throw new SyntaxError(`minY cannot be greater than maxY: ${str}`)
279+
if (z1 > z2) throw new SyntaxError(`minZ cannot be greater than maxZ: ${str}`)
280+
281+
return new CoordinateMatrix3D(x1, x2, y1, y2, z1, z2, new Coordinate3D(cx, cy, cz))
282+
}
283+
284+
}

0 commit comments

Comments
 (0)