discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Barnsley Fern recursive

EB
Eric Buijs
Tue, Sep 11, 2018 8:06 PM

It's probably a crazy idea but I've created a simple script for the Barnsley
Fern fractal. The script works but it brings my PC, an 2011 iMac, to it's
knees (OpenSCAD takes over 10Gb of memory with the script below and I want
to increase the number of objects even further). I was wondering if someone
has any suggestions to improve/optimize the script and make it less memory
hungry. Or is OpenSCAD not suitable for this kind of work. Thanks.

m1 = [[0,0],[0,0.16]];

c1 = [0,0];

m2 = [[0.85,0.04],[-0.04,0.85]];

c2 = [0, 0.16];

m3 = [[0.2,-0.26],[0.23,0.22]];

c3 = [0,1.6];

m4 = [[-0.15,0.28],[0.26,0.24]];

c4 = [0,0.44];

function f1(p) = m1 * p + c1;

function f2(p) = m2 * p + c2;

function f3(p) = m3 * p + c3;

function f4(p) = m4 * p + c4;

module plotCircle(p) {
translate(100 * p) circle(r=0.5,$fn=50);
}

module BarnsleyFern(p,index) {
r = rands(0,1,1)[0];
plotCircle(p);
if (r <= 0.01) {
BarnsleyFern(f1(p), index-1);
}
else if (r > 0.01 && r <=0.86) {
BarnsleyFern(f2(p),index-1);
}
else if (r > 0.86 && r <=0.94) {
BarnsleyFern(f3(p),index-1);
}
else if (r > 0.94 && r < 0.99) {
BarnsleyFern(f4(p),index-1);
}
}

p = [0,0];
BarnsleyFern(p,20000);

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

It's probably a crazy idea but I've created a simple script for the Barnsley Fern fractal. The script works but it brings my PC, an 2011 iMac, to it's knees (OpenSCAD takes over 10Gb of memory with the script below and I want to increase the number of objects even further). I was wondering if someone has any suggestions to improve/optimize the script and make it less memory hungry. Or is OpenSCAD not suitable for this kind of work. Thanks. m1 = [[0,0],[0,0.16]]; c1 = [0,0]; m2 = [[0.85,0.04],[-0.04,0.85]]; c2 = [0, 0.16]; m3 = [[0.2,-0.26],[0.23,0.22]]; c3 = [0,1.6]; m4 = [[-0.15,0.28],[0.26,0.24]]; c4 = [0,0.44]; function f1(p) = m1 * p + c1; function f2(p) = m2 * p + c2; function f3(p) = m3 * p + c3; function f4(p) = m4 * p + c4; module plotCircle(p) { translate(100 * p) circle(r=0.5,$fn=50); } module BarnsleyFern(p,index) { r = rands(0,1,1)[0]; plotCircle(p); if (r <= 0.01) { BarnsleyFern(f1(p), index-1); } else if (r > 0.01 && r <=0.86) { BarnsleyFern(f2(p),index-1); } else if (r > 0.86 && r <=0.94) { BarnsleyFern(f3(p),index-1); } else if (r > 0.94 && r < 0.99) { BarnsleyFern(f4(p),index-1); } } p = [0,0]; BarnsleyFern(p,20000); -- Sent from: http://forum.openscad.org/
CS
Colin Smart
Tue, Sep 11, 2018 8:59 PM

If you are just drawing, consider using a different tool, such as processing.org

It avoids the extra effort of maintaining an ‘object’.

If you want an object, then I would generally look to do the numerical
processing elsewhere and import the shapes for rendering.

Others may have better ideas.

Colin


Colin Smart (07968 049660)

On 11 Sep 2018, at 21:06, Eric Buijs ebuijs@mac.com wrote:

It's probably a crazy idea but I've created a simple script for the Barnsley
Fern fractal. The script works but it brings my PC, an 2011 iMac, to it's
knees (OpenSCAD takes over 10Gb of memory with the script below and I want
to increase the number of objects even further). I was wondering if someone
has any suggestions to improve/optimize the script and make it less memory
hungry. Or is OpenSCAD not suitable for this kind of work. Thanks.

m1 = [[0,0],[0,0.16]];

c1 = [0,0];

m2 = [[0.85,0.04],[-0.04,0.85]];

c2 = [0, 0.16];

m3 = [[0.2,-0.26],[0.23,0.22]];

c3 = [0,1.6];

m4 = [[-0.15,0.28],[0.26,0.24]];

c4 = [0,0.44];

function f1(p) = m1 * p + c1;

function f2(p) = m2 * p + c2;

function f3(p) = m3 * p + c3;

function f4(p) = m4 * p + c4;

module plotCircle(p) {
translate(100 * p) circle(r=0.5,$fn=50);
}

module BarnsleyFern(p,index) {
r = rands(0,1,1)[0];
plotCircle(p);
if (r <= 0.01) {
BarnsleyFern(f1(p), index-1);
}
else if (r > 0.01 && r <=0.86) {
BarnsleyFern(f2(p),index-1);
}
else if (r > 0.86 && r <=0.94) {
BarnsleyFern(f3(p),index-1);
}
else if (r > 0.94 && r < 0.99) {
BarnsleyFern(f4(p),index-1);
}
}

