Limitation to MATLAB Hidden Line Removal? - matlab

I've asked this question on Mathwork's site so if cross-posting isn't allowed let me know and I'll delete this.
I'm trying to render small objects and large objects together in MATLAB. I'm using the camera commands to restrict my field of view such that I can see both. However, when I do that, the hidden line removal fails on the small objects. I would have thought that hidden line removal would have been done at machine precision for floats, but it appears not. Is this a function of my graphics card or can I work around this?
The code below is the minimum example I could come up with. Plotted on an axis with default limits, the hidden line removal works fine (left) When the axis is set to large extents (compared to the object) the line removal fails (middle). When I make the axis disappear things are fine again (right).
For this example, I can just hide the axis and the output looks correct. But for what I'm actually trying to do, that's not an option. Any suggestions or can someone point me to the proper limit between the smallest and largest objects in a scene that will properly render?
The code to generate the spheres above is below. Thanks in advance!
Images as generated by MATLAB 2018A
clearvars
F1 = figure(1);
clf
set(F1,'color',[1 1 1],'Renderer','opengl'); % have to enable this to clip surfaces behind the camera
for step = [2 1 3] % out of order because the axis in case 2 is trying to hide the first plot
subplot(1,3,step)
view(gca,3);
camproj(gca,'Perspective'); % have to enable this to clip surfaces behind the camera
[Xs,Ys,Zs] = sphere(20);
r = 30e-6;
surf(gca,Xs*r,Ys*r,Zs*r);
axis(gca,'equal');
% three different behaviors, pick a number 1, 2, or 3
switch step
case 1 % this plots the sphere correctly
%axis([-2 2 -2 2 -2 2]);
%axis off
case 2 % this messes up the hidden line removal
axis([-2 2 -2 2 -2 2]);
%axis off
case 3 % removing the axis walls fixes things again
axis([-2 2 -2 2 -2 2]);
axis off
end
% put viewpoint on unit sphere
camera_pos = get(gca,'CameraPosition');
mag_camera_pos = sqrt(sum(camera_pos.^2));
camera_pos = camera_pos / mag_camera_pos;
set(gca,'CameraPosition',camera_pos);
final_angle = 2.5*atand(r/1);
set(gca,'CameraViewAngle',final_angle);
end
drawnow

Related

Plotting interactive lattice in a simulation matlab

I'm running a simulation that describes activity at the front and back of a 2D, square lattice. the front and back are described for example by:
front= [-1 1 -1 0 1 0 1 2 -2 1 ];
back = [ 1 0 0 0 2 0 1 -2 -2 1 ];
each number indicates different activity on the lattice.
I want to plot this interactively so that each value in the lattice will be marked by a different marker and color and the plot will be updated every iteration.
So far I have something like:
% the upper and lower edges of the lattice
figure (1)
hold on
plot(linspace(1,100,10),10*ones(1,10),'k'); %front
plot(linspace(1,100,10),1*ones(1,10),'k'); %back
% the front and back when are equal 0 zero (initial condition)
plot(100*ones(1,10),1:10,'ob','markersize',10); % front
plot(1*ones(1,10),1:10,'ob','markersize',10); % back
xlim([-1 101])
ylim([-1 11])
This marks the initial setup of the system I'm working on, plot it to see what I'm referring to.
now in each iteration I want to view the circles change colors for different values, for example:
figure (1)
ind=find(front==1);
if (isenum(ind)==0)
plot(100*ones(1,length(ind)),ind,'or','markerfacecolor','r');
end
This is done 10 times, for 5 values at the front and 5 at the back, and is quite heavy on the simulation
I wish to find a way that I can span the entire vector front/back on the lattice with "one go" and have different markers assigned to each value. I manage to do it with imagesc, however, I lose the graphics I want to keep while piloting the markers (I wish to add arrows and other stuff later as well). does anyone have any experience with these kind of things?

I don't know hot to plot this nonlp's feasible reagion in matlab

