use pandas to get barycentric ratio from linear map and use ratio to convert dataset coordinates X,Y

Viewed 106

I am not quite sure about barycentric coords but I need to use it to do a transformation of coordinates from one local system into the global system using python script. I have this dataset in this format:

        pixIndex       X       Y   R    G    B
               1       0       0  227  227  227
               2       1       0  237  237  237
               3       2       0   0     0    0
               4       3       0  232  232  232
               5       4       0  233  233  233
    ...        ...     ...     ...  ...  ...  ...

I have these 3 coordinates from the global coordinate system : {103.890991,1.369125} , {103.8892,1.368017}, (103.8903,1.367166) and in the middle of these 3 points is {103.890221,1.367944}

And these are the equivalent coordinates in the local coordinate system: {207.0,489.0} , {500.0,58.0} , {84.0.17.0} and in the middle of these 3 points is {197.0,262.0}

The barycentric coords can be found with this code but it is not in python:

Vector Triangle::getBarycentricCoordinatesAt( const Vector & P ) const
{
  Vector bary ;

  // The area of a triangle is 
  real areaABC = DOT( normal, CROSS( (b - a), (c - a) )  ) ;
  real areaPBC = DOT( normal, CROSS( (b - P), (c - P) )  ) ;
  real areaPCA = DOT( normal, CROSS( (c - P), (a - P) )  ) ;

  bary.x = areaPBC / areaABC ; // alpha
  bary.y = areaPCA / areaABC ; // beta
  bary.z = 1.0f - bary.x - bary.y ; // gamma

  return bary ;
}

Credit from https://gamedev.stackexchange.com/questions/23743/whats-the-most-efficient-way-to-find-barycentric-coordinates

Could someone tell me how to python equivalent would look for this snippet and how to use the barycentric ratios to convert my current dataset [local coordinate system to global coordinate system]

I suppose that the formula would be something like this P=uA+vB+wC. when I have obtained the ratios represented by bary from the above snippet code for both the local and global coordinate systems. [source: https://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/barycentric-coordinates#:~:text=To%20compute%20the%20position%20of,(barycentric%20coordinates%20are%20normalized).]

The end result would ideally look something like this

 pixIndex  X  Y    R    G    B    T_X  T_Y
       1   0  0  227  227  227  100.0  1.5
       2   1  0  237  237  237   90.0  1.6
       3   2  0    0    0    0  110.0  1.5
       4   3  0  232  232  232  112.0  1.7

As I am not very familiar with python, could someone show me how I should go about with this conversion from local to global coord system using the reference points I have provided in python?

Any help is much appreciated :)

1 Answers

Here the algo from C to Python for the Barycenter, for the translation i don't know how to do this, if you got the ratio it's will be easy i think.

import numpy as np
def Barycenter(P, triangle): 
    """
    We use numpy to manipulate coordinates 
    triangle and P are a numpy array
    triangle[0] = a; triangle[1] = b; triangle[2] = c

    """
    vector_A = triangle[1] - triangle[2]
    vector_B = triangle[1] - trinagle[0]
    normal = abs(np.cross( vector_A, vector_B))
    areaABC = np.dot(normal , np.cross(triangle[1] - triangle[0], triangle -[2] triangle[0]))
    areaPBC = np.dot(normal , np.cross(triangle[1] - P, triangle[2] - P))
    areaPCA = np.dot(normal , np.cross(triangle[2] - P, triangle[0] - P))
    bary_x = areaPBC / areaABC
    bary_y = areaPCA / areaABC
    bary_z = 1.0 - bary_x - bary_y
return [bary_x ,  bary_y, bary_z]
Related