embedded-charts 0.3.0

A rich graph framework for embedded systems using embedded-graphics with std/no_std support
Documentation
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Curve interpolation algorithms for smooth data visualization.
//!
//! This module provides various interpolation algorithms optimized for embedded systems:
//! - Cubic spline interpolation for smooth curves
//! - Catmull-Rom spline for balanced smoothness and control
//! - Bezier curve interpolation for artistic control
//! - Linear interpolation as a fallback
//!
//! All algorithms are designed to work with the no_std environment and use
//! static allocation for memory efficiency.

use crate::data::Point2D;
use crate::error::{ChartError, ChartResult};
use heapless::Vec;

/// Maximum number of interpolated points that can be generated
pub const MAX_INTERPOLATED_POINTS: usize = 512;

/// Type of curve interpolation to use
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InterpolationType {
    /// Linear interpolation - straight lines between points
    Linear,
    /// Cubic spline interpolation - smooth curves through all points
    CubicSpline,
    /// Catmull-Rom spline - smooth curves with local control
    CatmullRom,
    /// Bezier curve approximation - artistic smooth curves
    Bezier,
}

/// Configuration for curve interpolation
#[derive(Debug, Clone)]
pub struct InterpolationConfig {
    /// Type of interpolation to use
    pub interpolation_type: InterpolationType,
    /// Number of points to generate between each pair of data points
    pub subdivisions: u32,
    /// Tension parameter for splines (0.0 = loose, 1.0 = tight)
    pub tension: f32,
    /// Whether to create a closed curve (connect last point to first)
    pub closed: bool,
}

impl Default for InterpolationConfig {
    fn default() -> Self {
        Self {
            interpolation_type: InterpolationType::CubicSpline,
            subdivisions: 8,
            tension: 0.5,
            closed: false,
        }
    }
}

/// Curve interpolator that generates smooth curves from discrete data points
pub struct CurveInterpolator;

impl CurveInterpolator {
    /// Interpolate a series of 2D points to create a smooth curve
    ///
    /// # Arguments
    /// * `points` - Input data points to interpolate
    /// * `config` - Interpolation configuration
    ///
    /// # Returns
    /// A vector of interpolated points that form a smooth curve
    pub fn interpolate(
        points: &[Point2D],
        config: &InterpolationConfig,
    ) -> ChartResult<Vec<Point2D, MAX_INTERPOLATED_POINTS>> {
        if points.len() < 2 {
            return Err(ChartError::InsufficientData);
        }

        match config.interpolation_type {
            InterpolationType::Linear => Self::linear_interpolation(points, config),
            InterpolationType::CubicSpline => Self::cubic_spline_interpolation(points, config),
            InterpolationType::CatmullRom => Self::catmull_rom_interpolation(points, config),
            InterpolationType::Bezier => Self::bezier_interpolation(points, config),
        }
    }

    /// Linear interpolation - simply subdivides straight lines
    fn linear_interpolation(
        points: &[Point2D],
        config: &InterpolationConfig,
    ) -> ChartResult<Vec<Point2D, MAX_INTERPOLATED_POINTS>> {
        let mut result = Vec::new();

        for i in 0..points.len() - 1 {
            let p0 = points[i];
            let p1 = points[i + 1];

            // Add the starting point
            result.push(p0).map_err(|_| ChartError::MemoryFull)?;

            // Add subdivided points
            for j in 1..config.subdivisions {
                let t = j as f32 / config.subdivisions as f32;
                let x = p0.x + t * (p1.x - p0.x);
                let y = p0.y + t * (p1.y - p0.y);
                result
                    .push(Point2D::new(x, y))
                    .map_err(|_| ChartError::MemoryFull)?;
            }
        }

        // Add the final point
        if let Some(last) = points.last() {
            result.push(*last).map_err(|_| ChartError::MemoryFull)?;
        }

        Ok(result)
    }

