<div dir="ltr"><div>BLUF: can now reduce unnecessary numeric bloat in X3D models.</div><div><br></div><div>Problem statement: a long-running difficulty in many X3D models is that floating point numbers are often written into files with an extraordinary number of digits following the decimal point.</div><div><br></div><div>This doesn't make the models incorrect, but it often makes them unwieldy for editing and Web use.  Few of us need sub-nanometer precision when we look at a 3D model (cough cough).</div><div><br></div><div>Usually this is not a big deal when writing software, since programming languages typically make it easy to format floating point numbers.  However various tools and actual practice is widely different, and that can result in VERY LARGE MODELS when exporting X3D.  There are many such products in the X3D Examples Archives.  As a case in point, machine tolerances needed for CAD production is more precise than the human eye can actually discern.</div><div><br></div><div>Thus I began pursuing a string-based approach to reducing floating-point precision, to facilitate the right-sizing of model files in our archives and elsewhere.</div><div><br></div><div>An important feature of X3D is that most models are in meters, unless indicated otherwise by presence of a UNIT statement.  So that consistency can be exploited.  For most purposes, am thinking that<b> 4 significant digits</b> is plenty for most visual models (and 3D printed models) since</div><div><ul style="color:rgb(0,0,0);font-family:"Times New Roman""><li style=""><b style="">0.0001 meters</b>  equals <b style="">one-tenth of a millimeter</b>.</li><li style=""><b style="">0.0001 radians</b> equals <b style="">0.0057 degrees</b>.</li><li style="">RGB color changes past 2 or 3 digits are very subtle and perhaps imperceptible. </li></ul><div><font color="#000000" face="arial, sans-serif">Searching for a regular expression (regex) to accomplish this task yielded minimal results... and so I've developed one.  Am happy to announce it here.</font></div></div><div><ul><li><b>X3D Regular Expressions (regexes)</b></li><li>X3D Regular Expressions (regexes) are used to validate the correctness of string and numeric array values in an X3D scene.</li><li><a href="https://www.web3d.org/specifications/X3dRegularExpressions.html">https://www.web3d.org/specifications/X3dRegularExpressions.html</a></li><li><a href="https://www.web3d.org/specifications/X3dRegularExpressions.html#SignificantDigitsPrecision" style="font-family:"Times New Roman";font-size:13.3333px;text-align:-webkit-center">numeric significant-digits precision filtering</a></li></ul></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div><p style="color:rgb(0,0,0);font-family:"Times New Roman""><b style=""><a name="SignificantDigitsPrecision" style="">Numeric significant-digits precision filtering</a></b> is often desirable. Many geometry-export software tools naively emit floating-point numbers of extra-large length that result in unnecessary file size, download delays, and wasted computation when parsing. Such inefficiency is often noticeable and problematic for large model files, reducing performance and inhibiting scalability.</p></div></div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><p style="color:rgb(0,0,0);font-family:"Times New Roman"">It is helpful to remember that regexes have general expressive power (similar to other programming languages) that can search, and optionally replace, any string pattern that can be unambiguously expressed. Typically the control of numeric significant-digit precision is controlled by software formatting of numeric outputs, not by processing the character strings in an XML document. No suitable regex pattern was found in any of the references listed. Nevertheless, a Web search reveals a candidate solution in the StackOverflow post "<a href="https://stackoverflow.com/questions/51947938/reduce-float-precision-using-regexp-in-swift" target="_blank">Reduce float precision using regexp</a>" with a corresponding <a href="https://regex101.com/r/yINM4e/latest" target="_blank">regex demo</a> on Regex 101 website.</p></div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><p style="color:rgb(0,0,0);font-family:"Times New Roman"">Here is a refined pattern for floating-point arrays as string-based X3D attributes.</p></div></blockquote><div><ul style="color:rgb(0,0,0);font-family:"Times New Roman""><ul><li>ignores leading      <span class="gmail-whitespace" title="optional whitespace" style="background-color:lightgrey;font-weight:bold">whitespace</span>       preceding the numeric value,</li><li>returns necessary <span class="gmail-grouprepetition" title="group 1 selection" style="background-color:aquamarine;font-weight:bold">(</span><span class="gmail-float" title="leading digits" style="background-color:lightsteelblue;font-weight:bold">first  group</span><span class="gmail-grouprepetition" title="group 1 selection" style="background-color:aquamarine;font-weight:bold">)</span>     including the optional +/- sign character and then leading digits,</li><li>returns optional   <span class="gmail-grouprepetition" title="group 2 selection" style="background-color:aquamarine;font-weight:bold">(</span><span class="gmail-float" title="leading digits" style="background-color:lightsteelblue;font-weight:bold">second group</span><span class="gmail-grouprepetition" title="group 2 selection" style="background-color:aquamarine;font-weight:bold">)</span>  including the decimal point and (in this case) <b><code>4</code></b> trailing digits,</li><li>ignores omitted     <span class="gmail-whitespace" title="omitted excess digits" style="background-color:lightgrey;font-weight:bold">excess digits</span>     that may follow beyond the desired precision,</li><li>returns optional   <span class="gmail-grouprepetition" title="group 3 selection" style="background-color:aquamarine;font-weight:bold">(</span><span class="gmail-float" title="leading digits" style="background-color:lightsteelblue;font-weight:bold">third  group</span><span class="gmail-grouprepetition" title="group 3 selection" style="background-color:aquamarine;font-weight:bold">)</span>    including any trailing scientific notation, if present (for example <code><b>E-04</b></code>), and</li><li>ignores trailing      <span class="gmail-whitespace" title="optional whitespace" style="background-color:lightgrey;font-weight:bold">whitespace</span>       including optional comma, which is an allowed X3D whitespace character.</li></ul></ul><table border="1" align="center" style="font-family:"Times New Roman""><tbody><tr><th align="center">Significant-digits Precision Pattern</th></tr><tr><td>Truncate following <b><code>4</code></b> digits of precision after decimal point, without rounding<br><i>XML Schema:</i>   <code><xs:restriction base="xs:decimal"/></code> controls type but not precision or significant digits<br><i>Regex pattern:</i>  <code><span class="gmail-whitespace" title="optional whitespace" style="background-color:lightgrey;font-weight:bold">\s*</span><span class="gmail-grouprepetition" title="group 1 selection" style="background-color:aquamarine;font-weight:bold">(</span><span class="gmail-float" title="optional + or - sign" style="background-color:lightsteelblue;font-weight:bold">[+-]?</span><span class="gmail-float" title="leading digits" style="background-color:lightsteelblue;font-weight:bold">\d*</span><span class="gmail-grouprepetition" title="group 1 selection" style="background-color:aquamarine;font-weight:bold">)</span><span class="gmail-grouprepetition" title="group 2 selection" style="background-color:aquamarine;font-weight:bold">(</span><span class="gmail-float" title="decimal point" style="background-color:lightsteelblue;font-weight:bold">\.</span><span class="gmail-float" title="trailing digits" style="background-color:lightsteelblue;font-weight:bold">\d{0,<b>4</b>}</span><span class="gmail-grouprepetition" title="group 2 selection" style="background-color:aquamarine;font-weight:bold">)</span><span class="gmail-whitespace" title="omitted excess digits" style="background-color:lightgrey;font-weight:bold">\d*</span><span class="gmail-grouprepetition" title="group 3 selection" style="background-color:aquamarine;font-weight:bold">(</span><span class="gmail-float" title="scientific notation exponent" style="background-color:lightsteelblue;font-weight:bold">[Ee][+-]?\d+</span><span class="gmail-grouprepetition" title="group 3 selection" style="background-color:aquamarine;font-weight:bold">)</span><span class="gmail-whitespace" title="optional whitespace" style="background-color:lightgrey;font-weight:bold">\s*</span><span class="gmail-whitespace" title="comma is optional whitespace" style="background-color:lightgrey;font-weight:bold">[,]?</span></code></td></tr><tr><td>regex101 examples: <a href="https://regex101.com/r/mjYeZe/latest">numeric filter, significant digits precision</a></td></tr></tbody></table></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><p style="color:rgb(0,0,0);font-family:"Times New Roman"">The pattern truncates following the desired digits of precision after decimal point, without rounding. If present, any trailing zeros within the significant digits are retained. Thus additional preprocessing might be necessary for rounding off positive/negative values and trimming trailing zeros (as accomplished in <a href="https://www.web3d.org/x3d/stylesheets/X3dTidy.html" target="_blank">X3dTidy XSLT stylesheet</a>).</p></div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><p style="color:rgb(0,0,0);font-family:"Times New Roman"">Since X3D models are typically defined using default units of meters for length, and radians for angle values, four digits of precision may be satisfactory for many purposes. The nature and purpose of data in the model itself matters, so if in doubt use higher precision.</p></div></blockquote><div><ul style="color:rgb(0,0,0);font-family:"Times New Roman""><ul><li><b>0.0001 meters</b>  equals <b>one-tenth of a millimeter</b>.</li><li><b>0.0001 radians</b> equals <b>0.0057 degrees</b>.</li></ul></ul></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><p style="color:rgb(0,0,0);font-family:"Times New Roman""><i style="">Warning:</i> authors are advised to <b style="">not</b> apply this regex if preservation of full-precision values might be necessary for preserving archival correctness, or for maintaining verifiable <a href="https://en.wiktionary.org/wiki/provenance" target="_blank" style="">data provenance</a> of an original 3D dataset.</p></div></blockquote><div><p style="color:rgb(0,0,0)"><font face="arial, sans-serif"><br></font></p><p style="color:rgb(0,0,0)"><font face="arial, sans-serif">This capability is now deployed in X3dTidy.xslt stylesheet, and the interim release of <a href="https://www.web3d.org/x3d/tools/X3D-Edit/X3D-Edit.html">X3D-Edit</a>. Authors can pick however many significant digits they want to retain, the offered default is 4 digits.</font></p><p style="color:rgb(0,0,0)"><font face="arial, sans-serif">Thanks to an interesting suggestion by Carol McDonald, the implementation also includes a related option to retain trailing zeros.  That is sometimes useful as an indication of expected precision, perhaps even as a contract requirement.</font></p><p style="color:rgb(0,0,0)"><font face="arial, sans-serif">Here is a screenshot image showing the <a href="https://www.web3d.org/x3d/tools/X3D-Edit/images/X3D-EditX3D-TidyPanel.png">X3D-Edit panel for X3D Tidy settings</a> that authors can use.</font></p><p style="color:rgb(0,0,0)">I am now going through all of our (nearly 4,000) examples by applying X3D Tidy during the global build process, reviewing change logs, then visually confirming results are OK before committing each update.  The next message describes results seen in the Humanoid Animation (HAnim) example archives.</p><p style="color:rgb(0,0,0)">Onward we go, am looking forward to further X3D model refinements.  Have fun with X3D and HAnim!  😃 👍</p></div><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div style="color:rgb(34,34,34)"><font face="monospace">all the best, Don</font></div><div style="color:rgb(34,34,34)"><font face="monospace">-- </font></div><div style="color:rgb(34,34,34)"><font face="monospace">X3D Graphics, Maritime Robotics, Distributed Simulation</font></div><div style="color:rgb(34,34,34)"><font face="monospace">Relative Motion Consulting  <a href="https://RelativeMotion.info" target="_blank">https://RelativeMotion.info</a></font></div></div></div></div></div>