import rhinoscriptsyntax as rs
import sys, random  ###                                              
#sys.path.append("C:/Hue03/")     ### add path where "DM_lib.py" can be found !!!                                              
sys.path.append("P:/DM2/")     ### add path where "DM_lib.py" can be found !!!                                              
#sys.path.append("P:/dm2/")     ### add path where "DM_lib.py" can be found !!!                                              
import DM_lib as dm        ### reload(dm)  
import math  
############################## 

print " ,--. ,--. '  '         ,--.   ,--.        "
print "|  .'   /,--.,--.,--.--.|  |-. `--' ,---.  "
print "|  .   ' |  ||  ||  .--'| .-. ',--.(  .-'  "
print "|  |\   \'  ''  ' |  |   | `-' ||  |.-'  `) "
print "`--' '--' `----' `--'    `---' `--'`----'  "
print "\n"

rs.UnitSystem(3)
dm.PointRadius(displayModeX=1, rad=2, styl=3, verbose=1)

if 1:
    rs.EnableRedraw(0)
    #delete absolutely all
    dm.eA()
    #print "pntRandVec",dm.pntRandVec(-1,1)
    
    anz = 5500
    deltaAngle = 360/anz
    rad = random.uniform(5.0, 10)
    vec = [rad, 0,0]
    #rs.AddPoint(vec)
    #vecX = rs.VectorRotate(vec,29.0,[0,1,1])
    #rs.AddPoint(vecX)
    
    coords = []
    for i in range(anz):
        vecX = rs.VectorRotate(vec, random.uniform(0,270), [0,0,1])
        vecX = rs.VectorRotate(vecX, random.uniform(0,180), [0,1,0])
        coords.append( vecX )
newPoints = rs.AddPoints(coords)

for pnt in newPoints :
    colo = [random.randint(200,255), random.randint(0,160), random.randint(0,10) ]
    rs.ObjectColor( pnt, colo )

pln0 =rs.PlaneFromNormal([0,0,0], [0,1,1], xaxis=[0,1,0])
pln1 =rs.PlaneFromNormal([0,0,0], [-1,-1,0], xaxis=[0,1,0])
plnT = rs.PlaneFromNormal([0,0,-1],[0,-1,-1], xaxis=[0,-1,0])

# Add Arc0
#circ0 = rs.AddCircle( pln0, rad)
circ0 = rs.AddArc(pln0, rad, 180)
rs.RotateObject(circ0, [0,0,0], 90,[0,0,1])
rs.ObjectPrintColor(circ0, [0,200,0])
rs.ObjectPrintWidth(circ0, 0.2 )

#Add ArcT Teeth
circT = rs.AddArc(plnT, rad, 180)
rs.RotateObject(circT, [0,0,0], 90,[0,0,1])
rs.ObjectPrintColor(circT, [0,200,0])
rs.ObjectPrintWidth(circT, 0.2 )

# Add Arc1
circ1 = rs.AddArc(pln1, rad, 180)
rs.RotateObject(circ1, [0,0,0], 90,[0,0,1])
rs.ObjectPrintColor(circ1, [0,200,0])
rs.ObjectPrintWidth(circ1, 0.2 )

        #print deltaAngle*i
#coords upper jaw
dm.printDisplay(state=1, scale=10, thickness=1, color='Display')
lin0 = rs.AddLine( rs.CurveStartPoint(circ0), rs.CurveEndPoint(circ0))
rs.ObjectPrintColor(lin0, [0,200,220])
rs.ObjectPrintWidth(lin0, 0.2 )
coordsC = rs.DivideCurve(circ0, 1001, 0)
coordsL = rs.DivideCurve(lin0, 1001, 0)

#coords teeth
dm.printDisplay(state=1, scale=10, thickness=1, color='Display')
coordsCT = rs.DivideCurve(circT, 1001, 0)

#coords lower jaw
lin1 = rs.AddLine( rs.CurveStartPoint(circ0), rs.CurveEndPoint(circ0))
rs.ObjectPrintColor(lin1, [0,200,220])
rs.ObjectPrintWidth(lin1, 0.2 )
coordsC1 = rs.DivideCurve(circ1, 1001, 0)
coordsL1 = rs.DivideCurve(lin1, 1001, 0)

teethCoords1 = []
teethCoords2 = []
#teethCoords = []
fuellCoords = []
for i in range(1001):
    pC = random.choice(coordsC)
    pL = random.choice(coordsL)
    pC1 = random.choice(coordsC1)
    pL1 = random.choice(coordsL1)
    pT = random.choice(coordsCT)
    #rs.AddCurve( [pC, pL] )
    pX = dm.pntInbetween(pC, pL, random.uniform(0,1))
    fuellCoords.append( pX )
    if i > 400 and i < 600:
        teethCoords1.append(pC)
        teethCoords2.append(pT)
    pX1 = dm.pntInbetween(pC1, pL1, random.uniform(0,1))
    fuellCoords.append( pX1 )
