From 3D points to 2D matrices
2 次查看(过去 30 天)
显示 更早的评论
Hi everyone, I have a very straight-forward question, but I seem to have lots of difficulty to find a clear response online (I read through at least 10 posts on Mathworks and stackoverflow, and can't find a clear answer).
I basically want to convert a cloud of points (matrix Nx3) into three 2D matrixes representing the planes X-Y, Y-Z, X-Z.
Basically if I was to give you a cloud of points generating a sphere:
r=1;
p=1;
for phi=0:0.1:pi
for theta=0:0.1:2*pi
X(p)=r*cos(theta)*sin(phi);
Y(p)=r*sin(theta)*sin(phi);
Z(p)=r*sin(phi);
p=p+1;
end
end
You could view it as a cloud of points with scatter:
scatter(X,Y,Z);
Now I want to transform that data to three 2D matrixes (xy, yz, xz), such that I could plot them with surf:
surf(xy,yz,xz);
I know I could get these three matrices with that function:
[xy yz xz]=sphere(n);
But I basically would like to do that for 3D data (not analytical functions). The sphere is just a particular example :)
How can I do that? so it looks like I need to do an interpolation of the projection of these points on the three normal planes.
Thanks a lot for your help!
0 个评论
回答(1 个)
Walter Roberson
2016-12-8
In a situation such as that, you would generally end up with several values in the z plane for some x, y combinations. In order to surf() you need to decide which of the several values will be your "representative" values. You could choose mean() or median(), but for your purpose you probably instead want either min() or max() depending on the angle of view.
2 个评论
Walter Roberson
2016-12-8
I suggest that you quantize the permitted range of values in each direction, and then convert each 3D coordinate into the corresponding indices. That is, if you were to divide the space up into a 3D grid of cuboids, then for each point in the N x 3, you would want to know the indices of the voxel it would fall into on the grid.
Once the 3D indices are known, you can take them two at a time as indices for accumarray(), setting the default value to NaN, and telling accumarray() to use either @max or @min as the function (depending on the side you want to look from.) For each pair of dimensions, this gives you a 2D grid that tells you, by index, which of the cuboids would be the "first" you would see if you were looking from that side. Use the non-NaN indices to convert to corresponding spatial coordinates in the dimension that you collapsed.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!