vislite 0.6.0 → 0.7.0-alpha.0

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 (115) hide show
  1. package/CHANGELOG +9 -0
  2. package/README.md +4 -4
  3. package/lib/Buffer/index.umd.js +2 -2
  4. package/lib/Buffer/index.umd.min.js +2 -2
  5. package/lib/Canvas/index.umd.js +2 -2
  6. package/lib/Canvas/index.umd.min.js +2 -2
  7. package/lib/Cardinal/index.umd.js +2 -2
  8. package/lib/Cardinal/index.umd.min.js +2 -2
  9. package/lib/Eoap/index.umd.js +2 -2
  10. package/lib/Eoap/index.umd.min.js +2 -2
  11. package/lib/Geometry/index.umd.js +2 -2
  12. package/lib/Geometry/index.umd.min.js +2 -2
  13. package/lib/Hermite/index.umd.js +2 -2
  14. package/lib/Hermite/index.umd.min.js +2 -2
  15. package/lib/Matrix4/index.umd.js +2 -2
  16. package/lib/Matrix4/index.umd.min.js +2 -2
  17. package/lib/Mercator/index.umd.js +2 -2
  18. package/lib/Mercator/index.umd.min.js +2 -2
  19. package/lib/OralCanvas/index.es.js +2 -2
  20. package/lib/OralCanvas/index.es.min.js +2 -2
  21. package/lib/OralWebGL/index.es.js +2 -2
  22. package/lib/OralWebGL/index.es.min.js +2 -2
  23. package/lib/SVG/index.umd.js +2 -2
  24. package/lib/SVG/index.umd.min.js +2 -2
  25. package/lib/Shader/index.umd.js +2 -2
  26. package/lib/Shader/index.umd.min.js +2 -2
  27. package/lib/Texture/index.umd.js +2 -2
  28. package/lib/Texture/index.umd.min.js +2 -2
  29. package/lib/TreeLayout/index.umd.js +2 -2
  30. package/lib/TreeLayout/index.umd.min.js +2 -2
  31. package/lib/WebGL/index.umd.js +2 -2
  32. package/lib/WebGL/index.umd.min.js +2 -2
  33. package/lib/animation/index.umd.js +2 -2
  34. package/lib/animation/index.umd.min.js +2 -2
  35. package/lib/formatColor/index.umd.js +2 -2
  36. package/lib/formatColor/index.umd.min.js +2 -2
  37. package/lib/getLoopColors/index.umd.js +2 -2
  38. package/lib/getLoopColors/index.umd.min.js +2 -2
  39. package/lib/getWebGLContext/index.umd.js +2 -2
  40. package/lib/getWebGLContext/index.umd.min.js +2 -2
  41. package/lib/index.umd.js +2 -2
  42. package/lib/index.umd.min.js +2 -2
  43. package/lib/move/index.umd.js +2 -2
  44. package/lib/move/index.umd.min.js +2 -2
  45. package/lib/resizeObserver/index.umd.js +2 -2
  46. package/lib/resizeObserver/index.umd.min.js +2 -2
  47. package/lib/rotate/index.umd.js +2 -2
  48. package/lib/rotate/index.umd.min.js +2 -2
  49. package/lib/ruler/index.umd.js +2 -2
  50. package/lib/ruler/index.umd.min.js +2 -2
  51. package/lib/scale/index.umd.js +2 -2
  52. package/lib/scale/index.umd.min.js +2 -2
  53. package/lib/throttle/index.umd.js +2 -2
  54. package/lib/throttle/index.umd.min.js +2 -2
  55. package/lib/viewHandler/index.umd.js +2 -2
  56. package/lib/viewHandler/index.umd.min.js +2 -2
  57. package/package/TreeLayout/index.ts +2 -2
  58. package/package.json +1 -1
  59. package/src/Canvas.ts +70 -70
  60. package/src/Eoap.ts +66 -66
  61. package/src/Geometry.ts +82 -82
  62. package/src/Matrix4/index.ts +84 -84
  63. package/src/Matrix4/move.ts +12 -12
  64. package/src/Matrix4/rotate.ts +15 -15
  65. package/src/Matrix4/transform.ts +64 -64
  66. package/src/Mercator.ts +27 -27
  67. package/src/SVG.ts +24 -24
  68. package/src/WebGL.ts +66 -66
  69. package/src/animation.ts +98 -98
  70. package/src/common/assemble.ts +22 -22
  71. package/src/common/canvas/arc.ts +51 -51
  72. package/src/common/canvas/config.ts +158 -158
  73. package/src/common/canvas/gradient.ts +29 -29
  74. package/src/common/canvas/index.ts +177 -177
  75. package/src/common/canvas/painter.ts +417 -417
  76. package/src/common/geometry/prism-horizontal.ts +35 -35
  77. package/src/common/geometry/prism-vertical.ts +40 -40
  78. package/src/common/geometry/sphere-fragment.ts +40 -40
  79. package/src/common/geometry/tool/circle.ts +11 -11
  80. package/src/common/mergeOption.ts +6 -6
  81. package/src/common/setStyle.ts +5 -5
  82. package/src/common/svg/config.ts +186 -186
  83. package/src/common/svg/gradient.ts +63 -63
  84. package/src/common/svg/index.ts +5 -5
  85. package/src/common/svg/tool.ts +44 -44
  86. package/src/common/tree/toInnerTree.ts +57 -57
  87. package/src/common/tree/toPlainTree.ts +132 -132
  88. package/src/common/webgl/buffer.ts +79 -79
  89. package/src/common/webgl/doDraw.ts +108 -108
  90. package/src/common/webgl/getColorFactory.ts +26 -26
  91. package/src/common/webgl/getShader.ts +48 -48
  92. package/src/common/webgl/getTexture.ts +11 -11
  93. package/src/common/webgl/getWebGLContext.ts +28 -28
  94. package/src/common/webgl/index.ts +224 -224
  95. package/src/common/webgl/shader.ts +75 -75
  96. package/src/common/webgl/texture.ts +97 -97
  97. package/src/formatColor.ts +44 -44
  98. package/src/getLoopColors.ts +42 -42
  99. package/src/interpolation/Cardinal.ts +110 -110
  100. package/src/interpolation/Hermite.ts +65 -65
  101. package/src/layout/TreeLayout.ts +186 -186
  102. package/src/move.ts +7 -7
  103. package/src/resizeObserver.ts +37 -37
  104. package/src/rotate.ts +7 -7
  105. package/src/ruler.ts +209 -209
  106. package/src/throttle.ts +52 -52
  107. package/src/viewHandler.ts +158 -158
  108. package/types/Canvas.d.ts +308 -308
  109. package/types/CanvasOption.d.ts +8 -8
  110. package/types/CanvasOpts.d.ts +9 -9
  111. package/types/Matrix4.d.ts +47 -47
  112. package/types/SVGConfig.d.ts +79 -76
  113. package/uni-app/ui-canvas.vue +9 -31
  114. package/.editorconfig +0 -19
  115. package/.eslintrc.js +0 -26
