use crate::exception::FrankaException;
use crate::FrankaResult;
use std::path::Path;
pub fn has_realtime_kernel() -> bool {
Path::new("/sys/kernel/realtime").exists()
}
pub fn set_current_thread_to_highest_scheduler_priority() -> FrankaResult<()> {
unsafe {
let max_priority = libc::sched_get_priority_max(libc::SCHED_FIFO);
if max_priority == -1 {
return Err(FrankaException::RealTimeException {
message: "libfranka-rs: unable to get maximum possible thread priority".to_string(),
});
}
let thread_param = libc::sched_param {
sched_priority: max_priority - 1,
};
if libc::pthread_setschedparam(libc::pthread_self(), libc::SCHED_FIFO, &thread_param) != 0 {
return Err(FrankaException::RealTimeException {
message: "libfranka-rs: unable to set realtime scheduling".to_string(),
});
}
if libc::mlockall(libc::MCL_CURRENT | libc::MCL_FUTURE) != 0 {
return Err(FrankaException::RealTimeException {
message: "libfranka-rs: unable to lock memory".to_string(),
});
}
}
Ok(())
}
#[allow(clippy::float_cmp)]
pub fn is_homogeneous_transformation(transform: &[f64; 16]) -> bool {
const ORTHONORMAL_THRESHOLD: f64 = 1e-5;
if transform[3] != 0.0 || transform[7] != 0.0 || transform[11] != 0.0 || transform[15] != 1.0 {
return false;
}
for j in 0..3 {
if f64::abs(
f64::sqrt(
transform[j * 4].powf(2.)
+ transform[j * 4 + 1].powf(2.)
+ transform[j * 4 + 2].powf(2.),
) - 1.,
) > ORTHONORMAL_THRESHOLD
{
return false;
}
}
for i in 0..3 {
if f64::abs(
f64::sqrt(
transform[i].powf(2.) + transform[4 + i].powf(2.) + transform[2 * 4 + i].powf(2.),
) - 1.,
) > ORTHONORMAL_THRESHOLD
{
return false;
}
}
true
}