ParaView Calculator atan2 - paraview

I'm currently trying to convert given Cartesian coordinates (x,y,z) to spherical coordinates (r, theta, phi) using the ParaView Calculator-Filter, where theta is the polar angle and phi the azimuthal angle.
This I want to do over a domain of a quarter-sphere:
(r in [r_inn, r_out], theta in [0, pi], phi in [0, 2pi].
So far I defined the following result variables that give the expected results:
r = sqrt(coordsX^2 + coordsY^2 + coordsZ^2)
theta = acos(coordsZ/r)
For the azimuthal vector I'm aware that I would have to take care of the quadrant of (x,y) when using
phi = atan(y/x).
This usually is achieved using an extra function like atan2 in C. Such a function does not seem to be supplied by the Calculator Filter or the Python Calculator Filter.
Is there any easy way to implementing something like the atan2 using the graphical interface?
Any comments are much appreciated, thanks!
UPDATE:
After Neil Twist pointed out, that in the Python Calculator the inverse tangent function can be called as arctan2(y, x), I'm now facing the problem that I can't access the coordinates of a cell via the variables coordsX/Y/Z, that are available in the simple Calculator filter.
Now, the question is: How can I access the cell coordinates in the Python calculator?

You can use the numpy extensions of the Python Calculator in ParaView, but numpy have called the function arctan2 rather than atan2.
There are numpy docs for trigonometric functions, but annoyingly you can't use all the functions directly, for example you can do arctan2(x1, x2), but you can't do pi and have to use numpy.pi.
For context, there are PythonCalculator docs too.
To access the coordsX and coordsY is a bit tricky, but can be achieved using the points variable. This is actually an array of all the points, each of which is an array of x, y and z coords.
To use the co-ordinates you need to extract them like so:
[point[0] for point in points]
[point[1] for point in points]
[point[2] for point in points]
So to use the arctan function with Y and X coords, you could do the following:
arctan2([point[1] for point in points], [point[0] for point in points])
Update:
After a little more investigation, there may be a slightly nicer way to get coordsX/Y/Z:
points[:,0]
points[:,1]
points[:,2]
giving
arctan2(points[:,1], points[:,0])
Another helpful reference is the numpy_interface algorithms.

Related

Graph a 3D plot given an equation with three variables

I have an equation:
b*cos(alpha) - a*sin(alpha) + b*cos(betta)-a*sin(betta) - b*cos(gamma) + a*sin(gamma) = 0
I want to create a 3D plot of this in Matlab with alpha vs betta vs gamma ( x - y - z ). I don't understand how to represent the equation so it could be plotted. How can I do this?
It is possible to assume that a = b = 1;
You need to understand what you have at hand. What does the equation show? Which form is it written? How does MATLAB plot different type of equations?
Fist, lets try to understand what type of function you have. It has 3 variables, but it equals to zero. A 3 variable equation generally defines a surface in 3D. In your case, this surface is described on its implicit form.
Now, if we look at the documentation of MATLAB, surfaces are generally plotted with surf, but surf needs 3 inputs (x,y,z) and you can not easily isolate your 3 variables.
Ah! but luckily, there is a thing called a search engine, that can gives us hints. Now that we know what kind of equation we have, we might as well use Google (or your favorite search engine) and type "implicit surface plot MATLAB", and that search will return a function called fimplicit3.
I think it seems to work:

Matlab : Intersect point of curves

Say for example I have data which forms the parabolic curve y=x^2, and I want to read off the x value for a given y value. How do I go about doing this in MATLAB?
If it were a straight line, I could just use the equation of the line of best fit to calculate easily, however I can't do this with a curved line. If I can't find a solution, I'll solve for roots
Thanks in advance.
If all data are arrays (not analytical expressions), I usually do that finding minimal absolute error
x=some_array;
[~,ind]=min(abs(x.^2-y0))
Here y0 is a given y value
If your data are represented by a function, you can use fsolve:
function y = myfun(x)
y=x^2-y0
[x,fval] = fsolve(#myfun,x0,options)
For symbolic computations, one can use solve
syms x
solve(x^2 - y0)
Assuming your two curves are just two vectors of data, I would suggest you use Fast and Robust Curve Intersections from the File Exchange. See also these two similar questions: how to find intersection points when lines are created from an array and Finding where plots may cross with octave / matlab.

Intersection between curve from points and a line

I'm new in matlab and I'm trying to find the intersection between a curve (from points) and a line.
I've some points and I've plot the interpolation between this points. Now I want to find the intersection between the interpolation (xi,yi) curve and another line.
x = [94.8;84.4;63.1;49.4;40.6;33.8;23.2;20.1;10.2;9.2;7.9];
y = [0; 11.4;29.7;41.6;47.5;50.1;52.9;50.6;32.2;28.1;0];
xi=94.8:-0.1:7.9;
yi=interp1(x,y,xi,'spline');
plot(x,y,'*');
hold on
plot(xi,yi);
I've researched but everything I've found needs a function. I already tried to convert the curve to a function using polyfit but the fit is not good enought.
It is posible to do this in matlab?
Thanks.
Basically, the error message ask you to input a function handle (similar to function pointer in other languages). It's not necessary to convert it into something that matches a mathematical definition of a function (e.g. polynom):
f=#(xi)(interp1(x,y,xi,'spline'))
This can be evaluated at every xi.
Usage like every other function:
f(1)
f(1:3)

Outline your hand with splines (Matlab)

I'm trying to write a script so that one can put his hand on the screen, click a few points with ginput, and have matlab generate an outline of the persons hand using splines. However, I'm quite unsure how you can have splines connect points that result from your clicks, as they of course are described by some sort of parametrization. How can you use the spline command built into matlab when the points aren't supposed to be connected 'from left to right'?
The code I have so far is not much, it just makes a box and lets you click some points
FigHandle = figure('Position', [15,15, 1500, 1500]);
rectangle('Position',[0,0,40,40])
daspect([1,1,1])
[x,y] = ginput;
So I suppose my question is really what to do with x and y so that you can spline them in such a way that they are connected 'chronologically'. (And, in the end, connecting the last one to the first one)
look into function cscvn
curve = cscvn(points)
returns a parametric variational, or natural, cubic spline curve (in ppform) passing through the given sequence points(:j), j = 1:end.
An excellent example here:
http://www.mathworks.com/help/curvefit/examples/constructing-spline-curves-in-2d-and-3d.html
I've found an alternative for using the cscvn function.
Using a semi-arclength parametrisation, I can create the spline from the arrays x and y as follows:
diffx = diff(x);
diffy = diff(y);
t = zeros(1,length(x)-1);
for n = 1:length(x)-1
t(n+1) = t(n) + sqrt(diffx(n).^2+diffy(n).^2);
end
tj = linspace(t(1),t(end),300);
xj = interp1(t,x,tj,'spline');
yj = interp1(t,y,tj,'spline');
plot(x,y,'b.',xj,yj,'r-')
This creates pretty decent outlines.
What this does is use the fact that a curve in the plane can be approximated by connecting a finite number of points on the curve using line segments to create a polygonal path. Using this we can parametrize the points (x,y) in terms of t. As we only have a few points to create t from, we create more by adding linearly spaced points in between. Using the function interp1, we then find the intermediate values of x and y that correspond to these linearly spaced t, ti.
Here is an example of how to do it using linear interpolation: Interpolating trajectory from unsorted array of 2D points where order matters. This should get you to the same result as plot(x,y).
The idea in that post is to loop through each consecutive pair of points and interpolate between just those points. You might be able to adapt this to work with splines, you need to give it 4 points each time though which could cause problems since they could double back.
To connect the start and end though just do this before interpolating:
x(end+1) = x(1);
y(end+1) = y(1);

Find minimum distance between a point and a curve in MATLAB

I would like to use a MATLAB function to find the minimum length between a point and a curve? The curve is described by a complicated function that is not quite smooth. So I hope to use an existing tool of matlab to compute this. Do you happen to know one?
When someone says "its complicated" the answer is always complicated too, since I never know exactly what you have. So I'll describe some basic ideas.
If the curve is a known nonlinear function, then use the symbolic toolbox to start with. For example, consider the function y=x^3-3*x+5, and the point (x0,y0) =(4,3) in the x,y plane.
Write down the square of the distance. Euclidean distance is easy to write.
(x - x0)^2 + (y - y0)^2 = (x - 4)^2 + (x^3 - 3*x + 5 - 3)^2
So, in MATLAB, I'll do this partly with the symbolic toolbox. The minimal distance must lie at a root of the first derivative.
sym x
distpoly = (x - 4)^2 + (x^3 - 3*x + 5 - 3)^2;
r = roots(diff(distpoly))
r =
-1.9126
-1.2035
1.4629
0.82664 + 0.55369i
0.82664 - 0.55369i
I'm not interested in the complex roots.
r(imag(r) ~= 0) = []
r =
-1.9126
-1.2035
1.4629
Which one is a minimzer of the distance squared?
subs(P,r(1))
ans =
35.5086
subs(P,r(2))
ans =
42.0327
subs(P,r(3))
ans =
6.9875
That is the square of the distance, here minimized by the last root in the list. Given that minimal location for x, of course we can find y by substitution into the expression for y(x)=x^3-3*x+5.
subs('x^3-3*x+5',r(3))
ans =
3.7419
So it is fairly easy if the curve can be written in a simple functional form as above. For a curve that is known only from a set of points in the plane, you can use my distance2curve utility. It can find the point on a space curve spline interpolant in n-dimensions that is closest to a given point.
For other curves, say an ellipse, the solution is perhaps most easily solved by converting to polar coordinates, where the ellipse is easily written in parametric form as a function of polar angle. Once that is done, write the distance as I did before, and then solve for a root of the derivative.
A difficult case to solve is where the function is described as not quite smooth. Is this noise or is it a non-differentiable curve? For example, a cubic spline is "not quite smooth" at some level. A piecewise linear function is even less smooth at the breaks. If you actually just have a set of data points that have a bit of noise in them, you must decide whether to smooth out the noise or not. Do you wish to essentially find the closest point on a smoothed approximation, or are you looking for the closest point on an interpolated curve?
For a list of data points, if your goal is to not do any smoothing, then a good choice is again my distance2curve utility, using linear interpolation. If you wanted to do the computation yourself, if you have enough data points then you could find a good approximation by simply choosing the closest data point itself, but that may be a poor approximation if your data is not very closely spaced.
If your problem does not lie in one of these classes, you can still often solve it using a variety of methods, but I'd need to know more specifics about the problem to be of more help.
There's two ways you could go about this.
The easy way that will work if your curve is reasonably smooth and you don't need too high precision is to evaluate your curve at a dense number of points and simply find the minimum distance:
t = (0:0.1:100)';
minDistance = sqrt( min( sum( bxsfun(#minus, [x(t),y(t)], yourPoint).^2,2)));
The harder way is to minimize a function of t (or x) that describes the distance
distance = #(t)sum( (yourPoint - [x(t),y(t)]).^2 );
%# you can use the minimum distance from above as a decent starting guess
tAtMin = fminsearch(distance,minDistance);
minDistanceFitte = distance(tAtMin);