#######################################
### DM2_w21 # AGruber@tugraz.at     ###
### hu_04_startUP # tool4_tetraeder ###
#######################################

##############################
import rhinoscriptsyntax as rs
import random, time, itertools         ### add (your !) path to rhino's default/search path
import dm2_lib as dm2
##############################


rs.UnitSystem(4)                                        # km = 5, meters = 4, cm = 3 etc
rs.Command("-groundplane Options On=No enter enter", 0) # groundplane > off
rs.ShowGrid(None, 0)
rs.ShowGridAxes(None, 0)
rs.EnableRedraw(0)
rs.ViewDisplayMode(view=None, mode="Rendered")
dm2.eA()                ### same as: rs.DeleteObjects( rs.AllObjects() )
rs.Redraw()

print "                            /\\       "
print "                           /_|\\      "
print "                      TETR/  |_\\EDER _01"
print "........................./   |  \\....... h = a/3.0*(6.0**0.5) .. \" a drittel mal wurzel aus 6 \"\n"


pointsX = [[0, 3, 0], [3, 0, 0], [3, 3, 0]]
if 0:
    ptA = dm2.pntRandVec(-3, 3)
    ptB = dm2.pntRandVec(-3, 3)
    ptX = dm2.pntRandVec(-3, 3)
    dm2.pointsX = [ptA, ptB, ptX]
    pointsX = [ptA, ptB, ptX]

tetraCoords1 = dm2.getTetraCoords( points=pointsX, flipH=0, flipAB=0, drawTetra=1, drawAll=0 )
#tetraCoords = dm2.getTetraCoords( points=pointsX, flipH=0, flipAB=1, drawTetra=1, drawAll=2 )

#dm2.getTetraCoords( tetraCoords[2], flipH=0, flipAB=0, drawTetra=1, drawAll=2 )

if 0: ### test / visualize
    
    for i, FaceCoordList in enumerate(tetraCoords1):
        print "face", i, 
        print FaceCoordList
    
    print
    for i, cor in enumerate(tetraCoords1[0]):
        print "cor", i, 
        print cor

    #rs.DeleteObjects( rs.ObjectsByType( 1 + 8192 ) )

    rs.AddSrfPt(tetraCoords1[3])         ### achtung surface - gaaaanz boeze !
    rs.AddPoints( tetraCoords1[1] )      ### .. just 4 fun & test
    rs.ObjectColor( rs.AllObjects()[:4], [0,0,200] )

#______here comes my tool for tetra_pak:

for i in range (0,4):
    
    pointsX = []
    SPts = tetraCoords1[i]
    SPt1 = SPts[0]
    SPt2 = SPts[1]
    SPt3 = SPts[2]
    
    pointsX.append (SPt1)
    pointsX.append (SPt2)
    pointsX.append (SPt3)

    if i == 0: 
    	tetraCoords = dm2.getTetraCoords( points=pointsX, flipH=0, flipAB=1, drawTetra=1, drawAll=0 )
    else: 
    	dm2.getTetraCoords( points=pointsX, flipH=0, flipAB=0, drawTetra=1, drawAll=0 )


    tetraCoords2 = dm2.getTetraCoords( points=pointsX, flipH=0, flipAB=0, drawTetra=1, drawAll=0 )

    pointsY = []
    SecPts = tetraCoords2[i]
    SecPt1 = SecPts[0]
    SecPt2 = SecPts[1]
    SecPt3 = SecPts[2]

    pointsY.append (SecPt1)
    pointsY.append (SecPt2)
    pointsY.append (SecPt3)

    if i == 0: 
    	tetraCoords = dm2.getTetraCoords( points=pointsY, flipH=0, flipAB=1, drawTetra=1, drawAll=0 )
    else: 
    	dm2.getTetraCoords( points=pointsY, flipH=0, flipAB=0, drawTetra=1, drawAll=0 )


    tetraCoords3 = dm2.getTetraCoords( points=pointsY, flipH=0, flipAB=0, drawTetra=1, drawAll=0 )

    pointsZ = []
    trdPts = tetraCoords3[i]
    trdPt1 = trdPts[0]
    trdPt2 = trdPts[1]
    trdPt3 = trdPts[2]

    pointsZ.append (trdPt1)
    pointsZ.append (trdPt2)
    pointsZ.append (trdPt3)

    if i == 0: 
    	tetraCoords3 = dm2.getTetraCoords( points=pointsZ, flipH=0, flipAB=1, drawTetra=1, drawAll=0 )
    else: 
    	dm2.getTetraCoords( points=pointsZ, flipH=0, flipAB=0, drawTetra=1, drawAll=0 )
        

rs.DeleteObjects(rs.ObjectsByType(1))

#______EOC

###############################################
#___4_lazy_experts: automated capture to LW P:/
if 1:
    rs.ZoomExtents()
    rs.Command("-zoom Factor 0.95 enter", 0) # decrease zoom for better framingg
    rs.Redraw()
if  0:
    path = "P:/hu_04/images/"
    name = "tst.png"
    rs.Command("-viewCaptureToFile Width=480  Height=360  Scale=1.5 DrawCPlaneAxes=No TransparentBackground=Yes "+path+name, 0)
    # .jpg and Scale=4 for webSite submission
    name = "tst_"+str(int(time.time()))+".jpg"
    rs.Command("-viewCaptureToFile Width=480  Height=360  Scale=4 TransparentBackground=No  "+path+name, 0)