libSBNW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Modules Pages
transform.h
Go to the documentation of this file.
1 /*== SAGITTARIUS =====================================================================
2  * Copyright (c) 2012, Jesse K Medley
3  * All rights reserved.
4 
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of The University of Washington nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 //== FILEDOC =========================================================================
29 
34 //== BEGINNING OF CODE ===============================================================
35 
36 #ifndef __SBNW_TRANSFORM_H_
37 #define __SBNW_TRANSFORM_H_
38 
39 //== INCLUDES ========================================================================
40 
42 #include "graphfab/layout/point.h"
43 #include "graphfab/layout/box.h"
44 
45 //-- C++ code --
46 #ifdef __cplusplus
47 
48 #include <iostream>
49 
50 namespace Graphfab {
51 
52  struct cutout {
53  cutout(Real a, Real b, Real c, Real d)
54  : u(a), v(b), x(c), y(d) {}
55  Real u, v;
56  Real x, y;
57 
58  Real det() const { return u*y - v*x; }
59  };
60 
61  class _GraphfabExport Affine2d {
62  protected:
63  static Real min(Real x, Real y) {
64  if(x < y)
65  return x;
66  else
67  return y;
68  }
69  public:
70  Affine2d() {
71  _e[0] = 1.; _e[1] = 0.; _e[2] = 0.;
72  _e[3] = 0.; _e[4] = 1.; _e[5] = 0.;
73  _e[6] = 0.; _e[7] = 0.; _e[8] = 1.;
74  }
75 
76  Affine2d(Real a, Real b, Real c,
77  Real u, Real v, Real w,
78  Real x, Real y, Real z) {
79  _e[0] = a; _e[1] = b; _e[2] = c;
80  _e[3] = u; _e[4] = v; _e[5] = w;
81  _e[6] = x; _e[7] = y; _e[8] = z;
82  }
83 
85  Affine2d inv() const;
86 
88  Real det() const;
89 
91  Real rc(int r, int c) const {
92  AT(0 <= r && r < 3, "Row out of range");
93  AT(0 <= c && c < 3, "Column out of range");
94  return _e[c+r*3];
95  }
96 
98  Real& rcref(int r, int c) {
99  AT(0 <= r && r < 3, "Row out of range");
100  AT(0 <= c && c < 3, "Column out of range");
101  return _e[c+r*3];
102  }
103 
104  // Hacky
105  Real scaleFactor() const {
106  return rc(0,0);
107  }
108 
109  void set(int i, int j, Real val) { rcref(i,j) = val; }
110 
111  // Creators:
112 
113  static Affine2d makeXlate(Real x, Real y) {
114  Affine2d a;
115  a.rcref(0,2) = x;
116  a.rcref(1,2) = y;
117  return a;
118  }
119 
120  static Affine2d makeXlate(const Point& p) { return makeXlate(p.x, p.y); }
121 
122  static Affine2d makeScale(Real x, Real y) {
123  Affine2d a;
124  a.rcref(0,0) = x;
125  a.rcref(1,1) = y;
126  return a;
127  }
128 
129  // usual basis transformation - z is xlate
130  // renamed to fromBasis
131  static Affine2d fromPoints(const Point& x, const Point& y, const Point& z) {
132  return Affine2d(
133  x.x, y.x, z.x,
134  x.y, y.y, z.y,
135  0., 0., 1.
136  );
137  }
138 
140  static Affine2d fromBasis(const Point& u, const Point& v, const Point& disp) {
141  return Affine2d(
142  u.x, v.x, disp.x,
143  u.y, v.y, disp.y,
144  0, 0, 1
145  );
146  }
147 
148  static Affine2d fromBox(const Box& b) {
149  return fromPoints(Point(b.getMax().x, 0), Point(0, b.getMax().y), b.getMin());
150  }
151 
152  static Affine2d sendTo(const Box& src, const Box& dst) {
153  return fromBox(dst)*fromBox(src).inv();
154  }
155 
156  static Affine2d FitToWindow(const Box& src, const Box& dst);
157 
158  // Operations:
159 
160  Point operator*(const Point& x) const;
161 
162  Box operator*(const Box& x) const;
163 
164  Affine2d operator*(const Real& k) const;
165 
166  Affine2d operator/(const Real& k) const { return (*this)*(1./k); }
167 
168  // Composition:
169 
170  static Affine2d compose(const Affine2d& u, const Affine2d& v);
171 
172  Affine2d operator*(const Affine2d& z) const { return compose(*this, z); }
173 
175  Point applyLinearOnly(const Point& x) const;
176 
177  Point getScale() const {
178  return Point(applyLinearOnly(Point(1, 0)).mag(),
179  applyLinearOnly(Point(0, 1)).mag());
180  }
181 
182  Point getDisplacement() const {
183  return Point(rc(0, 2), rc(1, 2));
184  }
185 
186  protected:
187 
188  Affine2d cofactors() const;
189 
190  Real cofactor(int i, int j) const;
191 
192  // minor for row, col
193  cutout getCutout(int r, int c) const;
194 
195  Real _e[9];
196  };
197 
198  Point xformPoint(const Point& p, const Affine2d& t);
199  Box xformBox(const Box& b, const Affine2d& t);
200 
202  //Affine2d makeXlate(const Point& p);
203 
204  _GraphfabExport std::ostream& operator<<(std::ostream& o, const Affine2d& t);
205 
206 }
207 
208 #endif
209 
210 #endif
SAGITTARIUS_REAL Real
Make Real visible to C. Consider letting Real lie in top namespace.
Definition: SagittariusCommon.h:136
Definition: SagittariusCommon.cpp:38
A box.
First file included.