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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/**
* @file thtrans.h
* Transformation structures.
*/
/* Copyright (C) 2006-2007 Stacho Mudrak, Marco Corvi
*
* $Date: $
* $RCSfile: $
* $Revision: $
*
* --------------------------------------------------------------------
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* --------------------------------------------------------------------
*/
#ifndef thtrans_2_h
#define thtrans_2_h
#include <math.h> // sqrt
#include "thinfnan.h" // NaN
#include <string>
#include <vector>
#include <list>
#include <memory>
struct thline2; // forward declaration
/** 2D vector
*
*/
struct thvec2 {
double m_x, m_y; //!< coordinates
thvec2() : m_x(0.0), m_y(0.0) {} //!< default cstr
thvec2(double x, double y) : m_x(x), m_y(y) {} //!< cstr
thvec2( thline2 & l1, thline2 & l2 ); //!< intersection of two lines
thvec2 operator += (const thvec2 & v); //!< vector addition
thvec2 operator -= (const thvec2 & v); //!< vector difference
thvec2 operator *= (const double & c); //!< multiplication by a scalar
thvec2 operator /= (const double & c); //!< division by a scalar
void minimize(thvec2 v); //!< min( this, v )
void maximize(thvec2 v); //!< max( this, v )
double length(); //!< |V| = sqrt( V * V )
double length2() const { return m_x*m_x + m_y*m_y; } //!< V * V
double orientation(); //!< orientation in degrees
void reset(); //!< V = 0
void normalize(); //!< V / |V|
thvec2 orthogonal() { return thvec2( m_y, -m_x); } //!< V^ (anticlockwise orthogonal vector)
bool is_nan() const { return thisnan(m_x) || thisnan(m_y); }
};
/** 2 x 2 matrix with real coefficient
* <PRE>
* m_xx m_xy
* m_yx m_yy
* </PRE>
*/
struct thmat2 {
double m_xx, m_xy, m_yx, m_yy; //!< matrix coefficients
thmat2() : m_xx(1.0), m_xy(0.0), m_yx(0.0), m_yy(1.0) {} //!< cstr
thmat2(double xx, double xy, double yx, double yy) : m_xx(xx), m_xy(xy), m_yx(yx), m_yy(yy) {}
thmat2 operator *= (const double & c); //!< multiplication by a scalar
thmat2 operator /= (const double & c); //!< division by a scalar
thmat2 inverse(); //!< inverse matrix
double determinant() { return m_xx*m_yy - m_xy*m_yx; } //!< determinant
double trace() { return m_xx + m_yy; } //!< trace
void reset(); //!< M = Identity matrix
};
/** line in the 2D plane, with equation
* <PRE>
* m_a * X + m_b * Y + m_c = 0
* </PRE>
*/
struct thline2 {
double m_a, m_b, m_c; //!< coefficients of the line
thline2() : m_a(0.0), m_b(0.0), m_c(0.0) {} //!< cstr
thline2(double a, double b, double c) : m_a(a), m_b(b), m_c(c) {}
thline2(thvec2 from, thvec2 to); //!< cstr: line joining two vectors
double eval(thvec2 p); //!< evaluate the line eq. on the vector
};
/** linear algebra operations
*/
thvec2 operator - (const thvec2 & v); // -V
thvec2 operator + (const thvec2 & v1, const thvec2 & v2); // V1 + V2
thvec2 operator - (const thvec2 & v1, const thvec2 & v2); // V1 - V2
double operator * (const thvec2 & v1, const thvec2 & v2); // V1 * V2
double operator ^ (const thvec2 & v1, const thvec2 & v2); // V1 ^ V2 = V1 * (V2^)
thvec2 operator * (const double & c, const thvec2 & v); // c * V1
thvec2 operator * (const thvec2 & v, const double & c); // V1 * c
thvec2 operator / (const thvec2 & v, const double & c); // V1 / c
thvec2 operator * (const thmat2 & m, const thvec2 & v); // M * V1
thmat2 operator * (const double & c, const thmat2 & m); // c * M
thmat2 operator * (const thmat2 & m, const double & c); // M * c
thmat2 operator / (const thmat2 & m, const double & c); // M / c
/**
* 2D bouding box.
*/
struct thbbox2 {
bool m_valid;
thvec2 m_min, m_max;
thbbox2() : m_valid(false) {}
bool is_valid();
void update(thvec2 pt);
};
/**
* 3x3 matrix
*/
struct thmat3
{
double m_xx, m_xy, m_xz; //!< first row
double m_yx, m_yy, m_yz; //!< second row
double m_zx, m_zy, m_zz; //!< second row
/** default cstr: set the matrix to the identity
*/
thmat3() : m_xx(1.0), m_xy(0.0), m_xz(0.0)
, m_yx(0.0), m_yy(1.0), m_yz(0.0)
, m_zx(0.0), m_zy(0.0), m_zz(1.0) { }
/** cstr with the nine matrix elements, row-wise
*/
thmat3(double xx, double xy, double xz,
double yx, double yy, double yz,
double zx, double zy, double zz)
: m_xx(xx), m_xy(xy), m_xz(xz)
, m_yx(yx), m_yy(yy), m_yz(yz)
, m_zx(zx), m_zy(zy), m_zz(zz) { }
/** set the matrix to the identity
*/
void reset();
/** compute the inverse matrix
* @return the inverse matrix
*/
thmat3 inverse();
};
/** linear transformation point
*/
struct thlintrans_pt {
thvec2 m_src; //!< point in the source domain
thvec2 m_tgt; //!< point in the target domain
thlintrans_pt() {}
/** cstr
*/
thlintrans_pt(thvec2 src, thvec2 tgt)
: m_src(src)
, m_tgt(tgt)
{ }
};
/** list of linear-transform points
*/
typedef std::list<thlintrans_pt> thlintrans_pt_list;
/**
* Linear transformation. Rotate, zoom, shift.
*/
struct thlintrans {
thmat2 m_fmat; //!< forward matrix
thmat2 m_bmat; //!< backward matrix
thvec2 m_shift;
double m_rot = {}, m_scale = {};
thlintrans_pt_list m_initpts;
thlintrans();
void reset();
void init(thmat2 A, thvec2 b);
void init(thvec2 src, thvec2 dst);
void init(thvec2 srcX, thvec2 srcY, thvec2 dstX, thvec2 dstY);
void insert_point(thvec2 src, thvec2 dst);
void init_points();
void init_backward();
thvec2 forward(thvec2 src);
thvec2 backward(thvec2 dst);
};
/**
* Linear zooming transformation.
*/
struct thlinzoomtrans {
bool m_valid, m_single;
thvec2 m_from, m_to;
double m_fl, m_fr, m_tl, m_tr;
int m_flc, m_frc, m_tlc, m_trc;
thline2 m_line_from, m_line_to, m_line;
double m_orient_from = 0.0, m_orient_to = 0.0, m_line_l = 0.0;
thlinzoomtrans();
void init_points(thvec2 from, thvec2 to);
void init_from(thvec2 src, double dst);
void init_to(thvec2 src, double dst);
void init();
thvec2 forward(thvec2 src);
};
/**
* Morphing transformation.
*/
struct thmorphtrans {
thlintrans_pt_list m_initpts;
void reset();
void insert_point(thvec2 src, thvec2 dst);
thvec2 forward(thvec2 src);
thvec2 backward(thvec2 dst, thvec2 ini = thvec2(0,0));
};
/**
* Advanced morphing transformation.
*/
struct thmorph2trans {
std::unique_ptr<struct thmorph2trans_members> m;
double m_eps = {};
thmorph2trans();
~thmorph2trans();
thmorph2trans(const thmorph2trans&) = delete;
thmorph2trans(thmorph2trans&&) = delete;
thmorph2trans& operator=(const thmorph2trans&) = delete;
thmorph2trans& operator=(thmorph2trans&&) = delete;
void reset();
void insert_point(thvec2 src, thvec2 dst, long id, double value = thnan);
void insert_extra_point(thvec2 src, thvec2 dst, long id, double value = thnan);
void insert_line(long from, long to);
void insert_lines_from_db();
void insert_zoom_point(thvec2 src, double dst, long id);
void init(double eps = 0.01);
thvec2 forward(thvec2 src);
thvec2 backward(thvec2 dst, thvec2 ini = thvec2(0,0));
double interpolate(thvec2 src);
};
#endif
|