@@ -1,12 +1,12 @@
1
1
  /*!
2
- * VISLite JavaScript Library v0.6.0
2
+ * VISLite JavaScript Library v0.7.0-alpha.0
3
3
  * git+https://github.com/oi-contrib/VISLite.git
4
4
  *
5
5
  * Copyright zxl20070701
6
6
  * Released under the MIT license
7
7
  * https://zxl20070701.github.io/notebook/home.html
8
8
  *
9
- * Publish Date: Sun Dec 17 2023 17:10:48 GMT+0800 (中国标准时间)
9
+ * Publish Date: Fri Dec 29 2023 22:39:54 GMT+0800 (中国标准时间)
10
10
  */
11
11
  (function (global, factory) {
12
12
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
@@ -1,11 +1,11 @@
1
1
  /*!
2
- * VISLite JavaScript Library v0.6.0
2
+ * VISLite JavaScript Library v0.7.0-alpha.0
3
3
  * git+https://github.com/oi-contrib/VISLite.git
4
4
  *
5
5
  * Copyright zxl20070701
6
6
  * Released under the MIT license
7
7
  * https://zxl20070701.github.io/notebook/home.html
8
8
  *
9
- * Publish Date: Sun Dec 17 2023 17:10:48 GMT+0800 (中国标准时间)
9
+ * Publish Date: Fri Dec 29 2023 22:39:54 GMT+0800 (中国标准时间)
10
10
  */
11
11
  var e,n;e=this,n=function(){"use strict";var e=function(e,n){var t=e.getBoundingClientRect();return{x:n.clientX-t.left,y:n.clientY-t.top}};return function(n){var t=document.getElementsByTagName("body")[0];t.addEventListener("keydown",(function(e){var t=function(e){return{37:"left",38:"up",39:"right",40:"down"}[e.keyCode||e.which]}(e);"up"==t?n({type:"lookUp",normal:[],value:.1,event:e}):"down"==t?n({type:"lookDown",normal:[],value:.1,event:e}):"left"==t?n({type:"lookLeft",normal:[],value:.1,event:e}):"right"==t&&n({type:"lookRight",normal:[],value:.1,event:e})}));var o=null,i=function(i){if(null==o)n({type:"hover",normal:[],value:0,event:i});else{var u=e(t,i),l=[u.x-o.x,u.y-o.y],a=[l[1],l[0],0];if(0==a[0]&&0==a[1])return;n({type:"rotate",normal:a,value:.01*(Math.abs(u.x-o.x)+Math.abs(u.y-o.y)),event:i}),o=u}};t.addEventListener("mousedown",(function(n){o=e(t,n)})),t.addEventListener("mouseup",(function(){o=null})),t.addEventListener("mousemove",(function(e){i(e)})),t.addEventListener("touchend",(function(){o=null})),t.addEventListener("touchstart",(function(n){o=e(t,n)})),t.addEventListener("touchmove",(function(e){i(e.touches[0])}));var u=function(e,t){0!=e&&n({type:"scale",value:e<0?.899:1.101,normal:[],event:t})};t.addEventListener("mousewheel",(function(e){u(e.wheelDelta,e)})),window.addEventListener&&window.addEventListener("DOMMouseScroll",(function(e){u(-1*e.detail,e)}),!1)}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e="undefined"!=typeof globalThis?globalThis:e||self).VISLite_viewHandler=n();
@@ -1,3 +1,3 @@
1
- import TreeLayout from "../../src/layout/TreeLayout"
2
-
1
+ import TreeLayout from "../../src/layout/TreeLayout"
2
+
3
3
  export default TreeLayout
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vislite",
3
- "version": "0.6.0",
3
+ "version": "0.7.0-alpha.0",
4
4
  "description": "灵活、快速、简单的数据可视化交互式跨端前端库",
5
5
  "main": "./lib/index.umd.min.js",
6
6
  "typings": "./types/index.d.ts",
package/src/Canvas.ts CHANGED
@@ -1,71 +1,71 @@
1
- import type CanvasType from '../types/Canvas'
2
- import type CanvasOptionType from '../types/CanvasOption'
3
-
4
- import OralCanvas from './common/canvas/index'
5
- import mergeOption from './common/mergeOption'
6
-
7
- class Canvas extends OralCanvas implements CanvasType {
8
- private __canvas: HTMLCanvasElement
9
- constructor(el: HTMLElement | null, option: CanvasOptionType = {}) {
10
- if (!el) {
11
- throw new Error("VISLite Canvas:The mount point requires an HTMLElement type but encountered null.")
12
- }
13
-
14
- option = mergeOption(option, {
15
- region: true,
16
- willReadFrequently: false
17
- })
18
-
19
- const width = el.clientWidth, height = el.clientHeight
20
-
21
- let ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement
22
-
23
- const _el = el as any
24
-
25
- // 如果已经初始化过了
26
- if (_el._vislite_canvas_) {
27
- ViewCanvas = _el._vislite_canvas_[0]
28
- RegionCanvas = _el._vislite_canvas_[1]
29
- }
30
-
31
- // 否则就初始化
32
- else {
33
- ViewCanvas = document.createElement('canvas')
34
- el.appendChild(ViewCanvas)
35
-
36
- if (option.region) {
37
- RegionCanvas = document.createElement('canvas')
38
- }
39
-
40
- _el._vislite_canvas_ = [ViewCanvas, RegionCanvas]
41
-
42
- el.setAttribute('vislite', 'Canvas')
43
- }
44
-
45
- // 设置画布大小
46
- for (const canvas of [ViewCanvas, RegionCanvas]) {
47
-
48
- if (canvas) {
49
- canvas.style.width = width + "px"
50
- canvas.setAttribute('width', width + "")
51
-
52
- canvas.style.height = height + "px"
53
- canvas.setAttribute('height', height + "")
54
- }
55
- }
56
-
57
- super(ViewCanvas, RegionCanvas, {
58
- willReadFrequently: option.willReadFrequently
59
- })
60
-
61
- this.__canvas = ViewCanvas
62
- }
63
-
64
- toDataURL(): Promise<string> {
65
- return new Promise(resolve => {
66
- resolve(this.__canvas.toDataURL())
67
- })
68
- }
69
- }
70
-
1
+ import type CanvasType from '../types/Canvas'
2
+ import type CanvasOptionType from '../types/CanvasOption'
3
+
4
+ import OralCanvas from './common/canvas/index'
5
+ import mergeOption from './common/mergeOption'
6
+
7
+ class Canvas extends OralCanvas implements CanvasType {
8
+ private __canvas: HTMLCanvasElement
9
+ constructor(el: HTMLElement | null, option: CanvasOptionType = {}) {
10
+ if (!el) {
11
+ throw new Error("VISLite Canvas:The mount point requires an HTMLElement type but encountered null.")
12
+ }
13
+
14
+ option = mergeOption(option, {
15
+ region: true,
16
+ willReadFrequently: false
17
+ })
18
+
19
+ const width = el.clientWidth, height = el.clientHeight
20
+
21
+ let ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement
22
+
23
+ const _el = el as any
24
+
25
+ // 如果已经初始化过了
26
+ if (_el._vislite_canvas_) {
27
+ ViewCanvas = _el._vislite_canvas_[0]
28
+ RegionCanvas = _el._vislite_canvas_[1]
29
+ }
30
+
31
+ // 否则就初始化
32
+ else {
33
+ ViewCanvas = document.createElement('canvas')
34
+ el.appendChild(ViewCanvas)
35
+
36
+ if (option.region) {
37
+ RegionCanvas = document.createElement('canvas')
38
+ }
39
+
40
+ _el._vislite_canvas_ = [ViewCanvas, RegionCanvas]
41
+
42
+ el.setAttribute('vislite', 'Canvas')
43
+ }
44
+
45
+ // 设置画布大小
46
+ for (const canvas of [ViewCanvas, RegionCanvas]) {
47
+
48
+ if (canvas) {
49
+ canvas.style.width = width + "px"
50
+ canvas.setAttribute('width', width + "")
51
+
52
+ canvas.style.height = height + "px"
53
+ canvas.setAttribute('height', height + "")
54
+ }
55
+ }
56
+
57
+ super(ViewCanvas, RegionCanvas, {
58
+ willReadFrequently: option.willReadFrequently
59
+ })
60
+
61
+ this.__canvas = ViewCanvas
62
+ }
63
+
64
+ toDataURL(): Promise<string> {
65
+ return new Promise(resolve => {
66
+ resolve(this.__canvas.toDataURL())
67
+ })
68
+ }
69
+ }
70
+
71
71
  export default Canvas
package/src/Eoap.ts CHANGED
@@ -1,66 +1,66 @@
1
- import type MapType from '../types/Map'
2
-
3
- // 围绕X轴旋转
4
- const rotateX = function (deg: number, x: number, y: number, z: number) {
5
- const cos = Math.cos(deg), sin = Math.sin(deg)
6
- return [x, y * cos - z * sin, y * sin + z * cos]
7
- }
8
-
9
- // 围绕Y轴旋转
10
- const rotateY = function (deg: number, x: number, y: number, z: number) {
11
- const cos = Math.cos(deg), sin = Math.sin(deg)
12
- return [z * sin + x * cos, y, z * cos - x * sin]
13
- }
14
-
15
- // 围绕Z轴旋转
16
- const rotateZ = function (deg: number, x: number, y: number, z: number) {
17
- const cos = Math.cos(deg), sin = Math.sin(deg)
18
- return [x * cos - y * sin, x * sin + y * cos, z]
19
- }
20
-
21
- /* 等角斜方位投影 */
22
- class Eoap implements MapType {
23
- readonly name: string = 'Eoap'
24
-
25
- private __scale: number // 缩放比例
26
- private __center: number[] // 投影中心经纬度
27
-
28
- constructor(scale: number = 7, center: number[] = [107, 36]) {
29
- this.__scale = scale
30
- this.__center = center
31
- }
32
-
33
- use(λ: number, φ: number): [number, number, number] {
34
- /**
35
- * 通过旋转的方法
36
- * 先旋转出点的位置
37
- * 然后根据把地心到旋转中心的这条射线变成OZ这条射线的变换应用到初始化点上
38
- * 这样求的的点的x,y就是最终结果
39
- *
40
- * 计算过程:
41
- * 1.初始化点的位置是p(x,0,0),其中x的值是地球半径除以缩放倍速
42
- * 2.根据点的纬度对p进行旋转,旋转后得到的p的坐标纬度就是目标纬度
43
- * 3.同样的对此刻的p进行经度的旋转,这样就获取了极点作为中心点的坐标
44
- * 4.接着想象一下为了让旋转中心移动到极点需要进行旋转的经纬度是多少,记为lo和la
45
- * 5.然后再对p进行经度度旋转lo获得新的p
46
- * 6.然后再对p进行纬度旋转la获得新的p
47
- * 7.旋转结束
48
- *
49
- * 特别注意:第5和第6步顺序一定不可以调换,原因来自经纬度定义上
50
- * 【除了经度为0的位置,不然纬度的旋转会改变原来的经度值,反过来不会】
51
- *
52
- */
53
- let p = rotateY((360 - φ) / 180 * Math.PI, 100 * this.__scale, 0, 0)
54
- p = rotateZ(λ / 180 * Math.PI, p[0], p[1], p[2])
55
- p = rotateZ((90 - this.__center[0]) / 180 * Math.PI, p[0], p[1], p[2])
56
- p = rotateX((90 - this.__center[1]) / 180 * Math.PI, p[0], p[1], p[2])
57
-
58
- return [
59
- -p[0], // 加-号是因为浏览器坐标和地图不一样
60
- p[1],
61
- p[2]
62
- ]
63
- }
64
- }
65
-
66
- export default Eoap
1
+ import type MapType from '../types/Map'
2
+
3
+ // 围绕X轴旋转
4
+ const rotateX = function (deg: number, x: number, y: number, z: number) {
5
+ const cos = Math.cos(deg), sin = Math.sin(deg)
6
+ return [x, y * cos - z * sin, y * sin + z * cos]
7
+ }
8
+
9
+ // 围绕Y轴旋转
10
+ const rotateY = function (deg: number, x: number, y: number, z: number) {
11
+ const cos = Math.cos(deg), sin = Math.sin(deg)
12
+ return [z * sin + x * cos, y, z * cos - x * sin]
13
+ }
14
+
15
+ // 围绕Z轴旋转
16
+ const rotateZ = function (deg: number, x: number, y: number, z: number) {
17
+ const cos = Math.cos(deg), sin = Math.sin(deg)
18
+ return [x * cos - y * sin, x * sin + y * cos, z]
19
+ }
20
+
21
+ /* 等角斜方位投影 */
22
+ class Eoap implements MapType {
23
+ readonly name: string = 'Eoap'
24
+
25
+ private __scale: number // 缩放比例
26
+ private __center: number[] // 投影中心经纬度
27
+
28
+ constructor(scale: number = 7, center: number[] = [107, 36]) {
29
+ this.__scale = scale
30
+ this.__center = center
31
+ }
32
+
33
+ use(λ: number, φ: number): [number, number, number] {
34
+ /**
35
+ * 通过旋转的方法
36
+ * 先旋转出点的位置
37
+ * 然后根据把地心到旋转中心的这条射线变成OZ这条射线的变换应用到初始化点上
38
+ * 这样求的的点的x,y就是最终结果
39
+ *
40
+ * 计算过程:
41
+ * 1.初始化点的位置是p(x,0,0),其中x的值是地球半径除以缩放倍速
42
+ * 2.根据点的纬度对p进行旋转,旋转后得到的p的坐标纬度就是目标纬度
43
+ * 3.同样的对此刻的p进行经度的旋转,这样就获取了极点作为中心点的坐标
44
+ * 4.接着想象一下为了让旋转中心移动到极点需要进行旋转的经纬度是多少,记为lo和la
45
+ * 5.然后再对p进行经度度旋转lo获得新的p
46
+ * 6.然后再对p进行纬度旋转la获得新的p
47
+ * 7.旋转结束
48
+ *
49
+ * 特别注意:第5和第6步顺序一定不可以调换,原因来自经纬度定义上
50
+ * 【除了经度为0的位置,不然纬度的旋转会改变原来的经度值,反过来不会】
51
+ *
52
+ */
53
+ let p = rotateY((360 - φ) / 180 * Math.PI, 100 * this.__scale, 0, 0)
54
+ p = rotateZ(λ / 180 * Math.PI, p[0], p[1], p[2])
55
+ p = rotateZ((90 - this.__center[0]) / 180 * Math.PI, p[0], p[1], p[2])
56
+ p = rotateX((90 - this.__center[1]) / 180 * Math.PI, p[0], p[1], p[2])
57
+
58
+ return [
59
+ -p[0], // 加-号是因为浏览器坐标和地图不一样
60
+ p[1],
61
+ p[2]
62
+ ]
63
+ }
64
+ }
65
+
66
+ export default Eoap
package/src/Geometry.ts CHANGED
@@ -1,83 +1,83 @@
1
- import type GeometryType from "../types/Geometry"
2
- import type GeometryOptionType from "../types/GeometryOption"
3
- import type GeometryResultType from "../types/GeometryResult"
4
-
5
- import { splitNum } from './common/geometry/tool/circle'
6
- import prismHorizontal from './common/geometry/prism-horizontal'
7
- import prismVertical from './common/geometry/prism-vertical'
8
- import sphereFragment from './common/geometry/sphere-fragment'
9
- import mergeOption from './common/mergeOption'
10
-
11
- class Geometry implements GeometryType {
12
- readonly name: string = 'Geometry'
13
-
14
- private __option: GeometryOptionType
15
-
16
- constructor(option: GeometryOptionType = {}) {
17
- this.__option = mergeOption(option, {
18
- precision: 0.1, // 精度
19
- normal: false, // 是否需要法向量
20
- })
21
- }
22
-
23
- config(option: GeometryOptionType) {
24
- for (const key in option) {
25
- this.__option[key] = option[key]
26
- }
27
- return this
28
- }
29
-
30
- // 圆柱体
31
- cylinder(x: number, y: number, z: number, radius: number, height: number): Array<GeometryResultType> {
32
-
33
- // 求解出需要切割多少份比较合理
34
- const num = splitNum(this.__option.precision, radius)
35
- return this.prism(x, y, z, radius, height, num)
36
- }
37
-
38
- // 棱柱体
39
- prism(x: number, y: number, z: number, radius: number, height: number, num: number): Array<GeometryResultType> {
40
-
41
- const result = [{
42
- points: [],
43
- length: 0,
44
- method: "TRIANGLES" as const
45
- }]
46
-
47
- // 绘制底部的盖子
48
- result[0].points.push(...prismHorizontal(this.__option.normal, x, y, z, radius, num, height > 0 ? -1 : 1))
49
-
50
- // 绘制顶部的盖子
51
- result[0].points.push(...prismHorizontal(this.__option.normal, x, y + height, z, radius, num, height > 0 ? 1 : -1))
52
-
53
- // 绘制侧边部分
54
- result[0].points.push(...prismVertical(this.__option.normal, x, y, z, radius, height, num))
55
-
56
- result[0].length = 12 * num
57
- return result
58
- }
59
-
60
- // 球体
61
- sphere(cx: number, cy: number, cz: number, radius: number): Array<GeometryResultType> {
62
-
63
- // 求解出需要切割多少份比较合理
64
- const num = splitNum(this.__option.precision, radius)
65
-
66
- // 然后一瓣瓣的绘制
67
- const result = [{
68
- points: [],
69
- length: 0,
70
- method: "TRIANGLES" as const
71
- }]
72
- for (let i = 0; i < num; i++) {
73
- result[0].points.push(...sphereFragment(this.__option.normal, cx, cy, cz, radius, num, i))
74
- }
75
-
76
- result[0].length = result[0].points.length / (this.__option.normal ? 6 : 3)
77
-
78
- return result
79
- }
80
-
81
- }
82
-
1
+ import type GeometryType from "../types/Geometry"
2
+ import type GeometryOptionType from "../types/GeometryOption"
3
+ import type GeometryResultType from "../types/GeometryResult"
4
+
5
+ import { splitNum } from './common/geometry/tool/circle'
6
+ import prismHorizontal from './common/geometry/prism-horizontal'
7
+ import prismVertical from './common/geometry/prism-vertical'
8
+ import sphereFragment from './common/geometry/sphere-fragment'
9
+ import mergeOption from './common/mergeOption'
10
+
11
+ class Geometry implements GeometryType {
12
+ readonly name: string = 'Geometry'
13
+
14
+ private __option: GeometryOptionType
15
+
16
+ constructor(option: GeometryOptionType = {}) {
17
+ this.__option = mergeOption(option, {
18
+ precision: 0.1, // 精度
19
+ normal: false, // 是否需要法向量
20
+ })
21
+ }
22
+
23
+ config(option: GeometryOptionType) {
24
+ for (const key in option) {
25
+ this.__option[key] = option[key]
26
+ }
27
+ return this
28
+ }
29
+
30
+ // 圆柱体
31
+ cylinder(x: number, y: number, z: number, radius: number, height: number): Array<GeometryResultType> {
32
+
33
+ // 求解出需要切割多少份比较合理
34
+ const num = splitNum(this.__option.precision, radius)
35
+ return this.prism(x, y, z, radius, height, num)
36
+ }
37
+
38
+ // 棱柱体
39
+ prism(x: number, y: number, z: number, radius: number, height: number, num: number): Array<GeometryResultType> {
40
+
41
+ const result = [{
42
+ points: [],
43
+ length: 0,
44
+ method: "TRIANGLES" as const
45
+ }]
46
+
47
+ // 绘制底部的盖子
48
+ result[0].points.push(...prismHorizontal(this.__option.normal, x, y, z, radius, num, height > 0 ? -1 : 1))
49
+
50
+ // 绘制顶部的盖子
51
+ result[0].points.push(...prismHorizontal(this.__option.normal, x, y + height, z, radius, num, height > 0 ? 1 : -1))
52
+
53
+ // 绘制侧边部分
54
+ result[0].points.push(...prismVertical(this.__option.normal, x, y, z, radius, height, num))
55
+
56
+ result[0].length = 12 * num
57
+ return result
58
+ }
59
+
60
+ // 球体
61
+ sphere(cx: number, cy: number, cz: number, radius: number): Array<GeometryResultType> {
62
+
63
+ // 求解出需要切割多少份比较合理
64
+ const num = splitNum(this.__option.precision, radius)
65
+
66
+ // 然后一瓣瓣的绘制
67
+ const result = [{
68
+ points: [],
69
+ length: 0,
70
+ method: "TRIANGLES" as const
71
+ }]
72
+ for (let i = 0; i < num; i++) {
73
+ result[0].points.push(...sphereFragment(this.__option.normal, cx, cy, cz, radius, num, i))
74
+ }
75
+
76
+ result[0].length = result[0].points.length / (this.__option.normal ? 6 : 3)
77
+
78
+ return result
79
+ }
80
+
81
+ }
82
+
83
83
  export default Geometry
@@ -1,85 +1,85 @@
1
- import type Matrix4Type from '../../types/Matrix4'
2
-
3
- // 两个4x4矩阵相乘
4
- // 或矩阵和齐次坐标相乘
5
- const _multiply = function (matrix4: number[], param: number[]) {
6
- const newParam: number[] = []
7
- for (let i = 0; i < 4; i++)
8
- for (let j = 0; j < param.length / 4; j++)
9
- newParam[j * 4 + i] =
10
- matrix4[i] * param[j * 4] +
11
- matrix4[i + 4] * param[j * 4 + 1] +
12
- matrix4[i + 8] * param[j * 4 + 2] +
13
- matrix4[i + 12] * param[j * 4 + 3]
14
- return newParam
15
- }
16
-
17
- import _move from './move'
18
- import _rotate from './rotate'
19
- import _scale from './scale'
20
- import _transform from './transform'
21
-
22
- // 列主序存储的4x4矩阵
23
-
24
- const __initMatrix4 = [
25
- 1, 0, 0, 0,
26
- 0, 1, 0, 0,
27
- 0, 0, 1, 0,
28
- 0, 0, 0, 1
29
- ]
30
-
31
- class Matrix4 implements Matrix4Type {
32
- readonly name: string = 'Matrix4'
33
-
34
- private __matrix4: number[]
35
-
36
- constructor(initMatrix4: number[] = __initMatrix4) {
37
- this.__matrix4 = initMatrix4
38
- }
39
-
40
- // 设置内置矩阵
41
- setValue(initMatrix4: number[] = __initMatrix4) {
42
- this.__matrix4 = initMatrix4
43
- return this
44
- }
45
-
46
- // 移动
47
- move(dis: number, a: number, b: number, c: number = 0) {
48
- this.__matrix4 = _multiply(_move(dis, a, b, c), this.__matrix4)
49
- return this
50
- }
51
-
52
- // 旋转
53
- rotate(deg: number, a1: number, b1: number, c1?: number, a2?: number, b2?: number, c2?: number) {
54
- const matrix4s = _transform(a1, b1, c1, a2, b2, c2)
55
- this.__matrix4 = _multiply(_multiply(_multiply(matrix4s[1], _rotate(deg)), matrix4s[0]), this.__matrix4)
56
- return this
57
- }
58
-
59
- // 缩放
60
- scale(xTimes: number, yTimes: number, zTimes: number, cx: number = 0, cy: number = 0, cz: number = 0) {
61
- this.__matrix4 = _multiply(_scale(xTimes, yTimes, zTimes, cx, cy, cz), this.__matrix4)
62
- return this
63
- }
64
-
65
- // 乘法
66
- // 可以传入一个矩阵(matrix4,flag)
67
- multiply(newMatrix4: number[], flag: boolean = false) {
68
- this.__matrix4 = flag ? _multiply(this.__matrix4, newMatrix4) : _multiply(newMatrix4, this.__matrix4)
69
- return this
70
- }
71
-
72
- // 对一个坐标应用变换
73
- // 齐次坐标(x,y,z,w)
74
- use(x: number, y: number, z: number = 0, w: number = 1): [number, number, number, number] {
75
- // w为0表示点位于无穷远处,忽略
76
- return _multiply(this.__matrix4, [x, y, z, w]) as [number, number, number, number]
77
- }
78
-
79
- // 矩阵的值
80
- value() {
81
- return this.__matrix4
82
- }
83
- }
84
-
1
+ import type Matrix4Type from '../../types/Matrix4'
2
+
3
+ // 两个4x4矩阵相乘
4
+ // 或矩阵和齐次坐标相乘
5
+ const _multiply = function (matrix4: number[], param: number[]) {
6
+ const newParam: number[] = []
7
+ for (let i = 0; i < 4; i++)
8
+ for (let j = 0; j < param.length / 4; j++)
9
+ newParam[j * 4 + i] =
10
+ matrix4[i] * param[j * 4] +
11
+ matrix4[i + 4] * param[j * 4 + 1] +
12
+ matrix4[i + 8] * param[j * 4 + 2] +
13
+ matrix4[i + 12] * param[j * 4 + 3]
14
+ return newParam
15
+ }
16
+
17
+ import _move from './move'
18
+ import _rotate from './rotate'
19
+ import _scale from './scale'
20
+ import _transform from './transform'
21
+
22
+ // 列主序存储的4x4矩阵
23
+
24
+ const __initMatrix4 = [
25
+ 1, 0, 0, 0,
26
+ 0, 1, 0, 0,
27
+ 0, 0, 1, 0,
28
+ 0, 0, 0, 1
29
+ ]
30
+
31
+ class Matrix4 implements Matrix4Type {
32
+ readonly name: string = 'Matrix4'
33
+
34
+ private __matrix4: number[]
35
+
36
+ constructor(initMatrix4: number[] = __initMatrix4) {
37
+ this.__matrix4 = initMatrix4
38
+ }
39
+
40
+ // 设置内置矩阵
41
+ setValue(initMatrix4: number[] = __initMatrix4) {
42
+ this.__matrix4 = initMatrix4
43
+ return this
44
+ }
45
+
46
+ // 移动
47
+ move(dis: number, a: number, b: number, c: number = 0) {
48
+ this.__matrix4 = _multiply(_move(dis, a, b, c), this.__matrix4)
49
+ return this
50
+ }
51
+
52
+ // 旋转
53
+ rotate(deg: number, a1: number, b1: number, c1?: number, a2?: number, b2?: number, c2?: number) {
54
+ const matrix4s = _transform(a1, b1, c1, a2, b2, c2)
55
+ this.__matrix4 = _multiply(_multiply(_multiply(matrix4s[1], _rotate(deg)), matrix4s[0]), this.__matrix4)
56
+ return this
57
+ }
58
+
59
+ // 缩放
60
+ scale(xTimes: number, yTimes: number, zTimes: number, cx: number = 0, cy: number = 0, cz: number = 0) {
61
+ this.__matrix4 = _multiply(_scale(xTimes, yTimes, zTimes, cx, cy, cz), this.__matrix4)
62
+ return this
63
+ }
64
+
65
+ // 乘法
66
+ // 可以传入一个矩阵(matrix4,flag)
67
+ multiply(newMatrix4: number[], flag: boolean = false) {
68
+ this.__matrix4 = flag ? _multiply(this.__matrix4, newMatrix4) : _multiply(newMatrix4, this.__matrix4)
69
+ return this
70
+ }
71
+
72
+ // 对一个坐标应用变换
73
+ // 齐次坐标(x,y,z,w)
74
+ use(x: number, y: number, z: number = 0, w: number = 1): [number, number, number, number] {
75
+ // w为0表示点位于无穷远处,忽略
76
+ return _multiply(this.__matrix4, [x, y, z, w]) as [number, number, number, number]
77
+ }
78
+
79
+ // 矩阵的值
80
+ value() {
81
+ return this.__matrix4
82
+ }
83
+ }
84
+
85
85
  export default Matrix4