What is the index of a point on a 3D mesh? - matlab

When using the MatLab command meshToPet, I am using 3 inputs p (points), e (edges) and t (triangles). The description for the edges as per the website, https://uk.mathworks.com/help/pde/ug/mesh-data-pet-triples.html, says
"e(1,k) is the index of the first point in mesh edge k."
What does the index of the first point in mesh mean?

e has k edges on them. The coordinates of points on such edges are in p.
so, for given edge number k, e(1,k) returns an index. This index is the index of the first of the points (p) that define the edge you selected (edge k).
to get the actual coordinates of such point, you can do [xp,yp]=p(:,e(1,k), as "e(1,k) is the index of the first point in mesh edge k."

Related

Finding fourth vertex of rectangle

Given three points (x1,y1),(x2,y2),(x3,y3) on a Cartesian plane can u find a fourth point such that these four points form a rectangle using c program
Tha code I am expecting

Optimizing computation of distance to triangle using barycentric coordinates

Building on the discussions here and here. I'm trying to compute the shortest distance between a 3D line and a 3D triangle.
I'm using barycentric coordinates to determine whether or not the point is inside the triangle. So given a triangle defined by vertices UVW and a line defined by point AB, I first compute the intersection of line AB with the plane defined by UVW. Let's call this intersection P and assume I've already done the checks to verify whether or not the point actually intersects the plane at all.
I then compute barycentric coordinates (S,T) such that S is defined along the edge UV and T is defined along the edge UW. Naturally, if 0≤S and 0≤T and S+T≤1 then P is on the triangle (or its edge) and my distance to the triangle is obviously zero.
If that's not true then P is outside the triangle and I need to compute the distance. The guidance of from the first link says to project point P onto all three edges to get three candidate points. Adding those points to the three triangle's vertices, you then have six points to test against.
Isn't it easier than that, though? If T<0, then don't you already know that UV is the closest edge and you only have to test against the projection of P onto that line? Similarly, if S<0 then UW would be the closest edge. If T>0 and S>0 then VW is the closest edge.
Thus based on the signs of S and T you already know the closest edge and only have to compute the distance from P to its projection onto that edge. If the projection isn't inside the triangle, then the closest point is either vertex. Thus your computations are about 1/3 of the proposed methods.
Am I missing something here, or is this a valid optimization? I'm fairly new to barycentric coordinates and their attributes.
It turns out that the problem of closest distance from a point and from a line are very similar and can both be reduced to a pure 2D problem.
Distance from a point
By Pythagoras, the squared distance from a point to a point of the triangle is the sum of the squared distance to the plane of support of the triangle and the squared distance of the projection of the point to that plane.
The latter distance is precisely the distance from the normal line to the triangle.
Distance from a line
Looking in the direction of the line, you see the projected triangle and the line is reduced to a single point. The requested 3D distance is equal to the 2D distance seen on the projection.
To obtain the desired coordinates, you use an auxiliary coordinate frame such that Z is in the direction of the line (and XY is a perpendicular plane); for convenience, choose the origin of the new frame to be on the line. Then by just ignoring Z, you get a planar problem in XY. The change of coordinates is an affine tranformation.
Point vs. triangle
Consider the three triangles formed by the origin (projection of the point/line) and a pair of triangle vertices (taken in cyclic order). The signed area of these triangles is a mere 2x2 determinant.
If the three areas have the same sign, the point is inside. Otherwise, the signs tell you where you are among the six surrounding regions, either past an edge or past a vertex.
On the upper figure, the point is inside (three positive areas). On the other figure, it is outside of the top-right edge (one negative area). Also note that dividing an area by the length of the corresponding side, you get the distance to the side. (Factor 2 omitted.)
The total work is
compute the affine frame;
convert the coordinates of the 3 or 4 points;
compute the three signed areas;
if inside, you are done;
otherwise, if in an edge region, compute a distance to a line and two distances to points;
otherwise you are in a vertex region, compute two distances to lines and one distance to vertex.

Extract the longest edge of a mesh generated by pdetool

When I generate a mesh with pdetoolbox, like the one in the image. How can I extract the norm (longest edge value) of the mesh? I tried reading the documentation of "pdetool" and found the "MaxElementSize" property in the "FEMesh Properties". Although I don't know how to use "MaxElementSize" in the PDE Toolbox GUI.
From the toolbox menu, select Mesh then Export Mesh:
You will then have the option to change variable name. The important ones here are the first two, i.e. names for points and edges. In this example, I keep them as defaults, i.e. p for points and e for edges.
points is a 2*n matrix, each column is represents the X- and Y-coordinates of a vertex. e is a 7*n matrix, each column being the parameters for constructing an edge. The first two rows is the index of the vertices of the edge and the rests are not of interest here.
% Each edge is bounded by two vertices.
% Extract the coordinates of the first set of vertices.
e1 = p(:,e(1,:));
% Extract the coordinates of the second set of vertices.
e2 = p(:,e(2,:));
% Calculate the square distance.
dsqr = sum((e1 - e2).^2);
% Take the maximum.
[dsqrMax, idx] = max(dsqr);
% Length of the longest edge
dMax = sqrt(dsqrMax);
idx is the index of the longest edge in e matrix. You can extract all information of the edge by e(:,idx);.

Given 4 line equation of a convex quadrilateral, how can I find coordinate of 4 corner?

I have four line equations y=mx+b and there is 6 intersections point.
It is known that those line form a convex quadrilateral.
How can I find which four intersection form the quadrilateral (preferably in order)?
First Concave QUAD:
The 6th intersection point should be also in 1) I just forget to draw it there.
compute all the intersection points and pair them to lines
l1: p2,p5,p6
l2: p1,p4,p6
l3: p1,p2,p3
l4: p3,p4,p5
l means line and p means intersection point
determine if point is only edge or also middle
so middle point is if you got another point from each side of line it belongs. In other words if you convert point to its parameter position (or distance from some start point) then middle point is in between the other two points distances. The parameter/distance you directly obtain while intersection computation but in case you don't you can use this:
t(p) = dot(p-A,B-A)
where A,B are line endpoints, p is queried intersection point and t(p) is its scalar "distance" from A.
So find out which points are only edge e and which are middle m:
l1: e2,e5,m6
l2: e1,e4,m6
l3: e1,m2,e3
l4: e3,m4,e5
now if any point is at least once middle then it is partially middle, if it is only edge then it is edge and if it is only middle then it is middle:
edge: p1,p3,p5
partial: p2,p4
middle: p6
construct polygon
So the edge points we have to use. The partial points we skip (as they lie on already used line) and finally we also use middle point. We know our polygon will be:
(p1,p3,p5) + (p6)
Now we need to find where the concave middle point p6 will go. There are 3 combinations:
e1,m6,e3,e5
e1,e3,m6,e5
e1,e3,e5,m6
we know that m6 belong to l1,l2 and l1,l2 has also p2,p5,p1,p4 from which are edge points only: e1,e5 so the m6 will be placed between them so the correct solution is:
e1,e3,e5,m6
Now Convex QUAD:
if we take advantage from #1,#2 then to form convex quad we have to use the middle and partial middle points and chose one of the pure edge points. Select the one that is not belong to line with pure middle point. So we have to use:
(p2,p4) + (m6) + one_from(e1,e3,e5)
the m6 is not belonging to l3,l4 so we need find edge belonging to both which is e3 so
(p2,p4) + (m6) + (e3)
Now we just have to find out the order. middle point and edge point will not be near themselves so you got 2 solutions:
p2,m6,p4,e3
p2,e3,p4,m6
Both are correct they are just reverse of themselves (differend polygon winding rule) so you can chose the one you need based on z coordinate of cross product of any two neighboring vertices.
Solve systems of linear equations for every line pair
y = m[i] * x + b[i]
y = m[j] * x + b[j]
where m[i], b[i] are coefficients of i-th line and get (x,y) for intersection.
If there are exactly 6 intersection (no parallel lines, no degeneracies), then every line has three intersection points in row. Two intersections are "outer" and one (middle) is "inner", so there are three inner and three outer points.
Seems that convex quadrilateral always contains all the three inner points, so we can separate inner points, and get corresponding fourth point.
For example, if lines are designated as A,B,C,D, and intersections AB, AC, CD are inner, then fourth point is intersection BD (B and D are unique in intersection list)
P.S. Note that line equation form y=mx+b is not suitable for all possible lines (not for vertical lines), so it would better to use more general form like A*x + B*y + C = 0 or other.

