Discussion:
[OpenSCAD] understanding the
Mark Harrison
2018-08-04 06:39:49 UTC
Permalink
Here's something I'm having trouble understanding, if someone can help
me understand I'd be very appreciative.

Here's a nifty way of drawing a cube with cut off corners.
But, it fails on the mirror of the corner at [1,1,1].

module corner(wid) {
corner_points = [[0,0,0],[1,0,0],[0,1,0],[0,0,1]];
corner_faces = [[0,3,1],[0,2,3],[0,1,2],[1,3,2]];
polyhedron(wid*corner_points, corner_faces);
}

module cornercube(wid, cornerwid) {
cube_vertices = [[0,0,0],[0,0,1],[0,1,0],[0,1,1],
[1,0,0],[1,0,1],[1,1,0],[1,1,1]];
difference() {
cube(wid);
for (v = cube_vertices) {
#translate(wid*v) mirror(v) corner(cornerwid);
}
}
}
cornercube(20,5);

This code fixes things by handling that corner as a special case.
Can someone explain to me what's different about the one corner?
And is there some transformation that will work on all eight corners?

module cornercube2(wid,cornerwid) {
cube_vertices7 = [[0,0,0],[0,0,1],[0,1,0],[0,1,1],
[1,0,0],[1,0,1],[1,1,0]];
cube_vertices1 = [[1,1,1]];
difference() {
cube(wid);
for (v = cube_vertices7) {
translate(wid*v) mirror(v) corner(cornerwid);
}
for (v = cube_vertices1) {
#translate(wid*v) rotate([180,90,0]) corner(cornerwid);
}
}
}
cornercube2(20,5);
Ronaldo Persiano
2018-08-04 14:01:24 UTC
Permalink
I don't have how to test this now. Have you tried to visualize just the
mirrored awkward corner? Something like:

translate(10*[1,1,1]) mirror ( [1,1,1]) corner(3);
% cube(10);
doug moen
2018-08-04 14:36:18 UTC
Permalink
Something is wrong with your logic.

Intuitively, I would expect that a rotation and translation is needed to
position each corner.
You are using a mirror and a translation.

