Clipping Surface Plots When Plotting Large Volumes

21 views (last 30 days)
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;
%generate test points
[theta phi]=meshgrid(-180:10:180,-180:10:180);
[a b]=size(theta);
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];
[x y z]=sphere(30);
%define initial figure properties
set(gcf,'OuterPosition',[100 100 500 500],'Color','Black');
xlim([-inf inf]);
ylim([-inf inf]);
zlim([-inf inf]);
axis off;
shading flat;
hold on;
p1=surf(x,y,z,'EdgeColor','None','FaceColor',[.5 .5 .5],'Parent',axesh,'Clipping','off');
hold off;
material dull;
camproj perspective;
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
Adam Danz
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.
Adam on 22 Sep 2021
Copy. Thank you for taking the time to look again. It would probably help a lot if I knew enough about renderring to accurately describe the issue! :-)
It may be beneficial to see if there is any way to issue camera settings directly to the opengl via Matlab. I have found a toolbox that claims to be able to do so and am digging through it now.

Sign in to comment.

Answers (1)

Adam Danz
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
% 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;
p1=surf(x,y,z,'EdgeColor','None','FaceColor',[.5 .5 .5],'Parent',axesh,'Clipping','off');
axis equal % ADDED
% hold off;
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;
% 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.
% drawnow;
xlim % = [-25 25]
ylim % = [-25 25]
zlim % = [-25 25]
Return the axis limits to their original values before expansion.
Alternatively, move the camera position back a bit,
axesh.CameraPosition(1) = 1;
Adam on 29 Sep 2021
Okay. I am able to change the xlimits as you show by why does changing the -x side limit help with clipping on the +x side of the sphere? It is nice that it works but that doesn't make any sense to me at all.
Simlarly, if I execute the following at the end of the script, I would think that it would clip much of the globe but it doesn't.
axesh.XLim(1) = 7000; % SPECIFY AXIS LIMS
axesh.XLim(2) = 9999; % SPECIFY AXIS LIMS
Adam Danz
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.

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!