Skip to content

Commit

Permalink
Merge pull request #1478 from openscad/rotate_extrude-angle
Browse files Browse the repository at this point in the history
Angle parameter for rotate_extrude
  • Loading branch information
kintel committed Nov 20, 2015
2 parents da79f97 + 28d27c3 commit ea50b90
Show file tree
Hide file tree
Showing 22 changed files with 381 additions and 77 deletions.
26 changes: 23 additions & 3 deletions examples/Basics/rotate_extrude.scad
@@ -1,8 +1,8 @@
echo(version=version());

// rotate_extrude() always rotates the 2D shape 360 degrees
// around the Z axis. Note that the 2D shape must be either
// completely on the positive or negative side of the X axis.
// rotate_extrude() rotates a 2D shape around the Z axis.
// Note that the 2D shape must be either completely on the
// positive or negative side of the X axis.
color("red")
rotate_extrude()
translate([10, 0])
Expand All @@ -23,6 +23,26 @@ color("green")
polygon( points=[[0,0],[8,4],[4,8],[4,12],[12,16],[0,20]] );


// By default rotate_extrude forms a full 360 degree circle,
// but a partial rotation can be performed by using the angle parameter.
// Positive angles create an arc starting from the X axis, going counter-clockwise.
// Negative angles generate an arc in the clockwise direction.
color("magenta")
translate([40,40]){
rotate_extrude(angle=180)
translate([12.5,0])
square(5);
translate([7.5,0])
rotate_extrude(angle=180)
translate([5,0])
square(5);
translate([-7.5,0])
rotate_extrude(angle=-180)
translate([5,0])
square(5);
}


// Written in 2015 by Torsten Paul <Torsten.Paul@gmx.de>
//
// To the extent possible under law, the author(s) have dedicated all
Expand Down
70 changes: 58 additions & 12 deletions src/GeometryEvaluator.cc
Expand Up @@ -761,12 +761,21 @@ Response GeometryEvaluator::visit(State &state, const LinearExtrudeNode &node)
return ContinueTraversal;
}

