[x3d-public] announce: updated encoding, X3D to JSON Stylesheet Converter: string arrays, whitespace, UTF-8, Script #source

Don Brutzman brutzman at nps.edu
Tue Nov 17 22:17:23 PST 2015


[summary: from plaintext block to total escapism and now to string array for #source block]

On 11/17/2015 7:21 AM, John Carlson wrote:
> Why isn't an array of strings acceptable?

On 11/17/2015 7:23 AM, John Carlson wrote:
> It's a typical javascript pattern to create arrays of strings and then join them.

glad to hear it!

If arrays of json strings are typical and acceptable and not an undue complication for plain old javascript programmers, then it is certainly fine with me.  I strongly believe that human readability is important.  Quoted strings are surely better than garble.

It is no trouble for me to export .x3d to .json #source that way as well.  I have added another option to the stylesheet switch tonight to produce that alternative.

Example attached, excerpt follows.  Hmmm, not too bad, and certainly not confusable.

I will again regenerate all examples in that form if opinions are leaning that way.

==========================================
             "-children":[
               { "Script":
[...snip...]
                   "#source":[
"ecmascript:",
"function initialize ()",
"{",
"    checkCoordinatePoints ();",
"}",
"function checkCoordinatePoints ()",
"{",
"    // A parallelepiped has three sets of four parallel edges; the edges within each set are of equal length.",
"    // Quality assurance: check that lengths of corresponding sides match.",
"    ",
"    edge01 = length2(point[0], point[1]); // top",
"    edge12 = length2(point[1], point[2]);",
"    edge23 = length2(point[2], point[3]);",
"    edge30 = length2(point[3], point[0]);",
"    edge45 = length2(point[4], point[5]); // bottom",
"    edge56 = length2(point[5], point[6]);",
"    edge67 = length2(point[6], point[7]);",
"    edge74 = length2(point[7], point[4]);",
"    edge04 = length2(point[0], point[4]); // sides",
"    edge15 = length2(point[1], point[5]);",
"    edge26 = length2(point[2], point[6]);",
"    edge37 = length2(point[3], point[7]);",
"    ",
"    epsilon = edge01 * 0.001;",
"    ",
"    if      ((Math.abs(edge01 - edge23) > epsilon) ||",
"             (Math.abs(edge23 - edge67) > epsilon) ||",
"             (Math.abs(edge67 - edge45) > epsilon) ||",
"             (Math.abs(edge45 - edge01) > epsilon))",
"         Browser.println ('Warning, mismatched parallelopiped sides 02/33/45/67');",
"    else if ((Math.abs(edge30 - edge12) > epsilon) ||",
"             (Math.abs(edge12 - edge56) > epsilon) ||",
"             (Math.abs(edge56 - edge74) > epsilon) ||",
"             (Math.abs(edge74 - edge30) > epsilon))",
"         Browser.println ('Warning, mismatched parallelopiped sides 30/12/56/74');",
"    else if ((Math.abs(edge04 - edge15) > epsilon) ||",
"             (Math.abs(edge15 - edge26) > epsilon) ||",
"             (Math.abs(edge26 - edge37) > epsilon) ||",
"             (Math.abs(edge37 - edge04) > epsilon))",
"         Browser.println ('Warning, mismatched parallelopiped sides 04/15/26/37');",
"}",
"function length2 (pointA, pointB)",
"{    ",
"    return Math.sqrt((pointA.x - pointB.x)*(pointA.x - pointB.x) +",
"                     (pointA.y - pointB.y)*(pointA.y - pointB.y) +",
"                     (pointA.z - pointB.z)*(pointA.z - pointB.z));",
"}",
"function set_point (eventValue)",
"{",
"   // input eventValue received for inputOutput field",
"    point = eventValue;",
"    checkCoordinatePoints ();",
"}"
]
                 }
               }
             ]
==========================================

Example reassembly to join strings back together in javascript:

http://stackoverflow.com/questions/2392766/multiline-strings-in-json
> Once parsed, I just use myData.modify_head.join('\n') or myData.modify_head.join(),
>depending upon whether I want a line break after each string or not.

Wondering, what would that join() code specifically look like for an X3D JSON loader?

Looking forward: seems like a group/community decision if we want to specify a string array as X3D JSON default for #source blocks.

