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

Don Brutzman brutzman at nps.edu
Sun Nov 15 23:04:30 PST 2015


[Summary: updates for string arrays, whitespace, UTF-8 encoding, Script #source.]

=================================================
Great recommendation regarding string arrays, thanks John.  Now implemented (and deployed) in the manner you suggest.

On 11/13/2015 12:49 PM, John Carlson wrote:
> Don, I think the JSON encoding or translation of string arrays should be improved.  See attached HelloWorld.json for something that looks better.  We could probably make yours work, but for a human encoding a scene, it’s very counter-intuitive.  It looks like you’re creating an array to put a single string in it.
>
> Thanks.
>
> //  This looks better:
>
> "@url":["earth-topo.png", "earth-topo.jpg", "earth-topo-small.gif", "http://www.web3d.org/x3d/content/examples/Basic/earth-topo.png", "http://www.web3d.org/x3d/content/examples/Basic/earth-topo.jpg", "http://www.web3d.org/x3d/content/examples/Basic/earth-topo-small.gif”]
>
> // than this, which is what you have:
>
> "@url":["\"earth-topo.png\" \"earth-topo.jpg\" \"earth-topo-small.gif\" \"http://www.web3d.org/x3d/content/examples/Basic/earth-topo.png\" \"http://www.web3d.org/x3d/content/examples/Basic/earth-topo.jpg\" \"http://www.web3d.org/x3d/content/examples/Basic/earth-topo-small.gif\”"]
>
> John

Example excerpts, much cleaner than before:

"-appearance":[
   { "Appearance":
     {
       "-material":[
         { "Material":
           {
             "@DEF":"MaterialLightBlue",
             "@diffuseColor":[0.1,0.5,1]
           }
         }
       ],
       "-texture":[
         { "ImageTexture":
           {
             "@DEF":"ImageCloudlessEarth",
             "@url":["earth-topo.png","earth-topo.jpg","earth-topo-small.gif","http://www.web3d.org/x3d/content/examples/Basic/earth-topo.png","http://www.web3d.org/x3d/content/examples/Basic/earth-topo.jpg","http://www.web3d.org/x3d/content/examples/Basic/earth-topo-small.gif"]
           }
         }
       ]
     }
   }
]
  
and

"-geometry":[
   { "Text":
     {
       "@DEF":"TextMessage",
       "@string":["Hello","world!"],
       "-fontStyle":[
         { "FontStyle":
           {
             "@justify":["MIDDLE","MIDDLE"]
           }
         }
       ]
     }
   }
],
=================================================

As part of the previous development, encountered a large number of MFString values in the archives that were not "correctly" "quoted as required".

Fixed numerous .x3d scenes lacking quote marks, checked them in, improved schematron diagnostics, and made the X3dToJson.xslt stylesheet forgiving of this flaw.  It will add quotes around MFString values if completely missing, allowing valid JSON to be produced.  Also provides a stylesheet console warning to fix the appropriate source .x3d node/attribute if that issue is encountered.

=================================================

Based on Friday's helpful teleconference discussion, the following improvements have also been posted:

- No space after commas separating numeric values in an array, especially helpful for large mesh geometries.

- UTF8 encoding for output, via
   <xsl:output method="text" encoding="UTF-8"/> <!-- output methods: xml html text -->

On 11/13/2015 9:48 AM, John Carlson wrote:
> Unicode is handled with \u followed by 4 hexadecimal digits, I believe.

Looking at the JSON spec definitions for string, it seems that internationalized values such as those found in these examples are legal without unicode escaping.  So I think we might be OK already, but browsers and jslint are perhaps overstrict in flagging that as and error...

http://X3dGraphics.com/examples/X3dForAdvancedModeling/HelloWorldScenes/HelloSeoul.x3d
http://X3dGraphics.com/examples/X3dForAdvancedModeling/HelloWorldScenes/HelloSeoul.json
http://x3dgraphics.com/examples/X3dForAdvancedModeling/HelloWorldScenes/HelloSeoul.html
http://x3dgraphics.com/examples/X3dForAdvancedModeling/HelloWorldScenes/HelloSeoul.xhtml

To check I installed the Mozilla Firefox language pack for Korean.  However the symbols do not render correctly using JSON, only in HTML.
https://addons.mozilla.org/en-us/firefox/addon/korean-kr-language-pack

Am thinking there are probably other ways to test & confirm that this encoding approach is OK, a little more research is needed.

=================================================

Added another capability: embedded javascript source within a Script node, which wasn't included before.  Handled similarly to "#comment" key, slightly different in that it is always a single multi-line string object, rather than an array of single-line comment strings.

The result below looks good to me, according to the JSON specification.  However jslint balks... is it really an error, perhaps an overzealous jslint configuration setting controls this?

Console:
> C:\x3d-code\www.web3d.org\x3d\content\examples\X3dForAdvancedModeling/GeometricShapes//ParallelepipedPrototype.x3d processing with X3dToJson stylesheet
>  Script ParallelepipedScript contains CDATA source-code text, copied as "#source"
> C:\x3d-code\www.web3d.org\x3d\content\examples\X3dForAdvancedModeling/GeometricShapes//ParallelepipedPrototype.json processing with jslint
> jslint:C:\x3d-code\www.web3d.org\x3d\content\examples\X3dForAdvancedModeling/GeometricShapes//ParallelepipedPrototype.json:279:29:JavaScript URL.
> jslint:C:\x3d-code\www.web3d.org\x3d\content\examples\X3dForAdvancedModeling/GeometricShapes//ParallelepipedPrototype.json:284:10:Unexpected character '(space)'.

Excerpt:
http://X3dGraphics.com/examples/X3dForAdvancedModeling/GeometricShapes/ParallelepipedPrototype.x3d
http://X3dGraphics.com/examples/X3dForAdvancedModeling/GeometricShapes/ParallelepipedPrototype.json

             "-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 ();
}
"
                 }
               }
             ]

How does it look?  I believe that this is the last major language feature to include, further work will be tuning/improving what is there.

=================================================

I was able to reduce some of the recursion in the stylesheet, and tried to nudge the memory settings in multiple ways, but the large scenes provoking out-of-memory errors were unimpressed and continue to explode in the same amount of time as before... will continue looking at those configurations.

=================================================

Closer and closer... comments and improvements remain appreciated and welcome.

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



More information about the x3d-public mailing list