Some thoughts on deriving world coordinates for objects

18 messages
Open this post in threaded view
|

Some thoughts on deriving world coordinates for objects

 [ Note:  In March 2019, I said this wasn't possible.  I was wrong. ] Definition:  "world coordinates" are the resulting "universe" coordinate system of the whole model, the coordinate system that the displayed axes are relative to.  They are distinct from the local coordinate system inside a transformed object, and from the device coordinates used to represent the model on a display device. People frequently ask about various problems that involve things like measuring distances between points on a model. There are two tough problems in these questions: 1)  OpenSCAD modules are black holes; information goes in but no information comes out.  You cannot extract any information about the contents of the module.  But it can echo, and maybe that's helpful.  Anyhow, I'm not thinking about this part of the problem. 2)  Modules are mostly opaque to data coming *in*, too.  The object doesn't know what its ancestors will do to it.  A cube() thinks that it has a corner at [0,0,0], but usually it will be translated and rotated so that it doesn't, and it doesn't have any way to either reverse that transformation or evaluate it to determine its world coordinates. It's this latter that I've been musing about. There's no intrinsic reason why an object can't know its own transformation.  After all, it's called through a tree of transformations.  Its parents could tell it the transformation. Consider these redefinitions of some standard primitives: ```// Initially, the local coordinates are the same as world coordinates. \$mm = [ [1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1] ]; // Given a point in local coordinates, return its location in world coordinates. function whereis(p3) = let(r4 = \$mm * [p3.x, p3.y, p3.z, 1]) [ r4.x, r4.y, r4.z]; // Translate, but tracking the transformation. module translate(delta) { m = [ [1,0,0,delta.x], [0,1,0,delta.y], [0,0,1,delta.z], [0,0,0,1] ]; \$mm = \$mm * m; multmatrix(m) children(); } // Rotate, tracking transformations. module rotate(a) { a = is_num(a) ? [ 0,0,a ] : a; echo(a); rx = [ [1, 0, 0, 0], [0, cos(a.x), -sin(a.x), 0], [0, sin(a.x), cos(a.x), 0], [0, 0, 0, 1] ]; ry = [ [ cos(a.y), 0, sin(a.y), 0], [ 0, 1, 0, 0], [ -sin(a.y), 0, cos(a.y), 0], [ 0, 0, 0, 1] ]; rz = [ [ cos(a.z), -sin(a.z), 0, 0], [ sin(a.z), cos(a.z), 0, 0], [ 0, 0, 1, 0], [ 0, 0, 0, 1] ]; m = rx*ry*rz; \$mm = \$mm * m; multmatrix(m) children(); } // Scale, tracking transformations module scale(s) { s = is_num(s) ? [ s,s,s ] : s; m = [ [ s.x, 0, 0, 0 ], [ 0, s.y, 0, 0 ], [ 0, 0, s.z, 0 ], [ 0, 0, 0, 1 ] ]; \$mm = \$mm * m; multmatrix(m) children(); } scale(2) rotate(45) translate([0,5,0]) translate([10,0,0]) { cube(); echo(whereis([0,0,0])); } ``` These "transformation-tracking" transformation modules give their children a transformation matrix \$mm that is the transformation from local coordinates to world coordinates.  This allows the child to, for any point in its local coordinate system, determine (and presumably emit via echo()) the corresponding world coordinates for that point. For extra fun, let's give translate() another feature: ```\$rm = \$mm; // Translate, but tracking the transformation and reverse transformation. module translate(delta) { m = [ [1,0,0,delta.x], [0,1,0,delta.y], [0,0,1,delta.z], [0,0,0,1] ]; \$mm = \$mm * m; rm = [ [1,0,0,-delta.x], [0,1,0,-delta.y], [0,0,1,-delta.z], [0,0,0,1] ]; \$rm = \$rm * rm; multmatrix(m) children(); } ``` and add a new module: ```module origin() { multmatrix(\$rm) children(); } ``` Now if we do this: ```translate([0,5,0]) translate([10,0,0]) { cube(); origin() cube(); } ``` We get a cube that's out at [10,5,0] and another cube that's at the origin, and the cube at the origin stays there no matter what we do to the enclosing transformations. (Equivalent extensions to rotate() and scale() left as an exercise for the reader.) Net, you would be able to determine world coordinates, and you'd be able to position things in world coordinates even from inside a transformed coordinate system. This is similar to schemes that others have devised where you maintain a transformation matrix "by hand", and then you apply it to your points and objects.  This scheme does it within the normal structure of OpenSCAD transformations. Note that while I've done this "in user mode", it could be implemented inside OpenSCAD's built-in transformations.  (OCD:  Compatibly, give or take the namespace intrusions.) Problem:  scale with any axis having a scale of zero can't be reversed. Problem:  reversing multmatrix itself is a matrix inversion, isn't trivial.  (But it doesn't seem awful either.  Might be best to have origin() invert \$mm, rather than having multmatrix invert its argument.) Combined with the \$vxx variables, I think you could do things like have label floating in a constant place on the screen, with an arrow pointing at a particular point on the model. You could *not* directly measure the distance between two points on the model, because of the "module is a black hole" effect.  (Special case:  you can measure relative to an object that is the current object's ancestor, because black holes only keep information from leaving.)  Similarly, you could not position an object relative to an object that is not its ancestor. Does anybody else think that there might be interesting possibilities here? _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

Re: Some thoughts on deriving world coordinates for objects

 On 10/22/2020 7:48 PM, Jordan Brown wrote: [ Note:  In March 2019, I said this wasn't possible.  I was wrong. ] For anybody who is curious about the mistake that I made back then... I had assumed that a module yielded an object based on its parameters, period.  That object could then be transformed, *after* it was created.  Because it could be transformed after it was created, it couldn't possibly know the relationship between its local coordinate system and the world coordinate system, because at the time it was created that relationship wasn't known. There are perfectly reasonable graphics systems that use this model. But that's not how OpenSCAD objects are created.  They are always created in the context of their ancestral tree of transformations.  That tree exists *before* the object is constructed.  In addition, that tree can pass information down into the object in \$ variables. As a result, it *is* possible for an OpenSCAD object to know how its local coordinate system relates to the world coordinate system. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

Re: Some thoughts on deriving world coordinates for objects

 In reply to this post by JordanBrown It sounds similar to the Relativity library:On Thu, Oct 22, 2020, at 10:48 PM, Jordan Brown wrote:[ Note:  In March 2019, I said this wasn't possible.  I was wrong. ]Definition:  "world coordinates" are the resulting "universe" coordinate system of the whole model, the coordinate system that the displayed axes are relative to.  They are distinct from the local coordinate system inside a transformed object, and from the device coordinates used to represent the model on a display device. People frequently ask about various problems that involve things like measuring distances between points on a model. There are two tough problems in these questions: 1)  OpenSCAD modules are black holes; information goes in but no information comes out.  You cannot extract any information about the contents of the module.  But it can echo, and maybe that's helpful.  Anyhow, I'm not thinking about this part of the problem. 2)  Modules are mostly opaque to data coming *in*, too.  The object doesn't know what its ancestors will do to it.  A cube() thinks that it has a corner at [0,0,0], but usually it will be translated and rotated so that it doesn't, and it doesn't have any way to either reverse that transformation or evaluate it to determine its world coordinates. It's this latter that I've been musing about. There's no intrinsic reason why an object can't know its own transformation.  After all, it's called through a tree of transformations.  Its parents could tell it the transformation. Consider these redefinitions of some standard primitives: ```// Initially, the local coordinates are the same as world coordinates. \$mm = [ [1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1] ]; // Given a point in local coordinates, return its location in world coordinates. function whereis(p3) = let(r4 = \$mm * [p3.x, p3.y, p3.z, 1]) [ r4.x, r4.y, r4.z]; // Translate, but tracking the transformation. module translate(delta) { m = [ [1,0,0,delta.x], [0,1,0,delta.y], [0,0,1,delta.z], [0,0,0,1] ]; \$mm = \$mm * m; multmatrix(m) children(); } // Rotate, tracking transformations. module rotate(a) { a = is_num(a) ? [ 0,0,a ] : a; echo(a); rx = [ [1, 0, 0, 0], [0, cos(a.x), -sin(a.x), 0], [0, sin(a.x), cos(a.x), 0], [0, 0, 0, 1] ]; ry = [ [ cos(a.y), 0, sin(a.y), 0], [ 0, 1, 0, 0], [ -sin(a.y), 0, cos(a.y), 0], [ 0, 0, 0, 1] ]; rz = [ [ cos(a.z), -sin(a.z), 0, 0], [ sin(a.z), cos(a.z), 0, 0], [ 0, 0, 1, 0], [ 0, 0, 0, 1] ]; m = rx*ry*rz; \$mm = \$mm * m; multmatrix(m) children(); } // Scale, tracking transformations module scale(s) { s = is_num(s) ? [ s,s,s ] : s; m = [ [ s.x, 0, 0, 0 ], [ 0, s.y, 0, 0 ], [ 0, 0, s.z, 0 ], [ 0, 0, 0, 1 ] ]; \$mm = \$mm * m; multmatrix(m) children(); } scale(2) rotate(45) translate([0,5,0]) translate([10,0,0]) { cube(); echo(whereis([0,0,0])); } ```These "transformation-tracking" transformation modules give their children a transformation matrix \$mm that is the transformation from local coordinates to world coordinates.  This allows the child to, for any point in its local coordinate system, determine (and presumably emit via echo()) the corresponding world coordinates for that point.For extra fun, let's give translate() another feature:```\$rm = \$mm; // Translate, but tracking the transformation and reverse transformation. module translate(delta) { m = [ [1,0,0,delta.x], [0,1,0,delta.y], [0,0,1,delta.z], [0,0,0,1] ]; \$mm = \$mm * m; rm = [ [1,0,0,-delta.x], [0,1,0,-delta.y], [0,0,1,-delta.z], [0,0,0,1] ]; \$rm = \$rm * rm; multmatrix(m) children(); } ```and add a new module:```module origin() { multmatrix(\$rm) children(); } ```Now if we do this:```translate([0,5,0]) translate([10,0,0]) { cube(); origin() cube(); } ```We get a cube that's out at [10,5,0] and another cube that's at the origin, and the cube at the origin stays there no matter what we do to the enclosing transformations.(Equivalent extensions to rotate() and scale() left as an exercise for the reader.)Net, you would be able to determine world coordinates, and you'd be able to position things in world coordinates even from inside a transformed coordinate system.This is similar to schemes that others have devised where you maintain a transformation matrix "by hand", and then you apply it to your points and objects.  This scheme does it within the normal structure of OpenSCAD transformations.Note that while I've done this "in user mode", it could be implemented inside OpenSCAD's built-in transformations.  (OCD:  Compatibly, give or take the namespace intrusions.)Problem:  scale with any axis having a scale of zero can't be reversed.Problem:  reversing multmatrix itself is a matrix inversion, isn't trivial.  (But it doesn't seem awful either.  Might be best to have origin() invert \$mm, rather than having multmatrix invert its argument.)Combined with the \$vxx variables, I think you could do things like have label floating in a constant place on the screen, with an arrow pointing at a particular point on the model.You could *not* directly measure the distance between two points on the model, because of the "module is a black hole" effect.  (Special case:  you can measure relative to an object that is the current object's ancestor, because black holes only keep information from leaving.)  Similarly, you could not position an object relative to an object that is not its ancestor. Does anybody else think that there might be interesting possibilities here? _______________________________________________OpenSCAD mailing list_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

Re: Some thoughts on deriving world coordinates for objects

 On 10/22/2020 8:27 PM, Doug Moen wrote: It sounds similar to the Relativity library: Not really.  Relativity (from a brief look) is about constructing objects connected to one another. This concept is about managing the relationship between local coordinates and world coordinates. The two are orthogonal to one another. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

Re: Some thoughts on deriving world coordinates for objects

Open this post in threaded view
|

Re: Some thoughts on deriving world coordinates for objects

Open this post in threaded view
|

Re: Some thoughts on deriving world coordinates for objects

 In reply to this post by JordanBrown On 2020-10-23 04:48, Jordan Brown wrote: > There's no intrinsic reason why an object can't know its own > transformation.  After all, it's called through a tree of > transformations.  Its parents could tell it the transformation. If you want to be a purist you might say that an object and its transformation(s) are separate, the transformation defines the context and an object can exist in several contexts at the same time. I come from the tradition of Finite Element Analysis and super-element techniques where this is important. A super-element can thus not know its transformation, because it exists in multiple contexts on multiple levels, and the same super-element might be "re-tracked" in several contexts. However, in Constructive Solid Geometry (CSG) this way of looking at things is not so central, and unlike for Finite Element super-elements a CSG object is typically copied with each transformation, so each copy can thus know its transformation. This is exactly what happens in AngelCAD. Also, since 3d objects are named variables there, they can be used to return some information. Any 2d or 3d object inherits from "shape" ( https://arnholm.github.io/angelcad-docs/docs/classshape.html ) and a shape can return its transformation in the form of a tmatrix object which is essentially just a 4x4 homogenous transformation matrix like in your code examples > Problem:  reversing multmatrix itself is a matrix inversion, isn't > trivial.  (But it doesn't seem awful either.  Might be best to have > origin() invert \$mm, rather than having multmatrix invert its > argument.) General NxN matrix inversion isn't trivial, but the special case of 4x4 inversion is pretty much trivial and can be coded symbolically. Carsten Arnholm _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

Re: Some thoughts on deriving world coordinates for objects

Open this post in threaded view
|

Re: Some thoughts on deriving world coordinates for objects

 In reply to this post by cacb On 10/23/2020 1:43 AM, [hidden email] wrote: > On 2020-10-23 04:48, Jordan Brown wrote: >> There's no intrinsic reason why an object can't know its own >> transformation.  After all, it's called through a tree of >> transformations.  Its parents could tell it the transformation. > If you want to be a purist you might say that an object and its > transformation(s) are separate, the transformation defines the context > and an object can exist in several contexts at the same time. See my separate message on the mistake that I made in March 2019.  That was how I was thinking, that an object was a black box to which a transformation was applied. But that's not what OpenSCAD does.  I wouldn't say that it "copies" the object, but rather that each invocation creates a new instance of the object, in the context of its transformation. Neither scheme is wrong... they're just different. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

Re: Some thoughts on deriving world coordinates for objects

 In reply to this post by RobWLakes On 10/23/2020 1:00 AM, Rob Ward wrote: This is assuming there is some reverse logic provided at the time the forward logic of an openscad design is conceived. But, for almost all transformations, there is. When you say ```translate([10,0,0]) ... ``` the inverse is ```translate([-10,0,0]) ... ``` and similarly for any other transformation, except scale(0) variants. The trick is in keeping track of those transformations and their inverses as you work your way through the stack of transformations. Note that I'm not attempting to "undo" Boolean operations... just transformations. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

Re: Some thoughts on deriving world coordinates for objects

 In reply to this post by RevarBat On 10/23/2020 1:26 AM, Revar Desmera wrote: > Actually if you want to go to the effort of overriding all the > transformation built-ins (translate, rotate, scale) with user modules > calling multmatrix(), and provide an alternate call for multmatrix() > itself, it should become fairly trivial to keep track of the world > coordinates compared to local. Yes.  I did that, mostly, but you did even more of it, and you supplied the matrix invert that I hadn't done yet.  (Mostly because I haven't done a matrix invert in >40 years...) Cool!  Thanks! I haven't tried it, but I suspect that you have a bug in reference_frame().  When you supply a "mat" argument to it, I think you need to first multiply by \$local_matrix to get back to world coordinates, before multiplying by the specified matrix to get to the desired reference frame. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

Re: Some thoughts on deriving world coordinates for objects

 In reply to this post by JordanBrown On 10/22/2020 7:48 PM, Jordan Brown wrote: > Combined with the \$vxx variables, I think you could do things like > have label floating in a constant place on the screen, with an arrow > pointing at a particular point on the model. And indeed, I've got a test program that does exactly that. Can anybody point me at how to take an OpenSCAD animation sequence and turn it into an animated GIF? I *did* run into an ... interesting ... variable scoping issue. I'll send a separate message on that. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

Re: Some thoughts on deriving world coordinates for objects

 In reply to this post by JordanBrown I’m afraid I didn’t see your code until after I’d posted, but yeah, that looks like the same idea. You’re right about the bug in reference_frame().  I wrote this code off the top of my head, and in parts cannibalized from my BOSL2 library at 3am.  Only minimal testing was actually done.  Proof of concept or some such.  The matrix inverse I implemented based on https://www.mathsisfun.com/algebra/matrix-inverse-minors-cofactors-adjugate.htmlI suspect I can re-implement this code to not even need the matrix inverse code, if we construct the inverse matrix as we go.  Either by keeping a stack of the individual transforms, or by reversing the matrix multiplications in another \$var.  Not sure if that works, but I think it will. I had also written code to get the local scaling and rotation as well as the local translation, but I’m pretty sure those will get screwed up by any skew transforms. - Revar > On Oct 23, 2020, at 9:15 AM, Jordan Brown <[hidden email]> wrote: > > On 10/23/2020 1:26 AM, Revar Desmera wrote: >> Actually if you want to go to the effort of overriding all the transformation built-ins (translate, rotate, scale) with user modules calling multmatrix(), and provide an alternate call for multmatrix() itself, it should become fairly trivial to keep track of the world coordinates compared to local. > > Yes.  I did that, mostly, but you did even more of it, and you supplied the matrix invert that I hadn't done yet.  (Mostly because I haven't done a matrix invert in >40 years...) > > Cool!  Thanks! > > I haven't tried it, but I suspect that you have a bug in reference_frame().  When you supply a "mat" argument to it, I think you need to first multiply by \$local_matrix to get back to world coordinates, before multiplying by the specified matrix to get to the desired reference frame. > _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

Re: Some thoughts on deriving world coordinates for objects

 On 10/23/2020 1:15 PM, Revar Desmera wrote: `I suspect I can re-implement this code to not even need the matrix inverse code, if we construct the inverse matrix as we go.` That's what I was doing, but when you get to a multmatrix() you need to invert it.  Since my guess is that multmatrix is more common than resetting to the world coordinate system, it seems like it'd be better to do the invert at the end. `I had also written code to get the local scaling and rotation as well as the local translation, but I’m pretty sure those will get screwed up by any skew transforms.` Yes. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

Re: Some thoughts on deriving world coordinates for objects

 In reply to this post by JordanBrown On 10/23/2020 9:16 AM, Jordan Brown wrote: On 10/22/2020 7:48 PM, Jordan Brown wrote: Combined with the \$vxx variables, I think you could do things like have label floating in a constant place on the screen, with an arrow pointing at a particular point on the model. And indeed, I've got a test program that does exactly that. So there's several things going on in that animation (assuming that it works - it's my first time)... There's a cube, translated away from the origin, orbiting around the Z axis. There's a cube created *inside* that translation and rotation, that's locked back to the origin. The camera is orbiting around the scene. There's a label stuck at a fixed location on the display. There's an arrow from the label to the orbiting cube. Here's the source.  I don't claim that this is the cleanest or best way to do this... it's just a proof of concept. --- ```// Initially, the local coordinates are the same as world coordinates. \$mm = [ [1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1] ]; \$rm = \$mm; // Given a point in local coordinates, return its location in world coordinates. function whereis(p3) = let(r4 = \$mm * [p3.x, p3.y, p3.z, 1]) [ r4.x, r4.y, r4.z]; // Translate, but tracking the transformation and reverse transformation. module translate(delta) { m = [ [1,0,0,delta.x], [0,1,0,delta.y], [0,0,1,delta.z], [0,0,0,1] ]; \$mm = \$mm * m; rm = [ [1,0,0,-delta.x], [0,1,0,-delta.y], [0,0,1,-delta.z], [0,0,0,1] ]; \$rm = rm * \$rm; multmatrix(m) children(); } // Rotate, tracking transformations. module rotate(a) { a = is_num(a) ? [ 0,0,a ] : a; rx = [ [1, 0, 0, 0], [0, cos(a.x), -sin(a.x), 0], [0, sin(a.x), cos(a.x), 0], [0, 0, 0, 1] ]; ry = [ [ cos(a.y), 0, sin(a.y), 0], [ 0, 1, 0, 0], [ -sin(a.y), 0, cos(a.y), 0], [ 0, 0, 0, 1] ]; rz = [ [ cos(a.z), -sin(a.z), 0, 0], [ sin(a.z), cos(a.z), 0, 0], [ 0, 0, 1, 0], [ 0, 0, 0, 1] ]; m = rx*ry*rz; \$mm = \$mm * m; b = -a; rbx = [ [1, 0, 0, 0], [0, cos(b.x), -sin(b.x), 0], [0, sin(b.x), cos(b.x), 0], [0, 0, 0, 1] ]; rby = [ [ cos(b.y), 0, sin(b.y), 0], [ 0, 1, 0, 0], [ -sin(b.y), 0, cos(b.y), 0], [ 0, 0, 0, 1] ]; rbz = [ [ cos(b.z), -sin(b.z), 0, 0], [ sin(b.z), cos(b.z), 0, 0], [ 0, 0, 1, 0], [ 0, 0, 0, 1] ]; rm = rbz*rby*rbx; \$rm = rm * \$rm; multmatrix(m) children(); } // Scale, tracking transformations module scale(s) { s = is_num(s) ? [ s,s,s ] : s; m = [ [ s.x, 0, 0, 0 ], [ 0, s.y, 0, 0 ], [ 0, 0, s.z, 0 ], [ 0, 0, 0, 1 ] ]; \$mm = \$mm * m; rm = [ [ 1/s.x, 0, 0, 0 ], [ 0, 1/s.y, 0, 0 ], [ 0, 0, 1/s.z, 0 ], [ 0, 0, 0, 1 ] ]; \$rm = rm * \$rm; multmatrix(m) children(); } // Lock the children to the viewport. // Undoes any model transforms and viewport transforms. // I don't know if I really have the \$vpd interaction correct. module vp() { r = \$vpr; origin() translate(\$vpt) rotate([0,0,r.z]) rotate([0,r.y,0]) rotate([r.x,0,0]) scale(\$vpd/250) children(); } // Reset the transform for children to the world coordinate // system, undoing any model transforms. module origin() { multmatrix(\$rm) { // And, since we're back at the basic transformation, // reset our record of the transforms. \$mm = [ [1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1] ]; \$rm = \$mm; children(); } } // Given an [x,y] or [x,y,z], transform to a // [rho, theta] or [rho, theta, phi] function topolar(p) = len(p) == 3 ? topolar3(p) : topolar2(p); function topolar2(p) = [ norm(p), atan2(p.y, p.x) ]; function topolar3(p) = [ norm(p), atan2(p.y, p.x), atan2(norm([p.x,p.y]), p.z) ]; // Draw a line (a cylinder) from p1 to p2, with a diameter of d. // Shorten the p1 end by off0 and the p2 end by off1. // Add arrowheads of length h0 at the p1 and and h1 at the p2 end. module line(p1, p2, d, off0, off1, h0, h1) { off0 = off0 ? off0 : 0; off1 = off1 ? off1 : 0; translate(p1) { polar = topolar(p2-p1); rho = polar[0]; theta = polar[1]; phi = polar[2]; rotate([0,0,theta]) rotate([0,phi,0]) translate([0,0,off0]) { if (h0) { cylinder(d1=d,d2=3*d, h=h0); } if (h1) { translate([0,0,rho-off1-off0-h1]) cylinder(d1=3*d, d2=d, h=h1); } cylinder(d=d, h=rho-off1-off0); } } } // OK, let's set up a demo. // Take a cube. // Scale it up. Translate and rotate it so that it orbits the origin. // *Inside* that transform, reset back to the original world // coordinates and place another cube at the origin. // Have the camera orbit the origin. // Add a label at a fixed place on the viewport. // Add an arrow from the label to a point on the orbiting cube. \$vpr = [\$vpr[0],\$vpr[1],360*\$t]; \$vpt = [0,0,0]; scale(2) rotate(2*360*\$t) translate([0,5,0]) translate([10,0,0]) group() { cube(3); p1 = whereis([0,0,3]); origin() cube(3); vp() translate([30,20,0]) group() { p2 = whereis([0,0,0]); text("cube", valign="center"); origin() line(p1,p2,d=0.5,off0=2, off1=2, h0=2); } } ``` _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

Re: Some thoughts on deriving world coordinates for objects

 I think this is way cool! I'm unable to find group() in https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Languagebut I suppose it does nothing more than ensure its parent sees a single child? -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
 On 10/27/2020 5:46 AM, rickan wrote: ```I think this is way cool! I'm unable to find group() in https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language but I suppose it does nothing more than ensure its parent sees a single child? ``` Yes, though the precise need is to defer execution of the assignments in the block until the parent runs.  (Ref my message " 'interesting' scoping behavior for \$ variables" of 10/23/2020 at 9:48am Pacific.) I should have swapped it out for something documented like union(). _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org