diff options
16 files changed, 110 insertions, 10 deletions
| diff --git a/src/primitives.cc b/src/primitives.cc index 13a6794..a587d43 100644 --- a/src/primitives.cc +++ b/src/primitives.cc @@ -57,6 +57,8 @@ public:  	primitive_type_e type;  	PrimitiveModule(primitive_type_e type) : type(type) { }  	virtual AbstractNode *instantiate(const Context *ctx, const ModuleInstantiation *inst, const EvalContext *evalctx) const; +private: +	Value lookup_radius(const Context &ctx, const std::string &radius_var, const std::string &diameter_var) const;  };  class PrimitiveNode : public AbstractPolyNode @@ -105,6 +107,35 @@ public:  	virtual PolySet *evaluate_polyset(class PolySetEvaluator *) const;  }; +/** + * Return a radius value by looking up both a diameter and radius variable. + * The diameter has higher priority, so if found an additionally set radius + * value is ignored. + *  + * @param ctx data context with variable values. + * @param radius_var name of the variable to lookup for the radius value. + * @param diameter_var name of the variable to lookup for the diameter value. + * @return radius value of type Value::NUMBER or Value::UNDEFINED if both + *         variables are invalid or not set. + */ +Value PrimitiveModule::lookup_radius(const Context &ctx, const std::string &diameter_var, const std::string &radius_var) const +{ +	const Value d = ctx.lookup_variable(diameter_var, true); +	const Value r = ctx.lookup_variable(radius_var, true); +	const bool r_defined = (r.type() == Value::NUMBER); +	 +	if (d.type() == Value::NUMBER) { +		if (r_defined) { +			PRINTB("WARNING: Ignoring radius variable '%s' as diameter '%s' is defined too.", radius_var % diameter_var); +		} +		return Value(d.toDouble() / 2.0); +	} else if (r_defined) { +		return r; +	} else { +		return Value(); +	} +} +  AbstractNode *PrimitiveModule::instantiate(const Context *ctx, const ModuleInstantiation *inst, const EvalContext *evalctx) const  {  	PrimitiveNode *node = new PrimitiveNode(inst, this->type); @@ -170,22 +201,21 @@ AbstractNode *PrimitiveModule::instantiate(const Context *ctx, const ModuleInsta  	}  	if (type == SPHERE) { -		Value r = c.lookup_variable("r"); +		const Value r = lookup_radius(c, "d", "r");  		if (r.type() == Value::NUMBER) {  			node->r1 = r.toDouble();  		}  	}  	if (type == CYLINDER) { -		Value h = c.lookup_variable("h"); -		Value r, r1, r2; -		r1 = c.lookup_variable("r1"); -		r2 = c.lookup_variable("r2"); -		r = c.lookup_variable("r", true); // silence warning since r has no default value -		Value center = c.lookup_variable("center"); +		const Value h = c.lookup_variable("h");  		if (h.type() == Value::NUMBER) {  			node->h = h.toDouble();  		} + +		const Value r = lookup_radius(c, "d", "r"); +		const Value r1 = lookup_radius(c, "d1", "r1"); +		const Value r2 = lookup_radius(c, "d2", "r2");  		if (r.type() == Value::NUMBER) {  			node->r1 = r.toDouble();  			node->r2 = r.toDouble(); @@ -196,6 +226,8 @@ AbstractNode *PrimitiveModule::instantiate(const Context *ctx, const ModuleInsta  		if (r2.type() == Value::NUMBER) {  			node->r2 = r2.toDouble();  		} +		 +		const Value center = c.lookup_variable("center");  		if (center.type() == Value::BOOL) {  			node->center = center.toBool();  		} @@ -218,7 +250,7 @@ AbstractNode *PrimitiveModule::instantiate(const Context *ctx, const ModuleInsta  	}  	if (type == CIRCLE) { -		Value r = c.lookup_variable("r"); +		const Value r = lookup_radius(c, "d", "r");  		if (r.type() == Value::NUMBER) {  			node->r1 = r.toDouble();  		} diff --git a/testdata/scad/features/circle-tests.scad b/testdata/scad/features/circle-tests.scad index 90cd9f6..57b1992 100644 --- a/testdata/scad/features/circle-tests.scad +++ b/testdata/scad/features/circle-tests.scad @@ -8,4 +8,6 @@ translate([6,-3,0]) circle(1, $fn=12);  translate([0,-6,0]) circle(1, $fa=20, $fs=0.3);  translate([3,-6,0]) circle(1, $fa=30, $fs=0.3);  translate([6,-6,0]) circle(1, $fa=40, $fs=0.3); -translate([3,-9,0]) circle(1, $fn=0.1); +translate([0,-9,0]) circle(1, $fn=0.1); +translate([3,-9,0]) circle(d=2, $fn=8); +translate([6,-9,0]) circle(r=10, d=2, $fn=8); diff --git a/testdata/scad/features/cylinder-diameter-tests.scad b/testdata/scad/features/cylinder-diameter-tests.scad new file mode 100644 index 0000000..57eab65 --- /dev/null +++ b/testdata/scad/features/cylinder-diameter-tests.scad @@ -0,0 +1,13 @@ +cylinder(); +translate([-11,-11,0]) cylinder(h=3, r=5); +translate([-11,  0,0]) cylinder(h=5, d=10); +translate([-11, 11,0]) cylinder(h=7, r=1, d=10); +translate([ 11,-11,0]) cylinder(h=5, r1=5); +translate([ 11,  0,0]) cylinder(h=7, d1=10); +translate([ 11, 11,0]) cylinder(h=9, r1=1, d1=10); +translate([ 22,-11,0]) cylinder(h=5, r2=5); +translate([ 22,  0,0]) cylinder(h=7, d2=10); +translate([ 22, 11,0]) cylinder(h=9, r2=1, d2=10); +translate([ 33,-11,0]) cylinder(h=5, r1=5, r2=5); +translate([ 33,  0,0]) cylinder(h=7, d1=10, d2=10); +translate([ 33, 11,0]) cylinder(h=9, r1=1, d1=10, r2=1, d2=10); diff --git a/testdata/scad/features/sphere-tests.scad b/testdata/scad/features/sphere-tests.scad index cc80738..5c38f6b 100644 --- a/testdata/scad/features/sphere-tests.scad +++ b/testdata/scad/features/sphere-tests.scad @@ -9,3 +9,5 @@ translate([22,-11, 0]) sphere(5, $fa=20, $fs=0.3);  translate([22,  0, 0]) sphere(5, $fa=30, $fs=0.3);  translate([22, 11, 0]) sphere(5, $fa=40, $fs=0.3);  translate([11, 22, 0]) sphere(5, $fn=0.1); +translate([33,  0, 0]) sphere(d=10); +translate([33, 11, 0]) sphere(r=1, d=10); diff --git a/tests/regression/cgalpngtest/circle-tests-expected.png b/tests/regression/cgalpngtest/circle-tests-expected.pngBinary files differ index d640042..39a3481 100644 --- a/tests/regression/cgalpngtest/circle-tests-expected.png +++ b/tests/regression/cgalpngtest/circle-tests-expected.png diff --git a/tests/regression/cgalpngtest/cylinder-diameter-tests-expected.png b/tests/regression/cgalpngtest/cylinder-diameter-tests-expected.pngBinary files differ new file mode 100644 index 0000000..0877e5b --- /dev/null +++ b/tests/regression/cgalpngtest/cylinder-diameter-tests-expected.png diff --git a/tests/regression/cgalpngtest/sphere-tests-expected.png b/tests/regression/cgalpngtest/sphere-tests-expected.pngBinary files differ index 781d8a9..fd1549f 100644 --- a/tests/regression/cgalpngtest/sphere-tests-expected.png +++ b/tests/regression/cgalpngtest/sphere-tests-expected.png diff --git a/tests/regression/dumptest/circle-tests-expected.csg b/tests/regression/dumptest/circle-tests-expected.csg index b522850..3e183ee 100644 --- a/tests/regression/dumptest/circle-tests-expected.csg +++ b/tests/regression/dumptest/circle-tests-expected.csg @@ -27,7 +27,13 @@ group() {  	multmatrix([[1, 0, 0, 6], [0, 1, 0, -6], [0, 0, 1, 0], [0, 0, 0, 1]]) {  		circle($fn = 0, $fa = 40, $fs = 0.3, r = 1);  	} -	multmatrix([[1, 0, 0, 3], [0, 1, 0, -9], [0, 0, 1, 0], [0, 0, 0, 1]]) { +	multmatrix([[1, 0, 0, 0], [0, 1, 0, -9], [0, 0, 1, 0], [0, 0, 0, 1]]) {  		circle($fn = 0.1, $fa = 12, $fs = 2, r = 1);  	} +	multmatrix([[1, 0, 0, 3], [0, 1, 0, -9], [0, 0, 1, 0], [0, 0, 0, 1]]) { +		circle($fn = 8, $fa = 12, $fs = 2, r = 1); +	} +	multmatrix([[1, 0, 0, 6], [0, 1, 0, -9], [0, 0, 1, 0], [0, 0, 0, 1]]) { +		circle($fn = 8, $fa = 12, $fs = 2, r = 1); +	}  } diff --git a/tests/regression/dumptest/cylinder-diameter-tests-expected.csg b/tests/regression/dumptest/cylinder-diameter-tests-expected.csg new file mode 100644 index 0000000..98fab0d --- /dev/null +++ b/tests/regression/dumptest/cylinder-diameter-tests-expected.csg @@ -0,0 +1,39 @@ +group() { +	cylinder($fn = 0, $fa = 12, $fs = 2, h = 1, r1 = 1, r2 = 1, center = false); +	multmatrix([[1, 0, 0, -11], [0, 1, 0, -11], [0, 0, 1, 0], [0, 0, 0, 1]]) { +		cylinder($fn = 0, $fa = 12, $fs = 2, h = 3, r1 = 5, r2 = 5, center = false); +	} +	multmatrix([[1, 0, 0, -11], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) { +		cylinder($fn = 0, $fa = 12, $fs = 2, h = 5, r1 = 5, r2 = 5, center = false); +	} +	multmatrix([[1, 0, 0, -11], [0, 1, 0, 11], [0, 0, 1, 0], [0, 0, 0, 1]]) { +		cylinder($fn = 0, $fa = 12, $fs = 2, h = 7, r1 = 5, r2 = 5, center = false); +	} +	multmatrix([[1, 0, 0, 11], [0, 1, 0, -11], [0, 0, 1, 0], [0, 0, 0, 1]]) { +		cylinder($fn = 0, $fa = 12, $fs = 2, h = 5, r1 = 5, r2 = 1, center = false); +	} +	multmatrix([[1, 0, 0, 11], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) { +		cylinder($fn = 0, $fa = 12, $fs = 2, h = 7, r1 = 5, r2 = 1, center = false); +	} +	multmatrix([[1, 0, 0, 11], [0, 1, 0, 11], [0, 0, 1, 0], [0, 0, 0, 1]]) { +		cylinder($fn = 0, $fa = 12, $fs = 2, h = 9, r1 = 5, r2 = 1, center = false); +	} +	multmatrix([[1, 0, 0, 22], [0, 1, 0, -11], [0, 0, 1, 0], [0, 0, 0, 1]]) { +		cylinder($fn = 0, $fa = 12, $fs = 2, h = 5, r1 = 1, r2 = 5, center = false); +	} +	multmatrix([[1, 0, 0, 22], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) { +		cylinder($fn = 0, $fa = 12, $fs = 2, h = 7, r1 = 1, r2 = 5, center = false); +	} +	multmatrix([[1, 0, 0, 22], [0, 1, 0, 11], [0, 0, 1, 0], [0, 0, 0, 1]]) { +		cylinder($fn = 0, $fa = 12, $fs = 2, h = 9, r1 = 1, r2 = 5, center = false); +	} +	multmatrix([[1, 0, 0, 33], [0, 1, 0, -11], [0, 0, 1, 0], [0, 0, 0, 1]]) { +		cylinder($fn = 0, $fa = 12, $fs = 2, h = 5, r1 = 5, r2 = 5, center = false); +	} +	multmatrix([[1, 0, 0, 33], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) { +		cylinder($fn = 0, $fa = 12, $fs = 2, h = 7, r1 = 5, r2 = 5, center = false); +	} +	multmatrix([[1, 0, 0, 33], [0, 1, 0, 11], [0, 0, 1, 0], [0, 0, 0, 1]]) { +		cylinder($fn = 0, $fa = 12, $fs = 2, h = 9, r1 = 5, r2 = 5, center = false); +	} +} diff --git a/tests/regression/dumptest/sphere-tests-expected.csg b/tests/regression/dumptest/sphere-tests-expected.csg index 45c0858..783b7e8 100644 --- a/tests/regression/dumptest/sphere-tests-expected.csg +++ b/tests/regression/dumptest/sphere-tests-expected.csg @@ -30,4 +30,10 @@ group() {  	multmatrix([[1, 0, 0, 11], [0, 1, 0, 22], [0, 0, 1, 0], [0, 0, 0, 1]]) {  		sphere($fn = 0.1, $fa = 12, $fs = 2, r = 5);  	} +	multmatrix([[1, 0, 0, 33], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) { +		sphere($fn = 0, $fa = 12, $fs = 2, r = 5); +	} +	multmatrix([[1, 0, 0, 33], [0, 1, 0, 11], [0, 0, 1, 0], [0, 0, 0, 1]]) { +		sphere($fn = 0, $fa = 12, $fs = 2, r = 5); +	}  } diff --git a/tests/regression/opencsgtest/circle-tests-expected.png b/tests/regression/opencsgtest/circle-tests-expected.pngBinary files differ index 06f7d9c..20f9ca2 100644 --- a/tests/regression/opencsgtest/circle-tests-expected.png +++ b/tests/regression/opencsgtest/circle-tests-expected.png diff --git a/tests/regression/opencsgtest/cylinder-diameter-tests-expected.png b/tests/regression/opencsgtest/cylinder-diameter-tests-expected.pngBinary files differ new file mode 100644 index 0000000..7425635 --- /dev/null +++ b/tests/regression/opencsgtest/cylinder-diameter-tests-expected.png diff --git a/tests/regression/opencsgtest/sphere-tests-expected.png b/tests/regression/opencsgtest/sphere-tests-expected.pngBinary files differ index d1b4845..65a6cf2 100644 --- a/tests/regression/opencsgtest/sphere-tests-expected.png +++ b/tests/regression/opencsgtest/sphere-tests-expected.png diff --git a/tests/regression/throwntogethertest/circle-tests-expected.png b/tests/regression/throwntogethertest/circle-tests-expected.pngBinary files differ index 06f7d9c..2d5bde0 100644 --- a/tests/regression/throwntogethertest/circle-tests-expected.png +++ b/tests/regression/throwntogethertest/circle-tests-expected.png diff --git a/tests/regression/throwntogethertest/cylinder-diameter-tests-expected.png b/tests/regression/throwntogethertest/cylinder-diameter-tests-expected.pngBinary files differ new file mode 100644 index 0000000..7425635 --- /dev/null +++ b/tests/regression/throwntogethertest/cylinder-diameter-tests-expected.png diff --git a/tests/regression/throwntogethertest/sphere-tests-expected.png b/tests/regression/throwntogethertest/sphere-tests-expected.pngBinary files differ index d1b4845..65a6cf2 100644 --- a/tests/regression/throwntogethertest/sphere-tests-expected.png +++ b/tests/regression/throwntogethertest/sphere-tests-expected.png | 
