3D visualisation with VTK/paraview - visualization

Here is my problem :
I have a 2D domain (say a square) divided in triangles in a unstructured manner. On each triangle (denoted T), I define a constant scalar, denoted f(T).
I want to plot the surface (T,f(T)) using Paraview.
So I have created a vtk file like this:
vtk DataFile Version 3.1
my personnal comment here
ASCII DATASET UNSTRUCTURED_GRID
POINTS 4 FLOAT
0 0 0
0 1 0
1 0 0
1 1 0
CELLS 2 8
3 0 1 2
3 1 2 3
CELL_TYPES 2
5 5
CELL_DATA 2
SCALARS Namedata double
LOOKUP_TABLE default
2
-5
I would like to see (thanks to Paraview) a 3D plot. Ideally, the visualization on this example should be two triangles: the first one should have its z-coordinates equal to 2 and the second one should have its z-coordinates equal to -5. I don't know if something is possible or if the vtk format is the good one for what I'm trying to do.
If it is not possible, I would be happy to represent only the two points (x-coordinate of the triangle number 1, y-coordinate of the triangle number 1, z-coordinate = 2) and (x-coordinate of the triangle number 2, y-coordinate of the triangle number 2, z-coordinate = -5) with or without interpolation.

This is not hard to do in ParaView. There is filter named "Warp By Scalar" which can translate geometry in the z direction (or any other direction) based on a scalar field. The only issue is that Warp By Scalar works on point data, and you want to warp by cell data. So you have to go through a few steps first.
Add the "Shrink" filter to your data. Set the "Scale Factor" parameter to 1. Apply. Your data will look the same, but it will have the effect of breaking apart the triangles, which are actually sharing points in your original data.
Add the "Cell Data to Point Data" filter to the output of the Shrink filter. Apply.
Add the "Warp By Scalar" filter to the output of Cell Data to Point Data. Apply.

Related

Is it possible to separate the different level contours given by imcontour in Matlab?

I am using the image processing toolbox and the
imcontour
function. I have 2 questions.
1) what are the levels exactly ?
2) How can I separate the different level contours given in c when using
[c,h]=imcontour(Img,3)
When displaying the contour, Matlab presents different colors for the different levels. I would like to retrieve the coordinates of the inner contour only, how can I do this ? On this image, I presented the output of the imcontour function with level 1, level 2, level 3 and level 4. I am only interested in the yellow part of the level 3 image.
Yes, this is possible, but not really trivial. The C matrix returned by [C,h]=imcontour(Img,3) is the ContourMatrix. This is a 2xN array containing all vertices for the lines drawn.
Say you have 3 contours (as per your second input argument to imcountour), and want to find the vertices to the 3rd contour (which is what I presume is the yellow line). You need to skip ahead two contours first:
index = 1;
for ii=1:2 % skip 2 contours
index = index + 1 + C(2,index);
end
N = C(2,index); % number of vertices for 3rd contour
level = C(1,index); % grey-level of 3rd contour
vertices = C(:,index+(1:N));

Create a plot with a 2D colourmap depending on two variables

I want to display an image (e.g.imshow) and use a colormap to represent the values of my data points.
However, colormap only gives the option to be dependent on a single variable, but I want a "2D colormap" which depends on two variables.
For example I have a simple image 2x2 pixels:
img = [
1 1 5 6;
1 2 8 7;
2 1 4 3;
2 2 15 3]
Here the first two values of each row are the coordinates, the other two are the values describing the pixel (call them x and y).
When displaying the image I want to use a 2D colormap. For example something like this, which picks a colour depending on both variables (x and y):
Is there an option in MATLAB do to this, possibly in one of the extra toolboxes?
If not can this be done manually? I was thinking by overlaying a grey scale image given from the first value over a colormap image given by the second value a similar effect could be achieved.
In your 2D colormap you are actually using the HSV color space.
Basically, your x axis is Hue, and Y axis is Saturation. You can convert any value into this space if its properly scaled. If you make sure that you scale your 3rd and 4rd column in the [0-1] interval you can easily do
colorRGB=hsv2rgb([val3,val4,0.5]);
If you perform this operation for each pixel, you'll get the image you want.
I gave a extended explanation of how HSV works here

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?

Clustering Connected Squares in MATLAB

Assume there is a mesh which is colored in the specific pattern:
As you can see, these 62 red squares are connected together in three different groups(Clusters). You might like to download the data from the following link.
Click here for downloading mesh coordinates (XX, YY, ZZ) and its colours(C)
Then, you can generate the mesh below with this code:
load('data.mat');
figure('units','normalized','outerposition',[0 0 1 1]); % Opening big figure
axis equal; axis off;
SURF = surf( XX , YY , ZZ, C);
Now, I am looking for a code which can segment these three clusters.
Please bear in mind that you should consider those squares as a one group which have maximum one square gap distance in any direction. It means, in this particular case, we have three clusters of 11 , 13 and 38 squares.
Thank you in advance for your time.
You can use connected component labeling.
In Matlab, assuming your matrix contains only 0 and 1 (or you can make it so), you would use bwlabel.
L = bwlabel(data, 8)
Now L will be a matrix the same size as data with labels 1, 2, 3... in place of the 1's.
The 8 as the second parameter denotes the connectivity of the components. 4-connected would mean that one square is connected to the other only if it is to the left, right, above or below the square. 8-connected means that squares are also connected if they are adjacent diagonally, as in the bottom-right of your sample. 8 is the default, and you can leave it out, but you should be aware of the distinction in case you need it to behave differently in the future.

Ask for plotting rectangular pulse with controlled rising/falling edge

I would like to plot a series of rectangular pulse to a vector data input. The plot profile would create a rising edge of the pulse for a positive number and create the falling edge for the negative of that number. The plot should separate color for each represented number.
For example, if vector input X is [1 -1 2 -2 3 4 1 -4 -1 -3]
Amplitude of data ‘1’ is 5,
Amplitude of data ‘2’ is 4,
Amplitude of data ‘3’ is 3 and
Amplitude of data ‘4’ is 2
So, the input X got index from t(1) to t(10). The output of plot or chart should look like the inserted image
How would I can write a MATLAB code for this work?
Thank you.
B.Bundit
The plot is very small but I think you want something like bar in matlab. Documentation is here. If the vector that you have is changes then you can create a vector of values for plotting using cumsum.
X=[1 -1 2 -2 3 4 1 -4 -1 -3];
Xplot=cumsum([5,X(2:end)]); % //so 5 will be your initial value
Since the plot you give above has different widths of bars, you would also need a vector of the center of each data point and the width of each data point.
means= [1 2 3 4 5 6 7 8 9 10];
widths=[1 1 1 1 1 1 1 2 1 1];
For different color bar plots you can do:
colors=['r','g','k','b','c','m','y','r','g','k'];
for i=1:length(X)
h=bar(means(i),Xplot(i),widths(i));
if i==1, hold on; end
set(h,'FaceColor',colors(i));
end
% //This will label each bar, or you could define your axes before the loop
set(gca,'XTickLabel',means);
You can explore other properties to set here. The class barseries does not have a FaceAlpha property so I'm not sure if you can make them transparent. You could however set the FaceColor to none and have the EdgeColor be set to your color of choice. Note that edge color is specified by an RGB triplet and not a letter code.
You may also need to resort your data by width, so that the widest bars are plotted last and are thus on top. This would look like this:
[widths_sorted,sort_idx]=sort(widths,'ascending');
Xplot_sorted=Xplot(sorted_idx);
means_sorted=means(sorted_idx);