How can I project a 3-D sphere onto a 2-D surface?
Show older comments
I got a matrix M with 3 col, n rows. The points created by the matrix are all on a sphere with radius r. In a seperate matrix, let's call it "U", same size. Has either 0's or the exact same row. (indicating parts of the the sphere that have been selected).
So a sphere where nothing has been selected is a 3xn matrix filled with 0's.
I want to project the sphere onto a 2D surface (lets ignore the z, and plot x and y), which gives me a "double" sphere, spanned by points. I used the scatter command to get this result. I shifted half of the circle down in y, so u would get two circles next to eachother.
Now... here's my question:
Can I create a memory efficient 2-D plot, in which the points are translated to "surface parts", in order to better indicate which parts of the sphere has been seen.
The reason for the memory efficient part is due to a constant refresh of the data.
See attached picture for an more visual explanation.
Added question: say I don't cut the circle in half in the middle, but say at 1/3. What would be the most easy way to "fold" the data points outward that would otherwise be obscured by the datapoints that have the same x&y, but different z points.
(see explanation in the left down corner)
Thanks in advance,
5 Comments
matt dash
on 11 Dec 2014
How big is "n"? Have you already solved the problem using "Scatter" and you're looking for something faster? The "memory efficient" is a red herring... you should have no trouble getting this to work just fine even for reasonably large n (i.e. large enough that you wouldn't be able to see the individual points) if you're using the set and get functions instead of deleting and calling scatter again each time you want to refresh. Also, if you don't need different colored/sized points, you'll get better performance using plot or line than using scatter.
luc
on 11 Dec 2014
matt dash
on 11 Dec 2014
Can you post some example code? The solution will depend on how your points are arranged in relation to each other. Do you know which points get connected to which other points in the "web"?
Short answer: you can set x/y/z data to NaN to temporarily make those points disappear. That should get you the speed you need. But i'm not clear on exactly what you're asking about the web and the projection.
luc
on 16 Dec 2014
Accepted Answer
More Answers (1)
Ok... based on your code above, if your question is just how you can quickly update the data to reflect new information about which points are on/off, the answer is that you can use NaNs to turn points off (looks like you already know this) and then set the x/y/z data of the trisurf or surf object to quickly update the sphere. Here is an example... on my computer this gives me about 400 frames per second in 2014b, or 60 frames per second in earlier versions... so this should be fast enough to keep up with the max possible speed of your monitor:
[x,y,z]=sphere(50);
data=[x(:) y(:) z(:)];
figure('renderer','opengl')
axes('xlim',[-1 1],'ylim',[-1 1],'zlim',[-1 1])
L = surface(x,y,z);
view(3)
axis square
n=size(data,1);
tic
for i = 1:1000
npts = randi(n);
idx=false(n,1);
idx(1:npts)=true;
idx=idx(randperm(n));
thisx=x;
thisy=y;
thisz=z;
thisx(idx)=nan;
thisy(idx)=nan;
thisz(idx)=nan;
set(L,'xdata',thisx,'ydata',thisy,'zdata',thisz);
drawnow;
end
t=toc
disp(['Frames per second = ',num2str(1000/t)])
2 Comments
luc
on 11 Dec 2014
matt dash
on 11 Dec 2014
I'm not sure about the 2d vs 3d thing. All matlab axes are 3d, they just appear 2d when you view them from above. So a "2D" plot is identical to a 3D plot with all the z values set to 0. I'm not sure if this really provides any performance benefit.
As far as plotting both halves of the sphere separately, i would just define a plane, and use it in a condition that checks which side of the plane each point of the sphere is. Then have one plot show the points on one side, one plot show the points on the other side. Same idea applies if you had a more complex condition (splitting the sphere into 3 regions etc). I'm still not clear if you want to apply some additional transformation, like a map projection, but in any case I can't help you with that. If you happen to have the mapping toolbox it has many transformations built in.
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!