|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T g
Length: 8341 (0x2095) Types: TextFile Names: »gsmatrix.c«
└─⟦a05ed705a⟧ Bits:30007078 DKUUG GNU 2/12/89 └─⟦ff23ba0e6⟧ »./ghostscript-1.3.tar.Z« └─⟦a24a58cd3⟧ └─⟦this⟧ »gsmatrix.c«
/* Copyright (C) 1989 Aladdin Enterprises. All rights reserved. Distributed by Free Software Foundation, Inc. This file is part of Ghostscript. Ghostscript is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to anyone for the consequences of using it or for whether it serves any particular purpose or works at all, unless he says so in writing. Refer to the Ghostscript General Public License for full details. Everyone is granted permission to copy, modify and redistribute Ghostscript, but only under the conditions described in the Ghostscript General Public License. A copy of this license is supposed to have been given to you along with Ghostscript so you can know your rights and responsibilities. It should be in a file named COPYING. Among other things, the copyright notice and this notice must be preserved on all copies. */ /* gsmatrix.c */ /* Matrix operators for GhostScript library */ #include <math.h> #include "gx.h" #include "gserrors.h" #include "gxfixed.h" #include "gxmatrix.h" /* The identity matrix */ /* This should be private (static), but the interpreter */ /* has to be able to fill in the "unused" words. */ /*static*/ gs_matrix gs_identity_matrix = { identity_matrix_body }; /* Macro to test for shortcut (no xy or yx terms) */ #define is_fzero(f) (*(long *)(&(f)) == 0) #define shortcut(pmat) (is_fzero((pmat)->xy) && is_fzero((pmat)->yx)) /* ------ Matrix creation ------ */ /* Create an identity matrix */ void gs_make_identity(gs_matrix *pmat) { *pmat = gs_identity_matrix; } /* Create a translation matrix */ int gs_make_translation(floatp dx, floatp dy, register gs_matrix *pmat) { *pmat = gs_identity_matrix; pmat->tx = dx; pmat->ty = dy; return 0; } /* Create a scaling matrix */ int gs_make_scaling(floatp sx, floatp sy, register gs_matrix *pmat) { *pmat = gs_identity_matrix; pmat->xx = sx; pmat->yy = sy; return 0; } /* Create a rotation matrix. */ /* The angle is in degrees. */ int gs_make_rotation(floatp ang, register gs_matrix *pmat) { float theta = ang * (M_PI / 180.0); *pmat = gs_identity_matrix; pmat->xx = pmat->yy = cos(theta); pmat->yx = -(pmat->xy = sin(theta)); return 0; } /* ------ Matrix arithmetic ------ */ /* Multiply two matrices. We should check for floating exceptions, */ /* but for the moment it's just too awkward. */ int gs_matrix_multiply(gs_matrix *pm1, gs_matrix *pm2, register gs_matrix *pmr) { /* We copy the arguments locally, */ /* in case they overlap the result. */ gs_matrix m1, m2; m1 = *pm1; m2 = *pm2; pmr->xx = m1.xx * m2.xx + m1.xy * m2.yx; pmr->xy = m1.xx * m2.xy + m1.xy * m2.yy; pmr->yx = m1.yx * m2.xx + m1.yy * m2.yx; pmr->yy = m1.yx * m2.xy + m1.yy * m2.yy; pmr->tx = m1.tx * m2.xx + m1.ty * m2.yx + m2.tx; pmr->ty = m1.tx * m2.xy + m1.ty * m2.yy + m2.ty; return 0; } /* Invert a matrix. Return gs_error_undefinedresult if not invertible. */ int gs_matrix_invert(register gs_matrix *pm, register gs_matrix *pmr) { /* We have to be careful about fetch/store order, */ /* because pm might be the same as pmr. */ if ( shortcut(pm) ) { if ( is_fzero(pm->xx) || is_fzero(pm->yy) ) return_error(gs_error_undefinedresult); pmr->xx = 1.0 / pm->xx; pmr->xy = 0.0; pmr->yx = 0.0; pmr->yy = 1.0 / pm->yy; pmr->tx = - pm->tx * pmr->xx; pmr->ty = - pm->ty * pmr->yy; } else { double det = pm->xx * pm->yy - pm->xy * pm->yx; float mxx = pm->xx, mtx = pm->tx; if ( det == 0 ) return_error(gs_error_undefinedresult); pmr->xx = pm->yy / det; pmr->xy = - pm->xy / det; pmr->yx = - pm->yx / det; pmr->yy = mxx / det; /* xx is already changed */ pmr->tx = - (pm->tx * pmr->xx + pm->ty * pmr->yx); pmr->ty = - (mtx * pmr->xy + pm->ty * pmr->yy); /* tx is already changed */ } return 0; } /* Rotate a matrix, possibly in place. The angle is in degrees. */ int gs_matrix_rotate(register gs_matrix *pm, floatp ang, register gs_matrix *pmr) { gs_matrix mat; float mxx, mxy; int quads; float tsin, tcos; /* We do some special checking for multiples of 90, */ /* so we don't get any rounding errors. */ if ( ang >= -360 && ang <= 360 && ang == (quads = (int)ang / 90) * 90 ) { int isin = 0, icos = 1, t; quads &= 3; while ( quads-- ) t = isin, isin = icos, icos = -t; tsin = isin, tcos = icos; } else { float theta = ang * (M_PI / 180.0); tsin = sin(theta); tcos = cos(theta); } mxx = pm->xx, mxy = pm->xy; pmr->xx = tcos * pm->xx + tsin * pm->yx; pmr->xy = tcos * pm->xy + tsin * pm->yy; pmr->yx = tcos * pm->yx - tsin * mxx; pmr->yy = tcos * pm->yy - tsin * mxy; pmr->tx = pm->tx; pmr->ty = pm->ty; return 0; } /* ------ Coordinate transformations (floating point) ------ */ /* Note that all the transformation routines take separate */ /* x and y arguments, but return their result in a point. */ /* Transform a point. */ int gs_point_transform(floatp x, floatp y, register gs_matrix *pmat, register gs_point *ppt) { ppt->x = x * pmat->xx + pmat->tx; ppt->y = y * pmat->yy + pmat->ty; if ( !is_fzero(pmat->yx) ) ppt->x += y * pmat->yx; if ( !is_fzero(pmat->xy) ) ppt->y += x * pmat->xy; return 0; } /* Inverse-transform a point. */ /* Return gs_error_undefinedresult if the matrix is not invertible. */ int gs_point_transform_inverse(floatp x, floatp y, register gs_matrix *pmat, register gs_point *ppt) { if ( shortcut(pmat) ) { ppt->x = (x - pmat->tx) / pmat->xx; ppt->y = (y - pmat->ty) / pmat->yy; return 0; } else { /* There are faster ways to do this, */ /* but we won't implement one unless we have to. */ gs_matrix imat; int code = gs_matrix_invert(pmat, &imat); if ( code < 0 ) return code; return gs_point_transform(x, y, &imat, ppt); } } /* Transform a distance. */ int gs_distance_transform(floatp dx, floatp dy, register gs_matrix *pmat, register gs_point *pdpt) { pdpt->x = dx * pmat->xx; pdpt->y = dy * pmat->yy; if ( !is_fzero(pmat->yx) ) pdpt->x += dy * pmat->yx; if ( !is_fzero(pmat->xy) ) pdpt->y += dx * pmat->xy; return 0; } /* Inverse-transform a distance. */ /* Return gs_error_undefinedresult if the matrix is not invertible. */ int gs_distance_transform_inverse(floatp dx, floatp dy, register gs_matrix *pmat, register gs_point *pdpt) { if ( shortcut(pmat) ) { pdpt->x = dx / pmat->xx; pdpt->y = dy / pmat->yy; } else { double det = pmat->xx * pmat->yy - pmat->xy * pmat->yx; if ( det == 0 ) return_error(gs_error_undefinedresult); pdpt->x = (dx * pmat->yy - dy * pmat->yx) / det; pdpt->y = (dy * pmat->xx - dx * pmat->xy) / det; } return 0; } /* Inverse-transform a bounding box. */ /* Return gs_error_undefinedresult if the matrix is not invertible. */ int gs_bbox_transform_inverse(float pbox_in[4], gs_matrix *pmat, float pbox_out[4]) { int code; gs_point p, w, h; float xmin, ymin, xmax, ymax; /* We must recompute the max and min after transforming, */ /* since a rotation may be involved. */ if ( (code = gs_point_transform_inverse(pbox_in[0], pbox_in[1], pmat, &p)) < 0 || (code = gs_distance_transform_inverse((float)(pbox_in[2] - pbox_in[0]), 0.0, pmat, &w)) < 0 || (code = gs_distance_transform_inverse(0.0, (float)(pbox_in[3] - pbox_in[1]), pmat, &h)) < 0 ) return code; xmin = xmax = p.x; if ( w.x < 0 ) xmin += w.x; else xmax += w.x; if ( h.x < 0 ) xmin += h.x; else xmax += h.x; ymin = ymax = p.y; if ( w.y < 0 ) ymin += w.y; else ymax += w.y; if ( h.y < 0 ) ymin += h.y; else ymax += h.y; pbox_out[0] = xmin, pbox_out[1] = ymin; pbox_out[2] = xmax, pbox_out[3] = ymax; return 0; } /* ------ Coordinate transformations (to fixed point) ------ */ /* Transform a point with a fixed-point result. */ int gs_point_transform2fixed(register gs_matrix_fixed *pmat, floatp x, floatp y, gs_fixed_point *ppt) { ppt->x = float2fixed(x * pmat->xx) + float2fixed(y * pmat->yx) + pmat->tx_fixed; ppt->y = float2fixed(x * pmat->xy) + float2fixed(y * pmat->yy) + pmat->ty_fixed; return 0; } /* Transform a distance with a fixed-point result. */ int gs_distance_transform2fixed(register gs_matrix_fixed *pmat, floatp dx, floatp dy, gs_fixed_point *ppt) { ppt->x = float2fixed(dx * pmat->xx) + float2fixed(dy * pmat->yx); ppt->y = float2fixed(dx * pmat->xy) + float2fixed(dy * pmat->yy); return 0; }