transform.cc 3.64 KB
Newer Older
1
// -*- related-file-name: "../include/lcdf/transform.hh" -*-
Eddie Kohler's avatar
Eddie Kohler committed
2 3 4

/* transform.{cc,hh} -- planar affine transformations
 *
Eddie Kohler's avatar
Eddie Kohler committed
5
 * Copyright (c) 2000-2019 Eddie Kohler
Eddie Kohler's avatar
Eddie Kohler committed
6 7 8 9 10 11 12 13 14 15
 *
 * 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 (at your option)
 * 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.
 */

16 17 18
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
19
#include <lcdf/transform.hh>
Eddie Kohler's avatar
Eddie Kohler committed
20
#include <lcdf/straccum.hh>
Eddie Kohler's avatar
Eddie Kohler committed
21
#include <math.h>
22 23 24

Transform::Transform()
{
25 26
    _m[0] = _m[3] = 1;
    _m[1] = _m[2] = _m[4] = _m[5] = 0;
27
    _null = true;
28 29
}

30 31 32 33 34 35 36 37 38 39 40
Transform::Transform(const double m[6])
{
    _m[0] = m[0];
    _m[1] = m[1];
    _m[2] = m[2];
    _m[3] = m[3];
    _m[4] = m[4];
    _m[5] = m[5];
    check_null(0);
}

41 42 43
Transform::Transform(double m0, double m1, double m2,
		     double m3, double m4, double m5)
{
44 45 46 47 48 49
    _m[0] = m0;
    _m[1] = m1;
    _m[2] = m2;
    _m[3] = m3;
    _m[4] = m4;
    _m[5] = m5;
50 51 52 53 54 55 56
    check_null(0);
}

void
Transform::check_null(double tolerance)
{
    _null = (fabs(_m[0] - 1) < tolerance && fabs(_m[1]) < tolerance
57 58
	     && fabs(_m[2]) < tolerance && fabs(_m[3] - 1) < tolerance
	     && fabs(_m[4]) < tolerance && fabs(_m[5]) < tolerance);
59 60 61 62
}


void
63
Transform::scale(double x, double y)
64
{
65
    _m[0] *= x;
66 67 68
    _m[1] *= x;
    _m[2] *= y;
    _m[3] *= y;
Eddie Kohler's avatar
Eddie Kohler committed
69 70

    if (x != 1 || y != 1)
71
	_null = false;
72 73 74
}

void
75
Transform::rotate(double r)
76
{
77 78
    double c = cos(r);
    double s = sin(r);
79

80
    double a = _m[0], b = _m[2];
81
    _m[0] = a*c + b*s;
82
    _m[2] = b*c - a*s;
83

84 85 86
    a = _m[1], b = _m[3];
    _m[1] = a*c + b*s;
    _m[3] = b*c - a*s;
Eddie Kohler's avatar
Eddie Kohler committed
87 88

    if (r != 0)
89
	_null = false;
90 91 92
}

void
93
Transform::translate(double x, double y)
94
{
95 96
    _m[4] += _m[0]*x + _m[2]*y;
    _m[5] += _m[1]*x + _m[3]*y;
Eddie Kohler's avatar
Eddie Kohler committed
97 98

    if (x != 0 || y != 0)
99
	_null = false;
100
}
Eddie Kohler's avatar
Eddie Kohler committed
101

102 103 104 105 106 107 108 109
void Transform::raw_translate(double x, double y) {
    _m[4] += x;
    _m[5] += y;

    if (x != 0 || y != 0)
        _null = false;
}

110 111 112 113 114 115
void
Transform::shear(double s)
{
    *this *= Transform(1, 0, s, 1, 0, 0);
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
Transform& Transform::operator*=(const Transform& x) {
    if (x.null())
        /* do nothing */;
    else if (null())
        memcpy(_m, x._m, sizeof(_m));
    else {
        double m[6];
        m[0] = _m[0] * x._m[0] + _m[2] * x._m[1];
        m[1] = _m[1] * x._m[0] + _m[3] * x._m[1];
        m[2] = _m[0] * x._m[2] + _m[2] * x._m[3];
        m[3] = _m[1] * x._m[2] + _m[3] * x._m[3];
        m[4] = _m[0] * x._m[4] + _m[2] * x._m[5] + _m[4];
        m[5] = _m[1] * x._m[4] + _m[3] * x._m[5] + _m[5];
        memcpy(_m, m, sizeof(_m));
    }
    return *this;
Eddie Kohler's avatar
Eddie Kohler committed
132 133 134
}


135 136
void
Transform::real_apply_to(Point &p) const
Eddie Kohler's avatar
Eddie Kohler committed
137
{
138
    double x = p.x;
139 140
    p.x = x*_m[0] + p.y*_m[2] + _m[4];
    p.y = x*_m[1] + p.y*_m[3] + _m[5];
Eddie Kohler's avatar
Eddie Kohler committed
141 142 143
}

Point
144
Transform::real_apply(const Point &p) const
Eddie Kohler's avatar
Eddie Kohler committed
145
{
146 147
    return Point(p.x*_m[0] + p.y*_m[2] + _m[4],
		 p.x*_m[1] + p.y*_m[3] + _m[5]);
Eddie Kohler's avatar
Eddie Kohler committed
148 149 150 151 152
}

Bezier &
operator*=(Bezier &b, const Transform &t)
{
153
    if (!t.null()) {
Eddie Kohler's avatar
Eddie Kohler committed
154 155 156 157 158
	b.mpoint(0) *= t;
	b.mpoint(1) *= t;
	b.mpoint(2) *= t;
	b.mpoint(3) *= t;
    }
Eddie Kohler's avatar
Eddie Kohler committed
159 160 161 162 163 164
    return b;
}

Bezier
operator*(const Bezier &b, const Transform &t)
{
165
    return (t.null()
Eddie Kohler's avatar
Eddie Kohler committed
166 167
	    ? b
	    : Bezier(b.point(0) * t, b.point(1) * t, b.point(2) * t, b.point(3) * t));
Eddie Kohler's avatar
Eddie Kohler committed
168
}
Eddie Kohler's avatar
Eddie Kohler committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182

String
Transform::unparse() const
{
    StringAccum sa;
    sa << '[';
    for (int i = 0; i < 6; i++) {
	if (i)
	    sa << ',' << ' ';
	sa << _m[i];
    }
    sa << ']';
    return sa.take_string();
}