Calculate distance between coordinates but with restrictions

I managed to find the intersections of an arbitrary number of lines within a binary image.
Then i use a function where i detect the intersections between the lines. So now i have the coordinates of the intersections saved in an array.
Now i want to calculate the distance between the intersections(an imaginary line that connects all my intersections) but i want the distance calculation to traverse along the lines that are already on the binary image.
So the distance calculation cannot escape a line while calculating, instead it must ''walk along it''.
The imaginary path(of which eventually we calculate its distance) must walk along the already drawn lines.
EDIT** THIS IS MY INTERSECTION DETECTION ''ALGO''
clear all
pellara4=imread('C:/users/lemesios/desktop/pellara4.jpg');
blackwhitepellara=im2bw(pellara4,0.5);
I = blackwhitepellara;
C = corner(I);
num_of_rows=size(C,1);
num_of_cols=size(C,2);
for z =1:num_of_rows
k=C(z,2);
j=C(z,1);
if (I(k+1,j)==0)&& (I(k,j+1)==0) && (I(k-1,j)==0) && (I(k,j-1)==0)
imshow(I);
hold on
plot((j), (k), 'b*');
disp(k);
disp(j);
end
end
I feel this is similar to Dijkstra's algorithm. You can denote the intersection points by nodes. Then generate a mesh where each intersecting point is connected to every other intersecting point. Then if there exists a line in the binary image, assign a unit weight, otherwise assign inf i.e. infinite weight. When you have to measure distnace between m-th point and n-th point (say), then make m-th point as source and n-th point as destination and find the shortest path according to Dijkstra's algorithm.