p = [0,0];
BarnsleyFern(p,20000);

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


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

If you are just drawing, consider using a different tool, such as processing.org It avoids the extra effort of maintaining an ‘object’. If you want an object, then I would generally look to do the numerical processing elsewhere and import the shapes for rendering. Others may have better ideas. Colin --------------------------------- Colin Smart (07968 049660) > On 11 Sep 2018, at 21:06, Eric Buijs <ebuijs@mac.com> wrote: > > It's probably a crazy idea but I've created a simple script for the Barnsley > Fern fractal. The script works but it brings my PC, an 2011 iMac, to it's > knees (OpenSCAD takes over 10Gb of memory with the script below and I want > to increase the number of objects even further). I was wondering if someone > has any suggestions to improve/optimize the script and make it less memory > hungry. Or is OpenSCAD not suitable for this kind of work. Thanks. > > m1 = [[0,0],[0,0.16]]; > > c1 = [0,0]; > > m2 = [[0.85,0.04],[-0.04,0.85]]; > > c2 = [0, 0.16]; > > m3 = [[0.2,-0.26],[0.23,0.22]]; > > c3 = [0,1.6]; > > m4 = [[-0.15,0.28],[0.26,0.24]]; > > c4 = [0,0.44]; > > function f1(p) = m1 * p + c1; > > function f2(p) = m2 * p + c2; > > function f3(p) = m3 * p + c3; > > function f4(p) = m4 * p + c4; > > module plotCircle(p) { > translate(100 * p) circle(r=0.5,$fn=50); > } > > module BarnsleyFern(p,index) { > r = rands(0,1,1)[0]; > plotCircle(p); > if (r <= 0.01) { > BarnsleyFern(f1(p), index-1); > } > else if (r > 0.01 && r <=0.86) { > BarnsleyFern(f2(p),index-1); > } > else if (r > 0.86 && r <=0.94) { > BarnsleyFern(f3(p),index-1); > } > else if (r > 0.94 && r < 0.99) { > BarnsleyFern(f4(p),index-1); > } > } > > > p = [0,0]; > BarnsleyFern(p,20000); > > > > > > > > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
TP
Torsten Paul
Tue, Sep 11, 2018 9:11 PM

That exact script takes less than a second for me and memory does
not grow over 350MB when running multiple times (using the latest
nightly build on Linux).

ciao,
Torsten.

That exact script takes less than a second for me and memory does not grow over 350MB when running multiple times (using the latest nightly build on Linux). ciao, Torsten.
MS
Mark Schafer
Tue, Sep 11, 2018 9:18 PM

yet another good reason for a binary somewhere - even if its not a
release...
I'm on Windows.

On 9/12/2018 9:11 AM, Torsten Paul wrote:

That exact script takes less than a second for me and memory does
not grow over 350MB when running multiple times (using the latest
nightly build on Linux).

ciao,
  Torsten.

yet another good reason for a binary somewhere - even if its not a release... I'm on Windows. On 9/12/2018 9:11 AM, Torsten Paul wrote: > That exact script takes less than a second for me and memory does > not grow over 350MB when running multiple times (using the latest > nightly build on Linux). > > ciao, >   Torsten.
TP
Torsten Paul
Tue, Sep 11, 2018 9:23 PM

On 09/11/2018 11:18 PM, Mark Schafer wrote:

yet another good reason for a binary somewhere - even if > its not a release... I'm on Windows.

What's wrong with the one from the official download page?

http://www.openscad.org/downloads.html#snapshots

ciao,
Torsten.

On 09/11/2018 11:18 PM, Mark Schafer wrote: > yet another good reason for a binary somewhere - even if > its not a release... I'm on Windows. > What's wrong with the one from the official download page? http://www.openscad.org/downloads.html#snapshots ciao, Torsten.
EB
Eric Buijs
Tue, Sep 11, 2018 9:47 PM

Thanks Thorsten, I will try the latest development snapshot.

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

Thanks Thorsten, I will try the latest development snapshot. -- Sent from: http://forum.openscad.org/
MS
Mark Schafer
Tue, Sep 11, 2018 9:52 PM

I'm old - I forgot :(

On 9/12/2018 9:23 AM, Torsten Paul wrote:

On 09/11/2018 11:18 PM, Mark Schafer wrote:

yet another good reason for a binary somewhere - even if > its not a
release... I'm on Windows.

What's wrong with the one from the official download page?

http://www.openscad.org/downloads.html#snapshots

ciao,
  Torsten.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

I'm old - I forgot :( On 9/12/2018 9:23 AM, Torsten Paul wrote: > On 09/11/2018 11:18 PM, Mark Schafer wrote: >> yet another good reason for a binary somewhere - even if > its not a >> release... I'm on Windows. >> > What's wrong with the one from the official download page? > > http://www.openscad.org/downloads.html#snapshots > > ciao, >   Torsten. > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
EB
Eric Buijs
Wed, Sep 12, 2018 9:20 AM

