Meshgrid orthogonal to a line in 3D Space
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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;
0 commentaires
Réponses (3)
Matt J
le 20 Mai 2022
Modifié(e) : Matt J
le 20 Mai 2022
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 commentaires
Matt J
le 20 Mai 2022
Modifié(e) : Matt J
le 20 Mai 2022
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1005765/image.png)
0 commentaires
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!