Home > matGeom > geom2d > createBasisTransform.m

createBasisTransform

PURPOSE ^

CREATEBASISTRANSFORM Compute matrix for transforming a basis into another basis.

SYNOPSIS ^

function transfo = createBasisTransform(source, target)

DESCRIPTION ^

CREATEBASISTRANSFORM Compute matrix for transforming a basis into another basis.

   TRANSFO = createBasisTransform(SOURCE, TARGET)
   Both SOURCE and TARGET represent basis, in the following form:
   [x0 y0  ex1 ey1  ex2 ey2]
   [y0 y0] is the origin of the basis, [ex1 ey1] is the first direction
   vector, and [ex2 ey2] is the second direction vector.

   The result TRANSFO is a 3-by-3 matrix such that a point expressed with
   coordinates of the first basis will be represented by new coordinates
   P2 = transformPoint(P1, TRANSFO) in the target basis.
   
   TRANSFO = createBasisTransform(TARGET)
   Assumes the source is the standard (Oij) basis, with origin at (0,0),
   first direction vector equal to (1,0) and second direction  vector
   equal to (0,1).


   Example
     % define source and target bases
     src = [ 0 0   1  0    0  1];
     tgt = [20 0  .5 .5  -.5 .5];
     trans = createBasisTransform(src, tgt);
     % create a polygon in source basis
     poly = [10 10;30 10; 30 20; 20 20;20 40; 10 40];
     figure;
     subplot(121); drawPolygon(poly, 'b'); axis equal; axis([-10 50 -10 50]);
     hold on; drawLine([0 0 1 0], 'k'); drawLine([0 0 0 1], 'k');
     drawLine([20 0 1 1], 'r'); drawLine([20 0 -1 1], 'r');
     t = -1:5; plot(t*5+20, t*5, 'r.'); plot(-t*5+20, t*5, 'r.');
     % transform the polygon in target basis
     poly2 = transformPoint(poly, trans);
     subplot(122); drawPolygon(poly2, 'b'); axis equal; axis([-10 50 -10 50]);
     hold on; drawLine([0 0 1 0], 'r'); drawLine([0 0 0 1], 'r');
     t = -1:5; plot(t*10, zeros(size(t)), 'r.'); plot(zeros(size(t)), t*10, 'r.');

   See also
   transforms2d

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function transfo = createBasisTransform(source, target)
0002 %CREATEBASISTRANSFORM Compute matrix for transforming a basis into another basis.
0003 %
0004 %   TRANSFO = createBasisTransform(SOURCE, TARGET)
0005 %   Both SOURCE and TARGET represent basis, in the following form:
0006 %   [x0 y0  ex1 ey1  ex2 ey2]
0007 %   [y0 y0] is the origin of the basis, [ex1 ey1] is the first direction
0008 %   vector, and [ex2 ey2] is the second direction vector.
0009 %
0010 %   The result TRANSFO is a 3-by-3 matrix such that a point expressed with
0011 %   coordinates of the first basis will be represented by new coordinates
0012 %   P2 = transformPoint(P1, TRANSFO) in the target basis.
0013 %
0014 %   TRANSFO = createBasisTransform(TARGET)
0015 %   Assumes the source is the standard (Oij) basis, with origin at (0,0),
0016 %   first direction vector equal to (1,0) and second direction  vector
0017 %   equal to (0,1).
0018 %
0019 %
0020 %   Example
0021 %     % define source and target bases
0022 %     src = [ 0 0   1  0    0  1];
0023 %     tgt = [20 0  .5 .5  -.5 .5];
0024 %     trans = createBasisTransform(src, tgt);
0025 %     % create a polygon in source basis
0026 %     poly = [10 10;30 10; 30 20; 20 20;20 40; 10 40];
0027 %     figure;
0028 %     subplot(121); drawPolygon(poly, 'b'); axis equal; axis([-10 50 -10 50]);
0029 %     hold on; drawLine([0 0 1 0], 'k'); drawLine([0 0 0 1], 'k');
0030 %     drawLine([20 0 1 1], 'r'); drawLine([20 0 -1 1], 'r');
0031 %     t = -1:5; plot(t*5+20, t*5, 'r.'); plot(-t*5+20, t*5, 'r.');
0032 %     % transform the polygon in target basis
0033 %     poly2 = transformPoint(poly, trans);
0034 %     subplot(122); drawPolygon(poly2, 'b'); axis equal; axis([-10 50 -10 50]);
0035 %     hold on; drawLine([0 0 1 0], 'r'); drawLine([0 0 0 1], 'r');
0036 %     t = -1:5; plot(t*10, zeros(size(t)), 'r.'); plot(zeros(size(t)), t*10, 'r.');
0037 %
0038 %   See also
0039 %   transforms2d
0040 %
0041 
0042 % ------
0043 % Author: David Legland
0044 % e-mail: david.legland@inra.fr
0045 % Created: 2010-12-03,    using Matlab 7.9.0.529 (R2009b)
0046 % Copyright 2010 INRA - Cepia Software Platform.
0047 
0048 % init basis transform to identity
0049 t1 = eye(3);
0050 t2 = eye(3);
0051 
0052 if nargin == 2
0053     % from source to reference basis
0054     t1(1:2, 1) = source(3:4);
0055     t1(1:2, 2) = source(5:6);
0056     t1(1:2, 3) = source(1:2);
0057 else
0058     % if only one input, use first input as target basis, and leave the
0059     % first matrix to identity
0060     target = source;
0061 end
0062 
0063 % from reference to target basis
0064 t2(1:2, 1) = target(3:4);
0065 t2(1:2, 2) = target(5:6);
0066 t2(1:2, 3) = target(1:2);
0067 
0068 % compute transform matrix
0069 transfo = zeros(3, 3);
0070 maxSz = 1;
0071 for i = 1:maxSz
0072     % coordinate of three reference points in source basis
0073     po = t1(1:2, 3, i)';
0074     px = po + t1(1:2, 1, i)';
0075     py = po + t1(1:2, 2, i)';
0076     
0077     % express coordinates of reference points in the new basis
0078     t2i = inv(t2(:,:,i));
0079     pot = transformPoint(po, t2i);
0080     pxt = transformPoint(px, t2i);
0081     pyt = transformPoint(py, t2i);
0082     
0083     % compute direction vectors in new basis
0084     vx = pxt - pot;
0085     vy = pyt - pot;
0086 
0087     % concatenate result in a 3-by-3 affine transform matrix
0088     transfo(:,:,i) = [vx' vy' pot' ; 0 0 1];
0089 end
0090

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019