Turi Create  4.0
mapping_function.hpp
1 /* Copyright © 2019 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at
5  * https://opensource.org/licenses/BSD-3-Clause
6  */
7 
8 #ifndef TURI_ONE_SHOT_MAPPING_FUNCTION_H_
9 #define TURI_ONE_SHOT_MAPPING_FUNCTION_H_
10 
11 #include <Eigen/Core>
12 #include <Eigen/Dense>
13 
14 #include <algorithm>
15 #include <cmath>
16 #include <limits>
17 #include <random>
18 #include <vector>
19 
20 #include <boost/gil/gil_all.hpp>
21 
22 namespace boost {
23 namespace gil {
24 
25 /* Matrix - Vector multiplication */
26 template <typename T, typename F>
27 boost::gil::point2<F> operator*(const boost::gil::point2<T>& p,
28  const Eigen::Matrix<F, 3, 3>& m) {
29  float denominator = m(2, 0) * p.x + m(2, 1) * p.y + m(2, 2);
30  if (denominator == 0) {
31  // TODO: Figure out the right failure behavior for when denominator is 0.
32  return boost::gil::point2<F>(0, 0);
33  }
34  return boost::gil::point2<F>(
35  (m(0, 0) * p.x + m(0, 1) * p.y + m(0, 2)) / denominator,
36  (m(1, 0) * p.x + m(1, 1) * p.y + m(1, 2)) / denominator);
37 }
38 
39 /* Conforming to the MapFn concept required by Boost GIL, for Eigen::Matrix3f
40  */
41 template <typename T>
42 struct mapping_traits;
43 
44 template <typename F, typename F2>
45 boost::gil::point2<F> transform(const Eigen::Matrix<F, 3, 3>& mat,
46  const boost::gil::point2<F2>& src) {
47  return src * mat;
48 }
49 
50 template <typename F>
51 struct mapping_traits<Eigen::Matrix<F, 3, 3> > {
52  using result_type = boost::gil::point2<F>;
53 };
54 
55 } // namespace gil
56 } // namespace boost
57 
58 #endif // TURI_ONE_SHOT_MAPPING_FUNCTION_H_