# Path: pam/pam.py
# SEED = 0
#     SEED = bpy.context.scene.pam_mapping.seed
# def computeUVScalingFactor(obj):
# def map3dPointToParticle(obj, particle_system, location):
# def maskParticle(p_layer, p_index, mask_layer, distance=0.2):
# def distanceToMask(p_layer, p_index, particle_index, mask_layer):
# def computeConnectivityProbability(uv1, uv2, func, args):
# def computeDistance_PreToSynapse(no_connection, pre_index, synapses=[]):
# def sortNeuronsToUV(layer, neuronset, u_or_v):
# def computeMapping(layers, connections, distances, point, debug=False):
# def computeDistanceToSynapse(ilayer, slayer, p_3d, s_2d, dis):
# def addConnection(*args):
# def replaceMapping(index, connection):
# def computeAllConnections():
# def updateMapping(index):
# def computeConnectivity(con, create=True, threads = None):
# def post_neuron_wrapper(x):
# def post_neuron_initializer(players, pconnections, pdistances):
# def pre_neuron_wrapper(x):
# def pre_neuron_initializer(players, pconnections, pdistances, puv_grid, pno_synapses):
# def computeConnectivityThreaded(con, create=True, threads = None):
# def computeConnectivityAll(layers, neuronset1, neuronset2, slayer, connections, distances, func, args):
# def printConnections():
# def computeDistance(layer1, layer2, neuronset1, neuronset2, common_layer,
#                     connection_matrix):
# def measureUVs(objects):
# def initializeUVs():
# def returnNeuronGroups():
# def resetOrigins():
# def initialize3D():


import mstree
import mst_blender
import numpy as np
import bpy
import mathutils
from pam import pam
    # all items are stored in the first list-item of the output
    points = points[0]
    
    # Run mstree
    root_point = mstree.mstree(points, balancing_factor)

    # Convert node positions from uv to 3d
    nodes = mstree.tree_to_list(root_point)
    
    interpolated_nodes = []
    for node in nodes:
        parent = node.parent
        interpolated_nodes.append(node)
        if parent is None:
            continue
        points = interpolate(parent.pos, node.pos, interpolation_resolution)[1:-1]
        print(len(points))
        parent.children.remove(node)
        active_parent = parent
        for point in points:
            new_node = mstree.Node(active_parent, np.array(point), -1)
            interpolated_nodes.append(new_node)
            active_parent = new_node
        node.parent = active_parent
        active_parent.children.append(node)


    node_uv_points = [mathutils.Vector((node.pos[0], node.pos[1])) for node in interpolated_nodes]
    print(len(node_uv_points))


# Path: pam/pam.py
# SEED = 0
#     SEED = bpy.context.scene.pam_mapping.seed
# def computeUVScalingFactor(obj):
# def map3dPointToParticle(obj, particle_system, location):
# def maskParticle(p_layer, p_index, mask_layer, distance=0.2):
# def distanceToMask(p_layer, p_index, particle_index, mask_layer):
# def computeConnectivityProbability(uv1, uv2, func, args):
# def computeDistance_PreToSynapse(no_connection, pre_index, synapses=[]):
# def sortNeuronsToUV(layer, neuronset, u_or_v):
# def computeMapping(layers, connections, distances, point, debug=False):
# def computeDistanceToSynapse(ilayer, slayer, p_3d, s_2d, dis):
# def addConnection(*args):
# def replaceMapping(index, connection):
# def computeAllConnections():
# def updateMapping(index):
# def computeConnectivity(con, create=True, threads = None):
# def post_neuron_wrapper(x):
# def post_neuron_initializer(players, pconnections, pdistances):
# def pre_neuron_wrapper(x):
# def pre_neuron_initializer(players, pconnections, pdistances, puv_grid, pno_synapses):
# def computeConnectivityThreaded(con, create=True, threads = None):
# def computeConnectivityAll(layers, neuronset1, neuronset2, slayer, connections, distances, func, args):
# def printConnections():
# def computeDistance(layer1, layer2, neuronset1, neuronset2, common_layer,
#                     connection_matrix):
# def measureUVs(objects):
# def initializeUVs():
# def returnNeuronGroups():
# def resetOrigins():
# def initialize3D():


# Path: pam/trees/uv_tree.py
import mstree
import mst_blender
import numpy as np
import bpy
import mathutils
from pam import pam

    # all items are stored in the first list-item of the output
    points = points[0]
    
    # Run mstree
    root_point = mstree.mstree(points, balancing_factor)

    # Convert node positions from uv to 3d
    nodes = mstree.tree_to_list(root_point)
    
    interpolated_nodes = []
    for node in nodes:
        parent = node.parent
        interpolated_nodes.append(node)
        if parent is None:
            continue
        points = interpolate(parent.pos, node.pos, interpolation_resolution)[1:-1]
        print(len(points))
        parent.children.remove(node)
        active_parent = parent
        for point in points:
            new_node = mstree.Node(active_parent, np.array(point), -1)
            interpolated_nodes.append(new_node)
            active_parent = new_node
        node.parent = active_parent
        active_parent.children.append(node)


    node_uv_points = [mathutils.Vector((node.pos[0], node.pos[1])) for node in interpolated_nodes]
    print(len(node_uv_points))


    node_3d_points = pam.mapUVPointTo3d(obj, node_uv_points)