    /// Cubic spline interpolation for smooth curves
    fn cubic_spline_interpolation(
        points: &[Point2D],
        config: &InterpolationConfig,
    ) -> ChartResult<Vec<Point2D, MAX_INTERPOLATED_POINTS>> {
        let mut result = Vec::new();
        let n = points.len();

        if n < 3 {
            return Self::linear_interpolation(points, config);
        }

        // Calculate derivatives for natural cubic spline
        let mut derivatives = Vec::<f32, 256>::new();
        for _i in 0..n {
            derivatives.push(0.0).map_err(|_| ChartError::MemoryFull)?;
        }

        // Calculate second derivatives using simplified approach
        for i in 1..n - 1 {
            let h1 = points[i].x - points[i - 1].x;
            let h2 = points[i + 1].x - points[i].x;
            let delta1 = (points[i].y - points[i - 1].y) / h1;
            let delta2 = (points[i + 1].y - points[i].y) / h2;
            derivatives[i] = 2.0 * (delta2 - delta1) / (h1 + h2);
        }

        // Generate interpolated points
        for i in 0..n - 1 {
            let p0 = points[i];
            let p1 = points[i + 1];
            let d0 = derivatives[i];
            let d1 = derivatives[i + 1];

            result.push(p0).map_err(|_| ChartError::MemoryFull)?;

            let h = p1.x - p0.x;
            for j in 1..config.subdivisions {
                let t = j as f32 / config.subdivisions as f32;
                let t2 = t * t;
                let t3 = t2 * t;

                // Cubic Hermite interpolation
                let h00 = 2.0 * t3 - 3.0 * t2 + 1.0;
                let h10 = t3 - 2.0 * t2 + t;
                let h01 = -2.0 * t3 + 3.0 * t2;
                let h11 = t3 - t2;

                let x = p0.x + t * h;
                let y = h00 * p0.y + h10 * h * d0 + h01 * p1.y + h11 * h * d1;

                result
                    .push(Point2D::new(x, y))
                    .map_err(|_| ChartError::MemoryFull)?;
            }
        }

        if let Some(last) = points.last() {
            result.push(*last).map_err(|_| ChartError::MemoryFull)?;
        }

        Ok(result)
    }

    /// Catmull-Rom spline interpolation
    fn catmull_rom_interpolation(
        points: &[Point2D],
        config: &InterpolationConfig,
    ) -> ChartResult<Vec<Point2D, MAX_INTERPOLATED_POINTS>> {
        let mut result = Vec::new();
        let n = points.len();

        if n < 3 {
            return Self::linear_interpolation(points, config);
        }

        // Process each segment
        for i in 0..n - 1 {
            // Get control points (with boundary handling)
            let p0 = if i == 0 { points[0] } else { points[i - 1] };
            let p1 = points[i];
            let p2 = points[i + 1];
            let p3 = if i + 2 < n {
                points[i + 2]
            } else {
                points[n - 1]
            };

            result.push(p1).map_err(|_| ChartError::MemoryFull)?;

            // Generate subdivided points
            for j in 1..config.subdivisions {
                let t = j as f32 / config.subdivisions as f32;
                let t2 = t * t;
                let t3 = t2 * t;

                // Catmull-Rom basis functions
                let x = 0.5
                    * ((2.0 * p1.x)
                        + (-p0.x + p2.x) * t
                        + (2.0 * p0.x - 5.0 * p1.x + 4.0 * p2.x - p3.x) * t2
                        + (-p0.x + 3.0 * p1.x - 3.0 * p2.x + p3.x) * t3);

                let y = 0.5
                    * ((2.0 * p1.y)
                        + (-p0.y + p2.y) * t
                        + (2.0 * p0.y - 5.0 * p1.y + 4.0 * p2.y - p3.y) * t2
                        + (-p0.y + 3.0 * p1.y - 3.0 * p2.y + p3.y) * t3);

                result
                    .push(Point2D::new(x, y))
                    .map_err(|_| ChartError::MemoryFull)?;
            }
        }

        if let Some(last) = points.last() {
            result.push(*last).map_err(|_| ChartError::MemoryFull)?;
        }

        Ok(result)
    }

    /// Bezier curve interpolation
    fn bezier_interpolation(
        points: &[Point2D],
        config: &InterpolationConfig,
    ) -> ChartResult<Vec<Point2D, MAX_INTERPOLATED_POINTS>> {
        let mut result = Vec::new();
        let n = points.len();

        if n < 3 {
            return Self::linear_interpolation(points, config);
        }

        // Generate control points for quadratic Bezier curves
        for i in 0..n - 1 {
            let p0 = points[i];
            let p2 = points[i + 1];

            // Simple control point calculation
            let mid_x = (p0.x + p2.x) * 0.5;
            let mid_y = (p0.y + p2.y) * 0.5;

            // Add some curvature based on tension
            let offset_x = (p2.y - p0.y) * config.tension * 0.2;
            let offset_y = (p0.x - p2.x) * config.tension * 0.2;

            let p1 = Point2D::new(mid_x + offset_x, mid_y + offset_y);

            result.push(p0).map_err(|_| ChartError::MemoryFull)?;

            // Generate quadratic Bezier curve
            for j in 1..config.subdivisions {
                let t = j as f32 / config.subdivisions as f32;
                let one_minus_t = 1.0 - t;
                let t2 = t * t;
                let one_minus_t2 = one_minus_t * one_minus_t;

                let x = one_minus_t2 * p0.x + 2.0 * one_minus_t * t * p1.x + t2 * p2.x;
                let y = one_minus_t2 * p0.y + 2.0 * one_minus_t * t * p1.y + t2 * p2.y;

                result
                    .push(Point2D::new(x, y))
                    .map_err(|_| ChartError::MemoryFull)?;
            }
        }

        if let Some(last) = points.last() {
            result.push(*last).map_err(|_| ChartError::MemoryFull)?;
        }

        Ok(result)
    }

