# How to find the current x, y, z location

26 messages
12
Open this post in threaded view
|

## How to find the current x, y, z location

 I have code with recursive functions that use translate() to change the current location. The code works fine, but now I have to add code that needs to know the current x, y, z location that resulted from many nested translate() functions. How can I find the current x, y, z location? Thank you! -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: How to find the current x, y, z location

 jamcultur wrote > I have code with recursive functions that use translate() to change the > current location. The code works fine, but now I have to add code that > needs > to know the current x, y, z location that resulted from many nested > translate() functions. How can I find the current x, y, z location? Your question is too vague.  I think you need to post a code example and explain what you are trying to do.  What is the "current location"?  The (x,y,z) location of what?   And what are you hoping to do with it once you find it?   -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: How to find the current x, y, z location

 To me, the "current location" is location that is the reference point of any object that you draw. For example, if you wrote sphere(d=10);, the center of the sphere would be at the current location. That location could have any x, y, and z coordinates, depending on the previous translates and rotates. I need to know those coordinates in order to determine whether to draw the next object. I should also mention that there are also many rotate() functions in the recursive functions I mentioned previously. -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: How to find the current x, y, z location

Open this post in threaded view
|

## Re: How to find the current x, y, z location

Open this post in threaded view
|

## Re: How to find the current x, y, z location

 In reply to this post by jamcultur If I correctly understand your needs: you could add absolute position (vector) as a parameter of the recursive function, so every time you call again the function, you update the vector you pass accordingly to the applied translation. I used a similar approach to apply color to a 3D fractal, depending on the cumulative displacement from [0,0,0]. jamcultur wrote > I have code with recursive functions that use translate() to change the > current location. The code works fine, but now I have to add code that > needs > to know the current x, y, z location that resulted from many nested > translate() functions. How can I find the current x, y, z location? > Thank you! -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: How to find the current x, y, z location

 You understand correctly, and I was thinking of doing exactly what you suggest. I was hoping that OpenSCAD had an easier way to do it. OpenSCAD obviously knows the current [x, y, z] and it seems like it should be simple for it to make that information available to the program. -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: How to find the current x, y, z location

 The generated geometry depends on the variables. Making the variables dependent on the geometry could create a circular dependency unless it is restricted by scope rules and would require more passes.On Sat, 2 Mar 2019 at 15:54, jamcultur <[hidden email]> wrote:You understand correctly, and I was thinking of doing exactly what you suggest. I was hoping that OpenSCAD had an easier way to do it. OpenSCAD obviously knows the current [x, y, z] and it seems like it should be simple for it to make that information available to the program. -- Sent from: http://forum.openscad.org/ _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: How to find the current x, y, z location

 In reply to this post by jamcultur don't know whether it will help you, but the way nophead described, is not too difficult to go, if you have translate(), rotate(), and scale() as functions. Say your module contains code like this rotate([100,0,100]) translate([0,30,100]) rotate([33,0,100]) translate([0,0,100]) cube(100); if you want to know where cube is mapped, or where any other point q will be mapped, you just use the same transformation sequence in function syntax.   q=[0,0,0]; q1 = rotate([100,0,100], translate([0,30,100], rotate([33,0,100], translate([0,0,100], q)))); echo(q1); Here is my implementation of the three functions. The code looks more difficult than it is, because the functions are part of a richer interface, accept parameter calls without [] and work recursive. They operate over points, lists of points, lists of lists of points and so on. I use them mainly in connection with sweep(), but they will also serve your needs. function translate(x=0, y=0, z=0, v) =   let(v = (len(x)==3)?y:v, x = (len(x)==3)?x:[x, y, z])   len(v[0])?[for (i=v) translate(x,i)]:v+x; function rotate(x=0, y=0, z=0, v) =  // 2D vectors allowed   let(v = (len(x)==3)?y:v, x=(len(x)==3)?x:[x, y, z])   len(v[0])? [for(i=v) rotate(x,i)]:Rz(x[2], Ry(x[1], Rx(x[0], v)));   function Rx(x, A) = len(A[0][0])?[for(i=A) Rx(x, i)]:     A*[[1, 0, 0], [0, cos(x), sin(x)], [0, -sin(x), cos(x)]]; function Ry(y, A) = len(A[0][0])?[for(i=A) Ry(y, i)]:     A*[[cos(y), 0, sin(y)], [0, 1, 0], [-sin(y), 0, cos(y)]]; function Rz(z, A) = len(A[0][0])?     [for(i=A) Rz(z, i)]:     len(A[0])==2?     A*[[cos(z), sin(z)], [-sin(z), cos(z)]]:     A*[[cos(z), sin(z), 0], [-sin(z), cos(z), 0], [0, 0, 1]]; function scale(x=1, y=1, z=1, v) =   let(v = (len(x)==3)?y:v, x = (len(x)==3)?x:[x, y, z])   len(v[0])?[for (i=v) S_(x,i)]:[v[0]*x[0], v[1]*x[1], v[2]*x[2]]; -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: How to find the current x, y, z location

 In reply to this post by nophead On 3/2/2019 12:17 AM, nop head wrote: Basically you always tell OpenSCAD where something is, you can never ask it. (I know that you know this.) And even then, you can only tell it where something is relative to its own transformations... you can't specify an absolute location. That is, you can't say ```sequence( ) of( ) transformations() {     translate_back_to_origin( ) {         object( );     } }``` The only way to do that would be to know the sequence of transformations and unwind them. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: How to find the current x, y, z location

 I still want to know what the original poster was actually trying to do with a specific code example. I think without that it's difficult to really say what the best solution is.   Why, if you have a series of transformations, would you not just put whatever second thing you need inside that same code.   What is the reason for wanting to do something later inside a series of transformations you did before?  That is the obvious question I have.   -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: How to find the current x, y, z location

 In reply to this post by jamcultur On 3/2/2019 7:49 AM, jamcultur wrote: `OpenSCAD obviously knows the current [x, y, z]` [ Speaking from an external observer's point of view, without knowledge of the internals... others, feel free to correct me.] First, note that "current position" isn't the complete picture - there's also rotation, scale, and skew. I don't think it does know the current position.. It doesn't execute a program in a conventional sense, producing geometry as a direct result of the execution. Rather, it executes a program[*] that produces a tree of transformations, operations, and objects, and a subsequent step applies those transformations and operations to those objects to produce the final absolute geometry. Turn on Design/"Display CSG Tree" to see the results of the execution.  Note that the generated geometry still has each of the transformation steps; there are no absolute coordinates. [*] Even that isn't quite right.  Note that there are almost no unbounded loops and there are no conventional variables.  In a lot of ways it's more like a macro processor than a language interpreter.  Everything is done at "compile" time and there is no "run time". Even if it was a conventional language that directly generated the final geometry, I don't know if it always would know the current transformation relative to the absolute coordinate system. The most straightforward way to do it would be to have a transformation function call its children and then apply its transformation to them, returning a transformed object.  In such a design, each function would have no idea what coordinate system *it* was embedded in; you would only know absolute coordinates when you get back to the top of the call stack. It *might* be equivalent to "left associate" the transformations, building up a "current transformation", applying that to the primitive objects, and then doing all of the boolean operations in the absolute coordinate system.  I don't think that can work for operations like offset, projection, and extrusion, but maybe I just haven't warped my brain enough. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: How to find the current x, y, z location

Open this post in threaded view
|

## Re: How to find the current x, y, z location

Open this post in threaded view
|

## Re: How to find the current x, y, z location

 In reply to this post by JordanBrown JordanBrown wrote > And even then, you can only tell it where something is relative to its > own transformations... you can't specify an absolute location. > > That is, you can't say > >     sequence( ) of( ) transformations() { >         translate_back_to_origin( ) { >             object( ); >         } >     } > > The only way to do that would be to know the sequence of transformations > and unwind them. Hmm, why would one like to write this? You can simple write             object( ); -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: How to find the current x, y, z location

Open this post in threaded view
|

## Re: How to find the current x, y, z location

Open this post in threaded view
|

## Re: How to find the current x, y, z location

Open this post in threaded view
|

## Re: How to find the current x, y, z location

 In reply to this post by Parkinbot On 3/4/2019 1:51 AM, Parkinbot wrote: ```JordanBrown wrote ``` ```And even then, you can only tell it where something is relative to its own transformations... you can't specify an absolute location. That is, you can't say sequence( ) of( ) transformations() {     translate_back_to_origin( ) {         object( );     } } The only way to do that would be to know the sequence of transformations and unwind them. ``` ```Hmm, why would one like to write this? You can simple write object( ); ``` It was a thought experiment, not a real-world example.  The point was that not only is it impossible to retrieve the absolute position from inside a series of transformations, but it is also impossible to *specify* an absolute position for whatever reason. Perhaps object() is buried in a module with other stuff that *is* intended to be relative to the transformations. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org