How to create 2D vector flow field from components in 2 different .vtk files in Paraview? - paraview

I am rather new to Paraview and have been reading a bit about vector plotting techniques to indicate flow direction and velocity magnitudes. I am quite stuck, however, with my 2 files.
These are both .vtk files of the magnitude of the x and y components of a surface velocity field of a glacier. What I would like in the end is just a field of arrows which point in the direction of the flow and whose size correlates to the velocity magnitude. I have read about stream tracer but it stays greyed out for me. I wonder if anybody can help me or at least give me some guidance about how this can be done. Thanks!
x component: veloc_x.vtk - https://drive.google.com/file/d/1VpFKe_my_Ep0rt80CIcw4B8xNi3AwiSE/view?usp=sharing
y component: veloc_y.vtk - https://drive.google.com/file/d/1eP4FgSZMO-bEZQJzKSbLuTl90zxbxlsh/view?usp=sharing

The steps are as following:
load both files
select them, use Append Attributes filter
Merge Vector Components to put X and Y in a vector.
You may want to use Calculator to create a Z array containing only 0 before step 3. because Merge Vector expect 3 components.
Then use the Glyph representation to setup the arrows in the vector field.

Related

Moving least squares fitting for point displacements having issues

Explanation of the problem:
I have points with (x,y,z) coordinates at two+ distinct times. For convenience, they can be imagined as irregularly spaced points along the surface of an inverted paraboloid.
There is some minimal thickness to the paraboloid. The paraboloid changes shape slightly as time proceeds (like a balloon inflating) and when it does so, all of the points move.
By substracting the coordinates at time2 - time1, I can get the displacement vectors at each point.
It is important to note (and I suspect this might be the source of the problem) that at the first time point, the x and y coordinates range from 0 to 2000, and the z coordinates are all within a narrower range - say 350 to 450. During the deformation, each point has an x component of displacement, y component, and z component.
The x and y components are small (~50 at most), while the z component is the largest (goes up to 400 near the center, much less near the edges).
Using weighted moving least squares at the location of each point, I am trying to fit the components of displacements to a second degree polynomial surface in terms of the original x,y,z coordinates of the point: eg.
x component of
displacement = ax^2 + bxy + cx + dy^2.. + hz^2 + iz + j
I use the lsqr function in MATLAB,like so, looping through each point for each time interval:
Ux = displacements{k,1}(:,1);
Cx = lsqr((adjust_B_matrix'*W*adjust_B_matrix),(adjust_B_matrix'*W*Ux),1e-7,10000);
W is the weight matrix, and adjust_B_matrix is the matrix of all (x,y,z) coordinates at time 1, shifted so that they're all centered around the point at which I'm trying to fit the function.
What is going wrong?
It's just not working -- once I have the functions, they're re-centered around the actual coordinates of the points.
But once I plot the resulting points (initial pointx + displacementx, initial pointy + displacementy, initial pointz + displacementz) by plugging in the coordinates at time 1 into the now-discovered functions, it just spits out a surface that looks just like the surface at time 1.
What might be going wrong? Things I have tried:
It's not an issue with the code itself- I generated 'fake' data using a grid of points and it worked perfectly. The predicted locations were superimposed with the actual coordinates and I was able to get back the function I started with. But in my trial example, I used x,y,z from 0 to 5, evenly spaced.
Global fitting works (but I need local fitting...).
I tried MATLAB's curve fitting toolbox and just tried to fit one of the displacements to only x and y coordinates, globally. It worked perfectly.
I think I shouldn't have a singular matrix issue because I use a large radius (around 75-80) points in the calculations, somewhat dispersed in 3D space.
Suspicions:
I think it has to do with the uneven distribution of initial (x,y,z) coordinates, but I don't know why or how to fix the issue, or even what method I can use.
If you read this far, thank you so much. Any advice would be greatly appreciated.
Figure for reference:
green = predicted points at time 2. Overlapping mostly with red, the actual coordinates of the points at time 1.
blue is the correct coordinates of points at time 2 (this is where the green ones should be if things were working).
image
Updated link for files:
http://a.tmp.ninja/eWfkNmFZyTFk.zip
Contents - code, sample data (please load the .mat files).
I can't actually access the code you posted, so here's some general suggestions.
It does look like the curve fitting toolbox has tools that do exactly what you are looking for, checkout the bottom of this page: https://www.mathworks.com/help/curvefit/polynomial.html#bt9ykh.
It looks like for whatever your learned function for the displacement is just very small or zero everywhere. I suspect the issue is just a minor typo/error on your part somewhere in your pipeline, possibly translating what you have to work with the fit function will reveal the issue.
This really shouldn't be the issue, but in the future if you had much more unbalanced data you could normalize it all before fitting (x_norm = (x - x_mu)/x_std).
Also, I don't think this is your problem either, but you can check if your matrix is close to singular by checking the condition number using the cord() function. So you could check cond(adjust_B_matrix'Wadjust_B_matrix). Second, If you check the documentation for lsqr there is an option to get a debug return flag, that is worth checking too.

