Skip to content

Commit

Permalink
Reapply: Image zoom (#1100)
Browse files Browse the repository at this point in the history
* Reapply "Image zZoom für Alex (#1023)" (#1090)

This reverts commit d234679.

* image zoom diesmal aber richtig

Co-authored-by: knoellle <[email protected]>

* make clippy happy

---------

Co-authored-by: okiwi6 <oleflb>
Co-authored-by: knoellle <[email protected]>
  • Loading branch information
oleflb and knoellle authored Jul 4, 2024
1 parent a997587 commit e94b982
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 201 deletions.
41 changes: 17 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions crates/coordinate_systems/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,10 @@ generate_coordinate_system!(
/// Same as [RightFoot] but shifted down to the sole.
/// See [official documentation](http://doc.aldebaran.com/2-8/family/nao_technical/masses_naov6.html#right-foot)
RightSole,
/// 2D Coordinate System for Twix Widgets
///
/// Origin: top left corner of the image
/// X axis points right
/// Y axis points down
Screen,
);
2 changes: 1 addition & 1 deletion tools/twix/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "twix"
version = "0.5.1"
version = "0.5.2"
edition.workspace = true
license.workspace = true
homepage.workspace = true
Expand Down
1 change: 1 addition & 0 deletions tools/twix/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ mod selectable_panel_macro;
mod twix_painter;
mod value_buffer;
mod visuals;
mod zoom_and_pan;

#[derive(Debug, Parser)]
struct Arguments {
Expand Down
66 changes: 40 additions & 26 deletions tools/twix/src/panels/image/mod.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
use std::{str::FromStr, sync::Arc};

use color_eyre::{eyre::eyre, Result};
use eframe::{
egui::{ComboBox, Image, Response, TextureOptions, Ui, Widget},
epaint::Vec2,
};
use coordinate_systems::Pixel;
use eframe::egui::{ComboBox, Response, SizeHint, TextureOptions, Ui, Widget};
use geometry::rectangle::Rectangle;
use linear_algebra::{point, vector};
use log::error;
use nalgebra::Similarity2;
use serde::{Deserialize, Serialize};
use serde_json::{from_value, json, Value};

use communication::client::{Cycler, CyclerOutput, Output};
use linear_algebra::vector;

use crate::{
image_buffer::ImageBuffer,
nao::Nao,
panel::Panel,
twix_painter::{CoordinateSystem, TwixPainter},
twix_painter::{Orientation, TwixPainter},
zoom_and_pan::ZoomAndPanTransform,
};

use self::{cycler_selector::VisionCyclerSelector, overlay::Overlays};
Expand Down Expand Up @@ -51,6 +50,7 @@ pub struct ImagePanel {
cycler_selector: VisionCyclerSelector,
overlays: Overlays,
image_kind: ImageKind,
zoom_and_pan: ZoomAndPanTransform,
}

impl Panel for ImagePanel {
Expand Down Expand Up @@ -94,6 +94,7 @@ impl Panel for ImagePanel {
cycler_selector,
overlays,
image_kind,
zoom_and_pan: ZoomAndPanTransform::default(),
}
}

Expand Down Expand Up @@ -151,36 +152,49 @@ impl Widget for &mut ImagePanel {
self.overlays
.combo_box(ui, self.cycler_selector.selected_cycler());
});
let (response, mut painter) = TwixPainter::allocate(
ui,
vector![640.0, 480.0],
point![0.0, 0.0],
Orientation::LeftHanded,
);
self.zoom_and_pan.apply(ui, &mut painter, &response);

match self.show_image(ui) {
Ok(response) => response,
Err(error) => ui.label(format!("{error:#?}")),
}
let _ = self
.show_image(&painter)
.map_err(|error| ui.label(format!("{error:#?}")));
let _ = self.overlays.paint(&painter);
response
}
}

