RP
Ronaldo Persiano
Sat, Feb 1, 2020 1:42 PM
The C-like for has the following general form:
[for( initialization;
test;
iteration ) output ]
The output is done always after each test and the for escape just after the
test before the output. So, a better understanding of the control structure
is grasped by:
[ for( i = 0,
initialization;
i < 8;
(output(i))
i = i+1,
iteration ) ]
where I suppose a simple iteration and test. In above example, we will
have 8 outputs and 8 iterations besides the initialization. However, the
last iteration does not produces any output. When the iterations include
very demanding processes or illegal operations firing warnings we need to
do tests in the iteration steps in order to avoid them.
All that could be avoided if the output was done always before the test.
The C-like for has the following general form:
[for( initialization;
test;
iteration ) output ]
The output is done always after each test and the for escape just after the
test before the output. So, a better understanding of the control structure
is grasped by:
[ for( i = 0,
initialization;
i < 8;
(output(i))
i = i+1,
iteration ) ]
where I suppose a simple iteration and test. In above example, we will
have 8 outputs and 8 iterations besides the initialization. However, the
last iteration does not produces any output. When the iterations include
very demanding processes or illegal operations firing warnings we need to
do tests in the iteration steps in order to avoid them.
All that could be avoided if the output was done always before the test.
NH
nop head
Sat, Feb 1, 2020 2:11 PM
Isn't it exactly the same as C? The iteration part is done after the loop
body is executed and the test is done before it.
E.g.
initialisation;
while(test) {
....
iteration;
}
So iteration is executed the same number of times as the body and the last
case fails the test.
On Sat, 1 Feb 2020 at 13:42, Ronaldo Persiano rcmpersiano@gmail.com wrote:
The C-like for has the following general form:
[for( initialization;
test;
iteration ) output ]
The output is done always after each test and the for escape just after
the test before the output. So, a better understanding of the control
structure is grasped by:
[ for( i = 0,
initialization;
i < 8;
(output(i))
i = i+1,
iteration ) ]
where I suppose a simple iteration and test. In above example, we will
have 8 outputs and 8 iterations besides the initialization. However, the
last iteration does not produces any output. When the iterations include
very demanding processes or illegal operations firing warnings we need to
do tests in the iteration steps in order to avoid them.
All that could be avoided if the output was done always before the test.
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Isn't it exactly the same as C? The iteration part is done after the loop
body is executed and the test is done before it.
E.g.
initialisation;
while(test) {
....
iteration;
}
So iteration is executed the same number of times as the body and the last
case fails the test.
On Sat, 1 Feb 2020 at 13:42, Ronaldo Persiano <rcmpersiano@gmail.com> wrote:
> The C-like for has the following general form:
>
> [for( initialization;
> test;
> iteration ) output ]
>
>
> The output is done always after each test and the for escape just after
> the test before the output. So, a better understanding of the control
> structure is grasped by:
>
> [ for( i = 0,
> initialization;
>
> i < 8;
>
> (output(i))
>
> i = i+1,
>
> iteration ) ]
>
>
> where I suppose a simple iteration and test. In above example, we will
> have 8 outputs and 8 iterations besides the initialization. However, the
> last iteration does not produces any output. When the iterations include
> very demanding processes or illegal operations firing warnings we need to
> do tests in the iteration steps in order to avoid them.
>
> All that could be avoided if the output was done always before the test.
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
A
adrianv
Sat, Feb 1, 2020 2:49 PM
Well....kind of. The attraction of this loop structure is that you can
assign and reassign variables in the iteration part. And while it may look
C-like, OpenSCAD is not C. In OpenSCAD the loop body isn't "executed" it is
output. In C you would actually do your computations in the output part at
the end. But the whole reason to use this structure in OpenSCAD is so you
can reassign variables. This means the computation all happens in the
iteration block. Here's a simple example:
// Produces length n vector counting from 0
function example(n) =
[each // Needed to avoid singleton list output
for(output=[], i=0;
i<=n;
output=concat(output,[i]), // Here's the computation!
i=i+1,
dummy=echo(i=i,output=output))
if (i==n) output // Output only in the last iteration
];
Note how I can repeatedly add to the output variable without the need for
recursion? So there are two potential issues. One issue is in getting the
answer out. Here you see I need an "if" to output the answer when the
computation is done with a duplicated test for loop termination, which is
easy to detect since the termination test is very simple in this case. The
second issue as Rinaldo notes is that the iteration block runs an extra
time---it must run when i=n even though I don't want output for this case.
So this can produce an error, or it can waste time.
So running echo(example(4)) produces:
ECHO: i = 1, output = [0]
ECHO: i = 2, output = [0, 1]
ECHO: i = 3, output = [0, 1, 2]
ECHO: i = 4, output = [0, 1, 2, 3]
ECHO: i = 5, output = [0, 1, 2, 3, 4]
ECHO: [0, 1, 2, 3]
nophead wrote
Isn't it exactly the same as C? The iteration part is done after the loop
body is executed and the test is done before it.
E.g.
initialisation;
while(test) {
....
iteration;
}
So iteration is executed the same number of times as the body and the last
case fails the test.
On Sat, 1 Feb 2020 at 13:42, Ronaldo Persiano <
The C-like for has the following general form:
[for( initialization;
test;
iteration ) output ]
The output is done always after each test and the for escape just after
the test before the output. So, a better understanding of the control
structure is grasped by:
[ for( i = 0,
initialization;
i < 8;
(output(i))
i = i+1,
iteration ) ]
where I suppose a simple iteration and test. In above example, we will
have 8 outputs and 8 iterations besides the initialization. However, the
last iteration does not produces any output. When the iterations include
very demanding processes or illegal operations firing warnings we need to
do tests in the iteration steps in order to avoid them.
All that could be avoided if the output was done always before the test.
OpenSCAD mailing list
Well....kind of. The attraction of this loop structure is that you can
assign and reassign variables in the iteration part. And while it may look
C-like, OpenSCAD is not C. In OpenSCAD the loop body isn't "executed" it is
output. In C you would actually do your computations in the output part at
the end. But the whole reason to use this structure in OpenSCAD is so you
can reassign variables. This means the computation all happens in the
iteration block. Here's a simple example:
// Produces length n vector counting from 0
function example(n) =
[each // Needed to avoid singleton list output
for(output=[], i=0;
i<=n;
output=concat(output,[i]), // Here's the computation!
i=i+1,
dummy=echo(i=i,output=output))
if (i==n) output // Output only in the last iteration
];
Note how I can repeatedly add to the output variable without the need for
recursion? So there are two potential issues. One issue is in getting the
answer out. Here you see I need an "if" to output the answer when the
computation is done with a duplicated test for loop termination, which is
easy to detect since the termination test is very simple in this case. The
second issue as Rinaldo notes is that the iteration block runs an extra
time---it must run when i=n even though I don't want output for this case.
So this can produce an error, or it can waste time.
So running echo(example(4)) produces:
ECHO: i = 1, output = [0]
ECHO: i = 2, output = [0, 1]
ECHO: i = 3, output = [0, 1, 2]
ECHO: i = 4, output = [0, 1, 2, 3]
ECHO: i = 5, output = [0, 1, 2, 3, 4]
ECHO: [0, 1, 2, 3]
nophead wrote
> Isn't it exactly the same as C? The iteration part is done after the loop
> body is executed and the test is done before it.
>
> E.g.
> initialisation;
> while(test) {
> ....
> iteration;
> }
>
> So iteration is executed the same number of times as the body and the last
> case fails the test.
>
> On Sat, 1 Feb 2020 at 13:42, Ronaldo Persiano <
> rcmpersiano@
> > wrote:
>
>> The C-like for has the following general form:
>>
>> [for( initialization;
>> test;
>> iteration ) output ]
>>
>>
>> The output is done always after each test and the for escape just after
>> the test before the output. So, a better understanding of the control
>> structure is grasped by:
>>
>> [ for( i = 0,
>> initialization;
>>
>> i < 8;
>>
>> (output(i))
>>
>> i = i+1,
>>
>> iteration ) ]
>>
>>
>> where I suppose a simple iteration and test. In above example, we will
>> have 8 outputs and 8 iterations besides the initialization. However, the
>> last iteration does not produces any output. When the iterations include
>> very demanding processes or illegal operations firing warnings we need to
>> do tests in the iteration steps in order to avoid them.
>>
>> All that could be avoided if the output was done always before the test.
>> _______________________________________________
>> OpenSCAD mailing list
>>
> Discuss@.openscad
>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@.openscad
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
--
Sent from: http://forum.openscad.org/
NH
nop head
Sat, Feb 1, 2020 3:07 PM
Perhaps to fix Ronaldo's issue allow comma separated expressions in the
test part that only get executed if the test passes, before the output.
On Sat, 1 Feb 2020 at 14:50, adrianv avm4@cornell.edu wrote:
Well....kind of. The attraction of this loop structure is that you can
assign and reassign variables in the iteration part. And while it may look
C-like, OpenSCAD is not C. In OpenSCAD the loop body isn't "executed" it
is
output. In C you would actually do your computations in the output part
at
the end. But the whole reason to use this structure in OpenSCAD is so you
can reassign variables. This means the computation all happens in the
iteration block. Here's a simple example:
// Produces length n vector counting from 0
function example(n) =
[each // Needed to avoid singleton list output
for(output=[], i=0;
i<=n;
output=concat(output,[i]), // Here's the computation!
i=i+1,
dummy=echo(i=i,output=output))
if (i==n) output // Output only in the last iteration
];
Note how I can repeatedly add to the output variable without the need for
recursion? So there are two potential issues. One issue is in getting the
answer out. Here you see I need an "if" to output the answer when the
computation is done with a duplicated test for loop termination, which is
easy to detect since the termination test is very simple in this case. The
second issue as Rinaldo notes is that the iteration block runs an extra
time---it must run when i=n even though I don't want output for this case.
So this can produce an error, or it can waste time.
So running echo(example(4)) produces:
ECHO: i = 1, output = [0]
ECHO: i = 2, output = [0, 1]
ECHO: i = 3, output = [0, 1, 2]
ECHO: i = 4, output = [0, 1, 2, 3]
ECHO: i = 5, output = [0, 1, 2, 3, 4]
ECHO: [0, 1, 2, 3]
nophead wrote
Isn't it exactly the same as C? The iteration part is done after the loop
body is executed and the test is done before it.
E.g.
initialisation;
while(test) {
....
iteration;
}
So iteration is executed the same number of times as the body and the
case fails the test.
On Sat, 1 Feb 2020 at 13:42, Ronaldo Persiano <
The C-like for has the following general form:
[for( initialization;
test;
iteration ) output ]
The output is done always after each test and the for escape just after
the test before the output. So, a better understanding of the control
structure is grasped by:
[ for( i = 0,
initialization;
i < 8;
(output(i))
i = i+1,
iteration ) ]
where I suppose a simple iteration and test. In above example, we will
have 8 outputs and 8 iterations besides the initialization. However, the
last iteration does not produces any output. When the iterations include
very demanding processes or illegal operations firing warnings we need
do tests in the iteration steps in order to avoid them.
All that could be avoided if the output was done always before the test.
OpenSCAD mailing list
Perhaps to fix Ronaldo's issue allow comma separated expressions in the
test part that only get executed if the test passes, before the output.
On Sat, 1 Feb 2020 at 14:50, adrianv <avm4@cornell.edu> wrote:
> Well....kind of. The attraction of this loop structure is that you can
> assign and reassign variables in the iteration part. And while it may look
> C-like, OpenSCAD is not C. In OpenSCAD the loop body isn't "executed" it
> is
> output. In C you would actually do your computations in the output part
> at
> the end. But the whole reason to use this structure in OpenSCAD is so you
> can reassign variables. This means the computation all happens in the
> iteration block. Here's a simple example:
>
> // Produces length n vector counting from 0
> function example(n) =
> [each // Needed to avoid singleton list output
> for(output=[], i=0;
> i<=n;
> output=concat(output,[i]), // Here's the computation!
> i=i+1,
> dummy=echo(i=i,output=output))
> if (i==n) output // Output only in the last iteration
> ];
>
> Note how I can repeatedly add to the output variable without the need for
> recursion? So there are two potential issues. One issue is in getting the
> answer out. Here you see I need an "if" to output the answer when the
> computation is done with a duplicated test for loop termination, which is
> easy to detect since the termination test is very simple in this case. The
> second issue as Rinaldo notes is that the iteration block runs an extra
> time---it must run when i=n even though I don't want output for this case.
> So this can produce an error, or it can waste time.
>
> So running echo(example(4)) produces:
>
> ECHO: i = 1, output = [0]
> ECHO: i = 2, output = [0, 1]
> ECHO: i = 3, output = [0, 1, 2]
> ECHO: i = 4, output = [0, 1, 2, 3]
> ECHO: i = 5, output = [0, 1, 2, 3, 4]
> ECHO: [0, 1, 2, 3]
>
>
> nophead wrote
> > Isn't it exactly the same as C? The iteration part is done after the loop
> > body is executed and the test is done before it.
> >
> > E.g.
> > initialisation;
> > while(test) {
> > ....
> > iteration;
> > }
> >
> > So iteration is executed the same number of times as the body and the
> last
> > case fails the test.
> >
> > On Sat, 1 Feb 2020 at 13:42, Ronaldo Persiano <
>
> > rcmpersiano@
>
> > > wrote:
> >
> >> The C-like for has the following general form:
> >>
> >> [for( initialization;
> >> test;
> >> iteration ) output ]
> >>
> >>
> >> The output is done always after each test and the for escape just after
> >> the test before the output. So, a better understanding of the control
> >> structure is grasped by:
> >>
> >> [ for( i = 0,
> >> initialization;
> >>
> >> i < 8;
> >>
> >> (output(i))
> >>
> >> i = i+1,
> >>
> >> iteration ) ]
> >>
> >>
> >> where I suppose a simple iteration and test. In above example, we will
> >> have 8 outputs and 8 iterations besides the initialization. However, the
> >> last iteration does not produces any output. When the iterations include
> >> very demanding processes or illegal operations firing warnings we need
> to
> >> do tests in the iteration steps in order to avoid them.
> >>
> >> All that could be avoided if the output was done always before the test.
> >> _______________________________________________
> >> OpenSCAD mailing list
> >>
>
> > Discuss@.openscad
>
> >> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
> >>
> >
> > _______________________________________________
> > OpenSCAD mailing list
>
> > Discuss@.openscad
>
> > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
>
>
>
>
> --
> Sent from: http://forum.openscad.org/
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
RP
Ronaldo Persiano
Sat, Feb 1, 2020 3:13 PM
Isn't it exactly the same as C? The iteration part is done after the loop
body is executed and the test is done before it.
E.g.
initialisation;
while(test) {
....
iteration;
}
So iteration is executed the same number of times as the body and the last
case fails the test.
Yes, but, as adrianv pointed out, in C you may choose where the output is
done: in the middle of the iteration, at the end or at the beginning (the
only choice available in OpenSCAD).
Perhaps to fix Ronaldo's issue allow comma separated expressions in the
test part that only get executed if the test passes, before the output.
Would you elaborate on that with an example?
>
> Isn't it exactly the same as C? The iteration part is done after the loop
> body is executed and the test is done before it.
>
> E.g.
> initialisation;
> while(test) {
> ....
> iteration;
> }
>
> So iteration is executed the same number of times as the body and the last
> case fails the test.
>
Yes, but, as adrianv pointed out, in C you may choose where the output is
done: in the middle of the iteration, at the end or at the beginning (the
only choice available in OpenSCAD).
Perhaps to fix Ronaldo's issue allow comma separated expressions in the
> test part that only get executed if the test passes, before the output.
Would you elaborate on that with an example?
A
adrianv
Sat, Feb 1, 2020 3:33 PM
Perhaps to fix Ronaldo's issue allow comma separated expressions in the
test part that only get executed if the test passes, before the output.
Right now it does not appear you can put any expressions into the test. The
test is simply a condition.
But if you did make this change, it is basically restructuring the command
so that the computations all happen in the condition test instead of all in
the iteration block. That seems kind of weird and confusing. (In a comma
separated list, which thing is the condition?) Changing when the output
occurs seems like a better solution, though it creates its own weird
behaviors, I think mainly if you try to use the for loop in a simpler way,
e.g.
function example2(n) =
[ for(i=0;
i<n;
i=i+1) i];
currently outputs the same vector as my previous example, [0,...,n-1]. But
if we have the output run after the iteration, then it will instead produce
[1,...,n].
--
Sent from: http://forum.openscad.org/
> Perhaps to fix Ronaldo's issue allow comma separated expressions in the
>> test part that only get executed if the test passes, before the output.
Right now it does not appear you can put any expressions into the test. The
test is simply a condition.
But if you did make this change, it is basically restructuring the command
so that the computations all happen in the condition test instead of all in
the iteration block. That seems kind of weird and confusing. (In a comma
separated list, which thing is the condition?) Changing when the output
occurs seems like a better solution, though it creates its own weird
behaviors, I think mainly if you try to use the for loop in a simpler way,
e.g.
function example2(n) =
[ for(i=0;
i<n;
i=i+1) i];
currently outputs the same vector as my previous example, [0,...,n-1]. But
if we have the output run after the iteration, then it will instead produce
[1,...,n].
--
Sent from: http://forum.openscad.org/
NH
nop head
Sat, Feb 1, 2020 3:51 PM
rotations = [for(i = 0, rot = fs_frame(tangents);
i <= last;
i = i + 1,
rot = i <= last ? rotate_from_to(tangents[i - 1],
tangents[i]) * rot : undef) rot];
I was thinking it could be rewritten as:
rotations = [for(i = 0, rot = fs_frame(tangents);
i <= last, rot = rotate_from_to(tangents[i],
tangents[i + 1] ) * rot;
i = i + 1,
) rot];
But the problem is the test gets executed before the first iteration, so it
is not right.
I think if it was written in C it would need a conditional break between
the output and the calculation. I don't think it can be done with a simple
for loop. I.e. it needs to be:
initialisation;
for(;;) {
output;
if(++i >= n)
break;
calculate;
}
In my case there needs to be one less calculate than there are outputs
because the first output is calculated by the initialisation.
On Sat, 1 Feb 2020 at 15:14, Ronaldo Persiano rcmpersiano@gmail.com wrote:
Isn't it exactly the same as C? The iteration part is done after the loop
body is executed and the test is done before it.
E.g.
initialisation;
while(test) {
....
iteration;
}
So iteration is executed the same number of times as the body and the
last case fails the test.
Yes, but, as adrianv pointed out, in C you may choose where the output is
done: in the middle of the iteration, at the end or at the beginning (the
only choice available in OpenSCAD).
Perhaps to fix Ronaldo's issue allow comma separated expressions in the
test part that only get executed if the test passes, before the output.
>From my own code I have:
rotations = [for(i = 0, rot = fs_frame(tangents);
i <= last;
i = i + 1,
rot = i <= last ? rotate_from_to(tangents[i - 1],
tangents[i]) * rot : undef) rot];
I was thinking it could be rewritten as:
rotations = [for(i = 0, rot = fs_frame(tangents);
i <= last, rot = rotate_from_to(tangents[i],
tangents[i + 1] ) * rot;
i = i + 1,
) rot];
But the problem is the test gets executed before the first iteration, so it
is not right.
I think if it was written in C it would need a conditional break between
the output and the calculation. I don't think it can be done with a simple
for loop. I.e. it needs to be:
initialisation;
for(;;) {
output;
if(++i >= n)
break;
calculate;
}
In my case there needs to be one less calculate than there are outputs
because the first output is calculated by the initialisation.
On Sat, 1 Feb 2020 at 15:14, Ronaldo Persiano <rcmpersiano@gmail.com> wrote:
> Isn't it exactly the same as C? The iteration part is done after the loop
>> body is executed and the test is done before it.
>>
>> E.g.
>> initialisation;
>> while(test) {
>> ....
>> iteration;
>> }
>>
>> So iteration is executed the same number of times as the body and the
>> last case fails the test.
>>
>
> Yes, but, as adrianv pointed out, in C you may choose where the output is
> done: in the middle of the iteration, at the end or at the beginning (the
> only choice available in OpenSCAD).
>
> Perhaps to fix Ronaldo's issue allow comma separated expressions in the
>> test part that only get executed if the test passes, before the output.
>
>
> Would you elaborate on that with an example?
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
A
adrianv
Sat, Feb 1, 2020 4:27 PM
In C it can be done without complications something like this:
for(i=0, rot[0] = init, i<=last;i++)
rot[i+1] = rotate_from_to(tangents[i],tangents[i+1])*rot[i];
In C you can create "output" any time you want. You can do the same thing
in openscad using concat, but I assume that would be slow compared to your
posted approach. In my posted example I built up the output using concat.
nophead wrote
From my own code I have:
rotations = [for(i = 0, rot = fs_frame(tangents);
i <= last;
i = i + 1,
rot = i <= last ? rotate_from_to(tangents[i - 1],
tangents[i]) * rot : undef) rot];
I was thinking it could be rewritten as:
rotations = [for(i = 0, rot = fs_frame(tangents);
i <= last, rot = rotate_from_to(tangents[i],
tangents[i + 1] ) * rot;
i = i + 1,
) rot];
But the problem is the test gets executed before the first iteration, so
it
is not right.
I think if it was written in C it would need a conditional break between
the output and the calculation. I don't think it can be done with a simple
for loop. I.e. it needs to be:
initialisation;
for(;;) {
output;
if(++i >= n)
break;
calculate;
}
In my case there needs to be one less calculate than there are outputs
because the first output is calculated by the initialisation.
On Sat, 1 Feb 2020 at 15:14, Ronaldo Persiano <
Isn't it exactly the same as C? The iteration part is done after the loop
body is executed and the test is done before it.
E.g.
initialisation;
while(test) {
....
iteration;
}
So iteration is executed the same number of times as the body and the
last case fails the test.
Yes, but, as adrianv pointed out, in C you may choose where the output is
done: in the middle of the iteration, at the end or at the beginning (the
only choice available in OpenSCAD).
Perhaps to fix Ronaldo's issue allow comma separated expressions in the
test part that only get executed if the test passes, before the output.
Would you elaborate on that with an example?
OpenSCAD mailing list
In C it can be done without complications something like this:
for(i=0, rot[0] = init, i<=last;i++)
rot[i+1] = rotate_from_to(tangents[i],tangents[i+1])*rot[i];
In C you can create "output" any time you want. You can do the same thing
in openscad using concat, but I assume that would be slow compared to your
posted approach. In my posted example I built up the output using concat.
nophead wrote
> From my own code I have:
> rotations = [for(i = 0, rot = fs_frame(tangents);
> i <= last;
> i = i + 1,
> rot = i <= last ? rotate_from_to(tangents[i - 1],
> tangents[i]) * rot : undef) rot];
>
> I was thinking it could be rewritten as:
> rotations = [for(i = 0, rot = fs_frame(tangents);
> i <= last, rot = rotate_from_to(tangents[i],
> tangents[i + 1] ) * rot;
> i = i + 1,
> ) rot];
>
> But the problem is the test gets executed before the first iteration, so
> it
> is not right.
>
> I think if it was written in C it would need a conditional break between
> the output and the calculation. I don't think it can be done with a simple
> for loop. I.e. it needs to be:
>
> initialisation;
> for(;;) {
> output;
> if(++i >= n)
> break;
> calculate;
> }
>
> In my case there needs to be one less calculate than there are outputs
> because the first output is calculated by the initialisation.
>
>
> On Sat, 1 Feb 2020 at 15:14, Ronaldo Persiano <
> rcmpersiano@
> > wrote:
>
>> Isn't it exactly the same as C? The iteration part is done after the loop
>>> body is executed and the test is done before it.
>>>
>>> E.g.
>>> initialisation;
>>> while(test) {
>>> ....
>>> iteration;
>>> }
>>>
>>> So iteration is executed the same number of times as the body and the
>>> last case fails the test.
>>>
>>
>> Yes, but, as adrianv pointed out, in C you may choose where the output is
>> done: in the middle of the iteration, at the end or at the beginning (the
>> only choice available in OpenSCAD).
>>
>> Perhaps to fix Ronaldo's issue allow comma separated expressions in the
>>> test part that only get executed if the test passes, before the output.
>>
>>
>> Would you elaborate on that with an example?
>> _______________________________________________
>> OpenSCAD mailing list
>>
> Discuss@.openscad
>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@.openscad
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
--
Sent from: http://forum.openscad.org/
NH
nop head
Sat, Feb 1, 2020 7:16 PM
Yes although tangents[last+1] does not exist, so the loop needs to be one
less, i.e. i < last. The first element is a specific value and the rest are
calculated from the difference between two tangents, so for n tangents
there are only n-1 differences.
On Sat, 1 Feb 2020 at 16:28, adrianv avm4@cornell.edu wrote:
In C it can be done without complications something like this:
for(i=0, rot[0] = init, i<=last;i++)
rot[i+1] = rotate_from_to(tangents[i],tangents[i+1])*rot[i];
In C you can create "output" any time you want. You can do the same thing
in openscad using concat, but I assume that would be slow compared to your
posted approach. In my posted example I built up the output using
concat.
nophead wrote
From my own code I have:
rotations = [for(i = 0, rot = fs_frame(tangents);
i <= last;
i = i + 1,
rot = i <= last ? rotate_from_to(tangents[i - 1],
tangents[i]) * rot : undef) rot];
I was thinking it could be rewritten as:
rotations = [for(i = 0, rot = fs_frame(tangents);
i <= last, rot = rotate_from_to(tangents[i],
tangents[i + 1] ) * rot;
i = i + 1,
) rot];
But the problem is the test gets executed before the first iteration, so
it
is not right.
I think if it was written in C it would need a conditional break between
the output and the calculation. I don't think it can be done with a
for loop. I.e. it needs to be:
initialisation;
for(;;) {
output;
if(++i >= n)
break;
calculate;
}
In my case there needs to be one less calculate than there are outputs
because the first output is calculated by the initialisation.
On Sat, 1 Feb 2020 at 15:14, Ronaldo Persiano <
Isn't it exactly the same as C? The iteration part is done after the
body is executed and the test is done before it.
E.g.
initialisation;
while(test) {
....
iteration;
}
So iteration is executed the same number of times as the body and the
last case fails the test.
Yes, but, as adrianv pointed out, in C you may choose where the output
done: in the middle of the iteration, at the end or at the beginning
only choice available in OpenSCAD).
Perhaps to fix Ronaldo's issue allow comma separated expressions in the
test part that only get executed if the test passes, before the output.
Would you elaborate on that with an example?
OpenSCAD mailing list
Yes although tangents[last+1] does not exist, so the loop needs to be one
less, i.e. i < last. The first element is a specific value and the rest are
calculated from the difference between two tangents, so for n tangents
there are only n-1 differences.
On Sat, 1 Feb 2020 at 16:28, adrianv <avm4@cornell.edu> wrote:
> In C it can be done without complications something like this:
>
> for(i=0, rot[0] = init, i<=last;i++)
> rot[i+1] = rotate_from_to(tangents[i],tangents[i+1])*rot[i];
>
> In C you can create "output" any time you want. You can do the same thing
> in openscad using concat, but I assume that would be slow compared to your
> posted approach. In my posted example I built up the output using
> concat.
>
>
> nophead wrote
> > From my own code I have:
> > rotations = [for(i = 0, rot = fs_frame(tangents);
> > i <= last;
> > i = i + 1,
> > rot = i <= last ? rotate_from_to(tangents[i - 1],
> > tangents[i]) * rot : undef) rot];
> >
> > I was thinking it could be rewritten as:
> > rotations = [for(i = 0, rot = fs_frame(tangents);
> > i <= last, rot = rotate_from_to(tangents[i],
> > tangents[i + 1] ) * rot;
> > i = i + 1,
> > ) rot];
> >
> > But the problem is the test gets executed before the first iteration, so
> > it
> > is not right.
> >
> > I think if it was written in C it would need a conditional break between
> > the output and the calculation. I don't think it can be done with a
> simple
> > for loop. I.e. it needs to be:
> >
> > initialisation;
> > for(;;) {
> > output;
> > if(++i >= n)
> > break;
> > calculate;
> > }
> >
> > In my case there needs to be one less calculate than there are outputs
> > because the first output is calculated by the initialisation.
> >
> >
> > On Sat, 1 Feb 2020 at 15:14, Ronaldo Persiano <
>
> > rcmpersiano@
>
> > > wrote:
> >
> >> Isn't it exactly the same as C? The iteration part is done after the
> loop
> >>> body is executed and the test is done before it.
> >>>
> >>> E.g.
> >>> initialisation;
> >>> while(test) {
> >>> ....
> >>> iteration;
> >>> }
> >>>
> >>> So iteration is executed the same number of times as the body and the
> >>> last case fails the test.
> >>>
> >>
> >> Yes, but, as adrianv pointed out, in C you may choose where the output
> is
> >> done: in the middle of the iteration, at the end or at the beginning
> (the
> >> only choice available in OpenSCAD).
> >>
> >> Perhaps to fix Ronaldo's issue allow comma separated expressions in the
> >>> test part that only get executed if the test passes, before the output.
> >>
> >>
> >> Would you elaborate on that with an example?
> >> _______________________________________________
> >> OpenSCAD mailing list
> >>
>
> > Discuss@.openscad
>
> >> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
> >>
> >
> > _______________________________________________
> > OpenSCAD mailing list
>
> > Discuss@.openscad
>
> > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
>
>
>
>
> --
> Sent from: http://forum.openscad.org/
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
RP
Ronaldo Persiano
Sat, Feb 1, 2020 8:41 PM
In C you can create "output" any time you want. You can do the same thing
in openscad using concat, but I assume that would be slow compared to your
posted approach. In my posted example I built up the output using
concat.
I don't see how concat may be of any help here. If your code is:
[ for( init; // no "output" variable initialization
test;
iteration part 1,
output = concat(output, [value]),
iteration part 2
) if(last_iteration) each output ]
you may equivalently write:
[ for( init; // no "output" variable initialization
test;
iteration part 1,
output = value,
iteration part 2
) if( ! first output) output ]
without concat and each.
Your posted example for instance could be rewritten as:
function example(n) =
[for( i = 0;
i<=n;
output = i, // Here's the computation!
i = i+1//,
) if(i>0) output // avoids the nonexistent initial "output"
];
What remains unsolved are the illegal or time consuming operations in the
iteration section after the output computation. They have to be filtered
one by one.
>
> In C you can create "output" any time you want. You can do the same thing
> in openscad using concat, but I assume that would be slow compared to your
> posted approach. In my posted example I built up the output using
> concat.
>
I don't see how concat may be of any help here. If your code is:
[ for( init; // no "output" variable initialization
test;
iteration part 1,
output = concat(output, [value]),
iteration part 2
) if(last_iteration) each output ]
you may equivalently write:
[ for( init; // no "output" variable initialization
test;
iteration part 1,
output = value,
iteration part 2
) if( ! first output) output ]
without concat and each.
Your posted example for instance could be rewritten as:
function example(n) =
[for( i = 0;
i<=n;
output = i, // Here's the computation!
i = i+1//,
) if(i>0) output // avoids the nonexistent initial "output"
];
What remains unsolved are the illegal or time consuming operations in the
iteration section after the output computation. They have to be filtered
one by one.