It's been a while since I studied matrix transformations, but it seems to
me that a rotation can *sometimes* be implemented using a mirror, but not
in the general case. So maybe you are getting lucky, and a mirror just
happens to be sufficient to perform the rotation in 7 of the 8 cases?
Here's something I'm having trouble understanding, if someone can help me
understand I'd be very appreciative.
Here's a nifty way of drawing a cube with cut off corners.
But, it fails on the mirror of the corner at [1,1,1].
module corner(wid) {
corner_points = [[0,0,0],[1,0,0],[0,1,0],[0,0,1]];
corner_faces = [[0,3,1],[0,2,3],[0,1,2],[1,3,2]];
polyhedron(wid*corner_points, corner_faces);
}
module cornercube(wid, cornerwid) {
cube_vertices = [[0,0,0],[0,0,1],[0,1,0],[0,1,1],
[1,0,0],[1,0,1],[1,1,0],[1,1,1]];
difference() {
cube(wid);
for (v = cube_vertices) {
#translate(wid*v) mirror(v) corner(cornerwid);
}
}
}
cornercube(20,5);
This code fixes things by handling that corner as a special case.
Can someone explain to me what's different about the one corner?
And is there some transformation that will work on all eight corners?
module cornercube2(wid,cornerwid) {
cube_vertices7 = [[0,0,0],[0,0,1],[0,1,0],[0,1,1],
[1,0,0],[1,0,1],[1,1,0]];
cube_vertices1 = [[1,1,1]];
difference() {
cube(wid);
for (v = cube_vertices7) {
translate(wid*v) mirror(v) corner(cornerwid);
}
for (v = cube_vertices1) {
#translate(wid*v) rotate([180,90,0]) corner(cornerwid);
}
}
}
cornercube2(20,5);
_______________________________________________
OpenSCAD mailing list
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
doug moen
2018-08-04 14:38:41 UTC
Permalink
Another way to cut corners off a cube is to intersect it with an
octahedron. I think the logic for this would be easier.
Post by doug moen
Something is wrong with your logic.
Intuitively, I would expect that a rotation and translation is needed to
position each corner.
You are using a mirror and a translation.
It's been a while since I studied matrix transformations, but it seems to
me that a rotation can *sometimes* be implemented using a mirror, but not
in the general case. So maybe you are getting lucky, and a mirror just
happens to be sufficient to perform the rotation in 7 of the 8 cases?
Here's something I'm having trouble understanding, if someone can help me
understand I'd be very appreciative.
Here's a nifty way of drawing a cube with cut off corners.
But, it fails on the mirror of the corner at [1,1,1].
module corner(wid) {
corner_points = [[0,0,0],[1,0,0],[0,1,0],[0,0,1]];
corner_faces = [[0,3,1],[0,2,3],[0,1,2],[1,3,2]];
polyhedron(wid*corner_points, corner_faces);
}
module cornercube(wid, cornerwid) {
cube_vertices = [[0,0,0],[0,0,1],[0,1,0],[0,1,1],
[1,0,0],[1,0,1],[1,1,0],[1,1,1]];
difference() {
cube(wid);
for (v = cube_vertices) {
#translate(wid*v) mirror(v) corner(cornerwid);
}
}
}
cornercube(20,5);
This code fixes things by handling that corner as a special case.
Can someone explain to me what's different about the one corner?
And is there some transformation that will work on all eight corners?
module cornercube2(wid,cornerwid) {
cube_vertices7 = [[0,0,0],[0,0,1],[0,1,0],[0,1,1],
[1,0,0],[1,0,1],[1,1,0]];
cube_vertices1 = [[1,1,1]];
difference() {
cube(wid);
for (v = cube_vertices7) {
translate(wid*v) mirror(v) corner(cornerwid);
}
for (v = cube_vertices1) {
#translate(wid*v) rotate([180,90,0]) corner(cornerwid);
}
}
}
cornercube2(20,5);
_______________________________________________
OpenSCAD mailing list
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Carsten Arnholm
2018-08-05 12:59:06 UTC
Permalink
Post by doug moen
Another way to cut corners off a cube is to intersect it with an
octahedron. I think the logic for this would be easier.
Indeed. The solution is trivial if you write a module defining an
octahedron with vertices on the main axes, distanced "size" from origin,
i.e. module octahedron(size);

Then you can do

module cornercube(wid,cornerwid)
{
intersection()
{
octahedron(size=wid*sqrt(2)-cornerwid/2);
cube(size:wid,center=true);
}
}

Carsten Arnholm
Parkinbot
2018-08-07 10:51:20 UTC
Permalink
Yeah Carsten,

a more convenient way to code this would be:

cornercube(20,5);
module octahedron(d = 10)
scale(d*sqrt(3))
{
cylinder(r1=1, r2=0, h=sqrt(3)*.5, $fn=4);
scale([1,1,-1])cylinder(r1=1, r2=0, h=sqrt(3)*.5, $fn=4);
}


module cornercube(d=20, d1=5)
intersection()
{
cube(d, true);
octahedron(d-d1);
}




--
Sent from: http://forum.openscad.org/
Parkinbot
2018-08-07 11:05:07 UTC
Permalink
Sorry, I miscalculated the height of the octahedron. It is as easy as this:

module octahedron(d = 10)
{
scale(d*sqrt(3))
{
cylinder(r1=1, r2=0, h=1, $fn=4);
scale([1,1,-1])cylinder(r1=1, r2=0, h=1, $fn=4);
}
}





--
Sent from: http://forum.openscad.org/
a***@arnholm.org
2018-08-07 11:26:39 UTC
Permalink
Post by Parkinbot
module octahedron(d = 10)
{
scale(d*sqrt(3))
{
cylinder(r1=1, r2=0, h=1, $fn=4);
scale([1,1,-1])cylinder(r1=1, r2=0, h=1, $fn=4);
}
}
Yes, you can do it this way, although it is "cheating" as it relies on
the OpenSCAD discretization of a cylinder instead of defining the
polyhedron explicitly. It is more compact, but less readable.

