####################################################################################################
#
# Invoking X3D model self-test:
#
#   $ python BounceTrimesh.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.2',
  head=head(
    children=[
    component(level=2,name='RigidBodyPhysics'),
    meta(content='BounceTrimesh.x3d',name='title'),
    meta(content='Example scene for RigidBodyPhysics component',name='description'),
    meta(content='Alan Hudson and Justin Couch',name='creator'),
    meta(content='Don Brutzman',name='translator'),
    meta(content='1 January 2008',name='created'),
    meta(content='26 December 2008',name='translated'),
    meta(content='20 October 2019',name='modified'),
    meta(content='http://www.xj3d.org/extensions/index.html#Physics',name='reference'),
    meta(content='originals/bounce_trimesh.x3dv',name='reference'),
    meta(content='https://www.web3d.org/x3d/specifications/ISO-IEC-FDIS-19775-1.2-X3D-AbstractSpecification/Part01/components/rigid_physics.html',name='reference'),
    meta(content='under development',name='warning'),
    meta(content='https://www.web3d.org/x3d/content/examples/Basic/RigidBodyPhysics/BounceTrimesh.x3d',name='identifier'),
    meta(content='X3D-Edit 3.3, https://www.web3d.org/x3d/tools/X3D-Edit',name='generator'),
    meta(content='Xj3D converter, http://www.xj3d.org',name='generator'),
    meta(content='../license.html',name='license')]),
  Scene=Scene(
    children=[
    WorldInfo(title='BounceTrimesh.x3d'),
    Viewpoint(position=(0.0,0.0,4.0)),
    Group(
      children=[
      CollidableShape(DEF='BALL-GEOM',
        shape=Shape(
          appearance=Appearance(
            material=Material(emissiveColor=(1.0,0.0,0.0))),
          geometry=Sphere(radius=0.1))),
      CollidableShape(DEF='GROUND-GEOM',translation=(0.0,-0.5,0.0),
        shape=Shape(
          appearance=Appearance(
            material=Material(emissiveColor=(0.0,1.0,0.0))),
          geometry=TriangleSet(ccw=True,colorPerVertex=True,normalPerVertex=True,solid=True,
            coord=Coordinate(point=[(-10.0,0.0,10.0),(10.0,0.0,10.0),(10.0,0.0,-10.0),(-10.0,0.0,10.0),(10.0,0.0,-10.0),(-10.0,0.0,-10.0)]))))]),
    Transform(translation=(1.0,1.0,0.0),
      children=[
      Shape(
        appearance=Appearance(
          material=Material(DEF='INDICATOR-MATERIAL',emissiveColor=(0.0,0.0,1.0))),
        geometry=Sphere(radius=0.2))]),
    RigidBodyCollection(DEF='BODY-COLLECTION',
      bodies=[
      RigidBody(DEF='BODY-1',position=(0.0,2.0,0.0),
        geometry=[
        CollidableShape(USE='BALL-GEOM')])]),
    CollisionSensor(DEF='COLLISION-OUTPUT',
      collider=CollisionCollection(DEF='COLLISION-GROUP',bounce=1.0,minBounceSpeed=1.0,
        collidables=[
        CollidableShape(USE='BALL-GEOM'),
        CollidableShape(USE='GROUND-GEOM')])),
    Script(DEF='CONTACT-SCRIPT',directOutput=True,
      field=[
      field(accessType='inputOnly',name='activeSensor',type='SFBool'),
      field(accessType='outputOnly',name='indicatorColor',type='SFColor'),
      field(accessType='inputOnly',name='set_jointContacts',type='MFNode'),
      field(accessType='outputOnly',name='correctedContacts',type='MFNode'),
      field(accessType='initializeOnly',name='ground',type='SFNode',
        children=[
        CollidableShape(USE='GROUND-GEOM')])],

    sourceCode="""
ecmascript:
    function set_jointContacts(val) {
      for(i = 0; i < val.length; i++) {
        if(val[i].geometry1.equals(ground)) {
	  val[i].geometry1 = null;
	  val[i].body1 = null;
	  val[i].bounce = 1;
	}

        if(val[i].geometry2.equals(ground)) {
	  val[i].geometry2 = null;
	  val[i].body2 = null;
	  val[i].bounce = 1;
	}
      }
      correctedContacts = val;
    }

    function activeSensor(val) {
      if(val) {
        indicatorColor.r = 0;
        indicatorColor.g = 1;
        indicatorColor.b = 0;
      }	else {
        indicatorColor.r = 1;
        indicatorColor.g = 0;
        indicatorColor.b = 0;
      }
    }
"""),
    ROUTE(fromField='contacts',fromNode='COLLISION-OUTPUT',toField='set_jointContacts',toNode='CONTACT-SCRIPT'),
    ROUTE(fromField='isActive',fromNode='COLLISION-OUTPUT',toField='activeSensor',toNode='CONTACT-SCRIPT'),
    ROUTE(fromField='indicatorColor',fromNode='CONTACT-SCRIPT',toField='emissiveColor',toNode='INDICATOR-MATERIAL'),
    ROUTE(fromField='correctedContacts',fromNode='CONTACT-SCRIPT',toField='set_contacts',toNode='BODY-COLLECTION')])
)

### X3D model conversion complete ###

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

print('Self-test diagnostics for BounceTrimesh.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 BounceTrimesh.py load and self-test diagnostics complete.")
