From 3d57f6119639e0ec48e549842f07fe68ea7e5b85 Mon Sep 17 00:00:00 2001 From: tsuki Date: Wed, 6 Mar 2024 00:28:15 +0800 Subject: [PATCH] sync --- radarg_plugin_interface/src/lib.rs | 2 +- src/components/app.rs | 18 +++++++++-- src/data_utils.rs | 44 +++++++++++++++++++++++++++ src/main.rs | 1 + src/pipeline/element.rs | 30 +++++++++++++----- src/pipeline/element_impl.rs | 6 ++-- src/pipeline/mod.rs | 1 + src/pipeline/new_pipeline.rs | 11 ++++--- src/widgets/render/interior/layers.rs | 25 +++++++-------- src/widgets/render/interior/mod.rs | 2 +- 10 files changed, 110 insertions(+), 30 deletions(-) create mode 100644 src/data_utils.rs diff --git a/radarg_plugin_interface/src/lib.rs b/radarg_plugin_interface/src/lib.rs index c8f8380..5e990f0 100644 --- a/radarg_plugin_interface/src/lib.rs +++ b/radarg_plugin_interface/src/lib.rs @@ -67,7 +67,7 @@ pub struct MetaData { } #[repr(C)] -#[derive(StableAbi, Clone, Debug)] +#[derive(StableAbi, Clone, Copy, Debug)] #[sabi(impl_InterfaceType(Sync, Send, Debug))] pub enum PluginResultType { // Single diff --git a/src/components/app.rs b/src/components/app.rs index 9bc955f..6e7d860 100644 --- a/src/components/app.rs +++ b/src/components/app.rs @@ -5,7 +5,8 @@ use super::{ setting::SettingModel, ControlPanelOutputMsg, TimelineMsg, }; -use crate::pipeline::element::Element; +use crate::pipeline::element::{Element, InstantElement, InstantElementDrawerType}; +use crate::data_utils::plugin_result_impl; use crate::{ coords::{ cms::CMS, @@ -45,6 +46,8 @@ use std::{ use radarg_plugin_interface::PluginResult; use tokio::{sync::oneshot, task}; use tracing::{debug, error, info, warn}; +use crate::pipeline::{GridElementImpl, OffscreenRenderer}; +use crate::widgets::AssoElement; relm4::new_action_group!(FileActionGroup, "file"); relm4::new_stateless_action!(OpenAction, FileActionGroup, "open"); @@ -216,7 +219,18 @@ impl Component for AppModel { .forward(sender.input_sender(), move |response| match response { OpenDialogResponse::Accept(path) => { let data = Self::open_file_only(path); - let mut layer = Layer::new(true, "New Layer".to_string(), None); + let element_impl = plugin_result_impl(&data); + let mut renderer = OffscreenRenderer::new(3000,3000).unwrap(); + let mut canvas = renderer.create_canvas(); + let mut cms_ref = dialog_cms.lock().unwrap(); + + let data = element_impl.render(&data,&mut canvas,&mut (*cms_ref)); + let element = Element::create_instant( + InstantElementDrawerType::Prepared(data), + dialog_dispatcher.clone(), + "New Element".to_string(), + ).get_instance(); + let layer = Layer::new(true, "New Layer".to_string(), AssoElement::Instant(element)); AppMsg::NewLayer(layer) } _ => AppMsg::Close, diff --git a/src/data_utils.rs b/src/data_utils.rs new file mode 100644 index 0000000..0e61ab2 --- /dev/null +++ b/src/data_utils.rs @@ -0,0 +1,44 @@ +use radarg_plugin_interface::{CoordType, PluginResult, PluginResultType}; +use crate::pipeline::element::ElementImpl; +use crate::pipeline::GridElementImpl; +use crate::utils::*; +macro_rules! data_to_grid { + ($_type:ident,$(($branch:path ,$boundary_norm: expr)),+) => { + match $_type { + $( + $branch => { + let element_impl = GridElementImpl::new($boundary_norm); + Box::new(element_impl) + } + ),+ + _ => panic!("Invalid type") + } + + }; +} +pub fn plugin_result_impl(a: &PluginResult) -> Box { + let block= a.blocks.first().unwrap(); + match block.coord_type { + CoordType::Cartesian => { + let _type = block.data_type; + data_to_grid!( + _type, + (PluginResultType::R, create_dbz_boundarynorm()), + (PluginResultType::V, create_vel_boundarynorm()), + (PluginResultType::CC, create_cc_boundarynorm()), + (PluginResultType::ZDR, create_zdr_boundarynorm()), + (PluginResultType::PHIDP, create_phidp_boundarynorm()), + (PluginResultType::KDP, create_kdp_boundarynorm()), + (PluginResultType::DBZ, create_dbz_boundarynorm()), + (PluginResultType::VIL, create_vil_boundarynorm()), + (PluginResultType::OHP, create_vil_boundarynorm()), + (PluginResultType::THP, create_vil_boundarynorm()), + (PluginResultType::ET, create_et_boundarynorm()) + ) + } + CoordType::Other | CoordType::Polar => { + panic!("Invalid type"); + } + } +} + diff --git a/src/main.rs b/src/main.rs index e3c4ae9..18d75f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ use once_cell::{sync::Lazy as SafeLazy, unsync::Lazy as UnsafeLazy}; use tracing::info; use tracing_subscriber; mod widgets; +mod data_utils; const APP_ID: &str = "org.tsuki.radar_g"; static RUNTIME: SafeLazy = diff --git a/src/pipeline/element.rs b/src/pipeline/element.rs index fac1e69..e89e4c1 100644 --- a/src/pipeline/element.rs +++ b/src/pipeline/element.rs @@ -23,6 +23,7 @@ use std::{ pin::Pin, sync::{Arc, Mutex}, }; +use radarg_plugin_interface::PluginResult; use tokio::sync::{ oneshot::{channel, Receiver, Sender}, Notify, @@ -35,7 +36,7 @@ static ELEMENT_ID: AtomicUsize = AtomicUsize::new(0); pub type Data = Box; pub type Buffer = Arc, (Option, Option)>>>; -type DrawFunc = Box; +type DrawFunc = Arc>; type IResult = Result; #[derive(Debug)] @@ -55,7 +56,7 @@ impl Element { } pub fn create_instant( _type: InstantElementDrawerType, - dispatcher: Arc, + dispatcher: Rc, key: String, ) -> Self { Element::Instant(InstantElement::new(_type, dispatcher, key)) @@ -73,6 +74,20 @@ impl Element { Element::Instant(e) => e.key.clone(), } } + + pub fn get_instance(mut self) -> InstantElement { + match self { + Element::TimeSeries(e) => panic!("TimeSeries element does not have instance"), + Element::Instant(v) => v + } + } + + pub fn get_time_series_instance(mut self) -> TimeSeriesElement { + match self { + Element::TimeSeries(v) => v, + Element::Instant(e) => panic!("Instant element does not have instance"), + } + } } #[derive(Debug)] pub struct TimeSeriesElement { @@ -86,6 +101,7 @@ pub struct TimeSeriesElement { dispatcher: Rc, } +#[derive(Clone)] pub enum InstantElementDrawerType { Draw(DrawFunc), Prepared(Target), @@ -96,20 +112,20 @@ impl Debug for InstantElementDrawerType { f.debug_struct("InstantElementDrawerType").finish() } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct InstantElement { pub id: ElementID, pub key: String, draw_type: InstantElementDrawerType, - dispatcher: Arc, + dispatcher: Rc, } pub trait ElementImpl: Debug + Send + Sync + 'static { - fn render(&self, data: Box, canvas: &mut CanvasWrapper, cms: &mut CMS) -> Target; + fn render(&self, data: &PluginResult, canvas: &mut CanvasWrapper, cms: &mut CMS) -> Target; } impl InstantElement { - fn new(_type: InstantElementDrawerType, dispatcher: Arc, key: String) -> Self { + fn new(_type: InstantElementDrawerType, dispatcher: Rc, key: String) -> Self { let id = ELEMENT_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed); Self { id, @@ -201,7 +217,7 @@ impl TimeSeriesElement { date_time, true, 3, - Arc::new(move |canvas, cms| imp.render(Box::new(()), canvas, cms)), + Arc::new(move |data ,canvas, cms| imp.render(data, canvas, cms)), self.cms.clone(), ); diff --git a/src/pipeline/element_impl.rs b/src/pipeline/element_impl.rs index 186d390..6c118d7 100644 --- a/src/pipeline/element_impl.rs +++ b/src/pipeline/element_impl.rs @@ -8,6 +8,7 @@ use crate::widgets::{LayerImpl, CMS}; use num_traits::{AsPrimitive, FromPrimitive, Num, NumOps}; use std::any::Any; use std::fmt::Debug; +use radarg_plugin_interface::PluginResult; #[derive(Debug)] pub struct GridElementImpl @@ -46,11 +47,12 @@ where { fn render( &self, - data: Box, + data: &PluginResult, canvas: &mut CanvasWrapper, cms: &mut crate::coords::cms::CMS, ) -> crate::pipeline::element::Target { - let data = data.downcast::>().unwrap(); + let first_block = data.blocks.first().unwrap(); + let data = first_block.clone().into(); let result = self.renderer.render(canvas, cms, &data, (3000.0, 3000.0)); result } diff --git a/src/pipeline/mod.rs b/src/pipeline/mod.rs index 739a4f2..de3afb7 100644 --- a/src/pipeline/mod.rs +++ b/src/pipeline/mod.rs @@ -11,3 +11,4 @@ pub use dispatcher::Dispatcher; pub use element::RenderResult; pub use new_pipeline::Pipeline; pub use offscreen_renderer::OffscreenRenderer; +pub use element_impl::*; diff --git a/src/pipeline/new_pipeline.rs b/src/pipeline/new_pipeline.rs index 31985a1..0a4ae16 100644 --- a/src/pipeline/new_pipeline.rs +++ b/src/pipeline/new_pipeline.rs @@ -23,6 +23,7 @@ use std::{ rc::Rc, sync::{Arc, Mutex}, }; +use radarg_plugin_interface::PluginResult; use tokio::{ sync::{mpsc, oneshot}, task, @@ -83,7 +84,7 @@ impl Pipeline { current_time: DateTime, check_existed: bool, max_retry_time: usize, - task: Arc Target + Send + Sync>, + task: Arc Target + Send + Sync>, cms: Arc>, ) -> Option>> { let paths = { @@ -122,25 +123,25 @@ impl Pipeline { fn worker( &self, datetime: DateTime, - task: Arc Target + Send + Sync>, + task: Arc Target + Send + Sync>, cms: Arc>, path: impl AsRef + Send + 'static, ) -> BoxFuture<'static, RenderR> { Box::pin(async move { let loader = PLUGIN_MANAGER.get_plugin_by_name("etws_loader").unwrap(); let mut loaded_data = loader.load(path.as_ref().into()).unwrap(); - let first_block = loaded_data.blocks.pop().unwrap(); + let meta = loaded_data.meta.clone().into(); let handle = task::spawn_blocking(move || { let mut offscreen_renderer = OffscreenRenderer::new(3000, 3000).unwrap(); let mut canvas_wrapper = offscreen_renderer.create_canvas(); let mut cms = cms.lock().unwrap(); - let target = task(&mut canvas_wrapper, &mut cms); + let target = task(&loaded_data,&mut canvas_wrapper, &mut cms); target }); let target = handle.await.unwrap(); - Ok(RenderResult::new(target, loaded_data.meta.into())) + Ok(RenderResult::new(target, meta)) }) } diff --git a/src/widgets/render/interior/layers.rs b/src/widgets/render/interior/layers.rs index f576bd7..4188a76 100644 --- a/src/widgets/render/interior/layers.rs +++ b/src/widgets/render/interior/layers.rs @@ -23,6 +23,7 @@ type PrepareFunc = Arc< type DrawFunc = Arc; pub type LayerImplSync = Arc>>; +#[derive(Clone)] pub enum AssoElement { TimeSeries(Arc>), Instant(element::InstantElement), @@ -60,18 +61,18 @@ impl Layer { pub fn draw(&self, render: &Render, window_size: (f32, f32)) { if self.visiable { - if let Some(element) = &self.associated_element { - let element = element.lock().unwrap(); - match *element { - Element::TimeSeries(ref e) => { - if self.time.is_none() { - return; - } - let time = self.time.unwrap(); - } - Element::Instant(ref e) => {} - } - } + // if let Some(element) = &self.associated_element { + // let element = element.lock().unwrap(); + // match *element { + // Element::TimeSeries(ref e) => { + // if self.time.is_none() { + // return; + // } + // let time = self.time.unwrap(); + // } + // Element::Instant(ref e) => {} + // } + // } } } diff --git a/src/widgets/render/interior/mod.rs b/src/widgets/render/interior/mod.rs index 95a7aa0..506dee8 100644 --- a/src/widgets/render/interior/mod.rs +++ b/src/widgets/render/interior/mod.rs @@ -2,7 +2,7 @@ mod imp; mod layers; use super::super::Render; use femtovg::{renderer::OpenGl, Canvas}; -pub use layers::{Layer, LayerImpl, LayerImplSync}; +pub use layers::{Layer, LayerImpl, LayerImplSync, AssoElement}; use std::cell::Ref; use super::imp::{RenderConfig, RenderStatus};