geometrix 0.5.10 → 0.5.11

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.
Files changed (90) hide show
  1. package/dist/index.d.ts +505 -0
  2. package/dist/index.js +3848 -0
  3. package/dist/index.js.map +1 -0
  4. package/package.json +6 -1
  5. package/.eslintignore +0 -17
  6. package/.eslintrc.cjs +0 -24
  7. package/.prettierignore +0 -15
  8. package/.prettierrc +0 -8
  9. package/src/aaExportContent.ts +0 -213
  10. package/src/aaExportFile.ts +0 -141
  11. package/src/aaParamGeom.ts +0 -62
  12. package/src/angle_utils.test.ts +0 -83
  13. package/src/angle_utils.ts +0 -98
  14. package/src/arc_to_stroke.ts +0 -73
  15. package/src/canvas_utils.test.ts +0 -28
  16. package/src/canvas_utils.ts +0 -159
  17. package/src/contour.test.ts +0 -37
  18. package/src/contour.ts +0 -806
  19. package/src/designParams.ts +0 -178
  20. package/src/figure.test.ts +0 -21
  21. package/src/figure.ts +0 -400
  22. package/src/index.ts +0 -16
  23. package/src/line.test.ts +0 -78
  24. package/src/line.ts +0 -359
  25. package/src/paramFile.ts +0 -52
  26. package/src/point.test.ts +0 -36
  27. package/src/point.ts +0 -246
  28. package/src/prepare_pax.ts +0 -102
  29. package/src/segment.test.ts +0 -26
  30. package/src/segment.ts +0 -701
  31. package/src/sub_design.ts +0 -16
  32. package/src/triangle_utils.test.ts +0 -38
  33. package/src/triangle_utils.ts +0 -112
  34. package/src/vector.test.ts +0 -28
  35. package/src/vector.ts +0 -122
  36. package/src/volume.ts +0 -50
  37. package/src/write_dxf.ts +0 -100
  38. package/src/write_openjscad.ts +0 -284
  39. package/src/write_openscad.ts +0 -305
  40. package/src/write_pax.ts +0 -73
  41. package/src/write_svg.ts +0 -101
  42. package/svg/any_triangle.svg +0 -156
  43. package/svg/arc_definition.svg +0 -506
  44. package/svg/construct_corner_rounded_ext_arc_ext_arc.svg +0 -378
  45. package/svg/construct_corner_rounded_int_arc_ext_arc.svg +0 -359
  46. package/svg/construct_corner_rounded_int_arc_int_arc.svg +0 -356
  47. package/svg/construct_corner_rounded_stroke_ext_arc.svg +0 -374
  48. package/svg/construct_corner_rounded_stroke_ext_arc_obtuse.svg +0 -370
  49. package/svg/construct_corner_rounded_stroke_ext_arc_obtuse_method2.svg +0 -311
  50. package/svg/construct_corner_rounded_stroke_int_arc.svg +0 -364
  51. package/svg/construct_corner_rounded_stroke_int_arc_obtuse.svg +0 -358
  52. package/svg/construct_corner_rounded_stroke_int_arc_obtuse_method2.svg +0 -237
  53. package/svg/construct_corner_rounded_stroke_stroke.svg +0 -280
  54. package/svg/construct_corner_wideacc.svg +0 -286
  55. package/svg/construct_corner_widened.svg +0 -278
  56. package/svg/construction_of_line_intersection_1.svg +0 -187
  57. package/svg/construction_of_line_intersection_2.svg +0 -189
  58. package/svg/contour_arc_definition_options.svg +0 -324
  59. package/svg/contour_point_absolute_relative.svg +0 -258
  60. package/svg/contour_tangential_two_arcs.svg +0 -531
  61. package/svg/contour_tangential_two_arcs_impossible_case.svg +0 -175
  62. package/svg/contour_tangential_two_arcs_requirements.svg +0 -364
  63. package/svg/corner_rounded_ext_arc_ext_arc.svg +0 -281
  64. package/svg/corner_rounded_int_arc_ext_arc.svg +0 -281
  65. package/svg/corner_rounded_int_arc_int_arc.svg +0 -279
  66. package/svg/corner_rounded_stroke_ext_arc.svg +0 -218
  67. package/svg/corner_rounded_stroke_int_arc.svg +0 -225
  68. package/svg/corner_rounded_stroke_stroke.svg +0 -174
  69. package/svg/geom_dev.svg +0 -14708
  70. package/svg/geom_modules.svg +0 -246
  71. package/svg/geom_user.svg +0 -58
  72. package/svg/line_axis_x_cases.svg +0 -1408
  73. package/svg/line_axis_y_cases.svg +0 -1369
  74. package/svg/line_distanceOrig.svg +0 -318
  75. package/svg/line_getAxisXIntersection.svg +0 -262
  76. package/svg/line_getAxisXIntersection_2.svg +0 -244
  77. package/svg/line_getAxisYIntersection.svg +0 -304
  78. package/svg/line_getAxisYIntersection_2.svg +0 -285
  79. package/svg/line_getAxisYIntersection_3.svg +0 -277
  80. package/svg/line_intersection.svg +0 -346
  81. package/svg/line_projectPoint.svg +0 -311
  82. package/svg/point_1.svg +0 -243
  83. package/svg/point_2.svg +0 -409
  84. package/svg/point_3.svg +0 -298
  85. package/svg/point_4.svg +0 -272
  86. package/svg/point_5.svg +0 -356
  87. package/svg/right_triangle.svg +0 -194
  88. package/svg/vector_definition.svg +0 -130
  89. package/tsconfig.json +0 -13
  90. package/vitest.config.ts +0 -7
