// ============================================================================== // Nyx Universal Smart Board — Core Document & Ink Stroke Model // Module: nyx_smartboard_core.nyx // Invariant: O(V+E) Intra-Procedural Region Memory | Zero-GC Allocation // ============================================================================== module NyxSmartBoardCore; import nyx.math.vector; import nyx.runtime.region; /// 2D Input Point captured from stylus, finger touch, or optical sensor. public struct Point { public x: f32; public y: f32; public pressure: f32; // 0.0 (hover) to 1.0 (firm contact) public timestamp_ms: u32; // Milliseconds elapsed since stroke initiation } /// Continuous digital ink stroke with variable width and geometric bounds. public struct Stroke { public id: u64; public color_rgba: u32; // 0xRRGGBBAA public base_width_mm: f32; public points: Array; public is_selected: bool; public is_math_recognized: bool; public recognized_latex: string; public min_x: f32; public min_y: f32; public max_x: f32; public max_y: f32; } /// Page unit containing layered strokes and active graphical overlays. public struct BoardPage { public page_id: u64; public width_mm: f32; public height_mm: f32; public strokes: Array; public stroke_counter: u64; public background_mode: u32; // 0: Dark Slate Chalkboard, 1: Modern Whiteboard, 2: Coordinate Grid } public class SmartBoardDocument { private pages: Array; private active_page_index: u32; private memory_frame_active: bool; public constructor() { self.pages = new Array(); self.active_page_index = 0; self.memory_frame_active = true; // Initialize default primary board page (1920x1080mm virtual space) let default_page = BoardPage { page_id: 1, width_mm: 1920.0, height_mm: 1080.0, strokes: new Array(), stroke_counter: 0, background_mode: 0 }; self.pages.push(default_page); } /// Appends a new stroke to the active board page using stack region allocation. public method add_stroke(points: Array, color: u32, width: f32) -> u64 { region ink_region { let p_len = points.length(); if (p_len == 0) { return 0; } let min_x = points[0].x; let max_x = points[0].x; let min_y = points[0].y; let max_y = points[0].y; let i = 0; while (i < p_len) { let pt = points[i]; if (pt.x < min_x) { min_x = pt.x; } if (pt.x > max_x) { max_x = pt.x; } if (pt.y < min_y) { min_y = pt.y; } if (pt.y > max_y) { max_y = pt.y; } i = i + 1; } let current_page = self.pages[self.active_page_index]; current_page.stroke_counter = current_page.stroke_counter + 1; let stroke_id = current_page.stroke_counter; let new_stroke = Stroke { id: stroke_id, color_rgba: color, base_width_mm: width, points: points, is_selected: false, is_math_recognized: false, recognized_latex: "", min_x: min_x, min_y: min_y, max_x: max_x, max_y: max_y }; current_page.strokes.push(new_stroke); return stroke_id; } } /// Resamples a raw stroke into N equidistant arc-length control points (CROHME standard: N=32). public method resample_stroke_arclength(stroke: Stroke, target_points: u32) -> Array { region resample_region { let resampled = new Array(); let src_pts = stroke.points; let total_pts = src_pts.length(); if (total_pts <= 1 || target_points <= 1) { return src_pts; } // Calculate total polyline arc length let total_length = 0.0; let i = 1; while (i < total_pts) { let dx = src_pts[i].x - src_pts[i - 1].x; let dy = src_pts[i].y - src_pts[i - 1].y; total_length = total_length + sqrt(dx * dx + dy * dy); i = i + 1; } let step = total_length / (target_points - 1); let current_dist = 0.0; let current_segment_idx = 0; let accumulated_length = 0.0; resampled.push(src_pts[0]); let target_idx = 1; while (target_idx < target_points - 1) { let target_dist = target_idx * step; while (current_segment_idx < total_pts - 1) { let p1 = src_pts[current_segment_idx]; let p2 = src_pts[current_segment_idx + 1]; let seg_dx = p2.x - p1.x; let seg_dy = p2.y - p1.y; let seg_len = sqrt(seg_dx * seg_dx + seg_dy * seg_dy); if (accumulated_length + seg_len >= target_dist) { let t = (target_dist - accumulated_length) / seg_len; let interp_pt = Point { x: p1.x + seg_dx * t, y: p1.y + seg_dy * t, pressure: p1.pressure + (p2.pressure - p1.pressure) * t, timestamp_ms: (p1.timestamp_ms as f32 + (p2.timestamp_ms - p1.timestamp_ms) as f32 * t) as u32 }; resampled.push(interp_pt); break; } else { accumulated_length = accumulated_length + seg_len; current_segment_idx = current_segment_idx + 1; } } target_idx = target_idx + 1; } resampled.push(src_pts[total_pts - 1]); return resampled; } } /// Selects all strokes bounded inside a lasso polygon or rectangular area. public method select_strokes_in_bounds(min_x: f32, min_y: f32, max_x: f32, max_y: f32) -> Array { let selected_ids = new Array(); let current_page = self.pages[self.active_page_index]; let stroke_count = current_page.strokes.length(); let i = 0; while (i < stroke_count) { let s = current_page.strokes[i]; if (s.min_x >= min_x && s.max_x <= max_x && s.min_y >= min_y && s.max_y <= max_y) { s.is_selected = true; selected_ids.push(s.id); } i = i + 1; } return selected_ids; } public method get_active_page() -> BoardPage { return self.pages[self.active_page_index]; } }