1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use ceres_solver_sys as sys;
use std::os::raw::c_void;
use std::pin::Pin;
use std::slice;
pub type LossFunctionType = Box<dyn Fn(f64, &mut [f64; 3])>;
pub enum LossFunction {
Custom(CustomLossFunction),
Stock(StockLossFunction),
}
impl LossFunction {
pub fn custom(func: impl Into<LossFunctionType>) -> Self {
let func: LossFunctionType = func.into();
Self::Custom(CustomLossFunction {
func: Box::pin(func),
})
}
pub fn huber(a: f64) -> Self {
let stock = StockLossFunction {
inner: unsafe { sys::ceres_create_huber_loss_function_data(a) },
name: "Huber",
};
Self::Stock(stock)
}
pub fn soft_l1(a: f64) -> Self {
let stock = StockLossFunction {
inner: unsafe { sys::ceres_create_softl1_loss_function_data(a) },
name: "SoftLOne",
};
Self::Stock(stock)
}
pub fn cauchy(a: f64) -> Self {
let stock = StockLossFunction {
inner: unsafe { sys::ceres_create_cauchy_loss_function_data(a) },
name: "Cauchy",
};
Self::Stock(stock)
}
pub fn arctan(a: f64) -> Self {
let stock = StockLossFunction {
inner: unsafe { sys::ceres_create_arctan_loss_function_data(a) },
name: "Arctan",
};
Self::Stock(stock)
}
pub fn tolerant_loss(a: f64, b: f64) -> Self {
let stock = StockLossFunction {
inner: unsafe { sys::ceres_create_tolerant_loss_function_data(a, b) },
name: "TolerantLoss",
};
Self::Stock(stock)
}
#[inline]
pub fn loss(&self, squared_norm: f64, out: &mut [f64; 3]) {
match self {
Self::Custom(custom) => (custom.func)(squared_norm, out),
Self::Stock(stock) => stock.loss(squared_norm, out),
}
}
pub(crate) fn ffi_function(&self) -> unsafe extern "C" fn(*mut c_void, f64, *mut f64) {
match self {
Self::Stock(_) => sys::ceres_stock_loss_function,
Self::Custom(_) => ffi_custom_loss_function,
}
}
pub(crate) fn ffi_user_data(&mut self) -> *mut c_void {
match self {
Self::Custom(custom) => {
Pin::into_inner(custom.func.as_mut()) as *mut LossFunctionType as *mut c_void
}
Self::Stock(stock) => stock.inner,
}
}
}
pub struct CustomLossFunction {
pub func: Pin<Box<LossFunctionType>>,
}
pub struct StockLossFunction {
inner: *mut c_void,
pub name: &'static str,
}
impl StockLossFunction {
#[inline]
fn loss(&self, squared_norm: f64, out: &mut [f64; 3]) {
unsafe { sys::ceres_stock_loss_function(self.inner, squared_norm, out.as_mut_ptr()) }
}
}
impl Drop for StockLossFunction {
fn drop(&mut self) {
unsafe { sys::ceres_free_stock_loss_function_data(self.inner) }
}
}
#[no_mangle]
unsafe extern "C" fn ffi_custom_loss_function(
user_data: *mut c_void,
squared_norm: f64,
out: *mut f64,
) {
let func = (user_data as *mut LossFunctionType).as_ref().unwrap();
let out = slice::from_raw_parts_mut(out, 3).try_into().unwrap();
(func)(squared_norm, out)
}