Skip to main content

webizen_studio/render/
scene.rs

1//! Geometric primitives for manifold projection: vector math, projection viewpoint, projected points.
2//!
3//! These compile on every target (no `web-sys`), so the same projection viewpoint logic
4//! is shared across different backends for geometric examination of the 10D epistemic manifold.
5//!
6//! NOTE: This is NOT traditional 3D scene rendering - these are geometric operations
7//! for examining relationships in the high-dimensional epistemic manifold.
8
9/// Minimal 3D vector for geometric calculations in manifold projection.
10#[derive(Clone, Copy, Debug, Default, PartialEq)]
11pub struct Vec3 {
12    pub x: f64,
13    pub y: f64,
14    pub z: f64,
15}
16
17impl Vec3 {
18    /// Creates a new instance.
19    pub fn new(x: f64, y: f64, z: f64) -> Self {
20        Self { x, y, z }
21    }
22
23    /// Vector subtraction.
24    pub fn sub(self, other: Self) -> Self {
25        Self::new(self.x - other.x, self.y - other.y, self.z - other.z)
26    }
27
28    /// Vector addition.
29    pub fn add(self, other: Self) -> Self {
30        Self::new(self.x + other.x, self.y + other.y, self.z + other.z)
31    }
32
33    /// Scalar multiply.
34    pub fn scale(self, k: f64) -> Self {
35        Self::new(self.x * k, self.y * k, self.z * k)
36    }
37
38    /// Dot product.
39    pub fn dot(self, other: Self) -> f64 {
40        self.x * other.x + self.y * other.y + self.z * other.z
41    }
42
43    /// Cross product.
44    pub fn cross(self, other: Self) -> Self {
45        Self::new(
46            self.y * other.z - self.z * other.y,
47            self.z * other.x - self.x * other.z,
48            self.x * other.y - self.y * other.x,
49        )
50    }
51
52    /// Vector length.
53    pub fn length(self) -> f64 {
54        self.dot(self).sqrt()
55    }
56
57    /// Unit vector.
58    pub fn normalize(self) -> Self {
59        let len = self.length();
60        if len <= f64::EPSILON {
61            Self::default()
62        } else {
63            Self::new(self.x / len, self.y / len, self.z / len)
64        }
65    }
66}
67
68/// A manifold point after projection into screen space, with retained depth so
69/// callers can fade/scale by distance for geometric examination of epistemic relationships.
70#[derive(Clone, Copy, Debug, Default, PartialEq)]
71pub struct ScreenPoint {
72    pub x: f64,
73    pub y: f64,
74    pub depth: f64,
75}
76
77/// A projection viewpoint for examining the 10D epistemic manifold.
78/// `focal_scale` multiplies `min(viewport)` to set
79/// the effective focal length, matching the original physics-surface tuning.
80#[derive(Clone, Copy, Debug, PartialEq)]
81pub struct Camera {
82    pub eye: Vec3,
83    pub target: Vec3,
84    pub world_up: Vec3,
85    pub focal_scale: f64,
86}
87
88impl Default for Camera {
89    fn default() -> Self {
90        Self {
91            eye: Vec3::new(0.0, 8.0, 18.0),
92            target: Vec3::new(0.0, 0.5, 0.0),
93            world_up: Vec3::new(0.0, 1.0, 0.0),
94            focal_scale: 0.88,
95        }
96    }
97}
98
99impl Camera {
100    /// Project a manifold point to screen space for geometric examination.
101    /// Returns `None` for points at or behind the near plane.
102    /// This is NOT traditional 3D rendering - it's geometric projection for examining
103    /// relationships in the 10D epistemic manifold.
104    pub fn project(&self, world: Vec3, viewport: (f64, f64)) -> Option<ScreenPoint> {
105        let forward = self.target.sub(self.eye).normalize();
106        let right = forward.cross(self.world_up).normalize();
107        let up = right.cross(forward).normalize();
108        let relative = world.sub(self.eye);
109
110        let view_x = relative.dot(right);
111        let view_y = relative.dot(up);
112        let view_z = relative.dot(forward);
113
114        if view_z <= 0.2 {
115            return None;
116        }
117
118        let focal = viewport.0.min(viewport.1) * self.focal_scale;
119        Some(ScreenPoint {
120            x: viewport.0 * 0.5 + (view_x / view_z) * focal,
121            y: viewport.1 * 0.52 - (view_y / view_z) * focal,
122            depth: view_z,
123        })
124    }
125
126    /// Rotate projection viewpoint around target using yaw (horizontal) and pitch (vertical) angles.
127    /// This adjusts the geometric examination viewpoint of the 10D epistemic manifold.
128    /// All calculations use stack-allocated f64 values for zero-heap compliance.
129    pub fn orbit(&mut self, yaw: f64, pitch: f64) {
130        // Calculate vector from target to eye (camera radius)
131        let dx = self.eye.x - self.target.x;
132        let dy = self.eye.y - self.target.y;
133        let dz = self.eye.z - self.target.z;
134
135        // Current spherical coordinates
136        let radius = (dx * dx + dy * dy + dz * dz).sqrt();
137        let current_yaw = dz.atan2(dx);
138        let current_pitch = (dy / radius).asin();
139
140        // Apply rotation with pitch clamping to avoid gimbal lock
141        let new_yaw = current_yaw + yaw;
142        let new_pitch = (current_pitch + pitch).clamp(
143            -std::f64::consts::FRAC_PI_2 + 0.01,
144            std::f64::consts::FRAC_PI_2 - 0.01,
145        );
146
147        // Convert back to Cartesian coordinates (stack-allocated)
148        let cos_pitch = new_pitch.cos();
149        let sin_pitch = new_pitch.sin();
150        let cos_yaw = new_yaw.cos();
151        let sin_yaw = new_yaw.sin();
152
153        // Update camera position
154        self.eye.x = self.target.x + radius * cos_pitch * cos_yaw;
155        self.eye.y = self.target.y + radius * sin_pitch;
156        self.eye.z = self.target.z + radius * cos_pitch * sin_yaw;
157    }
158
159    /// Zoom projection viewpoint in/out by adjusting distance to target.
160    /// This adjusts the geometric examination scale of the 10D epistemic manifold.
161    /// Uses stack-allocated f64 values for zero-heap compliance.
162    pub fn zoom(&mut self, delta: f64) {
163        // Calculate current distance to target
164        let dx = self.eye.x - self.target.x;
165        let dy = self.eye.y - self.target.y;
166        let dz = self.eye.z - self.target.z;
167        let current_distance = (dx * dx + dy * dy + dz * dz).sqrt();
168
169        // Apply zoom with minimum distance clamp
170        let zoom_factor = (-delta * 0.1).exp(); // Smooth exponential zoom
171        let new_distance = (current_distance * zoom_factor).max(0.5);
172
173        // Scale the position vector
174        let scale = new_distance / current_distance;
175        self.eye.x = self.target.x + dx * scale;
176        self.eye.y = self.target.y + dy * scale;
177        self.eye.z = self.target.z + dz * scale;
178    }
179
180    /// Pan projection viewpoint in screen space (dx, dy are screen-relative deltas).
181    /// This adjusts the geometric examination position in the 10D epistemic manifold.
182    /// Uses stack-allocated f64 values for zero-heap compliance.
183    pub fn pan(&mut self, dx: f64, dy: f64) {
184        // Calculate camera forward and right vectors
185        let forward = self.target.sub(self.eye).normalize();
186        let right = forward.cross(self.world_up).normalize();
187        let up = right.cross(forward).normalize();
188
189        // Scale pan by distance to target for consistent feel
190        let distance = self.eye.sub(self.target).length();
191        let pan_scale = distance * 0.001;
192
193        // Apply pan to both position and target
194        let pan = right.scale(dx * pan_scale).add(up.scale(dy * pan_scale));
195        self.eye = self.eye.add(pan);
196        self.target = self.target.add(pan);
197    }
198}