Spaces:
Runtime error
Runtime error
File size: 8,471 Bytes
052979d |
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
class TrajectoryVisualizer {
constructor(containerId) {
this.container = document.getElementById(containerId);
this.trajectories = [];
this.objects = [];
this.trails = [];
this.isPlaying = false;
this.currentFrame = 0;
this.maxFrames = 0;
this.showTrails = true;
this.init();
}
init() {
// Scene
this.scene = new THREE.Scene();
this.scene.fog = new THREE.Fog(0x1a1a2e, 5, 15);
// Camera
this.camera = new THREE.PerspectiveCamera(
75,
this.container.clientWidth / this.container.clientHeight,
0.1,
1000
);
this.camera.position.set(3, 3, 3);
this.camera.lookAt(0, 0, 0);
// Renderer
this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
this.renderer.setSize(this.container.clientWidth, this.container.clientHeight);
this.renderer.setClearColor(0x1a1a2e);
this.container.appendChild(this.renderer.domElement);
// Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
this.scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(5, 5, 5);
this.scene.add(directionalLight);
const pointLight = new THREE.PointLight(0x667eea, 1, 100);
pointLight.position.set(0, 3, 0);
this.scene.add(pointLight);
// Grid helper
const gridHelper = new THREE.GridHelper(4, 20, 0x444444, 0x222222);
gridHelper.position.y = -1;
this.scene.add(gridHelper);
// Axes helper
const axesHelper = new THREE.AxesHelper(2);
this.scene.add(axesHelper);
// Handle window resize
window.addEventListener('resize', () => this.onWindowResize());
// Mouse controls
this.setupControls();
// Start animation loop
this.animate();
}
setupControls() {
let isDragging = false;
let previousMousePosition = { x: 0, y: 0 };
this.container.addEventListener('mousedown', (e) => {
isDragging = true;
});
this.container.addEventListener('mousemove', (e) => {
if (isDragging) {
const deltaX = e.offsetX - previousMousePosition.x;
const deltaY = e.offsetY - previousMousePosition.y;
const rotationSpeed = 0.005;
this.camera.position.applyAxisAngle(
new THREE.Vector3(0, 1, 0),
deltaX * rotationSpeed
);
const lookAt = new THREE.Vector3(0, 0, 0);
this.camera.lookAt(lookAt);
}
previousMousePosition = { x: e.offsetX, y: e.offsetY };
});
this.container.addEventListener('mouseup', () => {
isDragging = false;
});
// Zoom with mouse wheel
this.container.addEventListener('wheel', (e) => {
e.preventDefault();
const zoomSpeed = 0.1;
const direction = new THREE.Vector3();
this.camera.getWorldDirection(direction);
if (e.deltaY < 0) {
this.camera.position.addScaledVector(direction, zoomSpeed);
} else {
this.camera.position.addScaledVector(direction, -zoomSpeed);
}
});
}
loadTrajectories(trajectories) {
this.trajectories = trajectories;
this.currentFrame = 0;
this.isPlaying = false;
// Find max frames
this.maxFrames = 0;
trajectories.forEach(traj => {
const maxFrame = Math.max(...traj.points.map(p => p.frame));
if (maxFrame > this.maxFrames) this.maxFrames = maxFrame;
});
// Clear existing objects
this.objects.forEach(obj => this.scene.remove(obj));
this.trails.forEach(line => this.scene.remove(line));
this.objects = [];
this.trails = [];
// Create objects for each trajectory
const colors = [0xff6b6b, 0x4ecdc4, 0xffe66d, 0x95e1d3, 0xf38181, 0xaa96da, 0xfcbad3, 0xa8e6cf];
trajectories.forEach((traj, index) => {
const color = colors[index % colors.length];
const points = traj.points;
// Create trail line
const positions = new Float32Array(points.length * 3);
points.forEach((point, i) => {
positions[i * 3] = point.x;
positions[i * 3 + 1] = point.y;
positions[i * 3 + 2] = point.z;
});
const geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
const material = new THREE.LineBasicMaterial({
color: color,
linewidth: 2
});
const line = new THREE.Line(geometry, material);
line.geometry.setDrawRange(0, 0);
this.scene.add(line);
this.trails.push(line);
// Create sphere for current position
const sphereGeometry = new THREE.SphereGeometry(0.05, 16, 16);
const sphereMaterial = new THREE.MeshPhongMaterial({
color: color,
emissive: color,
emissiveIntensity: 0.5
});
const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
sphere.visible = false;
this.scene.add(sphere);
this.objects.push(sphere);
});
}
play() {
this.isPlaying = true;
}
pause() {
this.isPlaying = false;
}
reset() {
this.currentFrame = 0;
this.isPlaying = false;
this.objects.forEach(obj => obj.visible = false);
this.trails.forEach(trail => {
trail.geometry.setDrawRange(0, 0);
});
}
setShowTrails(show) {
this.showTrails = show;
this.trails.forEach(trail => trail.visible = show);
}
update() {
if (this.isPlaying && this.trajectories.length > 0) {
this.currentFrame++;
// Update each trajectory
this.trajectories.forEach((traj, idx) => {
const points = traj.points;
const currentPoint = points.find(p => p.frame === this.currentFrame);
if (currentPoint) {
const obj = this.objects[idx];
obj.position.set(currentPoint.x, currentPoint.y, currentPoint.z);
obj.visible = true;
// Update trail
if (this.showTrails) {
const trail = this.trails[idx];
const visiblePoints = points.filter(p => p.frame <= this.currentFrame);
trail.geometry.setDrawRange(0, visiblePoints.length);
}
}
});
// Loop animation
if (this.currentFrame >= this.maxFrames) {
this.currentFrame = 0;
}
}
}
animate() {
requestAnimationFrame(() => this.animate());
this.update();
this.renderer.render(this.scene, this.camera);
}
onWindowResize() {
this.camera.aspect = this.container.clientWidth / this.container.clientHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(this.container.clientWidth, this.container.clientHeight);
}
destroy() {
window.removeEventListener('resize', () => this.onWindowResize());
this.renderer.dispose();
while(this.container.firstChild) {
this.container.removeChild(this.container.firstChild);
}
}
} |