Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* libmspub
3 : * Version: MPL 1.1 / GPLv2+ / LGPLv2+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * Copyright (C) 2012 Brennan Vincent <brennanv@email.arizona.edu>
17 : *
18 : * All Rights Reserved.
19 : *
20 : * For minor contributions see the git repository.
21 : *
22 : * Alternatively, the contents of this file may be used under the terms of
23 : * either the GNU General Public License Version 2 or later (the "GPLv2+"), or
24 : * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
25 : * in which case the provisions of the GPLv2+ or the LGPLv2+ are applicable
26 : * instead of those above.
27 : */
28 :
29 :
30 : //TODO : Adjust handles, glue points
31 :
32 : #include "VectorTransformation2D.h"
33 : #include <math.h>
34 :
35 0 : libmspub::VectorTransformation2D::VectorTransformation2D() : m_m11(1), m_m12(0), m_m21(0), m_m22(1), m_x(0), m_y(0)
36 : {
37 0 : }
38 :
39 : //We choose by convention to make function composition LEFT-multiplication, rather than right.
40 0 : libmspub::VectorTransformation2D libmspub::operator*(const VectorTransformation2D &l, const VectorTransformation2D &r)
41 : {
42 0 : VectorTransformation2D ret;
43 0 : ret.m_m11 = l.m_m11 * r.m_m11 + l.m_m12 * r.m_m21;
44 0 : ret.m_m12 = l.m_m11 * r.m_m12 + l.m_m12 * r.m_m22;
45 0 : ret.m_m21 = l.m_m21 * r.m_m11 + l.m_m22 * r.m_m21;
46 0 : ret.m_m22 = l.m_m21 * r.m_m12 + l.m_m22 * r.m_m22;
47 0 : ret.m_x = l.m_m11 * r.m_x + l.m_m12 * r.m_y + l.m_x;
48 0 : ret.m_y = l.m_m21 * r.m_x + l.m_m22 * r.m_y + l.m_y;
49 0 : return ret;
50 : }
51 :
52 0 : libmspub::VectorTransformation2D libmspub::VectorTransformation2D::fromFlips(bool flipH, bool flipV)
53 : {
54 0 : VectorTransformation2D ret;
55 0 : ret.m_m21 = ret.m_m12 = 0;
56 0 : ret.m_m11 = flipH ? -1 : 1;
57 0 : ret.m_m22 = flipV ? -1 : 1;
58 0 : return ret;
59 : }
60 :
61 0 : libmspub::VectorTransformation2D libmspub::VectorTransformation2D::fromTranslate(double x, double y)
62 : {
63 0 : VectorTransformation2D ret;
64 0 : ret.m_m11 = ret.m_m22 = 1;
65 0 : ret.m_m21 = ret.m_m12 = 0;
66 0 : ret.m_x = x;
67 0 : ret.m_y = y;
68 0 : return ret;
69 : }
70 :
71 0 : libmspub::VectorTransformation2D libmspub::VectorTransformation2D::fromCounterRadians(double theta)
72 : {
73 0 : VectorTransformation2D ret;
74 0 : ret.m_m11 = cos(theta);
75 0 : ret.m_m12 = -sin(theta);
76 0 : ret.m_m21 = sin(theta);
77 0 : ret.m_m22 = cos(theta);
78 0 : return ret;
79 : }
80 :
81 0 : libmspub::Vector2D libmspub::VectorTransformation2D::transform(Vector2D v) const
82 : {
83 0 : double x = m_m11 * v.m_x + m_m12 * v.m_y + m_x;
84 0 : double y = m_m21 * v.m_x + m_m22 * v.m_y + m_y;
85 0 : return Vector2D(x, y);
86 : }
87 :
88 0 : libmspub::Vector2D libmspub::VectorTransformation2D::transformWithOrigin(Vector2D v, Vector2D origin) const
89 : {
90 0 : return transform(v - origin) + origin;
91 : }
92 :
93 0 : libmspub::Vector2D libmspub::operator+(const Vector2D &l, const Vector2D &r)
94 : {
95 0 : double x = l.m_x + r.m_x;
96 0 : double y = l.m_y + r.m_y;
97 0 : return Vector2D(x, y);
98 : }
99 :
100 0 : libmspub::Vector2D libmspub::operator-(const Vector2D &l, const Vector2D &r)
101 : {
102 0 : double x = l.m_x - r.m_x;
103 0 : double y = l.m_y - r.m_y;
104 0 : return Vector2D(x, y);
105 : }
106 :
107 0 : double libmspub::VectorTransformation2D::getRotation() const
108 : {
109 0 : if (fabs(getHorizontalScaling()) > 0.0001)
110 : {
111 0 : return atan2(m_m21, m_m11);
112 : }
113 0 : if (fabs(getVerticalScaling()) > 0.0001)
114 : {
115 0 : return atan2(-m_m12, m_m22);
116 : }
117 0 : return 0;
118 : }
119 :
120 0 : double libmspub::VectorTransformation2D::getHorizontalScaling() const
121 : {
122 0 : return m_m11 * m_m11 + m_m21 * m_m21;
123 : }
124 :
125 0 : double libmspub::VectorTransformation2D::getVerticalScaling() const
126 : {
127 0 : return m_m12 * m_m12 + m_m22 * m_m22;
128 : }
129 :
130 0 : bool libmspub::VectorTransformation2D::orientationReversing() const
131 : {
132 : // Is the determinant negative?
133 0 : return m_m11 * m_m22 < m_m12 * m_m21;
134 : }
135 : /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
|