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 "       arcade game - ghosts - Namco"

print "================================================."
print "     .-.   .-.     .--.                         |"
print "    | OO| | OO|   / _.-' .-.   .-.  .-.   .''.  |"
print "    |   | |   |   \  '-. '-'   '-'  '-'   '..'  |"
print "    '^^^' '^^^'    '--'                         |"
print "===============.  .-.  .================.  .-.  |"
print "\n"


rs.ViewDisplayMode(view=None, mode="wireframe")
rs.ShowGrid(view=None, show=0)
rs.Command("cplane w t enter", 0)						# cPlane World Top

#dm.PointRadius(displayModeX=0, rad=6, styl=3, verbose=0)        # 2 square; 3 round
#dm.PointRadius(displayModeX=0, rad=5, styl=0, verbose=0) # 0 wireframe
#dm.PointRadius(displayModeX=1, rad=17, styl=2, verbose=0) # 1 shaded
#dm.PointRadius(displayModeX=2, rad=9, styl=3, verbose=1) # rendered

dm.printDisplay(0)                                      # nomen est omen
rs.EnableRedraw(0)

rs.UnitSystem(3)

dm.eA()
rs.Redraw()
#dm.PointRadius(displayModeX=1, rad=2, styl=0, verbose=1)

if 1:
    rs.EnableRedraw(0)
    #delete absolutely all
    dm.eA()
   
    anz = 5500
    deltaAngle = 360/anz
    rad = random.uniform(5.0, 10)
    vec = [rad, 0,0]
    
    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 )

#dm.printDisplay(state=1, scale=100, thickness=100)

#dm.PointRadius(displayModeX=0, rad=3, styl=3, verbose=1)
newPoints = rs.AddPoints(coords)
#for i in range( 100 ):
#    rs.Command("setobjectdisplaymode -selID "+str(newPoints[i])+" enter \"shaded\" enter", 2)


for pnt in newPoints :
    colo = [random.randint(200,255), random.randint(200,255), random.randint(0,50) ]
    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, [255,255,255])
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, [255,255,255])
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, [255,255,255])
rs.ObjectPrintWidth(circ1, 0.2 )

#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, [255,0,0])
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, [255,0,0])
rs.ObjectPrintWidth(lin1, 0.2 )
coordsC1 = rs.DivideCurve(circ1, 1001, 0)
coordsL1 = rs.DivideCurve(lin1, 1001, 0)

teethCoords1 = []
teethCoords2 = []
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)
    pX = dm.pntInbetween(pC, pL, random.uniform(0,1))
    fuellCoords.append( pX )
    if i > 300 and i < 700:
        teethCoords1.append(pC)
        teethCoords2.append(pT)
    pX1 = dm.pntInbetween(pC1, pL1, random.uniform(0,1))
    fuellCoords.append( pX1 )
rs.ObjectColor(rs.AddPoints(fuellCoords), [255,0,0] )
    
for i in range(398):
            tCLine = rs.ObjectColor(rs.AddLine(teethCoords1[i],teethCoords2[i+1]),[255,255,255])

#dm.PointRadius(displayModeX=0, rad=5, styl=3, verbose=1)

#(x - cx)^2 + (y - cy)^2 + (z - cz)^2 < r^2 
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
        while theta<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))
            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)
            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))
            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)
    

#dm.PointRadius(displayModeX=1, rad=2, styl=3, verbose=1)

DistributeCirclesOnSphere(position=(-rad))


#Poster Pacman auf Labyrinthwand
if 1:
    grayScale = 0
    makeEleva = 2
    makeCurve = 0
    intervall = 3
    img_name = "pacman_kl.jpg"
    img_path = img_name
    ########
    pixels = dm.getPixels(img_path, steps_x=intervall, steps_y=intervall)
    ### make 3D_grafik from pixels_lists:
allCoords = []

for y,row in enumerate(pixels[0:]): ### enumerate = generate counter 0,1,2,3,4,5...
    coords = []
    for x,col in enumerate(row[0:]):
        dm.esc()
        avg = int( (col[0] + col[1] + col[2])/3 ) ### average, for gray_value
        if grayScale:
            col = [avg, avg, avg]
        cor = [(rad)*2*0.5+17, x, y]
        if 1 :
            rs.ObjectColor( rs.AddPoint( cor ), col)

rand = random.uniform(20, -10)
#print "cor",cor
newCoords = []
def labyrinth(xRange,yRange,zRange,neg,dist):
        for x in range(xRange):
             if x > xRange * 0.5:
                x += dist
                for y in range(yRange):
                        for z in range(zRange):
                                if neg == 0:
                                    cor = [x,y,z]
                                    newCoords.append(cor)
                                    cor = [x,y,-z]
                                    newCoords.append(cor)
                                else:
                                    cor = [x,-y-1,z]
                                    newCoords.append(cor)
                                    cor = [x,-y-1,-z]
                                    newCoords.append(cor)
        #print "newCoords",newCoords
        newPoints = rs.AddPoints(newCoords)
        x = 0
        #rennt ewig :(
#        for i in range( len(newPoints) ):
#            rs.Command("setobjectdisplaymode -selID "+str(newPoints[i])+" enter \"shaded\" enter", 1)
        for pnt in newPoints :
            colo = [random.randint(0,100), random.randint(0,100), random.randint(200,255) ]
            rs.ObjectColor( pnt, colo )
                
dm.PointRadius(displayModeX=0, rad=3, styl=3, verbose=1)
labyrinth(int(rad)*2,random.randint(70,80),20,0,17)
labyrinth(random.randint(10,30),random.randint(30,80),20,0,40)
labyrinth(random.randint(10,20),random.randint(60,70),20,1,60)

#Punkte-Kugel - Anzahl der Punkte mal 4
vec = [3, 0,0]
vecX = rs.VectorRotate(vec,29.0,[0,1,1])
anz = 500
deltaAngle = 360/anz
#Kugel verschieben 
center = [0,-30,0]
curvepoints = []
for i in range(anz):
        vecX = rs.VectorRotate(vec,deltaAngle*i,dm.pntRandVec(-1,1)[0])
        vecX = rs.VectorScale(vecX,random.uniform(0,1))
        vecX = rs.VectorAdd(vecX, center)
        newCoords = rs.AddPoint(vecX)
        curvepoints.append(newCoords)

newPoints = rs.AddPoints(curvepoints)
curve  = rs.AddCurve(newPoints, 1)
rs.ObjectColor(rs.AddTextDot(str(anz*4), newCoords), [130,0,150])