package/.eslintrc.cjs DELETED
@@ -1,24 +0,0 @@
1
- module.exports = {
2
- extends: [
3
- 'eslint:recommended',
4
- 'plugin:@typescript-eslint/recommended-type-checked',
5
- //'plugin:@typescript-eslint/strict-type-checked',
6
- 'plugin:@typescript-eslint/stylistic-type-checked',
7
- 'prettier'
8
- ],
9
- plugins: ['@typescript-eslint'],
10
- parser: '@typescript-eslint/parser',
11
- parserOptions: {
12
- //sourceType: 'module',
13
- //ecmaVersion: 2020,
14
- project: true,
15
- tsconfigRootDir: __dirname
16
- },
17
- root: true
18
- //env: {
19
- // browser: true,
20
- // es2021: true,
21
- // node: true
22
- //},
23
- //ignorePatterns: ['*.cjs'],
24
- };
package/.prettierignore DELETED
@@ -1,15 +0,0 @@
1
- .DS_Store
2
- node_modules
3
- /build
4
- /dist
5
- /.svelte-kit
6
- /package
7
- .env
8
- .env.*
9
- !.env.example
10
- README.md
11
-
12
- # Ignore files for PNPM, NPM and YARN
13
- pnpm-lock.yaml
14
- package-lock.json
15
- yarn.lock
package/.prettierrc DELETED
@@ -1,8 +0,0 @@
1
- {
2
- "useTabs": true,
3
- "singleQuote": true,
4
- "trailingComma": "none",
5
- "printWidth": 100,
6
- "plugins": [],
7
- "overrides": []
8
- }
@@ -1,213 +0,0 @@
1
- // aaExportContent.ts
2
-
3
- import type { tParamVal } from './designParams';
4
- import type { tGeom } from './aaParamGeom';
5
- import { colors } from './canvas_utils';
6
- import { Point, point, pointMinMax } from './point';
7
- import type { tContour } from './contour';
8
- import type { Figure } from './figure';
9
- import { mergeFaces } from './figure';
10
- import type { SvgWriter } from './write_svg';
11
- import { svgWriter } from './write_svg';
12
- import { dxfWriter } from './write_dxf';
13
- import { paxWrite } from './write_pax';
14
- import { oscadWrite } from './write_openscad';
15
- import { ojscadWrite } from './write_openjscad';
16
- import * as zip from '@zip.js/zip.js';
17
-
18
- // SVG
19
- class MinMaxPoint {
20
- xMin: number;
21
- xMax: number;
22
- yMin: number;
23
- yMax: number;
24
- firstDone: boolean;
25
- constructor() {
26
- this.firstDone = false;
27
- this.xMin = 0;
28
- this.xMax = 0;
29
- this.yMin = 0;
30
- this.yMax = 0;
31
- }
32
- addAContour(aCtr: tContour[]) {
33
- if (aCtr.length > 0) {
34
- const pts: Point[] = [];
35
- if (this.firstDone) {
36
- pts.push(point(this.xMin, this.yMin));
37
- pts.push(point(this.xMax, this.yMax));
38
- }
39
- for (const ctr of aCtr) {
40
- pts.push(...ctr.generatePoints());
41
- }
42
- const [Xmin, Xmax, Ymin, Ymax] = pointMinMax(pts);
43
- this.xMin = Xmin;
44
- this.xMax = Xmax;
45
- this.yMin = Ymin;
46
- this.yMax = Ymax;
47
- }
48
- }
49
- getViewBox() {
50
- const Xdelta = Math.round((this.xMax - this.xMin) * 1.1) + 10;
51
- const Ydelta = Math.round((this.yMax - this.yMin) * 1.1) + 10;
52
- const Xmin2 = Math.round(this.xMin - Xdelta * 0.05);
53
- const Ymin2 = Math.round(this.yMin - Ydelta * 0.05);
54
- return [Xmin2, Xdelta, Ymin2, Ydelta];
55
- }
56
- }
57
-
58
- interface tGroup {
59
- gpName: string;
60
- gpColor: string;
61
- ctrs: tContour[];
62
- }
63
-
64
- class SvgWriter2 {
65
- minMax: MinMaxPoint;
66
- groups: tGroup[];
67
- svg: SvgWriter;
68
- constructor() {
69
- this.minMax = new MinMaxPoint();
70
- this.groups = [];
71
- this.svg = svgWriter();
72
- }
73
- addAContour(aCtrs: tContour[], groupId = '', icolor = colors.contour) {
74
- this.minMax.addAContour(aCtrs);
75
- const grp: tGroup = { gpName: groupId, gpColor: icolor, ctrs: aCtrs };
76
- this.groups.push(grp);
77
- }
78
- makeSvg(yCeiling: number) {
79
- for (const grp of this.groups) {
80
- if (grp.gpName !== '') {
81
- this.svg.addGroup(grp.gpName);
82
- }
83
- for (const ctr of grp.ctrs) {
84
- let ctrColor = ctr.imposedColor;
85
- if (ctrColor === '') {
86
- ctrColor = grp.gpColor;
87
- }
88
- this.svg.addSvgString(ctr.toSvg(yCeiling, ctrColor));
89
- }
90
- if (grp.gpName !== '') {
91
- this.svg.closeGroup();
92
- }
93
- }
94
- }
95
- stringify() {
96
- const [Xmin2, Xdelta, Ymin2, Ydelta] = this.minMax.getViewBox();
97
- this.svg.addHeader(Xmin2, Xdelta, Ymin2, Ydelta);
98
- this.makeSvg(this.minMax.yMax + this.minMax.yMin);
99
- return this.svg.stringify();
100
- }
101
- }
102
- function svgWriter2(): SvgWriter2 {
103
- const rSvgWriter2 = new SvgWriter2();
104
- return rSvgWriter2;
105
- }
106
- function figureToSvg(aCtr: tContour[]): string {
107
- const sw2 = svgWriter2();
108
- sw2.addAContour(aCtr);
109
- return sw2.stringify();
110
- }
111
- function figureToSvgDeco(fig: Figure) {
112
- const sw2 = svgWriter2();
113
- sw2.addAContour(fig.mainList, 'main', colors.main);
114
- sw2.addAContour(fig.mainBList, 'mainB', colors.mainB);
115
- sw2.addAContour(fig.secondList, 'second', colors.second);
116
- sw2.addAContour(fig.secondBList, 'secondB', colors.secondB);
117
- sw2.addAContour(fig.dynamicsList, 'dynamics', colors.dynamics);
118
- const rSvgDeco = sw2.stringify();
119
- return rSvgDeco;
120
- }
121
-
122
- // DXF
123
- function figureToDxf(aCtr: tContour[]): string {
124
- const dxf = dxfWriter();
125
- //const firstDxfLayer = dxf.addLayer('first');
126
- for (const ctr of aCtr) {
127
- if (ctr.circle) {
128
- const seg = ctr.toDxfSeg()[0];
129
- dxf.addCircle(seg.p1x, seg.p1y, seg.radius);
130
- } else {
131
- for (const seg of ctr.toDxfSeg()) {
132
- if (seg.arc) {
133
- dxf.addArc(seg.p1x, seg.p1y, seg.radius, seg.a1, seg.a2);
134
- } else {
135
- dxf.addLine(seg.p1x, seg.p1y, seg.p2x, seg.p2y);
136
- }
137
- }
138
- }
139
- }
140
- const rDxf = dxf.stringify();
141
- return rDxf;
142
- }
143
-
144
- // PAX
145
- function makePax(paramVal: tParamVal, geome0: tGeom): string {
146
- const rStr = paxWrite().getPaxStr(paramVal, geome0);
147
- return rStr;
148
- }
149
-
150
- // OpenSCad
151
- function makeOpenscad(geome0: tGeom): string {
152
- const paxJson = paxWrite().getPaxJson({}, geome0);
153
- const rStr = oscadWrite().getExportFile(paxJson);
154
- return rStr;
155
- }
156
-
157
- // OpenJSCAD
158
- function makeOpenjscad(geome0: tGeom): string {
159
- const paxJson = paxWrite().getPaxJson({}, geome0);
160
- const rStr = ojscadWrite().getExportFile(paxJson);
161
- return rStr;
162
- }
163
-
164
- // ZIP
165
- async function makeZip(
166
- paramVal: tParamVal,
167
- geome0: tGeom,
168
- tSim: number,
169
- geome1: tGeom
170
- ): Promise<Blob> {
171
- // zip writer preparation
172
- const zipFileWriter = new zip.BlobWriter('application/zip');
173
- const zipWriter = new zip.ZipWriter(zipFileWriter);
174
- const partName = geome0.partName;
175
- // zip payload
176
- const zParam = new zip.TextReader(JSON.stringify(paramVal, null, 2));
177
- await zipWriter.add(`param_${partName}.json`, zParam);
178
- const zLog0 = new zip.TextReader(geome0.logstr);
179
- await zipWriter.add(`geom_${partName}_log.txt`, zLog0);
180
- const zLog1 = new zip.TextReader(geome1.logstr);
181
- await zipWriter.add(`geom_${partName}_t${tSim}_log.txt`, zLog1);
182
- for (const face in geome0.fig) {
183
- const svgOne = new zip.TextReader(figureToSvg(geome0.fig[face].mainList));
184
- await zipWriter.add(`face_${partName}_${face}.svg`, svgOne);
185
- const dxfOne = new zip.TextReader(figureToDxf(geome0.fig[face].mainList));
186
- await zipWriter.add(`face_${partName}_${face}.dxf`, dxfOne);
187
- const svgOneDeco = new zip.TextReader(figureToSvgDeco(geome0.fig[face]));
188
- await zipWriter.add(`deco_${partName}_${face}.svg`, svgOneDeco);
189
- const svgOneDecoT = new zip.TextReader(figureToSvgDeco(geome1.fig[face]));
190
- await zipWriter.add(`deco_${partName}_${face}_t${tSim}.svg`, svgOneDecoT);
191
- }
192
- const mergedFace = mergeFaces(geome0.fig);
193
- const svgMerged = new zip.TextReader(figureToSvg(mergedFace.mainList));
194
- await zipWriter.add(`face_${partName}_all_merged.svg`, svgMerged);
195
- const dxfMerged = new zip.TextReader(figureToDxf(mergedFace.mainList));
196
- await zipWriter.add(`face_${partName}_all_merged.dxf`, dxfMerged);
197
- const svgMergedDeco = new zip.TextReader(figureToSvgDeco(mergedFace));
198
- await zipWriter.add(`deco_${partName}_all_merged.svg`, svgMergedDeco);
199
- const svgMergedDecoT = new zip.TextReader(figureToSvgDeco(mergedFace));
200
- await zipWriter.add(`deco_${partName}_all_merged_t${tSim}.svg`, svgMergedDecoT);
201
- const zPax = new zip.TextReader(makePax(paramVal, geome0));
202
- await zipWriter.add(`${partName}.pax.json`, zPax);
203
- const zSCad = new zip.TextReader(makeOpenscad(geome0));
204
- await zipWriter.add(`${partName}_noarc_openscad.scad`, zSCad);
205
- const zJScad = new zip.TextReader(makeOpenjscad(geome0));
206
- await zipWriter.add(`${partName}_noarc_jscad.js`, zJScad);
207
- // zip writer finalization
208
- await zipWriter.close();
209
- const rFileContent = await zipFileWriter.getData();
210
- return rFileContent;
211
- }
212
-
213
- export { figureToSvg, figureToDxf, makePax, makeOpenscad, makeOpenjscad, makeZip };
@@ -1,141 +0,0 @@
1
- // aaExportFile.ts
2
-
3
- import type { tParamVal } from './designParams';
4
- import type { tGeomFunc } from './aaParamGeom';
5
- import {
6
- figureToSvg,
7
- figureToDxf,
8
- makePax,
9
- makeOpenscad,
10
- makeOpenjscad,
11
- makeZip
12
- } from './aaExportContent';
13
- import { mergeFaces } from './figure';
14
-
15
- enum EFormat {
16
- eSVG,
17
- eSVGALL,
18
- eDXF,
19
- eDXFALL,
20
- ePAX,
21
- eOPENSCAD,
22
- eJSCAD,
23
- eZIP
24
- }
25
-
26
- function fileTextContent(
27
- fgeom: tGeomFunc,
28
- paramVal: tParamVal,
29
- eFace: string,
30
- exportFormat: EFormat
31
- ): string {
32
- const geome0 = fgeom(0, paramVal);
33
- let rFileContent = '';
34
- if (!geome0.calcErr) {
35
- const figList = Object.keys(geome0.fig);
36
- if (exportFormat === EFormat.eSVG) {
37
- if (figList.includes(eFace)) {
38
- const figu = geome0.fig[eFace];
39
- rFileContent = figureToSvg(figu.mainList);
40
- } else {
41
- console.log(`err749: fileTextContent eFace ${eFace} invalid`);
42
- }
43
- } else if (exportFormat === EFormat.eSVGALL) {
44
- const figu = mergeFaces(geome0.fig);
45
- rFileContent = figureToSvg(figu.mainList);
46
- } else if (exportFormat === EFormat.eDXF) {
47
- if (figList.includes(eFace)) {
48
- const figu = geome0.fig[eFace];
49
- rFileContent = figureToDxf(figu.mainList);
50
- } else {
51
- console.log(`err759: fileTextContent eFace ${eFace} invalid`);
52
- }
53
- } else if (exportFormat === EFormat.eDXFALL) {
54
- const figu = mergeFaces(geome0.fig);
55
- rFileContent = figureToDxf(figu.mainList);
56
- } else if (exportFormat === EFormat.ePAX) {
57
- rFileContent = makePax(paramVal, geome0);
58
- } else if (exportFormat === EFormat.eOPENSCAD) {
59
- rFileContent = makeOpenscad(geome0);
60
- } else if (exportFormat === EFormat.eJSCAD) {
61
- rFileContent = makeOpenjscad(geome0);
62
- } else {
63
- console.log(`err912: unknown exportFormat ${exportFormat}`);
64
- }
65
- } else {
66
- console.log('err931: error by calling geome ${geome0.calcErr}');
67
- }
68
- return rFileContent;
69
- }
70
-
71
- async function fileBinContent(
72
- fgeom: tGeomFunc,
73
- tSim: number,
74
- paramVal: tParamVal,
75
- exportFormat: EFormat
76
- ): Promise<Blob> {
77
- const geome0 = fgeom(0, paramVal);
78
- const geome1 = fgeom(tSim, paramVal);
79
- let rFileContent = new Blob();
80
- if (!geome0.calcErr && !geome1.calcErr) {
81
- if (exportFormat === EFormat.eZIP) {
82
- rFileContent = await makeZip(paramVal, geome0, tSim, geome1);
83
- } else {
84
- console.log(`err913: unknown exportFormat ${exportFormat}`);
85
- }
86
- } else {
87
- console.log('err932: error by calling geome ${geome0.calcErr} ${geome1.calcErr}');
88
- }
89
- return rFileContent;
90
- }
91
-
92
- function fileMime(exportFormat: EFormat): string {
93
- let rMime = '';
94
- if (exportFormat === EFormat.eSVG || exportFormat === EFormat.eSVGALL) {
95
- rMime = 'image/svg+xml';
96
- } else if (exportFormat === EFormat.eDXF || exportFormat === EFormat.eDXFALL) {
97
- rMime = 'application/dxf';
98
- } else if (exportFormat === EFormat.ePAX) {
99
- rMime = 'application/json';
100
- //rMime = 'text/plain';
101
- } else if (exportFormat === EFormat.eOPENSCAD) {
102
- rMime = 'text/plain';
103
- } else if (exportFormat === EFormat.eJSCAD) {
104
- rMime = 'text/plain';
105
- } else if (exportFormat === EFormat.eZIP) {
106
- rMime = 'application/zip';
107
- //} else {
108
- // console.log(`err903: unknown exportFormat ${exportFormat}`);
109
- }
110
- return rMime;
111
- }
112
-
113
- function fileSuffix(exportFormat: EFormat): string {
114
- let rSuffix = '';
115
- if (exportFormat === EFormat.eSVG || exportFormat === EFormat.eSVGALL) {
116
- rSuffix = '.svg';
117
- } else if (exportFormat === EFormat.eDXF || exportFormat === EFormat.eDXFALL) {
118
- rSuffix = '.dxf';
119
- } else if (exportFormat === EFormat.ePAX) {
120
- rSuffix = '.pax.json';
121
- } else if (exportFormat === EFormat.eOPENSCAD) {
122
- rSuffix = '_noarc_openscad.scad';
123
- } else if (exportFormat === EFormat.eJSCAD) {
124
- rSuffix = '_noarc_jscad.js';
125
- } else if (exportFormat === EFormat.eZIP) {
126
- rSuffix = '.zip';
127
- //} else {
128
- // console.log(`err904: unknown exportFormat ${exportFormat}`);
129
- }
130
- return rSuffix;
131
- }
132
-
133
- function fileBin(exportFormat: EFormat): boolean {
134
- let rBin = false;
135
- if (exportFormat === EFormat.eZIP) {
136
- rBin = true;
137
- }
138
- return rBin;
139
- }
140
-
141
- export { EFormat, fileBinContent, fileTextContent, fileMime, fileSuffix, fileBin };
@@ -1,62 +0,0 @@
1
- // aaParamGeom.ts
2
-
3
- import type { tParamDef, tParamVal } from './designParams';
4
- import type { tFaces } from './figure';
5
- import type { tVolume } from './volume';
6
- import type { tSubDesign } from './sub_design';
7
-
8
- interface tGeom {
9
- partName: string;
10
- calcErr: boolean;
11
- logstr: string;
12
- fig: tFaces;
13
- vol: tVolume;
14
- sub: tSubDesign;
15
- }
16
- type tGeomFunc = (t: number, ipVal: tParamVal) => tGeom;
17
-
18
- interface tPageDef {
19
- pTitle: string;
20
- pDescription: string;
21
- pDef: tParamDef;
22
- pGeom: tGeomFunc;
23
- }
24
- type tAllPageDef = Record<string, tPageDef>;
25
- type tAllLink = Record<string, string>; // partName: its-link
26
-
27
- function fround(ireal: number, iprecision = 1000.0): number {
28
- return Math.floor(ireal * iprecision) / iprecision;
29
- }
30
-
31
- function initGeom(partName: string): tGeom {
32
- const rGeom: tGeom = {
33
- partName: partName,
34
- calcErr: true,
35
- logstr: '',
36
- fig: {},
37
- vol: { extrudes: [], volumes: [] },
38
- sub: {}
39
- };
40
- return rGeom;
41
- }
42
-
43
- function checkGeom(iGeom: tGeom) {
44
- if (iGeom.calcErr) {
45
- let errMsg = `err182: Error in sub-design ${iGeom.partName}\n`;
46
- errMsg += prefixLog(iGeom.logstr, iGeom.partName);
47
- throw errMsg;
48
- }
49
- }
50
-
51
- function prefixLog(iLog: string, iPartName: string): string {
52
- let rLog = '';
53
- for (const oneline of iLog.split('\n')) {
54
- if (oneline !== '') {
55
- rLog += `[${iPartName}]: ${oneline}\n`;
56
- }
57
- }
58
- return rLog;
59
- }
60
-
61
- export type { tGeom, tGeomFunc, tPageDef, tAllPageDef, tAllLink };
62
- export { fround, initGeom, checkGeom, prefixLog };
@@ -1,83 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import {
3
- degToRad,
4
- radToDeg,
5
- roundZero,
6
- withinZero2Pi,
7
- withinPiPi,
8
- withinZeroPi,
9
- withinHPiHPi
10
- } from './angle_utils';
11
-
12
- describe('angle suit', () => {
13
- it('radian to degree', () => {
14
- expect(radToDeg(Math.PI / 6)).toBeCloseTo(30, 5);
15
- });
16
- it('degree to radian', () => {
17
- expect(degToRad(-45)).toBeCloseTo(-Math.PI / 4, 5);
18
- });
19
- it('roundZero', () => {
20
- expect(roundZero(degToRad(90) - Math.PI / 2)).toBe(0);
21
- });
22
- // withinZero2Pi
23
- it('withinZero2Pi 1', () => {
24
- expect(withinZero2Pi(Math.PI / 3)).toBeCloseTo(Math.PI / 3, 5);
25
- });
26
- it('withinZero2Pi 2', () => {
27
- expect(withinZero2Pi((-2 * Math.PI) / 3)).toBeCloseTo((4 * Math.PI) / 3, 5);
28
- });
29
- it('withinZero2Pi 3', () => {
30
- expect(withinZero2Pi(-Math.PI / 3)).toBeCloseTo((5 * Math.PI) / 3, 5);
31
- });
32
- it('withinZero2Pi 4', () => {
33
- expect(withinZero2Pi((8 * Math.PI) / 3)).toBeCloseTo((2 * Math.PI) / 3, 5);
34
- });
35
- it('withinZero2Pi 5', () => {
36
- expect(withinZero2Pi((-14 * Math.PI) / 3)).toBeCloseTo((4 * Math.PI) / 3, 5);
37
- });
38
- // withinPiPi
39
- it('withinPiPi 1', () => {
40
- expect(withinPiPi((3 * Math.PI) / 8)).toBeCloseTo((3 * Math.PI) / 8, 5);
41
- });
42
- it('withinPiPi 2', () => {
43
- expect(withinPiPi((7 * Math.PI) / 8)).toBeCloseTo((7 * Math.PI) / 8, 5);
44
- });
45
- it('withinPiPi 3', () => {
46
- expect(withinPiPi((9 * Math.PI) / 8)).toBeCloseTo((-7 * Math.PI) / 8, 5);
47
- });
48
- it('withinPiPi 4', () => {
49
- expect(withinPiPi((15 * Math.PI) / 8)).toBeCloseTo(-Math.PI / 8, 5);
50
- });
51
- it('withinPiPi 5', () => {
52
- expect(withinPiPi((-13 * Math.PI) / 8)).toBeCloseTo((3 * Math.PI) / 8, 5);
53
- });
54
- // withinZeroPi
55
- it('withinZeroPi 1', () => {
56
- expect(withinZeroPi((2 * Math.PI) / 7)).toBeCloseTo((2 * Math.PI) / 7, 5);
57
- });
58
- it('withinZeroPi 2', () => {
59
- expect(withinZeroPi((6 * Math.PI) / 7)).toBeCloseTo((6 * Math.PI) / 7, 5);
60
- });
61
- it('withinZeroPi 3', () => {
62
- expect(withinZeroPi((9 * Math.PI) / 7)).toBeCloseTo((2 * Math.PI) / 7, 5);
63
- });
64
- it('withinZeroPi 4', () => {
65
- expect(withinZeroPi(-Math.PI / 7)).toBeCloseTo((6 * Math.PI) / 7, 5);
66
- });
67
- it('withinZeroPi 5', () => {
68
- expect(withinZeroPi((-4 * Math.PI) / 7)).toBeCloseTo((3 * Math.PI) / 7, 5);
69
- });
70
- // withinHPiHPi
71
- it('withinHPiHPi 1', () => {
72
- expect(withinHPiHPi((2 * Math.PI) / 7)).toBeCloseTo((2 * Math.PI) / 7, 5);
73
- });
74
- it('withinHPiHPi 2', () => {
75
- expect(withinHPiHPi((5 * Math.PI) / 9)).toBeCloseTo((-4 * Math.PI) / 9, 5);
76
- });
77
- it('withinHPiHPi 3', () => {
78
- expect(withinHPiHPi((-8 * Math.PI) / 9)).toBeCloseTo(Math.PI / 9, 5);
79
- });
80
- it('withinHPiHPi 4', () => {
81
- expect(withinHPiHPi((17 * Math.PI) / 9)).toBeCloseTo(-Math.PI / 9, 5);
82
- });
83
- });
@@ -1,98 +0,0 @@
1
- // angle_utils.ts
2
- // some useful functions manipulating angles
3
- // angle_utils.ts has no dependency
4
-
5
- const tolerance = 10 ** -4;
6
-
7
- /* utils for angles */
8
-
9
- function degToRad(degrees: number): number {
10
- return degrees * (Math.PI / 180);
11
- }
12
-
13
- function radToDeg(rad: number): number {
14
- return rad / (Math.PI / 180);
15
- }
16
-
17
- function roundZero(ix: number): number {
18
- let rx = ix;
19
- if (Math.abs(rx) < tolerance) {
20
- rx = 0;
21
- }
22
- return rx;
23
- }
24
-
25
- function withinZero2Pi(ia: number): number {
26
- let ra = ia % (2 * Math.PI);
27
- if (ra < 0) {
28
- //console.log(`dbg026: ${ra}`);
29
- ra += 2 * Math.PI;
30
- }
31
- return ra;
32
- }
33
-
34
- function withinPiPi(ia: number): number {
35
- let ra = withinZero2Pi(ia);
36
- if (ra > Math.PI) {
37
- ra -= 2 * Math.PI;
38
- }
39
- return ra;
40
- }
41
-
42
- function withinZeroPi(ia: number): number {
43
- let ra = ia % Math.PI;
44
- if (ra < 0) {
45
- ra += Math.PI;
46
- }
47
- return ra;
48
- }
49
-
50
- function withinHPiHPi(ia: number): number {
51
- let ra = withinZeroPi(ia);
52
- if (ra > Math.PI / 2) {
53
- ra -= Math.PI;
54
- }
55
- return ra;
56
- }
57
-
58
- function orientedArc(aStart: number, aStop: number, ccw: boolean): number {
59
- const arc = withinPiPi(aStop) - withinPiPi(aStart);
60
- const arc2 = ccw ? withinZero2Pi(arc) : withinZero2Pi(arc) - 2 * Math.PI;
61
- return arc2;
62
- }
63
-
64
- function isWithin(aNew: number, aStart: number, aStop: number, ccw: boolean): boolean {
65
- let rYes = false;
66
- if (roundZero(withinPiPi(aNew - aStart)) === 0) {
67
- rYes = true;
68
- } else if (roundZero(withinPiPi(aNew - aStop)) === 0) {
69
- rYes = true;
70
- } else {
71
- const arcOrig = orientedArc(aStart, aStop, ccw);
72
- const arcNew = orientedArc(aStart, aNew, ccw);
73
- if (Math.abs(arcNew) < Math.abs(arcOrig)) {
74
- rYes = true;
75
- }
76
- }
77
- return rYes;
78
- }
79
-
80
- function ffix(ifloat: number): string {
81
- return ifloat.toFixed(2);
82
- }
83
-
84
- /* export */
85
-
86
- export {
87
- tolerance,
88
- degToRad,
89
- radToDeg,
90
- roundZero,
91
- withinZero2Pi,
92
- withinPiPi,
93
- withinZeroPi,
94
- withinHPiHPi,
95
- orientedArc,
96
- isWithin,
97
- ffix
98
- };