jscad / OpenJSCAD.org

JSCAD is an open source set of modular, browser and command line tools for creating parametric 2D and 3D designs with JavaScript code. It provides a quick, precise and reproducible method for generating 3D models, and is especially useful for 3D printing applications.
https://openjscad.xyz/
MIT License
2.65k stars 513 forks source link

Rounding issues #276

Closed udif closed 6 years ago

udif commented 7 years ago

OpenJSCAD is not rounding numbers when it should, while importing SVG:

http://labs.koenklaren.nl/kodi-logo/kodi-logo.svg

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 18.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Kodi_Logo_1_" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
     y="0px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<g id="Kodi_Logo">
    <g>
        <path fill="#2E3192" d="M171.1,374.8c-6.2,6.2-6.2,16.4,0,22.6l73.5,73.5c6.2,6.2,16.4,6.2,22.6,0l73.5-73.5
            c6.2-6.2,6.2-16.4,0-22.6l-73.5-73.5c-6.2-6.2-16.4-6.2-22.6,0L171.1,374.8z"/>
    </g>
    <g>
        <path fill="#2E3192" d="M301.3,244.7c-6.2,6.2-6.2,16.4,0,22.6l73.5,73.5c6.2,6.2,16.4,6.2,22.6,0l73.5-73.5
            c6.2-6.2,6.2-16.4,0-22.6l-73.5-73.5c-6.2-6.2-16.4-6.2-22.6,0L301.3,244.7z"/>
    </g>
    <g>
        <path fill="#2E3192" d="M126.1,175.7c0-8.8-5.1-10.9-11.3-4.7L41,244.7c-6.2,6.2-6.2,16.4,0,22.6l73.7,73.7
            c6.2,6.2,11.3,4.1,11.3-4.7V175.7z"/>
    </g>
    <g>
        <path fill="#2E3192" d="M267.3,41c-6.2-6.2-16.4-6.2-22.6,0l-59.2,59.2c-6.2,6.2-11.3,18.5-11.3,27.3l-0.1,160.6
            c0,8.8,5.1,10.9,11.3,4.7l73.6-73.8c6.2-6.2,16.4-16.4,22.6-22.6l59.2-59.2c6.2-6.2,6.2-16.4,0-22.6L267.3,41z"/>
    </g>
</g>
</svg>

Produces this:

//
// producer: OpenJSCAD.org 0.5.2 (2016/10/01) SVG Importer
// date: Mon May 29 2017 00:45:39 GMT+0300 (Jerusalem Daylight Time)
// source: kodi-logo.svg
//
function main(params) {
  var cag0 = new CAG();
    var cag1 = new CAG();
      var cag2 = new CAG();
      var cag20 = new CAG();
      var cag201 = new CSG.Path2D([[48.28821841999999,-105.77688056]],false);
      cag201 = cag201.appendBezier([[46.538440779999995,-107.52665819999999],[46.538440779999995,-110.40532463999999],[48.28821841999999,-112.15510228]]);
      cag201 = cag201.appendPoint([69.03155011999999,-132.89843398]);
      cag201 = cag201.appendBezier([[70.78132775999998,-134.64821161999998],[73.65999419999999,-134.64821161999998],[75.40977183999999,-132.89843398]]);
      cag201 = cag201.appendPoint([96.15310353999999,-112.15510228]);
      cag201 = cag201.appendBezier([[97.90288117999998,-110.40532464],[97.90288117999998,-107.5266582],[96.15310353999999,-105.77688056]]);
      cag201 = cag201.appendPoint([75.40977183999999,-85.03354886]);
      cag201 = cag201.appendBezier([[73.65999419999999,-83.28377121999999],[70.78132775999998,-83.28377121999999],[69.03155011999999,-85.03354886]]);
      cag201 = cag201.appendPoint([48.28821841999999,-105.77688056]);

..... I've trimmed the rest, I think it's pretty obvious: Notice the 99999's after the 8th digit (there are also 00000's in the trimmed part not here)

z3dev commented 7 years ago

@udif Excellent observation. We now have some standards for the precision, which should be applied here.

Do you need a quick fix?

z3dev commented 7 years ago

Actually, I remembered why there's "scaling" now.

Basically, SVG documents have an default size of 100%. What 100% means depends on the device being used, i.e. Screen, printer, plotter, etc.

If you KNOW the size required then set width and height on the SVG document.

If you don't provide width or height then OpenJSCAD uses the standard screen size (in pixels) to scale the SVG to fit the screen.

Yes. Rounding will occur.

z3dev commented 6 years ago

Also, view box does mean much... it just sets the outside dimensions of the view. It does NOT set the size or precision of the device. The viewpoint of a computer screen and the viewpoint of a printer maybe totally different.

Maybe this is why Microsoft cannot create documents that fit a "page".