1

I'm working in php with 3D geometries(not the best choice,I know...). I have K coplanar 3D points, also with x,y,z value. Together they form a polygon. I need to triangulate this polygon. I have already a working delaunay traingulation function which works for 2D Polygons. So I want to rotate the given points, so that they lay on a plane parallel to the x,y plane. After that I can triangulated it using the x,y values. The following pseudocode shall describe how I want to get to this goal.

I build up the following code with reference on this (I'm usign the answer accepted from the OP): https://math.stackexchange.com/questions/180418/calculate-rotation-matrix-to-align-vector-a-to-vector-b-in-3d, but it doesn't work as I expected. In order to know if it worked, every mapped point shall then have the same 'z' value. Here is the question, how do I get the correct rotation matrix? Or did I made a conceptual mistake?

function matrixRotationMapping(Point $p, Point $q, Point $r)
        {
            $normalPolygon =calculatePlaneNormal($p, $q, $r);
            $v = crossProduct($normalPolygon, new Point(0, 0, 1));
            $c = dotProduct($normalPolygon, new Point(0, 0, 1));
            $matrix = buildRotationMatrix($v, $c);    
            return $matrix;
        }    
    
function buildRotationMatrix($v, $c)
        {
            $R2 = new Matrix(array(array(1, -$v->z, $v->y), array($v->z, 1, -$v->x), array(-$v->y, $v->x, 1)));
            $costant = 1/(1+$c);
            $R3 = multiplyMatrices($R2, $R2);
            $R3 = multiplyMatricesWithFactor($R3, $costant);
            $finalMatrix = sumMatrices($R2, $R3);
            return $finalMatrix;
        }
                            
function calc2DMapping($points)
        {
             $rotationMatrix = matrixRotationMapping($points[0], $points[1], $points[2]);
             foreach($points as $point)
                {
                    $mappedPoint = $rotationMatrix->multiplyWithPoint($point);              
                    $mappedPoints[] = new MappedPoint($mappedPoint);
                }       
        }

I found another helpful description of the problem, but I wasn't able to implement it: Mapping coordinates from plane given by normal vector to XY plane

Thanks in advance for your attention.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Swisx
  • 119
  • 11

1 Answers1

0

You need basis vectors X,Y,Z first. So let take the mid point A and two distant points to it B,C (not on single line) from your data set first. The X,Y should lie in the plane and Z should be normal to it so:

X = B-A     // any non zero vector inside plane
X = X / |X| // unit in size

Y = C-A     // any non zero vector inside plane
(X.Y) != 0  // but not parallel to X !!!
Y = Y / |Y| // unit in size

Compute normal to the plane your points lie in and correct Y axis.

Z = X x Y   // cross product gives you perpendicular vector
Y = Z x X   // now all vectors are perpendicular and unit

So feed these 3 vectors to rotation part of your transform matrix and set origin to A. But as you need to go from your data set to the plane local coordinate you need inverse matrix (or use pseudo inverse based on transposing)

Anyway now with the basis vectors you can map your plane parametrically like this:

P(u,v) = A + u*X + v*Y

Where u,v = <-inf,+inf> are surface distances form A in X,Y directions. That can get handy sometimes. If you need to compute u,v from P then exploit dot product:

u = ((P-A).X) = dot(P-A,X)
v = ((P-A).Y) = dot(P-A,Y)

Which can be also used to transform to 2D instead of using matrix ...

Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • Hi, I'm sorry, but I don't get completely your solution. I already know that all the points lie on the same plane, I can easily find out the normal and the equation of the plane. What I to want to do , is to rotate/map all points to a plane that is parallel to the standard X,Y plane, also the plane defined by (0,0,1) Vector. – Swisx Mar 08 '17 at 14:58
  • @Swisx the last equation gives you `u,v` you can use it as `x,y` coordinate of the rotated point `P` ... `z=0` what is unclear ? (you do not understand vector math? or what specifically? ) – Spektre Mar 09 '17 at 07:43
  • Thaks for the seconda explanation, yeah in the last part I was not so sure what did you meant. But know I got it and I could implement it. It looks like it's working even with edge cases, when the points lie on a plane orthogonal to the x,y plane. thank you very much. – Swisx Mar 09 '17 at 09:43
  • @Swisx glad to be of help. – Spektre Mar 09 '17 at 09:58