####################################################################################################
#
# Invoking X3D model self-test:
#
#   $ python IcosahedronSubdivisionLevel2.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='Interchange',version='3.1',
  head=head(
    children=[
    meta(content='IcosahedronSubdivisionLevel2.x3d',name='title'),
    meta(content='IcosahedronSubdivisionLevel2.ply',name='reference'),
    meta(content='Icosahedron is a polyhedron with twenty faces, subdivided to level 2, where all 162 vertices and 320 faces produce regular (equilateral) triangles.',name='description'),
    meta(content='13 September 2014',name='created'),
    meta(content='20 October 2019',name='modified'),
    meta(content='Don Brutzman',name='creator'),
    meta(content='Icosahedron.x3d',name='reference'),
    meta(content='https://en.wikipedia.org/wiki/Icosahedron',name='reference'),
    meta(content='https://en.wiktionary.org/wiki/icosahedron',name='reference'),
    meta(content='https://en.wikipedia.org/wiki/Platonic_solid',name='reference'),
    meta(content='https://en.wikipedia.org/wiki/Geodesic_grid',name='reference'),
    meta(content='https://www.web3d.org/x3d/content/examples/X3dForAdvancedModeling/GeometricShapes/IcosahedronSubdivisionLevel2.x3d',name='identifier'),
    meta(content='Meshlab X3D Exported, http://meshlab.sourceforge.net',name='generator'),
    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='IcosahedronSubdivisionLevel2.x3d'),
    Background(skyColor=[(0.5,1,0.9)]),
    Viewpoint(description='Icosahedron, subdivision level 2',position=(0,0,6)),
    Shape(DEF='IcosahedronSubdivisionLevel2',
      geometry=IndexedFaceSet(coordIndex=[0,1,2,-1,3,4,5,-1,6,7,8,-1,9,10,11,-1,12,13,14,-1,15,16,17,-1,18,19,20,-1,21,22,23,-1,24,25,26,-1,27,28,29,-1,30,31,32,-1,33,34,35,-1,36,37,38,-1,39,40,41,-1,42,43,44,-1,45,46,47,-1,48,49,50,-1,51,52,53,-1,54,55,56,-1,57,58,59,-1,60,2,61,-1,62,63,0,-1,1,64,65,-1,62,5,66,-1,60,67,3,-1,4,68,69,-1,70,8,71,-1,72,73,6,-1,7,74,75,-1,72,11,76,-1,70,77,9,-1,10,78,79,-1,80,14,81,-1,82,83,12,-1,13,84,85,-1,82,17,86,-1,80,87,15,-1,16,88,89,-1,90,20,91,-1,92,93,18,-1,19,94,95,-1,92,23,96,-1,90,97,21,-1,22,98,99,-1,100,26,101,-1,102,103,24,-1,25,104,105,-1,102,29,106,-1,100,107,27,-1,28,108,109,-1,110,32,111,-1,112,113,30,-1,31,114,115,-1,112,35,116,-1,110,117,33,-1,34,118,119,-1,108,38,63,-1,107,88,36,-1,37,87,64,-1,66,41,109,-1,69,91,39,-1,40,95,106,-1,61,44,115,-1,65,81,42,-1,43,85,111,-1,114,47,67,-1,113,98,45,-1,46,97,68,-1,71,50,105,-1,75,86,48,-1,49,89,101,-1,104,53,77,-1,103,94,51,-1,52,93,78,-1,118,56,73,-1,117,84,54,-1,55,83,74,-1,76,59,119,-1,79,96,57,-1,58,99,116,-1,120,0,2,-1,0,121,1,-1,2,1,122,-1,120,3,5,-1,3,123,4,-1,5,4,124,-1,125,6,8,-1,6,126,7,-1,8,7,127,-1,125,9,11,-1,9,128,10,-1,11,10,129,-1,130,12,14,-1,12,131,13,-1,14,13,132,-1,130,15,17,-1,15,133,16,-1,17,16,134,-1,135,18,20,-1,18,136,19,-1,20,19,137,-1,135,21,23,-1,21,138,22,-1,23,22,139,-1,140,24,26,-1,24,141,25,-1,26,25,142,-1,140,27,29,-1,27,143,28,-1,29,28,144,-1,145,30,32,-1,30,146,31,-1,32,31,147,-1,145,33,35,-1,33,148,34,-1,35,34,149,-1,143,36,38,-1,36,133,37,-1,38,37,121,-1,124,39,41,-1,39,137,40,-1,41,40,144,-1,122,42,44,-1,42,132,43,-1,44,43,147,-1,146,45,47,-1,45,138,46,-1,47,46,123,-1,127,48,50,-1,48,134,49,-1,50,49,142,-1,141,51,53,-1,51,136,52,-1,53,52,128,-1,148,54,56,-1,54,131,55,-1,56,55,126,-1,129,57,59,-1,57,139,58,-1,59,58,149,-1,150,60,61,-1,60,120,2,-1,61,2,122,-1,120,62,0,-1,62,151,63,-1,0,63,121,-1,122,1,65,-1,1,121,64,-1,65,64,152,-1,151,62,66,-1,62,120,5,-1,66,5,124,-1,120,60,3,-1,60,150,67,-1,3,67,123,-1,124,4,69,-1,4,123,68,-1,69,68,153,-1,154,70,71,-1,70,125,8,-1,71,8,127,-1,125,72,6,-1,72,155,73,-1,6,73,126,-1,127,7,75,-1,7,126,74,-1,75,74,156,-1,155,72,76,-1,72,125,11,-1,76,11,129,-1,125,70,9,-1,70,154,77,-1,9,77,128,-1,129,10,79,-1,10,128,78,-1,79,78,157,-1,152,80,81,-1,80,130,14,-1,81,14,132,-1,130,82,12,-1,82,156,83,-1,12,83,131,-1,132,13,85,-1,13,131,84,-1,85,84,158,-1,156,82,86,-1,82,130,17,-1,86,17,134,-1,130,80,15,-1,80,152,87,-1,15,87,133,-1,134,16,89,-1,16,133,88,-1,89,88,159,-1,153,90,91,-1,90,135,20,-1,91,20,137,-1,135,92,18,-1,92,157,93,-1,18,93,136,-1,137,19,95,-1,19,136,94,-1,95,94,160,-1,157,92,96,-1,92,135,23,-1,96,23,139,-1,135,90,21,-1,90,153,97,-1,21,97,138,-1,139,22,99,-1,22,138,98,-1,99,98,161,-1,159,100,101,-1,100,140,26,-1,101,26,142,-1,140,102,24,-1,102,160,103,-1,24,103,141,-1,142,25,105,-1,25,141,104,-1,105,104,154,-1,160,102,106,-1,102,140,29,-1,106,29,144,-1,140,100,27,-1,100,159,107,-1,27,107,143,-1,144,28,109,-1,28,143,108,-1,109,108,151,-1,158,110,111,-1,110,145,32,-1,111,32,147,-1,145,112,30,-1,112,161,113,-1,30,113,146,-1,147,31,115,-1,31,146,114,-1,115,114,150,-1,161,112,116,-1,112,145,35,-1,116,35,149,-1,145,110,33,-1,110,158,117,-1,33,117,148,-1,149,34,119,-1,34,148,118,-1,119,118,155,-1,151,108,63,-1,108,143,38,-1,63,38,121,-1,143,107,36,-1,107,159,88,-1,36,88,133,-1,121,37,64,-1,37,133,87,-1,64,87,152,-1,151,66,109,-1,66,124,41,-1,109,41,144,-1,124,69,39,-1,69,153,91,-1,39,91,137,-1,144,40,106,-1,40,137,95,-1,106,95,160,-1,150,61,115,-1,61,122,44,-1,115,44,147,-1,122,65,42,-1,65,152,81,-1,42,81,132,-1,147,43,111,-1,43,132,85,-1,111,85,158,-1,150,114,67,-1,114,146,47,-1,67,47,123,-1,146,113,45,-1,113,161,98,-1,45,98,138,-1,123,46,68,-1,46,138,97,-1,68,97,153,-1,154,71,105,-1,71,127,50,-1,105,50,142,-1,127,75,48,-1,75,156,86,-1,48,86,134,-1,142,49,101,-1,49,134,89,-1,101,89,159,-1,154,104,77,-1,104,141,53,-1,77,53,128,-1,141,103,51,-1,103,160,94,-1,51,94,136,-1,128,52,78,-1,52,136,93,-1,78,93,157,-1,155,118,73,-1,118,148,56,-1,73,56,126,-1,148,117,54,-1,117,158,84,-1,54,84,131,-1,126,55,74,-1,55,131,83,-1,74,83,156,-1,155,76,119,-1,76,129,59,-1,119,59,149,-1,129,79,57,-1,79,157,96,-1,57,96,139,-1,149,58,116,-1,58,139,99,-1,116,99,161,-1],solid=False,
        coord=Coordinate(point=[(0.262866,0.951057,0.16246),(0.525731,0.850651,0),(0.262866,0.951057,-0.16246),(-0.262866,0.951057,-0.16246),(-0.525731,0.850651,0),(-0.262866,0.951057,0.16246),(0.262866,-0.951057,-0.16246),(0.525731,-0.850651,0),(0.262866,-0.951057,0.16246),(-0.262866,-0.951057,0.16246),(-0.525731,-0.850651,0),(-0.262866,-0.951057,-0.16246),(0.951057,-0.16246,-0.262866),(0.850651,0,-0.525731),(0.951057,0.16246,-0.262866),(0.951057,0.16246,0.262866),(0.850651,0,0.525731),(0.951057,-0.16246,0.262866),(-0.951057,-0.16246,0.262866),(-0.850651,0,0.525731),(-0.951057,0.16246,0.262866),(-0.951057,0.16246,-0.262866),(-0.850651,0,-0.525731),(-0.951057,-0.16246,-0.262866),(-0.16246,-0.262866,0.951057),(0,-0.525731,0.850651),(0.16246,-0.262866,0.951057),(0.16246,0.262866,0.951057),(0,0.525731,0.850651),(-0.16246,0.262866,0.951057),(-0.16246,0.262866,-0.951057),(0,0.525731,-0.850651),(0.16246,0.262866,-0.951057),(0.16246,-0.262866,-0.951057),(0,-0.525731,-0.850651),(-0.16246,-0.262866,-0.951057),(0.587785,0.425325,0.688191),(0.688191,0.587785,0.425325),(0.425325,0.688191,0.587785),(-0.688191,0.587785,0.425325),(-0.587785,0.425325,0.688191),(-0.425325,0.688191,0.587785),(0.688191,0.587785,-0.425325),(0.587785,0.425325,-0.688191),(0.425325,0.688191,-0.587785),(-0.587785,0.425325,-0.688191),(-0.688191,0.587785,-0.425325),(-0.425325,0.688191,-0.587785),(0.688191,-0.587785,0.425325),(0.587785,-0.425325,0.688191),(0.425325,-0.688191,0.587785),(-0.587785,-0.425325,0.688191),(-0.688191,-0.587785,0.425325),(-0.425325,-0.688191,0.587785),(0.587785,-0.425325,-0.688191),(0.688191,-0.587785,-0.425325),(0.425325,-0.688191,-0.587785),(-0.688191,-0.587785,-0.425325),(-0.587785,-0.425325,-0.688191),(-0.425325,-0.688191,-0.587785),(0,0.961938,-0.273267),(0.259892,0.862668,-0.433889),(0,0.961938,0.273267),(0.259892,0.862668,0.433889),(0.702046,0.69378,0.160622),(0.702046,0.69378,-0.160622),(-0.259892,0.862668,0.433889),(-0.259892,0.862668,-0.433889),(-0.702046,0.69378,-0.160622),(-0.702046,0.69378,0.160622),(0,-0.961938,0.273267),(0.259892,-0.862668,0.433889),(0,-0.961938,-0.273267),(0.259892,-0.862668,-0.433889),(0.702046,-0.69378,-0.160622),(0.702046,-0.69378,0.160622),(-0.259892,-0.862668,-0.433889),(-0.259892,-0.862668,0.433889),(-0.702046,-0.69378,0.160622),(-0.702046,-0.69378,-0.160622),(0.961938,0.273267,0),(0.862668,0.433889,-0.259892),(0.961938,-0.273267,0),(0.862668,-0.433889,-0.259892),(0.69378,-0.160622,-0.702046),(0.69378,0.160622,-0.702046),(0.862668,-0.433889,0.259892),(0.862668,0.433889,0.259892),(0.69378,0.160622,0.702046),(0.69378,-0.160622,0.702046),(-0.961938,0.273267,0),(-0.862668,0.433889,0.259892),(-0.961938,-0.273267,0),(-0.862668,-0.433889,0.259892),(-0.69378,-0.160622,0.702046),(-0.69378,0.160622,0.702046),(-0.862668,-0.433889,-0.259892),(-0.862668,0.433889,-0.259892),(-0.69378,0.160622,-0.702046),(-0.69378,-0.160622,-0.702046),(0.273267,0,0.961938),(0.433889,-0.259892,0.862668),(-0.273267,0,0.961938),(-0.433889,-0.259892,0.862668),(-0.160622,-0.702046,0.69378),(0.160622,-0.702046,0.69378),(-0.433889,0.259892,0.862668),(0.433889,0.259892,0.862668),(0.160622,0.702046,0.69378),(-0.160622,0.702046,0.69378),(0.273267,0,-0.961938),(0.433889,0.259892,-0.862668),(-0.273267,0,-0.961938),(-0.433889,0.259892,-0.862668),(-0.160622,0.702046,-0.69378),(0.160622,0.702046,-0.69378),(-0.433889,-0.259892,-0.862668),(0.433889,-0.259892,-0.862668),(0.160622,-0.702046,-0.69378),(-0.160622,-0.702046,-0.69378),(0,1,0),(0.5,0.809017,0.309017),(0.5,0.809017,-0.309017),(-0.5,0.809017,-0.309017),(-0.5,0.809017,0.309017),(0,-1,0),(0.5,-0.809017,-0.309017),(0.5,-0.809017,0.309017),(-0.5,-0.809017,0.309017),(-0.5,-0.809017,-0.309017),(1,0,0),(0.809017,-0.309017,-0.5),(0.809017,0.309017,-0.5),(0.809017,0.309017,0.5),(0.809017,-0.309017,0.5),(-1,0,0),(-0.809017,-0.309017,0.5),(-0.809017,0.309017,0.5),(-0.809017,0.309017,-0.5),(-0.809017,-0.309017,-0.5),(0,0,1),(-0.309017,-0.5,0.809017),(0.309017,-0.5,0.809017),(0.309017,0.5,0.809017),(-0.309017,0.5,0.809017),(0,0,-1),(-0.309017,0.5,-0.809017),(0.309017,0.5,-0.809017),(0.309017,-0.5,-0.809017),(-0.309017,-0.5,-0.809017),(0,0.850651,-0.525731),(0,0.850651,0.525731),(0.850651,0.525731,0),(-0.850651,0.525731,0),(0,-0.850651,0.525731),(0,-0.850651,-0.525731),(0.850651,-0.525731,0),(-0.850651,-0.525731,0),(0.525731,0,-0.850651),(0.525731,0,0.850651),(-0.525731,0,0.850651),(-0.525731,0,-0.850651)])),
      appearance=Appearance(
        material=Material(diffuseColor=(0.2,0.5,0.8))))])
)

### X3D model conversion complete ###

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

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