from ..cAPI import SimulationAPI
import numpy as np

def Compute(obj, base_normal_attribute, twist_attribute = ''):
    positions = obj.data.attributes['position']
    n_curves = obj.data.attributes.domain_size('CURVE')
    n_cvs = obj.data.attributes.domain_size('POINT')
    positions_data = np.zeros((n_cvs, 3), dtype=np.float32)
    positions.data.foreach_get('vector', np.ravel(positions_data))
    verts = SimulationAPI.CreateVector3Array(n_cvs)
    SimulationAPI.CopyVector3Array(verts, positions_data)
    start_cvs = SimulationAPI.CreateIntArray(n_curves + 1)
    for i in range(n_curves + 1):
        start_cvs[i] = obj.data.curve_offset_data[i].value

    base_normals_attr = obj.data.attributes[base_normal_attribute]
    base_normals_data = np.zeros((n_curves, 3), dtype=np.float32)
    base_normals_attr.data.foreach_get('vector', np.ravel(base_normals_data))
    base_normals = SimulationAPI.CreateVector3Array(n_curves)
    SimulationAPI.CopyVector3Array(base_normals, base_normals_data)

    twists = SimulationAPI.FloatArray([0] * n_cvs)
    if twist_attribute:
        twist_attr = obj.data.attributes[twist_attribute]
        twist_data = np.zeros(n_cvs, dtype=np.float32)
        twist_attr.data.foreach_get('value', twist_data)
        SimulationAPI.CopyFloatArray(twists, twist_data)

    normals = SimulationAPI.CreateVector3Array(n_cvs)
    # all computations are in the object space
    SimulationAPI.Utils.ComputeNormal(start_cvs, twists, verts, base_normals, normals);
    return normals
