##############################################
### Umgang
##############################################


##############################################
import rhinoscriptsyntax as rs
import random, sys
import sys
sys.path.append("P:/WWW/coburger/dm2/")     ### add path to rhino's default/search path
sys.path.append("P:/")                   ### for all and _diag
import dm2_lib as dm2
##############################################
#reload (dm2)

rs.UnitSystem(4)                                        # km = 5, meters = 4, cm = 3 etc
rs.Command("-groundplane Options On=No enter enter", 0) # groundplane > 0 = off
rs.ShowGrid(None, 0)                                    # grid > 0 = off
rs.ShowGridAxes(None, 0)                                # y/y/z axen display > 0 = off
rs.ViewDisplayMode(view=None, mode="Ghosted")			# shadeMode fuer current view
rs.Command("cplane w t enter", 0)						# cPlane World Top
rs.Command("PrintDisplay State=Off Color=Display Thickness=40 enter", 0)


rs.EnableRedraw(0)
rs.UnselectAllObjects()
dm2.newEmptyLayer( "LODGE", [32, 32, 32] )
dm2.newEmptyLayer( "LODGE::crv", [200,  100, 20] )
dm2.newEmptyLayer( "LODGE::srf", [ 100,  100, 200] )
dm2.newEmptyLayer( "LODGE::tmp",  [210,  32, 200] )
rs.Redraw()

##############################################
""" 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 = dm2.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 = dm2.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 once
#dm2.allData = dm2.getLodge( demo=0, verbose=0 )
### exec once


allData = dm2.allData
if 0:
    i=1
    data = allData[i]
    centers = []
    for j, coords in enumerate(data[0:]):
        rs.AddCurve( coords, 1 )
        centers.append(dm2.pntCentroid(coords))
        if j==0 or j==2: dm2.textDots( coords)
    dm2.textDots(centers)
if 0:
    for i,coords in enumerate(allData[1]):
        rs.AddCurve( coords, 1 )
        if i%4==0: 
            rs.AddCurve( coords, 2 )

myPanel = [[2,0,0], [random.uniform(0,0.5), 0,0]]
if 0:
    rs.CurrentLayer("LODGE::tmp")
    sections = allData[4]
    rs.AddCurve( sections[0], 1 )
    rs.AddCurve( sections[-1], 1 )
    rs.ObjectColor(rs.AllObjects()[:2], [100,100,120])
    rs.ObjectName( rs.AllObjects()[:2], "lodgeSections")
    #dm2.eAA()
    myPanel = allData[0][0]
    #dm2.textDots( myPanel )
    #rs.AddCurve( myPanel, 1 )

def myPanel( myPanel=myPanel, anz=10, showPnts=1 ):
    p0 = myPanel[0]
    p1 = myPanel[1]
    p2 = myPanel[2]
    p3 = myPanel[3]
    crv = rs.AddCurve( myPanel, 2 )
    rs.HideObject( crv )
    cen = dm2.pntCentroid( myPanel )
    plnX = rs.PlaneFromPoints(cen, p0, p1)
    param  = rs.CurveClosestPoint( crv, cen)
    ptX = rs.EvaluateCurve( crv, param )
    rad = rs.Distance( cen, ptX )
    #rs.AddPoint( ptX )
    circ = rs.AddCircle(plnX, rad*.9)
    crv = rs.AddCurve( [p0, p1, p1, p2,  p3, p3, p0], 3)
    coordsCirc = rs.DivideCurve( circ, anz, showPnts)
    coordsCrv = rs.DivideCurve( crv, anz, showPnts)
    for i in range( anz ):
        rs.AddLine( coordsCirc[i], coordsCrv[i] )
if 0:
    for i,panel in enumerate(allData[3]):
        #myPanel(panel, anz=i*8+8,  showPnts=0)
        myPanel(panel, anz=random.randint(8,8**2),  showPnts=0)


print len(allData)
print len(allData[0])


panelX = allData[0][0]

def myPanel ( myPanel=panelX, radFac = 0.45, divi = 8, normDist=0.5 ):
    print len(myPanel)
    rs.AddCurve(myPanel, 1)
    rs.AddCurve(myPanel, 2)
    coords = myPanel[:]
    
    p0=myPanel[0]
    p1=myPanel[1]
    p2=myPanel[2]
    p3=myPanel[3]
    
    vec0 = rs.VectorSubtract(p1,p0)
    vec1 = rs.VectorSubtract(p3,p0)
    vec = rs.VectorCrossProduct( vec0, vec1)
    vec = rs.VectorUnitize( vec)
    vec = rs.VectorScale(vec, normDist ) 
    
    
    coords.append(coords[0])
    elli = rs.AddCurve(coords, 1 )
    centerX = dm2.pntCentroid( myPanel)
    centerX = rs.VectorAdd( centerX, vec)
    rs.AddPoint( centerX )
    rad = rs.Distance(p0, p1)*radFac
    plnX = rs.PlaneFromPoints(centerX, p0, p1 )
    
    circ = rs.AddCircle( plnX, rad )
    coordsC = rs.DivideCurve( circ, divi, 0)
    coordsE = rs.DivideCurve( elli, divi, 0)
    rs.DeleteObject( elli )
    
    for i in range(divi):
        rs.AddLine(coordsC[i], coordsE[i])
        
    #dm2.textDots( myPanel )
    
    
for i, panelX in enumerate(allData[1][0:8]):
    if i%2==0:
        myPanel(myPanel=panelX, radFac=0.45, divi=i*2, normDist=-0.05*i )
    else:
        myPanel(myPanel=panelX, radFac=0.45, divi=i*2, normDist= 0.05*i )

########## EOS
dm2.eDup()
dm2.printDisplay(1)
rs.CurrentLayer("Default")