Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add example to create mask and use set_mouse_callback #473

Merged
merged 4 commits into from
Apr 21, 2024

Conversation

gfgafn
Copy link
Contributor

@gfgafn gfgafn commented Jul 2, 2023

No description provided.

examples/create_mask.rs Outdated Show resolved Hide resolved
Comment on lines 195 to 207
/// Converts an `i32` to a `opencv::highgui::MouseEventTypes`
///
/// # Panics
///
/// Panics if the argument less than 0 or greater than 11.
fn mouse_event_from_i32(value: i32) -> opencv::highgui::MouseEventTypes {
(value.gt(&(opencv::highgui::MouseEventTypes::EVENT_MOUSEHWHEEL as i32) /* 11 */)
|| (value.lt(&(opencv::highgui::MouseEventTypes::EVENT_MOUSEMOVE as i32) /* 0 */)))
.then(|| panic!("Invalid cv::highgui::MouseEventTypes value: {}", value));

// Safe because of the previous check
unsafe { std::mem::transmute(value) }
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this function be generated automatically during the build process?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely! I’ll take a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

  • With this automatic implementation, we can easily use highgui::MouseEventTypes enum and pattern matching together to take advantage of exhaustiveness checking, enabling us to discover issues earlier. This approach is preferred over comparing values of type i32 with if-else statements and certain constants(e.g. if event == highgui::EVENT_MOUSEMOVE {} ).

    I think the same principle can be applied to other enums within opencv::highgui module, such as highgui::MouseEventFlagshighgui::QtButtonTypes etc.

    impl From<i32> for crate::highgui::MouseEventTypes {
    	fn from(v: i32) -> Self {
    		(v.gt(&(crate::highgui::MouseEventTypes::EVENT_MOUSEHWHEEL as i32))
    			|| (v.lt(&(crate::highgui::MouseEventTypes::EVENT_MOUSEMOVE as i32))))
    		.then(|| panic!("Invalid cv::highgui::MouseEventTypes value: {}", v));
    		// Safe because of the previous check
    		unsafe { std::mem::transmute(v) }
    	}
    }
  • And the type MouseCallback may can be redefined

    pub type MouseCallback = Option<Box<dyn FnMut(i32, i32, i32, i32) + Send + Sync + 'static>>;

    to

    pub type MouseCallback = Option<Box<dyn FnMut(highgui::MouseEventTypes, i32, i32, highgui::MouseEventFlags) + Send + Sync + 'static>>;

    But it will make breaking changes!

@twistedfall twistedfall marked this pull request as ready for review April 21, 2024 07:51
@twistedfall twistedfall merged commit dc5f695 into twistedfall:master Apr 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants