diff --git a/examples/test_serialisation.rs b/examples/test_serialisation.rs new file mode 100644 index 0000000..ca0e220 --- /dev/null +++ b/examples/test_serialisation.rs @@ -0,0 +1,31 @@ +use serde_json::Result; +use trap_rust::trap::tracks::RenderableLines; + +/* +Compare output with the following python +```python +import numpy as np +import cv2 + +H =np.array([[ 2.47442963e+02, -7.01714050e+01, -9.71749119e+01], + [ 1.02328119e+01, 1.47185254e+02, 1.96295638e+02], + [-1.20921986e-03, -3.32735973e-02, 1.00000000e+00]]) + +points =[[1.,1.0],[2.0,2.0]] +print(cv2.perspectiveTransform(np.array([points]), H)) +``` + */ +fn main(){ + let jsonstring = "{\"lines\": [{\"points\": [{\"position\": [ 1.008936882019043, 7.654696464538574], \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0250474214553833, \"y\": 7.690868854522705}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0388848781585693, \"y\": 7.721379280090332}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0500627756118774, \"y\": 7.7452192306518555}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.058650016784668, \"y\": 7.762533187866211}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0649632215499878, \"y\": 7.774103164672852}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.069427251815796, \"y\": 7.780993461608887}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0724823474884033, \"y\": 7.7843122482299805}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.074531078338623, \"y\": 7.785078525543213}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0759128332138062, \"y\": 7.784152030944824}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0768933296203613, \"y\": 7.782205581665039}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.07766854763031, \"y\": 7.779738426208496}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0783727169036865, \"y\": 7.777091026306152}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0790910720825195, \"y\": 7.774479866027832}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.079870581626892, \"y\": 7.772024631500244}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.080731749534607, \"y\": 7.769777774810791}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0816771984100342, \"y\": 7.767746925354004}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0826985836029053, \"y\": 7.765912055969238}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0837825536727905, \"y\": 7.764240741729736}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0849148035049438, \"y\": 7.762696743011475}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0860803127288818, \"y\": 7.76124382019043}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0872671604156494, \"y\": 7.759851455688477}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0884658098220825, \"y\": 7.758495807647705}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0896704196929932, \"y\": 7.757160186767578}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0908781290054321, \"y\": 7.755831718444824}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0920900106430054, \"y\": 7.754508018493652}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.093311071395874, \"y\": 7.7531890869140625}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0945500135421753, \"y\": 7.751882553100586}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0958201885223389, \"y\": 7.750600337982178}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0971378087997437, \"y\": 7.749356269836426}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0985240936279297, \"y\": 7.748173236846924}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0999997854232788, \"y\": 7.7470703125}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.1015851497650146, \"y\": 7.746068477630615}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.1032946109771729, \"y\": 7.74518346786499}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.105128526687622, \"y\": 7.744417190551758}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.107064127922058, \"y\": 7.743751049041748}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.1090428829193115, \"y\": 7.743133544921875}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.1109544038772583, \"y\": 7.742464065551758}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.1126197576522827, \"y\": 7.741580009460449}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.1137734651565552, \"y\": 7.740234375}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.1140486001968384, \"y\": 7.738085746765137}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.1129714250564575, \"y\": 7.734691143035889}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.1099693775177002, \"y\": 7.729508399963379}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.1044032573699951, \"y\": 7.721929550170898}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0956374406814575, \"y\": 7.711339950561523}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0831615924835205, \"y\": 7.697234153747559}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0667784214019775, \"y\": 7.679385185241699}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.0468770265579224, \"y\": 7.65809965133667}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}, {\"position\": {\"x\": 1.027421236038208, \"y\": 7.636694431304932}, \"color\": {\"r\": 1.0, \"g\": 0.0, \"b\": 0.0, \"a\": 1.0}}]}]}"; + + let res: Result = serde_json::from_str(&jsonstring); + + match res { + Ok(lines) => println!("{:?}", lines), // if Ok(255), set x to 255 + Err(_e) => { + println!("No valid json?"); + println!("{}", _e); + }, // if Err("some message"), panic with error message "some message" + }; +} + diff --git a/src/main.rs b/src/main.rs index de014f9..ddf0f10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,8 +43,8 @@ fn main() { .add_plugins(ZmqPlugin { // url: "tcp://localhost:5558".to_string(), - // url: "tcp://100.109.175.82:99173".to_string(), - url: "tcp://127.0.0.1:99173".to_string(), + url: "tcp://100.109.175.82:99174".to_string(), + // url: "tcp://127.0.0.1:99173".to_string(), filter: "".to_string(), target: ZmqReceiveTarget::LINES }) diff --git a/src/trap/tracks.rs b/src/trap/tracks.rs index 5fb6780..b209424 100644 --- a/src/trap/tracks.rs +++ b/src/trap/tracks.rs @@ -40,18 +40,18 @@ pub struct Track { } // coordinates in world space. To be converted to laser::Point or a drawable point -#[derive(Clone, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct RenderablePoint{ pub position: Vec2, - pub color: Color + pub color: Srgba } -#[derive(Clone, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct RenderableLine{ points: Vec } -#[derive(Clone, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct RenderableLines{ pub lines: Vec } @@ -88,7 +88,7 @@ impl From<&Track> for RenderableLines{ for position in track.history.iter() { points.push(RenderablePoint{ position: position.clone(), - color: Color::linear_rgba(1.0, 0., 0., 1.0) + color: Srgba::new(1.0, 0., 0., 1.0) }); } lines.push(RenderableLine{points}) diff --git a/src/trap/zmqplugin.rs b/src/trap/zmqplugin.rs index 54ad2d3..ab3a88c 100644 --- a/src/trap/zmqplugin.rs +++ b/src/trap/zmqplugin.rs @@ -1,7 +1,7 @@ use zmq::Socket; use serde_json::Result; use bevy::{ecs::system::SystemState, prelude::*, render::Render}; - +use std::num::NonZero; use super::{laser::{LaserApi, LaserTimer}, tracks::{Frame, LaserPoints, RenderableLines, Track, TrackBundle}}; @@ -46,7 +46,8 @@ fn setup_zmq(world: &mut World, params: &mut SystemState>) { fn receive_zmq_lines( subscriber: NonSend, mut lasers: Query<(&mut LaserApi, &mut LaserTimer)>, - time: Res