CREATEBASISTRANSFORM3D Compute matrix for transforming a basis into another basis. TRANSFO = createBasisTransform3d(SOURCE, TARGET) will create a 4-by-4 transformation matrix representing the transformation from SOURCE basis to TARGET basis. SOURCE and TARGET are either standard 1-by-9 geom3d PLANE representations of the form: [x0 y0 z0 ex1 ey1 ez1 ex2 ey2 ez2] OR SOURCE and TARGET may be any string such as 'global' or 'g' in which case they represent the global plane [0 0 0 1 0 0 0 1 0]. The resulting TRANSFO matrix is such that a point expressed with coordinates of the first basis will be represented by new coordinates P2 = transformPoint3d(P1, TRANSFO) in the target basis. Either (or both) SOURCE or TARGET may be an N-by-9 set of N planes. In that case, TRANSFO will be a 4-by-4-by-N array of N transformation matrices. Example: % Calculate local plane coords. of a point given in global coords. plane = [10 10 10 1 0 0 0 1 0]; transfo = createBasisTransform3d('global', plane); PT_IN_PLANE = transformPoint3d([3 8 2], transfo) PT_IN_PLANE = 13 18 12 See also transforms3d, transformPoint3d, planePosition, createBasisTransform
0001 function transfo = createBasisTransform3d(source, target) 0002 %CREATEBASISTRANSFORM3D Compute matrix for transforming a basis into another basis. 0003 % 0004 % TRANSFO = createBasisTransform3d(SOURCE, TARGET) will create a 4-by-4 0005 % transformation matrix representing the transformation from SOURCE basis 0006 % to TARGET basis. 0007 % SOURCE and TARGET are either standard 1-by-9 geom3d PLANE 0008 % representations of the form: [x0 y0 z0 ex1 ey1 ez1 ex2 ey2 ez2] 0009 % OR 0010 % SOURCE and TARGET may be any string such as 'global' or 'g' in which 0011 % case they represent the global plane [0 0 0 1 0 0 0 1 0]. 0012 % 0013 % The resulting TRANSFO matrix is such that a point expressed with 0014 % coordinates of the first basis will be represented by new coordinates 0015 % P2 = transformPoint3d(P1, TRANSFO) in the target basis. 0016 % 0017 % Either (or both) SOURCE or TARGET may be an N-by-9 set of N planes. In 0018 % that case, TRANSFO will be a 4-by-4-by-N array of N transformation 0019 % matrices. 0020 % 0021 % Example: 0022 % % Calculate local plane coords. of a point given in global coords. 0023 % plane = [10 10 10 1 0 0 0 1 0]; 0024 % transfo = createBasisTransform3d('global', plane); 0025 % PT_IN_PLANE = transformPoint3d([3 8 2], transfo) 0026 % PT_IN_PLANE = 0027 % 13 18 12 0028 % 0029 % See also 0030 % transforms3d, transformPoint3d, planePosition, createBasisTransform 0031 0032 % ------ 0033 % Author: David Legland 0034 % e-mail: david.legland@inra.fr 0035 % Created: 2010-12-03, using Matlab 7.9.0.529 (R2009b) 0036 % Copyright 2010 INRA - Cepia Software Platform. 0037 0038 % HISTORY 0039 % 2013-07-03 added support for multiple inputs (Sven Holcombe) 0040 % 2017-10-16 rewrite 0041 0042 % size of input arguments 0043 srcSz = size(source, 1); 0044 tgtSz = size(target, 1); 0045 maxSz = max(srcSz, tgtSz); 0046 0047 % check case of multiple inputs 0048 if maxSz > 1 0049 [t1, t2] = deal( bsxfun(@times, eye(4), ones(1,1,maxSz)) ); 0050 if srcSz > 1 0051 source = permute(source, [3 2 1]); 0052 end 0053 if tgtSz > 1 0054 target = permute(target, [3 2 1]); 0055 end 0056 else 0057 [t1, t2] = deal(eye(4)); 0058 end 0059 0060 % Place source and target planes into t1 and t2 t-form matrices. If either 0061 % input is non-numeric it is assumed to mean 'global', or identity t-form. 0062 if isnumeric(source) 0063 if maxSz > 1 && srcSz == 1 0064 source = bsxfun(@times, source, ones(1,1,maxSz)); 0065 end 0066 t1(1:3, 1, :) = source(1, 4:6, :); 0067 t1(1:3, 2, :) = source(1, 7:9, :); 0068 t1(1:3, 3, :) = crossProduct3d(source(1,4:6,:), source(1,7:9,:)); 0069 t1(1:3, 4, :) = source(1, 1:3, :); 0070 end 0071 if isnumeric(target) 0072 if maxSz > 1 && tgtSz == 1 0073 target = bsxfun(@times, target, ones(1,1,maxSz)); 0074 end 0075 t2(1:3, 1, :) = target(1, 4:6, :); 0076 t2(1:3, 2, :) = target(1, 7:9, :); 0077 t2(1:3, 3, :) = crossProduct3d(target(1,4:6,:), target(1,7:9,:)); 0078 t2(1:3, 4, :) = target(1, 1:3, :); 0079 end 0080 0081 0082 % compute transform matrix 0083 transfo = zeros(4, 4, maxSz); 0084 for i = 1:maxSz 0085 % coordinate of four reference points in source basis 0086 po = t1(1:3, 4, i)'; 0087 px = po + t1(1:3, 1, i)'; 0088 py = po + t1(1:3, 2, i)'; 0089 pz = po + t1(1:3, 3, i)'; 0090 0091 % express coordinates of reference points in the new basis 0092 t2i = inv(t2(:,:,i)); 0093 pot = transformPoint3d(po, t2i); 0094 pxt = transformPoint3d(px, t2i); 0095 pyt = transformPoint3d(py, t2i); 0096 pzt = transformPoint3d(pz, t2i); 0097 0098 % compute direction vectors in new basis 0099 vx = pxt - pot; 0100 vy = pyt - pot; 0101 vz = pzt - pot; 0102 0103 % concatenate result in a 4-by-4 affine transform matrix 0104 transfo(:,:,i) = [vx' vy' vz' pot' ; 0 0 0 1]; 0105 end