#-----------------------------------------------------------------------
#                   Primitive tsxPython sample script
#                      Beta-sheet (protein motif) 1
#                       deformed cylindrical NURBS
# 
# $Id$
#-----------------------------------------------------------------------

import ptsxpy as p
from math import *

def chkknot1():
    print( "----" )
    nkv = p.NPatchGetNumVKnots( ch1 )
    print( "nkv=", nkv )
    knots1 = p.NPatchGetVKnots( ch1 )
    fp1 = Float_p( knots1 )
    for i in range( nkv ):
        print( "i=", i, fp1.get() )
        fp1.inc()
    print( "----" )

org   = Vec3f( 0., 0., 0. )
xaxis = Vec3f( 1., 0., 0. )
yaxis = Vec3f( 0., 1., 0. )
zaxis = Vec3f( 0., 0., 1. )

t = 0.2 # thickness
r1 = 1.
h1 = 2.
lat_div = 7
long_div = 4

# Make a cylindrical NURBS
ncy1 = p.NURBSCylinderCreate( e_tsxTRUE, r1, r1, h1, 0., 360., e_tsxTRUE, lat_div, long_div, 1, e_tsxFALSE, e_tsxTRUE )
print( "ncy1=", ncy1 )
p.SceneAddObject( ncy1, e_tsxFALSE )

# The true NURBS patch is the cylinder's child.
print( "ncy1=", ncy1 )
ch1 = p.GNodeGetFirstChild( ncy1 ) # This is the NURBS

chkknot1()

# Get number of control points about U (longitude) and V (latitude) directions.
nu = p.NPatchGetNumUCntlPts( ch1 ) # 3 * long_div + 1
nv = p.NPatchGetNumVCntlPts( ch1 ) # 3 * lat_div  + 1 (when no caps)
#
# Get an array of control points ( nu * nv elements)
cps1 = p.NPatchGetCntlPts( ch1 )
print( "nu=", nu, "nv=", nv, "cps1=", cps1 )
p1 = Vec3f_p( cps1 )
sz1 = r1 / 10.
a = r1 / 1000.
b = r1 / 100.
c = h1 / lat_div
d = c / 2.99
e = 0.5 * r1
f = r1 / 2.
g = 1.5 * c
print( "g=", g )
for j in range( nv ):
    for i in range( nu ):
        #
        if abs( abs( p1.x() ) - r1 ) < a  and  abs( abs( p1.y() ) - r1 ) > a:
            p1.set_y( a if p1.y() > 0. else -a )
        if abs( abs( p1.y() ) - r1 ) < a  and  abs( abs( p1.x() ) - r1 ) > a:
            p1.set_x( b if p1.x() > 0. else -b )
        #
        if j == 2 or j == 5:
            p1.set_z( p1.z() + d )
        if j in [ 1, 4, 7 ]:
            p1.set_z( p1.z() - d )
        if j >= 2 and j <= 4:
            if p1.y() > p1.x():
                p1.set_x( p1.x() - e )
                p1.set_y( p1.y() + e )
                p1.set_z( p1.z() + c )
            else:
                p1.set_x( p1.x() + e )
                p1.set_y( p1.y() - e )
                p1.set_z( p1.z() + c )
        if j <= 1:
            if p1.y() > p1.x():
                p1.set_x( p1.x() + f )
                p1.set_y( p1.y() - f )
                p1.set_z( p1.z() + g )
            else:
                p1.set_x( p1.x() - f )
                p1.set_y( p1.y() + f )
                p1.set_z( p1.z() + g )
        #cb1 = p.CreateCube( 1, sz1, sz1, sz1 ) # for debug
        #p.SceneAddObject( cb1, e_tsxFALSE    ) # for debug
        #p,p.GNodeSetLocation( cb1, p1.p )      # for debug
        p1.inc()

chkknot1()

k1 = FloatArray( 3 )
for v1 in [ 0.05, 0.22 ]:
    k1[ 0 ] = v1
    k1[ 1 ] = v1
    k1[ 2 ] = v1
    chk1 = Vec3f_p( k1.p )
    print( "chk1=", chk1.prt() )
    ret = p.NPatchRefineKnotVector( ch1, k1.p, 3, e_tsxNPATCH_KNOT_VECTOR_V )
    print( "NPatchRefineKnotVector() ret=", ret )
#nv = p.NPatchGetNumVCntlPts( ch1 ) # 3 * lat_div  + 1 (when no caps)
#print( "new nv=", nv )

chkknot1()

nu = p.NPatchGetNumUCntlPts( ch1 ) # 3 * long_div + 1
nv = p.NPatchGetNumVCntlPts( ch1 ) # 3 * lat_div  + 1 (when no caps)
cps1 = p.NPatchGetCntlPts( ch1 )
d1 = 0.8 * r1
for j in range( nv ):
    p1 = Vec3f_p( cps1 + nu * j * sizeof_CtsxVector3f )
    for i in range( nu ):
        if j <= 1:
            p1.set_x( p1.x() - 0.2 * d1 )
            p1.set_y( p1.y() - 0.2 * d1 )
        elif j > 13 and ( ( j - 2 ) % 6 <= 2 or j >= nv - 2 ):
            p1.set_x( p1.x() - d1 )
            p1.set_y( p1.y() - d1 )
        p1.inc()


p.GNodeRotate( ncy1, org.p, zaxis.p, pi / 4., e_tsxWorldFrame )
ax1 = Axes3f( (1.,0.,0.),(0.,1.,0.),(0.,0.,1.) )
p.MNodeSetAxesOrientation( ncy1, ax1.p )
vec1 = Vec3f( 0.8, 0.1, 6. )
p.GNodeScale( ncy1, vec1.p, e_tsxWorldFrame )
vec1 = Vec3f( 3., 0., -1. )
p,p.GNodeSetLocation( ncy1, vec1.p )


p.NPatchBuildVisRepAuto( ch1, tsxNPATCH_VR_DRAG )

p.SceneDraw()

