####################################################################################################
#
# Invoking X3D model self-test:
#
#   $ python rccart2.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='Interactive',version='4.0',
  head=head(
    children=[
    meta(content='rccart2.x3d',name='title'),
    meta(content='fix naming, cartilage?',name='TODO'),
    meta(content='cartilage model, TODO finish',name='description'),
    meta(content='Don Brutzman, Joe Williams, John Carlson, Damon Hernandez',name='creator'),
    meta(content='Don Brutzman',name='translator'),
    meta(content='5 December 2013',name='created'),
    meta(content='5 March 2026',name='translated'),
    meta(content='23 April 2026',name='modified'),
    meta(content='originals/rccart2.x3d',name='reference'),
    meta(content='https://en.wikipedia.org/wiki/List_of_bones_of_the_human_skeleton',name='reference'),
    meta(content='usage examples https://www.web3d.org/x3d/content/examples/HumanoidAnimation/Medical',name='reference'),
    meta(content='X3D-Edit 4.0, https://www.web3d.org/x3d/tools/X3D-Edit',name='generator'),
    meta(content='X3D Tidy, https://www.web3d.org/x3d/stylesheets/X3dTidy.html',name='generator'),
    meta(content='https://www.web3d.org/x3d/content/examples/HumanoidAnimation/Bones/rccart2.x3d',name='identifier'),
    meta(content='../license.html',name='license')]),
  Scene=Scene(
    children=[
    WorldInfo(title='*TODO_add_title*'),
    Background(skyColor=[(0.858824,1,0.94902)]),
    Comment('user hover lights bone and reveals description, user selection/deselection temporarily binds/unbinds close-up hidden viewpoint'),
    TouchSensor(DEF='UserTouchSensor',description='cartilage rccart2'),
    Transform(DEF='rccart2',translation=(-0.0326,1.4158,0.0413),
      children=[
      BooleanToggle(DEF='UserTouchState'),
      ROUTE(fromField='isActive',fromNode='UserTouchSensor',toField='set_boolean',toNode='UserTouchState'),
      Viewpoint(DEF='HiddenViewpoint',nearDistance=0.001,position=(0,0,0.2),retainUserOffsets=True,
        navigationInfo=NavigationInfo(transitionType=["ANIMATE"],type=["EXAMINE"])),
      ROUTE(fromField='toggle',fromNode='UserTouchState',toField='set_bind',toNode='HiddenViewpoint'),
      DirectionalLight(DEF='HiddenLight',ambientIntensity=1,color=(0.1,0.1,1),on=False),
      ROUTE(fromField='toggle',fromNode='UserTouchState',toField='on',toNode='HiddenLight'),
      Transform(DEF='CenterOfRotationForJoint',visible=False,
        children=[
        Comment('insert computations for joint center of rotation here'),
        Inline(DEF='AxesDisplay',description='RGB display axes showing XYZ direction in local coordinate system',url=["AxesDisplay.x3d"],visible=False)]),
      ROUTE(fromField='toggle',fromNode='UserTouchState',toField='visible',toNode='CenterOfRotationForJoint'),
      Shape(
        appearance=Appearance(
          material=Material(DEF='CartilageMaterial',ambientIntensity=0.965,diffuseColor=(1,1,0.6),shininess=0.05,specularColor=(0.95,0.95,0.8))),
        geometry=IndexedFaceSet(DEF='rccart2_geometry',coordIndex=[0,1,2,-1,2,3,0,-1,4,5,6,-1,7,8,9,-1,10,11,12,-1,10,13,11,-1,13,14,11,-1,12,15,10,-1,2,16,17,-1,9,18,19,-1,9,20,18,-1,21,22,23,-1,23,24,21,-1,25,26,27,-1,25,28,26,-1,11,14,29,-1,30,31,32,-1,31,33,32,-1,34,35,36,-1,36,37,34,-1,38,39,40,-1,41,42,34,-1,43,44,45,-1,45,46,43,-1,12,39,15,-1,47,48,49,-1,49,50,47,-1,51,52,53,-1,53,54,51,-1,40,55,56,-1,57,47,50,-1,50,58,57,-1,6,59,60,-1,59,61,60,-1,17,62,5,-1,17,63,62,-1,64,65,66,-1,64,67,65,-1,49,48,68,-1,68,69,49,-1,53,52,70,-1,52,71,70,-1,35,72,73,-1,40,39,74,-1,67,45,75,-1,45,76,75,-1,41,34,37,-1,37,77,41,-1,54,53,57,-1,57,78,54,-1,79,60,61,-1,79,80,60,-1,81,0,82,-1,31,83,10,-1,10,33,31,-1,24,81,82,-1,24,23,81,-1,84,22,21,-1,84,85,22,-1,8,86,20,-1,20,9,8,-1,10,83,13,-1,83,87,13,-1,16,2,1,-1,49,88,25,-1,25,50,49,-1,33,10,15,-1,15,89,33,-1,11,8,7,-1,7,12,11,-1,3,82,0,-1,17,4,2,-1,17,5,4,-1,84,43,90,-1,43,84,44,-1,19,7,9,-1,19,91,7,-1,33,92,32,-1,92,33,89,-1,58,25,27,-1,50,25,58,-1,80,49,69,-1,80,88,49,-1,3,24,82,-1,93,24,3,-1,35,42,72,-1,42,35,34,-1,51,40,56,-1,54,40,51,-1,38,15,39,-1,15,38,89,-1,29,8,11,-1,29,86,8,-1,90,85,84,-1,90,94,85,-1,28,80,79,-1,80,28,88,-1,53,47,57,-1,53,70,47,-1,95,48,96,-1,48,95,68,-1,97,41,77,-1,97,98,41,-1,76,73,75,-1,99,73,76,-1,64,45,67,-1,64,46,45,-1,100,40,74,-1,100,55,40,-1,72,75,73,-1,72,101,75,-1,98,102,103,-1,95,102,98,-1,96,71,104,-1,70,71,96,-1,91,74,7,-1,74,91,100,-1,78,58,92,-1,78,57,58,-1,56,64,66,-1,56,55,64,-1,105,99,76,-1,105,93,99,-1,4,97,77,-1,4,106,97,-1,29,13,87,-1,29,14,13,-1,63,16,107,-1,63,17,16,-1,1,107,16,-1,107,1,108,-1,108,0,81,-1,108,1,0,-1,106,68,97,-1,69,68,106,-1,2,77,37,-1,2,4,77,-1,3,37,36,-1,2,37,3,-1,36,93,3,-1,99,93,36,-1,65,75,101,-1,75,65,67,-1,78,89,38,-1,78,92,89,-1,39,7,74,-1,39,12,7,-1,100,43,46,-1,100,91,43,-1,102,96,104,-1,96,102,95,-1,41,103,42,-1,98,103,41,-1,44,76,45,-1,44,105,76,-1,46,55,100,-1,55,46,64,-1,35,99,36,-1,35,73,99,-1,98,68,95,-1,68,98,97,-1,48,70,96,-1,47,70,48,-1,32,26,30,-1,27,26,32,-1,94,19,18,-1,19,94,90,-1,40,78,38,-1,40,54,78,-1,93,21,24,-1,105,21,93,-1,69,60,80,-1,106,60,69,-1,58,32,92,-1,58,27,32,-1,90,91,19,-1,91,90,43,-1,105,84,21,-1,44,84,105,-1,59,5,62,-1,5,59,6,-1,106,6,60,-1,6,106,4,-1],creaseAngle=1.571,
          coord=Coordinate(DEF='rccart2_coordinate',point=[(0.0178,0.0024,0.0036),(0.0177,0.0025,0.0035),(0.0128,0.0004,0.0041),(0.0130,-0.0006,0.0049),(0.0123,0.0021,0.0026),(0.0172,0.0036,0.0024),(0.0172,0.0037,0.0023),(0.0144,-0.0017,-0.0014),(0.0193,0.0029,-0.0051),(0.0194,0.0024,-0.0050),(0.0190,0.0037,-0.0055),(0.0197,0.0037,-0.0052),(0.0145,-0.0000,-0.0023),(0.0199,0.0038,-0.0053),(0.0199,0.0038,-0.0053),(0.0143,0.0009,-0.0028),(0.0174,0.0026,0.0032),(0.0174,0.0027,0.0031),(0.0202,-0.0044,-0.0010),(0.0202,-0.0045,-0.0011),(0.0193,0.0025,-0.0049),(0.0164,-0.0104,0.0087),(0.0166,-0.0103,0.0085),(0.0180,-0.0053,0.0075),(0.0179,-0.0052,0.0077),(0.0130,0.0119,-0.0079),(0.0144,0.0115,-0.0085),(0.0143,0.0115,-0.0087),(0.0135,0.0117,-0.0080),(0.0196,0.0037,-0.0052),(0.0161,0.0097,-0.0080),(0.0191,0.0041,-0.0058),(0.0159,0.0098,-0.0082),(0.0190,0.0040,-0.0058),(-0.0051,-0.0023,0.0049),(-0.0052,-0.0040,0.0057),(0.0087,-0.0025,0.0055),(0.0088,-0.0013,0.0047),(0.0099,-0.0012,-0.0011),(0.0099,-0.0016,-0.0009),(-0.0039,-0.0038,0.0000),(-0.0055,-0.0011,0.0042),(-0.0186,-0.0020,0.0035),(0.0129,-0.0089,0.0035),(0.0118,-0.0103,0.0060),(0.0068,-0.0101,0.0060),(0.0079,-0.0093,0.0036),(0.0074,0.0050,-0.0025),(0.0074,0.0051,-0.0024),(0.0104,0.0076,-0.0040),(0.0105,0.0077,-0.0047),(-0.0145,0.0011,-0.0028),(-0.0148,0.0022,-0.0025),(-0.0058,0.0024,-0.0015),(-0.0049,0.0011,-0.0015),(-0.0041,-0.0062,0.0011),(-0.0153,-0.0050,-0.0012),(0.0079,0.0046,-0.0029),(0.0112,0.0070,-0.0049),(0.0172,0.0036,0.0022),(0.0147,0.0086,-0.0027),(0.0149,0.0084,-0.0028),(0.0173,0.0034,0.0023),(0.0175,0.0027,0.0029),(-0.0054,-0.0094,0.0035),(-0.0198,-0.0085,0.0043),(-0.0174,-0.0081,0.0016),(-0.0068,-0.0103,0.0061),(0.0074,0.0044,-0.0011),(0.0107,0.0066,-0.0024),(-0.0062,0.0030,-0.0010),(-0.0154,0.0030,-0.0015),(-0.0197,-0.0054,0.0048),(-0.0055,-0.0065,0.0066),(0.0098,-0.0020,-0.0007),(-0.0067,-0.0092,0.0069),(0.0072,-0.0093,0.0073),(0.0082,0.0005,0.0032),(0.0086,0.0034,-0.0027),(0.0140,0.0107,-0.0059),(0.0137,0.0109,-0.0059),(0.0178,0.0022,0.0035),(0.0176,0.0019,0.0039),(0.0190,0.0037,-0.0055),(0.0150,-0.0118,0.0064),(0.0153,-0.0115,0.0063),(0.0193,0.0028,-0.0051),(0.0196,0.0038,-0.0052),(0.0131,0.0119,-0.0080),(0.0137,0.0023,-0.0034),(0.0185,-0.0100,0.0023),(0.0140,-0.0054,0.0008),(0.0124,0.0051,-0.0044),(0.0126,-0.0059,0.0072),(0.0185,-0.0099,0.0024),(-0.0062,0.0029,0.0002),(-0.0064,0.0032,-0.0005),(0.0077,0.0028,0.0011),(-0.0057,0.0016,0.0021),(0.0083,-0.0063,0.0072),(0.0095,-0.0058,0.0013),(-0.0202,-0.0077,0.0048),(-0.0159,0.0032,-0.0006),(-0.0170,0.0017,0.0012),(-0.0159,0.0032,-0.0007),(0.0119,-0.0092,0.0075),(0.0114,0.0046,0.0000),(0.0175,0.0025,0.0031),(0.0178,0.0023,0.0034)])))])])
)

### X3D model conversion complete ###

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

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