File: affine.cc

package info (click to toggle)
epix 1.2.6-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,176 kB
  • ctags: 2,623
  • sloc: cpp: 16,442; sh: 4,848; makefile: 205
file content (300 lines) | stat: -rw-r--r-- 6,679 bytes parent folder | download | duplicates (4)
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
293
294
295
296
297
298
299
300
/* 
 * affine.cc -- ePiX::affine class
 *
 * This file is part of ePiX, a C++ library for creating high-quality 
 * figures in LaTeX 
 *
 * Version 1.1.21
 * Last Change: September 23, 2007
 *
 * 
 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
 * Andrew D. Hwang <rot 13 nujnat at zngupf dot ubylpebff dot rqh>
 * Department of Mathematics and Computer Science
 * College of the Holy Cross
 * Worcester, MA, 01610-2395, USA
 *
 *
 * ePiX 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
 * (at your option) any later version.
 *
 * ePiX 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 ePiX; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include <cmath>

#include "errors.h"
#include "constants.h"

#include "pairs.h"
#include "triples.h"
#include "functions.h"

#include "affine.h"

namespace ePiX {

  // rotate/reflect about (0,0)
  static pair __epix_rotate(double Cs, double Sn, const pair& arg)
  {
    double x(arg.x1()), y(arg.x2());
    return pair(Cs*x - Sn*y, Sn*x + Cs*y);
  }

  static pair __epix_reflect(double Cs, double Sn, const pair& arg)
  {
    double x(arg.x1()), y(arg.x2());
    return pair(Cs*x + Sn*y, Sn*x - Cs*y);
  }

  // identity
  affine::affine()
    : m_00(0,0), m_10(1,0), m_01(0,1) { }

  // images of (1,0), (0,1), (0,0)
  affine::affine(const pair& pr1, const pair& pr2, const pair& loc)
    : m_00(loc), m_10(pr1), m_01(pr2) { }

  affine::affine(const P& pr1, const P& pr2, const P& loc)
    : m_00(loc.x1(), loc.x2()),
      m_10(pr1.x1(), pr1.x2()),
      m_01(pr2.x1(), pr2.x2()) { }

  affine&   affine::shift(const pair& arg)
  {
    m_00 += arg;
    m_10 += arg;
    m_01 += arg;

    return *this;
  }

  affine&   affine::shift(const P& arg)
  {
    return shift(pair(arg.x1(), arg.x2()));
  }

  affine&  affine::rotate(double theta, const pair& ctr)
  {
    const double Cs(Cos(theta));
    const double Sn(Sin(theta));

    // shift
    m_00 -= ctr;
    m_10 -= ctr;
    m_01 -= ctr;

    // rotate about origin and shift back
    m_00 = __epix_rotate(Cs, Sn, m_00) + ctr;
    m_10 = __epix_rotate(Cs, Sn, m_10) + ctr;
    m_01 = __epix_rotate(Cs, Sn, m_01) + ctr;

    return *this;
  }

  affine&  affine::rotate(double theta, const P& ctr)
  {
    return rotate(theta, pair(ctr.x1(), ctr.x2()));
  }

  affine& affine::reflect(double theta, const pair& ctr)
  {
    const double Cs(Cos(2*theta));
    const double Sn(Sin(2*theta));

    // shift
    m_00 -= ctr;
    m_10 -= ctr;
    m_01 -= ctr;

    // reflect about origin and shift back
    m_00 = __epix_reflect(Cs, Sn, m_00) + ctr;
    m_10 = __epix_reflect(Cs, Sn, m_10) + ctr;
    m_01 = __epix_reflect(Cs, Sn, m_01) + ctr;

    return *this;
  }

  affine&  affine::reflect(double theta, const P& ctr)
  {
    return reflect(theta, pair(ctr.x1(), ctr.x2()));
  }

  affine& affine::h_scale(double sc, const pair& ctr)
  {
    m_00 -= ctr;
    m_10 -= ctr;
    m_01 -= ctr;

    const pair scale(sc, 1);

    m_00 = (m_00 & scale) + ctr;
    m_10 = (m_10 & scale) + ctr;
    m_01 = (m_01 & scale) + ctr;

    return *this;
  }

  affine&  affine::h_scale(double sc, const P& ctr)
  {
    return h_scale(sc, pair(ctr.x1(), ctr.x2()));
  }

  affine& affine::v_scale(double sc, const pair& ctr)
  {
    // shift
    m_00 -= ctr;
    m_10 -= ctr;
    m_01 -= ctr;

    const pair scale(1, sc);

    m_00 = (m_00 & scale) + ctr;
    m_10 = (m_10 & scale) + ctr;
    m_01 = (m_01 & scale) + ctr;

    return *this;
  }

  affine&  affine::v_scale(double sc, const P& ctr)
  {
    return v_scale(sc, pair(ctr.x1(), ctr.x2()));
  }

  affine&   affine::scale(double sc, const pair& ctr)
  {
    // shift
    m_00 -= ctr;
    m_10 -= ctr;
    m_01 -= ctr;

    const pair scale(sc, sc);

    m_00 = (m_00 & scale) + ctr;
    m_10 = (m_10 & scale) + ctr;
    m_01 = (m_01 & scale) + ctr;

    return *this;
  }

  affine&  affine::scale(double sc, const P& ctr)
  {
    return scale(sc, pair(ctr.x1(), ctr.x2()));
  }


  affine&  affine::h_shear(double sc, const pair& ctr)
  {
    // shift
    m_00 -= ctr;
    m_10 -= ctr;
    m_01 -= ctr;

    m_00 += pair(sc*m_00.x2(), 0) + ctr;
    m_10 += pair(sc*m_10.x2(), 0) + ctr;
    m_01 += pair(sc*m_01.x2(), 0) + ctr;

    return *this;
  }

  affine&  affine::h_shear(double sc, const P& ctr)
  {
    return h_shear(sc, pair(ctr.x1(), ctr.x2()));
  }

  affine&  affine::v_shear(double sc, const pair& ctr)
  {
    // shift
    m_00 -= ctr;
    m_10 -= ctr;
    m_01 -= ctr;

    m_00 += pair(0, sc*m_00.x1()) + ctr;
    m_10 += pair(0, sc*m_10.x1()) + ctr;
    m_01 += pair(0, sc*m_01.x1()) + ctr;

    return *this;
  }

  affine&  affine::v_shear(double sc, const P& ctr)
  {
    return v_shear(sc, pair(ctr.x1(), ctr.x2()));
  }

  affine& affine::postcomp(const affine& af)
  {
    m_00 = af(m_00);
    m_10 = af(m_10);
    m_01 = af(m_01);

    return *this;
  }

  affine& affine::invert()
  {
    m_10 -= m_00;
    m_01 -= m_00;

    const double denom(m_10.x1()*m_01.x2() - m_10.x2()*m_01.x1());

    if (fabs(denom) < EPIX_EPSILON)
      {
	// restore
	m_10 += m_00;
	m_01 += m_00;
	epix_warning("affine not invertible, no action");
	return *this;
      }

    // else compute inverse entries
    const double a11( m_01.x2()/denom);
    const double a12(-m_01.x1()/denom);
    const double a21(-m_10.x2()/denom);
    const double a22( m_10.x1()/denom);

    pair tmp_00(-a11*m_00.x1() + a12*m_00.x2(),
		 a21*m_00.x1() - a22*m_00.x2());

    m_00 = tmp_00;
    m_10 = m_00 + pair(a11, a21);
    m_01 = m_00 + pair(a12, a22);

    return *this;
  }


  // evaluation
  pair affine::operator() (const pair& arg) const
  {
    return m_00 + arg.x1()*(m_10 - m_00) + arg.x2()*(m_01 - m_00);
  }

  pair affine::operator() (const P& arg) const
  {
    return m_00 + arg.x1()*(m_10 - m_00) + arg.x2()*(m_01 - m_00);
  }

  // pre-composition
  affine affine::operator() (const affine& af) const
  {
    return affine(this->operator()(af.m_10),
		  this->operator()(af.m_01),
		  this->operator()(af.m_00));
  }

  bool affine::reverses_orientation() const
  {
    const pair col1(m_10 - m_00);
    const pair col2(m_01 - m_00);

    return (col1.x1()*col2.x2() - col1.x2()*col2.x1() < -EPIX_EPSILON);
  }
} // end of namespace