How to triangulate a polygon with and without vertices and holes inside - delaunay triangulation possible? - visualization

I got the following problem: I'm working on an algorithm and the output will be an arbitrary polygon. The polygon can be concave, have holes in it and also have vertices with edges that have an angle of 180 degree.
I need to triangulate said polygon with and without additional vertices inside it. Can the delaunay triangulation handle this, especially if I only have the polygon without vertices inside? I might be able to avoid holes inside the polygon, if that eases things.
Thanks for reading

You might have a look at these Fade2D examples:
https://www.geom.at/example4-zones-defined-areas-in-triangulations/
https://www.geom.at/boolean-operations-on-polygons-with-holes/
The student license is free.

You can do that using a constrained triangulation (Delaunay or not) to triangulate a polygon. There is an example using CGAL here.

You can use Triangle.
Running Triangle via the command line, you create an ASCII .poly file that specifies the vertices and edges of the input (i.e., your polygon) as well as any holes in the domain. Each hole is defined by its xyz location. For example, if I have a square domain with a triangular hole inside it, my input could look like:
7 2 0 0 # 7 vertices
1 1 -1
2 1 1
3 -1 1
4 -1 -1
5 -.5 -.5
6 .5 -.5
7 0 .5
7 0 # 7 segments: four for the square and three for the hole
1 1 2
2 2 3
3 3 4
4 4 1
5 5 6
6 6 7
7 7 5
1 # 1 hole
1 0.0 0.0
If you want to refine the mesh (i.e., add vertices inside), you can use either the -a option (limiting the maximum area of a triangle) or the -q option (controlling the quality of the triangles produced but not directly limiting the size). The documentation on command line switches is here.

Related

Flip data for use in graph upside down with intervals unchanged (exactly as in picture attached)

I am using MATLAB and need to flip data in a graph upside down as in pic below;
I.e. I have vector A = [1 2 3 4 4 4 3 3 3] (blue line) and I need to transform it exactly into B = [4 3 2 1 1 1 2 2 2] (red line). Note that I need the interval of the x- and y-axis to be unchanged. I have tried flipud, fliplr, flip and rot90, without any success.
Please note that I need to transform the data itself (it is not enough to flip the image upside down).
Stuck for a while, any help is much appreciated!
Mathematically, it is the following transformation:
B = -A + min(A) + max(A);

3D visualisation with VTK/paraview

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.

Rendering MATLAB patch faces with Plotly fig2plotly()

Problem: When attempting to export a polygon rendered with the patch command in MATLAB with fig2plotly, the final output is lacking the specified face colours.
Perhaps a demonstration would help. Take the following vertices and faces to define a cube (lifted from the MATLAB documentation):
vert = [0 0 0;1 0 0;1 1 0;0 1 0;0 0 1;1 0 1;1 1 1;0 1 1];
fac = [1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8];
And render it with the patch command, adding some colour information to the faces:
patch('Vertices',vert,'Faces', fac, 'FaceVertexCData',hsv(8),'FaceColor','interp')
And view it in 3D:
view(3)
axis vis3d
This gives a nice cube with interpolated colour values on the surface.
Now, if we attempt to export it to Plotly with the fig2plotly command:
fig2plotly(gcf)
It returns an empty cube (plotly link):
In other words, the line information has been captured, but not the faces. Even if we attempt to preserve the MATLAB styling, we still loose the face information:
fig2plotly(gcf, 'strip', false)
Any suggestions?
Improved support for patches has been added to ver. 2.2.9 of the wrapper (https://github.com/plotly/MATLAB-Online).
You can toggle this improved patch handling by setting the TriangulatePatch default to true within the plotlyfig.m file.
(https://github.com/plotly/MATLAB-Online/blob/master/plotly/plotlyfig.m#L61)
Colour gradients are not yet supported but the patches themselves should now render. Colour modifications can be made by manually modifying the attributes of the plotlyfig object or by using the web interface.

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?

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);