[OpenSCAD] OpenSCAD Digest, Vol 23, Issue 11

William Adams william_a_adams at msn.com
Sun Oct 23 20:28:01 CEST 2011


More examples of ternary:
function map_array(range, u) = (u*range) >= range ? range-1 : floor(u*range); 


and cascaded functions, with ternaries, that themselves call inbuilt functions, but not themselves:
function SSCos(shape, phi) = pow( abs(cos(shape[0]*phi/4) / shape[4]), shape[2]);
function SSSin(shape, phi) = pow(abs(sin(shape[0]*phi/4) / shape[5]), shape[3]);
function SSR(shape, phi) = pow((SSCos(shape,phi) + SSSin(shape,phi)), 1/shape[1]);

function _EvalSuperShape2D3(phi, r) = 
	abs(r) == 0 ? [0,0,0] : [1/r * cos(phi), 1/r*sin(phi),0];


function _EvalSuperShape2D2(phi, n1, t1, t2) =

	_EvalSuperShape2D3(phi, t1, t2, r=pow(t1+t2, 1/n1));


function EvalSuperShape2D(shape, phi) = _EvalSuperShape2D2(phi, shape[1], t1=SSCos(shape,phi), t2=SSSin(shape,phi));
I've thought about tail recursion as a mechanism for unrolling loops, but haven't figured out a way to express that in OpenScad.
Any ideas?
-- William

===============================

- Shaping clay is easier than digging it out of the ground.


http://blog.nanotechstyles.com
http://www.thingiverse.com/WilliamAAdams
https://github.com/Wiladams
 


Date: Sun, 23 Oct 2011 19:17:02 +0100
From: nop.head at gmail.com
To: openscad at rocklinux.org
Subject: Re: [OpenSCAD] OpenSCAD Digest, Vol 23, Issue 11

And I seem to remember reading somewhere that any loop can be converted to tail recursion, so it would seem to be a complete solution for iteration in functions.

On 23 October 2011 19:14, nop head <nop.head at gmail.com> wrote:

Actually it does work. I forgot that you need a boolean. This works:
function toto(k) = k > 0 ? toto(k - 1) + k : 0;


On 23 October 2011 19:06, Whosawhatsis <whosawhatsis at gmail.com> wrote:



                Are you sure that the ternary operator exists in OpenSCAD? I've tried to use it several times and it has never worked, forcing me to write a significantly longer script to code around it. I've never used a function in OpenSCAD (only modules), but if it doesn't work in functions, perhaps there are other situations where it would be very useful but doesn't work.


                
                 
                On Sunday, October 23, 2011 at 10:48 AM, nop head wrote:
                
                    It appears that you can have recursions because if a function calls itself openscad crashes. But for some reason the ternary operator seems to not work on functions. Is that just a bug or is there a fundamental reason why it doesn't?



If it did you could solve the problem with
function toto(k) = k ? toto(k-1) + k : 0;

On 23 October 2011 17:47, William Adams <william_a_adams at msn.com> wrote:







But of course RapCAD can do this!
 
It sure would be useful if functions could have bodies, and recursion.  But, plenty can be done without it.
 
You just have to leave typical notions of programming behind, and just do your loops as part of a module, where instead of returning a result, you code up an object directly.



 
Another way OpenScad could be extended is if the child() module could give you access to the various attributes of an object.  In particular the points mesh.  Then I could stuff anything into the points mesh, and read it somewhere else.  This is similar to what GLSL did in the early days where 'texture' was just another name for 'random memory'.



 
-- William

===============================

- Shaping clay is easier than digging it out of the ground.


http://blog.nanotechstyles.com
http://www.thingiverse.com/WilliamAAdams
https://github.com/Wiladams



 

 
Date: Sun, 23 Oct 2011 16:55:36 +0100
From: nop.head at gmail.com
To: openscad at rocklinux.org



Subject: Re: [OpenSCAD] OpenSCAD Digest, Vol 23, Issue 11

Surely it's an AP: function toto(k) = (k+1) * k / 2;
But that steps round the issue that there are no loops inside functions.




On 23 October 2011 16:46, Giles Bathgate <giles.bathgate at gmail.com> wrote:
> function toto(k) { return k + 1; }





Syntax error you want:



  function toto(k) = k+1;



Regards



Giles

_______________________________________________

OpenSCAD mailing list

OpenSCAD at rocklinux.org

http://rocklinux.net/mailman/listinfo/openscad




_______________________________________________
OpenSCAD mailing list
OpenSCAD at rocklinux.org
http://rocklinux.net/mailman/listinfo/openscad 		 	   		  

_______________________________________________

OpenSCAD mailing list

OpenSCAD at rocklinux.org

http://rocklinux.net/mailman/listinfo/openscad




_______________________________________________
OpenSCAD mailing list
OpenSCAD at rocklinux.org
http://rocklinux.net/mailman/listinfo/openscad



                 
                 
                 
                 
                
                 
                
                    

                
            
_______________________________________________

OpenSCAD mailing list

OpenSCAD at rocklinux.org

http://rocklinux.net/mailman/listinfo/openscad







_______________________________________________
OpenSCAD mailing list
OpenSCAD at rocklinux.org
http://rocklinux.net/mailman/listinfo/openscad 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rocklinux.net/pipermail/openscad/attachments/20111023/80b5867e/attachment-0001.html


More information about the OpenSCAD mailing list