Clipping Surface Plots When Plotting Large Volumes
27 views (last 30 days)
Show older comments
I am curious if anyone has experienced something like this before. I know that Matlab has various clipping options and I have played with those with no success. I am trying to create some graphics that include a fairly large plotting volume but want to zoom in on small features within that volume. What I am experiencing is once the objects at the extremes of the volumes are far enough apart from the origin of the plot, the plot starts to clip the graphic at the origin that I am interested in looking at, sometlimes clipping it out entirely. I have created a piece of sample code below that demonstrates this issue on my machine. I hope it isn't machine-specific and can be replicated on yours. Any assistance would be much appreciated.
clc; clear all; close all;
set(gcf,'renderer','opengl')
%generate test points
[theta phi]=meshgrid(-180:10:180,-180:10:180);
[a b]=size(theta);
iter=1;
pts(a*b,3)=0;
for i=1:a
for j=1:b
[x y z]=sph2cart(theta(i,j)*pi/180,phi(i,j)*pi/180,1);
pts(iter,:)=[x y z];
iter=iter+1;
end
end
[x y z]=sphere(30);
%define initial figure properties
figure(1)
set(gcf,'OuterPosition',[100 100 500 500],'Color','Black');
figureh=gcf;
axesh=gca;
set(axesh,'Parent',figureh);
set(gca,'Clipping','off','ClippingStyle','rectangle');
set(gcf,'Clipping','off');
xlim([-inf inf]);
ylim([-inf inf]);
zlim([-inf inf]);
axis off;
shading flat;
hold on;
p2=plot3(pts(:,1),pts(:,2),pts(:,3),'r.','Parent',axesh,'Clipping','off');
p1=surf(x,y,z,'EdgeColor','None','FaceColor',[.5 .5 .5],'Parent',axesh,'Clipping','off');
hold off;
light;
material dull;
cameratoolbar;
camproj perspective;
camva(120);
campos('manual');
campos([0.7997 -1.1424 1.0343]);
camtarget([1 0 .5]);
camup([0 0 1]);
% change the scalaar (50) to higher values to see total clipping of sphere
p2.XData=p2.XData*500;
p2.YData=p2.YData*500;
p2.ZData=p2.ZData*500;
drawnow;
5 Comments
Adam Danz
on 21 Sep 2021
Ah... I see. The sphere is expanding outside of the frustum. I'm not at my laptop at the moment but I will reply with an answer tomorrow unless someone else beats me to it.
Answers (1)
Adam Danz
on 22 Sep 2021
TL;DR: When the coordinates for the red-dotted sphere are expanded, the axis limits are updated. The new x-axis limits clip the sphere because the x-coordinate of the camera is within the range of x-coordinates of the sphere.
Solution: Either reset the x-axis limit after exansion or change the camera position.
Furthre reading: Scratchapixel is my go-to site for graphics, projection geometry, etc. This site came in handy for me when I was learning (still am learning) OpenGL.
Illustrating the problem
Generate the base figure before setting camera properties and before expanding the red-dot sphere. I've commented-out irrelevant lines of code and cleaned up some other sections where I've left comments.
% set(gcf,'renderer','opengl')
%generate test points % I REPLACED YOUR FOR-LOOPS
[theta phi]=meshgrid(-180:10:180,-180:10:180);
[x y z]=sph2cart(theta*pi/180,phi*pi/180,1);
pts = [x(:), y(:), z(:)];
[x y z]=sphere(30);
%define initial figure properties
figureh = figure(1); % ADDED FIG HANDLE OUTPUT
set(figureh,'Color','Black'); % REMOVED OUTERPOSITION PROPERTY; USE FIG HANDLE
% figureh=gcf;
% axesh=gca;
% set(axesh,'Parent',figureh);
axesh = axes(figureh); % CLEANER AND SIMPLER THAN ABOVE
set(axesh,'Clipping','off','ClippingStyle','rectangle'); % USE AX HANDLE
set(figureh,'Clipping','off'); % USE FIG HANDLE
% xlim([-inf inf]); % THIS DOES NOTHING
% ylim([-inf inf]); % THIS DOES NOTHING
% zlim([-inf inf]); % THIS DOES NOTHING
axis off;
shading flat;
hold on;
p2=plot3(pts(:,1),pts(:,2),pts(:,3),'r.','Parent',axesh,'Clipping','off');
p1=surf(x,y,z,'EdgeColor','None','FaceColor',[.5 .5 .5],'Parent',axesh,'Clipping','off');
axis equal % ADDED
% hold off;
light;
material dull;
% cameratoolbar;
To illustrate the desired camera position and target, I've added a blue square and yellow cross respectively. I've also added a YZ plane at the camera's x-location to show the camera's position relative to the sphere. This shows the camera's future position and target.
plot3(1, 0, .5, 'y+','LineWidth',2,'MarkerSize', 10)
plot3(0.7997, -1.1424, 1.0343, 'cs','LineWidth',2,'MarkerSize', 10)
patch(0.7997*[1,1,1,1],[-1 -1 1 1], [-1 1, 1 -1], 'w','FaceAlpha', .8)
view(33,33) % to show the other side of the plane
Now move the camera and examine the axis limits before expanding the red sphere.
view(3) % ADDED
camproj perspective;
camva(120);
% campos('manual'); % REDUNDANT
campos([0.7997 -1.1424 1.0343]);
camtarget([1 0 .5]);
camup([0 0 1]);
xl = xlim % = [-1 1]
yl = ylim % = [-1.1424 1]
zl = zlim % = [-1 1.0343]
Now expand the red sphere and examine the axis limits again.
p2.XData=p2.XData*25;
p2.YData=p2.YData*25;
p2.ZData=p2.ZData*25;
% drawnow;
xlim % = [-25 25]
ylim % = [-25 25]
zlim % = [-25 25]
Return the axis limits to their original values before expansion.
xlim(xl)
ylim(yl)
zlim(zl)
Alternatively, move the camera position back a bit,
axesh.CameraPosition(1) = 1;
7 Comments
Adam Danz
on 29 Sep 2021
Edited: Adam Danz
on 29 Sep 2021
What motivated me to change the lower access limit was to eliminate seeing the far side of Earth but I don't know why that changes the near clipping plane.
I've worked with perspective projections in the past but have never thoroughly investigated it's implementation in matlab.
Perhaps these links may be helpful and if you ever get to the bottom of it, I'd love to hear what you find.
See Also
Categories
Find more on Camera Views 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!