I used a different approach in my own code (not openSCAD), realising
that any convex polyhedron can be defined from just an array of vertex
coordinates. An octahedron is just one of many in this category
https://gist.github.com/arnholm/c71852301b56b318a83a59c20bb29fe8

Carsten Arnholm
Parkinbot
2018-08-07 11:41:10 UTC
Permalink
I don't think that using language primitives is "cheating", but I understand
what you mean. The main problem with the code given by the threadstarter is
that the cube is not centered. With a centered cube everything is clear and
easy.

Btw, there was another calculation error in my octahedron. I hope this the
final version:

module octahedron(d = 10)
{
x = d * 2 *sqrt(5)/3;
scale(x)
{
cylinder(r1=1, r2=0, h=1, $fn=4);
scale([1,1,-1])cylinder(r1=1, r2=0, h=1, $fn=4);
}
}




--
Sent from: http://forum.openscad.org/
Ronaldo Persiano
2018-08-07 14:40:18 UTC
Permalink
Post by a***@arnholm.org
I used a different approach in my own code (not openSCAD), realising
that any convex polyhedron can be defined from just an array of vertex
coordinates.
A similar approach in OpenSCAD language might be:

module octahedron(r=1) {
verts = [ [r,0,0], [0,r,0], [-r,0,0],
[0, -r,0], [0,0,r], [0,0,-r] ] ;
hull( polyhedron(verts, [0,1,2,3,4,5]) );
}
Troberg
2018-08-09 05:50:40 UTC
Permalink
Post by a***@arnholm.org
Yes, you can do it this way, although it is "cheating" as it relies on
the OpenSCAD discretization of a cylinder instead of defining the
polyhedron explicitly. It is more compact, but less readable.
I agree on the readability, but I wouldn't consider it cheating. It's a well
defined behaviour, it's not just some random effect of how it currently
works.



--
Sent from: http://forum.openscad.org/
a***@arnholm.org
2018-08-09 08:56:59 UTC
Permalink
Post by Troberg
Post by a***@arnholm.org
Yes, you can do it this way, although it is "cheating" as it relies on
the OpenSCAD discretization of a cylinder instead of defining the
polyhedron explicitly. It is more compact, but less readable.
I agree on the readability, but I wouldn't consider it cheating. It's a well
defined behaviour, it's not just some random effect of how it currently
works.
That is why is said "cheating" (with quotes). It relies on
implementation details such as the discretization of a cylinder (or a
cone rather) plus it also relies on implicit union of 2 such objects.
Granted, this is well known current OpenSCAD behavior, but still.

It is also a special case that works only for creating an octahedron. If
you wanted a different kind of convex polyhedron you would be out of
luck with this approach.

But for sure it solved the original problem as presented.

Carsten Arnholm

runsun
2018-08-05 02:51:46 UTC
Permalink
It's indeed a special case. If you transfer the original corner (red color)
DIRECTLY to its position (i.e., w/o mirror()), and compare its relationship
with the one that is mirrored (teal color), you can see from the graph how
it is a special case :
<Loading Image...>

Instead of mirroring 7 times to all corners, mirroring application like this
can be better achieved by mirroring the original one (C0) to [x,0,0] corner
--- make it C1. Then, mirror BOTH C0-C1 to [0,y,0], generating 4 corners,
than again onto [0,0,z] to generate 8 corners:

module corners(wid=20, cornerwid=5)
{
module mirror_dir(d)
{
mps = [[1,0,0],[0,1,0],[0,0,1]];
children(); //<==== original copy
translate( wid*mps[d]) mirror( mps[d] ) children(); //<==== mirrored
copy
}

mirror_dir(2)
mirror_dir(1)
mirror_dir(0)
corner(cornerwid);
}
<Loading Image...>

This would be a very good general mirroring module for future 8-corner
mirroring.



-----

$ Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), editor of choice: CudaText ( OpenSCAD lexer );&nbsp;$ Tips ;&nbsp;$ Snippets

--
Sent from: http://forum.openscad.org/
Loading...