@xort/cga 0.0.93 → 0.1.1
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/THIRD_PARTY_NOTICES.md +76 -0
- package/build/@xort_cga.js +20213 -12091
- package/build/cga.d.ts +3151 -2032
- package/build/cga.esm.js +20175 -0
- package/{src/shader → build/shaders}/cga.glsl +204 -202
- package/build/shaders/common.glsl +36 -0
- package/{src/shader → build/shaders}/noise/GLSL-Noise.md +1108 -1108
- package/build/shaders/noise/LICENSE +20 -0
- package/build/shaders/noise/cellular2D.glsl +69 -0
- package/{src/shader → build/shaders}/noise/cellular2x2.glsl +64 -64
- package/build/shaders/noise/cellular2x2x2.glsl +88 -0
- package/build/shaders/noise/cellular3D.glsl +187 -0
- package/{src/shader → build/shaders}/noise/classicnoise2D.glsl +113 -113
- package/{src/shader → build/shaders}/noise/classicnoise3D.glsl +177 -177
- package/{src/shader → build/shaders}/noise/classicnoise4D.glsl +302 -302
- package/{src/shader → build/shaders}/noise/noise2D.glsl +71 -71
- package/{src/shader → build/shaders}/noise/noise3D.glsl +103 -103
- package/{src/shader → build/shaders}/noise/noise3Dgrad.glsl +111 -111
- package/{src/shader → build/shaders}/noise/noise4D.glsl +129 -129
- package/{src/shader → build/shaders}/noise/psrdnoise2D.glsl +463 -462
- package/dist/THIRD_PARTY_NOTICES.md +76 -0
- package/dist/index.cjs +20352 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +3176 -0
- package/dist/index.d.ts +3176 -38
- package/dist/index.js +20177 -56
- package/dist/index.js.map +1 -0
- package/dist/shaders.cjs +21 -0
- package/dist/shaders.d.cts +20 -0
- package/dist/shaders.d.ts +20 -0
- package/dist/shaders.js +20 -0
- package/dist/three.cjs +3034 -0
- package/dist/three.cjs.map +1 -0
- package/dist/three.d.cts +308 -0
- package/dist/three.d.ts +308 -0
- package/dist/three.js +3007 -0
- package/dist/three.js.map +1 -0
- package/package.json +96 -47
- package/readme.md +111 -247
- package/.github/FUNDING.yml +0 -12
- package/cga/1./347/202/271/344/270/216/347/272/277/346/200/247/345/205/203/347/264/240/347/232/204/350/267/235/347/246/273.html +0 -601
- package/cga/1./347/202/271/344/270/216/347/272/277/346/200/247/345/205/203/347/264/240/347/232/204/350/267/235/347/246/273.md +0 -111
- package/cga/2./347/202/271/344/270/216/351/235/242/346/200/247/345/205/203/347/264/240.md +0 -4
- package/cga/img//347/202/271/344/270/216/345/260/204/347/272/277.png +0 -0
- package/cga/img//347/202/271/344/270/216/347/233/264/347/272/277.png +0 -0
- package/cga/img//347/202/271/344/270/216/347/272/277/346/256/265.png +0 -0
- package/cga/img//347/233/264/347/272/277/347/232/204/350/241/250/350/276/276/345/274/217.png +0 -0
- package/deploy.sh +0 -39
- package/dist/IKanimation/Bone.d.ts +0 -7
- package/dist/IKanimation/Bone.js +0 -20
- package/dist/IKanimation/CCDSolver.d.ts +0 -37
- package/dist/IKanimation/CCDSolver.js +0 -93
- package/dist/IKanimation/Joint.d.ts +0 -3
- package/dist/IKanimation/Joint.js +0 -17
- package/dist/IKanimation/constaint/constraint.d.ts +0 -10
- package/dist/IKanimation/constaint/constraint.js +0 -25
- package/dist/IKanimation/constaint/hinge.d.ts +0 -12
- package/dist/IKanimation/constaint/hinge.js +0 -46
- package/dist/IKanimation/constaint/slider.d.ts +0 -12
- package/dist/IKanimation/constaint/slider.js +0 -44
- package/dist/IKanimation/index.d.ts +0 -0
- package/dist/IKanimation/index.js +0 -9
- package/dist/IKanimation/isolver.d.ts +0 -4
- package/dist/IKanimation/isolver.js +0 -10
- package/dist/IKanimation/skeleton.d.ts +0 -13
- package/dist/IKanimation/skeleton.js +0 -31
- package/dist/alg/common.d.ts +0 -114
- package/dist/alg/common.js +0 -302
- package/dist/alg/curve.d.ts +0 -4
- package/dist/alg/curve.js +0 -11
- package/dist/alg/delaunator.d.ts +0 -29
- package/dist/alg/delaunator.js +0 -442
- package/dist/alg/delaunay.d.ts +0 -20
- package/dist/alg/delaunay.js +0 -199
- package/dist/alg/delaunayslow.d.ts +0 -18
- package/dist/alg/delaunayslow.js +0 -140
- package/dist/alg/extrude/extrude.d.ts +0 -3
- package/dist/alg/extrude/extrude.js +0 -17
- package/dist/alg/extrude.d.ts +0 -199
- package/dist/alg/extrude.js +0 -728
- package/dist/alg/offset/index.d.ts +0 -0
- package/dist/alg/offset/index.js +0 -9
- package/dist/alg/offset.d.ts +0 -0
- package/dist/alg/offset.js +0 -472
- package/dist/alg/pointset.d.ts +0 -94
- package/dist/alg/pointset.js +0 -223
- package/dist/alg/recognition.d.ts +0 -14
- package/dist/alg/recognition.js +0 -37
- package/dist/alg/result.d.ts +0 -22
- package/dist/alg/result.js +0 -2
- package/dist/alg/segmentset.d.ts +0 -11
- package/dist/alg/segmentset.js +0 -45
- package/dist/alg/sort.d.ts +0 -2
- package/dist/alg/sort.js +0 -14
- package/dist/alg/split.d.ts +0 -8
- package/dist/alg/split.js +0 -10
- package/dist/alg/trianglution.d.ts +0 -15
- package/dist/alg/trianglution.js +0 -70
- package/dist/alg/vol.d.ts +0 -6
- package/dist/alg/vol.js +0 -18
- package/dist/alg/voronoi.d.ts +0 -31
- package/dist/alg/voronoi.js +0 -418
- package/dist/app.d.ts +0 -1
- package/dist/app.js +0 -248
- package/dist/basic/distance.d.ts +0 -12
- package/dist/basic/distance.js +0 -39
- package/dist/csg/bsp.d.ts +0 -9
- package/dist/csg/bsp.js +0 -22
- package/dist/csg/node.d.ts +0 -12
- package/dist/csg/node.js +0 -75
- package/dist/csg/polygon.d.ts +0 -43
- package/dist/csg/polygon.js +0 -161
- package/dist/diamondMaterialShader.d.ts +0 -92
- package/dist/diamondMaterialShader.js +0 -118
- package/dist/effect/breath-light.d.ts +0 -4
- package/dist/effect/breath-light.js +0 -39
- package/dist/extends/geometryaid.d.ts +0 -28
- package/dist/extends/geometryaid.js +0 -71
- package/dist/extends/split.d.ts +0 -3
- package/dist/extends/split.js +0 -13
- package/dist/glview.d.ts +0 -112
- package/dist/glview.js +0 -564
- package/dist/math/Color.d.ts +0 -33
- package/dist/math/Color.js +0 -235
- package/dist/math/Euler.d.ts +0 -46
- package/dist/math/Euler.js +0 -225
- package/dist/math/HVec3.d.ts +0 -43
- package/dist/math/HVec3.js +0 -176
- package/dist/math/Mat3.d.ts +0 -29
- package/dist/math/Mat3.js +0 -244
- package/dist/math/Mat4.d.ts +0 -85
- package/dist/math/Mat4.js +0 -791
- package/dist/math/Math.d.ts +0 -130
- package/dist/math/Math.js +0 -198
- package/dist/math/Quat.d.ts +0 -82
- package/dist/math/Quat.js +0 -418
- package/dist/math/Vec2.d.ts +0 -68
- package/dist/math/Vec2.js +0 -328
- package/dist/math/Vec3.d.ts +0 -240
- package/dist/math/Vec3.js +0 -1082
- package/dist/math/Vec4.d.ts +0 -81
- package/dist/math/Vec4.js +0 -475
- package/dist/math/vecs.d.ts +0 -134
- package/dist/math/vecs.js +0 -464
- package/dist/render/bufferAttribute.d.ts +0 -104
- package/dist/render/bufferAttribute.js +0 -344
- package/dist/render/geometry.d.ts +0 -87
- package/dist/render/geometry.js +0 -769
- package/dist/render/mesh.d.ts +0 -30
- package/dist/render/mesh.js +0 -87
- package/dist/render/types.d.ts +0 -3
- package/dist/render/types.js +0 -8
- package/dist/robust/plane.d.ts +0 -7
- package/dist/robust/plane.js +0 -17
- package/dist/robust/vec.d.ts +0 -15
- package/dist/robust/vec.js +0 -71
- package/dist/struct/3d/Box.d.ts +0 -23
- package/dist/struct/3d/Box.js +0 -109
- package/dist/struct/3d/Capsule.d.ts +0 -12
- package/dist/struct/3d/Capsule.js +0 -36
- package/dist/struct/3d/Circle.d.ts +0 -52
- package/dist/struct/3d/Circle.js +0 -138
- package/dist/struct/3d/Disk.d.ts +0 -10
- package/dist/struct/3d/Disk.js +0 -22
- package/dist/struct/3d/Frustum.d.ts +0 -35
- package/dist/struct/3d/Frustum.js +0 -159
- package/dist/struct/3d/Line.d.ts +0 -40
- package/dist/struct/3d/Line.js +0 -340
- package/dist/struct/3d/Path.d.ts +0 -75
- package/dist/struct/3d/Path.js +0 -308
- package/dist/struct/3d/Plane.d.ts +0 -74
- package/dist/struct/3d/Plane.js +0 -298
- package/dist/struct/3d/Point.d.ts +0 -4
- package/dist/struct/3d/Point.js +0 -30
- package/dist/struct/3d/Polygon.d.ts +0 -8
- package/dist/struct/3d/Polygon.js +0 -55
- package/dist/struct/3d/Polyline.d.ts +0 -34
- package/dist/struct/3d/Polyline.js +0 -130
- package/dist/struct/3d/Ray.d.ts +0 -21
- package/dist/struct/3d/Ray.js +0 -262
- package/dist/struct/3d/Rectangle.d.ts +0 -7
- package/dist/struct/3d/Rectangle.js +0 -35
- package/dist/struct/3d/Segment.d.ts +0 -32
- package/dist/struct/3d/Segment.js +0 -334
- package/dist/struct/3d/Shape.d.ts +0 -4
- package/dist/struct/3d/Shape.js +0 -27
- package/dist/struct/3d/Sphere.d.ts +0 -11
- package/dist/struct/3d/Sphere.js +0 -30
- package/dist/struct/3d/Triangle.d.ts +0 -8
- package/dist/struct/3d/Triangle.js +0 -53
- package/dist/struct/bsp.d.ts +0 -9
- package/dist/struct/bsp.js +0 -44
- package/dist/struct/btree.d.ts +0 -0
- package/dist/struct/btree.js +0 -1
- package/dist/struct/data/ArrayList.d.ts +0 -42
- package/dist/struct/data/ArrayList.js +0 -172
- package/dist/struct/data/ArrayObject.d.ts +0 -30
- package/dist/struct/data/ArrayObject.js +0 -131
- package/dist/struct/data/type.d.ts +0 -13
- package/dist/struct/data/type.js +0 -18
- package/dist/struct/kdtree.d.ts +0 -0
- package/dist/struct/kdtree.js +0 -1
- package/dist/struct/polyTree.d.ts +0 -4
- package/dist/struct/polyTree.js +0 -27
- package/dist/struct/quatTree.d.ts +0 -0
- package/dist/struct/quatTree.js +0 -1
- package/dist/struct/tree.d.ts +0 -11
- package/dist/struct/tree.js +0 -26
- package/dist/utils/array.d.ts +0 -24
- package/dist/utils/array.js +0 -89
- package/dist/utils/types.d.ts +0 -24
- package/dist/utils/types.js +0 -20
- package/docs/.vuepress/components/distance.vue +0 -125
- package/docs/.vuepress/components/intersect.vue +0 -0
- package/docs/.vuepress/components/trianglution.vue +0 -121
- package/docs/.vuepress/components/utils.js +0 -314
- package/docs/.vuepress/config.js +0 -99
- package/docs/.vuepress/enhanceApp.js +0 -13
- package/docs/.vuepress/public/delaunay.png +0 -0
- package/docs/.vuepress/public/logo.png +0 -0
- package/docs/.vuepress/public/plane-set-convex.png +0 -0
- package/docs/.vuepress/public/sponsor/alipay.jpg +0 -0
- package/docs/.vuepress/public/sponsor/wechat.png +0 -0
- package/docs/README.md +0 -14
- package/docs/demo/README.md +0 -9
- package/docs/guide/README.md +0 -94
- package/docs/guide/array.md +0 -10
- package/docs/guide/cy/trianglution.html +0 -2
- package/docs/guide/distLineAll.md +0 -85
- package/docs/guide/distPointAll.md +0 -137
- package/docs/guide/distRayAll.md +0 -69
- package/docs/guide/distSegmentAll.md +0 -19
- package/docs/guide/point.vue +0 -12
- package/docs/guide/vector.md +0 -10
- package/docs/guide//345/270/270/347/224/250.md +0 -156
- package/docs/sponsor/README.md +0 -15
- package/docs//345/274/200/345/217/221/346/227/245/345/277/227/2020/345/271/2643/346/234/21013/346/227/245.md +0 -6
- package/effect/common.glsl +0 -34
- package/effect/drawing.glsl +0 -36
- package/effect/shape.arc.glsl +0 -13
- package/effect/shape.circle.glsl +0 -13
- package/effect/shape.grid.glsl +0 -42
- package/effect/shape.line.glsl +0 -18
- package/effect/shape.pie.glsl +0 -18
- package/effect/shape.plot.glsl +0 -10
- package/effect/shape.poly.glsl +0 -15
- package/effect/shape.rect.glsl +0 -14
- package/effect/shape.rectround.glsl +0 -15
- package/effect/shape.segment.glsl +0 -10
- package/effect/shape.spiral.glsl +0 -12
- package/effect/shape.star.glsl +0 -16
- package/examples/index.html +0 -31
- package/git.sh +0 -12
- package/gitproxy.sh +0 -6
- package/image/readme/1640758500266.png +0 -0
- package/image/readme/1640758513361.png +0 -0
- package/index.html +0 -14
- package/linear algorithm//346/225/260/345/255/246/350/241/250/350/276/276/345/274/217.md" +0 -122
- package/linear algorithm//347/237/251/351/230/265.md" +0 -91
- package/rollup.config.js +0 -82
- package/scripttest.html +0 -23
- package/src/IKanimation/Bone.ts +0 -20
- package/src/IKanimation/CCDSolver.ts +0 -114
- package/src/IKanimation/Joint.ts +0 -13
- package/src/IKanimation/constaint/constraint.ts +0 -23
- package/src/IKanimation/constaint/hinge.ts +0 -29
- package/src/IKanimation/constaint/slider.ts +0 -25
- package/src/IKanimation/index.ts +0 -8
- package/src/IKanimation/isolver.ts +0 -13
- package/src/IKanimation/skeleton.ts +0 -30
- package/src/alg/common.ts +0 -304
- package/src/alg/curve.ts +0 -9
- package/src/alg/delaunator.ts +0 -519
- package/src/alg/delaunay.ts +0 -163
- package/src/alg/delaunayslow.ts +0 -183
- package/src/alg/extrude/extrude.ts +0 -19
- package/src/alg/extrude.ts +0 -941
- package/src/alg/offset/index.ts +0 -8
- package/src/alg/offset.ts +0 -501
- package/src/alg/pointset.ts +0 -219
- package/src/alg/recognition.ts +0 -43
- package/src/alg/result.ts +0 -24
- package/src/alg/segmentset.ts +0 -44
- package/src/alg/sort.ts +0 -11
- package/src/alg/split.ts +0 -15
- package/src/alg/trianglution.ts +0 -53
- package/src/alg/vol.ts +0 -18
- package/src/alg/voronoi.ts +0 -303
- package/src/app.css +0 -12
- package/src/app.ts +0 -285
- package/src/assets/color.jpg +0 -0
- package/src/basic/distance.ts +0 -40
- package/src/clipper/clipper.d.ts +0 -323
- package/src/clipper/clipper.js +0 -7626
- package/src/clipper/clipper_float.js +0 -185
- package/src/clipper/clipper_float_unminified.js +0 -5639
- package/src/clipper/clipperint.js +0 -7626
- package/src/csg/bsp.ts +0 -24
- package/src/csg/csg.js +0 -583
- package/src/csg/node.ts +0 -91
- package/src/csg/polygon.ts +0 -177
- package/src/diamondMaterialShader.ts +0 -93
- package/src/effect/breath-light.ts +0 -24
- package/src/extends/geometryaid.ts +0 -86
- package/src/extends/split.ts +0 -15
- package/src/glview.ts +0 -570
- package/src/index.ts +0 -47
- package/src/math/Color.ts +0 -225
- package/src/math/Euler.ts +0 -238
- package/src/math/HVec3.ts +0 -1144
- package/src/math/Mat3.ts +0 -365
- package/src/math/Mat4.ts +0 -1134
- package/src/math/Math.ts +0 -213
- package/src/math/Quat.ts +0 -536
- package/src/math/Vec2.ts +0 -408
- package/src/math/Vec3.ts +0 -1307
- package/src/math/Vec4.ts +0 -587
- package/src/math/vecs.ts +0 -486
- package/src/render/bufferAttribute.ts +0 -491
- package/src/render/geometry.ts +0 -1329
- package/src/render/mesh.ts +0 -82
- package/src/render/types.ts +0 -17
- package/src/robust/plane.ts +0 -12
- package/src/robust/readme.md +0 -1
- package/src/robust/vec.ts +0 -61
- package/src/shader/common.glsl +0 -87
- package/src/shader/noise/cellular2D.glsl +0 -69
- package/src/shader/noise/cellular2x2x2.glsl +0 -88
- package/src/shader/noise/cellular3D.glsl +0 -187
- package/src/struct/3d/Box.ts +0 -124
- package/src/struct/3d/Capsule.ts +0 -18
- package/src/struct/3d/Circle.ts +0 -149
- package/src/struct/3d/Disk.ts +0 -22
- package/src/struct/3d/Frustum.ts +0 -189
- package/src/struct/3d/Line.ts +0 -378
- package/src/struct/3d/Path.ts +0 -314
- package/src/struct/3d/Plane.ts +0 -341
- package/src/struct/3d/Point.ts +0 -7
- package/src/struct/3d/PolyLine.ts +0 -115
- package/src/struct/3d/Polygon.ts +0 -46
- package/src/struct/3d/Ray.ts +0 -285
- package/src/struct/3d/Rectangle.ts +0 -18
- package/src/struct/3d/Segment.ts +0 -330
- package/src/struct/3d/Shape.ts +0 -7
- package/src/struct/3d/Sphere.ts +0 -31
- package/src/struct/3d/Triangle.ts +0 -28
- package/src/struct/bsp.ts +0 -18
- package/src/struct/btree.ts +0 -0
- package/src/struct/data/ArrayList.ts +0 -144
- package/src/struct/data/ArrayObject.ts +0 -105
- package/src/struct/data/type.ts +0 -14
- package/src/struct/kdtree.ts +0 -0
- package/src/struct/polyTree.ts +0 -8
- package/src/struct/quatTree.ts +0 -0
- package/src/struct/tree.ts +0 -30
- package/src/utils/array.ts +0 -106
- package/src/utils/types.ts +0 -42
- package/src-gis/app.ts +0 -7
- package/src-gis/core/Cartographic.ts +0 -48
- package/src-gis/core/EllipsoidGeodesic.ts +0 -56
- package/src-gis/core/EllipsoidTerrainProvider.ts +0 -35
- package/src-gis/core/HeightmapTerrainData.ts +0 -5
- package/src-gis/core/Rectangle.ts +0 -112
- package/src-gis/core/TaskProcessor.ts +0 -222
- package/src-gis/gis.ts +0 -25
- package/src-gis/globe.ts +0 -54
- package/src-gis/math/Cartographic.ts +0 -24
- package/src-gis/math/GVec3.ts +0 -55
- package/src-gis/math/ellipsoid.ts +0 -13
- package/src-gis/provider/DcTerrainProvider.ts +0 -36
- package/src-gis/provider/TerrainProvider.ts +0 -12
- package/src-gis/scene/DcImageryProvider.ts +0 -36
- package/src-gis/scene/GeographicTilingScheme.ts +0 -65
- package/src-gis/scene/Globe.ts +0 -76
- package/src-gis/scene/GlobeSurfaceShaderSet.ts +0 -9
- package/src-gis/scene/GlobeSurfaceTileProvider.ts +0 -25
- package/src-gis/scene/ImageryLayerCollection.ts +0 -75
- package/src-gis/scene/ImageryProvider.ts +0 -28
- package/src-gis/scene/QuadtreeTile.ts +0 -162
- package/src-gis/scene/UrlTemplateImageryProvider.ts +0 -32
- package/src-gis/scene/quadtree_primitive.ts +0 -58
- package/tsconfig.json +0 -77
- package/webpack.config.js +0 -112
- package//350/256/241/347/256/227/345/207/240/344/275/225/351/227/256/351/242/230.md +0 -26
package/dist/three.cjs
ADDED
|
@@ -0,0 +1,3034 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
// \0rolldown/runtime.js
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
12
|
+
desc = __getOwnPropDesc(from, key);
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: desc && desc.get && !desc.configurable ? desc.get : () => from[key],
|
|
15
|
+
enumerable: !desc || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
22
|
+
value: mod,
|
|
23
|
+
enumerable: true
|
|
24
|
+
}) : target, mod));
|
|
25
|
+
let three = require("three");
|
|
26
|
+
let three_addons_controls_OrbitControls_js = require("three/addons/controls/OrbitControls.js");
|
|
27
|
+
let three_addons_controls_TransformControls_js = require("three/addons/controls/TransformControls.js");
|
|
28
|
+
let three_addons_postprocessing_EffectComposer_js = require("three/addons/postprocessing/EffectComposer.js");
|
|
29
|
+
let three_addons_postprocessing_RenderPass_js = require("three/addons/postprocessing/RenderPass.js");
|
|
30
|
+
let three_addons_postprocessing_OutputPass_js = require("three/addons/postprocessing/OutputPass.js");
|
|
31
|
+
let three_addons_postprocessing_UnrealBloomPass_js = require("three/addons/postprocessing/UnrealBloomPass.js");
|
|
32
|
+
three = __toESM(three, 1);
|
|
33
|
+
// src/render/eventhandler.ts
|
|
34
|
+
var EventHandler = class {
|
|
35
|
+
listeners = /* @__PURE__ */ new Map();
|
|
36
|
+
on(type, listener) {
|
|
37
|
+
let set = this.listeners.get(type);
|
|
38
|
+
if (!set) this.listeners.set(type, set = /* @__PURE__ */ new Set());
|
|
39
|
+
set.add(listener);
|
|
40
|
+
return () => this.off(type, listener);
|
|
41
|
+
}
|
|
42
|
+
off(type, listener) {
|
|
43
|
+
const set = this.listeners.get(type);
|
|
44
|
+
set?.delete(listener);
|
|
45
|
+
if (!set?.size) this.listeners.delete(type);
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
once(type, listener) {
|
|
49
|
+
const off = this.on(type, (data) => {
|
|
50
|
+
off();
|
|
51
|
+
listener(data);
|
|
52
|
+
});
|
|
53
|
+
return off;
|
|
54
|
+
}
|
|
55
|
+
fire(type, data) {
|
|
56
|
+
for (const listener of [...this.listeners.get(type) ?? []]) listener(data);
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
removeAllListeners() {
|
|
60
|
+
this.listeners.clear();
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
// src/glview.ts
|
|
64
|
+
var GLView = class extends EventHandler {
|
|
65
|
+
renderer;
|
|
66
|
+
container;
|
|
67
|
+
effectComposer;
|
|
68
|
+
_controls;
|
|
69
|
+
_transfromControl;
|
|
70
|
+
_scene;
|
|
71
|
+
_camera;
|
|
72
|
+
_modelScene = new three.Object3D();
|
|
73
|
+
_helperScene = new three.Object3D();
|
|
74
|
+
outputPass = new three_addons_postprocessing_OutputPass_js.OutputPass();
|
|
75
|
+
renderPass;
|
|
76
|
+
observer;
|
|
77
|
+
abort = new AbortController();
|
|
78
|
+
pmrem;
|
|
79
|
+
environment;
|
|
80
|
+
selected = /* @__PURE__ */ new Map();
|
|
81
|
+
disposed = false;
|
|
82
|
+
postProcessing = false;
|
|
83
|
+
updates = /* @__PURE__ */ new Set();
|
|
84
|
+
allowSelected = true;
|
|
85
|
+
pointer = new three.Vector2();
|
|
86
|
+
constructor(settings = {}) {
|
|
87
|
+
super();
|
|
88
|
+
if (typeof document === "undefined") throw new Error("GLView requires a browser document");
|
|
89
|
+
this.container = settings.container ?? document.body;
|
|
90
|
+
this.renderer = settings.renderer ?? new three.WebGLRenderer({ antialias: true });
|
|
91
|
+
this.renderer.outputColorSpace = three.SRGBColorSpace;
|
|
92
|
+
this.renderer.setPixelRatio(settings.pixelRatio ?? Math.min(devicePixelRatio, 2));
|
|
93
|
+
this.container.appendChild(this.renderer.domElement);
|
|
94
|
+
this._scene = settings.scene ?? new three.Scene();
|
|
95
|
+
this._camera = settings.camera ?? new three.PerspectiveCamera(45, 1, .1, 2e4);
|
|
96
|
+
if (!settings.camera) this._camera.position.set(16, 4, 30);
|
|
97
|
+
this._scene.add(this._modelScene, this._helperScene);
|
|
98
|
+
this.clearColor = settings.clearColor ?? 0xaaeeff;
|
|
99
|
+
this._controls = new three_addons_controls_OrbitControls_js.OrbitControls(this._camera, this.domElement);
|
|
100
|
+
this._transfromControl = new three_addons_controls_TransformControls_js.TransformControls(this._camera, this.domElement);
|
|
101
|
+
this._helperScene.add(this._transfromControl.getHelper());
|
|
102
|
+
this._transfromControl.addEventListener("dragging-changed", (event) => this._controls.enabled = !event.value);
|
|
103
|
+
const light = new three.DirectionalLight(0xffffff, 3);
|
|
104
|
+
light.position.set(1, 1, 1);
|
|
105
|
+
this._helperScene.add(light, new three.AmbientLight(0xffffff, .5));
|
|
106
|
+
this.effectComposer = new three_addons_postprocessing_EffectComposer_js.EffectComposer(this.renderer);
|
|
107
|
+
this.renderPass = new three_addons_postprocessing_RenderPass_js.RenderPass(this.scene, this.camera);
|
|
108
|
+
this.effectComposer.addPass(this.renderPass);
|
|
109
|
+
this.effectComposer.addPass(this.outputPass);
|
|
110
|
+
this.postProcessing = settings.enablePostProcessing ?? settings._enablePostProcessing ?? false;
|
|
111
|
+
this.pmrem = new three.PMREMGenerator(this.renderer);
|
|
112
|
+
this.observer = new ResizeObserver(() => this.onResize());
|
|
113
|
+
this.observer.observe(this.container);
|
|
114
|
+
this.domElement.addEventListener("pointerdown", (event) => this.onMousedown(event), { signal: this.abort.signal });
|
|
115
|
+
this.onResize();
|
|
116
|
+
}
|
|
117
|
+
get scene() {
|
|
118
|
+
return this._scene;
|
|
119
|
+
}
|
|
120
|
+
set scene(value) {
|
|
121
|
+
this._scene.remove(this._modelScene, this._helperScene);
|
|
122
|
+
this._scene = value;
|
|
123
|
+
value.add(this._modelScene, this._helperScene);
|
|
124
|
+
this.renderPass.scene = value;
|
|
125
|
+
}
|
|
126
|
+
get camera() {
|
|
127
|
+
return this._camera;
|
|
128
|
+
}
|
|
129
|
+
set camera(value) {
|
|
130
|
+
this._camera = value;
|
|
131
|
+
this._controls.object = value;
|
|
132
|
+
this._transfromControl.camera = value;
|
|
133
|
+
this.renderPass.camera = value;
|
|
134
|
+
this.onResize();
|
|
135
|
+
}
|
|
136
|
+
get modelScene() {
|
|
137
|
+
return this._modelScene;
|
|
138
|
+
}
|
|
139
|
+
set modelScene(value) {
|
|
140
|
+
this._scene.remove(this._modelScene);
|
|
141
|
+
this._modelScene = value;
|
|
142
|
+
this._scene.add(value);
|
|
143
|
+
}
|
|
144
|
+
get helperScene() {
|
|
145
|
+
return this._helperScene;
|
|
146
|
+
}
|
|
147
|
+
set helperScene(value) {
|
|
148
|
+
this._scene.remove(this._helperScene);
|
|
149
|
+
this._helperScene = value;
|
|
150
|
+
this._scene.add(value);
|
|
151
|
+
value.add(this._transfromControl.getHelper());
|
|
152
|
+
}
|
|
153
|
+
get domElement() {
|
|
154
|
+
return this.renderer.domElement;
|
|
155
|
+
}
|
|
156
|
+
get width() {
|
|
157
|
+
return this.domElement.clientWidth;
|
|
158
|
+
}
|
|
159
|
+
get height() {
|
|
160
|
+
return this.domElement.clientHeight;
|
|
161
|
+
}
|
|
162
|
+
set clearColor(value) {
|
|
163
|
+
this.renderer.setClearColor(new three.Color(value));
|
|
164
|
+
}
|
|
165
|
+
set toneExposure(value) {
|
|
166
|
+
this.renderer.toneMappingExposure = value;
|
|
167
|
+
}
|
|
168
|
+
get enablePostProcessing() {
|
|
169
|
+
return this.postProcessing;
|
|
170
|
+
}
|
|
171
|
+
set enablePostProcessing(value) {
|
|
172
|
+
this.postProcessing = value;
|
|
173
|
+
}
|
|
174
|
+
get selecteds() {
|
|
175
|
+
return [...this.selected.keys()];
|
|
176
|
+
}
|
|
177
|
+
setTransformMode(mode) {
|
|
178
|
+
this._transfromControl.setMode(mode);
|
|
179
|
+
}
|
|
180
|
+
setTransfromMode(mode) {
|
|
181
|
+
this.setTransformMode(mode);
|
|
182
|
+
}
|
|
183
|
+
attach(model) {
|
|
184
|
+
this._transfromControl.attach(model);
|
|
185
|
+
}
|
|
186
|
+
detach(_model) {
|
|
187
|
+
this._transfromControl.detach();
|
|
188
|
+
this._controls.enabled = true;
|
|
189
|
+
}
|
|
190
|
+
pmrenvMap(texture, type = 1) {
|
|
191
|
+
let target;
|
|
192
|
+
if (type === 0 && texture instanceof three.Scene) target = this.pmrem.fromScene(texture);
|
|
193
|
+
else if (type === 2 && texture instanceof three.CubeTexture) target = this.pmrem.fromCubemap(texture);
|
|
194
|
+
else if (texture instanceof three.Texture) target = this.pmrem.fromEquirectangular(texture);
|
|
195
|
+
else throw new TypeError("Invalid PMREM source");
|
|
196
|
+
this.environment?.dispose();
|
|
197
|
+
this.environment = target;
|
|
198
|
+
this.scene.environment = target.texture;
|
|
199
|
+
return target.texture;
|
|
200
|
+
}
|
|
201
|
+
addPass(pass) {
|
|
202
|
+
this.effectComposer.insertPass(pass, this.effectComposer.passes.length - 1);
|
|
203
|
+
return pass;
|
|
204
|
+
}
|
|
205
|
+
addPassEx(name, params = {
|
|
206
|
+
threshold: .01,
|
|
207
|
+
strength: .1,
|
|
208
|
+
radius: .4
|
|
209
|
+
}) {
|
|
210
|
+
if (name !== "Bloom") throw new RangeError("Unknown postprocessing pass");
|
|
211
|
+
const pass = new three_addons_postprocessing_UnrealBloomPass_js.UnrealBloomPass(new three.Vector2(this.width, this.height), params.strength, params.radius, params.threshold);
|
|
212
|
+
return this.addPass(pass);
|
|
213
|
+
}
|
|
214
|
+
add(...objects) {
|
|
215
|
+
this.modelScene.add(...objects);
|
|
216
|
+
return this;
|
|
217
|
+
}
|
|
218
|
+
remove(...objects) {
|
|
219
|
+
this.unselect(objects);
|
|
220
|
+
this.modelScene.remove(...objects);
|
|
221
|
+
return this;
|
|
222
|
+
}
|
|
223
|
+
size(width, height) {
|
|
224
|
+
if (![width, height].every((v) => Number.isFinite(v) && v > 0)) throw new RangeError("Viewport size must be positive");
|
|
225
|
+
this.renderer.setSize(width, height);
|
|
226
|
+
this._camera.aspect = width / height;
|
|
227
|
+
this._camera.updateProjectionMatrix();
|
|
228
|
+
this.effectComposer.setSize(width, height);
|
|
229
|
+
}
|
|
230
|
+
onResize() {
|
|
231
|
+
this.size(Math.max(1, this.container.clientWidth), Math.max(1, this.container.clientHeight));
|
|
232
|
+
}
|
|
233
|
+
addUpdates(...callbacks) {
|
|
234
|
+
callbacks.forEach((callback) => this.updates.add(callback));
|
|
235
|
+
}
|
|
236
|
+
removeUpdates(...callbacks) {
|
|
237
|
+
callbacks.forEach((callback) => this.updates.delete(callback));
|
|
238
|
+
}
|
|
239
|
+
onMousedown(event) {
|
|
240
|
+
if (event.button !== 0 || !this.allowSelected || this._transfromControl.dragging) return;
|
|
241
|
+
this.pointer.set(event.clientX, event.clientY);
|
|
242
|
+
this.selectMD(event.ctrlKey || event.metaKey);
|
|
243
|
+
}
|
|
244
|
+
project(point) {
|
|
245
|
+
return point.clone().project(this.camera);
|
|
246
|
+
}
|
|
247
|
+
unproject(point) {
|
|
248
|
+
return point.clone().unproject(this.camera);
|
|
249
|
+
}
|
|
250
|
+
getScreenPosition(point) {
|
|
251
|
+
const p = this.project(point), rect = this.domElement.getBoundingClientRect();
|
|
252
|
+
return new three.Vector3(rect.left + (p.x + 1) * rect.width / 2, rect.top + (1 - p.y) * rect.height / 2, p.z);
|
|
253
|
+
}
|
|
254
|
+
getSelect(point, isMousePos = true, first = true) {
|
|
255
|
+
const rect = this.domElement.getBoundingClientRect(), ndc = isMousePos ? new three.Vector2((point.x - rect.left) / rect.width * 2 - 1, -(point.y - rect.top) / rect.height * 2 + 1) : point;
|
|
256
|
+
const ray = new three.Raycaster();
|
|
257
|
+
this.scene.updateMatrixWorld(true);
|
|
258
|
+
ray.setFromCamera(ndc, this.camera);
|
|
259
|
+
const hits = ray.intersectObjects(this.modelScene.children, true).filter((hit) => {
|
|
260
|
+
let obj = hit.object;
|
|
261
|
+
while (obj) {
|
|
262
|
+
if (!obj.visible) return false;
|
|
263
|
+
obj = obj.parent;
|
|
264
|
+
}
|
|
265
|
+
return true;
|
|
266
|
+
});
|
|
267
|
+
return first ? hits[0] : hits;
|
|
268
|
+
}
|
|
269
|
+
selectMD(multiple = false) {
|
|
270
|
+
const hit = this.getSelect(this.pointer);
|
|
271
|
+
this.selectModels(hit ? [hit.object] : [], multiple);
|
|
272
|
+
return this.selecteds;
|
|
273
|
+
}
|
|
274
|
+
selectModels(models, multiple = false) {
|
|
275
|
+
if (!multiple) this.unselect();
|
|
276
|
+
this.select(models);
|
|
277
|
+
}
|
|
278
|
+
select(models = []) {
|
|
279
|
+
if (!this.allowSelected) return;
|
|
280
|
+
for (const model of models) if (!this.selected.has(model)) {
|
|
281
|
+
const helper = new three.BoxHelper(model, 0xaa3366);
|
|
282
|
+
this.selected.set(model, helper);
|
|
283
|
+
this.helperScene.add(helper);
|
|
284
|
+
}
|
|
285
|
+
this.fire("selectionChanged", this.selecteds);
|
|
286
|
+
}
|
|
287
|
+
unselect(models = this.selecteds) {
|
|
288
|
+
for (const model of models) {
|
|
289
|
+
const helper = this.selected.get(model);
|
|
290
|
+
if (helper) {
|
|
291
|
+
helper.removeFromParent();
|
|
292
|
+
helper.dispose();
|
|
293
|
+
this.selected.delete(model);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
this.fire("selectionChanged", this.selecteds);
|
|
297
|
+
}
|
|
298
|
+
inRendererArea(point) {
|
|
299
|
+
const p = this.project(point);
|
|
300
|
+
return Math.abs(p.x) <= 1 && Math.abs(p.y) <= 1 && Math.abs(p.z) <= 1;
|
|
301
|
+
}
|
|
302
|
+
render() {
|
|
303
|
+
if (this.disposed) return;
|
|
304
|
+
this._controls.update();
|
|
305
|
+
this.selected.forEach((helper) => helper.update());
|
|
306
|
+
if (this.postProcessing) this.effectComposer.render();
|
|
307
|
+
else this.renderer.render(this.scene, this.camera);
|
|
308
|
+
}
|
|
309
|
+
run() {
|
|
310
|
+
if (this.disposed) throw new Error("GLView has been disposed");
|
|
311
|
+
this.renderer.setAnimationLoop((time) => {
|
|
312
|
+
this.updates.forEach((callback) => callback(time));
|
|
313
|
+
this.render();
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
stop() {
|
|
317
|
+
this.renderer.setAnimationLoop(null);
|
|
318
|
+
}
|
|
319
|
+
hide(object) {
|
|
320
|
+
object.visible = false;
|
|
321
|
+
}
|
|
322
|
+
hideOther(object) {
|
|
323
|
+
this.modelScene.traverse((node) => node.visible = false);
|
|
324
|
+
object.traverse((node) => node.visible = true);
|
|
325
|
+
for (let node = object; node; node = node.parent) node.visible = true;
|
|
326
|
+
}
|
|
327
|
+
showAll() {
|
|
328
|
+
this.modelScene.traverse((node) => node.visible = true);
|
|
329
|
+
}
|
|
330
|
+
dispose() {
|
|
331
|
+
if (this.disposed) return;
|
|
332
|
+
this.stop();
|
|
333
|
+
this.abort.abort();
|
|
334
|
+
this.observer.disconnect();
|
|
335
|
+
this.unselect();
|
|
336
|
+
this.updates.clear();
|
|
337
|
+
this._controls.dispose();
|
|
338
|
+
this._transfromControl.dispose();
|
|
339
|
+
this.effectComposer.passes.forEach((pass) => pass.dispose());
|
|
340
|
+
this.effectComposer.dispose();
|
|
341
|
+
this.environment?.dispose();
|
|
342
|
+
this.pmrem.dispose();
|
|
343
|
+
this.renderer.dispose();
|
|
344
|
+
this.domElement.remove();
|
|
345
|
+
this.removeAllListeners();
|
|
346
|
+
this.disposed = true;
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
// src/effect/breath-light.ts
|
|
350
|
+
var BreathLight = class extends three.Object3D {
|
|
351
|
+
geometry = new three.CircleGeometry(1, 32);
|
|
352
|
+
rings = [];
|
|
353
|
+
constructor(color = 2071759) {
|
|
354
|
+
super();
|
|
355
|
+
this.geometry.rotateX(-Math.PI / 2);
|
|
356
|
+
for (let i = 0; i < 3; i++) {
|
|
357
|
+
const ring = new three.Mesh(this.geometry, new three.MeshBasicMaterial({
|
|
358
|
+
color,
|
|
359
|
+
transparent: true,
|
|
360
|
+
opacity: 1 - i / 3,
|
|
361
|
+
depthWrite: false
|
|
362
|
+
}));
|
|
363
|
+
this.rings.push(ring);
|
|
364
|
+
this.add(ring);
|
|
365
|
+
}
|
|
366
|
+
this.update(0);
|
|
367
|
+
}
|
|
368
|
+
update(time) {
|
|
369
|
+
this.rings.forEach((ring, i) => {
|
|
370
|
+
const phase = ((time / 2e3 + i / 3) % 1 + 1) % 1;
|
|
371
|
+
ring.scale.setScalar(1 + phase * 3);
|
|
372
|
+
ring.material.opacity = 1 - phase;
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
dispose() {
|
|
376
|
+
this.geometry.dispose();
|
|
377
|
+
this.rings.forEach((ring) => ring.material.dispose());
|
|
378
|
+
this.clear();
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
// src/diamondMaterialShader.ts
|
|
382
|
+
var DiamondShader = {
|
|
383
|
+
defines: { RAY_BOUNCES: 5 },
|
|
384
|
+
vertexShader: [
|
|
385
|
+
"varying vec2 vUv;",
|
|
386
|
+
"varying vec3 Normal;",
|
|
387
|
+
"varying vec3 worldNormal;",
|
|
388
|
+
"varying vec3 vecPos;",
|
|
389
|
+
"varying vec3 viewPos;",
|
|
390
|
+
"void main() {",
|
|
391
|
+
"vUv = uv;",
|
|
392
|
+
"Normal = normal;",
|
|
393
|
+
"worldNormal = (modelMatrix * vec4(normal,0.0)).xyz;",
|
|
394
|
+
"vecPos = (modelMatrix * vec4(position, 1.0 )).xyz;",
|
|
395
|
+
"viewPos = (modelViewMatrix * vec4(position, 1.0 )).xyz;",
|
|
396
|
+
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
|
|
397
|
+
"}"
|
|
398
|
+
].join("\n"),
|
|
399
|
+
fragmentShader: [
|
|
400
|
+
"varying vec2 vUv;",
|
|
401
|
+
"varying vec3 Normal;",
|
|
402
|
+
"varying vec3 worldNormal;",
|
|
403
|
+
"varying vec3 vecPos;",
|
|
404
|
+
"varying vec3 viewPos;",
|
|
405
|
+
"uniform samplerCube tCubeMapNormals;",
|
|
406
|
+
"uniform samplerCube envMap;",
|
|
407
|
+
"uniform samplerCube envRefractionMap;",
|
|
408
|
+
"uniform sampler2D sphereMap;",
|
|
409
|
+
"uniform float envMapIntensity;",
|
|
410
|
+
"uniform float tanAngleSqCone;",
|
|
411
|
+
"uniform float coneHeight;",
|
|
412
|
+
"uniform int maxBounces;",
|
|
413
|
+
"uniform mat4 modelMatrix;",
|
|
414
|
+
"uniform mat4 invModelMatrix;",
|
|
415
|
+
"uniform float n2;",
|
|
416
|
+
"uniform float radius;",
|
|
417
|
+
"uniform bool bDebugBounces;",
|
|
418
|
+
"uniform float rIndexDelta;",
|
|
419
|
+
"uniform float normalOffset;",
|
|
420
|
+
"uniform float squashFactor;",
|
|
421
|
+
"uniform float distanceOffset;",
|
|
422
|
+
"uniform float geometryFactor;",
|
|
423
|
+
"uniform vec3 Absorbption;",
|
|
424
|
+
"uniform vec3 colorCorrection;",
|
|
425
|
+
"uniform vec3 boostFactors;",
|
|
426
|
+
"uniform vec3 centreOffset;",
|
|
427
|
+
"vec3 BRDF_Specular_GGX_Environment( const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness ) {",
|
|
428
|
+
"float dotNV = abs( dot( normal, viewDir ) );",
|
|
429
|
+
"const vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 );",
|
|
430
|
+
"const vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 );",
|
|
431
|
+
"vec4 r = roughness * c0 + c1;",
|
|
432
|
+
"float a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y;",
|
|
433
|
+
"vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw;",
|
|
434
|
+
"return specularColor * AB.x + AB.y;",
|
|
435
|
+
"}",
|
|
436
|
+
"vec4 SampleSpecularReflection(vec4 specularColor, vec3 direction ) {",
|
|
437
|
+
"direction.x *= -1.0;",
|
|
438
|
+
"direction.z *= -1.0;",
|
|
439
|
+
"vec3 tempDir = normalize(vec3(0., 0., 1.) + direction);",
|
|
440
|
+
"vec4 sampleColorRGB = envMapIntensity * textureCube( envMap, direction );",
|
|
441
|
+
"vec4 sampleColorRefraction = envMapIntensity * ( texture2D( sphereMap, tempDir.xy * 0.5 + 0.5 ) );",
|
|
442
|
+
"vec3 toneMappedColor = pow(sampleColorRGB.rgb,vec3(1./1.));",
|
|
443
|
+
"return vec4(toneMappedColor, 1.0);",
|
|
444
|
+
"}",
|
|
445
|
+
"vec4 SampleSpecularContribution(vec4 specularColor, vec3 direction ) {",
|
|
446
|
+
"direction = normalize(direction);",
|
|
447
|
+
"direction.x *= -1.0;",
|
|
448
|
+
"direction.z *= -1.0;",
|
|
449
|
+
"vec4 sampleColorRGB = envMapIntensity * textureCube( envMap, direction );",
|
|
450
|
+
"vec3 tempDir = normalize(vec3(0., 0., 1.) + direction);",
|
|
451
|
+
"float m = 2.8284271247461903 * sqrt( direction.z+1.0 );",
|
|
452
|
+
"vec4 sampleColorRefraction = envMapIntensity * texture2D( sphereMap, clamp(direction.xy / m + 0.45, vec2(0.), vec2(1.)) );",
|
|
453
|
+
"vec3 toneMappedColor = pow(sampleColorRGB.rgb,vec3(1./1.));",
|
|
454
|
+
"return vec4(toneMappedColor, 1.0);",
|
|
455
|
+
"}",
|
|
456
|
+
"vec3 intersectSphere(vec3 origin, vec3 direction) {",
|
|
457
|
+
"origin -= centreOffset;",
|
|
458
|
+
"direction.y /= squashFactor;",
|
|
459
|
+
"float A = dot(direction, direction);",
|
|
460
|
+
"float B = 2.0*dot(origin, direction);",
|
|
461
|
+
"float C = dot(origin, origin) - radius * radius;",
|
|
462
|
+
"float disc = B*B - 4.0 * A * C;",
|
|
463
|
+
"if(disc > 0.0)",
|
|
464
|
+
"{",
|
|
465
|
+
"disc = sqrt(disc);",
|
|
466
|
+
"float t1 = (-B + disc)*geometryFactor/A;",
|
|
467
|
+
"float t2 = (-B - disc)*geometryFactor/A;",
|
|
468
|
+
"float t = (t1 > t2) ? t1 : t2;",
|
|
469
|
+
"direction.y *= squashFactor;",
|
|
470
|
+
"return vec3(origin + centreOffset + direction * t);",
|
|
471
|
+
"}",
|
|
472
|
+
"return vec3(0.0);",
|
|
473
|
+
"}",
|
|
474
|
+
"vec3 debugBounces(int count) {",
|
|
475
|
+
"vec3 color = vec3(1.,1.,1.);",
|
|
476
|
+
"if(count == 1)",
|
|
477
|
+
"color = vec3(0.0,1.0,0.0);",
|
|
478
|
+
"else if(count == 2)",
|
|
479
|
+
"color = vec3(0.0,0.0,1.0);",
|
|
480
|
+
"else if(count == 3)",
|
|
481
|
+
"color = vec3(1.0,1.0,0.0);",
|
|
482
|
+
"else if(count == 4)",
|
|
483
|
+
"color = vec3(0.0,1.0,1.0);",
|
|
484
|
+
"else",
|
|
485
|
+
"color = vec3(0.0,1.0,0.0);",
|
|
486
|
+
"if(count ==0)",
|
|
487
|
+
"color = vec3(1.0,0.0,0.0);",
|
|
488
|
+
"return color;",
|
|
489
|
+
"}",
|
|
490
|
+
"vec3 traceRay(vec3 origin, vec3 direction, vec3 normal) {",
|
|
491
|
+
"vec3 outColor = vec3(0.0);",
|
|
492
|
+
"// Reflect/Refract ray entering the diamond",
|
|
493
|
+
"const float n1 = 1.0;",
|
|
494
|
+
"const float epsilon = 1e-4;",
|
|
495
|
+
"float f0 = (2.4- n1)/(2.4 + n1);",
|
|
496
|
+
"f0 *= f0;",
|
|
497
|
+
"vec3 attenuationFactor = vec3(1.0);",
|
|
498
|
+
"vec3 newDirection = refract(direction, normal, n1/n2);",
|
|
499
|
+
"vec3 reflectedDirection = reflect(direction, normal);",
|
|
500
|
+
"vec3 brdfReflected = BRDF_Specular_GGX_Environment(reflectedDirection, normal, vec3(f0), 0.0);",
|
|
501
|
+
"vec3 brdfRefracted = BRDF_Specular_GGX_Environment(newDirection, -normal, vec3(f0), 0.0);",
|
|
502
|
+
"attenuationFactor *= ( vec3(1.0) - brdfRefracted);",
|
|
503
|
+
"outColor += SampleSpecularReflection(vec4(1.0), reflectedDirection ).rgb * brdfReflected;",
|
|
504
|
+
"int count = 0;",
|
|
505
|
+
"newDirection = (invModelMatrix * vec4(newDirection, 0.0)).xyz;",
|
|
506
|
+
"newDirection = normalize(newDirection);",
|
|
507
|
+
"origin = (invModelMatrix * vec4(origin, 1.0)).xyz;",
|
|
508
|
+
"// ray bounces ",
|
|
509
|
+
"for( int i=0; i<RAY_BOUNCES; i++) { ",
|
|
510
|
+
"vec3 intersectedPos;",
|
|
511
|
+
"intersectedPos = intersectSphere(origin + vec3(epsilon), newDirection);",
|
|
512
|
+
"vec3 dist = intersectedPos - origin;",
|
|
513
|
+
"vec3 d = normalize(intersectedPos - centreOffset);",
|
|
514
|
+
"vec3 mappedNormal = textureCube( tCubeMapNormals, d ).xyz;",
|
|
515
|
+
"mappedNormal = 2. * mappedNormal - 1.;",
|
|
516
|
+
"mappedNormal.y += normalOffset;",
|
|
517
|
+
"mappedNormal = normalize(mappedNormal);",
|
|
518
|
+
"dist = (modelMatrix * vec4(dist, 1.)).xyz;",
|
|
519
|
+
"float r = sqrt(dot(dist, dist));",
|
|
520
|
+
"attenuationFactor *= exp(-r*Absorbption);",
|
|
521
|
+
"// refract the ray at first intersection ",
|
|
522
|
+
"vec3 oldOrigin = origin;",
|
|
523
|
+
"origin = intersectedPos - normalize(intersectedPos - centreOffset) * distanceOffset;",
|
|
524
|
+
"vec3 oldDir = newDirection;",
|
|
525
|
+
"newDirection = refract(newDirection, mappedNormal, n2/n1);",
|
|
526
|
+
"if( dot(newDirection, newDirection) == 0.0) { // Total Internal Reflection. Continue inside the diamond ",
|
|
527
|
+
"newDirection = reflect(oldDir, mappedNormal);",
|
|
528
|
+
"if(i == RAY_BOUNCES-1 ) //If the ray got trapped even after max iterations, simply sample along the outgoing refraction! ",
|
|
529
|
+
"{",
|
|
530
|
+
"vec3 brdfReflected = BRDF_Specular_GGX_Environment(-oldDir, mappedNormal, vec3(f0), 0.0);",
|
|
531
|
+
"vec3 d1 = (modelMatrix * vec4(oldDir, 0.0)).xyz;",
|
|
532
|
+
"outColor += SampleSpecularContribution(vec4(1.0), d1 ).rgb * colorCorrection * attenuationFactor * boostFactors * (vec3(1.0) - brdfReflected);",
|
|
533
|
+
"//outColor = vec3(1.,0.,0.);",
|
|
534
|
+
"//if(d1.y > 0.95) {",
|
|
535
|
+
"//outColor += d1.y * vec3(1.,0.,0) * attenuationFactor * (vec3(1.0) - brdfReflected) * boostFactors;",
|
|
536
|
+
"//}",
|
|
537
|
+
"}",
|
|
538
|
+
"} else { // Add the contribution from outgoing ray, and continue the reflected ray inside the diamond ",
|
|
539
|
+
"vec3 brdfRefracted = BRDF_Specular_GGX_Environment(newDirection, -mappedNormal, vec3(f0), 0.0);",
|
|
540
|
+
"// outgoing(refracted) ray's contribution ",
|
|
541
|
+
"vec3 d1 = (modelMatrix * vec4(newDirection, 0.0)).xyz;",
|
|
542
|
+
"vec3 colorG = SampleSpecularContribution(vec4(1.0), d1 ).rgb * ( vec3(1.0) - brdfRefracted);",
|
|
543
|
+
"vec3 dir1 = refract(oldDir, mappedNormal, (n2+rIndexDelta)/n1);",
|
|
544
|
+
"vec3 dir2 = refract(oldDir, mappedNormal, (n2-rIndexDelta)/n1);",
|
|
545
|
+
"vec3 d2 = (modelMatrix * vec4(dir1, 0.0)).xyz;",
|
|
546
|
+
"vec3 d3 = (modelMatrix * vec4(dir2, 0.0)).xyz;",
|
|
547
|
+
"vec3 colorR = SampleSpecularContribution(vec4(1.0), d2 ).rgb * ( vec3(1.0) - brdfRefracted);",
|
|
548
|
+
"vec3 colorB = SampleSpecularContribution(vec4(1.0), d3 ).rgb * ( vec3(1.0) - brdfRefracted);",
|
|
549
|
+
"outColor += vec3(colorR.r, colorG.g, colorB.b) * colorCorrection * attenuationFactor * boostFactors;",
|
|
550
|
+
"//outColor = oldDir;",
|
|
551
|
+
"//new reflected ray inside the diamond ",
|
|
552
|
+
"newDirection = reflect(oldDir, mappedNormal);",
|
|
553
|
+
"vec3 brdfReflected = BRDF_Specular_GGX_Environment(newDirection, mappedNormal, vec3(f0), 0.0);",
|
|
554
|
+
"attenuationFactor *= brdfReflected * boostFactors;",
|
|
555
|
+
"count++;",
|
|
556
|
+
"}",
|
|
557
|
+
"}",
|
|
558
|
+
"if(false)",
|
|
559
|
+
"outColor = debugBounces(count);",
|
|
560
|
+
"return outColor;",
|
|
561
|
+
"}",
|
|
562
|
+
"void main() {",
|
|
563
|
+
"vec3 normalizedNormal = normalize(worldNormal);",
|
|
564
|
+
"vec3 viewVector = normalize(vecPos - cameraPosition);",
|
|
565
|
+
"vec3 color = traceRay(vecPos, viewVector, normalizedNormal);",
|
|
566
|
+
"gl_FragColor = vec4(color.rgb,1.);",
|
|
567
|
+
"#include <tonemapping_fragment>",
|
|
568
|
+
"#include <colorspace_fragment>",
|
|
569
|
+
"//gl_FragColor = textureCube(tCubeMapNormals, normalize(Normal));",
|
|
570
|
+
"}"
|
|
571
|
+
].join("\n"),
|
|
572
|
+
uniforms: {
|
|
573
|
+
tCubeMapNormals: {
|
|
574
|
+
type: "t",
|
|
575
|
+
value: null
|
|
576
|
+
},
|
|
577
|
+
envMap: {
|
|
578
|
+
type: "t",
|
|
579
|
+
value: null
|
|
580
|
+
},
|
|
581
|
+
envRefractionMap: {
|
|
582
|
+
type: "t",
|
|
583
|
+
value: null
|
|
584
|
+
},
|
|
585
|
+
sphereMap: {
|
|
586
|
+
type: "t",
|
|
587
|
+
value: null
|
|
588
|
+
},
|
|
589
|
+
envMapIntensity: {
|
|
590
|
+
type: "f",
|
|
591
|
+
value: 1
|
|
592
|
+
},
|
|
593
|
+
maxBounces: {
|
|
594
|
+
type: "i",
|
|
595
|
+
value: 1
|
|
596
|
+
},
|
|
597
|
+
tanAngleSqCone: {
|
|
598
|
+
type: "f",
|
|
599
|
+
value: 0
|
|
600
|
+
},
|
|
601
|
+
coneHeight: {
|
|
602
|
+
type: "f",
|
|
603
|
+
value: 0
|
|
604
|
+
},
|
|
605
|
+
bDebugBounces: {
|
|
606
|
+
type: "i",
|
|
607
|
+
value: !1
|
|
608
|
+
},
|
|
609
|
+
rIndexDelta: {
|
|
610
|
+
type: "f",
|
|
611
|
+
value: .012
|
|
612
|
+
},
|
|
613
|
+
n2: {
|
|
614
|
+
type: "f",
|
|
615
|
+
value: 2.4
|
|
616
|
+
},
|
|
617
|
+
radius: {
|
|
618
|
+
type: "f",
|
|
619
|
+
value: 1
|
|
620
|
+
},
|
|
621
|
+
normalOffset: {
|
|
622
|
+
type: "f",
|
|
623
|
+
value: 0
|
|
624
|
+
},
|
|
625
|
+
squashFactor: {
|
|
626
|
+
type: "f",
|
|
627
|
+
value: .98
|
|
628
|
+
},
|
|
629
|
+
distanceOffset: {
|
|
630
|
+
type: "f",
|
|
631
|
+
value: 0
|
|
632
|
+
},
|
|
633
|
+
geometryFactor: {
|
|
634
|
+
type: "f",
|
|
635
|
+
value: .28
|
|
636
|
+
},
|
|
637
|
+
Absorbption: {
|
|
638
|
+
type: "v3",
|
|
639
|
+
value: new three.Vector3(0, 0, 0)
|
|
640
|
+
},
|
|
641
|
+
colorCorrection: {
|
|
642
|
+
type: "v3",
|
|
643
|
+
value: new three.Vector3(1, 1, 1)
|
|
644
|
+
},
|
|
645
|
+
boostFactors: {
|
|
646
|
+
type: "v3",
|
|
647
|
+
value: new three.Vector3(.892, .892, .98595025)
|
|
648
|
+
},
|
|
649
|
+
centreOffset: {
|
|
650
|
+
type: "v3",
|
|
651
|
+
value: new three.Vector3(0, 0, 0)
|
|
652
|
+
}
|
|
653
|
+
},
|
|
654
|
+
side: three.DoubleSide
|
|
655
|
+
};
|
|
656
|
+
new three.ShaderMaterial(DiamondShader);
|
|
657
|
+
// src/csg/node.ts
|
|
658
|
+
var Node = class Node {
|
|
659
|
+
front;
|
|
660
|
+
back;
|
|
661
|
+
divider;
|
|
662
|
+
polygons = [];
|
|
663
|
+
constructor(polygons = []) {
|
|
664
|
+
this.build(polygons);
|
|
665
|
+
}
|
|
666
|
+
clone() {
|
|
667
|
+
const node = new Node();
|
|
668
|
+
node.divider = this.divider?.clone();
|
|
669
|
+
node.polygons = this.polygons.map((p) => p.clone());
|
|
670
|
+
node.front = this.front?.clone();
|
|
671
|
+
node.back = this.back?.clone();
|
|
672
|
+
return node;
|
|
673
|
+
}
|
|
674
|
+
invert() {
|
|
675
|
+
this.polygons.forEach((p) => p.flip());
|
|
676
|
+
this.divider?.flip();
|
|
677
|
+
this.front?.invert();
|
|
678
|
+
this.back?.invert();
|
|
679
|
+
[this.front, this.back] = [this.back, this.front];
|
|
680
|
+
return this;
|
|
681
|
+
}
|
|
682
|
+
clipPolygons(polygons) {
|
|
683
|
+
if (!this.divider) return polygons.map((p) => p.clone());
|
|
684
|
+
let front = [], back = [];
|
|
685
|
+
for (const p of polygons) this.divider.splitPolygon(p, front, back, front, back);
|
|
686
|
+
if (this.front) front = this.front.clipPolygons(front);
|
|
687
|
+
back = this.back ? this.back.clipPolygons(back) : [];
|
|
688
|
+
return front.concat(back);
|
|
689
|
+
}
|
|
690
|
+
clipTo(node) {
|
|
691
|
+
this.polygons = node.clipPolygons(this.polygons);
|
|
692
|
+
this.front?.clipTo(node);
|
|
693
|
+
this.back?.clipTo(node);
|
|
694
|
+
return this;
|
|
695
|
+
}
|
|
696
|
+
allPolygons() {
|
|
697
|
+
return [
|
|
698
|
+
...this.polygons.map((p) => p.clone()),
|
|
699
|
+
...this.front?.allPolygons() ?? [],
|
|
700
|
+
...this.back?.allPolygons() ?? []
|
|
701
|
+
];
|
|
702
|
+
}
|
|
703
|
+
build(polygons) {
|
|
704
|
+
if (!polygons.length) return this;
|
|
705
|
+
this.divider ??= polygons[0].clone();
|
|
706
|
+
const front = [], back = [];
|
|
707
|
+
for (const p of polygons) this.divider.splitPolygon(p, this.polygons, this.polygons, front, back);
|
|
708
|
+
if (front.length) (this.front ??= new Node()).build(front);
|
|
709
|
+
if (back.length) (this.back ??= new Node()).build(back);
|
|
710
|
+
return this;
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
// src/csg/bsp.ts
|
|
714
|
+
/** BSP solid booleans on closed, consistently outward oriented convex polygon faces. */
|
|
715
|
+
var BSP = class BSP {
|
|
716
|
+
tree;
|
|
717
|
+
constructor(faces = []) {
|
|
718
|
+
this.tree = new Node(faces);
|
|
719
|
+
}
|
|
720
|
+
toPolygons() {
|
|
721
|
+
return this.tree.allPolygons();
|
|
722
|
+
}
|
|
723
|
+
clone() {
|
|
724
|
+
return new BSP(this.toPolygons());
|
|
725
|
+
}
|
|
726
|
+
inverse() {
|
|
727
|
+
const result = this.clone();
|
|
728
|
+
result.tree.invert();
|
|
729
|
+
return result;
|
|
730
|
+
}
|
|
731
|
+
sub(other) {
|
|
732
|
+
if (!this.toPolygons().length || !other.toPolygons().length) return this.clone();
|
|
733
|
+
const a = this.tree.clone(), b = other.tree.clone();
|
|
734
|
+
a.invert();
|
|
735
|
+
a.clipTo(b);
|
|
736
|
+
b.clipTo(a);
|
|
737
|
+
b.invert();
|
|
738
|
+
b.clipTo(a);
|
|
739
|
+
b.invert();
|
|
740
|
+
a.build(b.allPolygons());
|
|
741
|
+
a.invert();
|
|
742
|
+
return new BSP(a.allPolygons());
|
|
743
|
+
}
|
|
744
|
+
subtract(other) {
|
|
745
|
+
return this.sub(other);
|
|
746
|
+
}
|
|
747
|
+
union(other) {
|
|
748
|
+
if (!this.toPolygons().length) return other.clone();
|
|
749
|
+
if (!other.toPolygons().length) return this.clone();
|
|
750
|
+
const a = this.tree.clone(), b = other.tree.clone();
|
|
751
|
+
a.clipTo(b);
|
|
752
|
+
b.clipTo(a);
|
|
753
|
+
b.invert();
|
|
754
|
+
b.clipTo(a);
|
|
755
|
+
b.invert();
|
|
756
|
+
a.build(b.allPolygons());
|
|
757
|
+
return new BSP(a.allPolygons());
|
|
758
|
+
}
|
|
759
|
+
intersect(other) {
|
|
760
|
+
if (!this.toPolygons().length || !other.toPolygons().length) return new BSP();
|
|
761
|
+
const a = this.tree.clone(), b = other.tree.clone();
|
|
762
|
+
a.invert();
|
|
763
|
+
b.clipTo(a);
|
|
764
|
+
b.invert();
|
|
765
|
+
a.clipTo(b);
|
|
766
|
+
b.clipTo(a);
|
|
767
|
+
a.build(b.allPolygons());
|
|
768
|
+
a.invert();
|
|
769
|
+
return new BSP(a.allPolygons());
|
|
770
|
+
}
|
|
771
|
+
};
|
|
772
|
+
// node_modules/decimal.js/decimal.mjs
|
|
773
|
+
/*!
|
|
774
|
+
* decimal.js v10.6.0
|
|
775
|
+
* An arbitrary-precision Decimal type for JavaScript.
|
|
776
|
+
* https://github.com/MikeMcl/decimal.js
|
|
777
|
+
* Copyright (c) 2025 Michael Mclaughlin <M8ch88l@gmail.com>
|
|
778
|
+
* MIT Licence
|
|
779
|
+
*/
|
|
780
|
+
var EXP_LIMIT = 9e15;
|
|
781
|
+
var MAX_DIGITS = 1e9;
|
|
782
|
+
var NUMERALS = "0123456789abcdef";
|
|
783
|
+
var LN10 = "2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058";
|
|
784
|
+
var PI = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789";
|
|
785
|
+
var DEFAULTS = {
|
|
786
|
+
precision: 20,
|
|
787
|
+
rounding: 4,
|
|
788
|
+
modulo: 1,
|
|
789
|
+
toExpNeg: -7,
|
|
790
|
+
toExpPos: 21,
|
|
791
|
+
minE: -EXP_LIMIT,
|
|
792
|
+
maxE: EXP_LIMIT,
|
|
793
|
+
crypto: false
|
|
794
|
+
};
|
|
795
|
+
var inexact;
|
|
796
|
+
var quadrant;
|
|
797
|
+
var external = true;
|
|
798
|
+
var decimalError = "[DecimalError] ";
|
|
799
|
+
var invalidArgument = decimalError + "Invalid argument: ";
|
|
800
|
+
var precisionLimitExceeded = decimalError + "Precision limit exceeded";
|
|
801
|
+
var cryptoUnavailable = decimalError + "crypto unavailable";
|
|
802
|
+
var tag = "[object Decimal]";
|
|
803
|
+
var mathfloor = Math.floor;
|
|
804
|
+
var mathpow = Math.pow;
|
|
805
|
+
var isBinary = /^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i;
|
|
806
|
+
var isHex = /^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i;
|
|
807
|
+
var isOctal = /^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i;
|
|
808
|
+
var isDecimal = /^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;
|
|
809
|
+
var BASE = 1e7;
|
|
810
|
+
var LOG_BASE = 7;
|
|
811
|
+
var MAX_SAFE_INTEGER = 0x1fffffffffffff;
|
|
812
|
+
var LN10_PRECISION = LN10.length - 1;
|
|
813
|
+
var PI_PRECISION = PI.length - 1;
|
|
814
|
+
var P = { toStringTag: tag };
|
|
815
|
+
P.absoluteValue = P.abs = function() {
|
|
816
|
+
var x = new this.constructor(this);
|
|
817
|
+
if (x.s < 0) x.s = 1;
|
|
818
|
+
return finalise(x);
|
|
819
|
+
};
|
|
820
|
+
P.ceil = function() {
|
|
821
|
+
return finalise(new this.constructor(this), this.e + 1, 2);
|
|
822
|
+
};
|
|
823
|
+
P.clampedTo = P.clamp = function(min, max) {
|
|
824
|
+
var k, x = this, Ctor = x.constructor;
|
|
825
|
+
min = new Ctor(min);
|
|
826
|
+
max = new Ctor(max);
|
|
827
|
+
if (!min.s || !max.s) return new Ctor(NaN);
|
|
828
|
+
if (min.gt(max)) throw Error(invalidArgument + max);
|
|
829
|
+
k = x.cmp(min);
|
|
830
|
+
return k < 0 ? min : x.cmp(max) > 0 ? max : new Ctor(x);
|
|
831
|
+
};
|
|
832
|
+
P.comparedTo = P.cmp = function(y) {
|
|
833
|
+
var i, j, xdL, ydL, x = this, xd = x.d, yd = (y = new x.constructor(y)).d, xs = x.s, ys = y.s;
|
|
834
|
+
if (!xd || !yd) return !xs || !ys ? NaN : xs !== ys ? xs : xd === yd ? 0 : !xd ^ xs < 0 ? 1 : -1;
|
|
835
|
+
if (!xd[0] || !yd[0]) return xd[0] ? xs : yd[0] ? -ys : 0;
|
|
836
|
+
if (xs !== ys) return xs;
|
|
837
|
+
if (x.e !== y.e) return x.e > y.e ^ xs < 0 ? 1 : -1;
|
|
838
|
+
xdL = xd.length;
|
|
839
|
+
ydL = yd.length;
|
|
840
|
+
for (i = 0, j = xdL < ydL ? xdL : ydL; i < j; ++i) if (xd[i] !== yd[i]) return xd[i] > yd[i] ^ xs < 0 ? 1 : -1;
|
|
841
|
+
return xdL === ydL ? 0 : xdL > ydL ^ xs < 0 ? 1 : -1;
|
|
842
|
+
};
|
|
843
|
+
P.cosine = P.cos = function() {
|
|
844
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
845
|
+
if (!x.d) return new Ctor(NaN);
|
|
846
|
+
if (!x.d[0]) return new Ctor(1);
|
|
847
|
+
pr = Ctor.precision;
|
|
848
|
+
rm = Ctor.rounding;
|
|
849
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
|
|
850
|
+
Ctor.rounding = 1;
|
|
851
|
+
x = cosine(Ctor, toLessThanHalfPi(Ctor, x));
|
|
852
|
+
Ctor.precision = pr;
|
|
853
|
+
Ctor.rounding = rm;
|
|
854
|
+
return finalise(quadrant == 2 || quadrant == 3 ? x.neg() : x, pr, rm, true);
|
|
855
|
+
};
|
|
856
|
+
P.cubeRoot = P.cbrt = function() {
|
|
857
|
+
var e, m, n, r, rep, s, sd, t, t3, t3plusx, x = this, Ctor = x.constructor;
|
|
858
|
+
if (!x.isFinite() || x.isZero()) return new Ctor(x);
|
|
859
|
+
external = false;
|
|
860
|
+
s = x.s * mathpow(x.s * x, 1 / 3);
|
|
861
|
+
if (!s || Math.abs(s) == 1 / 0) {
|
|
862
|
+
n = digitsToString(x.d);
|
|
863
|
+
e = x.e;
|
|
864
|
+
if (s = (e - n.length + 1) % 3) n += s == 1 || s == -2 ? "0" : "00";
|
|
865
|
+
s = mathpow(n, 1 / 3);
|
|
866
|
+
e = mathfloor((e + 1) / 3) - (e % 3 == (e < 0 ? -1 : 2));
|
|
867
|
+
if (s == 1 / 0) n = "5e" + e;
|
|
868
|
+
else {
|
|
869
|
+
n = s.toExponential();
|
|
870
|
+
n = n.slice(0, n.indexOf("e") + 1) + e;
|
|
871
|
+
}
|
|
872
|
+
r = new Ctor(n);
|
|
873
|
+
r.s = x.s;
|
|
874
|
+
} else r = new Ctor(s.toString());
|
|
875
|
+
sd = (e = Ctor.precision) + 3;
|
|
876
|
+
for (;;) {
|
|
877
|
+
t = r;
|
|
878
|
+
t3 = t.times(t).times(t);
|
|
879
|
+
t3plusx = t3.plus(x);
|
|
880
|
+
r = divide(t3plusx.plus(x).times(t), t3plusx.plus(t3), sd + 2, 1);
|
|
881
|
+
if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
|
|
882
|
+
n = n.slice(sd - 3, sd + 1);
|
|
883
|
+
if (n == "9999" || !rep && n == "4999") {
|
|
884
|
+
if (!rep) {
|
|
885
|
+
finalise(t, e + 1, 0);
|
|
886
|
+
if (t.times(t).times(t).eq(x)) {
|
|
887
|
+
r = t;
|
|
888
|
+
break;
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
sd += 4;
|
|
892
|
+
rep = 1;
|
|
893
|
+
} else {
|
|
894
|
+
if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
|
|
895
|
+
finalise(r, e + 1, 1);
|
|
896
|
+
m = !r.times(r).times(r).eq(x);
|
|
897
|
+
}
|
|
898
|
+
break;
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
external = true;
|
|
903
|
+
return finalise(r, e, Ctor.rounding, m);
|
|
904
|
+
};
|
|
905
|
+
P.decimalPlaces = P.dp = function() {
|
|
906
|
+
var w, d = this.d, n = NaN;
|
|
907
|
+
if (d) {
|
|
908
|
+
w = d.length - 1;
|
|
909
|
+
n = (w - mathfloor(this.e / LOG_BASE)) * LOG_BASE;
|
|
910
|
+
w = d[w];
|
|
911
|
+
if (w) for (; w % 10 == 0; w /= 10) n--;
|
|
912
|
+
if (n < 0) n = 0;
|
|
913
|
+
}
|
|
914
|
+
return n;
|
|
915
|
+
};
|
|
916
|
+
P.dividedBy = P.div = function(y) {
|
|
917
|
+
return divide(this, new this.constructor(y));
|
|
918
|
+
};
|
|
919
|
+
P.dividedToIntegerBy = P.divToInt = function(y) {
|
|
920
|
+
var x = this, Ctor = x.constructor;
|
|
921
|
+
return finalise(divide(x, new Ctor(y), 0, 1, 1), Ctor.precision, Ctor.rounding);
|
|
922
|
+
};
|
|
923
|
+
P.equals = P.eq = function(y) {
|
|
924
|
+
return this.cmp(y) === 0;
|
|
925
|
+
};
|
|
926
|
+
P.floor = function() {
|
|
927
|
+
return finalise(new this.constructor(this), this.e + 1, 3);
|
|
928
|
+
};
|
|
929
|
+
P.greaterThan = P.gt = function(y) {
|
|
930
|
+
return this.cmp(y) > 0;
|
|
931
|
+
};
|
|
932
|
+
P.greaterThanOrEqualTo = P.gte = function(y) {
|
|
933
|
+
var k = this.cmp(y);
|
|
934
|
+
return k == 1 || k === 0;
|
|
935
|
+
};
|
|
936
|
+
P.hyperbolicCosine = P.cosh = function() {
|
|
937
|
+
var k, n, pr, rm, len, x = this, Ctor = x.constructor, one = new Ctor(1);
|
|
938
|
+
if (!x.isFinite()) return new Ctor(x.s ? 1 / 0 : NaN);
|
|
939
|
+
if (x.isZero()) return one;
|
|
940
|
+
pr = Ctor.precision;
|
|
941
|
+
rm = Ctor.rounding;
|
|
942
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
|
|
943
|
+
Ctor.rounding = 1;
|
|
944
|
+
len = x.d.length;
|
|
945
|
+
if (len < 32) {
|
|
946
|
+
k = Math.ceil(len / 3);
|
|
947
|
+
n = (1 / tinyPow(4, k)).toString();
|
|
948
|
+
} else {
|
|
949
|
+
k = 16;
|
|
950
|
+
n = "2.3283064365386962890625e-10";
|
|
951
|
+
}
|
|
952
|
+
x = taylorSeries(Ctor, 1, x.times(n), new Ctor(1), true);
|
|
953
|
+
var cosh2_x, i = k, d8 = new Ctor(8);
|
|
954
|
+
for (; i--;) {
|
|
955
|
+
cosh2_x = x.times(x);
|
|
956
|
+
x = one.minus(cosh2_x.times(d8.minus(cosh2_x.times(d8))));
|
|
957
|
+
}
|
|
958
|
+
return finalise(x, Ctor.precision = pr, Ctor.rounding = rm, true);
|
|
959
|
+
};
|
|
960
|
+
P.hyperbolicSine = P.sinh = function() {
|
|
961
|
+
var k, pr, rm, len, x = this, Ctor = x.constructor;
|
|
962
|
+
if (!x.isFinite() || x.isZero()) return new Ctor(x);
|
|
963
|
+
pr = Ctor.precision;
|
|
964
|
+
rm = Ctor.rounding;
|
|
965
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
|
|
966
|
+
Ctor.rounding = 1;
|
|
967
|
+
len = x.d.length;
|
|
968
|
+
if (len < 3) x = taylorSeries(Ctor, 2, x, x, true);
|
|
969
|
+
else {
|
|
970
|
+
k = 1.4 * Math.sqrt(len);
|
|
971
|
+
k = k > 16 ? 16 : k | 0;
|
|
972
|
+
x = x.times(1 / tinyPow(5, k));
|
|
973
|
+
x = taylorSeries(Ctor, 2, x, x, true);
|
|
974
|
+
var sinh2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20);
|
|
975
|
+
for (; k--;) {
|
|
976
|
+
sinh2_x = x.times(x);
|
|
977
|
+
x = x.times(d5.plus(sinh2_x.times(d16.times(sinh2_x).plus(d20))));
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
Ctor.precision = pr;
|
|
981
|
+
Ctor.rounding = rm;
|
|
982
|
+
return finalise(x, pr, rm, true);
|
|
983
|
+
};
|
|
984
|
+
P.hyperbolicTangent = P.tanh = function() {
|
|
985
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
986
|
+
if (!x.isFinite()) return new Ctor(x.s);
|
|
987
|
+
if (x.isZero()) return new Ctor(x);
|
|
988
|
+
pr = Ctor.precision;
|
|
989
|
+
rm = Ctor.rounding;
|
|
990
|
+
Ctor.precision = pr + 7;
|
|
991
|
+
Ctor.rounding = 1;
|
|
992
|
+
return divide(x.sinh(), x.cosh(), Ctor.precision = pr, Ctor.rounding = rm);
|
|
993
|
+
};
|
|
994
|
+
P.inverseCosine = P.acos = function() {
|
|
995
|
+
var x = this, Ctor = x.constructor, k = x.abs().cmp(1), pr = Ctor.precision, rm = Ctor.rounding;
|
|
996
|
+
if (k !== -1) return k === 0 ? x.isNeg() ? getPi(Ctor, pr, rm) : new Ctor(0) : new Ctor(NaN);
|
|
997
|
+
if (x.isZero()) return getPi(Ctor, pr + 4, rm).times(.5);
|
|
998
|
+
Ctor.precision = pr + 6;
|
|
999
|
+
Ctor.rounding = 1;
|
|
1000
|
+
x = new Ctor(1).minus(x).div(x.plus(1)).sqrt().atan();
|
|
1001
|
+
Ctor.precision = pr;
|
|
1002
|
+
Ctor.rounding = rm;
|
|
1003
|
+
return x.times(2);
|
|
1004
|
+
};
|
|
1005
|
+
P.inverseHyperbolicCosine = P.acosh = function() {
|
|
1006
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
1007
|
+
if (x.lte(1)) return new Ctor(x.eq(1) ? 0 : NaN);
|
|
1008
|
+
if (!x.isFinite()) return new Ctor(x);
|
|
1009
|
+
pr = Ctor.precision;
|
|
1010
|
+
rm = Ctor.rounding;
|
|
1011
|
+
Ctor.precision = pr + Math.max(Math.abs(x.e), x.sd()) + 4;
|
|
1012
|
+
Ctor.rounding = 1;
|
|
1013
|
+
external = false;
|
|
1014
|
+
x = x.times(x).minus(1).sqrt().plus(x);
|
|
1015
|
+
external = true;
|
|
1016
|
+
Ctor.precision = pr;
|
|
1017
|
+
Ctor.rounding = rm;
|
|
1018
|
+
return x.ln();
|
|
1019
|
+
};
|
|
1020
|
+
P.inverseHyperbolicSine = P.asinh = function() {
|
|
1021
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
1022
|
+
if (!x.isFinite() || x.isZero()) return new Ctor(x);
|
|
1023
|
+
pr = Ctor.precision;
|
|
1024
|
+
rm = Ctor.rounding;
|
|
1025
|
+
Ctor.precision = pr + 2 * Math.max(Math.abs(x.e), x.sd()) + 6;
|
|
1026
|
+
Ctor.rounding = 1;
|
|
1027
|
+
external = false;
|
|
1028
|
+
x = x.times(x).plus(1).sqrt().plus(x);
|
|
1029
|
+
external = true;
|
|
1030
|
+
Ctor.precision = pr;
|
|
1031
|
+
Ctor.rounding = rm;
|
|
1032
|
+
return x.ln();
|
|
1033
|
+
};
|
|
1034
|
+
P.inverseHyperbolicTangent = P.atanh = function() {
|
|
1035
|
+
var pr, rm, wpr, xsd, x = this, Ctor = x.constructor;
|
|
1036
|
+
if (!x.isFinite()) return new Ctor(NaN);
|
|
1037
|
+
if (x.e >= 0) return new Ctor(x.abs().eq(1) ? x.s / 0 : x.isZero() ? x : NaN);
|
|
1038
|
+
pr = Ctor.precision;
|
|
1039
|
+
rm = Ctor.rounding;
|
|
1040
|
+
xsd = x.sd();
|
|
1041
|
+
if (Math.max(xsd, pr) < 2 * -x.e - 1) return finalise(new Ctor(x), pr, rm, true);
|
|
1042
|
+
Ctor.precision = wpr = xsd - x.e;
|
|
1043
|
+
x = divide(x.plus(1), new Ctor(1).minus(x), wpr + pr, 1);
|
|
1044
|
+
Ctor.precision = pr + 4;
|
|
1045
|
+
Ctor.rounding = 1;
|
|
1046
|
+
x = x.ln();
|
|
1047
|
+
Ctor.precision = pr;
|
|
1048
|
+
Ctor.rounding = rm;
|
|
1049
|
+
return x.times(.5);
|
|
1050
|
+
};
|
|
1051
|
+
P.inverseSine = P.asin = function() {
|
|
1052
|
+
var halfPi, k, pr, rm, x = this, Ctor = x.constructor;
|
|
1053
|
+
if (x.isZero()) return new Ctor(x);
|
|
1054
|
+
k = x.abs().cmp(1);
|
|
1055
|
+
pr = Ctor.precision;
|
|
1056
|
+
rm = Ctor.rounding;
|
|
1057
|
+
if (k !== -1) {
|
|
1058
|
+
if (k === 0) {
|
|
1059
|
+
halfPi = getPi(Ctor, pr + 4, rm).times(.5);
|
|
1060
|
+
halfPi.s = x.s;
|
|
1061
|
+
return halfPi;
|
|
1062
|
+
}
|
|
1063
|
+
return new Ctor(NaN);
|
|
1064
|
+
}
|
|
1065
|
+
Ctor.precision = pr + 6;
|
|
1066
|
+
Ctor.rounding = 1;
|
|
1067
|
+
x = x.div(new Ctor(1).minus(x.times(x)).sqrt().plus(1)).atan();
|
|
1068
|
+
Ctor.precision = pr;
|
|
1069
|
+
Ctor.rounding = rm;
|
|
1070
|
+
return x.times(2);
|
|
1071
|
+
};
|
|
1072
|
+
P.inverseTangent = P.atan = function() {
|
|
1073
|
+
var i, j, k, n, px, t, r, wpr, x2, x = this, Ctor = x.constructor, pr = Ctor.precision, rm = Ctor.rounding;
|
|
1074
|
+
if (!x.isFinite()) {
|
|
1075
|
+
if (!x.s) return new Ctor(NaN);
|
|
1076
|
+
if (pr + 4 <= PI_PRECISION) {
|
|
1077
|
+
r = getPi(Ctor, pr + 4, rm).times(.5);
|
|
1078
|
+
r.s = x.s;
|
|
1079
|
+
return r;
|
|
1080
|
+
}
|
|
1081
|
+
} else if (x.isZero()) return new Ctor(x);
|
|
1082
|
+
else if (x.abs().eq(1) && pr + 4 <= PI_PRECISION) {
|
|
1083
|
+
r = getPi(Ctor, pr + 4, rm).times(.25);
|
|
1084
|
+
r.s = x.s;
|
|
1085
|
+
return r;
|
|
1086
|
+
}
|
|
1087
|
+
Ctor.precision = wpr = pr + 10;
|
|
1088
|
+
Ctor.rounding = 1;
|
|
1089
|
+
k = Math.min(28, wpr / LOG_BASE + 2 | 0);
|
|
1090
|
+
for (i = k; i; --i) x = x.div(x.times(x).plus(1).sqrt().plus(1));
|
|
1091
|
+
external = false;
|
|
1092
|
+
j = Math.ceil(wpr / LOG_BASE);
|
|
1093
|
+
n = 1;
|
|
1094
|
+
x2 = x.times(x);
|
|
1095
|
+
r = new Ctor(x);
|
|
1096
|
+
px = x;
|
|
1097
|
+
for (; i !== -1;) {
|
|
1098
|
+
px = px.times(x2);
|
|
1099
|
+
t = r.minus(px.div(n += 2));
|
|
1100
|
+
px = px.times(x2);
|
|
1101
|
+
r = t.plus(px.div(n += 2));
|
|
1102
|
+
if (r.d[j] !== void 0) for (i = j; r.d[i] === t.d[i] && i--;);
|
|
1103
|
+
}
|
|
1104
|
+
if (k) r = r.times(2 << k - 1);
|
|
1105
|
+
external = true;
|
|
1106
|
+
return finalise(r, Ctor.precision = pr, Ctor.rounding = rm, true);
|
|
1107
|
+
};
|
|
1108
|
+
P.isFinite = function() {
|
|
1109
|
+
return !!this.d;
|
|
1110
|
+
};
|
|
1111
|
+
P.isInteger = P.isInt = function() {
|
|
1112
|
+
return !!this.d && mathfloor(this.e / LOG_BASE) > this.d.length - 2;
|
|
1113
|
+
};
|
|
1114
|
+
P.isNaN = function() {
|
|
1115
|
+
return !this.s;
|
|
1116
|
+
};
|
|
1117
|
+
P.isNegative = P.isNeg = function() {
|
|
1118
|
+
return this.s < 0;
|
|
1119
|
+
};
|
|
1120
|
+
P.isPositive = P.isPos = function() {
|
|
1121
|
+
return this.s > 0;
|
|
1122
|
+
};
|
|
1123
|
+
P.isZero = function() {
|
|
1124
|
+
return !!this.d && this.d[0] === 0;
|
|
1125
|
+
};
|
|
1126
|
+
P.lessThan = P.lt = function(y) {
|
|
1127
|
+
return this.cmp(y) < 0;
|
|
1128
|
+
};
|
|
1129
|
+
P.lessThanOrEqualTo = P.lte = function(y) {
|
|
1130
|
+
return this.cmp(y) < 1;
|
|
1131
|
+
};
|
|
1132
|
+
P.logarithm = P.log = function(base) {
|
|
1133
|
+
var isBase10, d, denominator, k, inf, num, sd, r, arg = this, Ctor = arg.constructor, pr = Ctor.precision, rm = Ctor.rounding, guard = 5;
|
|
1134
|
+
if (base == null) {
|
|
1135
|
+
base = new Ctor(10);
|
|
1136
|
+
isBase10 = true;
|
|
1137
|
+
} else {
|
|
1138
|
+
base = new Ctor(base);
|
|
1139
|
+
d = base.d;
|
|
1140
|
+
if (base.s < 0 || !d || !d[0] || base.eq(1)) return new Ctor(NaN);
|
|
1141
|
+
isBase10 = base.eq(10);
|
|
1142
|
+
}
|
|
1143
|
+
d = arg.d;
|
|
1144
|
+
if (arg.s < 0 || !d || !d[0] || arg.eq(1)) return new Ctor(d && !d[0] ? -1 / 0 : arg.s != 1 ? NaN : d ? 0 : 1 / 0);
|
|
1145
|
+
if (isBase10) {
|
|
1146
|
+
if (d.length > 1) inf = true;
|
|
1147
|
+
else {
|
|
1148
|
+
for (k = d[0]; k % 10 === 0;) k /= 10;
|
|
1149
|
+
inf = k !== 1;
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
external = false;
|
|
1153
|
+
sd = pr + guard;
|
|
1154
|
+
num = naturalLogarithm(arg, sd);
|
|
1155
|
+
denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
|
|
1156
|
+
r = divide(num, denominator, sd, 1);
|
|
1157
|
+
if (checkRoundingDigits(r.d, k = pr, rm)) do {
|
|
1158
|
+
sd += 10;
|
|
1159
|
+
num = naturalLogarithm(arg, sd);
|
|
1160
|
+
denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
|
|
1161
|
+
r = divide(num, denominator, sd, 1);
|
|
1162
|
+
if (!inf) {
|
|
1163
|
+
if (+digitsToString(r.d).slice(k + 1, k + 15) + 1 == 1e14) r = finalise(r, pr + 1, 0);
|
|
1164
|
+
break;
|
|
1165
|
+
}
|
|
1166
|
+
} while (checkRoundingDigits(r.d, k += 10, rm));
|
|
1167
|
+
external = true;
|
|
1168
|
+
return finalise(r, pr, rm);
|
|
1169
|
+
};
|
|
1170
|
+
P.minus = P.sub = function(y) {
|
|
1171
|
+
var d, e, i, j, k, len, pr, rm, xd, xe, xLTy, yd, x = this, Ctor = x.constructor;
|
|
1172
|
+
y = new Ctor(y);
|
|
1173
|
+
if (!x.d || !y.d) {
|
|
1174
|
+
if (!x.s || !y.s) y = new Ctor(NaN);
|
|
1175
|
+
else if (x.d) y.s = -y.s;
|
|
1176
|
+
else y = new Ctor(y.d || x.s !== y.s ? x : NaN);
|
|
1177
|
+
return y;
|
|
1178
|
+
}
|
|
1179
|
+
if (x.s != y.s) {
|
|
1180
|
+
y.s = -y.s;
|
|
1181
|
+
return x.plus(y);
|
|
1182
|
+
}
|
|
1183
|
+
xd = x.d;
|
|
1184
|
+
yd = y.d;
|
|
1185
|
+
pr = Ctor.precision;
|
|
1186
|
+
rm = Ctor.rounding;
|
|
1187
|
+
if (!xd[0] || !yd[0]) {
|
|
1188
|
+
if (yd[0]) y.s = -y.s;
|
|
1189
|
+
else if (xd[0]) y = new Ctor(x);
|
|
1190
|
+
else return new Ctor(rm === 3 ? -0 : 0);
|
|
1191
|
+
return external ? finalise(y, pr, rm) : y;
|
|
1192
|
+
}
|
|
1193
|
+
e = mathfloor(y.e / LOG_BASE);
|
|
1194
|
+
xe = mathfloor(x.e / LOG_BASE);
|
|
1195
|
+
xd = xd.slice();
|
|
1196
|
+
k = xe - e;
|
|
1197
|
+
if (k) {
|
|
1198
|
+
xLTy = k < 0;
|
|
1199
|
+
if (xLTy) {
|
|
1200
|
+
d = xd;
|
|
1201
|
+
k = -k;
|
|
1202
|
+
len = yd.length;
|
|
1203
|
+
} else {
|
|
1204
|
+
d = yd;
|
|
1205
|
+
e = xe;
|
|
1206
|
+
len = xd.length;
|
|
1207
|
+
}
|
|
1208
|
+
i = Math.max(Math.ceil(pr / LOG_BASE), len) + 2;
|
|
1209
|
+
if (k > i) {
|
|
1210
|
+
k = i;
|
|
1211
|
+
d.length = 1;
|
|
1212
|
+
}
|
|
1213
|
+
d.reverse();
|
|
1214
|
+
for (i = k; i--;) d.push(0);
|
|
1215
|
+
d.reverse();
|
|
1216
|
+
} else {
|
|
1217
|
+
i = xd.length;
|
|
1218
|
+
len = yd.length;
|
|
1219
|
+
xLTy = i < len;
|
|
1220
|
+
if (xLTy) len = i;
|
|
1221
|
+
for (i = 0; i < len; i++) if (xd[i] != yd[i]) {
|
|
1222
|
+
xLTy = xd[i] < yd[i];
|
|
1223
|
+
break;
|
|
1224
|
+
}
|
|
1225
|
+
k = 0;
|
|
1226
|
+
}
|
|
1227
|
+
if (xLTy) {
|
|
1228
|
+
d = xd;
|
|
1229
|
+
xd = yd;
|
|
1230
|
+
yd = d;
|
|
1231
|
+
y.s = -y.s;
|
|
1232
|
+
}
|
|
1233
|
+
len = xd.length;
|
|
1234
|
+
for (i = yd.length - len; i > 0; --i) xd[len++] = 0;
|
|
1235
|
+
for (i = yd.length; i > k;) {
|
|
1236
|
+
if (xd[--i] < yd[i]) {
|
|
1237
|
+
for (j = i; j && xd[--j] === 0;) xd[j] = BASE - 1;
|
|
1238
|
+
--xd[j];
|
|
1239
|
+
xd[i] += BASE;
|
|
1240
|
+
}
|
|
1241
|
+
xd[i] -= yd[i];
|
|
1242
|
+
}
|
|
1243
|
+
for (; xd[--len] === 0;) xd.pop();
|
|
1244
|
+
for (; xd[0] === 0; xd.shift()) --e;
|
|
1245
|
+
if (!xd[0]) return new Ctor(rm === 3 ? -0 : 0);
|
|
1246
|
+
y.d = xd;
|
|
1247
|
+
y.e = getBase10Exponent(xd, e);
|
|
1248
|
+
return external ? finalise(y, pr, rm) : y;
|
|
1249
|
+
};
|
|
1250
|
+
P.modulo = P.mod = function(y) {
|
|
1251
|
+
var q, x = this, Ctor = x.constructor;
|
|
1252
|
+
y = new Ctor(y);
|
|
1253
|
+
if (!x.d || !y.s || y.d && !y.d[0]) return new Ctor(NaN);
|
|
1254
|
+
if (!y.d || x.d && !x.d[0]) return finalise(new Ctor(x), Ctor.precision, Ctor.rounding);
|
|
1255
|
+
external = false;
|
|
1256
|
+
if (Ctor.modulo == 9) {
|
|
1257
|
+
q = divide(x, y.abs(), 0, 3, 1);
|
|
1258
|
+
q.s *= y.s;
|
|
1259
|
+
} else q = divide(x, y, 0, Ctor.modulo, 1);
|
|
1260
|
+
q = q.times(y);
|
|
1261
|
+
external = true;
|
|
1262
|
+
return x.minus(q);
|
|
1263
|
+
};
|
|
1264
|
+
P.naturalExponential = P.exp = function() {
|
|
1265
|
+
return naturalExponential(this);
|
|
1266
|
+
};
|
|
1267
|
+
P.naturalLogarithm = P.ln = function() {
|
|
1268
|
+
return naturalLogarithm(this);
|
|
1269
|
+
};
|
|
1270
|
+
P.negated = P.neg = function() {
|
|
1271
|
+
var x = new this.constructor(this);
|
|
1272
|
+
x.s = -x.s;
|
|
1273
|
+
return finalise(x);
|
|
1274
|
+
};
|
|
1275
|
+
P.plus = P.add = function(y) {
|
|
1276
|
+
var carry, d, e, i, k, len, pr, rm, xd, yd, x = this, Ctor = x.constructor;
|
|
1277
|
+
y = new Ctor(y);
|
|
1278
|
+
if (!x.d || !y.d) {
|
|
1279
|
+
if (!x.s || !y.s) y = new Ctor(NaN);
|
|
1280
|
+
else if (!x.d) y = new Ctor(y.d || x.s === y.s ? x : NaN);
|
|
1281
|
+
return y;
|
|
1282
|
+
}
|
|
1283
|
+
if (x.s != y.s) {
|
|
1284
|
+
y.s = -y.s;
|
|
1285
|
+
return x.minus(y);
|
|
1286
|
+
}
|
|
1287
|
+
xd = x.d;
|
|
1288
|
+
yd = y.d;
|
|
1289
|
+
pr = Ctor.precision;
|
|
1290
|
+
rm = Ctor.rounding;
|
|
1291
|
+
if (!xd[0] || !yd[0]) {
|
|
1292
|
+
if (!yd[0]) y = new Ctor(x);
|
|
1293
|
+
return external ? finalise(y, pr, rm) : y;
|
|
1294
|
+
}
|
|
1295
|
+
k = mathfloor(x.e / LOG_BASE);
|
|
1296
|
+
e = mathfloor(y.e / LOG_BASE);
|
|
1297
|
+
xd = xd.slice();
|
|
1298
|
+
i = k - e;
|
|
1299
|
+
if (i) {
|
|
1300
|
+
if (i < 0) {
|
|
1301
|
+
d = xd;
|
|
1302
|
+
i = -i;
|
|
1303
|
+
len = yd.length;
|
|
1304
|
+
} else {
|
|
1305
|
+
d = yd;
|
|
1306
|
+
e = k;
|
|
1307
|
+
len = xd.length;
|
|
1308
|
+
}
|
|
1309
|
+
k = Math.ceil(pr / LOG_BASE);
|
|
1310
|
+
len = k > len ? k + 1 : len + 1;
|
|
1311
|
+
if (i > len) {
|
|
1312
|
+
i = len;
|
|
1313
|
+
d.length = 1;
|
|
1314
|
+
}
|
|
1315
|
+
d.reverse();
|
|
1316
|
+
for (; i--;) d.push(0);
|
|
1317
|
+
d.reverse();
|
|
1318
|
+
}
|
|
1319
|
+
len = xd.length;
|
|
1320
|
+
i = yd.length;
|
|
1321
|
+
if (len - i < 0) {
|
|
1322
|
+
i = len;
|
|
1323
|
+
d = yd;
|
|
1324
|
+
yd = xd;
|
|
1325
|
+
xd = d;
|
|
1326
|
+
}
|
|
1327
|
+
for (carry = 0; i;) {
|
|
1328
|
+
carry = (xd[--i] = xd[i] + yd[i] + carry) / BASE | 0;
|
|
1329
|
+
xd[i] %= BASE;
|
|
1330
|
+
}
|
|
1331
|
+
if (carry) {
|
|
1332
|
+
xd.unshift(carry);
|
|
1333
|
+
++e;
|
|
1334
|
+
}
|
|
1335
|
+
for (len = xd.length; xd[--len] == 0;) xd.pop();
|
|
1336
|
+
y.d = xd;
|
|
1337
|
+
y.e = getBase10Exponent(xd, e);
|
|
1338
|
+
return external ? finalise(y, pr, rm) : y;
|
|
1339
|
+
};
|
|
1340
|
+
P.precision = P.sd = function(z) {
|
|
1341
|
+
var k, x = this;
|
|
1342
|
+
if (z !== void 0 && z !== !!z && z !== 1 && z !== 0) throw Error(invalidArgument + z);
|
|
1343
|
+
if (x.d) {
|
|
1344
|
+
k = getPrecision(x.d);
|
|
1345
|
+
if (z && x.e + 1 > k) k = x.e + 1;
|
|
1346
|
+
} else k = NaN;
|
|
1347
|
+
return k;
|
|
1348
|
+
};
|
|
1349
|
+
P.round = function() {
|
|
1350
|
+
var x = this, Ctor = x.constructor;
|
|
1351
|
+
return finalise(new Ctor(x), x.e + 1, Ctor.rounding);
|
|
1352
|
+
};
|
|
1353
|
+
P.sine = P.sin = function() {
|
|
1354
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
1355
|
+
if (!x.isFinite()) return new Ctor(NaN);
|
|
1356
|
+
if (x.isZero()) return new Ctor(x);
|
|
1357
|
+
pr = Ctor.precision;
|
|
1358
|
+
rm = Ctor.rounding;
|
|
1359
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
|
|
1360
|
+
Ctor.rounding = 1;
|
|
1361
|
+
x = sine(Ctor, toLessThanHalfPi(Ctor, x));
|
|
1362
|
+
Ctor.precision = pr;
|
|
1363
|
+
Ctor.rounding = rm;
|
|
1364
|
+
return finalise(quadrant > 2 ? x.neg() : x, pr, rm, true);
|
|
1365
|
+
};
|
|
1366
|
+
P.squareRoot = P.sqrt = function() {
|
|
1367
|
+
var m, n, sd, r, rep, t, x = this, d = x.d, e = x.e, s = x.s, Ctor = x.constructor;
|
|
1368
|
+
if (s !== 1 || !d || !d[0]) return new Ctor(!s || s < 0 && (!d || d[0]) ? NaN : d ? x : 1 / 0);
|
|
1369
|
+
external = false;
|
|
1370
|
+
s = Math.sqrt(+x);
|
|
1371
|
+
if (s == 0 || s == 1 / 0) {
|
|
1372
|
+
n = digitsToString(d);
|
|
1373
|
+
if ((n.length + e) % 2 == 0) n += "0";
|
|
1374
|
+
s = Math.sqrt(n);
|
|
1375
|
+
e = mathfloor((e + 1) / 2) - (e < 0 || e % 2);
|
|
1376
|
+
if (s == 1 / 0) n = "5e" + e;
|
|
1377
|
+
else {
|
|
1378
|
+
n = s.toExponential();
|
|
1379
|
+
n = n.slice(0, n.indexOf("e") + 1) + e;
|
|
1380
|
+
}
|
|
1381
|
+
r = new Ctor(n);
|
|
1382
|
+
} else r = new Ctor(s.toString());
|
|
1383
|
+
sd = (e = Ctor.precision) + 3;
|
|
1384
|
+
for (;;) {
|
|
1385
|
+
t = r;
|
|
1386
|
+
r = t.plus(divide(x, t, sd + 2, 1)).times(.5);
|
|
1387
|
+
if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
|
|
1388
|
+
n = n.slice(sd - 3, sd + 1);
|
|
1389
|
+
if (n == "9999" || !rep && n == "4999") {
|
|
1390
|
+
if (!rep) {
|
|
1391
|
+
finalise(t, e + 1, 0);
|
|
1392
|
+
if (t.times(t).eq(x)) {
|
|
1393
|
+
r = t;
|
|
1394
|
+
break;
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
sd += 4;
|
|
1398
|
+
rep = 1;
|
|
1399
|
+
} else {
|
|
1400
|
+
if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
|
|
1401
|
+
finalise(r, e + 1, 1);
|
|
1402
|
+
m = !r.times(r).eq(x);
|
|
1403
|
+
}
|
|
1404
|
+
break;
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
external = true;
|
|
1409
|
+
return finalise(r, e, Ctor.rounding, m);
|
|
1410
|
+
};
|
|
1411
|
+
P.tangent = P.tan = function() {
|
|
1412
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
1413
|
+
if (!x.isFinite()) return new Ctor(NaN);
|
|
1414
|
+
if (x.isZero()) return new Ctor(x);
|
|
1415
|
+
pr = Ctor.precision;
|
|
1416
|
+
rm = Ctor.rounding;
|
|
1417
|
+
Ctor.precision = pr + 10;
|
|
1418
|
+
Ctor.rounding = 1;
|
|
1419
|
+
x = x.sin();
|
|
1420
|
+
x.s = 1;
|
|
1421
|
+
x = divide(x, new Ctor(1).minus(x.times(x)).sqrt(), pr + 10, 0);
|
|
1422
|
+
Ctor.precision = pr;
|
|
1423
|
+
Ctor.rounding = rm;
|
|
1424
|
+
return finalise(quadrant == 2 || quadrant == 4 ? x.neg() : x, pr, rm, true);
|
|
1425
|
+
};
|
|
1426
|
+
P.times = P.mul = function(y) {
|
|
1427
|
+
var carry, e, i, k, r, rL, t, xdL, ydL, x = this, Ctor = x.constructor, xd = x.d, yd = (y = new Ctor(y)).d;
|
|
1428
|
+
y.s *= x.s;
|
|
1429
|
+
if (!xd || !xd[0] || !yd || !yd[0]) return new Ctor(!y.s || xd && !xd[0] && !yd || yd && !yd[0] && !xd ? NaN : !xd || !yd ? y.s / 0 : y.s * 0);
|
|
1430
|
+
e = mathfloor(x.e / LOG_BASE) + mathfloor(y.e / LOG_BASE);
|
|
1431
|
+
xdL = xd.length;
|
|
1432
|
+
ydL = yd.length;
|
|
1433
|
+
if (xdL < ydL) {
|
|
1434
|
+
r = xd;
|
|
1435
|
+
xd = yd;
|
|
1436
|
+
yd = r;
|
|
1437
|
+
rL = xdL;
|
|
1438
|
+
xdL = ydL;
|
|
1439
|
+
ydL = rL;
|
|
1440
|
+
}
|
|
1441
|
+
r = [];
|
|
1442
|
+
rL = xdL + ydL;
|
|
1443
|
+
for (i = rL; i--;) r.push(0);
|
|
1444
|
+
for (i = ydL; --i >= 0;) {
|
|
1445
|
+
carry = 0;
|
|
1446
|
+
for (k = xdL + i; k > i;) {
|
|
1447
|
+
t = r[k] + yd[i] * xd[k - i - 1] + carry;
|
|
1448
|
+
r[k--] = t % BASE | 0;
|
|
1449
|
+
carry = t / BASE | 0;
|
|
1450
|
+
}
|
|
1451
|
+
r[k] = (r[k] + carry) % BASE | 0;
|
|
1452
|
+
}
|
|
1453
|
+
for (; !r[--rL];) r.pop();
|
|
1454
|
+
if (carry) ++e;
|
|
1455
|
+
else r.shift();
|
|
1456
|
+
y.d = r;
|
|
1457
|
+
y.e = getBase10Exponent(r, e);
|
|
1458
|
+
return external ? finalise(y, Ctor.precision, Ctor.rounding) : y;
|
|
1459
|
+
};
|
|
1460
|
+
P.toBinary = function(sd, rm) {
|
|
1461
|
+
return toStringBinary(this, 2, sd, rm);
|
|
1462
|
+
};
|
|
1463
|
+
P.toDecimalPlaces = P.toDP = function(dp, rm) {
|
|
1464
|
+
var x = this, Ctor = x.constructor;
|
|
1465
|
+
x = new Ctor(x);
|
|
1466
|
+
if (dp === void 0) return x;
|
|
1467
|
+
checkInt32(dp, 0, MAX_DIGITS);
|
|
1468
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1469
|
+
else checkInt32(rm, 0, 8);
|
|
1470
|
+
return finalise(x, dp + x.e + 1, rm);
|
|
1471
|
+
};
|
|
1472
|
+
P.toExponential = function(dp, rm) {
|
|
1473
|
+
var str, x = this, Ctor = x.constructor;
|
|
1474
|
+
if (dp === void 0) str = finiteToString(x, true);
|
|
1475
|
+
else {
|
|
1476
|
+
checkInt32(dp, 0, MAX_DIGITS);
|
|
1477
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1478
|
+
else checkInt32(rm, 0, 8);
|
|
1479
|
+
x = finalise(new Ctor(x), dp + 1, rm);
|
|
1480
|
+
str = finiteToString(x, true, dp + 1);
|
|
1481
|
+
}
|
|
1482
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
1483
|
+
};
|
|
1484
|
+
P.toFixed = function(dp, rm) {
|
|
1485
|
+
var str, y, x = this, Ctor = x.constructor;
|
|
1486
|
+
if (dp === void 0) str = finiteToString(x);
|
|
1487
|
+
else {
|
|
1488
|
+
checkInt32(dp, 0, MAX_DIGITS);
|
|
1489
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1490
|
+
else checkInt32(rm, 0, 8);
|
|
1491
|
+
y = finalise(new Ctor(x), dp + x.e + 1, rm);
|
|
1492
|
+
str = finiteToString(y, false, dp + y.e + 1);
|
|
1493
|
+
}
|
|
1494
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
1495
|
+
};
|
|
1496
|
+
P.toFraction = function(maxD) {
|
|
1497
|
+
var d, d0, d1, d2, e, k, n, n0, n1, pr, q, r, x = this, xd = x.d, Ctor = x.constructor;
|
|
1498
|
+
if (!xd) return new Ctor(x);
|
|
1499
|
+
n1 = d0 = new Ctor(1);
|
|
1500
|
+
d1 = n0 = new Ctor(0);
|
|
1501
|
+
d = new Ctor(d1);
|
|
1502
|
+
e = d.e = getPrecision(xd) - x.e - 1;
|
|
1503
|
+
k = e % LOG_BASE;
|
|
1504
|
+
d.d[0] = mathpow(10, k < 0 ? LOG_BASE + k : k);
|
|
1505
|
+
if (maxD == null) maxD = e > 0 ? d : n1;
|
|
1506
|
+
else {
|
|
1507
|
+
n = new Ctor(maxD);
|
|
1508
|
+
if (!n.isInt() || n.lt(n1)) throw Error(invalidArgument + n);
|
|
1509
|
+
maxD = n.gt(d) ? e > 0 ? d : n1 : n;
|
|
1510
|
+
}
|
|
1511
|
+
external = false;
|
|
1512
|
+
n = new Ctor(digitsToString(xd));
|
|
1513
|
+
pr = Ctor.precision;
|
|
1514
|
+
Ctor.precision = e = xd.length * LOG_BASE * 2;
|
|
1515
|
+
for (;;) {
|
|
1516
|
+
q = divide(n, d, 0, 1, 1);
|
|
1517
|
+
d2 = d0.plus(q.times(d1));
|
|
1518
|
+
if (d2.cmp(maxD) == 1) break;
|
|
1519
|
+
d0 = d1;
|
|
1520
|
+
d1 = d2;
|
|
1521
|
+
d2 = n1;
|
|
1522
|
+
n1 = n0.plus(q.times(d2));
|
|
1523
|
+
n0 = d2;
|
|
1524
|
+
d2 = d;
|
|
1525
|
+
d = n.minus(q.times(d2));
|
|
1526
|
+
n = d2;
|
|
1527
|
+
}
|
|
1528
|
+
d2 = divide(maxD.minus(d0), d1, 0, 1, 1);
|
|
1529
|
+
n0 = n0.plus(d2.times(n1));
|
|
1530
|
+
d0 = d0.plus(d2.times(d1));
|
|
1531
|
+
n0.s = n1.s = x.s;
|
|
1532
|
+
r = divide(n1, d1, e, 1).minus(x).abs().cmp(divide(n0, d0, e, 1).minus(x).abs()) < 1 ? [n1, d1] : [n0, d0];
|
|
1533
|
+
Ctor.precision = pr;
|
|
1534
|
+
external = true;
|
|
1535
|
+
return r;
|
|
1536
|
+
};
|
|
1537
|
+
P.toHexadecimal = P.toHex = function(sd, rm) {
|
|
1538
|
+
return toStringBinary(this, 16, sd, rm);
|
|
1539
|
+
};
|
|
1540
|
+
P.toNearest = function(y, rm) {
|
|
1541
|
+
var x = this, Ctor = x.constructor;
|
|
1542
|
+
x = new Ctor(x);
|
|
1543
|
+
if (y == null) {
|
|
1544
|
+
if (!x.d) return x;
|
|
1545
|
+
y = new Ctor(1);
|
|
1546
|
+
rm = Ctor.rounding;
|
|
1547
|
+
} else {
|
|
1548
|
+
y = new Ctor(y);
|
|
1549
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1550
|
+
else checkInt32(rm, 0, 8);
|
|
1551
|
+
if (!x.d) return y.s ? x : y;
|
|
1552
|
+
if (!y.d) {
|
|
1553
|
+
if (y.s) y.s = x.s;
|
|
1554
|
+
return y;
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
if (y.d[0]) {
|
|
1558
|
+
external = false;
|
|
1559
|
+
x = divide(x, y, 0, rm, 1).times(y);
|
|
1560
|
+
external = true;
|
|
1561
|
+
finalise(x);
|
|
1562
|
+
} else {
|
|
1563
|
+
y.s = x.s;
|
|
1564
|
+
x = y;
|
|
1565
|
+
}
|
|
1566
|
+
return x;
|
|
1567
|
+
};
|
|
1568
|
+
P.toNumber = function() {
|
|
1569
|
+
return +this;
|
|
1570
|
+
};
|
|
1571
|
+
P.toOctal = function(sd, rm) {
|
|
1572
|
+
return toStringBinary(this, 8, sd, rm);
|
|
1573
|
+
};
|
|
1574
|
+
P.toPower = P.pow = function(y) {
|
|
1575
|
+
var e, k, pr, r, rm, s, x = this, Ctor = x.constructor, yn = +(y = new Ctor(y));
|
|
1576
|
+
if (!x.d || !y.d || !x.d[0] || !y.d[0]) return new Ctor(mathpow(+x, yn));
|
|
1577
|
+
x = new Ctor(x);
|
|
1578
|
+
if (x.eq(1)) return x;
|
|
1579
|
+
pr = Ctor.precision;
|
|
1580
|
+
rm = Ctor.rounding;
|
|
1581
|
+
if (y.eq(1)) return finalise(x, pr, rm);
|
|
1582
|
+
e = mathfloor(y.e / LOG_BASE);
|
|
1583
|
+
if (e >= y.d.length - 1 && (k = yn < 0 ? -yn : yn) <= MAX_SAFE_INTEGER) {
|
|
1584
|
+
r = intPow(Ctor, x, k, pr);
|
|
1585
|
+
return y.s < 0 ? new Ctor(1).div(r) : finalise(r, pr, rm);
|
|
1586
|
+
}
|
|
1587
|
+
s = x.s;
|
|
1588
|
+
if (s < 0) {
|
|
1589
|
+
if (e < y.d.length - 1) return new Ctor(NaN);
|
|
1590
|
+
if ((y.d[e] & 1) == 0) s = 1;
|
|
1591
|
+
if (x.e == 0 && x.d[0] == 1 && x.d.length == 1) {
|
|
1592
|
+
x.s = s;
|
|
1593
|
+
return x;
|
|
1594
|
+
}
|
|
1595
|
+
}
|
|
1596
|
+
k = mathpow(+x, yn);
|
|
1597
|
+
e = k == 0 || !isFinite(k) ? mathfloor(yn * (Math.log("0." + digitsToString(x.d)) / Math.LN10 + x.e + 1)) : new Ctor(k + "").e;
|
|
1598
|
+
if (e > Ctor.maxE + 1 || e < Ctor.minE - 1) return new Ctor(e > 0 ? s / 0 : 0);
|
|
1599
|
+
external = false;
|
|
1600
|
+
Ctor.rounding = x.s = 1;
|
|
1601
|
+
k = Math.min(12, (e + "").length);
|
|
1602
|
+
r = naturalExponential(y.times(naturalLogarithm(x, pr + k)), pr);
|
|
1603
|
+
if (r.d) {
|
|
1604
|
+
r = finalise(r, pr + 5, 1);
|
|
1605
|
+
if (checkRoundingDigits(r.d, pr, rm)) {
|
|
1606
|
+
e = pr + 10;
|
|
1607
|
+
r = finalise(naturalExponential(y.times(naturalLogarithm(x, e + k)), e), e + 5, 1);
|
|
1608
|
+
if (+digitsToString(r.d).slice(pr + 1, pr + 15) + 1 == 1e14) r = finalise(r, pr + 1, 0);
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
r.s = s;
|
|
1612
|
+
external = true;
|
|
1613
|
+
Ctor.rounding = rm;
|
|
1614
|
+
return finalise(r, pr, rm);
|
|
1615
|
+
};
|
|
1616
|
+
P.toPrecision = function(sd, rm) {
|
|
1617
|
+
var str, x = this, Ctor = x.constructor;
|
|
1618
|
+
if (sd === void 0) str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
|
|
1619
|
+
else {
|
|
1620
|
+
checkInt32(sd, 1, MAX_DIGITS);
|
|
1621
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1622
|
+
else checkInt32(rm, 0, 8);
|
|
1623
|
+
x = finalise(new Ctor(x), sd, rm);
|
|
1624
|
+
str = finiteToString(x, sd <= x.e || x.e <= Ctor.toExpNeg, sd);
|
|
1625
|
+
}
|
|
1626
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
1627
|
+
};
|
|
1628
|
+
P.toSignificantDigits = P.toSD = function(sd, rm) {
|
|
1629
|
+
var x = this, Ctor = x.constructor;
|
|
1630
|
+
if (sd === void 0) {
|
|
1631
|
+
sd = Ctor.precision;
|
|
1632
|
+
rm = Ctor.rounding;
|
|
1633
|
+
} else {
|
|
1634
|
+
checkInt32(sd, 1, MAX_DIGITS);
|
|
1635
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1636
|
+
else checkInt32(rm, 0, 8);
|
|
1637
|
+
}
|
|
1638
|
+
return finalise(new Ctor(x), sd, rm);
|
|
1639
|
+
};
|
|
1640
|
+
P.toString = function() {
|
|
1641
|
+
var x = this, Ctor = x.constructor, str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
|
|
1642
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
1643
|
+
};
|
|
1644
|
+
P.truncated = P.trunc = function() {
|
|
1645
|
+
return finalise(new this.constructor(this), this.e + 1, 1);
|
|
1646
|
+
};
|
|
1647
|
+
P.valueOf = P.toJSON = function() {
|
|
1648
|
+
var x = this, Ctor = x.constructor, str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
|
|
1649
|
+
return x.isNeg() ? "-" + str : str;
|
|
1650
|
+
};
|
|
1651
|
+
function digitsToString(d) {
|
|
1652
|
+
var i, k, ws, indexOfLastWord = d.length - 1, str = "", w = d[0];
|
|
1653
|
+
if (indexOfLastWord > 0) {
|
|
1654
|
+
str += w;
|
|
1655
|
+
for (i = 1; i < indexOfLastWord; i++) {
|
|
1656
|
+
ws = d[i] + "";
|
|
1657
|
+
k = LOG_BASE - ws.length;
|
|
1658
|
+
if (k) str += getZeroString(k);
|
|
1659
|
+
str += ws;
|
|
1660
|
+
}
|
|
1661
|
+
w = d[i];
|
|
1662
|
+
ws = w + "";
|
|
1663
|
+
k = LOG_BASE - ws.length;
|
|
1664
|
+
if (k) str += getZeroString(k);
|
|
1665
|
+
} else if (w === 0) return "0";
|
|
1666
|
+
for (; w % 10 === 0;) w /= 10;
|
|
1667
|
+
return str + w;
|
|
1668
|
+
}
|
|
1669
|
+
function checkInt32(i, min, max) {
|
|
1670
|
+
if (i !== ~~i || i < min || i > max) throw Error(invalidArgument + i);
|
|
1671
|
+
}
|
|
1672
|
+
function checkRoundingDigits(d, i, rm, repeating) {
|
|
1673
|
+
var di, k = d[0], r, rd;
|
|
1674
|
+
for (; k >= 10; k /= 10) --i;
|
|
1675
|
+
if (--i < 0) {
|
|
1676
|
+
i += LOG_BASE;
|
|
1677
|
+
di = 0;
|
|
1678
|
+
} else {
|
|
1679
|
+
di = Math.ceil((i + 1) / LOG_BASE);
|
|
1680
|
+
i %= LOG_BASE;
|
|
1681
|
+
}
|
|
1682
|
+
k = mathpow(10, LOG_BASE - i);
|
|
1683
|
+
rd = d[di] % k | 0;
|
|
1684
|
+
if (repeating == null) {
|
|
1685
|
+
if (i < 3) {
|
|
1686
|
+
if (i == 0) rd = rd / 100 | 0;
|
|
1687
|
+
else if (i == 1) rd = rd / 10 | 0;
|
|
1688
|
+
r = rm < 4 && rd == 99999 || rm > 3 && rd == 49999 || rd == 5e4 || rd == 0;
|
|
1689
|
+
} else r = (rm < 4 && rd + 1 == k || rm > 3 && rd + 1 == k / 2) && (d[di + 1] / k / 100 | 0) == mathpow(10, i - 2) - 1 || (rd == k / 2 || rd == 0) && (d[di + 1] / k / 100 | 0) == 0;
|
|
1690
|
+
} else if (i < 4) {
|
|
1691
|
+
if (i == 0) rd = rd / 1e3 | 0;
|
|
1692
|
+
else if (i == 1) rd = rd / 100 | 0;
|
|
1693
|
+
else if (i == 2) rd = rd / 10 | 0;
|
|
1694
|
+
r = (repeating || rm < 4) && rd == 9999 || !repeating && rm > 3 && rd == 4999;
|
|
1695
|
+
} else r = ((repeating || rm < 4) && rd + 1 == k || !repeating && rm > 3 && rd + 1 == k / 2) && (d[di + 1] / k / 1e3 | 0) == mathpow(10, i - 3) - 1;
|
|
1696
|
+
return r;
|
|
1697
|
+
}
|
|
1698
|
+
function convertBase(str, baseIn, baseOut) {
|
|
1699
|
+
var j, arr = [0], arrL, i = 0, strL = str.length;
|
|
1700
|
+
for (; i < strL;) {
|
|
1701
|
+
for (arrL = arr.length; arrL--;) arr[arrL] *= baseIn;
|
|
1702
|
+
arr[0] += NUMERALS.indexOf(str.charAt(i++));
|
|
1703
|
+
for (j = 0; j < arr.length; j++) if (arr[j] > baseOut - 1) {
|
|
1704
|
+
if (arr[j + 1] === void 0) arr[j + 1] = 0;
|
|
1705
|
+
arr[j + 1] += arr[j] / baseOut | 0;
|
|
1706
|
+
arr[j] %= baseOut;
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
return arr.reverse();
|
|
1710
|
+
}
|
|
1711
|
+
function cosine(Ctor, x) {
|
|
1712
|
+
var k, len, y;
|
|
1713
|
+
if (x.isZero()) return x;
|
|
1714
|
+
len = x.d.length;
|
|
1715
|
+
if (len < 32) {
|
|
1716
|
+
k = Math.ceil(len / 3);
|
|
1717
|
+
y = (1 / tinyPow(4, k)).toString();
|
|
1718
|
+
} else {
|
|
1719
|
+
k = 16;
|
|
1720
|
+
y = "2.3283064365386962890625e-10";
|
|
1721
|
+
}
|
|
1722
|
+
Ctor.precision += k;
|
|
1723
|
+
x = taylorSeries(Ctor, 1, x.times(y), new Ctor(1));
|
|
1724
|
+
for (var i = k; i--;) {
|
|
1725
|
+
var cos2x = x.times(x);
|
|
1726
|
+
x = cos2x.times(cos2x).minus(cos2x).times(8).plus(1);
|
|
1727
|
+
}
|
|
1728
|
+
Ctor.precision -= k;
|
|
1729
|
+
return x;
|
|
1730
|
+
}
|
|
1731
|
+
var divide = (function() {
|
|
1732
|
+
function multiplyInteger(x, k, base) {
|
|
1733
|
+
var temp, carry = 0, i = x.length;
|
|
1734
|
+
for (x = x.slice(); i--;) {
|
|
1735
|
+
temp = x[i] * k + carry;
|
|
1736
|
+
x[i] = temp % base | 0;
|
|
1737
|
+
carry = temp / base | 0;
|
|
1738
|
+
}
|
|
1739
|
+
if (carry) x.unshift(carry);
|
|
1740
|
+
return x;
|
|
1741
|
+
}
|
|
1742
|
+
function compare(a, b, aL, bL) {
|
|
1743
|
+
var i, r;
|
|
1744
|
+
if (aL != bL) r = aL > bL ? 1 : -1;
|
|
1745
|
+
else for (i = r = 0; i < aL; i++) if (a[i] != b[i]) {
|
|
1746
|
+
r = a[i] > b[i] ? 1 : -1;
|
|
1747
|
+
break;
|
|
1748
|
+
}
|
|
1749
|
+
return r;
|
|
1750
|
+
}
|
|
1751
|
+
function subtract(a, b, aL, base) {
|
|
1752
|
+
var i = 0;
|
|
1753
|
+
for (; aL--;) {
|
|
1754
|
+
a[aL] -= i;
|
|
1755
|
+
i = a[aL] < b[aL] ? 1 : 0;
|
|
1756
|
+
a[aL] = i * base + a[aL] - b[aL];
|
|
1757
|
+
}
|
|
1758
|
+
for (; !a[0] && a.length > 1;) a.shift();
|
|
1759
|
+
}
|
|
1760
|
+
return function(x, y, pr, rm, dp, base) {
|
|
1761
|
+
var cmp, e, i, k, logBase, more, prod, prodL, q, qd, rem, remL, rem0, sd, t, xi, xL, yd0, yL, yz, Ctor = x.constructor, sign = x.s == y.s ? 1 : -1, xd = x.d, yd = y.d;
|
|
1762
|
+
if (!xd || !xd[0] || !yd || !yd[0]) return new Ctor(!x.s || !y.s || (xd ? yd && xd[0] == yd[0] : !yd) ? NaN : xd && xd[0] == 0 || !yd ? sign * 0 : sign / 0);
|
|
1763
|
+
if (base) {
|
|
1764
|
+
logBase = 1;
|
|
1765
|
+
e = x.e - y.e;
|
|
1766
|
+
} else {
|
|
1767
|
+
base = BASE;
|
|
1768
|
+
logBase = LOG_BASE;
|
|
1769
|
+
e = mathfloor(x.e / logBase) - mathfloor(y.e / logBase);
|
|
1770
|
+
}
|
|
1771
|
+
yL = yd.length;
|
|
1772
|
+
xL = xd.length;
|
|
1773
|
+
q = new Ctor(sign);
|
|
1774
|
+
qd = q.d = [];
|
|
1775
|
+
for (i = 0; yd[i] == (xd[i] || 0); i++);
|
|
1776
|
+
if (yd[i] > (xd[i] || 0)) e--;
|
|
1777
|
+
if (pr == null) {
|
|
1778
|
+
sd = pr = Ctor.precision;
|
|
1779
|
+
rm = Ctor.rounding;
|
|
1780
|
+
} else if (dp) sd = pr + (x.e - y.e) + 1;
|
|
1781
|
+
else sd = pr;
|
|
1782
|
+
if (sd < 0) {
|
|
1783
|
+
qd.push(1);
|
|
1784
|
+
more = true;
|
|
1785
|
+
} else {
|
|
1786
|
+
sd = sd / logBase + 2 | 0;
|
|
1787
|
+
i = 0;
|
|
1788
|
+
if (yL == 1) {
|
|
1789
|
+
k = 0;
|
|
1790
|
+
yd = yd[0];
|
|
1791
|
+
sd++;
|
|
1792
|
+
for (; (i < xL || k) && sd--; i++) {
|
|
1793
|
+
t = k * base + (xd[i] || 0);
|
|
1794
|
+
qd[i] = t / yd | 0;
|
|
1795
|
+
k = t % yd | 0;
|
|
1796
|
+
}
|
|
1797
|
+
more = k || i < xL;
|
|
1798
|
+
} else {
|
|
1799
|
+
k = base / (yd[0] + 1) | 0;
|
|
1800
|
+
if (k > 1) {
|
|
1801
|
+
yd = multiplyInteger(yd, k, base);
|
|
1802
|
+
xd = multiplyInteger(xd, k, base);
|
|
1803
|
+
yL = yd.length;
|
|
1804
|
+
xL = xd.length;
|
|
1805
|
+
}
|
|
1806
|
+
xi = yL;
|
|
1807
|
+
rem = xd.slice(0, yL);
|
|
1808
|
+
remL = rem.length;
|
|
1809
|
+
for (; remL < yL;) rem[remL++] = 0;
|
|
1810
|
+
yz = yd.slice();
|
|
1811
|
+
yz.unshift(0);
|
|
1812
|
+
yd0 = yd[0];
|
|
1813
|
+
if (yd[1] >= base / 2) ++yd0;
|
|
1814
|
+
do {
|
|
1815
|
+
k = 0;
|
|
1816
|
+
cmp = compare(yd, rem, yL, remL);
|
|
1817
|
+
if (cmp < 0) {
|
|
1818
|
+
rem0 = rem[0];
|
|
1819
|
+
if (yL != remL) rem0 = rem0 * base + (rem[1] || 0);
|
|
1820
|
+
k = rem0 / yd0 | 0;
|
|
1821
|
+
if (k > 1) {
|
|
1822
|
+
if (k >= base) k = base - 1;
|
|
1823
|
+
prod = multiplyInteger(yd, k, base);
|
|
1824
|
+
prodL = prod.length;
|
|
1825
|
+
remL = rem.length;
|
|
1826
|
+
cmp = compare(prod, rem, prodL, remL);
|
|
1827
|
+
if (cmp == 1) {
|
|
1828
|
+
k--;
|
|
1829
|
+
subtract(prod, yL < prodL ? yz : yd, prodL, base);
|
|
1830
|
+
}
|
|
1831
|
+
} else {
|
|
1832
|
+
if (k == 0) cmp = k = 1;
|
|
1833
|
+
prod = yd.slice();
|
|
1834
|
+
}
|
|
1835
|
+
prodL = prod.length;
|
|
1836
|
+
if (prodL < remL) prod.unshift(0);
|
|
1837
|
+
subtract(rem, prod, remL, base);
|
|
1838
|
+
if (cmp == -1) {
|
|
1839
|
+
remL = rem.length;
|
|
1840
|
+
cmp = compare(yd, rem, yL, remL);
|
|
1841
|
+
if (cmp < 1) {
|
|
1842
|
+
k++;
|
|
1843
|
+
subtract(rem, yL < remL ? yz : yd, remL, base);
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
remL = rem.length;
|
|
1847
|
+
} else if (cmp === 0) {
|
|
1848
|
+
k++;
|
|
1849
|
+
rem = [0];
|
|
1850
|
+
}
|
|
1851
|
+
qd[i++] = k;
|
|
1852
|
+
if (cmp && rem[0]) rem[remL++] = xd[xi] || 0;
|
|
1853
|
+
else {
|
|
1854
|
+
rem = [xd[xi]];
|
|
1855
|
+
remL = 1;
|
|
1856
|
+
}
|
|
1857
|
+
} while ((xi++ < xL || rem[0] !== void 0) && sd--);
|
|
1858
|
+
more = rem[0] !== void 0;
|
|
1859
|
+
}
|
|
1860
|
+
if (!qd[0]) qd.shift();
|
|
1861
|
+
}
|
|
1862
|
+
if (logBase == 1) {
|
|
1863
|
+
q.e = e;
|
|
1864
|
+
inexact = more;
|
|
1865
|
+
} else {
|
|
1866
|
+
for (i = 1, k = qd[0]; k >= 10; k /= 10) i++;
|
|
1867
|
+
q.e = i + e * logBase - 1;
|
|
1868
|
+
finalise(q, dp ? pr + q.e + 1 : pr, rm, more);
|
|
1869
|
+
}
|
|
1870
|
+
return q;
|
|
1871
|
+
};
|
|
1872
|
+
})();
|
|
1873
|
+
function finalise(x, sd, rm, isTruncated) {
|
|
1874
|
+
var digits, i, j, k, rd, roundUp, w, xd, xdi, Ctor = x.constructor;
|
|
1875
|
+
out: if (sd != null) {
|
|
1876
|
+
xd = x.d;
|
|
1877
|
+
if (!xd) return x;
|
|
1878
|
+
for (digits = 1, k = xd[0]; k >= 10; k /= 10) digits++;
|
|
1879
|
+
i = sd - digits;
|
|
1880
|
+
if (i < 0) {
|
|
1881
|
+
i += LOG_BASE;
|
|
1882
|
+
j = sd;
|
|
1883
|
+
w = xd[xdi = 0];
|
|
1884
|
+
rd = w / mathpow(10, digits - j - 1) % 10 | 0;
|
|
1885
|
+
} else {
|
|
1886
|
+
xdi = Math.ceil((i + 1) / LOG_BASE);
|
|
1887
|
+
k = xd.length;
|
|
1888
|
+
if (xdi >= k) {
|
|
1889
|
+
if (isTruncated) {
|
|
1890
|
+
for (; k++ <= xdi;) xd.push(0);
|
|
1891
|
+
w = rd = 0;
|
|
1892
|
+
digits = 1;
|
|
1893
|
+
i %= LOG_BASE;
|
|
1894
|
+
j = i - LOG_BASE + 1;
|
|
1895
|
+
} else break out;
|
|
1896
|
+
} else {
|
|
1897
|
+
w = k = xd[xdi];
|
|
1898
|
+
for (digits = 1; k >= 10; k /= 10) digits++;
|
|
1899
|
+
i %= LOG_BASE;
|
|
1900
|
+
j = i - LOG_BASE + digits;
|
|
1901
|
+
rd = j < 0 ? 0 : w / mathpow(10, digits - j - 1) % 10 | 0;
|
|
1902
|
+
}
|
|
1903
|
+
}
|
|
1904
|
+
isTruncated = isTruncated || sd < 0 || xd[xdi + 1] !== void 0 || (j < 0 ? w : w % mathpow(10, digits - j - 1));
|
|
1905
|
+
roundUp = rm < 4 ? (rd || isTruncated) && (rm == 0 || rm == (x.s < 0 ? 3 : 2)) : rd > 5 || rd == 5 && (rm == 4 || isTruncated || rm == 6 && (i > 0 ? j > 0 ? w / mathpow(10, digits - j) : 0 : xd[xdi - 1]) % 10 & 1 || rm == (x.s < 0 ? 8 : 7));
|
|
1906
|
+
if (sd < 1 || !xd[0]) {
|
|
1907
|
+
xd.length = 0;
|
|
1908
|
+
if (roundUp) {
|
|
1909
|
+
sd -= x.e + 1;
|
|
1910
|
+
xd[0] = mathpow(10, (LOG_BASE - sd % LOG_BASE) % LOG_BASE);
|
|
1911
|
+
x.e = -sd || 0;
|
|
1912
|
+
} else xd[0] = x.e = 0;
|
|
1913
|
+
return x;
|
|
1914
|
+
}
|
|
1915
|
+
if (i == 0) {
|
|
1916
|
+
xd.length = xdi;
|
|
1917
|
+
k = 1;
|
|
1918
|
+
xdi--;
|
|
1919
|
+
} else {
|
|
1920
|
+
xd.length = xdi + 1;
|
|
1921
|
+
k = mathpow(10, LOG_BASE - i);
|
|
1922
|
+
xd[xdi] = j > 0 ? (w / mathpow(10, digits - j) % mathpow(10, j) | 0) * k : 0;
|
|
1923
|
+
}
|
|
1924
|
+
if (roundUp) for (;;) if (xdi == 0) {
|
|
1925
|
+
for (i = 1, j = xd[0]; j >= 10; j /= 10) i++;
|
|
1926
|
+
j = xd[0] += k;
|
|
1927
|
+
for (k = 1; j >= 10; j /= 10) k++;
|
|
1928
|
+
if (i != k) {
|
|
1929
|
+
x.e++;
|
|
1930
|
+
if (xd[0] == BASE) xd[0] = 1;
|
|
1931
|
+
}
|
|
1932
|
+
break;
|
|
1933
|
+
} else {
|
|
1934
|
+
xd[xdi] += k;
|
|
1935
|
+
if (xd[xdi] != BASE) break;
|
|
1936
|
+
xd[xdi--] = 0;
|
|
1937
|
+
k = 1;
|
|
1938
|
+
}
|
|
1939
|
+
for (i = xd.length; xd[--i] === 0;) xd.pop();
|
|
1940
|
+
}
|
|
1941
|
+
if (external) {
|
|
1942
|
+
if (x.e > Ctor.maxE) {
|
|
1943
|
+
x.d = null;
|
|
1944
|
+
x.e = NaN;
|
|
1945
|
+
} else if (x.e < Ctor.minE) {
|
|
1946
|
+
x.e = 0;
|
|
1947
|
+
x.d = [0];
|
|
1948
|
+
}
|
|
1949
|
+
}
|
|
1950
|
+
return x;
|
|
1951
|
+
}
|
|
1952
|
+
function finiteToString(x, isExp, sd) {
|
|
1953
|
+
if (!x.isFinite()) return nonFiniteToString(x);
|
|
1954
|
+
var k, e = x.e, str = digitsToString(x.d), len = str.length;
|
|
1955
|
+
if (isExp) {
|
|
1956
|
+
if (sd && (k = sd - len) > 0) str = str.charAt(0) + "." + str.slice(1) + getZeroString(k);
|
|
1957
|
+
else if (len > 1) str = str.charAt(0) + "." + str.slice(1);
|
|
1958
|
+
str = str + (x.e < 0 ? "e" : "e+") + x.e;
|
|
1959
|
+
} else if (e < 0) {
|
|
1960
|
+
str = "0." + getZeroString(-e - 1) + str;
|
|
1961
|
+
if (sd && (k = sd - len) > 0) str += getZeroString(k);
|
|
1962
|
+
} else if (e >= len) {
|
|
1963
|
+
str += getZeroString(e + 1 - len);
|
|
1964
|
+
if (sd && (k = sd - e - 1) > 0) str = str + "." + getZeroString(k);
|
|
1965
|
+
} else {
|
|
1966
|
+
if ((k = e + 1) < len) str = str.slice(0, k) + "." + str.slice(k);
|
|
1967
|
+
if (sd && (k = sd - len) > 0) {
|
|
1968
|
+
if (e + 1 === len) str += ".";
|
|
1969
|
+
str += getZeroString(k);
|
|
1970
|
+
}
|
|
1971
|
+
}
|
|
1972
|
+
return str;
|
|
1973
|
+
}
|
|
1974
|
+
function getBase10Exponent(digits, e) {
|
|
1975
|
+
var w = digits[0];
|
|
1976
|
+
for (e *= LOG_BASE; w >= 10; w /= 10) e++;
|
|
1977
|
+
return e;
|
|
1978
|
+
}
|
|
1979
|
+
function getLn10(Ctor, sd, pr) {
|
|
1980
|
+
if (sd > LN10_PRECISION) {
|
|
1981
|
+
external = true;
|
|
1982
|
+
if (pr) Ctor.precision = pr;
|
|
1983
|
+
throw Error(precisionLimitExceeded);
|
|
1984
|
+
}
|
|
1985
|
+
return finalise(new Ctor(LN10), sd, 1, true);
|
|
1986
|
+
}
|
|
1987
|
+
function getPi(Ctor, sd, rm) {
|
|
1988
|
+
if (sd > PI_PRECISION) throw Error(precisionLimitExceeded);
|
|
1989
|
+
return finalise(new Ctor(PI), sd, rm, true);
|
|
1990
|
+
}
|
|
1991
|
+
function getPrecision(digits) {
|
|
1992
|
+
var w = digits.length - 1, len = w * LOG_BASE + 1;
|
|
1993
|
+
w = digits[w];
|
|
1994
|
+
if (w) {
|
|
1995
|
+
for (; w % 10 == 0; w /= 10) len--;
|
|
1996
|
+
for (w = digits[0]; w >= 10; w /= 10) len++;
|
|
1997
|
+
}
|
|
1998
|
+
return len;
|
|
1999
|
+
}
|
|
2000
|
+
function getZeroString(k) {
|
|
2001
|
+
var zs = "";
|
|
2002
|
+
for (; k--;) zs += "0";
|
|
2003
|
+
return zs;
|
|
2004
|
+
}
|
|
2005
|
+
function intPow(Ctor, x, n, pr) {
|
|
2006
|
+
var isTruncated, r = new Ctor(1), k = Math.ceil(pr / LOG_BASE + 4);
|
|
2007
|
+
external = false;
|
|
2008
|
+
for (;;) {
|
|
2009
|
+
if (n % 2) {
|
|
2010
|
+
r = r.times(x);
|
|
2011
|
+
if (truncate(r.d, k)) isTruncated = true;
|
|
2012
|
+
}
|
|
2013
|
+
n = mathfloor(n / 2);
|
|
2014
|
+
if (n === 0) {
|
|
2015
|
+
n = r.d.length - 1;
|
|
2016
|
+
if (isTruncated && r.d[n] === 0) ++r.d[n];
|
|
2017
|
+
break;
|
|
2018
|
+
}
|
|
2019
|
+
x = x.times(x);
|
|
2020
|
+
truncate(x.d, k);
|
|
2021
|
+
}
|
|
2022
|
+
external = true;
|
|
2023
|
+
return r;
|
|
2024
|
+
}
|
|
2025
|
+
function isOdd(n) {
|
|
2026
|
+
return n.d[n.d.length - 1] & 1;
|
|
2027
|
+
}
|
|
2028
|
+
function maxOrMin(Ctor, args, n) {
|
|
2029
|
+
var k, y, x = new Ctor(args[0]), i = 0;
|
|
2030
|
+
for (; ++i < args.length;) {
|
|
2031
|
+
y = new Ctor(args[i]);
|
|
2032
|
+
if (!y.s) {
|
|
2033
|
+
x = y;
|
|
2034
|
+
break;
|
|
2035
|
+
}
|
|
2036
|
+
k = x.cmp(y);
|
|
2037
|
+
if (k === n || k === 0 && x.s === n) x = y;
|
|
2038
|
+
}
|
|
2039
|
+
return x;
|
|
2040
|
+
}
|
|
2041
|
+
function naturalExponential(x, sd) {
|
|
2042
|
+
var denominator, guard, j, pow, sum, t, wpr, rep = 0, i = 0, k = 0, Ctor = x.constructor, rm = Ctor.rounding, pr = Ctor.precision;
|
|
2043
|
+
if (!x.d || !x.d[0] || x.e > 17) return new Ctor(x.d ? !x.d[0] ? 1 : x.s < 0 ? 0 : 1 / 0 : x.s ? x.s < 0 ? 0 : x : NaN);
|
|
2044
|
+
if (sd == null) {
|
|
2045
|
+
external = false;
|
|
2046
|
+
wpr = pr;
|
|
2047
|
+
} else wpr = sd;
|
|
2048
|
+
t = new Ctor(.03125);
|
|
2049
|
+
while (x.e > -2) {
|
|
2050
|
+
x = x.times(t);
|
|
2051
|
+
k += 5;
|
|
2052
|
+
}
|
|
2053
|
+
guard = Math.log(mathpow(2, k)) / Math.LN10 * 2 + 5 | 0;
|
|
2054
|
+
wpr += guard;
|
|
2055
|
+
denominator = pow = sum = new Ctor(1);
|
|
2056
|
+
Ctor.precision = wpr;
|
|
2057
|
+
for (;;) {
|
|
2058
|
+
pow = finalise(pow.times(x), wpr, 1);
|
|
2059
|
+
denominator = denominator.times(++i);
|
|
2060
|
+
t = sum.plus(divide(pow, denominator, wpr, 1));
|
|
2061
|
+
if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) {
|
|
2062
|
+
j = k;
|
|
2063
|
+
while (j--) sum = finalise(sum.times(sum), wpr, 1);
|
|
2064
|
+
if (sd == null) {
|
|
2065
|
+
if (rep < 3 && checkRoundingDigits(sum.d, wpr - guard, rm, rep)) {
|
|
2066
|
+
Ctor.precision = wpr += 10;
|
|
2067
|
+
denominator = pow = t = new Ctor(1);
|
|
2068
|
+
i = 0;
|
|
2069
|
+
rep++;
|
|
2070
|
+
} else return finalise(sum, Ctor.precision = pr, rm, external = true);
|
|
2071
|
+
} else {
|
|
2072
|
+
Ctor.precision = pr;
|
|
2073
|
+
return sum;
|
|
2074
|
+
}
|
|
2075
|
+
}
|
|
2076
|
+
sum = t;
|
|
2077
|
+
}
|
|
2078
|
+
}
|
|
2079
|
+
function naturalLogarithm(y, sd) {
|
|
2080
|
+
var c, c0, denominator, e, numerator, rep, sum, t, wpr, x1, x2, n = 1, guard = 10, x = y, xd = x.d, Ctor = x.constructor, rm = Ctor.rounding, pr = Ctor.precision;
|
|
2081
|
+
if (x.s < 0 || !xd || !xd[0] || !x.e && xd[0] == 1 && xd.length == 1) return new Ctor(xd && !xd[0] ? -1 / 0 : x.s != 1 ? NaN : xd ? 0 : x);
|
|
2082
|
+
if (sd == null) {
|
|
2083
|
+
external = false;
|
|
2084
|
+
wpr = pr;
|
|
2085
|
+
} else wpr = sd;
|
|
2086
|
+
Ctor.precision = wpr += guard;
|
|
2087
|
+
c = digitsToString(xd);
|
|
2088
|
+
c0 = c.charAt(0);
|
|
2089
|
+
if (Math.abs(e = x.e) < 15e14) {
|
|
2090
|
+
while (c0 < 7 && c0 != 1 || c0 == 1 && c.charAt(1) > 3) {
|
|
2091
|
+
x = x.times(y);
|
|
2092
|
+
c = digitsToString(x.d);
|
|
2093
|
+
c0 = c.charAt(0);
|
|
2094
|
+
n++;
|
|
2095
|
+
}
|
|
2096
|
+
e = x.e;
|
|
2097
|
+
if (c0 > 1) {
|
|
2098
|
+
x = new Ctor("0." + c);
|
|
2099
|
+
e++;
|
|
2100
|
+
} else x = new Ctor(c0 + "." + c.slice(1));
|
|
2101
|
+
} else {
|
|
2102
|
+
t = getLn10(Ctor, wpr + 2, pr).times(e + "");
|
|
2103
|
+
x = naturalLogarithm(new Ctor(c0 + "." + c.slice(1)), wpr - guard).plus(t);
|
|
2104
|
+
Ctor.precision = pr;
|
|
2105
|
+
return sd == null ? finalise(x, pr, rm, external = true) : x;
|
|
2106
|
+
}
|
|
2107
|
+
x1 = x;
|
|
2108
|
+
sum = numerator = x = divide(x.minus(1), x.plus(1), wpr, 1);
|
|
2109
|
+
x2 = finalise(x.times(x), wpr, 1);
|
|
2110
|
+
denominator = 3;
|
|
2111
|
+
for (;;) {
|
|
2112
|
+
numerator = finalise(numerator.times(x2), wpr, 1);
|
|
2113
|
+
t = sum.plus(divide(numerator, new Ctor(denominator), wpr, 1));
|
|
2114
|
+
if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) {
|
|
2115
|
+
sum = sum.times(2);
|
|
2116
|
+
if (e !== 0) sum = sum.plus(getLn10(Ctor, wpr + 2, pr).times(e + ""));
|
|
2117
|
+
sum = divide(sum, new Ctor(n), wpr, 1);
|
|
2118
|
+
if (sd == null) {
|
|
2119
|
+
if (checkRoundingDigits(sum.d, wpr - guard, rm, rep)) {
|
|
2120
|
+
Ctor.precision = wpr += guard;
|
|
2121
|
+
t = numerator = x = divide(x1.minus(1), x1.plus(1), wpr, 1);
|
|
2122
|
+
x2 = finalise(x.times(x), wpr, 1);
|
|
2123
|
+
denominator = rep = 1;
|
|
2124
|
+
} else return finalise(sum, Ctor.precision = pr, rm, external = true);
|
|
2125
|
+
} else {
|
|
2126
|
+
Ctor.precision = pr;
|
|
2127
|
+
return sum;
|
|
2128
|
+
}
|
|
2129
|
+
}
|
|
2130
|
+
sum = t;
|
|
2131
|
+
denominator += 2;
|
|
2132
|
+
}
|
|
2133
|
+
}
|
|
2134
|
+
function nonFiniteToString(x) {
|
|
2135
|
+
return String(x.s * x.s / 0);
|
|
2136
|
+
}
|
|
2137
|
+
function parseDecimal(x, str) {
|
|
2138
|
+
var e, i, len;
|
|
2139
|
+
if ((e = str.indexOf(".")) > -1) str = str.replace(".", "");
|
|
2140
|
+
if ((i = str.search(/e/i)) > 0) {
|
|
2141
|
+
if (e < 0) e = i;
|
|
2142
|
+
e += +str.slice(i + 1);
|
|
2143
|
+
str = str.substring(0, i);
|
|
2144
|
+
} else if (e < 0) e = str.length;
|
|
2145
|
+
for (i = 0; str.charCodeAt(i) === 48; i++);
|
|
2146
|
+
for (len = str.length; str.charCodeAt(len - 1) === 48; --len);
|
|
2147
|
+
str = str.slice(i, len);
|
|
2148
|
+
if (str) {
|
|
2149
|
+
len -= i;
|
|
2150
|
+
x.e = e = e - i - 1;
|
|
2151
|
+
x.d = [];
|
|
2152
|
+
i = (e + 1) % LOG_BASE;
|
|
2153
|
+
if (e < 0) i += LOG_BASE;
|
|
2154
|
+
if (i < len) {
|
|
2155
|
+
if (i) x.d.push(+str.slice(0, i));
|
|
2156
|
+
for (len -= LOG_BASE; i < len;) x.d.push(+str.slice(i, i += LOG_BASE));
|
|
2157
|
+
str = str.slice(i);
|
|
2158
|
+
i = LOG_BASE - str.length;
|
|
2159
|
+
} else i -= len;
|
|
2160
|
+
for (; i--;) str += "0";
|
|
2161
|
+
x.d.push(+str);
|
|
2162
|
+
if (external) {
|
|
2163
|
+
if (x.e > x.constructor.maxE) {
|
|
2164
|
+
x.d = null;
|
|
2165
|
+
x.e = NaN;
|
|
2166
|
+
} else if (x.e < x.constructor.minE) {
|
|
2167
|
+
x.e = 0;
|
|
2168
|
+
x.d = [0];
|
|
2169
|
+
}
|
|
2170
|
+
}
|
|
2171
|
+
} else {
|
|
2172
|
+
x.e = 0;
|
|
2173
|
+
x.d = [0];
|
|
2174
|
+
}
|
|
2175
|
+
return x;
|
|
2176
|
+
}
|
|
2177
|
+
function parseOther(x, str) {
|
|
2178
|
+
var base, Ctor, divisor, i, isFloat, len, p, xd, xe;
|
|
2179
|
+
if (str.indexOf("_") > -1) {
|
|
2180
|
+
str = str.replace(/(\d)_(?=\d)/g, "$1");
|
|
2181
|
+
if (isDecimal.test(str)) return parseDecimal(x, str);
|
|
2182
|
+
} else if (str === "Infinity" || str === "NaN") {
|
|
2183
|
+
if (!+str) x.s = NaN;
|
|
2184
|
+
x.e = NaN;
|
|
2185
|
+
x.d = null;
|
|
2186
|
+
return x;
|
|
2187
|
+
}
|
|
2188
|
+
if (isHex.test(str)) {
|
|
2189
|
+
base = 16;
|
|
2190
|
+
str = str.toLowerCase();
|
|
2191
|
+
} else if (isBinary.test(str)) base = 2;
|
|
2192
|
+
else if (isOctal.test(str)) base = 8;
|
|
2193
|
+
else throw Error(invalidArgument + str);
|
|
2194
|
+
i = str.search(/p/i);
|
|
2195
|
+
if (i > 0) {
|
|
2196
|
+
p = +str.slice(i + 1);
|
|
2197
|
+
str = str.substring(2, i);
|
|
2198
|
+
} else str = str.slice(2);
|
|
2199
|
+
i = str.indexOf(".");
|
|
2200
|
+
isFloat = i >= 0;
|
|
2201
|
+
Ctor = x.constructor;
|
|
2202
|
+
if (isFloat) {
|
|
2203
|
+
str = str.replace(".", "");
|
|
2204
|
+
len = str.length;
|
|
2205
|
+
i = len - i;
|
|
2206
|
+
divisor = intPow(Ctor, new Ctor(base), i, i * 2);
|
|
2207
|
+
}
|
|
2208
|
+
xd = convertBase(str, base, BASE);
|
|
2209
|
+
xe = xd.length - 1;
|
|
2210
|
+
for (i = xe; xd[i] === 0; --i) xd.pop();
|
|
2211
|
+
if (i < 0) return new Ctor(x.s * 0);
|
|
2212
|
+
x.e = getBase10Exponent(xd, xe);
|
|
2213
|
+
x.d = xd;
|
|
2214
|
+
external = false;
|
|
2215
|
+
if (isFloat) x = divide(x, divisor, len * 4);
|
|
2216
|
+
if (p) x = x.times(Math.abs(p) < 54 ? mathpow(2, p) : Decimal.pow(2, p));
|
|
2217
|
+
external = true;
|
|
2218
|
+
return x;
|
|
2219
|
+
}
|
|
2220
|
+
function sine(Ctor, x) {
|
|
2221
|
+
var k, len = x.d.length;
|
|
2222
|
+
if (len < 3) return x.isZero() ? x : taylorSeries(Ctor, 2, x, x);
|
|
2223
|
+
k = 1.4 * Math.sqrt(len);
|
|
2224
|
+
k = k > 16 ? 16 : k | 0;
|
|
2225
|
+
x = x.times(1 / tinyPow(5, k));
|
|
2226
|
+
x = taylorSeries(Ctor, 2, x, x);
|
|
2227
|
+
var sin2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20);
|
|
2228
|
+
for (; k--;) {
|
|
2229
|
+
sin2_x = x.times(x);
|
|
2230
|
+
x = x.times(d5.plus(sin2_x.times(d16.times(sin2_x).minus(d20))));
|
|
2231
|
+
}
|
|
2232
|
+
return x;
|
|
2233
|
+
}
|
|
2234
|
+
function taylorSeries(Ctor, n, x, y, isHyperbolic) {
|
|
2235
|
+
var j, t, u, x2, i = 1, pr = Ctor.precision, k = Math.ceil(pr / LOG_BASE);
|
|
2236
|
+
external = false;
|
|
2237
|
+
x2 = x.times(x);
|
|
2238
|
+
u = new Ctor(y);
|
|
2239
|
+
for (;;) {
|
|
2240
|
+
t = divide(u.times(x2), new Ctor(n++ * n++), pr, 1);
|
|
2241
|
+
u = isHyperbolic ? y.plus(t) : y.minus(t);
|
|
2242
|
+
y = divide(t.times(x2), new Ctor(n++ * n++), pr, 1);
|
|
2243
|
+
t = u.plus(y);
|
|
2244
|
+
if (t.d[k] !== void 0) {
|
|
2245
|
+
for (j = k; t.d[j] === u.d[j] && j--;);
|
|
2246
|
+
if (j == -1) break;
|
|
2247
|
+
}
|
|
2248
|
+
j = u;
|
|
2249
|
+
u = y;
|
|
2250
|
+
y = t;
|
|
2251
|
+
t = j;
|
|
2252
|
+
i++;
|
|
2253
|
+
}
|
|
2254
|
+
external = true;
|
|
2255
|
+
t.d.length = k + 1;
|
|
2256
|
+
return t;
|
|
2257
|
+
}
|
|
2258
|
+
function tinyPow(b, e) {
|
|
2259
|
+
var n = b;
|
|
2260
|
+
while (--e) n *= b;
|
|
2261
|
+
return n;
|
|
2262
|
+
}
|
|
2263
|
+
function toLessThanHalfPi(Ctor, x) {
|
|
2264
|
+
var t, isNeg = x.s < 0, pi = getPi(Ctor, Ctor.precision, 1), halfPi = pi.times(.5);
|
|
2265
|
+
x = x.abs();
|
|
2266
|
+
if (x.lte(halfPi)) {
|
|
2267
|
+
quadrant = isNeg ? 4 : 1;
|
|
2268
|
+
return x;
|
|
2269
|
+
}
|
|
2270
|
+
t = x.divToInt(pi);
|
|
2271
|
+
if (t.isZero()) quadrant = isNeg ? 3 : 2;
|
|
2272
|
+
else {
|
|
2273
|
+
x = x.minus(t.times(pi));
|
|
2274
|
+
if (x.lte(halfPi)) {
|
|
2275
|
+
quadrant = isOdd(t) ? isNeg ? 2 : 3 : isNeg ? 4 : 1;
|
|
2276
|
+
return x;
|
|
2277
|
+
}
|
|
2278
|
+
quadrant = isOdd(t) ? isNeg ? 1 : 4 : isNeg ? 3 : 2;
|
|
2279
|
+
}
|
|
2280
|
+
return x.minus(pi).abs();
|
|
2281
|
+
}
|
|
2282
|
+
function toStringBinary(x, baseOut, sd, rm) {
|
|
2283
|
+
var base, e, i, k, len, roundUp, str, xd, y, Ctor = x.constructor, isExp = sd !== void 0;
|
|
2284
|
+
if (isExp) {
|
|
2285
|
+
checkInt32(sd, 1, MAX_DIGITS);
|
|
2286
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
2287
|
+
else checkInt32(rm, 0, 8);
|
|
2288
|
+
} else {
|
|
2289
|
+
sd = Ctor.precision;
|
|
2290
|
+
rm = Ctor.rounding;
|
|
2291
|
+
}
|
|
2292
|
+
if (!x.isFinite()) str = nonFiniteToString(x);
|
|
2293
|
+
else {
|
|
2294
|
+
str = finiteToString(x);
|
|
2295
|
+
i = str.indexOf(".");
|
|
2296
|
+
if (isExp) {
|
|
2297
|
+
base = 2;
|
|
2298
|
+
if (baseOut == 16) sd = sd * 4 - 3;
|
|
2299
|
+
else if (baseOut == 8) sd = sd * 3 - 2;
|
|
2300
|
+
} else base = baseOut;
|
|
2301
|
+
if (i >= 0) {
|
|
2302
|
+
str = str.replace(".", "");
|
|
2303
|
+
y = new Ctor(1);
|
|
2304
|
+
y.e = str.length - i;
|
|
2305
|
+
y.d = convertBase(finiteToString(y), 10, base);
|
|
2306
|
+
y.e = y.d.length;
|
|
2307
|
+
}
|
|
2308
|
+
xd = convertBase(str, 10, base);
|
|
2309
|
+
e = len = xd.length;
|
|
2310
|
+
for (; xd[--len] == 0;) xd.pop();
|
|
2311
|
+
if (!xd[0]) str = isExp ? "0p+0" : "0";
|
|
2312
|
+
else {
|
|
2313
|
+
if (i < 0) e--;
|
|
2314
|
+
else {
|
|
2315
|
+
x = new Ctor(x);
|
|
2316
|
+
x.d = xd;
|
|
2317
|
+
x.e = e;
|
|
2318
|
+
x = divide(x, y, sd, rm, 0, base);
|
|
2319
|
+
xd = x.d;
|
|
2320
|
+
e = x.e;
|
|
2321
|
+
roundUp = inexact;
|
|
2322
|
+
}
|
|
2323
|
+
i = xd[sd];
|
|
2324
|
+
k = base / 2;
|
|
2325
|
+
roundUp = roundUp || xd[sd + 1] !== void 0;
|
|
2326
|
+
roundUp = rm < 4 ? (i !== void 0 || roundUp) && (rm === 0 || rm === (x.s < 0 ? 3 : 2)) : i > k || i === k && (rm === 4 || roundUp || rm === 6 && xd[sd - 1] & 1 || rm === (x.s < 0 ? 8 : 7));
|
|
2327
|
+
xd.length = sd;
|
|
2328
|
+
if (roundUp) for (; ++xd[--sd] > base - 1;) {
|
|
2329
|
+
xd[sd] = 0;
|
|
2330
|
+
if (!sd) {
|
|
2331
|
+
++e;
|
|
2332
|
+
xd.unshift(1);
|
|
2333
|
+
}
|
|
2334
|
+
}
|
|
2335
|
+
for (len = xd.length; !xd[len - 1]; --len);
|
|
2336
|
+
for (i = 0, str = ""; i < len; i++) str += NUMERALS.charAt(xd[i]);
|
|
2337
|
+
if (isExp) {
|
|
2338
|
+
if (len > 1) {
|
|
2339
|
+
if (baseOut == 16 || baseOut == 8) {
|
|
2340
|
+
i = baseOut == 16 ? 4 : 3;
|
|
2341
|
+
for (--len; len % i; len++) str += "0";
|
|
2342
|
+
xd = convertBase(str, base, baseOut);
|
|
2343
|
+
for (len = xd.length; !xd[len - 1]; --len);
|
|
2344
|
+
for (i = 1, str = "1."; i < len; i++) str += NUMERALS.charAt(xd[i]);
|
|
2345
|
+
} else str = str.charAt(0) + "." + str.slice(1);
|
|
2346
|
+
}
|
|
2347
|
+
str = str + (e < 0 ? "p" : "p+") + e;
|
|
2348
|
+
} else if (e < 0) {
|
|
2349
|
+
for (; ++e;) str = "0" + str;
|
|
2350
|
+
str = "0." + str;
|
|
2351
|
+
} else if (++e > len) for (e -= len; e--;) str += "0";
|
|
2352
|
+
else if (e < len) str = str.slice(0, e) + "." + str.slice(e);
|
|
2353
|
+
}
|
|
2354
|
+
str = (baseOut == 16 ? "0x" : baseOut == 2 ? "0b" : baseOut == 8 ? "0o" : "") + str;
|
|
2355
|
+
}
|
|
2356
|
+
return x.s < 0 ? "-" + str : str;
|
|
2357
|
+
}
|
|
2358
|
+
function truncate(arr, len) {
|
|
2359
|
+
if (arr.length > len) {
|
|
2360
|
+
arr.length = len;
|
|
2361
|
+
return true;
|
|
2362
|
+
}
|
|
2363
|
+
}
|
|
2364
|
+
function abs(x) {
|
|
2365
|
+
return new this(x).abs();
|
|
2366
|
+
}
|
|
2367
|
+
function acos(x) {
|
|
2368
|
+
return new this(x).acos();
|
|
2369
|
+
}
|
|
2370
|
+
function acosh(x) {
|
|
2371
|
+
return new this(x).acosh();
|
|
2372
|
+
}
|
|
2373
|
+
function add(x, y) {
|
|
2374
|
+
return new this(x).plus(y);
|
|
2375
|
+
}
|
|
2376
|
+
function asin(x) {
|
|
2377
|
+
return new this(x).asin();
|
|
2378
|
+
}
|
|
2379
|
+
function asinh(x) {
|
|
2380
|
+
return new this(x).asinh();
|
|
2381
|
+
}
|
|
2382
|
+
function atan(x) {
|
|
2383
|
+
return new this(x).atan();
|
|
2384
|
+
}
|
|
2385
|
+
function atanh(x) {
|
|
2386
|
+
return new this(x).atanh();
|
|
2387
|
+
}
|
|
2388
|
+
function atan2(y, x) {
|
|
2389
|
+
y = new this(y);
|
|
2390
|
+
x = new this(x);
|
|
2391
|
+
var r, pr = this.precision, rm = this.rounding, wpr = pr + 4;
|
|
2392
|
+
if (!y.s || !x.s) r = new this(NaN);
|
|
2393
|
+
else if (!y.d && !x.d) {
|
|
2394
|
+
r = getPi(this, wpr, 1).times(x.s > 0 ? .25 : .75);
|
|
2395
|
+
r.s = y.s;
|
|
2396
|
+
} else if (!x.d || y.isZero()) {
|
|
2397
|
+
r = x.s < 0 ? getPi(this, pr, rm) : new this(0);
|
|
2398
|
+
r.s = y.s;
|
|
2399
|
+
} else if (!y.d || x.isZero()) {
|
|
2400
|
+
r = getPi(this, wpr, 1).times(.5);
|
|
2401
|
+
r.s = y.s;
|
|
2402
|
+
} else if (x.s < 0) {
|
|
2403
|
+
this.precision = wpr;
|
|
2404
|
+
this.rounding = 1;
|
|
2405
|
+
r = this.atan(divide(y, x, wpr, 1));
|
|
2406
|
+
x = getPi(this, wpr, 1);
|
|
2407
|
+
this.precision = pr;
|
|
2408
|
+
this.rounding = rm;
|
|
2409
|
+
r = y.s < 0 ? r.minus(x) : r.plus(x);
|
|
2410
|
+
} else r = this.atan(divide(y, x, wpr, 1));
|
|
2411
|
+
return r;
|
|
2412
|
+
}
|
|
2413
|
+
function cbrt(x) {
|
|
2414
|
+
return new this(x).cbrt();
|
|
2415
|
+
}
|
|
2416
|
+
function ceil(x) {
|
|
2417
|
+
return finalise(x = new this(x), x.e + 1, 2);
|
|
2418
|
+
}
|
|
2419
|
+
function clamp(x, min, max) {
|
|
2420
|
+
return new this(x).clamp(min, max);
|
|
2421
|
+
}
|
|
2422
|
+
function config(obj) {
|
|
2423
|
+
if (!obj || typeof obj !== "object") throw Error(decimalError + "Object expected");
|
|
2424
|
+
var i, p, v, useDefaults = obj.defaults === true, ps = [
|
|
2425
|
+
"precision",
|
|
2426
|
+
1,
|
|
2427
|
+
MAX_DIGITS,
|
|
2428
|
+
"rounding",
|
|
2429
|
+
0,
|
|
2430
|
+
8,
|
|
2431
|
+
"toExpNeg",
|
|
2432
|
+
-EXP_LIMIT,
|
|
2433
|
+
0,
|
|
2434
|
+
"toExpPos",
|
|
2435
|
+
0,
|
|
2436
|
+
EXP_LIMIT,
|
|
2437
|
+
"maxE",
|
|
2438
|
+
0,
|
|
2439
|
+
EXP_LIMIT,
|
|
2440
|
+
"minE",
|
|
2441
|
+
-EXP_LIMIT,
|
|
2442
|
+
0,
|
|
2443
|
+
"modulo",
|
|
2444
|
+
0,
|
|
2445
|
+
9
|
|
2446
|
+
];
|
|
2447
|
+
for (i = 0; i < ps.length; i += 3) {
|
|
2448
|
+
if (p = ps[i], useDefaults) this[p] = DEFAULTS[p];
|
|
2449
|
+
if ((v = obj[p]) !== void 0) {
|
|
2450
|
+
if (mathfloor(v) === v && v >= ps[i + 1] && v <= ps[i + 2]) this[p] = v;
|
|
2451
|
+
else throw Error(invalidArgument + p + ": " + v);
|
|
2452
|
+
}
|
|
2453
|
+
}
|
|
2454
|
+
if (p = "crypto", useDefaults) this[p] = DEFAULTS[p];
|
|
2455
|
+
if ((v = obj[p]) !== void 0) {
|
|
2456
|
+
if (v === true || v === false || v === 0 || v === 1) {
|
|
2457
|
+
if (v) {
|
|
2458
|
+
if (typeof crypto != "undefined" && crypto && (crypto.getRandomValues || crypto.randomBytes)) this[p] = true;
|
|
2459
|
+
else throw Error(cryptoUnavailable);
|
|
2460
|
+
} else this[p] = false;
|
|
2461
|
+
} else throw Error(invalidArgument + p + ": " + v);
|
|
2462
|
+
}
|
|
2463
|
+
return this;
|
|
2464
|
+
}
|
|
2465
|
+
function cos(x) {
|
|
2466
|
+
return new this(x).cos();
|
|
2467
|
+
}
|
|
2468
|
+
function cosh(x) {
|
|
2469
|
+
return new this(x).cosh();
|
|
2470
|
+
}
|
|
2471
|
+
function clone(obj) {
|
|
2472
|
+
var i, p, ps;
|
|
2473
|
+
function Decimal(v) {
|
|
2474
|
+
var e, i, t, x = this;
|
|
2475
|
+
if (!(x instanceof Decimal)) return new Decimal(v);
|
|
2476
|
+
x.constructor = Decimal;
|
|
2477
|
+
if (isDecimalInstance(v)) {
|
|
2478
|
+
x.s = v.s;
|
|
2479
|
+
if (external) {
|
|
2480
|
+
if (!v.d || v.e > Decimal.maxE) {
|
|
2481
|
+
x.e = NaN;
|
|
2482
|
+
x.d = null;
|
|
2483
|
+
} else if (v.e < Decimal.minE) {
|
|
2484
|
+
x.e = 0;
|
|
2485
|
+
x.d = [0];
|
|
2486
|
+
} else {
|
|
2487
|
+
x.e = v.e;
|
|
2488
|
+
x.d = v.d.slice();
|
|
2489
|
+
}
|
|
2490
|
+
} else {
|
|
2491
|
+
x.e = v.e;
|
|
2492
|
+
x.d = v.d ? v.d.slice() : v.d;
|
|
2493
|
+
}
|
|
2494
|
+
return;
|
|
2495
|
+
}
|
|
2496
|
+
t = typeof v;
|
|
2497
|
+
if (t === "number") {
|
|
2498
|
+
if (v === 0) {
|
|
2499
|
+
x.s = 1 / v < 0 ? -1 : 1;
|
|
2500
|
+
x.e = 0;
|
|
2501
|
+
x.d = [0];
|
|
2502
|
+
return;
|
|
2503
|
+
}
|
|
2504
|
+
if (v < 0) {
|
|
2505
|
+
v = -v;
|
|
2506
|
+
x.s = -1;
|
|
2507
|
+
} else x.s = 1;
|
|
2508
|
+
if (v === ~~v && v < 1e7) {
|
|
2509
|
+
for (e = 0, i = v; i >= 10; i /= 10) e++;
|
|
2510
|
+
if (external) {
|
|
2511
|
+
if (e > Decimal.maxE) {
|
|
2512
|
+
x.e = NaN;
|
|
2513
|
+
x.d = null;
|
|
2514
|
+
} else if (e < Decimal.minE) {
|
|
2515
|
+
x.e = 0;
|
|
2516
|
+
x.d = [0];
|
|
2517
|
+
} else {
|
|
2518
|
+
x.e = e;
|
|
2519
|
+
x.d = [v];
|
|
2520
|
+
}
|
|
2521
|
+
} else {
|
|
2522
|
+
x.e = e;
|
|
2523
|
+
x.d = [v];
|
|
2524
|
+
}
|
|
2525
|
+
return;
|
|
2526
|
+
}
|
|
2527
|
+
if (v * 0 !== 0) {
|
|
2528
|
+
if (!v) x.s = NaN;
|
|
2529
|
+
x.e = NaN;
|
|
2530
|
+
x.d = null;
|
|
2531
|
+
return;
|
|
2532
|
+
}
|
|
2533
|
+
return parseDecimal(x, v.toString());
|
|
2534
|
+
}
|
|
2535
|
+
if (t === "string") {
|
|
2536
|
+
if ((i = v.charCodeAt(0)) === 45) {
|
|
2537
|
+
v = v.slice(1);
|
|
2538
|
+
x.s = -1;
|
|
2539
|
+
} else {
|
|
2540
|
+
if (i === 43) v = v.slice(1);
|
|
2541
|
+
x.s = 1;
|
|
2542
|
+
}
|
|
2543
|
+
return isDecimal.test(v) ? parseDecimal(x, v) : parseOther(x, v);
|
|
2544
|
+
}
|
|
2545
|
+
if (t === "bigint") {
|
|
2546
|
+
if (v < 0) {
|
|
2547
|
+
v = -v;
|
|
2548
|
+
x.s = -1;
|
|
2549
|
+
} else x.s = 1;
|
|
2550
|
+
return parseDecimal(x, v.toString());
|
|
2551
|
+
}
|
|
2552
|
+
throw Error(invalidArgument + v);
|
|
2553
|
+
}
|
|
2554
|
+
Decimal.prototype = P;
|
|
2555
|
+
Decimal.ROUND_UP = 0;
|
|
2556
|
+
Decimal.ROUND_DOWN = 1;
|
|
2557
|
+
Decimal.ROUND_CEIL = 2;
|
|
2558
|
+
Decimal.ROUND_FLOOR = 3;
|
|
2559
|
+
Decimal.ROUND_HALF_UP = 4;
|
|
2560
|
+
Decimal.ROUND_HALF_DOWN = 5;
|
|
2561
|
+
Decimal.ROUND_HALF_EVEN = 6;
|
|
2562
|
+
Decimal.ROUND_HALF_CEIL = 7;
|
|
2563
|
+
Decimal.ROUND_HALF_FLOOR = 8;
|
|
2564
|
+
Decimal.EUCLID = 9;
|
|
2565
|
+
Decimal.config = Decimal.set = config;
|
|
2566
|
+
Decimal.clone = clone;
|
|
2567
|
+
Decimal.isDecimal = isDecimalInstance;
|
|
2568
|
+
Decimal.abs = abs;
|
|
2569
|
+
Decimal.acos = acos;
|
|
2570
|
+
Decimal.acosh = acosh;
|
|
2571
|
+
Decimal.add = add;
|
|
2572
|
+
Decimal.asin = asin;
|
|
2573
|
+
Decimal.asinh = asinh;
|
|
2574
|
+
Decimal.atan = atan;
|
|
2575
|
+
Decimal.atanh = atanh;
|
|
2576
|
+
Decimal.atan2 = atan2;
|
|
2577
|
+
Decimal.cbrt = cbrt;
|
|
2578
|
+
Decimal.ceil = ceil;
|
|
2579
|
+
Decimal.clamp = clamp;
|
|
2580
|
+
Decimal.cos = cos;
|
|
2581
|
+
Decimal.cosh = cosh;
|
|
2582
|
+
Decimal.div = div;
|
|
2583
|
+
Decimal.exp = exp;
|
|
2584
|
+
Decimal.floor = floor;
|
|
2585
|
+
Decimal.hypot = hypot;
|
|
2586
|
+
Decimal.ln = ln;
|
|
2587
|
+
Decimal.log = log;
|
|
2588
|
+
Decimal.log10 = log10;
|
|
2589
|
+
Decimal.log2 = log2;
|
|
2590
|
+
Decimal.max = max;
|
|
2591
|
+
Decimal.min = min;
|
|
2592
|
+
Decimal.mod = mod;
|
|
2593
|
+
Decimal.mul = mul;
|
|
2594
|
+
Decimal.pow = pow;
|
|
2595
|
+
Decimal.random = random;
|
|
2596
|
+
Decimal.round = round;
|
|
2597
|
+
Decimal.sign = sign;
|
|
2598
|
+
Decimal.sin = sin;
|
|
2599
|
+
Decimal.sinh = sinh;
|
|
2600
|
+
Decimal.sqrt = sqrt;
|
|
2601
|
+
Decimal.sub = sub;
|
|
2602
|
+
Decimal.sum = sum;
|
|
2603
|
+
Decimal.tan = tan;
|
|
2604
|
+
Decimal.tanh = tanh;
|
|
2605
|
+
Decimal.trunc = trunc;
|
|
2606
|
+
if (obj === void 0) obj = {};
|
|
2607
|
+
if (obj) {
|
|
2608
|
+
if (obj.defaults !== true) {
|
|
2609
|
+
ps = [
|
|
2610
|
+
"precision",
|
|
2611
|
+
"rounding",
|
|
2612
|
+
"toExpNeg",
|
|
2613
|
+
"toExpPos",
|
|
2614
|
+
"maxE",
|
|
2615
|
+
"minE",
|
|
2616
|
+
"modulo",
|
|
2617
|
+
"crypto"
|
|
2618
|
+
];
|
|
2619
|
+
for (i = 0; i < ps.length;) if (!obj.hasOwnProperty(p = ps[i++])) obj[p] = this[p];
|
|
2620
|
+
}
|
|
2621
|
+
}
|
|
2622
|
+
Decimal.config(obj);
|
|
2623
|
+
return Decimal;
|
|
2624
|
+
}
|
|
2625
|
+
function div(x, y) {
|
|
2626
|
+
return new this(x).div(y);
|
|
2627
|
+
}
|
|
2628
|
+
function exp(x) {
|
|
2629
|
+
return new this(x).exp();
|
|
2630
|
+
}
|
|
2631
|
+
function floor(x) {
|
|
2632
|
+
return finalise(x = new this(x), x.e + 1, 3);
|
|
2633
|
+
}
|
|
2634
|
+
function hypot() {
|
|
2635
|
+
var i, n, t = new this(0);
|
|
2636
|
+
external = false;
|
|
2637
|
+
for (i = 0; i < arguments.length;) {
|
|
2638
|
+
n = new this(arguments[i++]);
|
|
2639
|
+
if (!n.d) {
|
|
2640
|
+
if (n.s) {
|
|
2641
|
+
external = true;
|
|
2642
|
+
return new this(1 / 0);
|
|
2643
|
+
}
|
|
2644
|
+
t = n;
|
|
2645
|
+
} else if (t.d) t = t.plus(n.times(n));
|
|
2646
|
+
}
|
|
2647
|
+
external = true;
|
|
2648
|
+
return t.sqrt();
|
|
2649
|
+
}
|
|
2650
|
+
function isDecimalInstance(obj) {
|
|
2651
|
+
return obj instanceof Decimal || obj && obj.toStringTag === tag || false;
|
|
2652
|
+
}
|
|
2653
|
+
function ln(x) {
|
|
2654
|
+
return new this(x).ln();
|
|
2655
|
+
}
|
|
2656
|
+
function log(x, y) {
|
|
2657
|
+
return new this(x).log(y);
|
|
2658
|
+
}
|
|
2659
|
+
function log2(x) {
|
|
2660
|
+
return new this(x).log(2);
|
|
2661
|
+
}
|
|
2662
|
+
function log10(x) {
|
|
2663
|
+
return new this(x).log(10);
|
|
2664
|
+
}
|
|
2665
|
+
function max() {
|
|
2666
|
+
return maxOrMin(this, arguments, -1);
|
|
2667
|
+
}
|
|
2668
|
+
function min() {
|
|
2669
|
+
return maxOrMin(this, arguments, 1);
|
|
2670
|
+
}
|
|
2671
|
+
function mod(x, y) {
|
|
2672
|
+
return new this(x).mod(y);
|
|
2673
|
+
}
|
|
2674
|
+
function mul(x, y) {
|
|
2675
|
+
return new this(x).mul(y);
|
|
2676
|
+
}
|
|
2677
|
+
function pow(x, y) {
|
|
2678
|
+
return new this(x).pow(y);
|
|
2679
|
+
}
|
|
2680
|
+
function random(sd) {
|
|
2681
|
+
var d, e, k, n, i = 0, r = new this(1), rd = [];
|
|
2682
|
+
if (sd === void 0) sd = this.precision;
|
|
2683
|
+
else checkInt32(sd, 1, MAX_DIGITS);
|
|
2684
|
+
k = Math.ceil(sd / LOG_BASE);
|
|
2685
|
+
if (!this.crypto) for (; i < k;) rd[i++] = Math.random() * 1e7 | 0;
|
|
2686
|
+
else if (crypto.getRandomValues) {
|
|
2687
|
+
d = crypto.getRandomValues(new Uint32Array(k));
|
|
2688
|
+
for (; i < k;) {
|
|
2689
|
+
n = d[i];
|
|
2690
|
+
if (n >= 429e7) d[i] = crypto.getRandomValues(/* @__PURE__ */ new Uint32Array(1))[0];
|
|
2691
|
+
else rd[i++] = n % 1e7;
|
|
2692
|
+
}
|
|
2693
|
+
} else if (crypto.randomBytes) {
|
|
2694
|
+
d = crypto.randomBytes(k *= 4);
|
|
2695
|
+
for (; i < k;) {
|
|
2696
|
+
n = d[i] + (d[i + 1] << 8) + (d[i + 2] << 16) + ((d[i + 3] & 127) << 24);
|
|
2697
|
+
if (n >= 214e7) crypto.randomBytes(4).copy(d, i);
|
|
2698
|
+
else {
|
|
2699
|
+
rd.push(n % 1e7);
|
|
2700
|
+
i += 4;
|
|
2701
|
+
}
|
|
2702
|
+
}
|
|
2703
|
+
i = k / 4;
|
|
2704
|
+
} else throw Error(cryptoUnavailable);
|
|
2705
|
+
k = rd[--i];
|
|
2706
|
+
sd %= LOG_BASE;
|
|
2707
|
+
if (k && sd) {
|
|
2708
|
+
n = mathpow(10, LOG_BASE - sd);
|
|
2709
|
+
rd[i] = (k / n | 0) * n;
|
|
2710
|
+
}
|
|
2711
|
+
for (; rd[i] === 0; i--) rd.pop();
|
|
2712
|
+
if (i < 0) {
|
|
2713
|
+
e = 0;
|
|
2714
|
+
rd = [0];
|
|
2715
|
+
} else {
|
|
2716
|
+
e = -1;
|
|
2717
|
+
for (; rd[0] === 0; e -= LOG_BASE) rd.shift();
|
|
2718
|
+
for (k = 1, n = rd[0]; n >= 10; n /= 10) k++;
|
|
2719
|
+
if (k < LOG_BASE) e -= LOG_BASE - k;
|
|
2720
|
+
}
|
|
2721
|
+
r.e = e;
|
|
2722
|
+
r.d = rd;
|
|
2723
|
+
return r;
|
|
2724
|
+
}
|
|
2725
|
+
function round(x) {
|
|
2726
|
+
return finalise(x = new this(x), x.e + 1, this.rounding);
|
|
2727
|
+
}
|
|
2728
|
+
function sign(x) {
|
|
2729
|
+
x = new this(x);
|
|
2730
|
+
return x.d ? x.d[0] ? x.s : 0 * x.s : x.s || NaN;
|
|
2731
|
+
}
|
|
2732
|
+
function sin(x) {
|
|
2733
|
+
return new this(x).sin();
|
|
2734
|
+
}
|
|
2735
|
+
function sinh(x) {
|
|
2736
|
+
return new this(x).sinh();
|
|
2737
|
+
}
|
|
2738
|
+
function sqrt(x) {
|
|
2739
|
+
return new this(x).sqrt();
|
|
2740
|
+
}
|
|
2741
|
+
function sub(x, y) {
|
|
2742
|
+
return new this(x).sub(y);
|
|
2743
|
+
}
|
|
2744
|
+
function sum() {
|
|
2745
|
+
var i = 0, args = arguments, x = new this(args[i]);
|
|
2746
|
+
external = false;
|
|
2747
|
+
for (; x.s && ++i < args.length;) x = x.plus(args[i]);
|
|
2748
|
+
external = true;
|
|
2749
|
+
return finalise(x, this.precision, this.rounding);
|
|
2750
|
+
}
|
|
2751
|
+
function tan(x) {
|
|
2752
|
+
return new this(x).tan();
|
|
2753
|
+
}
|
|
2754
|
+
function tanh(x) {
|
|
2755
|
+
return new this(x).tanh();
|
|
2756
|
+
}
|
|
2757
|
+
function trunc(x) {
|
|
2758
|
+
return finalise(x = new this(x), x.e + 1, 1);
|
|
2759
|
+
}
|
|
2760
|
+
P[Symbol.for("nodejs.util.inspect.custom")] = P.toString;
|
|
2761
|
+
P[Symbol.toStringTag] = "Decimal";
|
|
2762
|
+
var Decimal = P.constructor = clone(DEFAULTS);
|
|
2763
|
+
LN10 = new Decimal(LN10);
|
|
2764
|
+
PI = new Decimal(PI);
|
|
2765
|
+
// src/math/HVec3.ts
|
|
2766
|
+
/** Mutable decimal vector. Pass strings to preserve more precision than a JS number. */
|
|
2767
|
+
var HVec3 = class HVec3 {
|
|
2768
|
+
x;
|
|
2769
|
+
y;
|
|
2770
|
+
z;
|
|
2771
|
+
constructor(x = 0, y = 0, z = 0) {
|
|
2772
|
+
this.x = new Decimal(x);
|
|
2773
|
+
this.y = new Decimal(y);
|
|
2774
|
+
this.z = new Decimal(z);
|
|
2775
|
+
if (![
|
|
2776
|
+
this.x,
|
|
2777
|
+
this.y,
|
|
2778
|
+
this.z
|
|
2779
|
+
].every((v) => v.isFinite())) throw new RangeError("Vector components must be finite");
|
|
2780
|
+
}
|
|
2781
|
+
static isVec3(v) {
|
|
2782
|
+
return typeof v === "object" && v !== null && [
|
|
2783
|
+
"x",
|
|
2784
|
+
"y",
|
|
2785
|
+
"z"
|
|
2786
|
+
].every((key) => Decimal.isDecimal(v[key]));
|
|
2787
|
+
}
|
|
2788
|
+
get isVec3() {
|
|
2789
|
+
return true;
|
|
2790
|
+
}
|
|
2791
|
+
static get Up() {
|
|
2792
|
+
return new HVec3(0, 1, 0);
|
|
2793
|
+
}
|
|
2794
|
+
static get Down() {
|
|
2795
|
+
return new HVec3(0, -1, 0);
|
|
2796
|
+
}
|
|
2797
|
+
static get UnitX() {
|
|
2798
|
+
return new HVec3(1, 0, 0);
|
|
2799
|
+
}
|
|
2800
|
+
static get UnitY() {
|
|
2801
|
+
return new HVec3(0, 1, 0);
|
|
2802
|
+
}
|
|
2803
|
+
static get UnitZ() {
|
|
2804
|
+
return new HVec3(0, 0, 1);
|
|
2805
|
+
}
|
|
2806
|
+
set(x, y, z) {
|
|
2807
|
+
const v = new HVec3(x, y, z);
|
|
2808
|
+
this.x = v.x;
|
|
2809
|
+
this.y = v.y;
|
|
2810
|
+
this.z = v.z;
|
|
2811
|
+
return this;
|
|
2812
|
+
}
|
|
2813
|
+
setScalar(s) {
|
|
2814
|
+
return this.set(s, s, s);
|
|
2815
|
+
}
|
|
2816
|
+
setComponent(index, value) {
|
|
2817
|
+
if (!Number.isInteger(index) || index < 0 || index > 2) throw new RangeError("Component index must be 0, 1 or 2");
|
|
2818
|
+
const component = new Decimal(value);
|
|
2819
|
+
if (!component.isFinite()) throw new RangeError("Component must be finite");
|
|
2820
|
+
this[[
|
|
2821
|
+
"x",
|
|
2822
|
+
"y",
|
|
2823
|
+
"z"
|
|
2824
|
+
][index]] = component;
|
|
2825
|
+
return this;
|
|
2826
|
+
}
|
|
2827
|
+
getComponent(index) {
|
|
2828
|
+
if (!Number.isInteger(index) || index < 0 || index > 2) throw new RangeError("Component index must be 0, 1 or 2");
|
|
2829
|
+
return this[[
|
|
2830
|
+
"x",
|
|
2831
|
+
"y",
|
|
2832
|
+
"z"
|
|
2833
|
+
][index]];
|
|
2834
|
+
}
|
|
2835
|
+
copy(v) {
|
|
2836
|
+
return this.set(v.x, v.y, v.z);
|
|
2837
|
+
}
|
|
2838
|
+
clone() {
|
|
2839
|
+
return new HVec3(this.x, this.y, this.z);
|
|
2840
|
+
}
|
|
2841
|
+
add(v, w) {
|
|
2842
|
+
return w ? this.addVecs(v, w) : this.addVecs(this, v);
|
|
2843
|
+
}
|
|
2844
|
+
addVecs(a, b) {
|
|
2845
|
+
return this.set(a.x.add(b.x), a.y.add(b.y), a.z.add(b.z));
|
|
2846
|
+
}
|
|
2847
|
+
sub(v, w) {
|
|
2848
|
+
return w ? this.subVecs(v, w) : this.subVecs(this, v);
|
|
2849
|
+
}
|
|
2850
|
+
subVecs(a, b) {
|
|
2851
|
+
return this.set(a.x.sub(b.x), a.y.sub(b.y), a.z.sub(b.z));
|
|
2852
|
+
}
|
|
2853
|
+
multiplyScalar(s) {
|
|
2854
|
+
return this.set(this.x.mul(s), this.y.mul(s), this.z.mul(s));
|
|
2855
|
+
}
|
|
2856
|
+
divideScalar(s) {
|
|
2857
|
+
if (new Decimal(s).isZero()) throw new RangeError("Division by zero");
|
|
2858
|
+
return this.set(this.x.div(s), this.y.div(s), this.z.div(s));
|
|
2859
|
+
}
|
|
2860
|
+
cross(v, w) {
|
|
2861
|
+
return w ? this.crossVecs(v, w) : this.crossVecs(this, v);
|
|
2862
|
+
}
|
|
2863
|
+
crossVecs(a, b) {
|
|
2864
|
+
return this.set(a.y.mul(b.z).sub(a.z.mul(b.y)), a.z.mul(b.x).sub(a.x.mul(b.z)), a.x.mul(b.y).sub(a.y.mul(b.x)));
|
|
2865
|
+
}
|
|
2866
|
+
dot(v) {
|
|
2867
|
+
return this.x.mul(v.x).add(this.y.mul(v.y)).add(this.z.mul(v.z));
|
|
2868
|
+
}
|
|
2869
|
+
lengthSq() {
|
|
2870
|
+
return this.dot(this);
|
|
2871
|
+
}
|
|
2872
|
+
length() {
|
|
2873
|
+
return this.lengthSq().sqrt();
|
|
2874
|
+
}
|
|
2875
|
+
normalize() {
|
|
2876
|
+
const length = this.length();
|
|
2877
|
+
return length.isZero() ? this : this.divideScalar(length);
|
|
2878
|
+
}
|
|
2879
|
+
lerp(v, alpha) {
|
|
2880
|
+
return this.set(this.x.add(v.x.sub(this.x).mul(alpha)), this.y.add(v.y.sub(this.y).mul(alpha)), this.z.add(v.z.sub(this.z).mul(alpha)));
|
|
2881
|
+
}
|
|
2882
|
+
distanceTo(v) {
|
|
2883
|
+
return this.clone().sub(v).length();
|
|
2884
|
+
}
|
|
2885
|
+
equals(v) {
|
|
2886
|
+
return this.x.eq(v.x) && this.y.eq(v.y) && this.z.eq(v.z);
|
|
2887
|
+
}
|
|
2888
|
+
toArray() {
|
|
2889
|
+
return [
|
|
2890
|
+
this.x,
|
|
2891
|
+
this.y,
|
|
2892
|
+
this.z
|
|
2893
|
+
];
|
|
2894
|
+
}
|
|
2895
|
+
toJSON() {
|
|
2896
|
+
return this.toArray().map((v) => v.toString());
|
|
2897
|
+
}
|
|
2898
|
+
};
|
|
2899
|
+
// src/csg/polygon.ts
|
|
2900
|
+
var Polygon = class Polygon {
|
|
2901
|
+
w = new Decimal(0);
|
|
2902
|
+
normal = new HVec3();
|
|
2903
|
+
vertices;
|
|
2904
|
+
epsilon;
|
|
2905
|
+
constructor(vertices = [], epsilon = "1e-15") {
|
|
2906
|
+
this.epsilon = new Decimal(epsilon);
|
|
2907
|
+
if (!this.epsilon.isFinite() || this.epsilon.isNegative()) throw new RangeError("Invalid CSG tolerance");
|
|
2908
|
+
this.vertices = vertices.map((v) => v.clone());
|
|
2909
|
+
if (vertices.length) {
|
|
2910
|
+
this.calcPlane();
|
|
2911
|
+
if (this.vertices.some((v) => this.normal.dot(v).sub(this.w).abs().gt(this.epsilon))) throw new RangeError("CSG polygon must be planar");
|
|
2912
|
+
for (let i = 0; i < this.vertices.length; i++) {
|
|
2913
|
+
const a = this.vertices[i], b = this.vertices[(i + 1) % this.vertices.length], c = this.vertices[(i + 2) % this.vertices.length];
|
|
2914
|
+
if (b.clone().sub(a).cross(c.clone().sub(b)).dot(this.normal).lt(this.epsilon.negated())) throw new RangeError("CSG polygon must be convex and consistently wound");
|
|
2915
|
+
}
|
|
2916
|
+
}
|
|
2917
|
+
}
|
|
2918
|
+
calcPlane() {
|
|
2919
|
+
if (this.vertices.length < 3) throw new RangeError("A polygon needs at least three vertices");
|
|
2920
|
+
const a = this.vertices[0];
|
|
2921
|
+
for (let i = 1; i + 1 < this.vertices.length; i++) {
|
|
2922
|
+
const n = this.vertices[i].clone().sub(a).cross(this.vertices[i + 1].clone().sub(a));
|
|
2923
|
+
if (!n.lengthSq().isZero()) {
|
|
2924
|
+
this.normal = n.normalize();
|
|
2925
|
+
this.w = this.normal.dot(a);
|
|
2926
|
+
return this;
|
|
2927
|
+
}
|
|
2928
|
+
}
|
|
2929
|
+
throw new RangeError("Degenerate polygon");
|
|
2930
|
+
}
|
|
2931
|
+
flip() {
|
|
2932
|
+
this.normal.multiplyScalar(-1);
|
|
2933
|
+
this.w = this.w.negated();
|
|
2934
|
+
this.vertices.reverse();
|
|
2935
|
+
return this;
|
|
2936
|
+
}
|
|
2937
|
+
classifyVertex(v) {
|
|
2938
|
+
const d = this.normal.dot(v).sub(this.w);
|
|
2939
|
+
return d.lt(this.epsilon.negated()) ? 2 : d.gt(this.epsilon) ? 1 : 0;
|
|
2940
|
+
}
|
|
2941
|
+
classifySide(p) {
|
|
2942
|
+
return p.vertices.reduce((side, v) => side | this.classifyVertex(v), 0);
|
|
2943
|
+
}
|
|
2944
|
+
splitPolygon(p, coplanarFront = [], coplanarBack = [], front = [], back = []) {
|
|
2945
|
+
const side = this.classifySide(p);
|
|
2946
|
+
if (side === 0) (this.normal.dot(p.normal).gt(0) ? coplanarFront : coplanarBack).push(p.clone());
|
|
2947
|
+
else if (side === 1) front.push(p.clone());
|
|
2948
|
+
else if (side === 2) back.push(p.clone());
|
|
2949
|
+
else {
|
|
2950
|
+
const f = [], b = [];
|
|
2951
|
+
for (let i = 0; i < p.vertices.length; i++) {
|
|
2952
|
+
const vi = p.vertices[i], vj = p.vertices[(i + 1) % p.vertices.length];
|
|
2953
|
+
const ti = this.classifyVertex(vi), tj = this.classifyVertex(vj);
|
|
2954
|
+
if (ti !== 2) f.push(vi.clone());
|
|
2955
|
+
if (ti !== 1) b.push(vi.clone());
|
|
2956
|
+
if ((ti | tj) === 3) {
|
|
2957
|
+
const t = this.w.sub(this.normal.dot(vi)).div(this.normal.dot(vj.clone().sub(vi)));
|
|
2958
|
+
const v = vi.clone().lerp(vj, t);
|
|
2959
|
+
f.push(v);
|
|
2960
|
+
b.push(v.clone());
|
|
2961
|
+
}
|
|
2962
|
+
}
|
|
2963
|
+
if (f.length >= 3) front.push(new Polygon(f, p.epsilon));
|
|
2964
|
+
if (b.length >= 3) back.push(new Polygon(b, p.epsilon));
|
|
2965
|
+
}
|
|
2966
|
+
}
|
|
2967
|
+
clone() {
|
|
2968
|
+
return new Polygon(this.vertices, this.epsilon);
|
|
2969
|
+
}
|
|
2970
|
+
};
|
|
2971
|
+
// src/csg/three.ts
|
|
2972
|
+
/** BufferGeometry adapter. Mesh input is baked into world coordinates. */
|
|
2973
|
+
var ThreeBSP = class ThreeBSP {
|
|
2974
|
+
solid;
|
|
2975
|
+
constructor(source) {
|
|
2976
|
+
if (source instanceof BSP) {
|
|
2977
|
+
this.solid = source.clone();
|
|
2978
|
+
return;
|
|
2979
|
+
}
|
|
2980
|
+
const geometry = source instanceof three.Mesh ? (source.updateWorldMatrix(true, false), source.geometry.clone().applyMatrix4(source.matrixWorld)) : source;
|
|
2981
|
+
const position = geometry.getAttribute("position"), index = geometry.getIndex();
|
|
2982
|
+
if (!position || position.itemSize !== 3) throw new RangeError("CSG requires position triangles");
|
|
2983
|
+
const count = index?.count ?? position.count;
|
|
2984
|
+
if (count % 3) throw new RangeError("CSG requires triangle geometry");
|
|
2985
|
+
const faces = [];
|
|
2986
|
+
for (let i = 0; i < count; i += 3) {
|
|
2987
|
+
const vertices = Array.from({ length: 3 }, (_, j) => {
|
|
2988
|
+
const k = index ? index.getX(i + j) : i + j;
|
|
2989
|
+
return new HVec3(position.getX(k), position.getY(k), position.getZ(k));
|
|
2990
|
+
});
|
|
2991
|
+
try {
|
|
2992
|
+
faces.push(new Polygon(vertices));
|
|
2993
|
+
} catch (error) {
|
|
2994
|
+
if (!(error instanceof RangeError)) throw error;
|
|
2995
|
+
}
|
|
2996
|
+
}
|
|
2997
|
+
this.solid = new BSP(faces);
|
|
2998
|
+
if (source instanceof three.Mesh) geometry.dispose();
|
|
2999
|
+
}
|
|
3000
|
+
subtract(other) {
|
|
3001
|
+
return new ThreeBSP(this.solid.sub(other.solid));
|
|
3002
|
+
}
|
|
3003
|
+
union(other) {
|
|
3004
|
+
return new ThreeBSP(this.solid.union(other.solid));
|
|
3005
|
+
}
|
|
3006
|
+
intersect(other) {
|
|
3007
|
+
return new ThreeBSP(this.solid.intersect(other.solid));
|
|
3008
|
+
}
|
|
3009
|
+
toGeometry() {
|
|
3010
|
+
const position = [], normal = [];
|
|
3011
|
+
for (const face of this.solid.toPolygons()) for (let i = 1; i + 1 < face.vertices.length; i++) for (const point of [
|
|
3012
|
+
face.vertices[0],
|
|
3013
|
+
face.vertices[i],
|
|
3014
|
+
face.vertices[i + 1]
|
|
3015
|
+
]) {
|
|
3016
|
+
position.push(...point.toArray().map((v) => v.toNumber()));
|
|
3017
|
+
normal.push(...face.normal.toArray().map((v) => v.toNumber()));
|
|
3018
|
+
}
|
|
3019
|
+
const geometry = new three.BufferGeometry();
|
|
3020
|
+
geometry.setAttribute("position", new three.Float32BufferAttribute(position, 3));
|
|
3021
|
+
geometry.setAttribute("normal", new three.Float32BufferAttribute(normal, 3));
|
|
3022
|
+
geometry.computeBoundingSphere();
|
|
3023
|
+
return geometry;
|
|
3024
|
+
}
|
|
3025
|
+
toMesh(material = new three.MeshNormalMaterial()) {
|
|
3026
|
+
return new three.Mesh(this.toGeometry(), material);
|
|
3027
|
+
}
|
|
3028
|
+
};
|
|
3029
|
+
exports.BreathLight = BreathLight;
|
|
3030
|
+
exports.DiamondShader = DiamondShader;
|
|
3031
|
+
exports.GLView = GLView;
|
|
3032
|
+
exports.ThreeBSP = ThreeBSP;
|
|
3033
|
+
|
|
3034
|
+
//# sourceMappingURL=three.cjs.map
|