I searched and watched how to plot the 3 dimensions of nonlp program's
but I still don't know how to plot these constraints.
x^2+y^2+z^2=1
x>=2*y
2*y>=3*z
x>=3*z
I programmed like this but it doesn't work and I think this is wrong program for upper constraints.
func1 = #(x,y,z) sqrt(1-x.^2-y.^2);
func2 = #(x,y,z) max(x-2*y,0);
func3 = #(x,z) max(x-3*z,0);
ezsurf(func1,[0 1 0 1]);
hold on;
ezsurf(func2,[0 1 0 1]);
hold on;
ezsurf(func3,[0 1 0 1]);
axis([0 1 0 1 0 1]);
A way of doing that is to work with volumetric data.
The idea is to create a 3D space, with a F value. F will be positive in one side of your main equation and negative in the other.
[X,Y,Z]=meshgrid(-2:0.05:2,-2:0.05:2,-2:0.05:2);
F=X.^2+Y.^2+Z.^2-1;
Then the conditions are applied to this F. If you want to see the object "cut"
then substitute the place where conditions are not met with nan. If you want to see a filled object, then substitute it with a positive number (it may need to be negative in another situation, so check this for other examples.
% Conditions
cond1=X>=2*Y;
cond2=2*Y>=3*Z;
cond3=X>=3*Z;
% If you want the boundaries to show
F1=F;
F1(~cond1)=1;
F1(~cond2)=1;
F1(~cond3)=1;
% If you want the boundaries not to show
F2=F;
F2(~cond1)=NaN;
F2(~cond2)=NaN;
F2(~cond3)=NaN;
Then the idea is to create an isosurface in the zero level (your original function). Here in this code I created 2 plots so you can see the differences between filling and not filling the boundaries. Also some fancy coding in order to get nice figures. However the isosurface and patch parts are relevant to obtain the surface and plot it.
subplot(121)
iso1=isosurface(X,Y,Z,F1,0);
p=patch(iso1);
isonormals(X,Y,Z,F1,p);
set(p,'FaceColor','red','EdgeColor','none');
daspect([1 1 1])
axis equal
camlight
lighting gouraud
subplot(122)
iso2=isosurface(X,Y,Z,F2,0);
p=patch(iso2);
isonormals(X,Y,Z,F2,p);
set(p,'FaceColor','red','EdgeColor','none');
axis equal
daspect([1 1 1])
camlight headlight
lighting gouraud
Result:
Additionally, You can calculate the isosurface of the initial boundary F and plot it with a low alpha value, so you can better understand what piece of the domain you are actually selecting.

Creating annotation boxes for subplots in a for-loop in Matlab

I have the following code in Matlab that runs through a for loop, reads data from a file and plots 9 different figures, that correspond to some particular "channels" in my data, so I decided to annotate them in the for loop.
clear
clc
for i=1:9
subplot(3,3,i);
hold on
x = [4 13]; % from your example
y = ([1 1]); % from your example
y2 = ([-0.4 -0.4]);
H=area(x,y,'LineStyle','none',...
'FaceColor',[1 0.949019610881805 0.866666674613953]);
H1=area(x,y2,'LineStyle','none',...
'FaceColor',[1 0.949019610881805 0.866666674613953]);
% Create textbox
annotation('textbox',...
[0.719849840255583 0.603626943005185 0.176316293929713 0.308290155440411],...
'String',{'FABLIGHT04','Channel',i},...
'FontWeight','bold',...
'FontSize',10,...
'FontName','Geneva',...
'FitBoxToText','off',...
'EdgeColor','none');
axis([0 24 -0.4 1])
set(gca,'XTick',[0:1:24])
set(gca,'YTick',[-0.4:0.2:1])
xlabel('Time (s)');
end
Initially it was giving me 9 different figures and the annotation thing worked fine. But I wanted to be able to tile them onto a subplot for easier comparison.
Since I switched over to using subplot, it does not annotate my figure properly. On opening the editing dock and generating the code, I find that matlab is plotting everything first and then just putting the annotation boxes in the same figure, one on top of the other. Looking at the code it generated, it apparently takes this part of the code:
annotation('textbox',...
[0.719849840255583 0.603626943005185 0.176316293929713 0.308290155440411],...
'String',{'FABLIGHT04','Channel',i},...
'FontWeight','bold',...
'FontSize',10,...
'FontName','Geneva',...
'FitBoxToText','off',...
'EdgeColor','none');
and does it as:
annotation(figure1,'textbox'...)
etc etc
So for all 9 text boxes, it puts them onto the same figure. I tried to do S=subplot(3,3,i) then annotation(S,'textbox') etc etc, I have also tried S(i)=subplot(3,3,i) and then annotation(S,'textbox') etc etc but nothing seems to work.
I have also tried to change the location of the box. I can't seem to figure out how to make it smaller either.
Does anyone know how to have annotation boxes in the right subplot in a for loop?
Thanks
I'm afraid annotation objects are properties of figures and NOT axes, as such its harder to customize the position of each annotation objects because no matter how many subplots you have, they are all part of the same figure and you need to specify their position relatively to the figure coordinate system.
Therefore, you can manually set the position of each text box in your code depending on the subplot it belongs to...
Simple example:
clear
clc
close all
figure('Units','normalized'); %// new figure window
for k = 1:2
str = sprintf('Subplot %d',k);
subplot(1,2,k)
plot(rand(1,10));
%// Customize position here
hAnnot(k) = annotation('textbox', [k*.4-.2 .6 .1 .1],...
'String', str,'FontSize',14);
end
Which looks like this:
Its not very elegant but I'm personally not aware of any other option if you do need to use annotations objects. A less cumbersome alternative would be to use a simple text objects, which are properties of axes and therefore much more friendly to position :)
Hope that helps!

How to avoid curved corners in rectangular patch element in matlab figure?

When I generate a set of rectangular patches in a matlab figure, some of the rectangle edges are rendered curved or clipped rather than sharp, which is unwanted. This depends on the scale, zooming into the image tends to eliminate the effect. I thought this might have to do with an aliasing/compression effect. Curiously, using rectangle the problem seems to go away.
Here is an example of the problem at intermediate magnification (other problems such as dashed borders which shouldn't be there are also evident):
The code is from an answer to another question:
H=Hadamard(48); %# now to row-double the matrix
A=(1+H)/2;
B=(1-H)/2;
C=[A; B]; %# the code below randomly permutes elements within the rows of the matrix
[nRows,nCols] = size(C);
[junk,idx] = sort(rand(nRows,nCols),2); %# convert column indices into linear indices
idx = (idx-1)*nRows + ndgrid(1:nRows,1:nCols); %# rearrange whatever matrix
E = C;
E(:) = E(idx);
[X Y] = find(logical(E));
xl = length(X);
yl = length(Y);
figure, hold on
for ii=1:xl
patch(X(ii) + [0 0 1 1],Y(ii) + [0.15 0.9 0.9 0.1],[1 1 1],'Edgecolor',[1 1 1])
end
axis([0 max(X)+1 0 max(Y)+1])
axis('square')
set(gca,'color',[0 0 0])
set(gca,'XTickLabel',[],'YTickLabel',[],'XTick',[],'YTick',[])
My questions are:
(1) Is it possible (and how) to get rid of the curved corners and other glitches of patch objects seen in the example shown, at low to intermediate degrees of magnification used to display the entire figure on screen.
(2) Most important is to be able to generate an image file (jpg, png, pdf...) which lacks the curved corners. All formats I looked into appear to conserve the unwanted effect. Answering 2 makes answering (1) essentially unimportant, and answering (1) presumably solves (2).
Edit
Since the problem goes away when rectangle is used, this appears to be a problem with the matlab rendering engine? Note: the example was generated with R14 but the OP of the question linked to had similar problems (matlab version unknown).
I went through the various lighting and edge representation options available for patch objects but no improvement was observed.
The question is a likely repeat, for instance a similar questions was asked here.
The answer appears to be to avoid explicit use of patch when drawing rectangles. Use either fill or just rectangle instead. The following ways of generating the figure provide nearly equivalent results, as far as I could tell:
load had.mat % <-- load the data containing the matrix of interest in array E
[X Y] = find(logical(E));
xl = length(X);
yl = length(Y);
figure, hold on
for ii=1:xl
rectangle('Position',[X(ii) Y(ii)+.2 1 0.8],'facecolor',[1 1 1],'edgecolor',[1 1 1])
% fill([X(ii) X(ii) X(ii)+1 X(ii)+1], [Y(ii)+0.2 Y(ii)+0.8 Y(ii)+0.8 Y(ii)+0.2],[1 1 1],'edgecolor',[1 1 1],'marker','.','markersize',1)
end
set(gca,'color',[0 0 0])
set(gca,'XTickLabel',[],'YTickLabel',[],'XTick',[],'YTick',[])
set(gcf,'Renderer','zbuffer')

How to plot arrow with data coordinates in Matlab?

I know there is a function named annotation can plot arrows or double arrows. But annotation can only plot in normalized unit. For example:
annotation('arrows',[x1 x2],[y1 y2])
Here, [x1, x2] should be a ratio number less than one.
So, my question is how can I plot arrows with a true value rather than a normalized value?
I wonder if there is any other function can approach this or is there any function I can get the axis value of the figure so that I can adjust the true value into a normalized value.
For the positioning of annotations, Matlab offers the function dsxy2figxy to convert data space points to normalized space coordinates. However, for whatever reasons, the function is not included in the Matlab distribution and has to be "created" first.
Copy the following line into the command window and execute it to open the function in your editor.
edit(fullfile(docroot,'techdoc','creating_plots','examples','dsxy2figxy.m'))
To use the function dsxy2figxy save it somewhere in your matlab search path.
Please find the full instructions for the function dsxy2figxy at matlab-central: http://www.mathworks.de/help/techdoc/creating_plots/bquk5ia-1.html
Even though annotation uses normalized as default units, you can associate these objects to the current axes (gca) and use data units for setting X and Y properties.
Here is an example of plotting a single arrow.
plot(1:10);
ha = annotation('arrow'); % store the arrow information in ha
ha.Parent = gca; % associate the arrow the the current axes
ha.X = [5.5 5.5]; % the location in data units
ha.Y = [2 8];
ha.LineWidth = 3; % make the arrow bolder for the picture
ha.HeadWidth = 30;
ha.HeadLength = 30;
For anyone who comes across this topic looking to draw arrows in "data space" rather than in units relative to the figure and/or axes, I highly recommend arrow.m from the file exchange.
I've just discovered this method, since I don't want to have to bother with normalised units. Use the latex interpreter:
figure
plot([1:5],[1:5]*3,'.-')
%// Say I want to put an arrow pointing to the location, [3 9]
text(2.94,8.3,'\uparrow','fontsize',20)
text(2.8,7.8,'point [3,9]')
To make the arrow longer, use a larger fontsize.
Pros
Easier, faster and quicker than using normalised units
Don't need to install any functions (good for us lazy people..)
making use of the LaTeX interpreter, there is a whole range of arrows (up, down, left, right and other angles (see Symbol list)
Cons
Definitely needs trial and error/tweaking to get the correct location of the arrow head relative to the POI.
There is limited control over the length of the arrow
Some latex commands aren't understood by the interpreter (boo).
If I remember correctly you need to calculate the position of the axes in relation to the figure.
it should go like:
%% example plot
clf
plot(rand(5,2)*5)
%% get info specific to the axes you plan to plot into
set(gcf,'Units','normalized')
set(gca,'Units','normalized')
ax = axis;
ap = get(gca,'Position')
%% annotation from 1,2 to 3,4
xo = [1,3];
yo = [2,4];
xp = (xo-ax(1))/(ax(2)-ax(1))*ap(3)+ap(1);
yp = (yo-ax(3))/(ax(4)-ax(3))*ap(4)+ap(2);
ah=annotation('arrow',xp,yp,'Color','r');
Note Fixed offset in original calculation - ap(3),ap(4) are width and height of gca, not corner positions
After creating the annotation object you should set the property Units to an absolute one. Example:
arrowObj = annotation('arrow', [0.1 0.1], [0.5 0.5]);
set(arrowObj, 'Units', 'centimeters');
set(arrowObj, 'Position', [1 1 3 5]);
One approach would be to define an arrowhead in the axis units:
Ax=[0 -0.003 0.003 0]; % (Ax,Ay) form an upward pointing arrowhead.
Ay=[0.01 0.0060 0.0060 0.01];
Ax=Ax-mean(Ax); % center it on zero
Ay=Ay-mean(Ay);
Then at desired arrowhead index in on a curve vv, compute
x1=vv(in,1); y1=vv(in,2);
x2=vv(in+1,1); y2=vv(in+1,2);
u=x2-x1;
v=y2-y1;
th=-pi/2+atan2(v,u);
R=[cos(th) -sin(th); sin(th) cos(th)]; % Rotation matrix for local slope of vv.
A=R*[Ax;Ay]; % Rotate the arrowhead.
patch(x1+A(1,:),y1+A(2,:),'r','LineWidth',0.01) % plot rotated arrowhead at (x1,y1).
plot(x1+A(1,:),y1+A(2,:),'r','LineWidth',0.01) % Kludge to make boundary red too (I'm sure there is a more elegant way).
Worked for me, for my particular circumstances.
You can use the 'arrow' component in the (well-documented) DaVinci Draw toolbox (full disclosure: I wrote/sell the toolbox, though arrows are free).
Example syntax and example output are below.
davinci( 'arrow', 'X', [0 10], 'Y', [0 2], <plus-lots-of-options> )