rs.ObjectColor(rs.AddPoints(fuellCoords), [60,170,15] )
    
for i in range(100):
            rs.AddLine(teethCoords1[i],teethCoords2[i+1])
    
#xRange = int(rad)*2
#yRange = random.randint(7,13)
#zRange = random.randint(7,13)
#dm.PointRadius(displayModeX=1, rad=2, styl=3, verbose=1)

#count = -2
#start = -2
#end = -1
#newCoords = []
#for x in range(xRange):
#     #if x > xRange * 0.5:
#        x += -rad+1
#        for y in range(yRange):
#                y += 1
#                for z in range(zRange):
#                            cor = [-x,-y,-z]
#                            #print cor
#                            newCoords.append(cor)
#                            #dotted line
#                            count += 1
#                            start += 1
#                            end +=1
##                            print "count",count
##                            print "start",start
##                            print "end",end
##                            print "count mod 3",count % 3
#                            if count % 3 == 0:
#                                l1 = newCoords[start]
#                                l2 = newCoords[end]
#                                rs.AddLine(l1,l2)
#                            
#rs.AddPoints(newCoords)
#print len(newCoords)
#c1 = newCoords[0]
#c2 = newCoords[1]
#rs.AddLine(c1,c2)
#rs.Addline(newCoords[0],newCoords[1])
#rs.AddCurve(newCoords)
#shuffle#
#random.shuffle(newCoords)
#rs.AddCurve(newCoords)

def check(cx, cy, cz, x, y, z, ):
     
    x1 = math.pow((x-cx), 2)
    y1 = math.pow((y-cy), 2)
    z1 = math.pow((z-cz), 2)
    return (x1 + y1 + z1) # distance between the centre and given point

#Auge
def DistributeCirclesOnSphere(position):
    sphere_radius = 2.0#rs.GetReal("Radius of sphere", 10.0, 0.01)
    if not sphere_radius: return

    circle_radius = 0.1#rs.GetReal("Radius of packing circles", 0.05*sphere_radius, 0.001, 0.5*sphere_radius)
    if not circle_radius: return

    vertical_count = int( (math.pi*sphere_radius)/(2*circle_radius) )

    rs.EnableRedraw(False)
    phi = -0.5*math.pi
    phi_step = math.pi/vertical_count
    while phi<0.5*math.pi:
        horizontal_count = int( (2*math.pi*math.cos(phi)*sphere_radius)/(2*circle_radius) )
        if horizontal_count==0: horizontal_count=1
        theta = 0
        theta_step = 2*math.pi/horizontal_count
        #print theta_step
        while theta<2*math.pi-1e-8:
#            print 2*math.pi-1e-8
            eye_point = (sphere_radius*math.cos(theta)*math.cos(phi)+(position/2), sphere_radius*math.sin(theta)*math.cos(phi)+(position/2), sphere_radius*math.sin(phi)-(position/2))
#            circle_normal = rs.PointSubtract(eye_point, (0,0,0))
#            circle_plane = rs.PlaneFromNormal(eye_point, circle_normal)
#            rs.AddCircle(circle_plane, circle_radius)
#            print eye_point
            ans = check(0,0,0,eye_point[0],eye_point[1],eye_point[2])
            if ans>(rad**2) or ans==(rad**2):
                rs.AddPoint(eye_point)
            theta += theta_step
        phi += phi_step
    rs.EnableRedraw(True)
    
DistributeCirclesOnSphere(position=(-rad))


    
def DistributeCirclesOnSphere1(position):
    sphere_radius = 2.0#rs.GetReal("Radius of sphere", 10.0, 0.01)
    if not sphere_radius: return

    circle_radius = 0.1#rs.GetReal("Radius of packing circles", 0.05*sphere_radius, 0.001, 0.5*sphere_radius)
    if not circle_radius: return

    vertical_count = int( (math.pi*sphere_radius)/(2*circle_radius) )

    rs.EnableRedraw(False)
    phi = -0.5*math.pi
    phi_step = math.pi/vertical_count
    while phi<0.5*math.pi:
        horizontal_count = int( (2*math.pi*math.cos(phi)*sphere_radius)/(2*circle_radius) )
        if horizontal_count==0: horizontal_count=1
        theta = 0
        theta_step = 2*math.pi/horizontal_count
        #print theta_step
        while theta<2*math.pi-1e-8:
#            print 2*math.pi-1e-8
            eye_point = (sphere_radius*math.cos(theta)*math.cos(phi)-(position/2), sphere_radius*math.sin(theta)*math.cos(phi)+(position/2), sphere_radius*math.sin(phi)-(position/2))
            #print eye_point
            ans = check(0,0,0,eye_point[0],eye_point[1],eye_point[2])
            if ans>(rad**2) or ans==(rad**2):
                rs.AddPoint(eye_point)
            theta += theta_step
        phi += phi_step
    rs.EnableRedraw(True)
    
DistributeCirclesOnSphere1(position=(-rad))

#(x - cx)^2 + (y - cy)^2 + (z - cz)^2 < r^2 

     