# 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
    print(no_interpolations)
    if no_interpolations < 1:
        return [p1.to_tuple(), p2.to_tuple()]
    ps = np.linspace(0., 1., no_interpolations)
    new_points = []
    for p in ps:
        p_new = p1.lerp(p2, p)
        new_points.append(p_new.to_tuple())
    print(len(new_points))
    return new_points

def export_swc(root_node, outfilename, structure_identifier = 2):
    nodes = mstree.tree_to_list(root_node)
    f = open(outfilename, 'w')
    index = 0
    for node in nodes:
        node.index = index
        index += 1

        if node.parent is not None:
            parent_index = node.parent.index
        else:
            parent_index = -1

        f.write(' '.join((str(node.index), str(structure_identifier), str(node.pos[0]), str(node.pos[1]), str(node.pos[2]), str(0), str(parent_index))))
        f.write('\n')
    f.close()
    
def createAxons(p_obj, s_obj, quantity, mean, variance):
    for p in p_obj.particle_systems[0].particles:

# 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

    print(no_interpolations)
    if no_interpolations < 1:
        return [p1.to_tuple(), p2.to_tuple()]
    ps = np.linspace(0., 1., no_interpolations)
    new_points = []
    for p in ps:
        p_new = p1.lerp(p2, p)
        new_points.append(p_new.to_tuple())
    print(len(new_points))
    return new_points

def export_swc(root_node, outfilename, structure_identifier = 2):
    nodes = mstree.tree_to_list(root_node)
    f = open(outfilename, 'w')
    index = 0
    for node in nodes:
        node.index = index
        index += 1

        if node.parent is not None:
            parent_index = node.parent.index
        else:
            parent_index = -1

        f.write(' '.join((str(node.index), str(structure_identifier), str(node.pos[0]), str(node.pos[1]), str(node.pos[2]), str(0), str(parent_index))))
        f.write('\n')
    f.close()
    
def createAxons(p_obj, s_obj, quantity, mean, variance):
    for p in p_obj.particle_systems[0].particles:

        uv = pam.map3dPointToUV(p_obj, s_obj, p.location)