@pirireis/webglobeplugins 0.11.1-alpha → 0.13.0-alpha
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/Math/arc-cdf-points.js +252 -0
- package/Math/{arc-generate-points.js → arc-generate-points copy.js } +4 -0
- package/Math/arc-generate-points-exponantial.js +254 -0
- package/Math/arc.js +5 -5
- package/Math/circle-cdf-points.js +247 -0
- package/Math/circle.js +39 -0
- package/Math/methods.js +13 -3
- package/Math/vec3.js +3 -3
- package/package.json +1 -1
- package/programs/line-on-globe/circle-accurate.js +176 -175
- package/programs/line-on-globe/circle.js +166 -164
- package/programs/line-on-globe/linestrip/data.js +4 -0
- package/programs/line-on-globe/{linestrip.js → linestrip/linestrip.js} +38 -39
- package/programs/line-on-globe/to-the-surface.js +111 -109
- package/programs/rings/distancering/circleflatprogram.js +116 -120
- package/programs/rings/distancering/circlepaddingfreeangleprogram.js +1 -1
- package/programs/rings/distancering/circlepaddysharedbuffer.js +368 -354
- package/programs/rings/distancering/index.js +6 -5
- package/programs/rings/distancering/paddyflatprogram.js +127 -136
- package/programs/rings/distancering/paddyflatprogram2d.js +129 -138
- package/programs/rings/distancering/paddyflatprogram3d.js +128 -136
- package/programs/totems/camerauniformblock.js +35 -8
- package/programs/totems/canvas-webglobe-info.js +55 -20
- package/programs/totems/{camerauniformblock copy.js → canvas-webglobe-info1.js} +11 -76
- package/programs/vectorfields/logics/pixelbased.js +4 -20
- package/shape-on-terrain/arc/naive/plugin.js +249 -288
- package/shape-on-terrain/circle/plugin.js +284 -0
- package/shape-on-terrain/type.js +1 -0
- package/util/account/index.js +2 -2
- package/util/account/single-attribute-buffer-management/buffer-manager.js +2 -3
- package/util/account/single-attribute-buffer-management/buffer-orchestrator.js +2 -2
- package/util/build-strategy/general-strategy.js +62 -0
- package/util/build-strategy/static-dynamic.js +31 -0
- package/util/gl-util/draw-options/types.js +1 -1
- package/globe-types.js +0 -1
- package/programs/interface.js +0 -1
- package/programs/rings/distancering/shader.js +0 -1
- package/programs/totems/camerauniformblock1.js +0 -171
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// --- Utility Functions for 3D Vector Operations --- (Assumed to be the same as provided)
|
|
2
|
+
/**
|
|
3
|
+
* Normalizes a 3D vector to unit length.
|
|
4
|
+
* @param v The input vector.
|
|
5
|
+
* @returns The normalized vector. Returns [0,0,0] if the input vector is a zero vector.
|
|
6
|
+
*/
|
|
7
|
+
function vec3Normalize(v) {
|
|
8
|
+
const len = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
|
|
9
|
+
if (len < 1e-9) { // Use a small epsilon to handle near-zero vectors
|
|
10
|
+
return [0, 0, 0];
|
|
11
|
+
}
|
|
12
|
+
return [v[0] / len, v[1] / len, v[2] / len];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Calculates the dot product of two 3D vectors.
|
|
16
|
+
* @param v1 The first vector.
|
|
17
|
+
* @param v2 The second vector.
|
|
18
|
+
* @returns The dot product.
|
|
19
|
+
*/
|
|
20
|
+
function vec3Dot(v1, v2) {
|
|
21
|
+
return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Calculates the cross product of two 3D vectors.
|
|
25
|
+
* @param v1 The first vector.
|
|
26
|
+
* @param v2 The second vector.
|
|
27
|
+
* @returns The cross product vector.
|
|
28
|
+
*/
|
|
29
|
+
function vec3Cross(v1, v2) {
|
|
30
|
+
return [
|
|
31
|
+
v1[1] * v2[2] - v1[2] * v2[1],
|
|
32
|
+
v1[2] * v2[0] - v1[0] * v2[2],
|
|
33
|
+
v1[0] * v2[1] - v1[1] * v2[0]
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Scales a 3D vector by a scalar.
|
|
38
|
+
* @param v The input vector.
|
|
39
|
+
* @param s The scalar value.
|
|
40
|
+
* @returns The scaled vector.
|
|
41
|
+
*/
|
|
42
|
+
function vec3Scale(v, s) {
|
|
43
|
+
return [v[0] * s, v[1] * s, v[2] * s];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Adds two 3D vectors.
|
|
47
|
+
* @param v1 The first vector.
|
|
48
|
+
* @param v2 The second vector.
|
|
49
|
+
* @returns The resulting vector.
|
|
50
|
+
*/
|
|
51
|
+
function vec3Add(v1, v2) {
|
|
52
|
+
return [v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]];
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Subtracts the second 3D vector from the first.
|
|
56
|
+
* @param v1 The first vector.
|
|
57
|
+
* @param v2 The second vector.
|
|
58
|
+
* @returns The resulting vector.
|
|
59
|
+
*/
|
|
60
|
+
function vec3Sub(v1, v2) {
|
|
61
|
+
return [v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]];
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Clamps a numerical value within a specified range.
|
|
65
|
+
* @param value The value to clamp.
|
|
66
|
+
* @param min The minimum allowed value.
|
|
67
|
+
* @param max The maximum allowed value.
|
|
68
|
+
* @returns The clamped value.
|
|
69
|
+
*/
|
|
70
|
+
function clamp(value, min, max) {
|
|
71
|
+
return Math.max(min, Math.min(value, max));
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Rotates a vector around a given axis by a specified angle using Rodrigues' rotation formula.
|
|
75
|
+
* The axis must be a unit vector for correct results.
|
|
76
|
+
* @param v The vector to rotate.
|
|
77
|
+
* @param axis The unit axis of rotation.
|
|
78
|
+
* @param angle The angle of rotation in radians.
|
|
79
|
+
* @returns The rotated vector.
|
|
80
|
+
*/
|
|
81
|
+
function rotateVector(v, axis, angle) {
|
|
82
|
+
const cosAngle = Math.cos(angle);
|
|
83
|
+
const sinAngle = Math.sin(angle);
|
|
84
|
+
const oneMinusCos = 1.0 - cosAngle;
|
|
85
|
+
const dotAxisV = vec3Dot(axis, v);
|
|
86
|
+
const crossAxisV = vec3Cross(axis, v);
|
|
87
|
+
// Rodrigues' rotation formula:
|
|
88
|
+
// v' = v * cos(angle) + (axis x v) * sin(angle) + axis * (axis . v) * (1 - cos(angle))
|
|
89
|
+
const term1 = vec3Scale(v, cosAngle);
|
|
90
|
+
const term2 = vec3Scale(crossAxisV, sinAngle);
|
|
91
|
+
const term3 = vec3Scale(axis, dotAxisV * oneMinusCos);
|
|
92
|
+
return vec3Add(vec3Add(term1, term2), term3);
|
|
93
|
+
}
|
|
94
|
+
// --- Main Function for Arc Point Generation (CORRECTED) ---
|
|
95
|
+
/**
|
|
96
|
+
* Generates points on the shortest arc between p0 and p1 on a unit sphere.
|
|
97
|
+
* The arc is formed by rotating p0 around axisA. The density of points
|
|
98
|
+
* is higher closer to the attractionPoint, controlled by an exponential falloff.
|
|
99
|
+
*
|
|
100
|
+
* @param p0 The starting point of the arc (will be normalized).
|
|
101
|
+
* @param p1 The ending point of the arc (will be normalized).
|
|
102
|
+
* @param axisA The axis of rotation (will be normalized).
|
|
103
|
+
* @param attractionPoint The point on the unit sphere that attracts sampled points (will be normalized).
|
|
104
|
+
* @param pointCount The total number of points to generate on the arc, including p0 and p1. > evenSpacedPointCount.
|
|
105
|
+
* @param attractionStrength Controls the density bias via exponential decay.
|
|
106
|
+
* A value of 0 results in uniform sampling.
|
|
107
|
+
* Higher positive values (e.g., 10, 50, 200) result in exponentially stronger attraction.
|
|
108
|
+
* @returns An array of `Vec3` representing the deterministically sampled points on the arc.
|
|
109
|
+
*/
|
|
110
|
+
export function generateArcPoints(p0, p1, axisA, attractionPoint, pointCount, attractionStrength, adaptiveStrength = true, evenSpacedPointCount = null) {
|
|
111
|
+
// 0. Add evenly distanced points
|
|
112
|
+
let t_even_index = 0;
|
|
113
|
+
evenSpacedPointCount = evenSpacedPointCount ?? Math.floor(pointCount / 2.5);
|
|
114
|
+
const t_event = Array.from({ length: evenSpacedPointCount }, (_, i) => i / (evenSpacedPointCount - 1));
|
|
115
|
+
const sampledPoints = [];
|
|
116
|
+
const epsilon = 1e-9;
|
|
117
|
+
// 1. Normalize all input vectors
|
|
118
|
+
let nP0 = vec3Normalize(p0);
|
|
119
|
+
let nP1 = vec3Normalize(p1);
|
|
120
|
+
let nAxisA = vec3Normalize(axisA);
|
|
121
|
+
let nAttractionPoint = vec3Normalize(attractionPoint);
|
|
122
|
+
// Handle edge cases for pointCount
|
|
123
|
+
if (pointCount < 1)
|
|
124
|
+
return [];
|
|
125
|
+
if (pointCount === 1)
|
|
126
|
+
return [nP0];
|
|
127
|
+
// 2. Determine the total rotation angle
|
|
128
|
+
const p0Projected = vec3Sub(nP0, vec3Scale(nAxisA, vec3Dot(nP0, nAxisA)));
|
|
129
|
+
const p1Projected = vec3Sub(nP1, vec3Scale(nAxisA, vec3Dot(nP1, nAxisA)));
|
|
130
|
+
if (vec3Dot(p0Projected, p0Projected) < epsilon * epsilon || vec3Dot(p1Projected, p1Projected) < epsilon * epsilon) {
|
|
131
|
+
console.warn("generateArcPoints: p0 or p1 is colinear with axisA. Cannot form a valid arc. Falling back to linear interpolation.");
|
|
132
|
+
for (let i = 0; i < pointCount; i++) {
|
|
133
|
+
const t = i / (pointCount - 1);
|
|
134
|
+
sampledPoints.push(vec3Normalize(vec3Add(vec3Scale(nP0, 1 - t), vec3Scale(nP1, t))));
|
|
135
|
+
}
|
|
136
|
+
return sampledPoints;
|
|
137
|
+
}
|
|
138
|
+
const nP0Projected = vec3Normalize(p0Projected);
|
|
139
|
+
const nP1Projected = vec3Normalize(p1Projected);
|
|
140
|
+
const crossProj = vec3Cross(nP0Projected, nP1Projected);
|
|
141
|
+
const totalRotationAngle = Math.atan2(vec3Dot(crossProj, nAxisA), vec3Dot(nP0Projected, nP1Projected));
|
|
142
|
+
if (adaptiveStrength)
|
|
143
|
+
attractionStrength *= Math.pow(Math.max(1, (totalRotationAngle + 0.1)), 3); // Scale attraction strength by the arc length
|
|
144
|
+
if (Math.abs(totalRotationAngle) < epsilon) {
|
|
145
|
+
for (let i = 0; i < pointCount; i++)
|
|
146
|
+
sampledPoints.push(nP0);
|
|
147
|
+
return sampledPoints;
|
|
148
|
+
}
|
|
149
|
+
// 3. Find alpha_A: the angle parameter where the arc is closest to the attractionPoint
|
|
150
|
+
const alpha_A = (() => {
|
|
151
|
+
const dotProduct = vec3Dot(nP0, nAttractionPoint);
|
|
152
|
+
return Math.acos(dotProduct);
|
|
153
|
+
})();
|
|
154
|
+
// 4. Define the PDF and CDF based on an exponential decay model
|
|
155
|
+
const t_peak = totalRotationAngle !== 0 ? clamp(alpha_A / totalRotationAngle, 0, 1) : 0;
|
|
156
|
+
const s = Math.max(0, attractionStrength);
|
|
157
|
+
const area1 = (1 - Math.exp(-s * t_peak));
|
|
158
|
+
const area2 = (1 - Math.exp(-s * (1 - t_peak)));
|
|
159
|
+
const totalIntegratedArea = (area1 + area2) / s;
|
|
160
|
+
const cdfAtPeak = (area1 / s) / totalIntegratedArea;
|
|
161
|
+
// 5. Generate deterministic points using inverse transform sampling
|
|
162
|
+
for (let i = 0; i < pointCount - evenSpacedPointCount; i++) {
|
|
163
|
+
const u = (pointCount === 1) ? 0.5 : (i + 1) / (pointCount - evenSpacedPointCount + 1);
|
|
164
|
+
let t_sampled;
|
|
165
|
+
if (s < epsilon) {
|
|
166
|
+
t_sampled = u;
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
// Correctly calculate the area under the two parts of the exponential PDF
|
|
170
|
+
if (totalIntegratedArea < epsilon) {
|
|
171
|
+
t_sampled = u; // Fallback for extreme strength values
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
if (u <= cdfAtPeak) {
|
|
175
|
+
// *** BUG FIX START ***
|
|
176
|
+
// Correctly invert the CDF for the first part: [0, t_peak]
|
|
177
|
+
const u_prime = u / cdfAtPeak;
|
|
178
|
+
t_sampled = t_peak + Math.log(Math.exp(-s * t_peak) + u_prime * (1 - Math.exp(-s * t_peak))) / s;
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
// Correctly invert the CDF for the second part: [t_peak, 1]
|
|
182
|
+
const u_double_prime = (u - cdfAtPeak) / (1 - cdfAtPeak);
|
|
183
|
+
t_sampled = t_peak - Math.log(1 - u_double_prime * (1 - Math.exp(-s * (1 - t_peak)))) / s;
|
|
184
|
+
}
|
|
185
|
+
// *** BUG FIX END ***
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
t_sampled = clamp(t_sampled, 0, 1);
|
|
189
|
+
while (t_even_index < evenSpacedPointCount && t_sampled > t_event[t_even_index]) {
|
|
190
|
+
const actualRotationAngle = totalRotationAngle * t_event[t_even_index];
|
|
191
|
+
const currentPoint = rotateVector(nP0, nAxisA, actualRotationAngle);
|
|
192
|
+
sampledPoints.push(currentPoint);
|
|
193
|
+
t_even_index++;
|
|
194
|
+
}
|
|
195
|
+
const actualRotationAngle = totalRotationAngle * t_sampled;
|
|
196
|
+
const currentPoint = rotateVector(nP0, nAxisA, actualRotationAngle);
|
|
197
|
+
sampledPoints.push(currentPoint);
|
|
198
|
+
}
|
|
199
|
+
while (t_even_index < evenSpacedPointCount) {
|
|
200
|
+
const actualRotationAngle = totalRotationAngle * t_event[t_even_index];
|
|
201
|
+
const currentPoint = rotateVector(nP0, nAxisA, actualRotationAngle);
|
|
202
|
+
sampledPoints.push(currentPoint);
|
|
203
|
+
t_even_index++;
|
|
204
|
+
}
|
|
205
|
+
return sampledPoints;
|
|
206
|
+
}
|
|
207
|
+
// // --- Example Usage (for demonstration and testing) ---
|
|
208
|
+
// // Example 1: Basic arc, very strong attraction point in the middle
|
|
209
|
+
// const p0_ex1: Vec3 = [1, 0, 0];
|
|
210
|
+
// const p1_ex1: Vec3 = [0, 1, 0]; // 90 deg rotation around Z-axis
|
|
211
|
+
// const axisA_ex1: Vec3 = [0, 0, 1];
|
|
212
|
+
// const attractionPoint_ex1: Vec3 = [Math.sqrt(0.5), Math.sqrt(0.5), 0]; // On the arc, middle of P0-P1
|
|
213
|
+
// const numPoints_ex1 = 21;
|
|
214
|
+
// const strength_ex1 = 50.0; // Very strong attraction with the new exponential model
|
|
215
|
+
// console.log("--- Example 1: Attraction point in the middle of the arc (Exponentially Strong) ---");
|
|
216
|
+
// const points1 = generateArcPoints(p0_ex1, p1_ex1, axisA_ex1, attractionPoint_ex1, numPoints_ex1, strength_ex1);
|
|
217
|
+
// console.log(`Generated ${points1.length} points.`);
|
|
218
|
+
// // With high strength, points should be extremely clustered around the attraction point.
|
|
219
|
+
// const sequentialDistances1 = points1.map((p, i) => {
|
|
220
|
+
// if (i === 0) return 0;
|
|
221
|
+
// return vec3Distance(points1[i - 1], p);
|
|
222
|
+
// });
|
|
223
|
+
// console.log("Distances near the start:", sequentialDistances1.slice(1, 5).map(d => d.toFixed(5)));
|
|
224
|
+
// console.log("Distances near the middle (attraction point):", sequentialDistances1.slice(9, 13).map(d => d.toFixed(5)));
|
|
225
|
+
// console.log("Distances near the end:", sequentialDistances1.slice(-4).map(d => d.toFixed(5)));
|
|
226
|
+
// // Expected: Distances near the middle should be MUCH smaller than at the ends.
|
|
227
|
+
// // Example 2: Attraction point near p0, very high strength
|
|
228
|
+
// const p0_ex2: Vec3 = [1, 0, 0];
|
|
229
|
+
// const p1_ex2: Vec3 = [0, 1, 0];
|
|
230
|
+
// const axisA_ex2: Vec3 = [0, 0, 1];
|
|
231
|
+
// const attractionPoint_ex2: Vec3 = [0.99, 0.01, 0]; // Very close to P0
|
|
232
|
+
// const numPoints_ex2 = 10;
|
|
233
|
+
// const strength_ex2 = 100.0; // Extremely strong attraction
|
|
234
|
+
// console.log("\n--- Example 2: Attraction point near the start (p0) ---");
|
|
235
|
+
// const points2 = generateArcPoints(p0_ex2, p1_ex2, axisA_ex2, attractionPoint_ex2, numPoints_ex2, strength_ex2);
|
|
236
|
+
// console.log(`Generated ${points2.length} points.`);
|
|
237
|
+
// console.log("First 5 points (should be very close to each other and p0):", points2.slice(0, 5).map(p => p.map(coord => coord.toFixed(5))));
|
|
238
|
+
// console.log("Last 5 points (should be very spread out):", points2.slice(-5).map(p => p.map(coord => coord.toFixed(5))));
|
|
239
|
+
// // Example 4: No attraction (uniform distribution)
|
|
240
|
+
// const p0_ex4: Vec3 = [1, 0, 0];
|
|
241
|
+
// const p1_ex4: Vec3 = [0, 1, 0];
|
|
242
|
+
// const axisA_ex4: Vec3 = [0, 0, 1];
|
|
243
|
+
// const attractionPoint_ex4: Vec3 = [1, 0, 0]; // Irrelevant when strength is 0
|
|
244
|
+
// const numPoints_ex4 = 11;
|
|
245
|
+
// const strength_ex4 = 0.0; // No attraction (uniform)
|
|
246
|
+
// console.log("\n--- Example 4: Uniform distribution (strength = 0) ---");
|
|
247
|
+
// const points4 = generateArcPoints(p0_ex4, p1_ex4, axisA_ex4, attractionPoint_ex4, numPoints_ex4, strength_ex4);
|
|
248
|
+
// const sequentialDistances4 = points4.map((p, i) => {
|
|
249
|
+
// if (i === 0) return 0;
|
|
250
|
+
// return vec3Distance(points4[i - 1], p);
|
|
251
|
+
// });
|
|
252
|
+
// console.log("Sequential distances (should be almost identical):", sequentialDistances4.slice(1).map(d => d.toFixed(5)));
|
|
@@ -124,6 +124,7 @@ function solveQuadratic(a, b, c, minVal, maxVal) {
|
|
|
124
124
|
// Case 2: Quadratic equation
|
|
125
125
|
const discriminant = b * b - 4 * a * c;
|
|
126
126
|
if (discriminant < -epsilon) { // No real roots (discriminant is significantly negative)
|
|
127
|
+
console.log("No real roots for the quadratic equation.");
|
|
127
128
|
return null;
|
|
128
129
|
}
|
|
129
130
|
if (discriminant < epsilon) { // One real root (discriminant is close to zero)
|
|
@@ -224,6 +225,7 @@ export function generateArcPoints(p0, p1, axisA, attractionPoint, pointCount, st
|
|
|
224
225
|
// We search over `t` from 0 to 1, and map to actual rotation angles.
|
|
225
226
|
let alpha_A = 0.0; // This will be the actual rotation angle
|
|
226
227
|
let minAngleToAttraction = Math.PI; // Initialize with max possible angle
|
|
228
|
+
// TODO: USE GEOMETRY INSTEAD OF MARKOV CHOICE
|
|
227
229
|
const testSteps = 1000; // Granularity for finding alpha_A
|
|
228
230
|
for (let i = 0; i <= testSteps; i++) {
|
|
229
231
|
const t_test = i / testSteps; // Normalized parameter [0, 1]
|
|
@@ -236,6 +238,8 @@ export function generateArcPoints(p0, p1, axisA, attractionPoint, pointCount, st
|
|
|
236
238
|
alpha_A = currentAlpha;
|
|
237
239
|
}
|
|
238
240
|
}
|
|
241
|
+
// minAngleToAttraction = 0.0;
|
|
242
|
+
console.log("generated alpha_A:", alpha_A, "minAngleToAttraction:", minAngleToAttraction);
|
|
239
243
|
// 4. Define the PDF parameters (for normalized parameter t in [0,1])
|
|
240
244
|
// The density function for 't' (normalized angle from 0 to 1)
|
|
241
245
|
const minDensity = 1.0;
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
// --- Utility Functions for 3D Vector Operations --- (Assumed to be the same as provided)
|
|
2
|
+
/**
|
|
3
|
+
* Normalizes a 3D vector to unit length.
|
|
4
|
+
* @param v The input vector.
|
|
5
|
+
* @returns The normalized vector. Returns [0,0,0] if the input vector is a zero vector.
|
|
6
|
+
*/
|
|
7
|
+
function vec3Normalize(v) {
|
|
8
|
+
const len = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
|
|
9
|
+
if (len < 1e-9) { // Use a small epsilon to handle near-zero vectors
|
|
10
|
+
return [0, 0, 0];
|
|
11
|
+
}
|
|
12
|
+
return [v[0] / len, v[1] / len, v[2] / len];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Calculates the dot product of two 3D vectors.
|
|
16
|
+
* @param v1 The first vector.
|
|
17
|
+
* @param v2 The second vector.
|
|
18
|
+
* @returns The dot product.
|
|
19
|
+
*/
|
|
20
|
+
function vec3Dot(v1, v2) {
|
|
21
|
+
return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Calculates the cross product of two 3D vectors.
|
|
25
|
+
* @param v1 The first vector.
|
|
26
|
+
* @param v2 The second vector.
|
|
27
|
+
* @returns The cross product vector.
|
|
28
|
+
*/
|
|
29
|
+
function vec3Cross(v1, v2) {
|
|
30
|
+
return [
|
|
31
|
+
v1[1] * v2[2] - v1[2] * v2[1],
|
|
32
|
+
v1[2] * v2[0] - v1[0] * v2[2],
|
|
33
|
+
v1[0] * v2[1] - v1[1] * v2[0]
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Scales a 3D vector by a scalar.
|
|
38
|
+
* @param v The input vector.
|
|
39
|
+
* @param s The scalar value.
|
|
40
|
+
* @returns The scaled vector.
|
|
41
|
+
*/
|
|
42
|
+
function vec3Scale(v, s) {
|
|
43
|
+
return [v[0] * s, v[1] * s, v[2] * s];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Adds two 3D vectors.
|
|
47
|
+
* @param v1 The first vector.
|
|
48
|
+
* @param v2 The second vector.
|
|
49
|
+
* @returns The resulting vector.
|
|
50
|
+
*/
|
|
51
|
+
function vec3Add(v1, v2) {
|
|
52
|
+
return [v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]];
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Subtracts the second 3D vector from the first.
|
|
56
|
+
* @param v1 The first vector.
|
|
57
|
+
* @param v2 The second vector.
|
|
58
|
+
* @returns The resulting vector.
|
|
59
|
+
*/
|
|
60
|
+
function vec3Sub(v1, v2) {
|
|
61
|
+
return [v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]];
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Clamps a numerical value within a specified range.
|
|
65
|
+
* @param value The value to clamp.
|
|
66
|
+
* @param min The minimum allowed value.
|
|
67
|
+
* @param max The maximum allowed value.
|
|
68
|
+
* @returns The clamped value.
|
|
69
|
+
*/
|
|
70
|
+
function clamp(value, min, max) {
|
|
71
|
+
return Math.max(min, Math.min(value, max));
|
|
72
|
+
}
|
|
73
|
+
function vec3Distance(v1, v2) {
|
|
74
|
+
const dx = v1[0] - v2[0];
|
|
75
|
+
const dy = v1[1] - v2[1];
|
|
76
|
+
const dz = v1[2] - v2[2];
|
|
77
|
+
return Math.sqrt(dx * dx + dy * dy + dz * dz);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Rotates a vector around a given axis by a specified angle using Rodrigues' rotation formula.
|
|
81
|
+
* The axis must be a unit vector for correct results.
|
|
82
|
+
* @param v The vector to rotate.
|
|
83
|
+
* @param axis The unit axis of rotation.
|
|
84
|
+
* @param angle The angle of rotation in radians.
|
|
85
|
+
* @returns The rotated vector.
|
|
86
|
+
*/
|
|
87
|
+
function rotateVector(v, axis, angle) {
|
|
88
|
+
const cosAngle = Math.cos(angle);
|
|
89
|
+
const sinAngle = Math.sin(angle);
|
|
90
|
+
const oneMinusCos = 1.0 - cosAngle;
|
|
91
|
+
const dotAxisV = vec3Dot(axis, v);
|
|
92
|
+
const crossAxisV = vec3Cross(axis, v);
|
|
93
|
+
// Rodrigues' rotation formula:
|
|
94
|
+
// v' = v * cos(angle) + (axis x v) * sin(angle) + axis * (axis . v) * (1 - cos(angle))
|
|
95
|
+
const term1 = vec3Scale(v, cosAngle);
|
|
96
|
+
const term2 = vec3Scale(crossAxisV, sinAngle);
|
|
97
|
+
const term3 = vec3Scale(axis, dotAxisV * oneMinusCos);
|
|
98
|
+
return vec3Add(vec3Add(term1, term2), term3);
|
|
99
|
+
}
|
|
100
|
+
// --- Main Function for Arc Point Generation (CORRECTED) ---
|
|
101
|
+
/**
|
|
102
|
+
* Generates points on the shortest arc between p0 and p1 on a unit sphere.
|
|
103
|
+
* The arc is formed by rotating p0 around axisA. The density of points
|
|
104
|
+
* is higher closer to the attractionPoint, controlled by an exponential falloff.
|
|
105
|
+
*
|
|
106
|
+
* @param p0 The starting point of the arc (will be normalized).
|
|
107
|
+
* @param p1 The ending point of the arc (will be normalized).
|
|
108
|
+
* @param axisA The axis of rotation (will be normalized).
|
|
109
|
+
* @param attractionPoint The point on the unit sphere that attracts sampled points (will be normalized).
|
|
110
|
+
* @param pointCount The total number of points to generate on the arc, including p0 and p1.
|
|
111
|
+
* @param attractionStrength Controls the density bias via exponential decay.
|
|
112
|
+
* A value of 0 results in uniform sampling.
|
|
113
|
+
* Higher positive values (e.g., 10, 50, 200) result in exponentially stronger attraction.
|
|
114
|
+
* @returns An array of `Vec3` representing the deterministically sampled points on the arc.
|
|
115
|
+
*/
|
|
116
|
+
export function generateArcPoints(p0, p1, axisA, attractionPoint, pointCount, attractionStrength) {
|
|
117
|
+
const sampledPoints = [];
|
|
118
|
+
const epsilon = 1e-9;
|
|
119
|
+
// 1. Normalize all input vectors
|
|
120
|
+
let nP0 = vec3Normalize(p0);
|
|
121
|
+
let nP1 = vec3Normalize(p1);
|
|
122
|
+
let nAxisA = vec3Normalize(axisA);
|
|
123
|
+
let nAttractionPoint = vec3Normalize(attractionPoint);
|
|
124
|
+
// Handle edge cases for pointCount
|
|
125
|
+
if (pointCount < 1)
|
|
126
|
+
return [];
|
|
127
|
+
if (pointCount === 1)
|
|
128
|
+
return [nP0];
|
|
129
|
+
// 2. Determine the total rotation angle
|
|
130
|
+
const p0Projected = vec3Sub(nP0, vec3Scale(nAxisA, vec3Dot(nP0, nAxisA)));
|
|
131
|
+
const p1Projected = vec3Sub(nP1, vec3Scale(nAxisA, vec3Dot(nP1, nAxisA)));
|
|
132
|
+
if (vec3Dot(p0Projected, p0Projected) < epsilon * epsilon || vec3Dot(p1Projected, p1Projected) < epsilon * epsilon) {
|
|
133
|
+
console.warn("generateArcPoints: p0 or p1 is colinear with axisA. Cannot form a valid arc. Falling back to linear interpolation.");
|
|
134
|
+
for (let i = 0; i < pointCount; i++) {
|
|
135
|
+
const t = i / (pointCount - 1);
|
|
136
|
+
sampledPoints.push(vec3Normalize(vec3Add(vec3Scale(nP0, 1 - t), vec3Scale(nP1, t))));
|
|
137
|
+
}
|
|
138
|
+
return sampledPoints;
|
|
139
|
+
}
|
|
140
|
+
const nP0Projected = vec3Normalize(p0Projected);
|
|
141
|
+
const nP1Projected = vec3Normalize(p1Projected);
|
|
142
|
+
const crossProj = vec3Cross(nP0Projected, nP1Projected);
|
|
143
|
+
const totalRotationAngle = Math.atan2(vec3Dot(crossProj, nAxisA), vec3Dot(nP0Projected, nP1Projected));
|
|
144
|
+
if (Math.abs(totalRotationAngle) < epsilon) {
|
|
145
|
+
for (let i = 0; i < pointCount; i++)
|
|
146
|
+
sampledPoints.push(nP0);
|
|
147
|
+
return sampledPoints;
|
|
148
|
+
}
|
|
149
|
+
// 3. Find alpha_A: the angle parameter where the arc is closest to the attractionPoint
|
|
150
|
+
const alpha_A = (() => {
|
|
151
|
+
const dotProduct = vec3Dot(nP0, nAttractionPoint);
|
|
152
|
+
return Math.acos(dotProduct);
|
|
153
|
+
})();
|
|
154
|
+
// let alpha_A = 0.0;
|
|
155
|
+
// let minAngleToAttraction = Math.PI;
|
|
156
|
+
// const testSteps = 1000;
|
|
157
|
+
// for (let i = 0; i <= testSteps; i++) {
|
|
158
|
+
// const t_test = i / testSteps;
|
|
159
|
+
// const currentAlpha = totalRotationAngle * t_test;
|
|
160
|
+
// const pArcTest = rotateVector(nP0, nAxisA, currentAlpha);
|
|
161
|
+
// const dotProduct = clamp(vec3Dot(pArcTest, nAttractionPoint), -1.0, 1.0);
|
|
162
|
+
// const currentAngle = Math.acos(dotProduct);
|
|
163
|
+
// if (currentAngle < minAngleToAttraction) {
|
|
164
|
+
// minAngleToAttraction = currentAngle;
|
|
165
|
+
// alpha_A = currentAlpha;
|
|
166
|
+
// }
|
|
167
|
+
// }
|
|
168
|
+
// 4. Define the PDF and CDF based on an exponential decay model
|
|
169
|
+
const t_peak = totalRotationAngle !== 0 ? clamp(alpha_A / totalRotationAngle, 0, 1) : 0;
|
|
170
|
+
const s = Math.max(0, attractionStrength);
|
|
171
|
+
// 5. Generate deterministic points using inverse transform sampling
|
|
172
|
+
for (let i = 0; i < pointCount; i++) {
|
|
173
|
+
const u = (pointCount === 1) ? 0.5 : i / (pointCount - 1);
|
|
174
|
+
let t_sampled;
|
|
175
|
+
if (s < epsilon) {
|
|
176
|
+
t_sampled = u;
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
// Correctly calculate the area under the two parts of the exponential PDF
|
|
180
|
+
const area1 = (1 - Math.exp(-s * t_peak));
|
|
181
|
+
const area2 = (1 - Math.exp(-s * (1 - t_peak)));
|
|
182
|
+
const totalIntegratedArea = (area1 + area2) / s;
|
|
183
|
+
if (totalIntegratedArea < epsilon) {
|
|
184
|
+
t_sampled = u; // Fallback for extreme strength values
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
const cdfAtPeak = (area1 / s) / totalIntegratedArea;
|
|
188
|
+
if (u <= cdfAtPeak) {
|
|
189
|
+
// *** BUG FIX START ***
|
|
190
|
+
// Correctly invert the CDF for the first part: [0, t_peak]
|
|
191
|
+
const u_prime = u / cdfAtPeak;
|
|
192
|
+
t_sampled = t_peak + Math.log(Math.exp(-s * t_peak) + u_prime * (1 - Math.exp(-s * t_peak))) / s;
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
// Correctly invert the CDF for the second part: [t_peak, 1]
|
|
196
|
+
const u_double_prime = (u - cdfAtPeak) / (1 - cdfAtPeak);
|
|
197
|
+
t_sampled = t_peak - Math.log(1 - u_double_prime * (1 - Math.exp(-s * (1 - t_peak)))) / s;
|
|
198
|
+
}
|
|
199
|
+
// *** BUG FIX END ***
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
t_sampled = clamp(t_sampled, 0, 1);
|
|
203
|
+
const actualRotationAngle = totalRotationAngle * t_sampled;
|
|
204
|
+
const currentPoint = rotateVector(nP0, nAxisA, actualRotationAngle);
|
|
205
|
+
sampledPoints.push(currentPoint);
|
|
206
|
+
}
|
|
207
|
+
return sampledPoints;
|
|
208
|
+
}
|
|
209
|
+
// // --- Example Usage (for demonstration and testing) ---
|
|
210
|
+
// // Example 1: Basic arc, very strong attraction point in the middle
|
|
211
|
+
// const p0_ex1: Vec3 = [1, 0, 0];
|
|
212
|
+
// const p1_ex1: Vec3 = [0, 1, 0]; // 90 deg rotation around Z-axis
|
|
213
|
+
// const axisA_ex1: Vec3 = [0, 0, 1];
|
|
214
|
+
// const attractionPoint_ex1: Vec3 = [Math.sqrt(0.5), Math.sqrt(0.5), 0]; // On the arc, middle of P0-P1
|
|
215
|
+
// const numPoints_ex1 = 21;
|
|
216
|
+
// const strength_ex1 = 50.0; // Very strong attraction with the new exponential model
|
|
217
|
+
// console.log("--- Example 1: Attraction point in the middle of the arc (Exponentially Strong) ---");
|
|
218
|
+
// const points1 = generateArcPoints(p0_ex1, p1_ex1, axisA_ex1, attractionPoint_ex1, numPoints_ex1, strength_ex1);
|
|
219
|
+
// console.log(`Generated ${points1.length} points.`);
|
|
220
|
+
// // With high strength, points should be extremely clustered around the attraction point.
|
|
221
|
+
// const sequentialDistances1 = points1.map((p, i) => {
|
|
222
|
+
// if (i === 0) return 0;
|
|
223
|
+
// return vec3Distance(points1[i - 1], p);
|
|
224
|
+
// });
|
|
225
|
+
// console.log("Distances near the start:", sequentialDistances1.slice(1, 5).map(d => d.toFixed(5)));
|
|
226
|
+
// console.log("Distances near the middle (attraction point):", sequentialDistances1.slice(9, 13).map(d => d.toFixed(5)));
|
|
227
|
+
// console.log("Distances near the end:", sequentialDistances1.slice(-4).map(d => d.toFixed(5)));
|
|
228
|
+
// // Expected: Distances near the middle should be MUCH smaller than at the ends.
|
|
229
|
+
// // Example 2: Attraction point near p0, very high strength
|
|
230
|
+
// const p0_ex2: Vec3 = [1, 0, 0];
|
|
231
|
+
// const p1_ex2: Vec3 = [0, 1, 0];
|
|
232
|
+
// const axisA_ex2: Vec3 = [0, 0, 1];
|
|
233
|
+
// const attractionPoint_ex2: Vec3 = [0.99, 0.01, 0]; // Very close to P0
|
|
234
|
+
// const numPoints_ex2 = 10;
|
|
235
|
+
// const strength_ex2 = 100.0; // Extremely strong attraction
|
|
236
|
+
// console.log("\n--- Example 2: Attraction point near the start (p0) ---");
|
|
237
|
+
// const points2 = generateArcPoints(p0_ex2, p1_ex2, axisA_ex2, attractionPoint_ex2, numPoints_ex2, strength_ex2);
|
|
238
|
+
// console.log(`Generated ${points2.length} points.`);
|
|
239
|
+
// console.log("First 5 points (should be very close to each other and p0):", points2.slice(0, 5).map(p => p.map(coord => coord.toFixed(5))));
|
|
240
|
+
// console.log("Last 5 points (should be very spread out):", points2.slice(-5).map(p => p.map(coord => coord.toFixed(5))));
|
|
241
|
+
// // Example 4: No attraction (uniform distribution)
|
|
242
|
+
// const p0_ex4: Vec3 = [1, 0, 0];
|
|
243
|
+
// const p1_ex4: Vec3 = [0, 1, 0];
|
|
244
|
+
// const axisA_ex4: Vec3 = [0, 0, 1];
|
|
245
|
+
// const attractionPoint_ex4: Vec3 = [1, 0, 0]; // Irrelevant when strength is 0
|
|
246
|
+
// const numPoints_ex4 = 11;
|
|
247
|
+
// const strength_ex4 = 0.0; // No attraction (uniform)
|
|
248
|
+
// console.log("\n--- Example 4: Uniform distribution (strength = 0) ---");
|
|
249
|
+
// const points4 = generateArcPoints(p0_ex4, p1_ex4, axisA_ex4, attractionPoint_ex4, numPoints_ex4, strength_ex4);
|
|
250
|
+
// const sequentialDistances4 = points4.map((p, i) => {
|
|
251
|
+
// if (i === 0) return 0;
|
|
252
|
+
// return vec3Distance(points4[i - 1], p);
|
|
253
|
+
// });
|
|
254
|
+
// console.log("Sequential distances (should be almost identical):", sequentialDistances4.slice(1).map(d => d.toFixed(5)));
|
package/Math/arc.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { create as vec3create, set as vec3set, cross, dot, normalize, clone as vec3clone, copy as vec3copy, equals as vec3equals, lengthSquared,
|
|
1
|
+
import { create as vec3create, set as vec3set, cross, dot, normalize, clone as vec3clone, copy as vec3copy, equals as vec3equals, lengthSquared, fromLongLatToUnitVector, subtract, applyQuaternion, fromUnitVectorToLongLat } from "./vec3";
|
|
2
2
|
import { create as planeCreate, fromPoints as planeFromPoints, distanceToPoint as planeDistanceToPoint, projectPoint as planeProjectPoint } from "./plane";
|
|
3
3
|
import { create as quaternionCreate, fromAxisAngle } from "./quaternion";
|
|
4
4
|
import { EPSILON } from "./constants";
|
|
@@ -97,10 +97,10 @@ function populatePoints(out, arc, count, pointOfProjection = undefined) {
|
|
|
97
97
|
function _oppositePointsHandle(p0, p1, outNormal, outCoverPlane) {
|
|
98
98
|
// first choice pass through Anitkabir
|
|
99
99
|
const radians = Math.PI / 180;
|
|
100
|
-
|
|
100
|
+
fromLongLatToUnitVector(_0vector, [39.9334 * radians, 32.8597 * radians]); // Anitkabir
|
|
101
101
|
if (vec3equals(p0, _0vector) || vec3equals(p1, _0vector)) {
|
|
102
102
|
// if Anitkabir is too close to p0 or p1, use Selanic
|
|
103
|
-
|
|
103
|
+
fromLongLatToUnitVector(_0vector, [37.0016 * radians, 35.3213 * radians]); // Selanic
|
|
104
104
|
}
|
|
105
105
|
planeFromPoints({ normal: outNormal, distance: 0 }, p0, _0vector, p1);
|
|
106
106
|
cross(outCoverPlane.normal, outNormal, p0);
|
|
@@ -156,7 +156,7 @@ quaternionReuseCount = 1) {
|
|
|
156
156
|
// The loop generates all points *except* the very last one.
|
|
157
157
|
for (let i = 0; i < count - 1; i++) {
|
|
158
158
|
// First, store the longitude/latitude of the current point.
|
|
159
|
-
|
|
159
|
+
fromUnitVectorToLongLat(_longLat, currentPoint);
|
|
160
160
|
out[i * 2] = _longLat[0];
|
|
161
161
|
out[i * 2 + 1] = _longLat[1];
|
|
162
162
|
// --- OPTIMIZATION LOGIC ---
|
|
@@ -182,7 +182,7 @@ quaternionReuseCount = 1) {
|
|
|
182
182
|
if (count > 0) {
|
|
183
183
|
const finalIndex = count - 1;
|
|
184
184
|
const endPoint = is0Closer ? inArc.p1 : inArc.p0;
|
|
185
|
-
|
|
185
|
+
fromUnitVectorToLongLat(_longLat, endPoint);
|
|
186
186
|
out[finalIndex * 2] = _longLat[0];
|
|
187
187
|
out[finalIndex * 2 + 1] = _longLat[1];
|
|
188
188
|
}
|