import numpy as np
import matplotlib.pyplot as plt

from cellcomplex.property_topomesh.example_topomesh import circle_voronoi_topomesh
from cellcomplex.property_topomesh.analysis import compute_topomesh_property

from cellcomplex.property_topomesh.utils.matplotlib_tools import mpl_draw_topomesh, mpl_draw_incidence_graph

from cellcomplex.property_topomesh.utils.delaunay_tools import delaunay_triangulation_topomesh

np.random.seed(108)
_,topomesh = circle_voronoi_topomesh(2,circle_size=8,return_triangulation=True)

figure = plt.figure(0)
figure.clf()

figure.add_subplot(1,2,1)
figure.gca().axis('equal')
mpl_draw_topomesh(topomesh,figure,2,color='g')
mpl_draw_topomesh(topomesh,figure,1,color='b')
mpl_draw_topomesh(topomesh,figure,0,color='m')
figure.gca().axis('off')

compute_topomesh_property(topomesh,'neighbors',0)
vertex_neighbors = topomesh.wisp_property('neighbors',0).values()

coef = 0.2

vertex_positions = topomesh.wisp_property('barycenter',0).values()
vertex_neighbor_positions =  topomesh.wisp_property('barycenter',0).values(vertex_neighbors)
vertex_neighbor_center = np.array([np.mean(p,axis=0) for p in vertex_neighbor_positions])
new_vertex_positions = vertex_positions + coef*(vertex_neighbor_center-vertex_positions)

for p,n_p in zip(vertex_positions,vertex_neighbor_center):
    figure.gca().plot([p[0],n_p[0]],[p[1],n_p[1]],color='gold',zorder=6,alpha=0.33)
figure.gca().scatter(vertex_neighbor_center[:,0],vertex_neighbor_center[:,1],color='gold',s=10,alpha=0.33)
figure.gca().scatter(new_vertex_positions[:,0],new_vertex_positions[:,1],color='gold',s=20,zorder=7)

topomesh.update_wisp_property('barycenter',0,new_vertex_positions)

figure.add_subplot(1,2,2)
figure.gca().axis('equal')
mpl_draw_topomesh(topomesh,figure,2,color='g')
mpl_draw_topomesh(topomesh,figure,1,color='b')
mpl_draw_topomesh(topomesh,figure,0,color='m',plot_ids=True)
figure.gca().axis('off')

figure.set_size_inches(10,5)
figure.tight_layout()