Clipping Surface Plots When Plotting Large Volumes

25 次查看(过去 30 天)
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 个评论
Adam Danz
Adam Danz 2021-9-21
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
Adam 2021-9-22
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.
https://en.wikipedia.org/wiki/Psychtoolbox_for_MATLAB

请先登录,再进行评论。

回答(1 个)

Adam Danz
Adam Danz 2021-9-22
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 个评论
Adam
Adam 2021-9-29
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 2021-9-29
编辑:Adam Danz 2021-9-29
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Camera Views 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by