Catmull Clarke doesn't preserve planar normals - matlab

In the toy example I show, one of the surface normals is clearly incorrectly pointing inwards. I can create a new cube with the normals outward facing as expected, but after processing with Catmull Clarke, there is no guarantee that all normals will remain extant facing.
Since I'm using quadrilaterals by necessity, I know I can fix the face by transposing the vertice order i.e. [a b c d] -> [d c b a] and thus fix the normal. But how do I determine that a given face's normal is pointing the wrong direction?
(not enough rep to embed) https://gyazo.com/e20576e700196a43a2378eb055a71b38

You can check for sign of the dot product between the face normal vector and the vector from the cube's centroid to any point on the face.
Let's say the normal vector of the face [a,b,c,d] is n. Next, compute the centroid of the cube by averaging its 8 vertex coordinates, and let's call it p.
Then, calculate dir = dot(n,(a-p)). If dir > 0, the normal n is pointing outwards from the cube. If dir is negative, you have to flip the normal.
This method will work for the faces of any convex polyhedron. If you're dealing with non-convex polyhedra, you will have to use an approach like the one mentioned here.

Related

Intersection between two 3D surfaces

I have two independent 3D shapes; one is a square and another is a cone.
Let's assume the cone lies inside the square. How can I find out that the surface of the cone touches the square's surface when I move the cone in any direction?
It will be helpful if anyone can suggest an algorithm to check whether the surface touches another shape.
I am working with MATLAB, but the underlying logic will be appreciated in any language will be appriciated.
https://in.mathworks.com/matlabcentral/answers/367565-findout-surface-to-surface-intersection-between-two-3d-shapes
There is a relatively easy solution, thanks to the fact that the truncated cone is a convex shape, and finding its AABB is not so hard.
First rotate space so that the cube become axis aligned (and the cone in arbitrary position). Then to find the AABB of the base, is suffices to get the maxima an minima of the coordinates, using the parametric equation, C + R cos t + R' sin t, where C is the position vector of the center, and R, R' two orthogonal radii. You find the limit angles by canceling the derivative.
After finding the extrema on the three coordinates, the global bounding box is the one that surrounds these six points plus the apex.
By comparing the AABB to the cube, you can tell what distance remains before a collision, in any direction.

Sorting array of points in clockwise order

Is there such an algorithm to sort an array of 3D points in clockwise order?
I'm specifically dealing with right triangle in my case so only 3 points. (for build mesh)
Assume you have two edges connecting your three vertices.
E1 = V2 - V1
E2 = V1 - V3
They span a triangle. You can calculate the triangle's normal N like this:
N = cross(E1, E2)
This tells you which direction the triangle is facing. You can calculate whether the triangle is facing towards, or away from, a certain point of view P by projecting N onto the distance of your triangle from P.
D = V1 - P
d = dot(N, D)
If d is positive, the triangle looks away from P, if it is negative, it faces P.
You are now able to judge for every set of (V1,V2,V3) whether they are sorted correctly or not. If not, just swap V2 and V3 and they will be.
There is one pitfall though. If you are trying to build the hull of a closed mesh, the requirement is that all triangles are facing towards the outside. This can not be modelled by trying to make all triangles face a certain point, because that point would have to be different for each triangle. If the mesh is convex through, you can model it by requiring that all triangles face away from a certain point, which lies inside the convex mesh.
The algorithm for sorting is not difficult. The problem is, what plane that those points lie on. And which side it facing
Only a bunch of points cannot clockwise or counter clockwise by itself. You need a plane and a side to reference those point
edit : Actually what I used to said is a bit inaccurate. What you really need is a position and direction to reference, not a plane

How to convert camera pose (Translation matrix) obtained from the essential matrix to world coordinate system

I have extracted Rotation and Translation matrices from the essential matrix. The translation vector has a scale ambiguity. Therefore, I couldn't define its "true" values.
My steps were as follow:
F=estimateF(matches1,matches2,'RANSAC')
E=K2'*F*K1
[U S V]=svd(E)
s=(S(1,1)+S(2,2))/2
S=diag([s s 0])
E_new=U*S*V'
[U S V]=svd(E_new);
R1=U*W*V'
R2=U*W'*V';
t1=U(:,3);
t2=-t1
My problem is how to define the translation of the second camera from the first one in mm.
Unless you know some more information that ties your points to the real world, it's not possible to recover the absolute scale.
For example, if the matches where corners of squares of a calibration chessboard of which you know the size in mm, then you would be able to know how far cameras are from each other in mm.

MATLAB linear separation of point sets

