libgeda

m_transform.c

Go to the documentation of this file.
00001 /* gEDA - GPL Electronic Design Automation
00002  * libgeda - gEDA's library
00003  * Copyright (C) 1998-2010 Ales Hvezda
00004  * Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 #include <config.h>
00021 #include <math.h>
00022 #include <libgeda_priv.h>
00023 
00033 void m_transform_combine(TRANSFORM *result, TRANSFORM *a, TRANSFORM *b )
00034 {
00035   g_return_if_fail(result!=NULL);
00036   g_return_if_fail(a!=NULL);
00037   g_return_if_fail(b!=NULL);
00038 
00039   result->m[0][0] = a->m[0][0] * b->m[0][0] + a->m[0][1] * b->m[1][0];
00040   result->m[0][1] = a->m[0][0] * b->m[0][1] + a->m[0][1] * b->m[1][1];
00041   result->m[0][2] = a->m[0][0] * b->m[0][2] + a->m[0][1] * b->m[1][2] + a->m[0][2];
00042   result->m[1][0] = a->m[1][0] * b->m[0][0] + a->m[1][1] * b->m[1][0];
00043   result->m[1][1] = a->m[1][0] * b->m[0][1] + a->m[1][1] * b->m[1][1];
00044   result->m[1][2] = a->m[1][0] * b->m[0][2] + a->m[1][1] * b->m[1][2] + a->m[1][2];
00045 }
00046 
00051 void m_transform_init(TRANSFORM *transform)
00052 {
00053   g_return_if_fail(transform!=NULL);
00054 
00055   transform->m[0][0] = 1;
00056   transform->m[0][1] = 0;
00057   transform->m[0][2] = 0;
00058   transform->m[1][0] = 0;
00059   transform->m[1][1] = 1;
00060   transform->m[1][2] = 0;
00061 }
00062 
00068 void m_transform_invert(TRANSFORM *transform, TRANSFORM *inverse)
00069 {
00070   gdouble d;
00071 
00072   g_return_if_fail(transform!=NULL);
00073   g_return_if_fail(inverse!=NULL);
00074 
00075   d = transform->m[0][0]*transform->m[1][1] - transform->m[1][0]*transform->m[0][1];
00076 
00077   inverse->m[0][0] =  transform->m[1][1] / d;
00078   inverse->m[0][1] = -transform->m[0][1] / d;
00079   inverse->m[0][2] =  ( transform->m[0][1]*transform->m[1][2] - transform->m[1][1]*transform->m[0][2] ) / d;
00080   inverse->m[1][0] = -transform->m[1][0] / d;
00081   inverse->m[1][1] =  transform->m[0][0] / d;
00082   inverse->m[1][2] = -( transform->m[0][0]*transform->m[1][2] - transform->m[1][0]*transform->m[0][2] ) / d;
00083 }
00084 
00090 void m_transform_line(TRANSFORM *transform, LINE *line)
00091 {
00092   g_return_if_fail(transform!=NULL);
00093   g_return_if_fail(line!=NULL);
00094 
00095   m_transform_point(transform, &(line->x[0]), &(line->y[0]));
00096   m_transform_point(transform, &(line->x[1]), &(line->y[1]));
00097 }
00098 
00104 void m_transform_lines(TRANSFORM *transform, GArray *lines)
00105 {
00106   gint index;
00107 
00108   g_return_if_fail(transform!=NULL);
00109   g_return_if_fail(lines!=NULL);
00110 
00111   for (index=0; index<lines->len; index++) {
00112     LINE *line = &g_array_index(lines, LINE, index);
00113     m_transform_line(transform, line);
00114   }
00115 }
00116 
00123 void m_transform_point(TRANSFORM *transform, gint *x, gint *y)
00124 {
00125   gdouble tx;
00126   gdouble ty;
00127 
00128   g_return_if_fail(transform!=NULL);
00129   g_return_if_fail(x!=NULL);
00130   g_return_if_fail(y!=NULL);
00131 
00132   tx = *x;
00133   ty = *y;
00134 
00135   *x = round(transform->m[0][0] * tx + transform->m[0][1] * ty + transform->m[0][2]);
00136   *y = round(transform->m[1][0] * tx + transform->m[1][1] * ty + transform->m[1][2]);
00137 }
00138 
00144 void m_transform_points(TRANSFORM *transform, GArray *points)
00145 {
00146   gint index;
00147 
00148   g_return_if_fail(transform!=NULL);
00149   g_return_if_fail(points!=NULL);
00150 
00151   for (index=0; index<points->len; index++) {
00152     sPOINT *point = &g_array_index(points, sPOINT, index);
00153     m_transform_point(transform, &(point->x), &(point->y));
00154   }
00155 }
00156 
00162 void m_transform_rotate(TRANSFORM *transform, gdouble angle)
00163 {
00164   gdouble r = G_PI*angle/180.0;
00165   gdouble c = cos(r);
00166   gdouble s = sin(r);
00167   TRANSFORM temp;
00168 
00169   g_return_if_fail(transform!=NULL);
00170 
00171   temp = *transform;
00172 
00173   transform->m[0][0] = temp.m[0][0] *  c + temp.m[0][1] * s;
00174   transform->m[0][1] = temp.m[0][0] * -s + temp.m[0][1] * c;
00175   transform->m[1][0] = temp.m[1][0] *  c + temp.m[1][1] * s;
00176   transform->m[1][1] = temp.m[1][0] * -s + temp.m[1][1] * c;
00177 }
00178 
00185 void m_transform_scale(TRANSFORM *transform, gdouble factor)
00186 {
00187   g_return_if_fail(transform!=NULL);
00188   g_return_if_fail(factor!=0);
00189 
00190   transform->m[0][0] *= factor;
00191   transform->m[0][1] *= factor;
00192   transform->m[1][0] *= factor;
00193   transform->m[1][1] *= factor;
00194 }
00195 
00202 void m_transform_translate(TRANSFORM *transform, gdouble dx, gdouble dy)
00203 {
00204   g_return_if_fail(transform!=NULL);
00205 
00206   transform->m[0][2] += dx;
00207   transform->m[1][2] += dy;
00208 }
00209 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines