Skip to main content

rustface/math/
mod.rs

1// This file is part of the open-source port of SeetaFace engine, which originally includes three modules:
2//      SeetaFace Detection, SeetaFace Alignment, and SeetaFace Identification.
3//
4// This file is part of the SeetaFace Detection module, containing codes implementing the face detection method described in the following paper:
5//
6//      Funnel-structured cascade for multi-view face detection with alignment awareness,
7//      Shuzhe Wu, Meina Kan, Zhenliang He, Shiguang Shan, Xilin Chen.
8//      In Neurocomputing (under review)
9//
10// Copyright (C) 2016, Visual Information Processing and Learning (VIPL) group,
11// Institute of Computing Technology, Chinese Academy of Sciences, Beijing, China.
12//
13// As an open-source face recognition engine: you can redistribute SeetaFace source codes
14// and/or modify it under the terms of the BSD 2-Clause License.
15//
16// You should have received a copy of the BSD 2-Clause License along with the software.
17// If not, see < https://opensource.org/licenses/BSD-2-Clause>.
18
19#[inline]
20pub fn copy_u8_to_i32(src: &[u8], dest: &mut [i32]) {
21    let dest = &mut dest[0..src.len()]; // eliminates a branch from the loop
22    for (value, dest) in src.iter().copied().zip(dest.iter_mut()) {
23        *dest = i32::from(value);
24    }
25}
26
27pub fn square(src: &[i32], dest: &mut [u32]) {
28    for (value, dest) in src.iter().copied().zip(dest.iter_mut()) {
29        *dest = i32::pow(value, 2) as u32;
30    }
31}
32
33pub unsafe fn abs(src: *const i32, dest: *mut i32, length: usize) {
34    for i in 0..length as isize {
35        let value = *src.offset(i);
36        *dest.offset(i) = if value >= 0 { value } else { -value };
37    }
38}
39
40pub unsafe fn vector_add(left: *const i32, right: *const i32, dest: *mut i32, length: usize) {
41    for i in 0..length as isize {
42        *dest.offset(i) = *left.offset(i) + *right.offset(i);
43    }
44}
45
46pub unsafe fn vector_sub(left: *const i32, right: *const i32, dest: *mut i32, length: usize) {
47    for i in 0..length as isize {
48        *dest.offset(i) = *left.offset(i) - *right.offset(i);
49    }
50}
51
52pub fn vector_inner_product(left: &[f32], right: &[f32]) -> f32 {
53    left.iter().copied().zip(right.iter().copied()).map(|(l,r)| l * r).sum()
54}
55
56#[cfg(test)]
57mod tests {
58
59    use super::*;
60
61    #[test]
62    fn test_square() {
63        let mut vec = vec![1, 2, 3];
64        square(&[1, 2, 3], &mut vec);
65        assert_eq!(vec![1, 4, 9], vec);
66    }
67
68    #[test]
69    fn test_abs() {
70        let mut vec = vec![-1, 2, -3];
71        unsafe { abs(vec.as_ptr(), vec.as_mut_ptr(), vec.len()) };
72        assert_eq!(vec![1, 2, 3], vec);
73    }
74
75    #[test]
76    fn test_vector_add() {
77        let mut vec = vec![1, 2, 3];
78        unsafe { vector_add(vec.as_ptr(), vec.as_ptr(), vec.as_mut_ptr(), vec.len()) };
79        assert_eq!(vec![2, 4, 6], vec);
80    }
81
82    #[test]
83    fn test_vector_sub() {
84        let mut vec = vec![1, 2, 3];
85        unsafe { vector_sub(vec.as_ptr(), vec.as_ptr(), vec.as_mut_ptr(), vec.len()) };
86        assert_eq!(vec![0, 0, 0], vec);
87    }
88
89    #[test]
90    fn test_vector_inner_product() {
91        let vec = vec![1.0, 2.0, 3.0];
92        let result = vector_inner_product(&vec, &vec);
93        assert_eq!(14.0, result);
94    }
95}