Turi Create  4.0
affine.hpp
Go to the documentation of this file.
1 /* Copyright © 2017 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 https://opensource.org/licenses/BSD-3-Clause
5  */
6 /*
7  Copyright 2005-2007 Adobe Systems Incorporated
8  Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
9  or a copy at http://opensource.adobe.com/licenses.html)
10 */
11 
12 /*************************************************************************************************/
13 
14 #ifndef GIL_AFFINE_HPP
15 #define GIL_AFFINE_HPP
16 
17 #include <boost/gil/utilities.hpp> // point2
18 
19 ////////////////////////////////////////////////////////////////////////////////////////
20 /// \file
21 /// \brief support for affine transformations
22 /// \author Lubomir Bourdev and Hailin Jin \n
23 /// Adobe Systems Incorporated
24 /// \date 2005-2007 \n September 21, 2006
25 ///
26 ////////////////////////////////////////////////////////////////////////////////////////
27 
28 namespace boost { namespace gil {
29 
30 ////////////////////////////////////////////////////////////////////////////////////////
31 ///
32 /// Simple matrix to do 2D affine transformations. It is actually 3x3 but the last column is [0 0 1]
33 ///
34 ////////////////////////////////////////////////////////////////////////////////////////
35 template <typename T>
36 class matrix3x2 {
37 public:
38  matrix3x2() : a(1), b(0), c(0), d(1), e(0), f(0) {}
39  matrix3x2(T A, T B, T C, T D, T E, T F) : a(A),b(B),c(C),d(D),e(E),f(F) {}
40  matrix3x2(const matrix3x2& mat) : a(mat.a), b(mat.b), c(mat.c), d(mat.d), e(mat.e), f(mat.f) {}
41  matrix3x2& operator=(const matrix3x2& m) { a=m.a; b=m.b; c=m.c; d=m.d; e=m.e; f=m.f; return *this; }
42 
43  matrix3x2& operator*=(const matrix3x2& m) { (*this) = (*this)*m; return *this; }
44 
45  static matrix3x2 get_rotate(T rads) { T c=std::cos(rads); T s=std::sin(rads); return matrix3x2(c,s,-s,c,0,0); }
46  static matrix3x2 get_translate(const point2<T>& t) { return matrix3x2(1 ,0,0,1 ,t.x,t.y); }
47  static matrix3x2 get_translate(T x, T y) { return matrix3x2(1 ,0,0,1 ,x, y ); }
48  static matrix3x2 get_scale (const point2<T>& s) { return matrix3x2(s.x,0,0,s.y,0 ,0 ); }
49  static matrix3x2 get_scale (T x, T y) { return matrix3x2(x, 0,0,y, 0 ,0 ); }
50  static matrix3x2 get_scale (T s) { return matrix3x2(s ,0,0,s ,0 ,0 ); }
51 
52  T a,b,c,d,e,f;
53 };
54 
55 template <typename T>
56 matrix3x2<T> operator*(const matrix3x2<T>& m1, const matrix3x2<T>& m2) {
57  return matrix3x2<T>(
58  m1.a * m2.a + m1.b * m2.c,
59  m1.a * m2.b + m1.b * m2.d,
60  m1.c * m2.a + m1.d * m2.c,
61  m1.c * m2.b + m1.d * m2.d,
62  m1.e * m2.a + m1.f * m2.c + m2.e,
63  m1.e * m2.b + m1.f * m2.d + m2.f );
64 }
65 
66 template <typename T, typename F>
67 point2<F> operator*(const point2<T>& p, const matrix3x2<F>& m) {
68  return point2<F>(m.a*p.x + m.c*p.y + m.e, m.b*p.x + m.d*p.y + m.f);
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////////////
72 /// Define affine mapping that transforms the source coordinates by the affine transformation
73 ////////////////////////////////////////////////////////////////////////////////////////
74 /*
75 template <typename MapFn>
76 concept MappingFunctionConcept {
77  typename mapping_traits<MapFn>::result_type; where PointNDConcept<result_type>;
78 
79  template <typename Domain> { where PointNDConcept<Domain> }
80  result_type transform(MapFn&, const Domain& src);
81 };
82 */
83 
84 template <typename T> struct mapping_traits;
85 
86 template <typename F>
87 struct mapping_traits<matrix3x2<F> > {
88  typedef point2<F> result_type;
89 };
90 
91 template <typename F, typename F2>
92 point2<F> transform(const matrix3x2<F>& mat, const point2<F2>& src) { return src * mat; }
93 
94 } } // namespace boost::gil
95 
96 #endif
Define affine mapping that transforms the source coordinates by the affine transformation.
Definition: affine.hpp:84