all the best, Don
-- 
Don Brutzman  Naval Postgraduate School, Code USW/Br       brutzman at nps.edu
Watkins 270,  MOVES Institute, Monterey CA 93943-5000 USA   +1.831.656.2149
X3D graphics, virtual worlds, navy robotics http://faculty.nps.edu/brutzman
-------------- next part --------------
{ "X3D": {
    "@profile":"Immersive",
    "@version":3.3,
    "@xsd:noNamespaceSchemaLocation":"http://www.web3d.org/specifications/x3d-3.3.xsd",
  "head": {
    "meta": [
      {
        "@name":"title",
        "@content":"ParallelepipedPrototype.x3d"
      },
      {
        "@name":"description",
        "@content":"Reusable prototype for creating a Parallelepiped, which is a 3D figure formed by six parallelograms; also sometimes referred to as a rhomboid."
      },
      {
        "@name":"creator",
        "@content":"Don Brutzman"
      },
      {
        "@name":"created",
        "@content":"25 November 2011"
      },
      {
        "@name":"modified",
        "@content":"26 November 2011"
      },
      {
        "@name":"reference",
        "@content":"ParallelepipedExamples.x3d"
      },
      {
        "@name":"Image",
        "@content":"ParallelepipedVertexIndices.png"
      },
      {
        "@name":"drawing",
        "@content":"GeometricShapes.vsd"
      },
      {
        "@name":"reference",
        "@content":"http://en.wikipedia.org/wiki/Parallelepiped"
      },
      {
        "@name":"reference",
        "@content":"http://upload.wikimedia.org/wikipedia/commons/f/f6/Parallelepiped.svg"
      },
      {
        "@name":"reference",
        "@content":"http://upload.wikimedia.org/wikipedia/commons/thumb/f/f6/Parallelepiped.svg/500px-Parallelepiped.svg.png"
      },
      {
        "@name":"subject",
        "@content":"parallelopiped rhomboid geometry polyhedron"
      },
      {
        "@name":"identifier",
        "@content":"http://X3dGraphics.com/examples/X3dForAdvancedModeling/GeometricShapes/ParallelepipedPrototype.x3d"
      },
      {
        "@name":"reference",
        "@content":"http://www.web3d.org/x3d/content/examples/X3dResources.html"
      },
      {
        "@name":"generator",
        "@content":"X3D-Edit 3.3, https://savage.nps.edu/X3D-Edit"
      },
      {
        "@name":"license",
        "@content":"../license.html"
      }
    ]
  },
  "Scene": {
    "#comment":[
      "================================================================================"
    ],
    "-children":[
      { "ProtoDeclare":
        {
          "@name":"Parallelepiped",
          "@appinfo":"Parallelepiped is a 3D figure formed by six parallelograms; also sometimes referred to as a rhomboid",
          "field": [
            {
              "@name":"point",
              "@accessType":"inputOutput",
              "@type":"MFVec3f",
              "@value":[0,1,0,1,1,0,1,1,1,0,1,1,0,0,0,1,0,0,1,0,1,0,0,1]
            },
            {
              "@name":"colorPerVertex",
              "@accessType":"initializeOnly",
              "@type":"SFBool",
              "@value":true
            },
            {
              "@name":"normalPerVertex",
              "@accessType":"initializeOnly",
              "@type":"SFBool",
              "@value":true
            },
            {
              "@name":"colorIndex",
              "@accessType":"initializeOnly",
              "@type":"MFInt32"
            },
            {
              "@name":"normalIndex",
              "@accessType":"initializeOnly",
              "@type":"MFInt32"
            },
            {
              "@name":"texCoordIndex",
              "@accessType":"initializeOnly",
              "@type":"MFInt32"
            },
            {
              "@name":"color",
              "@accessType":"inputOutput",
              "@type":"SFNode",
            "#comment":[
              "initialization node (if any) goes here"
            ]
            },
            {
              "@name":"normal",
              "@accessType":"inputOutput",
              "@type":"SFNode",
            "#comment":[
              "initialization node (if any) goes here"
            ]
            },
            {
              "@name":"texCoord",
              "@accessType":"inputOutput",
              "@type":"SFNode",
            "#comment":[
              "initialization node (if any) goes here"
            ]
            }
          ],
          "ProtoBody": {
            "#comment":[
              "First node determines node type of this prototype",
              "This embedded Script provides the X3D author with additional visibility and control over prototype inputs and outputs"
            ],
            "-geometry":[
              { "IndexedFaceSet":
                {
                  "@DEF":"IFS",
                  "@convex":false,
                  "@coordIndex":[0,3,2,1,-1,4,5,6,7,-1,0,1,5,4,-1,1,2,6,5,-1,2,3,7,6,-1,3,0,4,7,-1],
                  "@solid":false,
                  "IS": {
                    "connect": [
                      {
                        "@nodeField":"colorPerVertex",
                        "@protoField":"colorPerVertex"
                      },
                      {
                        "@nodeField":"normalPerVertex",
                        "@protoField":"normalPerVertex"
                      },
                      {
                        "@nodeField":"colorIndex",
                        "@protoField":"colorIndex"
                      },
                      {
                        "@nodeField":"normalIndex",
                        "@protoField":"normalIndex"
                      },
                      {
                        "@nodeField":"texCoordIndex",
                        "@protoField":"texCoordIndex"
                      },
                      {
                        "@nodeField":"color",
                        "@protoField":"color"
                      },
                      {
                        "@nodeField":"normal",
                        "@protoField":"normal"
                      },
                      {
                        "@nodeField":"texCoord",
                        "@protoField":"texCoord"
                      }
                    ]
                  },
                  "-coord":[
                    { "Coordinate":
                      {
                        "IS": {
                          "connect": [
                            {
                              "@nodeField":"point",
                              "@protoField":"point"
                            }
                          ]
                        }
                      }
                    }
                  ]
                }
              }
            ],
            "-children":[
              { "Script":
                {
                  "@DEF":"ParallelepipedScript",
                  "field": [
                    {
                      "@name":"point",
                      "@accessType":"inputOutput",
                      "@type":"MFVec3f"
                    }
                  ],
                  "IS": {
                    "connect": [
                      {
                        "@nodeField":"point",
                        "@protoField":"point"
                      }
                    ]
                  },
                  "#source":[
"ecmascript:",
"function initialize ()",
"{",
"    checkCoordinatePoints ();",
"}",
"function checkCoordinatePoints ()",
"{",
"    // A parallelepiped has three sets of four parallel edges; the edges within each set are of equal length.",
"    // Quality assurance: check that lengths of corresponding sides match.",
"    ",
"    edge01 = length2(point[0], point[1]); // top",
"    edge12 = length2(point[1], point[2]);",
"    edge23 = length2(point[2], point[3]);",
"    edge30 = length2(point[3], point[0]);",
"    edge45 = length2(point[4], point[5]); // bottom",
"    edge56 = length2(point[5], point[6]);",
"    edge67 = length2(point[6], point[7]);",
"    edge74 = length2(point[7], point[4]);",
"    edge04 = length2(point[0], point[4]); // sides",
"    edge15 = length2(point[1], point[5]);",
"    edge26 = length2(point[2], point[6]);",
"    edge37 = length2(point[3], point[7]);",
"    ",
"    epsilon = edge01 * 0.001;",
"    ",
"    if      ((Math.abs(edge01 - edge23) > epsilon) ||",
"             (Math.abs(edge23 - edge67) > epsilon) ||",
"             (Math.abs(edge67 - edge45) > epsilon) ||",
"             (Math.abs(edge45 - edge01) > epsilon))",
"         Browser.println ('Warning, mismatched parallelopiped sides 02/33/45/67');",
"    else if ((Math.abs(edge30 - edge12) > epsilon) ||",
"             (Math.abs(edge12 - edge56) > epsilon) ||",
"             (Math.abs(edge56 - edge74) > epsilon) ||",
"             (Math.abs(edge74 - edge30) > epsilon))",
"         Browser.println ('Warning, mismatched parallelopiped sides 30/12/56/74');",
"    else if ((Math.abs(edge04 - edge15) > epsilon) ||",
"             (Math.abs(edge15 - edge26) > epsilon) ||",
"             (Math.abs(edge26 - edge37) > epsilon) ||",
"             (Math.abs(edge37 - edge04) > epsilon))",
"         Browser.println ('Warning, mismatched parallelopiped sides 04/15/26/37');",
"}",
"function length2 (pointA, pointB)",
"{    ",
"    return Math.sqrt((pointA.x - pointB.x)*(pointA.x - pointB.x) +",
"                     (pointA.y - pointB.y)*(pointA.y - pointB.y) +",
"                     (pointA.z - pointB.z)*(pointA.z - pointB.z));",
"}",
"function set_point (eventValue)",
"{",
"   // input eventValue received for inputOutput field",
"    point = eventValue;",
"    checkCoordinatePoints ();",
"}"
]
                }
              }
            ]
          }
        }
      },
      { "Background":
        {
          "@skyColor":[0.905882,1,0.858824]
        }
      },
      { "Anchor":
        {
          "@description":"Open ParallelepipedExamples scene",
          "@url":["ParallelepipedExamples.x3d","http://X3dGraphics.com/examples/X3dForAdvancedModeling/GeometricShapes/ParallelepipedExamples.x3d","ParallelepipedExamples.wrl","http://X3dGraphics.com/examples/X3dForAdvancedModeling/GeometricShapes/ParallelepipedExamples.wrl"],
          "-children":[
            { "Shape":
              {
                "-geometry":[
                  { "Text":
                    {
                      "@string":["This scene defines a","Parallelepiped prototype","","Select this text to open","ParallelepipedExamples,scene"],
                      "-fontStyle":[
                        { "FontStyle":
                          {
                            "@justify":["MIDDLE","MIDDLE"]
                          }
                        }
                      ]
                    }
                  }
                ],
                "-appearance":[
                  { "Appearance":
                    {
                      "@DEF":"BlueAppearance",
                      "-material":[
                        { "Material":
                          {
                            "@diffuseColor":[0,0.698039,1]
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            },
            { "Shape":
              {
                "@DEF":"TransparentBox",
                "-geometry":[
                  { "Box":
                    {
                      "@size":[11,5,0.1]
                    }
                  }
                ],
                "-appearance":[
                  { "Appearance":
                    {
                      "-material":[
                        { "Material":
                          {
                            "@transparency":1
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    ]
  }
  }
}


More information about the x3d-public mailing list