Fill cylinder inside a matrix

3 ビュー (過去 30 日間)
DG
DG 2019 年 2 月 12 日
コメント済み: Matt J 2021 年 1 月 3 日
Given a 3d matrix:
vx = 1; % mm
vy = 1; % mm
vz = 1; % mm
nx = 500; %Number of elements in x direction
ny = 500; %Number of elements in y direction
nz = 100; %Number of elements in z direction
x = linspace(-vx/2,vx/2,nx);
y = linspace(-vy/2,vy/2,ny);
z = linspace(-vz/2,vz/2,nz);
[X,Y,Z] = ndgrid(x,y,z);
mat= zeros(size(X)); %Matrix to fill with cyliinders
and 2 random points on the edge of the cube,
How can I make a cylinder of radius R between those 2 points?
I want to fill up the binary matrix with 1 where the cylinder is placed and 0 everywhere else.
Thank you for your help.

採用された回答

Matt J
Matt J 2019 年 2 月 13 日
編集済み: Matt J 2019 年 2 月 13 日
Suppose the two end points are r1=[x1,y1,z1], r2=[x2,y2,z2]
[X,Y,Z] = ndgrid(x,y,z);
dXYZ=[X(:),Y(:),Z(:)].'-r1(:);
u=r1-r2;
u=repmat(u(:)./norm(u),1,nx*ny*nz);
mat=vecnorm( cross(dXYZ,u) ,2,1 )<=R;
mat=reshape(mat,nx,ny,nz);
  3 件のコメント
Matt J
Matt J 2019 年 2 月 13 日
編集済み: Matt J 2019 年 2 月 13 日
Yes, u is the direction vector of the central line of the cylinder. The left hand side of the inequality is the formula for the distance of points in your ndgrid to that line.
Matt J
Matt J 2021 年 1 月 3 日
It should be giving you filled cylinders.

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2019 年 2 月 12 日
編集済み: Matt J 2019 年 2 月 12 日
Choose a coordinate (xcenter,ycenter) where the cylinder will be centered and do,
mat = ( (x-xcenter).^2 + (y-ycenter).^2 <= R^2 );
  1 件のコメント
DG
DG 2019 年 2 月 12 日
But I want cylinder between two locations. This won't work for a tilted cylinder, I think. Something like the attached picture. In this case, if I take a slice in z axis, it will form some kind of ellipse and not a circle.
Hope this makes it clear.
Thank you!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeComputational Geometry についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by