###################################
### DM2_w22 # AGruber@tugraz.at ###
### ue_04 pixel_2_vector demo   ###
###################################

##############################
import math
import rhinoscriptsyntax as rs      ###
import random, time, sys            ###
sys.path.append("P:/")              ### add LW P:/ to rhino's default search path AND
sys.path.append("P:/WWW/default114/dm2/")     ### add YOUR LW P:/WWW/user/ to rhino's default search path ...
import DM2_lib as dm                ### ... thus DM2_lib.py can be found !
##############################      reload (dm)


rs.UnitSystem(4)                                        # km = 5, meters = 4, cm = 3 etc
rs.ShowGrid(None, 0)                                    # grid > 0 = off
rs.ShowGridAxes(None, 1)                                # y/y/z axen display > 0/1 = off/on
rs.ViewDisplayMode(view=None, mode="Ghosted")			# shadeMode fuer current view
rs.Command("cplane w t enter", 0)						# cPlane World Top
dm.printDisplay(0)                                      # nomen est omen

rs.EnableRedraw(0)                                      ### 4_the_MACs: try 0/1 !
rs.UnselectAllObjects()
dm.newEmptyLayer( "Default")
dm.newEmptyLayer( "LODGE", [32, 32, 32] )
dm.newEmptyLayer( "LODGE::crv", [200,  20, 20] )
dm.newEmptyLayer( "LODGE::pnt", [ 100,  100, 100] )
### dm.newEmptyLayer( "LODGE::srf", [ 100,  100, 200] ) ### nixi_dixi :)
dm.newEmptyLayer( "LODGE::tmp",  [120,  120, 200] )
rs.Redraw()

def info():
    ##############################################
    """ LODGE
    Generate a small archetypal house, distorted along perspective algorithms
    A setup for the development of individual panels
    Inspired by
      Albrecht Duerer's "Underweysung" (1525)
      https://de.wikipedia.org/wiki/Perspektive#Geschichte
      https://en.wikipedia.org/wiki/Albrecht_Duerer
    And
      MVRDVs "Porterlodges for the National Park De Hoge Veluwe" (1996)
      https://mvrdv.com/projects/167/hoenderloo-lodge?photo=15877
      https://www.miesarch.com/work/2994
    Parameters:
      p_width (float, optional): Approx. panel width in units
      l_dist (float, optional): Approx. length of the lodge in units
      d_dist (float, optional): Approx. depth of the lodge in units
      h_dist (float, optional): Approx. height of the lodge in units
      x_pos (float, optional): Move origin of the lodge in x
      y_pos (float, optional): Move origin of the lodge in y
      d_grid (boolean, optional): Fixed depth grid on the the small sides
      d_distord (boolean, optional): Distord basic section of the house
      demo (boolean, optional): Run demo to get an object
      verbose (int, optional): talk & sleep / Print and depict generation of the lodge
    Returns:
      return[0] = list[][][] of all panels, containing groups of 4 points
      return[1] = list[][][] of just the sleeve's panels
      return[2] = list[][][] of just the right section's panels
      return[3] = list[][][] of just the left section's panels
      return[4] = list[][][] of the sections, containing groups of 5 points
      return[5] = list[][] of all the cvs on the sleeve
      return[6] = list[][] of all the cvs on the right section
      return[7] = list[][] of all the cvs on the left section
    Example:
      allData = dm.getLodge( verbose=1000, demo=1 )
      panels = allData[0]
      panels_sleeve = allData[1] // sleeve ~= hull
      panels_right = allData[2]
      panels_left = allData[3]
        sections = allData[4]
        cvs_sleeve= allData[5]
        cvs_right= allData[6]
        cvs_left= allData[7]
    ### allData = dm.getLodge( p_width=1.2, l_dist=10, d_dist=8, h_dist=6, pos_x=0, pos_y=0, d_grid=0, d_distord=1, demo=0, verbose=0 )
    """


### exec whenever ##################################
rs.CurrentLayer("LODGE::crv")                   ###
dm.allLodgeData = dm.getLodge( demo=0, verbose=0 )   ### activate this to get a new set of data
allLodgeData = dm.allLodgeData                            ### this set of data is generated @ startUp / when dm2_lib is loaded
####################################################

rs.CurrentLayer("LODGE::tmp")

if 1:                                           ### DEMO 0/1
    def myPanel( panelX ):
        pass
        rs.AddCurve( panelX, 1 )
        if i==0: dm.textDots( panelX )

    for i,panel in enumerate( allLodgeData[0]):
        pass
        myPanel( panel )

#_____________________________here you go:
    dm.eA()
def myPanel( panelX ):
    pass
    
for i,panel in enumerate( allLodgeData[0]):
    pass

for i,panel in enumerate( allLodgeData[0]):
    pass

#rs.AddCurve(allLodgeData[0][0])
#rs.AddCurve(allLodgeData[0][0],1)
#rs.textDots(allLodgeData[0][0])

def myPanel(coords,distVor = -2, amount = 5):
    #rs.AddCurve(coords,1)
    
    ##create centerpoints on both sides
    cor_01 = [coords[1],coords[2]]
    cor_02 = [coords[0],coords[3]]
    cen_01 = dm.pntCentroid(cor_01)
    cen_02 = dm.pntCentroid(cor_02)
    #rs.AddPoint(cen_01)
    
    ##define vectos
    nVec = dm.normVec3pnts(coords[0],coords[1],coords[2])
    cenVec = rs.VectorCreate(cen_02,cen_01)
    pVec_01 = rs.VectorCreate(coords[0],coords[1])
    pVec_02 = rs.VectorCreate(coords[3],coords[2])
    
    ##create waves
    for i in range(amount+1):
        
        ###define position of curves on relative x-axis
        divider = i/amount
        cenDis = rs.VectorScale(cenVec,divider)
        dis_01 = rs.VectorScale(pVec_01,divider)
        dis_02 = rs.VectorScale(pVec_02,divider)
        
        pCen = rs.VectorAdd(cen_01,cenDis)
        p01 = rs.VectorAdd(coords[1],dis_01)
        p02 = rs.VectorAdd(coords[2],dis_02)
        
        ###define wave along relative x
        #print i
        #if i <= amount/2:
        cos = []
        for e in range(180):
            cos.append( (abs(math.cos(math.radians(e)))))
            #print cos
        #print e
        #print i
        pos = int(round((len(cos)-1)*(i/amount),0))
        #print pos
        factor = cos[pos]
        distV = distVor*factor #/v01

        ###apply relative y
        pDist = rs.VectorAdd(pCen,rs.VectorScale(nVec,distV))
        rs.AddCurve([p01,pDist,p02],2)

##add all panels
for i,panel in enumerate(allLodgeData[1]):
    #coords = allLodgeData[0][0]
    #print i
    if i%2==0:
        i= i*-1 
    if i > 0:
        f = 1
    else:
        f = -1
    myPanel(panel,distVor = f ,amount = 10)


#____________________________:here you end

########## EOS / EndOfScript
rs.EnableRedraw(1)  ### 4_the_MACs
dm.eDup()           ### delete duplocate objects 
dm.printDisplay(1)
rs.ZoomExtents()
rs.CurrentLayer("Default")