vislite 0.6.0-alpha.2 → 0.6.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.
- package/.eslintrc.js +3 -1
- package/CHANGELOG +1 -1
- package/README.md +11 -3
- package/lib/Buffer/index.umd.js +2 -2
- package/lib/Buffer/index.umd.min.js +2 -2
- package/lib/Canvas/index.umd.js +2 -2
- package/lib/Canvas/index.umd.min.js +2 -2
- package/lib/Cardinal/index.umd.js +2 -2
- package/lib/Cardinal/index.umd.min.js +2 -2
- package/lib/Eoap/index.umd.js +2 -2
- package/lib/Eoap/index.umd.min.js +2 -2
- package/lib/Geometry/index.umd.js +2 -2
- package/lib/Geometry/index.umd.min.js +2 -2
- package/lib/Hermite/index.umd.js +2 -2
- package/lib/Hermite/index.umd.min.js +2 -2
- package/lib/Matrix4/index.umd.js +2 -2
- package/lib/Matrix4/index.umd.min.js +2 -2
- package/lib/Mercator/index.umd.js +2 -2
- package/lib/Mercator/index.umd.min.js +2 -2
- package/lib/OralCanvas/index.es.js +2 -2
- package/lib/OralCanvas/index.es.min.js +2 -2
- package/lib/OralWebGL/index.es.js +2 -2
- package/lib/OralWebGL/index.es.min.js +2 -2
- package/lib/SVG/index.umd.js +2 -2
- package/lib/SVG/index.umd.min.js +2 -2
- package/lib/Shader/index.umd.js +2 -2
- package/lib/Shader/index.umd.min.js +2 -2
- package/lib/Texture/index.umd.js +2 -2
- package/lib/Texture/index.umd.min.js +2 -2
- package/lib/TreeLayout/index.umd.js +2 -2
- package/lib/TreeLayout/index.umd.min.js +2 -2
- package/lib/WebGL/index.umd.js +2 -2
- package/lib/WebGL/index.umd.min.js +2 -2
- package/lib/animation/index.umd.js +2 -2
- package/lib/animation/index.umd.min.js +2 -2
- package/lib/formatColor/index.umd.js +2 -2
- package/lib/formatColor/index.umd.min.js +2 -2
- package/lib/getLoopColors/index.umd.js +2 -2
- package/lib/getLoopColors/index.umd.min.js +2 -2
- package/lib/getWebGLContext/index.umd.js +2 -2
- package/lib/getWebGLContext/index.umd.min.js +2 -2
- package/lib/index.umd.js +2 -2
- package/lib/index.umd.min.js +2 -2
- package/lib/move/index.umd.js +2 -2
- package/lib/move/index.umd.min.js +2 -2
- package/lib/resizeObserver/index.umd.js +2 -2
- package/lib/resizeObserver/index.umd.min.js +2 -2
- package/lib/rotate/index.umd.js +2 -2
- package/lib/rotate/index.umd.min.js +2 -2
- package/lib/ruler/index.umd.js +2 -2
- package/lib/ruler/index.umd.min.js +2 -2
- package/lib/scale/index.umd.js +2 -2
- package/lib/scale/index.umd.min.js +2 -2
- package/lib/throttle/index.umd.js +2 -2
- package/lib/throttle/index.umd.min.js +2 -2
- package/lib/viewHandler/index.umd.js +2 -2
- package/lib/viewHandler/index.umd.min.js +2 -2
- package/package.json +1 -1
- package/src/Canvas.ts +70 -70
- package/src/Eoap.ts +66 -66
- package/src/Geometry.ts +82 -82
- package/src/Matrix4/move.ts +12 -12
- package/src/Matrix4/rotate.ts +15 -15
- package/src/Matrix4/transform.ts +64 -64
- package/src/Mercator.ts +27 -27
- package/src/SVG.ts +24 -24
- package/src/WebGL.ts +66 -66
- package/src/animation.ts +98 -98
- package/src/common/assemble.ts +22 -22
- package/src/common/canvas/arc.ts +51 -51
- package/src/common/canvas/config.ts +158 -158
- package/src/common/canvas/gradient.ts +29 -29
- package/src/common/canvas/painter.ts +417 -417
- package/src/common/geometry/prism-horizontal.ts +35 -35
- package/src/common/geometry/prism-vertical.ts +40 -40
- package/src/common/geometry/sphere-fragment.ts +40 -40
- package/src/common/geometry/tool/circle.ts +11 -11
- package/src/common/mergeOption.ts +6 -6
- package/src/common/setStyle.ts +5 -5
- package/src/common/svg/config.ts +186 -186
- package/src/common/svg/gradient.ts +63 -63
- package/src/common/svg/index.ts +356 -356
- package/src/common/svg/tool.ts +44 -44
- package/src/common/tree/toInnerTree.ts +57 -57
- package/src/common/tree/toPlainTree.ts +132 -132
- package/src/common/webgl/buffer.ts +79 -79
- package/src/common/webgl/doDraw.ts +108 -108
- package/src/common/webgl/getColorFactory.ts +26 -26
- package/src/common/webgl/getShader.ts +48 -48
- package/src/common/webgl/getTexture.ts +11 -11
- package/src/common/webgl/getWebGLContext.ts +28 -28
- package/src/common/webgl/index.ts +224 -224
- package/src/common/webgl/shader.ts +75 -75
- package/src/common/webgl/texture.ts +97 -97
- package/src/formatColor.ts +44 -44
- package/src/getLoopColors.ts +42 -42
- package/src/interpolation/Cardinal.ts +110 -110
- package/src/interpolation/Hermite.ts +65 -65
- package/src/move.ts +7 -7
- package/src/resizeObserver.ts +37 -37
- package/src/rotate.ts +7 -7
- package/src/throttle.ts +52 -52
- package/src/viewHandler.ts +158 -158
- package/types/CanvasOption.d.ts +8 -8
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* VISLite JavaScript Library v0.6.0
|
|
2
|
+
* VISLite JavaScript Library v0.6.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
|
|
9
|
+
* Publish Date: Sun Dec 17 2023 17:10:48 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();
|
package/package.json
CHANGED
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
|
package/src/Matrix4/move.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 在(a,b,c)方向位移d
|
|
3
|
-
*/
|
|
4
|
-
export default function (d: number, a: number, b: number, c: number = 0) {
|
|
5
|
-
const sqrt = Math.sqrt(a * a + b * b + c * c)
|
|
6
|
-
return [
|
|
7
|
-
1, 0, 0, 0,
|
|
8
|
-
0, 1, 0, 0,
|
|
9
|
-
0, 0, 1, 0,
|
|
10
|
-
a * d / sqrt, b * d / sqrt, c * d / sqrt, 1
|
|
11
|
-
]
|
|
12
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 在(a,b,c)方向位移d
|
|
3
|
+
*/
|
|
4
|
+
export default function (d: number, a: number, b: number, c: number = 0) {
|
|
5
|
+
const sqrt = Math.sqrt(a * a + b * b + c * c)
|
|
6
|
+
return [
|
|
7
|
+
1, 0, 0, 0,
|
|
8
|
+
0, 1, 0, 0,
|
|
9
|
+
0, 0, 1, 0,
|
|
10
|
+
a * d / sqrt, b * d / sqrt, c * d / sqrt, 1
|
|
11
|
+
]
|
|
12
|
+
}
|
package/src/Matrix4/rotate.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 围绕0Z轴旋转
|
|
3
|
-
* 其它的旋转可以借助transform实现
|
|
4
|
-
* 旋转角度单位采用弧度制
|
|
5
|
-
*/
|
|
6
|
-
export default function (deg: number) {
|
|
7
|
-
const sin = Math.sin(deg),
|
|
8
|
-
cos = Math.cos(deg)
|
|
9
|
-
return [
|
|
10
|
-
cos, sin, 0, 0,
|
|
11
|
-
-sin, cos, 0, 0,
|
|
12
|
-
0, 0, 1, 0,
|
|
13
|
-
0, 0, 0, 1
|
|
14
|
-
]
|
|
15
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 围绕0Z轴旋转
|
|
3
|
+
* 其它的旋转可以借助transform实现
|
|
4
|
+
* 旋转角度单位采用弧度制
|
|
5
|
+
*/
|
|
6
|
+
export default function (deg: number) {
|
|
7
|
+
const sin = Math.sin(deg),
|
|
8
|
+
cos = Math.cos(deg)
|
|
9
|
+
return [
|
|
10
|
+
cos, sin, 0, 0,
|
|
11
|
+
-sin, cos, 0, 0,
|
|
12
|
+
0, 0, 1, 0,
|
|
13
|
+
0, 0, 0, 1
|
|
14
|
+
]
|
|
15
|
+
}
|
package/src/Matrix4/transform.ts
CHANGED
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 针对任意射线(a1,b1,c1)->(a2,b2,c2)
|
|
3
|
-
* 计算出两个变换矩阵
|
|
4
|
-
* 分别为:任意射线变成OZ轴变换矩阵 + OZ轴变回原来的射线的变换矩阵
|
|
5
|
-
*/
|
|
6
|
-
export default function (a1: number, b1: number, c1: number, a2: number, b2: number, c2: number) {
|
|
7
|
-
|
|
8
|
-
if (typeof a1 === 'number' && typeof b1 === 'number') {
|
|
9
|
-
|
|
10
|
-
// 如果设置两个点
|
|
11
|
-
// 表示二维上围绕某个点旋转
|
|
12
|
-
if (typeof c1 !== 'number') {
|
|
13
|
-
c1 = 0
|
|
14
|
-
a2 = a1
|
|
15
|
-
b2 = b1
|
|
16
|
-
c2 = 1
|
|
17
|
-
}
|
|
18
|
-
// 只设置三个点(设置不足六个点都认为只设置了三个点)
|
|
19
|
-
// 表示围绕从原点出发的射线旋转
|
|
20
|
-
else if (typeof a2 !== 'number' || typeof b2 !== 'number' || typeof c2 !== 'number') {
|
|
21
|
-
a2 = a1
|
|
22
|
-
b2 = b1
|
|
23
|
-
c2 = c1
|
|
24
|
-
a1 = 0
|
|
25
|
-
b1 = 0
|
|
26
|
-
c1 = 0
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (a1 == a2 && b1 == b2 && c1 == c2) throw new Error('It\'s not a legitimate ray!')
|
|
30
|
-
|
|
31
|
-
const sqrt1 = Math.sqrt((a2 - a1) * (a2 - a1) + (b2 - b1) * (b2 - b1)),
|
|
32
|
-
cos1 = sqrt1 != 0 ? (b2 - b1) / sqrt1 : 1,
|
|
33
|
-
sin1 = sqrt1 != 0 ? (a2 - a1) / sqrt1 : 0,
|
|
34
|
-
|
|
35
|
-
b = (a2 - a1) * sin1 + (b2 - b1) * cos1,
|
|
36
|
-
c = c2 - c1,
|
|
37
|
-
|
|
38
|
-
sqrt2 = Math.sqrt(b * b + c * c),
|
|
39
|
-
cos2 = sqrt2 != 0 ? c / sqrt2 : 1,
|
|
40
|
-
sin2 = sqrt2 != 0 ? b / sqrt2 : 0
|
|
41
|
-
|
|
42
|
-
return [
|
|
43
|
-
|
|
44
|
-
// 任意射线变成OZ轴变换矩阵
|
|
45
|
-
[
|
|
46
|
-
cos1, cos2 * sin1, sin1 * sin2, 0,
|
|
47
|
-
-sin1, cos1 * cos2, cos1 * sin2, 0,
|
|
48
|
-
0, -sin2, cos2, 0,
|
|
49
|
-
b1 * sin1 - a1 * cos1, c1 * sin2 - a1 * sin1 * cos2 - b1 * cos1 * cos2, -a1 * sin1 * sin2 - b1 * cos1 * sin2 - c1 * cos2, 1
|
|
50
|
-
],
|
|
51
|
-
|
|
52
|
-
// OZ轴变回原来的射线的变换矩阵
|
|
53
|
-
[
|
|
54
|
-
cos1, -sin1, 0, 0,
|
|
55
|
-
cos2 * sin1, cos2 * cos1, -sin2, 0,
|
|
56
|
-
sin1 * sin2, cos1 * sin2, cos2, 0,
|
|
57
|
-
a1, b1, c1, 1
|
|
58
|
-
]
|
|
59
|
-
|
|
60
|
-
]
|
|
61
|
-
} else {
|
|
62
|
-
throw new Error('a1 and b1 is required!')
|
|
63
|
-
}
|
|
64
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 针对任意射线(a1,b1,c1)->(a2,b2,c2)
|
|
3
|
+
* 计算出两个变换矩阵
|
|
4
|
+
* 分别为:任意射线变成OZ轴变换矩阵 + OZ轴变回原来的射线的变换矩阵
|
|
5
|
+
*/
|
|
6
|
+
export default function (a1: number, b1: number, c1: number, a2: number, b2: number, c2: number) {
|
|
7
|
+
|
|
8
|
+
if (typeof a1 === 'number' && typeof b1 === 'number') {
|
|
9
|
+
|
|
10
|
+
// 如果设置两个点
|
|
11
|
+
// 表示二维上围绕某个点旋转
|
|
12
|
+
if (typeof c1 !== 'number') {
|
|
13
|
+
c1 = 0
|
|
14
|
+
a2 = a1
|
|
15
|
+
b2 = b1
|
|
16
|
+
c2 = 1
|
|
17
|
+
}
|
|
18
|
+
// 只设置三个点(设置不足六个点都认为只设置了三个点)
|
|
19
|
+
// 表示围绕从原点出发的射线旋转
|
|
20
|
+
else if (typeof a2 !== 'number' || typeof b2 !== 'number' || typeof c2 !== 'number') {
|
|
21
|
+
a2 = a1
|
|
22
|
+
b2 = b1
|
|
23
|
+
c2 = c1
|
|
24
|
+
a1 = 0
|
|
25
|
+
b1 = 0
|
|
26
|
+
c1 = 0
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (a1 == a2 && b1 == b2 && c1 == c2) throw new Error('It\'s not a legitimate ray!')
|
|
30
|
+
|
|
31
|
+
const sqrt1 = Math.sqrt((a2 - a1) * (a2 - a1) + (b2 - b1) * (b2 - b1)),
|
|
32
|
+
cos1 = sqrt1 != 0 ? (b2 - b1) / sqrt1 : 1,
|
|
33
|
+
sin1 = sqrt1 != 0 ? (a2 - a1) / sqrt1 : 0,
|
|
34
|
+
|
|
35
|
+
b = (a2 - a1) * sin1 + (b2 - b1) * cos1,
|
|
36
|
+
c = c2 - c1,
|
|
37
|
+
|
|
38
|
+
sqrt2 = Math.sqrt(b * b + c * c),
|
|
39
|
+
cos2 = sqrt2 != 0 ? c / sqrt2 : 1,
|
|
40
|
+
sin2 = sqrt2 != 0 ? b / sqrt2 : 0
|
|
41
|
+
|
|
42
|
+
return [
|
|
43
|
+
|
|
44
|
+
// 任意射线变成OZ轴变换矩阵
|
|
45
|
+
[
|
|
46
|
+
cos1, cos2 * sin1, sin1 * sin2, 0,
|
|
47
|
+
-sin1, cos1 * cos2, cos1 * sin2, 0,
|
|
48
|
+
0, -sin2, cos2, 0,
|
|
49
|
+
b1 * sin1 - a1 * cos1, c1 * sin2 - a1 * sin1 * cos2 - b1 * cos1 * cos2, -a1 * sin1 * sin2 - b1 * cos1 * sin2 - c1 * cos2, 1
|
|
50
|
+
],
|
|
51
|
+
|
|
52
|
+
// OZ轴变回原来的射线的变换矩阵
|
|
53
|
+
[
|
|
54
|
+
cos1, -sin1, 0, 0,
|
|
55
|
+
cos2 * sin1, cos2 * cos1, -sin2, 0,
|
|
56
|
+
sin1 * sin2, cos1 * sin2, cos2, 0,
|
|
57
|
+
a1, b1, c1, 1
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
]
|
|
61
|
+
} else {
|
|
62
|
+
throw new Error('a1 and b1 is required!')
|
|
63
|
+
}
|
|
64
|
+
}
|