So I have a set of points V, which are the vertices of a convex polytope, and a separate point p. Basically, I want to check whether p is contained in V. To do so, I set up a linear program that checks whether there exists a hyperplane such that all points in V lie on one side, while p lies on the other, like so (using YALMIP):
z=sdpvar(size(p,1),1);
sdpvar z0;
LMI=[z'*vert-z0<=0,z'*probs-z0<=1];
solvesdp(LMI,-z'*probs+z0);
The hyperplane is defined by the set of points z such that z'*x - z0 = 0, such that if I get a value larger than zero for the point p, and one smaller than zero for all vertices, then I know they are separated by the plane (the second constraint is just so the problem is bounded). This works fine. However, now I want to check whether there is a hyperplane separating the two point sets such that it contains the origin. For this, I simply set z0 = 0, i.e. drop it entirely, getting:
z=sdpvar(size(p,1),1);
LMI=[z'*vert<=0,z'*probs<=1];
solvesdp(LMI,-z'*probs);
Now, however, even for cases in which I know there is a solution, it doesn't find it, and I'm at a loss for understanding why. As a test, I've used the vertices
v1=[0;0;0];v2=[1;0;0];v3=[0;1;0];v4=[1;1;1];
and the point
p=[0.4;0.6;0.6];
When plotted, that looks like the picture here.
So it's clear that there should be a plane separating the lone point and the polytope that contains the origin (the front and center point of the polytope).
One thing I've tried already is to offset the vertex of the polytope that's now on the origin from the origin a little (10^-5), such that the plane would not touch the polytope (although the LP should allow for that), but that didn't work either.
I'm grateful for any ideas!

Arrange the vertices of a 3D convex polygonal plane in counter clockwise direction in MATLAB

I have a convex polygon in 3D. For simplicity, let it be a square with vertices, (0,0,0),(1,1,0),(1,1,1),(0,0,1).. I need to arrange these vertices in counter clockwise order. I found a solution here. It is suggested to determine the angle at the center of the polygon and sort them. I am not clear how is that going to work. Does anyone have a solution? I need a solution which is robust and even works when the vertices get very close.
A sample MATLAB code would be much appreciated!
This is actually quite a tedious problem so instead of actually doing it I am just going to explain how I would do it. First find the equation of the plane (you only need to use 3 points for this) and then find your rotation matrix. Then find your vectors in your new rotated space. After that is all said and done find which quadrant your point is in and if n > 1 in a particular quadrant then you must find the angle of each point (theta = arctan(y/x)). Then simply sort each quadrant by their angle (arguably you can just do separation by pi instead of quadrants (sort the points into when the y-component (post-rotation) is greater than zero).
Sorry I don't have time to actually test this but give it a go and feel free to post your code and I can help debug it if you like.
Luckily you have a convex polygon, so you can use the angle trick: find a point in the interior (e.g., find the midpoint of two non-adjacent points), and draw vectors to all the vertices. Choose one vector as a base, calculate the angles to the other vectors and order them. You can calculate the angles using the dot product: A · B = A B cos θ = |A||B| cos θ.
Below are the steps I followed.
The 3D planar polygon can be rotated to 2D plane using the known formulas. Use the one under the section Rotation matrix from axis and angle.
Then as indicated by #Glenn, an internal points needs to be calculated to find the angles. I take that internal point as the mean of the vertex locations.
Using the x-axis as the reference axis, the angle, on a 0 to 2pi scale, for each vertex can be calculated using atan2 function as explained here.
The non-negative angle measured counterclockwise from vector a to vector b, in the range [0,2pi], if a = [x1,y1] and b = [x2,y2], is given by:
angle = mod(atan2(y2-y1,x2-x1),2*pi);
Finally, sort the angles, [~,XI] = sort(angle);.
It's a long time since I used this, so I might be wrong, but I believe the command convhull does what you need - it returns the convex hull of a set of points (which, since you say your points are a convex set, should be the set of points themselves), arranged in counter-clockwise order.
Note that MathWorks recently delivered a new class DelaunayTri which is intended to superseded the functionality of convhull and other older computational geometry stuff. I believe it's more accurate, especially when the points get very close together. However I haven't tried it.
Hope that helps!
So here's another answer if you want to use convhull. Easily project your polygon into an axes plane by setting one coordinate zero. For example, in (0,0,0),(1,1,0),(1,1,1),(0,0,1) set y=0 to get (0,0),(1,0),(1,1),(0,1). Now your problem is 2D.
You might have to do some work to pick the right coordinate if your polygon's plane is orthogonal to some axis, if it is, pick that axis. The criterion is to make sure that your projected points don't end up on a line.