Meshgrid orthogonal to a line in 3D Space

4 次查看(过去 30 天)
In 3D space, how do I generate a set of points, like a meshgrid, orthogonal to a specific straight line passing through its center? This is definitely not a difficult problem, but I'm overcomplicating it somewhere in my brain and related questions didn't help me with the gridding part.
For a line orientated along one axis, what I'm trying to do is this:
X = [-5:5]; Y = ones(size(X)); Z = ones(size(X)); %Define Line
No_points = 5;
plane_x = linspace(-1,1,5); plane_y = linspace(0.9,1.1,5); plane_z = linspace(0.9,1.1,5); %Specify Points
[plane_x,plane_y,plane_z]=meshgrid(plane_x,plane_y,plane_z);
figure;plot3(X,Y,Z,'r-'); hold on; plot3(plane_x(:),plane_y(:),plane_z(:),'k.');
How do I generate the same orthogonal meshgrid of points passing through the origin for a "3D line", e.g:
X = [-5:5]; Y = X; Z = X;

回答(3 个)

Matt J
Matt J 2022-5-20
编辑:Matt J 2022-5-20
Pick a 3D direction vector for the straight line, e.g.
d=[1,1,1];
Then,
d=d(:)./norm(d);
B=null(d.'); %basis
[x,y,z]=meshgrid(linspace(-5,5,10));
[m,n]=size(x);
res=@(q)reshape(q,1,m,n);
XYZ=B(:,1).*res(x) + B(:,2).*res(y) +d(:).*res(z);
scatter3(XYZ(1,:), XYZ(2,:), XYZ(3,:) );
axis equal
xlabel X, ylabel Y, zlabel Z, view(30,40)
  2 个评论
Torsten
Torsten 2022-5-20
编辑:Torsten 2022-5-20
... followed by the 5-fold copy-translate of this meshgrid along the positive and negative normal of the plane :-)

请先登录,再进行评论。


Matt J
Matt J 2022-5-20
编辑:Matt J 2022-5-20
There are also ready-made File Exchange tools you can use, like this one
d=[1;1;1]; %direction of line
d=d(:)./norm(d);
gtPlane=planarFit.groundtruth([],d,0); %ground truth plane
b0=[1,0,0];
b1=cross([0,1,0],gtPlane.normal); %Make one sampling direction parallel to x-z plane
b2=[]; %Make the other direction orthogonal to b1
t=linspace(-5,5,10);
XYZ=gtPlane.sample(b0,b1,b2,t,t); %Post-sample the plane
XYZ=num2cell( cell2mat(XYZ)+d(:).*reshape(t,1,1,[]) ,[2,3]); %expand along d
hPost=scatter3(XYZ{1}(:), XYZ{2}(:), XYZ{3}(:));
xlabel X, ylabel Y, zlabel Z; view(30,40); axis equal

Matt J
Matt J 2022-5-20
编辑:Matt J 2022-5-20
You can also start with an unrotated grid, then rotate it:
d=[1;1;1]; d=d(:).'/norm(d);
[X,Y,Z]=meshgrid(-5:5);
R=[d;null(d)'];
XYZ=num2cell( [X(:),Y(:),Z(:)]*R',1);
scatter3(XYZ{:});
axis equal
xlabel X, ylabel Y, zlabel Z, view(30,40)
  1 个评论
ADSW121365
ADSW121365 2022-5-23
Firstly, I assume my question is at fault rather than the answer. Both the first and this answer give the same solution, however this is more concise. Choosing Nx = 3 to make the issue/question clearer.
Implementation:
clear,close all; clc;
Line_X = linspace(-5,5,5); Line_Y = Line_X; Line_Z = Line_X;
%Generate Evaluation Points:
Xl = -2; Xu = 2; Nx = 3; Yl = -2; Yu = 2; Ny = 100; Zl = -2; Zu = 2; Nz = 100;
[opt_x,opt_y,opt_z] = ...
meshgrid(linspace(Xl,Xu,Nx),linspace(Yl,Yu,Ny),linspace(Zl,Zu,Nz));
%Rotate Points:
d=[1;1;1]; d=d(:).'/norm(d);
R=[d;null(d)'];
opt = [opt_x(:),opt_y(:),opt_z(:)]*R';
opt_x = opt(:,1)'; opt_y = opt(:,2)'; opt_z = opt(:,3)';
%Plot:
figure; plot3(Line_X,Line_Y,Line_Z,'r-'); hold on;
plot3(opt_x(:),opt_y(:),opt_z(:),'k.')
Here I am trying to make each of the 'planes' to intersect the line, but be perpendicular at all points. Returning back the the version I am able to code, for the simpler line used in my question I am looking for:
X = [-5:5]; Y = ones(size(X)); Z = ones(size(X)); %Define Line
plane_x = linspace(-2,2,3); plane_y = linspace(0.9,1.1,100); plane_z = linspace(0.9,1.1,100);
[plane_x,plane_y,plane_z]=meshgrid(plane_x,plane_y,plane_z);
figure;plot3(X,Y,Z,'r-'); hold on;
plot3(plane_x(:),plane_y(:),plane_z(:),'k.');

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by