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@inrae.fr 0035 % Created: 2010-12-03, using Matlab 7.9.0.529 (R2009b) 0036 % Copyright 2010-2024 INRA - Cepia Software Platform 0037 0038 % size of input arguments 0039 srcSz = size(source, 1); 0040 tgtSz = size(target, 1); 0041 maxSz = max(srcSz, tgtSz); 0042 0043 % check case of multiple inputs 0044 if maxSz > 1 0045 [t1, t2] = deal( bsxfun(@times, eye(4), ones(1,1,maxSz)) ); 0046 if srcSz > 1 0047 source = permute(source, [3 2 1]); 0048 end 0049 if tgtSz > 1 0050 target = permute(target, [3 2 1]); 0051 end 0052 else 0053 [t1, t2] = deal(eye(4)); 0054 end 0055 0056 % Place source and target planes into t1 and t2 t-form matrices. If either 0057 % input is non-numeric it is assumed to mean 'global', or identity t-form. 0058 if isnumeric(source) 0059 if maxSz > 1 && srcSz == 1 0060 source = bsxfun(@times, source, ones(1,1,maxSz)); 0061 end 0062 t1(1:3, 1, :) = source(1, 4:6, :); 0063 t1(1:3, 2, :) = source(1, 7:9, :); 0064 t1(1:3, 3, :) = crossProduct3d(source(1,4:6,:), source(1,7:9,:)); 0065 t1(1:3, 4, :) = source(1, 1:3, :); 0066 end 0067 if isnumeric(target) 0068 if maxSz > 1 && tgtSz == 1 0069 target = bsxfun(@times, target, ones(1,1,maxSz)); 0070 end 0071 t2(1:3, 1, :) = target(1, 4:6, :); 0072 t2(1:3, 2, :) = target(1, 7:9, :); 0073 t2(1:3, 3, :) = crossProduct3d(target(1,4:6,:), target(1,7:9,:)); 0074 t2(1:3, 4, :) = target(1, 1:3, :); 0075 end 0076 0077 0078 % compute transform matrix 0079 transfo = zeros(4, 4, maxSz); 0080 for i = 1:maxSz 0081 % coordinate of four reference points in source basis 0082 po = t1(1:3, 4, i)'; 0083 px = po + t1(1:3, 1, i)'; 0084 py = po + t1(1:3, 2, i)'; 0085 pz = po + t1(1:3, 3, i)'; 0086 0087 % express coordinates of reference points in the new basis 0088 t2i = inv(t2(:,:,i)); 0089 pot = transformPoint3d(po, t2i); 0090 pxt = transformPoint3d(px, t2i); 0091 pyt = transformPoint3d(py, t2i); 0092 pzt = transformPoint3d(pz, t2i); 0093 0094 % compute direction vectors in new basis 0095 vx = pxt - pot; 0096 vy = pyt - pot; 0097 vz = pzt - pot; 0098 0099 % concatenate result in a 4-by-4 affine transform matrix 0100 transfo(:,:,i) = [vx' vy' vz' pot' ; 0 0 0 1]; 0101 end