-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSphereDist.js
More file actions
137 lines (117 loc) · 3.7 KB
/
SphereDist.js
File metadata and controls
137 lines (117 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// in radians
function TSpherical_Coordinates(col = 0.0, lon = 0.0) {
this.col = col; // colatitude
this.lon = lon; // longitude
}
function Tvector2(x = 0.0, y = 0.0) {
this.x = x;
this.y = y;
}
function Tvector3(x = 0.0, y = 0.0, z = 0.0) {
this.x = x;
this.y = y;
this.z = z;
this.add = function(another) {
this.x += another.x;
this.y += another.y;
this.z += another.z;
}
}
function sqr(x) {
return x * x;
}
function Spherical2Cartesian(p) {
return new Tvector3(
Math.sin(p.col) * Math.cos(p.lon),
Math.sin(p.col) * Math.sin(p.lon),
Math.cos(p.col));
}
function Cartesian2Spherical(p) {
return new TSpherical_Coordinates(
Math.acos(p.z / Math.sqrt(sqr(p.x) + sqr(p.y) + sqr(p.z))),
Math.atan2(p.y, p.x));
}
function SphereDist(N = 100, hemisphere = false, relocating = true) {
this.version = "SphereDist version 0.8.3 by katahiromz";
this.N = N;
this.NP_on_hemisphere = N / 2;
this.hemisphere = hemisphere;
this.relocating = relocating;
this.GSS = [];
this.GetNP_on_hemisphere = function() {
var NP_on_hemisphere = 0;
for (var ith_point = 1; ith_point <= this.N; ++ith_point) {
if (this.GSS[ith_point].col <= Math.PI / 2.0)
break;
++NP_on_hemisphere;
}
this.NP_on_hemisphere = NP_on_hemisphere;
return NP_on_hemisphere;
}
this.relocate = function() {
const five_points = [0xFFFF, 2, 3, 5, 6, 7];
// relocate 1st point
var mean = new Tvector3();
for (var i = 1; i <= 5; ++i) {
var m = five_points[i];
mean.add(Spherical2Cartesian(this.GSS[m]));
}
this.GSS[1] = Cartesian2Spherical(mean);
// relocate Nth point
mean = new Tvector3();
for (var i = 1; i <= 5; ++i) {
var m = five_points[i];
mean.add(Spherical2Cartesian(this.GSS[this.N - m + 1]));
}
this.GSS[this.N] = Cartesian2Spherical(mean);
}
this.generate = function() {
if (this.N < 6) {
throw "N must be >= 6";
}
this.GSS = [];
for (var i = 0; i < this.N + 1; ++i) {
this.GSS.push(new TSpherical_Coordinates());
}
this.GSS[1].lon = 0.0;
this.GSS[1].col = Math.PI;
for (var k = 2; k <= this.N - 1; ++k) {
var hk = -1.0 + 2.0 * (k - 1) / (this.N - 1);
this.GSS[k].col = Math.acos(hk);
this.GSS[k].lon =
this.GSS[k - 1].lon +
3.6 / Math.sqrt(this.N) / Math.sqrt(1.0 - hk * hk);
}
this.GSS[this.N].lon = 0.0;
this.GSS[this.N].col = 0.0;
if (this.hemisphere) {
this.GetNP_on_hemisphere();
}
if (this.relocating) {
this.relocate();
}
}
this.rows = function() {
var vertexes = [];
const last = this.hemisphere ? this.NP_on_hemisphere : this.N;
for (var ith_point = 1; ith_point <= last; ++ith_point) {
var p = Spherical2Cartesian(this.GSS[ith_point]);
vertexes.push([p.x, p.y, -p.z]);
}
return vertexes;
}
this.get_text = function() {
var vertexes = this.rows();
var text = "";
for (var i = 0; i < vertexes.length; ++i) {
text += vertexes[i][0];
text += ", ";
text += vertexes[i][1];
text += ", ";
text += vertexes[i][2];
text += "\n";
}
return text;
}
this.generate();
}