####################################################################################################
#
# Invoking X3D model self-test:
#
#   $ python LineBoatPrototype.py
#
# Python package x3d.py package is available on PyPI for import.
#   This approach simplifies Python X3D deployment and use.
#   https://pypi.org/project/x3d
#
# Installation:
#       pip install x3d
# or
#       python -m pip install x3d
#
# Developer options for loading x3d package in other Python programs:
#
#    from x3d import *  # preferred approach, terser source that avoids x3d.* class prefixes
#
# or
#    import x3d         # traditional way to subclass x3d package, all classes require x3d.* prefix,
#                       # but python source is very verbose, for example x3d.Material x3d.Shape etc.
#                       # X3dToPython.xslt stylesheet insertPackagePrefix=true supports this option.
#
# Project home page:    # X3D Python Scene Access Interface Library (X3DPSAIL)
#                       # https://www.web3d.org/x3d/stylesheets/python/python.html
# Conversion generator: # https://www.web3d.org/x3d/stylesheets/X3dToPython.xslt
#
####################################################################################################

from x3d import *

newModel=X3D(profile='Immersive',version='3.0',
  head=head(
    children=[
    meta(content='LineBoatPrototype.x3d',name='title'),
    meta(content='James Harney',name='creator'),
    meta(content='13 December 2001',name='created'),
    meta(content='20 October 2019',name='modified'),
    meta(content='Simple model of line handling boat used to moor the USS COLE in Aden Harbor, Yemen in October 2000. Aligned down positive x axis as required for Savage vehicle models.',name='description'),
    meta(content='https://www.web3d.org/x3d/content/examples/Savage/Scenarios/UssColeTerroristAttack/TerroristBoatControlPanel.java',name='reference'),
    meta(content='https://www.web3d.org/x3d/content/examples/Savage/Scenarios/UssColeTerroristAttack/TerroristBoatActionInterpreter.java',name='reference'),
    meta(content='http://www.foia.navy.mil/usscole/index.html',name='reference'),
    meta(content='https://www.web3d.org/x3d/content/examples/Savage/Scenarios/UssColeTerroristAttack/LineBoatPrototype.x3d',name='identifier'),
    meta(content='X3D-Edit 3.3, https://www.web3d.org/x3d/tools/X3D-Edit',name='generator'),
    meta(content='../../license.html',name='license')]),
  Scene=Scene(
    children=[
    WorldInfo(title='LineBoatPrototype.x3d'),
    ProtoDeclare(name='LineBoat',
      ProtoInterface=ProtoInterface(
        field=[
        field(accessType='inputOnly',name='startExplosion',type='SFTime')]),
      ProtoBody=ProtoBody(
        children=[
        Group(
          children=[
          Transform(
            children=[
            Viewpoint(DEF='STBD',description='Line Boat stbd View',jump=False,orientation=(1,0,0,-0.2),position=(5,10,35)),
            Viewpoint(DEF='REAR',description='Line Boat Rear View',jump=False,orientation=(0,1,0,-1.57),position=(-30,8,0)),
            Viewpoint(description='Line Boat Port View',jump=False,orientation=(0,1,0,3.14),position=(5,8,-30)),
            Viewpoint(DEF='FRONT',description='Line Boat Front View',jump=False,orientation=(0,1,0,1.57),position=(32,8,0))]),
          PositionInterpolator(DEF='BoatPosit',key=[0.325,0.9,1],keyValue=[(0,0,0),(0,-15,0),(0,-.5,0)]),
          TimeSensor(DEF='BoatPositClock',cycleInterval=3.6,
            IS=IS(
              connect=[
              connect(nodeField='startTime',protoField='startExplosion')])),
          TimeSensor(DEF='DeadBoat',cycleInterval=5),
          OrientationInterpolator(DEF='DeadOrient',key=[0.4,0.9,1],keyValue=[(1,0,0,0.00),(1,0,0,3.14),(1,0,0,3.14)]),
          Transform(DEF='BoatTransform',
            children=[
            Transform(scale=(1.2,1.2,1.2),
              children=[
              Shape(
                geometry=Extrusion(creaseAngle=1.57,crossSection=[(0.00,1.00),(0.38,0.92),(0.71,0.71),(0.92,0.38),(1.00,0.00),(0.92,-0.38),(0.71,-0.71),(0.38,-0.92),(0.00,-1.00)],orientation=[(0,1,0,0),(0,1,0,0),(0,0,1,-0.5)],scale=[(1,1),(1,1),(1,.1)],solid=False,spine=[(0,0,0),(7.8842,0,0),(10.7,0.25,0)]),
                appearance=Appearance(
                  material=Material(diffuseColor=(.9,.9,.9),shininess=.1),
                  texture=ImageTexture(DEF='TEXTURE',url=["../../Scenarios/UssColeTerroristAttack/BoatTexture.jpg","BoatTexture.jpg","https://www.web3d.org/x3d/content/examples/Savage/Scenarios/UssColeTerroristAttack/BoatTexture.jpg"]))),
              Shape(
                geometry=Extrusion(creaseAngle=1.57,crossSection=[(0.10,0.96),(0.10,-0.96)],scale=[(1,1),(1,1),(1,.1)],solid=False,spine=[(.1,0,0),(7.8842,0,0),(10.6,0.25,0)]),
                appearance=Appearance(
                  material=Material(diffuseColor=(1,1,1)),
                  texture=ImageTexture(USE='TEXTURE')))]),
            Transform(translation=(0,-.2,0),
              children=[
              Shape(
                geometry=IndexedFaceSet(coordIndex=[0,2,6,4,0,-1,1,3,7,5,1,-1,0,1,5,4,0,-1,2,3,7,6,2,-1,4,6,7,5,4,-1],solid=False,
                  coord=Coordinate(point=[(2,0,.5),(3,0,.5),(2,0,-.5),(3,0,-.5),(2,1,.5),(3,1.5,.5),(2,1,-.5),(3,1.5,-.5)])),
                appearance=Appearance(
                  material=Material(diffuseColor=(.9,.9,.9))))])]),
          ROUTE(fromField='fraction_changed',fromNode='BoatPositClock',toField='set_fraction',toNode='BoatPosit'),
          ROUTE(fromField='value_changed',fromNode='BoatPosit',toField='set_translation',toNode='BoatTransform'),
          ROUTE(fromField='startTime',fromNode='BoatPositClock',toField='set_startTime',toNode='DeadBoat'),
          ROUTE(fromField='fraction_changed',fromNode='DeadBoat',toField='set_fraction',toNode='DeadOrient'),
          ROUTE(fromField='value_changed',fromNode='DeadOrient',toField='set_rotation',toNode='BoatTransform')])])),
    NavigationInfo(speed=10,type=["EXAMINE","FLY","ANY"],visibilityLimit=7000),
    Background(groundAngle=[1.309,1.570796],groundColor=[(0.1,0.1,0),(0.4,0.25,0.2),(0.6,0.6,0.6)],skyAngle=[1.309,1.571],skyColor=[(0,0.2,0.7),(0,0.5,1),(1,1,1)]),
    ProtoInstance(DEF='Boat',name='LineBoat'),
    Viewpoint(description='Line Boat View',jump=False,orientation=(1,0,0,-0.2),position=(0,10,35)),
    TouchSensor(DEF='Touch',description='Flips boat over on touch'),
    ROUTE(fromField='touchTime',fromNode='Touch',toField='startExplosion',toNode='Boat')])
)