impl ImagePanel {
fn show_image(&self, ui: &mut Ui) -> Result<Response> {
fn show_image(&self, painter: &TwixPainter<Pixel>) -> Result<()> {
let image_data = self
.image_buffer
.get_latest()
.map_err(|error| eyre!("{error}"))?;
let image_raw = bincode::deserialize::<Vec<u8>>(&image_data)?;
let context = painter.context();

let image_identifier = format!("bytes://image-{:?}", self.cycler_selector);
ui.ctx().forget_image(&image_identifier);
let image = Image::from_bytes(image_identifier, image_raw)
.texture_options(TextureOptions::NEAREST)
.fit_to_fraction(Vec2::splat(1.0));

let image_response = ui.add(image);

let painter = TwixPainter::paint_at(ui, image_response.rect).with_camera(
vector![640.0, 480.0],
Similarity2::identity(),
CoordinateSystem::LeftHand,
);
let _ = self.overlays.paint(&painter);
Ok(image_response)
context.forget_image(&image_identifier);
context.include_bytes(image_identifier.clone(), image_raw);
let result = context.try_load_texture(
&image_identifier,
TextureOptions::NEAREST,
SizeHint::Size(640, 480),
)?;

if let Some(id) = result.texture_id() {
painter.image(
id,
Rectangle {
min: point!(0.0, 0.0),
max: point!(640.0, 480.0),
},
);
}
Ok(())
}
}
4 changes: 2 additions & 2 deletions tools/twix/src/panels/image_color_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
image_buffer::ImageBuffer,
nao::Nao,
panel::Panel,
twix_painter::{CoordinateSystem, TwixPainter},
twix_painter::{Orientation, TwixPainter},
};

use super::image::cycler_selector::VisionCyclerSelector;
Expand Down Expand Up @@ -195,7 +195,7 @@ impl Widget for &mut ImageColorSelectPanel {
let painter = TwixPainter::<Pixel>::paint_at(ui, response.rect).with_camera(
vector![image.width() as f32, image.height() as f32],
Similarity2::identity(),
CoordinateSystem::LeftHand,
Orientation::LeftHanded,
);

if let Some(hover_position) = response.hover_pos() {
Expand Down
18 changes: 11 additions & 7 deletions tools/twix/src/panels/image_segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ use eframe::{
egui::{ComboBox, Response, Ui, Widget},
epaint::{Color32, Stroke},
};
use nalgebra::Similarity2;
use linear_algebra::{point, vector};
use serde::{Deserialize, Serialize};

use communication::client::CyclerOutput;
use coordinate_systems::Pixel;
use linear_algebra::{point, vector};
use serde_json::{json, Value};
use types::{
camera_position::CameraPosition,
Expand All @@ -20,8 +19,9 @@ use types::{
use crate::{
nao::Nao,
panel::Panel,
twix_painter::{CoordinateSystem, TwixPainter},
twix_painter::{Orientation, TwixPainter},
value_buffer::ValueBuffer,
zoom_and_pan::ZoomAndPanTransform,
};

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
Expand All @@ -45,6 +45,7 @@ pub struct ImageSegmentsPanel {
camera_position: CameraPosition,
color_mode: ColorMode,
use_filtered_segments: bool,
zoom_and_pan: ZoomAndPanTransform,
}

impl Panel for ImageSegmentsPanel {
Expand Down Expand Up @@ -73,6 +74,7 @@ impl Panel for ImageSegmentsPanel {
camera_position,
color_mode,
use_filtered_segments,
zoom_and_pan: ZoomAndPanTransform::default(),
}
}

Expand Down Expand Up @@ -161,12 +163,14 @@ impl Widget for &mut ImageSegmentsPanel {
Err(error) => return ui.label(format!("{error:?}")),
};

let (mut response, painter) = TwixPainter::<Pixel>::allocate_new(ui);
let painter = painter.with_camera(
let (mut response, mut painter) = TwixPainter::<Pixel>::allocate(
ui,
vector![640.0, 480.0],
Similarity2::identity(),
CoordinateSystem::LeftHand,
point![0.0, 0.0],
Orientation::LeftHanded,
);
self.zoom_and_pan.apply(ui, &mut painter, &response);

if let Some(hover_pos) = response.hover_pos() {
let image_coords = painter.transform_pixel_to_world(hover_pos);
let x = image_coords.x().round() as u16;
Expand Down
Loading

0 comments on commit e94b982

Please sign in to comment.