static void fill_ring(std::vector<Vector3d> &ring, const Outline2d &o, double a)
static void fill_ring(std::vector<Vector3d> &ring, const Outline2d &o, double a, bool flip)
{
for (unsigned int i=0;i<o.vertices.size();i++) {
ring[i][0] = o.vertices[i][0] * sin(a);
ring[i][1] = o.vertices[i][0] * cos(a);
ring[i][2] = o.vertices[i][1];
if (flip) {
unsigned int l = o.vertices.size()-1;
for (unsigned int i=0 ;i<o.vertices.size();i++) {
ring[i][0] = o.vertices[l-i][0] * sin(a);
ring[i][1] = o.vertices[l-i][0] * cos(a);
ring[i][2] = o.vertices[l-i][1];
}
} else {
for (unsigned int i=0 ;i<o.vertices.size();i++) {
ring[i][0] = o.vertices[i][0] * sin(a);
ring[i][1] = o.vertices[i][0] * cos(a);
ring[i][2] = o.vertices[i][1];
}
}
}

Expand All @@ -790,12 +799,15 @@ static void fill_ring(std::vector<Vector3d> &ring, const Outline2d &o, double a)
*/
static Geometry *rotatePolygon(const RotateExtrudeNode &node, const Polygon2d &poly)
{
if (node.angle == 0) return NULL;

PolySet *ps = new PolySet(3);
ps->setConvexity(node.convexity);


double min_x = 0;
double max_x = 0;
int fragments = 0;
BOOST_FOREACH(const Outline2d &o, poly.outlines()) {
double min_x = 0;
double max_x = 0;
BOOST_FOREACH(const Vector2d &v, o.vertices) {
min_x = fmin(min_x, v[0]);
max_x = fmax(max_x, v[0]);
Expand All @@ -806,16 +818,49 @@ static Geometry *rotatePolygon(const RotateExtrudeNode &node, const Polygon2d &p
return NULL;
}
}
int fragments = Calc::get_fragments_from_r(max_x - min_x, node.fn, node.fs, node.fa);
fragments = fmax(Calc::get_fragments_from_r(max_x - min_x, node.fn, node.fs, node.fa) * std::abs(node.angle) / 360, 1);
}

bool flip_faces = (min_x >= 0 && node.angle > 0 && node.angle != 360) || (min_x < 0 && (node.angle < 0 || node.angle == 360));

if (node.angle != 360) {
PolySet *ps_start = poly.tessellate(); // starting face
Transform3d rot(Eigen::AngleAxisd(M_PI/2, Vector3d::UnitX()));
ps_start->transform(rot);
// Flip vertex ordering
if (!flip_faces) {
BOOST_FOREACH(Polygon &p, ps_start->polygons) {
std::reverse(p.begin(), p.end());
}
}
ps->append(*ps_start);
delete ps_start;

PolySet *ps_end = poly.tessellate();
Transform3d rot2(Eigen::AngleAxisd(node.angle*M_PI/180, Vector3d::UnitZ()) * Eigen::AngleAxisd(M_PI/2, Vector3d::UnitX()));
ps_end->transform(rot2);
if (flip_faces) {
BOOST_FOREACH(Polygon &p, ps_end->polygons) {
std::reverse(p.begin(), p.end());
}
}
ps->append(*ps_end);
delete ps_end;
}

BOOST_FOREACH(const Outline2d &o, poly.outlines()) {
std::vector<Vector3d> rings[2];
rings[0].resize(o.vertices.size());
rings[1].resize(o.vertices.size());

fill_ring(rings[0], o, -M_PI/2); // first ring
fill_ring(rings[0], o, (node.angle == 360) ? -M_PI/2 : M_PI/2, flip_faces); // first ring
for (int j = 0; j < fragments; j++) {
double a = ((j+1)%fragments*2*M_PI) / fragments - M_PI/2; // start on the X axis
fill_ring(rings[(j+1)%2], o, a);
double a;
if (node.angle == 360)
a = -M_PI/2 + ((j+1)%fragments*2*M_PI) / fragments; // start on the -X axis, for legacy support
else
a = M_PI/2 - (j+1)*(node.angle*M_PI/180) / fragments; // start on the X axis
fill_ring(rings[(j+1)%2], o, a, flip_faces);

for (size_t i=0;i<o.vertices.size();i++) {
ps->append_poly();
Expand All @@ -829,6 +874,7 @@ static Geometry *rotatePolygon(const RotateExtrudeNode &node, const Polygon2d &p
}
}
}

return ps;
}

Expand Down
10 changes: 9 additions & 1 deletion src/rotateextrude.cc
Expand Up @@ -61,13 +61,15 @@ AbstractNode *RotateExtrudeModule::instantiate(const Context *ctx, const ModuleI
node->fn = c.lookup_variable("$fn")->toDouble();
node->fs = c.lookup_variable("$fs")->toDouble();
node->fa = c.lookup_variable("$fa")->toDouble();


ValuePtr file = c.lookup_variable("file");
ValuePtr layer = c.lookup_variable("layer", true);
ValuePtr convexity = c.lookup_variable("convexity", true);
ValuePtr origin = c.lookup_variable("origin", true);
ValuePtr scale = c.lookup_variable("scale", true);

ValuePtr angle = c.lookup_variable("angle", true);

if (!file->isUndefined()) {
printDeprecation("Support for reading files in rotate_extrude will be removed in future releases. Use a child import() instead.");
node->filename = lookup_file(file->toString(), inst->path(), c.documentPath());
Expand All @@ -77,13 +79,18 @@ AbstractNode *RotateExtrudeModule::instantiate(const Context *ctx, const ModuleI
node->convexity = (int)convexity->toDouble();
origin->getVec2(node->origin_x, node->origin_y);
node->scale = scale->toDouble();
node->angle = 360;
angle->getFiniteDouble(node->angle);

if (node->convexity <= 0)
node->convexity = 2;

if (node->scale <= 0)
node->scale = 1;

if ((node->angle <= -360) || (node->angle > 360))
node->angle = 360;

if (node->filename.empty()) {
std::vector<AbstractNode *> instantiatednodes = inst->instantiateChildren(evalctx);
node->children.insert(node->children.end(), instantiatednodes.begin(), instantiatednodes.end());
Expand All @@ -108,6 +115,7 @@ std::string RotateExtrudeNode::toString() const
;
}
stream <<
"angle = " << this->angle << ", "
"convexity = " << this->convexity << ", "
"$fn = " << this->fn << ", $fa = " << this->fa << ", $fs = " << this->fs << ")";

Expand Down
3 changes: 2 additions & 1 deletion src/rotateextrudenode.h
Expand Up @@ -11,6 +11,7 @@ class RotateExtrudeNode : public AbstractPolyNode
convexity = 0;
fn = fs = fa = 0;
origin_x = origin_y = scale = 0;
angle = 360;
}
virtual Response accept(class State &state, Visitor &visitor) const {
return visitor.visit(state, *this);
Expand All @@ -20,7 +21,7 @@ class RotateExtrudeNode : public AbstractPolyNode

int convexity;
double fn, fs, fa;
double origin_x, origin_y, scale;
double origin_x, origin_y, scale, angle;
Filename filename;
std::string layername;
};
40 changes: 40 additions & 0 deletions testdata/scad/3D/features/rotate_extrude-angle.scad
@@ -0,0 +1,40 @@
$fa=15;
$fs=4;

module face(x) {
translate([x,0]) difference() {
square(10,center=true);
square(5,center=true);
}
}

module face2() {
translate([5,0]) square(5);
}

// test negative partial angles and geometries on -X side
rotate_extrude(angle=45) face(10);
rotate_extrude(angle=45) face(-10);
rotate_extrude(angle=-45) face(21);
rotate_extrude(angle=-45) face(-21);

// test small angles, angle < $fa, render a single segment
rotate([0,0,90]) {
rotate_extrude(angle=5) face(10);
rotate_extrude(angle=5) face(-10);
rotate_extrude(angle=-5) face(21);
rotate_extrude(angle=-5) face(-21);
}

// show nothing
rotate_extrude(angle=0) face(5); // 0 angle

// various angles treated as full circle
translate([-40,40]) rotate_extrude() face2(); // unspecified
translate([0,40]) rotate_extrude(angle=0/0) face2(); // NaN
translate([40,40]) rotate_extrude(angle=1/0) face2(); // Infinity
translate([-40,0]) rotate_extrude(angle=-1/0) face2(); // -Infinity
translate([40,0]) rotate_extrude(angle=360) face2(); // 360
translate([-40,-40]) rotate_extrude(angle=-360) face2(); // -360
translate([0,-40]) rotate_extrude(angle=1000) face2(); // > 360
translate([40,-40]) rotate_extrude(angle=-1000) face2(); // < -360
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/regression/cgalpngtest/rotate_extrude-expected.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 13 additions & 13 deletions tests/regression/dumptest-examples/example007-expected.csg
Expand Up @@ -3,47 +3,47 @@ group() {
multmatrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, -10], [0, 0, 0, 1]]) {
group() {
difference() {
rotate_extrude(convexity = 3, $fn = 0, $fa = 12, $fs = 2) {
import(file = "example007.dxf", layer = "dorn", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2);
rotate_extrude(angle = 360, convexity = 3, $fn = 0, $fa = 12, $fs = 2) {
import(file = "example007.dxf", layer = "dorn", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2, timestamp = 1447128393);
}
group() {
multmatrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) {
group() {
intersection() {
multmatrix([[1, 0, 0, 0], [0, 0, -1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]) {
multmatrix([[1, 0, 0, 0], [0, 6.12323e-17, -1, 0], [0, 1, 6.12323e-17, 0], [0, 0, 0, 1]]) {
multmatrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, -50], [0, 0, 0, 1]]) {
linear_extrude(height = 100, center = false, convexity = 1, scale = [1, 1], $fn = 0, $fa = 12, $fs = 2) {
import(file = "example007.dxf", layer = "cutout1", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2);
import(file = "example007.dxf", layer = "cutout1", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2, timestamp = 1447128393);
}
}
}
multmatrix([[0, -1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) {
multmatrix([[1, 0, 0, 0], [0, 0, -1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]) {
multmatrix([[6.12323e-17, -1, 0, 0], [1, 6.12323e-17, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) {
multmatrix([[1, 0, 0, 0], [0, 6.12323e-17, -1, 0], [0, 1, 6.12323e-17, 0], [0, 0, 0, 1]]) {
multmatrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, -50], [0, 0, 0, 1]]) {
linear_extrude(height = 100, center = false, convexity = 2, scale = [1, 1], $fn = 0, $fa = 12, $fs = 2) {
import(file = "example007.dxf", layer = "cutout2", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2);
import(file = "example007.dxf", layer = "cutout2", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2, timestamp = 1447128393);
}
}
}
}
}
}
}
multmatrix([[0, -1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) {
multmatrix([[6.12323e-17, -1, 0, 0], [1, 6.12323e-17, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) {
group() {
intersection() {
multmatrix([[1, 0, 0, 0], [0, 0, -1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]) {
multmatrix([[1, 0, 0, 0], [0, 6.12323e-17, -1, 0], [0, 1, 6.12323e-17, 0], [0, 0, 0, 1]]) {
multmatrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, -50], [0, 0, 0, 1]]) {
linear_extrude(height = 100, center = false, convexity = 1, scale = [1, 1], $fn = 0, $fa = 12, $fs = 2) {
import(file = "example007.dxf", layer = "cutout1", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2);
import(file = "example007.dxf", layer = "cutout1", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2, timestamp = 1447128393);
}
}
}
multmatrix([[0, -1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) {
multmatrix([[1, 0, 0, 0], [0, 0, -1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]) {
multmatrix([[6.12323e-17, -1, 0, 0], [1, 6.12323e-17, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) {
multmatrix([[1, 0, 0, 0], [0, 6.12323e-17, -1, 0], [0, 1, 6.12323e-17, 0], [0, 0, 0, 1]]) {
multmatrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, -50], [0, 0, 0, 1]]) {
linear_extrude(height = 100, center = false, convexity = 2, scale = [1, 1], $fn = 0, $fa = 12, $fs = 2) {
import(file = "example007.dxf", layer = "cutout2", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2);
import(file = "example007.dxf", layer = "cutout2", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2, timestamp = 1447128393);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/regression/dumptest-examples/example009-expected.csg
@@ -1,26 +1,26 @@
group() {
group();
% linear_extrude(height = 22, center = true, convexity = 10, scale = [1, 1], $fn = 0, $fa = 12, $fs = 2) {
import(file = "example009.dxf", layer = "body", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2);
import(file = "example009.dxf", layer = "body", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2, timestamp = 1447128393);
}
% group() {
multmatrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 12], [0, 0, 0, 1]]) {
linear_extrude(height = 2, center = true, convexity = 10, scale = [1, 1], $fn = 0, $fa = 12, $fs = 2) {
import(file = "example009.dxf", layer = "plate", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2);
import(file = "example009.dxf", layer = "plate", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2, timestamp = 1447128393);
}
}
multmatrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, -12], [0, 0, 0, 1]]) {
linear_extrude(height = 2, center = true, convexity = 10, scale = [1, 1], $fn = 0, $fa = 12, $fs = 2) {
import(file = "example009.dxf", layer = "plate", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2);
import(file = "example009.dxf", layer = "plate", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2, timestamp = 1447128393);
}
}
}
intersection() {
linear_extrude(height = 20, center = true, convexity = 10, twist = -57.5288, slices = 4, scale = [1, 1], $fn = 0, $fa = 12, $fs = 2) {
import(file = "example009.dxf", layer = "fan_top", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2);
import(file = "example009.dxf", layer = "fan_top", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2, timestamp = 1447128393);
}
rotate_extrude(convexity = 10, $fn = 0, $fa = 12, $fs = 2) {
import(file = "example009.dxf", layer = "fan_side", origin = [0, -40], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2);
rotate_extrude(angle = 360, convexity = 10, $fn = 0, $fa = 12, $fs = 2) {
import(file = "example009.dxf", layer = "fan_side", origin = [0, -40], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2, timestamp = 1447128393);
}
}
}

0 comments on commit ea50b90

Please sign in to comment.