#1479 Be more robust when passed invalid value

This commit is contained in:
Marius Kintel 2015-11-15 01:53:24 -05:00
parent 9511a12eb4
commit a7ee1f2bcd
6 changed files with 9 additions and 1 deletions

View file

@ -26,6 +26,7 @@
#include "calc.h" #include "calc.h"
#include "grid.h" #include "grid.h"
#include <boost/math/special_functions/fpclassify.hpp>
/*! /*!
Returns the number of subdivision of a whole circle, given radius and Returns the number of subdivision of a whole circle, given radius and
@ -33,7 +34,9 @@
*/ */
int Calc::get_fragments_from_r(double r, double fn, double fs, double fa) int Calc::get_fragments_from_r(double r, double fn, double fs, double fa)
{ {
if (r < GRID_FINE) return 3; // FIXME: It would be better to refuse to create an object. Let's do more strict error handling
// in future versions of OpenSCAD
if (r < GRID_FINE || boost::math::isinf(fn) || boost::math::isnan(fn)) return 3;
if (fn > 0.0) return (int)(fn >= 3 ? fn : 3); if (fn > 0.0) return (int)(fn >= 3 ? fn : 3);
return (int)ceil(fmax(fmin(360.0 / fa, r*2*M_PI / fs), 5)); return (int)ceil(fmax(fmin(360.0 / fa, r*2*M_PI / fs), 5));
} }

View file

@ -7,3 +7,6 @@ sphere(1/0);
polygon([[0,0,0],[1,0,0],[1,1/0,0]]); polygon([[0,0,0],[1,0,0],[1,1/0,0]]);
polyhedron(points = [[1/0,0,0],[-1,0,0],[0,1,0],[0,-1,0],[0,0,1],[0,0,-1]], polyhedron(points = [[1/0,0,0],[-1,0,0],[0,1,0],[0,-1,0],[0,0,1],[0,0,-1]],
triangles = [[0,4,2],[0,2,5],[0,3,4],[0,5,3],[1,2,4],[1,5,2],[1,4,3], [1,3,5]]); triangles = [[0,4,2],[0,2,5],[0,3,4],[0,5,3],[1,2,4],[1,5,2],[1,4,3], [1,3,5]]);
cylinder($fn=1/0);
sphere($fn=1/0);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.3 KiB

After

Width:  |  Height:  |  Size: 7 KiB

View file

@ -7,4 +7,6 @@ group() {
sphere($fn = 0, $fa = 12, $fs = 2, r = inf); sphere($fn = 0, $fa = 12, $fs = 2, r = inf);
polygon(points = [[0, 0, 0], [1, 0, 0], [1, inf, 0]], paths = undef, convexity = 1); polygon(points = [[0, 0, 0], [1, 0, 0], [1, inf, 0]], paths = undef, convexity = 1);
polyhedron(points = [[inf, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0], [0, 0, 1], [0, 0, -1]], faces = [[0, 4, 2], [0, 2, 5], [0, 3, 4], [0, 5, 3], [1, 2, 4], [1, 5, 2], [1, 4, 3], [1, 3, 5]], convexity = 1); polyhedron(points = [[inf, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0], [0, 0, 1], [0, 0, -1]], faces = [[0, 4, 2], [0, 2, 5], [0, 3, 4], [0, 5, 3], [1, 2, 4], [1, 5, 2], [1, 4, 3], [1, 3, 5]], convexity = 1);
cylinder($fn = inf, $fa = 12, $fs = 2, h = 1, r1 = 1, r2 = 1, center = false);
sphere($fn = inf, $fa = 12, $fs = 2, r = 1);
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.3 KiB

After

Width:  |  Height:  |  Size: 7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.3 KiB

After

Width:  |  Height:  |  Size: 7 KiB