rustial-engine 0.0.1

Framework-agnostic 2.5D map engine for rustial
Documentation
//! Point-cloud / scatter overlay layer.

use std::any::Any;

use super::{ColorRamp, PointInstanceSet};
use crate::layer::{Layer, LayerId, LayerKind};

/// A point-cloud / scatter visualization layer.
#[derive(Debug, Clone)]
pub struct PointCloudLayer {
    id: LayerId,
    name: String,
    visible: bool,
    opacity: f32,
    z_index: i32,
    /// Point instances.
    pub points: PointInstanceSet,
    /// Fallback colour ramp for points without per-instance colour.
    pub ramp: ColorRamp,
}

impl PointCloudLayer {
    /// Create a new point-cloud layer.
    pub fn new(name: impl Into<String>, points: PointInstanceSet, ramp: ColorRamp) -> Self {
        Self {
            id: LayerId::next(),
            name: name.into(),
            visible: true,
            opacity: 1.0,
            z_index: 0,
            points,
            ramp,
        }
    }

    /// Replace the point set.
    pub fn set_points(&mut self, points: PointInstanceSet) {
        self.points = points;
    }
}

impl Layer for PointCloudLayer {
    fn id(&self) -> LayerId {
        self.id
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn kind(&self) -> LayerKind {
        LayerKind::Visualization
    }

    fn visible(&self) -> bool {
        self.visible
    }

    fn set_visible(&mut self, visible: bool) {
        self.visible = visible;
    }

    fn opacity(&self) -> f32 {
        self.opacity
    }

    fn set_opacity(&mut self, opacity: f32) {
        self.opacity = opacity.clamp(0.0, 1.0);
    }

    fn z_index(&self) -> i32 {
        self.z_index
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::visualization::{ColorStop, PointInstance};
    use rustial_math::GeoCoord;

    fn test_ramp() -> ColorRamp {
        ColorRamp::new(vec![
            ColorStop {
                value: 0.0,
                color: [0.0, 0.0, 1.0, 1.0],
            },
            ColorStop {
                value: 1.0,
                color: [1.0, 0.0, 0.0, 1.0],
            },
        ])
    }

    #[test]
    fn point_cloud_layer_basics() {
        let points = PointInstanceSet::new(vec![PointInstance::new(
            GeoCoord::from_lat_lon(0.0, 0.0),
            5.0,
        )
        .with_pick_id(1)]);
        let layer = PointCloudLayer::new("points", points, test_ramp());
        assert_eq!(layer.name(), "points");
        assert_eq!(layer.points.len(), 1);
        assert_eq!(layer.points.points[0].pick_id, 1);
    }

    #[test]
    fn point_cloud_layer_downcast() {
        let layer: Box<dyn Layer> = Box::new(PointCloudLayer::new(
            "test",
            PointInstanceSet::default(),
            test_ramp(),
        ));
        assert!(layer.as_any().downcast_ref::<PointCloudLayer>().is_some());
    }
}