[英]Slicing of 3D trajectory curve in MATLAB
I have numerical data that corresponds to a particle moving in three dimensions, sampled at n equally spaced times t = [1,2,3,4,5,...], resulting in three coordinate arrays, eg, for n=5:我有对应于在三个维度上移动的粒子的数值数据,在 n 个等间隔时间 t = [1,2,3,4,5,...] 采样,得到三个坐标 arrays,例如,对于 n=5 :
x = [1,2,3,4,5]
y = [1,5,6,7,10]
z = [2,3,8,9,10]
I have been able to find a decent interpolation using cscvn, but I am now at a loss as to how I would find the x and y coordinates that correspond to a fixed value of z, ie, a constant z slice.我已经能够使用 cscvn 找到合适的插值,但我现在不知道如何找到对应于固定 z 值(即常量 z 切片)的 x 和 y 坐标。
You can extract the individual cubics within the curve output by cscvn, then examine each one for if and where it crosses your chosen Z value within that piece's boundary.您可以通过 cscvn 提取曲线 output 内的各个立方体,然后检查每个立方体是否以及在何处与您选择的 Z 值在该部分边界内相交。
x = randi(10,[1 5]);
y = randi(10,[1 5]);
z = randi(10,[1 5]);
zChosen = 5;
curve = cscvn([x;y;z]);
parametricCrossingPoints = struct('crossingPoints',[],'Xs',[],'Ys',[],'Zs',[]);
parametricCrossingPoints = repmat(parametricCrossingPoints,curve.pieces,1);
% for each peice of the parametric curve, find the points where it crosses
% your desired z
for i = 1:size(curve.coefs,1)/3
% Find where parametric piece crosses zChosen
pPoints = roots(curve.coefs(i*3,:)'-[0;0;0;zChosen]);
% Filter out imaginary roots
pPoints = real(pPoints(imag(pPoints) == 0));
% Filter for points within the piece's boundary
pPoints(pPoints < 0 | pPoints > curve.breaks(i+1)-curve.breaks(i)) = [];
% Append points to list
parametricCrossingPoints(i).crossingPoints = pPoints + curve.breaks(i);
end
% Get x, y, and z of each crossing point
for i = 1:numel(parametricCrossingPoints)
xyzOfCrossingPoints = fnval(curve,parametricCrossingPoints(i).crossingPoints);
parametricCrossingPoints(i).Xs = xyzOfCrossingPoints(1,:);
parametricCrossingPoints(i).Ys = xyzOfCrossingPoints(2,:);
parametricCrossingPoints(i).Zs = xyzOfCrossingPoints(3,:); % Should all equal zChosen
end
% Collate points
Xs = [parametricCrossingPoints.Xs];
Ys = [parametricCrossingPoints.Ys];
Zs = [parametricCrossingPoints.Zs];
% Sometimes breakpoints aren't chosen, add them manually if necessary
xsToAdd = x(z == zChosen);
ysToAdd = y(z == zChosen);
for i = 1:numel(xsToAdd)
if ~any(Ys(Xs == xsToAdd(i)) == ysToAdd(i))
Xs(end+1) = xsToAdd(i);
Ys(end+1) = ysToAdd(i);
Zs(end+1) = zChosen;
end
end
% Plot results
fnplt(curve);
hold on
plot3(x,y,z,'o')
patch([min(x)-1,max(x)+1,max(x)+1,min(x)-1],...
[min(y)-1,min(y)-1,max(y)+1,max(y)+1],...
[zChosen,zChosen,zChosen,zChosen], ...
'green',...
'FaceAlpha',0.5)
plot3(Xs,Ys,Zs,'*')
hold off
There's some funky calculation to do around the parametric independent parameter (which doesn't match your time = t) for each piece of the curve and the specified breakpoints, but other than that it's pretty straightforward.对于每条曲线和指定的断点,围绕参数独立参数(与您的时间 = t 不匹配)进行一些时髦的计算,但除此之外它非常简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.