I have three 3D points like p1(x1,y1,z1)
, p2(x2,y2,z2)
, p3(x3,y3,z3)
.
I have another point, but I know only x
, y
value of that point like p4(x4,y4,Z)
, in which Z
is the value I like to compute.
I am sure p4(x4,y4)
point is inside a triangle formed by p1(x1,y1)
, p2(x2,y2)
, p3(x3,y3)
by checking with delaunay triangulation approach. How can I compute Z
value of point p4
? I like to implement it in C programming. Actually I am trying to implement griddata
in MATLAB.
Thanks
This is to support both MBo's and Konstantin's answers. Please don't accept this question, but one of the others.
This is how you would implement a solution in MATLAB:
Now, in C++, the mere fact of including the relevant
eigen
libraries blows up the executable size rather spectacularly. Whateigen
is capable of is complete overkill for this simple 2x2 system.So I wouldn't go as far as resort to
eigen
, unless you have tons of other linear algebra things to do. It is a simple 2x2 system, which is easy enough to solve by hand.Just KISS it; see DanielKO's answer :)
You can express P4 coordinates in the P1P2P3 vector basis.
This is easy-to-solve linear equation system. You have to find A and B coefficients, then use them to calculate z-coordinate
The mathematical problem here is to solve the following system of equations
or equivalently
for a, b, and z4.
To solve it in C/C++, you could either implement the Gauss algo (see also the Numerical Recipes book, it is available online), or use Linear Algebra libraries, such as Eigen, or others.
Remark: the approach is the same regardless if the point
(x4, y4)
lies within the triangle(x1, y1), (x2, y2), (x3, y3)
, or not.p1
,p2
,p3
define a plane. You can represent it by a point and a normal. For instance,P=p1
,N=(p2-P) x (p3-P)
(that is, N = cross product ofp1p2
andp1p3
).Now for p4 to be in the same plane, it satisfies the plane equation:
Re-arranging:
No linear system to solve, you just need a cross product.