### X3D model conversion complete ###

####################################################################################################
# Self-test diagnostics
####################################################################################################

print('Self-test diagnostics for LineBoatPrototype.py:')
if        metaDiagnostics(newModel): # built-in utility method in X3D class
    print(metaDiagnostics(newModel)) # display meta info, hint, warning, error, TODO values in this model
# print('check newModel.XML() serialization...')
newModelXML= newModel.XML() # test export method XML() for exceptions during export
newModel.XMLvalidate()
# print(newModelXML) # diagnostic

try:
#   print('check newModel.VRML() serialization...')
    newModelVRML=newModel.VRML() # test export method VRML() for exceptions during export
    # print(prependLineNumbers(newModelVRML)) # debug
    print("Python-to-VRML export of VRML output successful", flush=True)
except Exception as err: # usually BaseException
    # https://stackoverflow.com/questions/18176602/how-to-get-the-name-of-an-exception-that-was-caught-in-python
    print("*** Python-to-VRML export of VRML output failed:", type(err).__name__, err)
    if newModelVRML: # may have failed to generate
        print(prependLineNumbers(newModelVRML, err.lineno))

try:
#   print('check newModel.JSON() serialization...')
    newModelJSON=newModel.JSON() # test export method JSON() for exceptions during export
#   print(prependLineNumbers(newModelJSON)) # debug
    print("Python-to-JSON export of JSON output successful (under development)")
except Exception as err: # usually SyntaxError
    print("*** Python-to-JSON export of JSON output failed:", type(err).__name__, err)
    if newModelJSON: # may have failed to generate
        print(prependLineNumbers(newModelJSON,err.lineno))

print("python LineBoatPrototype.py load and self-test diagnostics complete.")