    /// Smooth a single point using neighboring points
    pub fn smooth_point(
        points: &[Point2D],
        index: usize,
        smoothing_factor: f32,
    ) -> ChartResult<Point2D> {
        if index >= points.len() {
            return Err(ChartError::InvalidRange);
        }

        let n = points.len();
        if n < 3 || index == 0 || index == n - 1 {
            return Ok(points[index]);
        }

        let prev = points[index - 1];
        let curr = points[index];
        let next = points[index + 1];

        // Apply smoothing using weighted average
        let factor = smoothing_factor.clamp(0.0, 1.0);
        let smoothed_x = curr.x * (1.0 - factor) + (prev.x + next.x) * 0.5 * factor;
        let smoothed_y = curr.y * (1.0 - factor) + (prev.y + next.y) * 0.5 * factor;

        Ok(Point2D::new(smoothed_x, smoothed_y))
    }

    /// Apply smoothing to an entire series of points
    pub fn smooth_series(
        points: &[Point2D],
        smoothing_factor: f32,
        iterations: u32,
    ) -> ChartResult<Vec<Point2D, 256>> {
        let mut working_points = Vec::new();
        for point in points {
            working_points
                .push(*point)
                .map_err(|_| ChartError::MemoryFull)?;
        }

        for _ in 0..iterations {
            let mut smoothed = Vec::new();
            for i in 0..working_points.len() {
                let smoothed_point = Self::smooth_point(&working_points, i, smoothing_factor)?;
                smoothed
                    .push(smoothed_point)
                    .map_err(|_| ChartError::MemoryFull)?;
            }
            working_points = smoothed;
        }

        Ok(working_points)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_linear_interpolation() {
        let mut points = heapless::Vec::<Point2D, 16>::new();
        points.push(Point2D::new(0.0, 0.0)).unwrap();
        points.push(Point2D::new(1.0, 1.0)).unwrap();
        let config = InterpolationConfig {
            interpolation_type: InterpolationType::Linear,
            subdivisions: 4,
            ..Default::default()
        };

        let result = CurveInterpolator::interpolate(&points, &config).unwrap();
        assert!(result.len() > 2);
        assert_eq!(result[0], points[0]);
        assert_eq!(result[result.len() - 1], points[1]);
    }

    #[test]
    fn test_catmull_rom_interpolation() {
        let mut points = heapless::Vec::<Point2D, 16>::new();
        points.push(Point2D::new(0.0, 0.0)).unwrap();
        points.push(Point2D::new(1.0, 1.0)).unwrap();
        points.push(Point2D::new(2.0, 0.0)).unwrap();
        let config = InterpolationConfig {
            interpolation_type: InterpolationType::CatmullRom,
            subdivisions: 8,
            ..Default::default()
        };

        let result = CurveInterpolator::interpolate(&points, &config).unwrap();
        assert!(result.len() > points.len());
    }

    #[test]
    fn test_point_smoothing() {
        let mut points = heapless::Vec::<Point2D, 16>::new();
        points.push(Point2D::new(0.0, 0.0)).unwrap();
        points.push(Point2D::new(1.0, 10.0)).unwrap(); // Spike
        points.push(Point2D::new(2.0, 0.0)).unwrap();

        let smoothed = CurveInterpolator::smooth_point(&points, 1, 0.5).unwrap();
        assert!(smoothed.y < 10.0); // Should be smoothed down
        assert!(smoothed.y > 0.0); // But still positive
    }

    #[test]
    fn test_series_smoothing() {
        let mut points = heapless::Vec::<Point2D, 16>::new();
        points.push(Point2D::new(0.0, 0.0)).unwrap();
        points.push(Point2D::new(1.0, 10.0)).unwrap();
        points.push(Point2D::new(2.0, 0.0)).unwrap();
        points.push(Point2D::new(3.0, 10.0)).unwrap();
        points.push(Point2D::new(4.0, 0.0)).unwrap();

        let smoothed = CurveInterpolator::smooth_series(&points, 0.3, 2).unwrap();
        assert_eq!(smoothed.len(), points.len());

        // Check that spikes are reduced
        assert!(smoothed[1].y < points[1].y);
        assert!(smoothed[3].y < points[3].y);
    }
}