You're right Torsten, I've tried the latest experimental build for OSX and it
makes a huge difference in memory. I've updated the code because I hadn't
been very thoughtful about the base case (see below for updated code). One
more thing. The recursive loop refuses to go beyond 1300 (or so). After that
I get the message: ERROR: Recursion detected calling module 'BarnsleyFern'
Any thoughts on that. Thanks guys.

m1 = [[0,0],[0,0.16]];

c1 = [0,0];

m2 = [[0.85,0.04],[-0.04,0.85]];

c2 = [0, 0.16];

m3 = [[0.2,-0.26],[0.23,0.22]];

c3 = [0,1.6];

m4 = [[-0.15,0.28],[0.26,0.24]];

c4 = [0,0.44];

function f1(p) = m1 * p + c1;

function f2(p) = m2 * p + c2;

function f3(p) = m3 * p + c3;

function f4(p) = m4 * p + c4;

module plotCircle(p) {
translate(100 * p) circle(r=0.5,$fn=50);
}

module BarnsleyFern(p,index) {
r = rands(0,1,1)[0];
plotCircle(p);
//echo(r,index);
if (r <= 0.01) {
BarnsleyFern(f1(p), index-1);
}
else if (r > 0.01 && r <=0.86) {
BarnsleyFern(f2(p),index-1);
}
else if (r > 0.86 && r <=0.94) {
BarnsleyFern(f3(p),index-1);
}
else if (index > 0) {
BarnsleyFern(f4(p),index-1);
}
}

p = [0,0];
BarnsleyFern(p,1400);

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

You're right Torsten, I've tried the latest experimental build for OSX and it makes a huge difference in memory. I've updated the code because I hadn't been very thoughtful about the base case (see below for updated code). One more thing. The recursive loop refuses to go beyond 1300 (or so). After that I get the message: ERROR: Recursion detected calling module 'BarnsleyFern' Any thoughts on that. Thanks guys. m1 = [[0,0],[0,0.16]]; c1 = [0,0]; m2 = [[0.85,0.04],[-0.04,0.85]]; c2 = [0, 0.16]; m3 = [[0.2,-0.26],[0.23,0.22]]; c3 = [0,1.6]; m4 = [[-0.15,0.28],[0.26,0.24]]; c4 = [0,0.44]; function f1(p) = m1 * p + c1; function f2(p) = m2 * p + c2; function f3(p) = m3 * p + c3; function f4(p) = m4 * p + c4; module plotCircle(p) { translate(100 * p) circle(r=0.5,$fn=50); } module BarnsleyFern(p,index) { r = rands(0,1,1)[0]; plotCircle(p); //echo(r,index); if (r <= 0.01) { BarnsleyFern(f1(p), index-1); } else if (r > 0.01 && r <=0.86) { BarnsleyFern(f2(p),index-1); } else if (r > 0.86 && r <=0.94) { BarnsleyFern(f3(p),index-1); } else if (index > 0) { BarnsleyFern(f4(p),index-1); } } p = [0,0]; BarnsleyFern(p,1400); -- Sent from: http://forum.openscad.org/
EB
Eric Buijs
Wed, Sep 12, 2018 9:25 AM

Colin, thanks for your reply. I use P5.js (which is similar to Processing but
uses Javascript instead of Java) from time to time but it's just that I like
to 'torture' OpenSCAD to see where it's limits are ;-)

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

Colin, thanks for your reply. I use P5.js (which is similar to Processing but uses Javascript instead of Java) from time to time but it's just that I like to 'torture' OpenSCAD to see where it's limits are ;-) -- Sent from: http://forum.openscad.org/
TP
Torsten Paul
Wed, Sep 12, 2018 9:37 AM

On 09/12/2018 11:20 AM, Eric Buijs wrote:

One more thing. The recursive loop refuses to go beyond 1300
(or so). After that I get the message: ERROR: Recursion detected
calling module 'BarnsleyFern' Any thoughts on that. Thanks guys.

That means it's running out of stack space, while not perfect,
it's trying to catch this issue before it just crashes.

I'm not sure if MacOS allows for changing the stack size available
to applications, but if it does, OpenSCAD tries to pick up that
limit.

Something to try would be running from a terminal window:

ulimit -s 65532
/Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD

ciao,
Torsten.

On 09/12/2018 11:20 AM, Eric Buijs wrote: > One more thing. The recursive loop refuses to go beyond 1300 > (or so). After that I get the message: ERROR: Recursion detected > calling module 'BarnsleyFern' Any thoughts on that. Thanks guys. > That means it's running out of stack space, while not perfect, it's trying to catch this issue before it just crashes. I'm not sure if MacOS allows for changing the stack size available to applications, but if it does, OpenSCAD tries to pick up that limit. Something to try would be running from a terminal window: > ulimit -s 65532 > /Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD ciao, Torsten.