3D mouse input in matlab/simulink - matlab

I want to take input coordinates through mouse in Matlab oR Simulink, there is no built-in facility in Matlab of input of 3D coordinate through mouse device, however the buit-in function ginput can only store 2D coordinates of mouse, is there is any possibility in MATLAB/SIMULINK to input 3D coordinates through mouse device?

If I understand you correctly, you want to get the coordinates (in data space) of the mouse click, when the plot is 3D. That is, you click somewhere in the plot and it returns your current position. I have actually tackled this exact problem before.
The main difficulty with this task--and the other posters have alluded to it--is that you are clicking on a 2D screen. Thus, you cannot specify 3 independent positions on a 2D screen uniquely. Rather, clicking on the screen defines a line segment, normal to the plane of the screen, and any of the 3D points along this line are equally valid. Do you understand why this is the case?
To demonstrate, try this short example in Matlab:
surf(peaks); %draw a sample plot
keydown = 2;
while keydown ~= 0,
disp('Click some where on the figure');
keydown = waitforbuttonpress;
end
currPt = get(gca,'CurrentPoint');
disp(currPt);
You'll observe that currPt is a 2x3 matrix. This defines the start and end points of this line. Let's plot this line now:
hold on;
plot3( currPt(:,1), currPt(:,2), currPt(:,3), 'k-', 'LineWidth', 2);
view(-19,46); %rotate to view this line
So the question is: how to define which point along this line you want to select? Well the answer depends on what type of data you have in the first place. If you have point data, choosing one of your vertices exactly can be tricky, and you might need to do some post-processing of your data (for example, to calculate the closest point in your dataset to the currPt line). If you have patch or surface data (such as this example), this is just the intersection of a line and a plane.
There are some tools on the File Exchange to get 3D points for various datasets. One that I just found is: http://www.mathworks.com/matlabcentral/fileexchange/7594-click3dpoint

Related

Limit impoly clicks and convert it to a rectangle - Matlab

I am using impoly as in the script below and I have two questions:
Can I limit the to points clicked (e.g., 5) and close it automatically?
Is there a way convert the impoly to imrect like in the attached image (red box)?
Script:
clc;
clear;
figure, imshow('pout.tif');
hpoly = impoly(gca);
From the documentation of impoly, I don't think it is directly possible. For such custom behaviour, you should probably write your own point picking function.
Several matlab functio ncan help you in this direction.
[x,y] = ginput(n) to pick points
impoint(hparent,x, y) to draw draggable points,
line to draw a the line between points, and the rectangle bounding box.
impoint has a 'PositionConstraintFcn' parameter, that will call a function of yours when the point is moved. You can use it to update the lines draw when the points are moved.
I suggest you to have a main function that handles the point picking (constraining the number of points, etc...), and a "display" function, that calculate the bounding box, draw the lines between points, that you can call when a point is added (in the main function), or when a point is moved (with the 'PositionConstraintFcn'parameter).

impoly only approximately correct on log-scale axes

When determining points within polygons using MATLAB's inpolygon function, I find that the results are exactly correct for polygons drawn on linear axes but only approximately correct for polygons drawn on log-scale axes. Although my suspicions lean in favor of a MATLAB bug, it's possible I've overlooked something.
The following code reproduces the issue I have been experiencing with other data. The results are shown in the following image (the bottom set of panels are zoomed views of the top panels). One can appreciate that there are unlabeled points inside the polygon and labeled points outside the polygon, neither of which should occur, in the case of a polygon drawn on log-scale axes (right). In contrast, the polygon test is exact for polygons drawn on linear axes (left).
n=2E4;
x(:,1)=rand(n,1); y(:,1)=rand(n,1);
x(:,2)=lognrnd(0.5,0.25,n,1); y(:,2)=lognrnd(0.5,0.25,n,1);
for m=1:2
subplot(1,2,m);
scatter(x(:,m),y(:,m),'.'); hold on;
if(m==2)
set(gca,'xscale','log'); set(gca,'yscale','log');
end
p=impoly(gca);
pc=getPosition(p);
in=inpolygon(x(:,m),y(:,m),pc(:,1),pc(:,2));
scatter(x(in,m),y(in,m),20);
end
I think you missed something: A line in normal scale is not a line in log scale. Your polygons are not properly drawn in the log scale, as you draw 2 points and put them together with a straight line.
Look at the real polygon in log space:
close all
clear
n=2e4;
x(:,1)=rand(n,1); y(:,1)=rand(n,1);
x(:,2)=lognrnd(0.5,0.25,n,1); y(:,2)=lognrnd(0.5,0.25,n,1);
for m=1:2
subplot(1,2,m);
scatter(x(:,m),y(:,m),'.'); hold on;
if(m==2)
set(gca,'xscale','log'); set(gca,'yscale','log');
end
p=impoly(gca);
pc=getPosition(p);
% plot polygon
hold on
for ii=1:size(pc,1)-1
plot(linspace(pc(ii,1),pc(ii+1,1),100),linspace(pc(ii,2),pc(ii+1,2),100),'g')
end
plot(linspace(pc(end,1),pc(1,1),100),linspace(pc(end,2),pc(1,2),100),'g')
in=inpolygon(x(:,m),y(:,m),pc(:,1),pc(:,2));
scatter(x(in,m),y(in,m),20);
end
Look at this zoomed in result (click to enlarge):
This happens because the polygon is defined in euclidean space, and it is defined as points linked by lines. If you want to work in log space, things may get complicated. One way to numerically approximate it is the inverse of what I did for plotting. Create dense enough sampled straight line on log space, convert it to linear space, and define a high vertex polygon with the resulting points. Then use inpolygon.

