LaserWeb / LaserWeb4

Collaborative effort on the next version of LaserWeb / CNCWeb
GNU Affero General Public License v3.0
714 stars 193 forks source link

JavaScript rounding error leads to incorrect document sizes #479

Closed llamasoft closed 6 years ago

llamasoft commented 6 years ago

Here is a workspace demonstrating the above phenomenon: workspace.json.txt
The workspace didn't capture it, but the "bitmap DPI" setting is 127.

The workspace shows that the image is 2 pixels tall, but at 127 DPI (5 PPI) it should result in a size of 0.4mm, but it instead results in 0.3999...7mm. This causes the resulting gcode to only raster one line of pixels, not two.

  "originalPixels": [17, 2],
  "originalSize": [3.4, 0.39999999999999997]

This rounding error is because of the DPI to DPM conversion which scales by 25.4 / settings.dpiBitmap. The root cause is that 25.4 doesn't have an exact floating point representation, which leads to the rounding error when dividing it.
This rounding error can be accounted for a few different ways:

(I'm planning to submit a pull request for this in the next day or two. I just wanted to give you some context before it shows up.)