MATLAB STL 3D modelling from IMU Data

I am trying to upload an STL file to MATLAB and be able to manipulate it but can't find the best way to do it.
What I am trying to do is import an STL a file of a hand tool and be able to rotate the 3D image by giving it roll, pitch and yaw angles. The whole system will involve a live read out from an IMU which calculates these angles (going to use a 9 axis IMU - 9250 and hope to incorporate space movement into this but that's progress for another day) which will feed into a function which alters the orientation of the model made from the STL to show in real time how the body is moving. Its important to note the body is fixed so no points can move relative to each other (simplifying the problem).
Currently I have not got far but have modeled the STL fixed in space:
model = createpde(3);
importGeometry(model,'Test_model.stl');
pdegplot(model);
This will plot the STL file. The model is made up of a certain number of faces and vertices which can be plotted but I cannot see a way of manipulating these. I figure that there should be some way of converting this to a 3D matrix of points in x,y,z which I can mulitply by a rotation vector to give a new position rotated by the three angles.
Rx = rotx(psi);
Ry = roty(theta);
Rz = rotz(phi);
R = Rx*Ry*Rz;
Then multiply the model by this and update the plot.
I will also need a way of offsetting all points by certain values to be able to change the point of rotation (where the IMU is placed). I figure once I get the coordinates in a matrix then I can offset them all by certain values in each direction x, y and z.
Can anyone help with this, I have been looking for similar projects but I have not been able to find anything with good code explanations as of yet. The way I am proposing is only my idea, if there is an easier method then please say. Thanks!
I do not have comment privileges so this may not seem like a complete answer.
I've done exactly this type of thin in MATLAB for other research but had to write my own data parsers as I do not have any tool boxes, or importGeometry() didn't exist at the time. The STL is structured as a list of triangles each with a normal and three vertices. I'd ask you, after importing STL what is the data format? An array of positions, a struct or object? Also, what s/w was used to make it. The gmsh format is easier to work with as it gives you a reduced list of points and lists of connections between them based on what simplex contains the points.
If the output of importGeometry is a struct with the full data set then you will have repeated data and need to (1) parse the struct, (2) delete duplicates, (3) stack the results in a 3-by-N or N-by-3 matrix, then operate on this result with the rotation matrix and update plots.
You haven't really asked a specific question but I hope that my comments were helpful.

Plotting arbitrary 3d finite element mesh with matlab

Hello guys I am trying to export a mesh from MSC Patran and then plot it in Matlab. The mesh can be of arbitrary shape. I have the x, y and z coordinates of all the nodes. So far I have tried many different options and here is why they failed:
Surfc() with meshgrid and griddata:
I generated a grid on x-y plane with meshgrid and then used griddata to obtain the z matrix. But this plot only works when there is only 1 z value corresponding to an x-y pair. In other words, for this to work z must be of type z = f(x,y).
pdegplot() : I found out that matlab can import and plot .stl files. I tried converting my coordinate matrix format and plot it with this function but it doesn't work either. Because apparently in .stl files an edge can not be shared by more than 2 elements. However my FEM files are always (i hope) shell elements. This means 3 or more elements can share the same elements.
Surfc() with 3d meshgrid: I found out that meshgrid() can take 3 inputs (x,y,z) and create a 3d mesh. However this didn't work either. I used a very small mesh with about 1000 nodes and the code was trying to generate 3 matrices with 1000x1000x1000 elements. That means about 3 gb of memory for a 1000 node mesh. Whats more, surfc couldn't plot even that.
Somehow importing other file formats automatically: so far I have been using patran neutral files (.out). I manually read the file and extract x,y,z data from it. Patran can also export to parasolid, iges and step file formats. I looked for direct ways of importing and plotting these in matlab but such functions don't exist as far as I have looked.
Manually generating a grid: Matlab can create 3D objects (like [x,y,z] = sphere()) and Surfc() can plot these despite what I said in (1.) and the x,y,z matrices generated by sphere() are not 3 dimensional like in (3.) so I tried following this and manually generate a 3d grid from my FEM file just for testing. I found that z has repeating columns and in each column (which acts as a layer) there are n values of x and y. When I tried doing the same thing for my mesh manually, surfc() didn't work again. It plotted a really weird shape that I can't even describe.
Finding a 3rd party plotting software: I tried using (light) software like gnuplot and visit but so far I am all wet. I am open to suggestions if you know any (preferably open source) software that can directly plot patran neutral files. But the software has to be capable of contour plotting also. As I am calculating a quantity for each node in Matlab and then plotting its contour on the mesh.
So you can have a tetramesh?
You seem to be working with FEM-stile meshes, thus the standard surface-plotting function wont work. For FEM-meshes of different shape (not tetra) you may need to write your own function...
If you have the grid points and grid cell connectivities in say variables p and c, you can use the external Matlab FEA Toolbox to plot both structured and unstructured grids with for example the plotgrid command
% Cread grid struct.
grid.p = p;
grid.c = c;
grid.a = gridadj(p,c,size(p,1)); % Reconstruct grid cell adjacencies.
grid.b = gridbdr(p,c,grid.a); % Reconstruct boundary information.
grid.s = ones(1,size(c,2)); % Set subdomain numbers to 1 for all grid cells.
% Plot grid.
plotgrid( grid )

Align 2 sets of 3D points in Unity by using Singular Value Decomposition method (SVD)

I wanted to translate a set of reference points on contour to a set of corresponding target points. There are total 8 points on each contour.
In order to calculate the rotation & translation vector, I was using Math.Net Numerics library to perform SVD calculation - The idea came from this URL (page 3-7):
But somehow I noticed that transformation done using result from SVD calculation seems inaccurate. The result as shown below:
The transform supposed to move reference points to target points as close as possible, but as highlighted, it moves far away from target point.
In addition, I also did a simple test whereby I calculated centroid for both contours and perform deduction: (TargetCentroid - RefCentroid = translation vector). The final transformation result is the same as going through SVD.
Am I did something wrong? Can anyone suggest a better solution to transform ref point to target point?
Edit:
1. Garment transformation from reference model to various target models
This seems like an over complicated solution to the problem.
If you have the target points, you can just Lerp the given points to their corresponding target points.
Or if the target is the same mesh but of different scale and rotation as in the picture, you can just Lerp the transform values, scale and rotation respectfully, without the need to go over all the points individually.
Using Vector3.Lerp
Edit:
Additionally, lerping will cause all the points to reach their targets at the same time, which is, in most cases, the desired behavior.

Plot normal vector field 3D

I am implementing the algorithm for Photometric Stereo where I have already calculated the normals from a set of images with different light directions.
How can I plot the normal vector field in matlab? I have a matrix of normals of size (N x 3).
I'm afraid you have left out a step. You need to retrieve the depth map from the surface normals, and then you can start plotting. To see how to do this, you can check out section 4 of the following paper:
http://www.wisdom.weizmann.ac.il/~vision/photostereo/Photometric%20Stereo%20with%20General%20Unknown%20Lighting%20-%20BasriJacobsKemelmacher_ijcv06.pdf
There are other resources on the web too; I don't know of any built-in function in any Matlab library, but I don't have the Computer Vision toolbox, so who knows?
I suspect you are looking for quiver3.
You need to present normals field, as an gradient field, then you can,use quiver function. And in gradient field previously normalized triple {pn,qn,rn}, the data is presented in such a way , as to rend the third component of it always equal to one (at least in theory). I mean with rn=1, or should I now say, that now : R=1, and you actually need only {P,Q} coomponents to present contents of gradient field with ordinary 2D quiver function. Thus, gradient vector is something quite different and distinct from normals field, because: P=pn/sqrt(pn^2+qn^2+rn^2) , and Q=qn/(pn^2+qn^2+rn^2) POINTWISELY saying.
However you don't bother with double for loops, run over X,Y directions, cause POINTWISELY, correctly rendered calculations for gradient field from normals, is the following: P=pn./(pn.^2 + qn.^2 + rn.^2).^(1/2);, and so on.
You can see as well: http://www.mathworks.com/matlabcentral/fileexchange/authors/126090/
You need to present normals field, as an gradient field, then you can use a Matlab's quiver function. And in gradient field, the previously normalized triple {pn,qn,rn}, of the data, is presented in such a way, as to rend the third component of it always equal to one (at least in theory).
I mean with rn = 1, or should I now say, that now with: some R=1, you actually need only {P,Q} coomponents to present contents of gradient field with ordinary 2D quiver function. Thus, gradient vector is something quite different and distinct from normals field, because:
P=pn/sqrt(pn^2+qn^2+rn^2) , and Q=qn/(pn^2+qn^2+rn^2) POINTWISELY saying.
However you don't bother with double for loops, which would be run over X, Y directions, cause POINTWISELY, correctly rendered calculations for gradient field from normals, are the following:
P=pn./(pn.^2 + qn.^2 + rn.^2).^(1/2); , and: Q=qn./(pn.^2 + qn.^2 + rn.^2);
You can see as well:
http://www.mathworks.com/matlabcentral/fileexchange/authors/126090/
Briefly saying, the gradient field always represents slopes on X, Y directions, while descending exactly one height unit alongside Z axis on the 3D surface retrieved with for instance Photometric Stereo algorithm. That is why the third component in quiver function visualization is always equal to one (i.e. R = 1), and practically irrelevant.
I have posted, last month, some codes, on THE simplest Photometric Stereo methods, on Mathworks Web pages, due to some span of time available to tide it all up, I mean my own so far produced codes in Matlab...