Moving point plot for 2 variable function Matlab

For the function z= 10+pow((x-2),2)+pow((y+5),2), I want to represent in a 3D plot every value of z for each (x,y) pair , with a display delay before each new value of z (like a moving point).
The values for x,y are read from files.
I tried to modify Jacobs code from Creating a point moving along a graph in MATLAB, but I couldn't get it to work for my 2 variable function.
I would like a general solution, because I will also need this plotting for a N-variable function.
Have a look at the following question and see if it answers yours:
animate plot / trajectory in matlab / octave

pca in matlab - 2D curve stretching

I have N 3D observations taken from an optical motion capture system in XYZ form.
The motion that was captured was just a simple circle arc, derived from a rigid body with fixed axis of rotation.
I used the princomp function in matlab to get all marker points on the same plane i.e. the plane on which the motion has been done.
(See a pic representing 3D data on the plane that was found, below)
What i want to do after the previous step is to look the fitted data on the plane that was found and get the curve of the captured motion in 2D.
In the princomp how to, it is said that
The first two coordinates of the principal component scores give the
projection of each point onto the plane, in the coordinate system of
the plane.
(from "Fitting an Orthogonal Regression Using Principal Components Analysis" article on mathworks help site)
So i thought that if i just plot those pc scores -plot(score(:,1),score(:,2))- i'll get the motion curve. Instead what i got is this.
(See a pic representing curve data in 2D derived from pc scores, below)
The 2d curve seems stretched and nonlinear (different y values for same x values) when it shouldn't be. The curve that i am looking for, should be interpolated by just using simple polynomial (polyfit) or circle fit in matlab.
Is this happening because the plane that was found looks like rhombus relative to the original coordinate system and the pc axes are rotated with respect to the basis of plane in such way that produce this stretch?
Then i thought that, this is happening because of the different coordinate systems of optical system and Matlab. Optical system's (ie cameras) co.sys. is XZY oriented and Matlab's default (i think) co.sys is XYZ oriented. I transformed my data to correspond to Matlab's co.sys through a rotation matrix, run again princomp but i got the same stretch in the 2D curve (the new curve just had different orientation now).
Somewhere else i read that
Principal Components Analysis chooses the first PCA axis as that line
that goes through the centroid, but also minimizes the square of the
distance of each point to that line. Thus, in some sense, the line is
as close to all of the data as possible. Equivalently, the line goes
through the maximum variation in the data. The second PCA axis also
must go through the centroid, and also goes through the maximum
variation in the data, but with a certain constraint: It must be
completely uncorrelated (i.e. at right angles, or "orthogonal") to PCA
axis 1.
I know that i am missing something but i have a problem understanding why i get a stretched curve. What i have to do so i can get the curve right?
Thanks in advance.
EDIT: Here is a sample data file (3 columns XYZ coords for 2 markers)
w w w.sendspace.com/file/2hiezc

Region of interest and Data vertices (3D) , matlab

i want to have a simple function similar to autocad that allows me to select all lines in 3D with mouse selection.
in fig I just want to get (x,y,z) of all vetices under selection. Selection function should be just like autocad.
1-mouse click on a line
2- area selection
With cursor info I get only a single point (x,y,z) as shown in figure. It would be nice if I can get all points inside rectangle made by mouse (region of interest can be polygon).
I think the function may look similar to
[BW, xi, yi] = roipoly(...)
in 3D
[ xi, yi,zi] = roipoly_new(...)
To solve this in general you would need something like the following:
[ xi, yi,zi] = roipoly_new(...)
Determine point of view, POV.
Project the ROI outwards from the POV, and test for intersections with all objects with known vertices. This is a ray tracing algorithm, but is analytic for linear problems, so it shouldn't be too hard to implement. First result on matlab central gives this:
http://www.mathworks.com/matlabcentral/fileexchange/authors/30179
Once you know which objects intersect your rays, the world's your oyster.