ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Metafun : save memory content ?
@ 2022-07-18 13:20 Fabrice L via ntg-context
  2022-07-18 14:39 ` Henning Hraban Ramm via ntg-context
  2022-07-18 16:31 ` Hans Hagen via ntg-context
  0 siblings, 2 replies; 8+ messages in thread
From: Fabrice L via ntg-context @ 2022-07-18 13:20 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Fabrice L

Dear list,

I’m doing animations in MetaFun, by simply using \startMPpage (…) \stopMPpage for each frame of the animation. Here is an example: https://youtu.be/yhxUbVQx9Uo (please note you can watch it in HD by adjusting the setting in YouTube).

This works usually great, but when the number of objects to draw by page is large, the resulting pdf file could be huge, several gigabytes sometimes. Such huge files are difficult (sometimes impossible) to convert in jpeg files (I’m using Image magick for the conversion), necessary to do a mp4 movie. 

One solution is to do several pdf files, for example one by second; with this option, I can do 60 files of 30 pages, for an animation of 60 seconds (1800 frames for 1 minute). Everything is smoother this way, quicker to convert files. But in order to do so, I have to be able to describe each frame of the animation with the same set of instructions; so the position of an object at time t is described by an equation for example.

But this is not always possible. If the animation is based on a random process, the position of an object at time t depends on his position at time t-1. I can still use this preceeding strategy by simulating the past at the begging of each second, starting with a unique random seed. So for example if I work on second 9, I begin by calculating the past from frame 1 to frame 240 (8*30) and then simulate time 241, draw frame 241, simulate time 242, draw time 242, etc.. This could be complicated to do, but is feasible. The drawback is that more I advance in time, the more computations I have to do at the beginning of each file. 

So to simplify things, one option would be to be save the state of some variables at some point in time, say t, in order to be able to access these informations at time t+1. So the strategy would be :

% File second1.pdf
for frame =1 to 30 :
	simulate position of objects ;
	draw the frame ;
endfor;
save the position of all objects ;

% File second2.pdf
Read (access) the position of all objects from second 1(after frame 30);
for frame =31 to 60 :
	simulate position of objects ;
	draw the frame ;
endfor;
save the position of all objects ;

Etc... 

Does someone has an idea of how to do such a thing ? I can read text file in Context with no problem and integrate them in the animation process with \startMPinclusions (…) \stopMPinclusions, but I did not find a way to write information from MetaFun to a text file. Is Lua the solution ? 

Thanks for any help !
Fabrice.
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Metafun : save memory content ?
  2022-07-18 13:20 Metafun : save memory content ? Fabrice L via ntg-context
@ 2022-07-18 14:39 ` Henning Hraban Ramm via ntg-context
  2022-07-18 15:45   ` Hans Hagen via ntg-context
  2022-07-18 16:31 ` Hans Hagen via ntg-context
  1 sibling, 1 reply; 8+ messages in thread
From: Henning Hraban Ramm via ntg-context @ 2022-07-18 14:39 UTC (permalink / raw)
  To: ntg-context; +Cc: Henning Hraban Ramm

Am 18.07.22 um 15:20 schrieb Fabrice L via ntg-context:
> Dear list,
> 
> I’m doing animations in MetaFun, by simply using \startMPpage (…) \stopMPpage for each frame of the animation. Here is an example: https://youtu.be/yhxUbVQx9Uo (please note you can watch it in HD by adjusting the setting in YouTube).

A great piece again! I’m amazed every time I look at your homepage 
(https://art-aleatoire.com for those who don’t know it).


> Does someone has an idea of how to do such a thing ? I can read text file in Context with no problem and integrate them in the animation process with \startMPinclusions (…) \stopMPinclusions, but I did not find a way to write information from MetaFun to a text file. Is Lua the solution ?

Lua is always the solution ;) but I don’t understand enough of both Lua 
and MetaFun...

Maybe there’s something in luametafun.pdf*?

*) Hans, this is one of the manuals that doesn’t have interaction, i.e. 
links and bookmarks; would be more usable with them.

Hraban
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Metafun : save memory content ?
  2022-07-18 14:39 ` Henning Hraban Ramm via ntg-context
@ 2022-07-18 15:45   ` Hans Hagen via ntg-context
  0 siblings, 0 replies; 8+ messages in thread
From: Hans Hagen via ntg-context @ 2022-07-18 15:45 UTC (permalink / raw)
  To: Henning Hraban Ramm via ntg-context; +Cc: Hans Hagen

On 7/18/2022 4:39 PM, Henning Hraban Ramm via ntg-context wrote:
> Am 18.07.22 um 15:20 schrieb Fabrice L via ntg-context:
>> Dear list,
>>
>> I’m doing animations in MetaFun, by simply using \startMPpage (…) 
>> \stopMPpage for each frame of the animation. Here is an example: 
>> https://youtu.be/yhxUbVQx9Uo (please note you can watch it in HD by 
>> adjusting the setting in YouTube).
> 
> A great piece again! I’m amazed every time I look at your homepage 
> (https://art-aleatoire.com for those who don’t know it).
> 
> 
>> Does someone has an idea of how to do such a thing ? I can read text 
>> file in Context with no problem and integrate them in the animation 
>> process with \startMPinclusions (…) \stopMPinclusions, but I did not 
>> find a way to write information from MetaFun to a text file. Is Lua 
>> the solution ?
> 
> Lua is always the solution ;) but I don’t understand enough of both Lua 
> and MetaFun...
> 
> Maybe there’s something in luametafun.pdf*?
> 
> *) Hans, this is one of the manuals that doesn’t have interaction, i.e. 
> links and bookmarks; would be more usable with them.
time ... time .. time

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Metafun : save memory content ?
  2022-07-18 13:20 Metafun : save memory content ? Fabrice L via ntg-context
  2022-07-18 14:39 ` Henning Hraban Ramm via ntg-context
@ 2022-07-18 16:31 ` Hans Hagen via ntg-context
  2022-07-18 17:53   ` Hans van der Meer via ntg-context
  2022-07-19  3:28   ` Fabrice L via ntg-context
  1 sibling, 2 replies; 8+ messages in thread
From: Hans Hagen via ntg-context @ 2022-07-18 16:31 UTC (permalink / raw)
  To: Fabrice L via ntg-context; +Cc: Hans Hagen

On 7/18/2022 3:20 PM, Fabrice L via ntg-context wrote:
> Dear list,
> 
> I’m doing animations in MetaFun, by simply using \startMPpage (…) \stopMPpage for each frame of the animation. Here is an example: https://youtu.be/yhxUbVQx9Uo (please note you can watch it in HD by adjusting the setting in YouTube).
> 
> This works usually great, but when the number of objects to draw by page is large, the resulting pdf file could be huge, several gigabytes sometimes. Such huge files are difficult (sometimes impossible) to convert in jpeg files (I’m using Image magick for the conversion), necessary to do a mp4 movie.
> 
> One solution is to do several pdf files, for example one by second; with this option, I can do 60 files of 30 pages, for an animation of 60 seconds (1800 frames for 1 minute). Everything is smoother this way, quicker to convert files. But in order to do so, I have to be able to describe each frame of the animation with the same set of instructions; so the position of an object at time t is described by an equation for example.
> 
> But this is not always possible. If the animation is based on a random process, the position of an object at time t depends on his position at time t-1. I can still use this preceeding strategy by simulating the past at the begging of each second, starting with a unique random seed. So for example if I work on second 9, I begin by calculating the past from frame 1 to frame 240 (8*30) and then simulate time 241, draw frame 241, simulate time 242, draw time 242, etc.. This could be complicated to do, but is feasible. The drawback is that more I advance in time, the more computations I have to do at the beginning of each file.
> 
> So to simplify things, one option would be to be save the state of some variables at some point in time, say t, in order to be able to access these informations at time t+1. So the strategy would be :
> 
> % File second1.pdf
> for frame =1 to 30 :
> 	simulate position of objects ;
> 	draw the frame ;
> endfor;
> save the position of all objects ;
> 
> % File second2.pdf
> Read (access) the position of all objects from second 1(after frame 30);
> for frame =31 to 60 :
> 	simulate position of objects ;
> 	draw the frame ;
> endfor;
> save the position of all objects ;
> 
> Etc...
> 
> Does someone has an idea of how to do such a thing ? I can read text file in Context with no problem and integrate them in the animation process with \startMPinclusions (…) \stopMPinclusions, but I did not find a way to write information from MetaFun to a text file. Is Lua the solution ?
there's always an idea ...

\starttext
     \startMPpage
         runscript("metapost.setparameterset('test', 
table.load('mydata.lua') or { })") ;
         path b ; b := getparameterpath "test" "test_c";
         draw fullcircle scaled 3cm ;
         draw b scaled .4;
       % show(b);
         getparameters "test" [
             test_a = 123,
             test_b = (456,789),
             test_c = (boundingbox currentpicture)
         ] ;
 
runscript("table.save('mydata.lua',(metapost.getparameterset('test')))") ;
     \stopMPpage
\stoptext

but maybe i need abetter one .. a proper save/restore in the tuc file or 
so, but you can play with this first

Hans


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Metafun : save memory content ?
  2022-07-18 16:31 ` Hans Hagen via ntg-context
@ 2022-07-18 17:53   ` Hans van der Meer via ntg-context
  2022-07-19  3:28   ` Fabrice L via ntg-context
  1 sibling, 0 replies; 8+ messages in thread
From: Hans van der Meer via ntg-context @ 2022-07-18 17:53 UTC (permalink / raw)
  To: NTG ConTeXt; +Cc: Hans van der Meer


[-- Attachment #1.1: Type: text/plain, Size: 4424 bytes --]

Storing in a memory block in the computers' internal memory would be super, of course. But I am afraid that is asking a bit too much;-)

dr. Hans van der Meer


> On 18 Jul 2022, at 18:31, Hans Hagen via ntg-context <ntg-context@ntg.nl> wrote:
> 
> On 7/18/2022 3:20 PM, Fabrice L via ntg-context wrote:
>> Dear list,
>> I’m doing animations in MetaFun, by simply using \startMPpage (…) \stopMPpage for each frame of the animation. Here is an example: https://youtu.be/yhxUbVQx9Uo (please note you can watch it in HD by adjusting the setting in YouTube).
>> This works usually great, but when the number of objects to draw by page is large, the resulting pdf file could be huge, several gigabytes sometimes. Such huge files are difficult (sometimes impossible) to convert in jpeg files (I’m using Image magick for the conversion), necessary to do a mp4 movie.
>> One solution is to do several pdf files, for example one by second; with this option, I can do 60 files of 30 pages, for an animation of 60 seconds (1800 frames for 1 minute). Everything is smoother this way, quicker to convert files. But in order to do so, I have to be able to describe each frame of the animation with the same set of instructions; so the position of an object at time t is described by an equation for example.
>> But this is not always possible. If the animation is based on a random process, the position of an object at time t depends on his position at time t-1. I can still use this preceeding strategy by simulating the past at the begging of each second, starting with a unique random seed. So for example if I work on second 9, I begin by calculating the past from frame 1 to frame 240 (8*30) and then simulate time 241, draw frame 241, simulate time 242, draw time 242, etc.. This could be complicated to do, but is feasible. The drawback is that more I advance in time, the more computations I have to do at the beginning of each file.
>> So to simplify things, one option would be to be save the state of some variables at some point in time, say t, in order to be able to access these informations at time t+1. So the strategy would be :
>> % File second1.pdf
>> for frame =1 to 30 :
>> 	simulate position of objects ;
>> 	draw the frame ;
>> endfor;
>> save the position of all objects ;
>> % File second2.pdf
>> Read (access) the position of all objects from second 1(after frame 30);
>> for frame =31 to 60 :
>> 	simulate position of objects ;
>> 	draw the frame ;
>> endfor;
>> save the position of all objects ;
>> Etc...
>> Does someone has an idea of how to do such a thing ? I can read text file in Context with no problem and integrate them in the animation process with \startMPinclusions (…) \stopMPinclusions, but I did not find a way to write information from MetaFun to a text file. Is Lua the solution ?
> there's always an idea ...
> 
> \starttext
>    \startMPpage
>        runscript("metapost.setparameterset('test', table.load('mydata.lua') or { })") ;
>        path b ; b := getparameterpath "test" "test_c";
>        draw fullcircle scaled 3cm ;
>        draw b scaled .4;
>      % show(b);
>        getparameters "test" [
>            test_a = 123,
>            test_b = (456,789),
>            test_c = (boundingbox currentpicture)
>        ] ;
> runscript("table.save('mydata.lua',(metapost.getparameterset('test')))") ;
>    \stopMPpage
> \stoptext
> 
> but maybe i need abetter one .. a proper save/restore in the tuc file or so, but you can play with this first
> 
> Hans
> 
> 
> -----------------------------------------------------------------
>                                          Hans Hagen | PRAGMA ADE
>              Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
>       tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
> -----------------------------------------------------------------
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
> 
> maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
> archive  : https://bitbucket.org/phg/context-mirror/commits/
> wiki     : https://contextgarden.net
> ___________________________________________________________________________________


[-- Attachment #1.2: Type: text/html, Size: 7801 bytes --]

[-- Attachment #2: Type: text/plain, Size: 496 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Metafun : save memory content ?
  2022-07-18 16:31 ` Hans Hagen via ntg-context
  2022-07-18 17:53   ` Hans van der Meer via ntg-context
@ 2022-07-19  3:28   ` Fabrice L via ntg-context
  2022-07-19 11:42     ` Bruce Horrocks via ntg-context
  1 sibling, 1 reply; 8+ messages in thread
From: Fabrice L via ntg-context @ 2022-07-19  3:28 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Fabrice L


[-- Attachment #1.1: Type: text/plain, Size: 6368 bytes --]


> Le 18 juill. 2022 à 12:31, Hans Hagen via ntg-context <ntg-context@ntg.nl> a écrit :
> 
> On 7/18/2022 3:20 PM, Fabrice L via ntg-context wrote:
>> Dear list,
>> I’m doing animations in MetaFun, by simply using \startMPpage (…) \stopMPpage for each frame of the animation. Here is an example: https://youtu.be/yhxUbVQx9Uo (please note you can watch it in HD by adjusting the setting in YouTube).
>> This works usually great, but when the number of objects to draw by page is large, the resulting pdf file could be huge, several gigabytes sometimes. Such huge files are difficult (sometimes impossible) to convert in jpeg files (I’m using Image magick for the conversion), necessary to do a mp4 movie.
>> One solution is to do several pdf files, for example one by second; with this option, I can do 60 files of 30 pages, for an animation of 60 seconds (1800 frames for 1 minute). Everything is smoother this way, quicker to convert files. But in order to do so, I have to be able to describe each frame of the animation with the same set of instructions; so the position of an object at time t is described by an equation for example.
>> But this is not always possible. If the animation is based on a random process, the position of an object at time t depends on his position at time t-1. I can still use this preceeding strategy by simulating the past at the begging of each second, starting with a unique random seed. So for example if I work on second 9, I begin by calculating the past from frame 1 to frame 240 (8*30) and then simulate time 241, draw frame 241, simulate time 242, draw time 242, etc.. This could be complicated to do, but is feasible. The drawback is that more I advance in time, the more computations I have to do at the beginning of each file.
>> So to simplify things, one option would be to be save the state of some variables at some point in time, say t, in order to be able to access these informations at time t+1. So the strategy would be :
>> % File second1.pdf
>> for frame =1 to 30 :
>> 	simulate position of objects ;
>> 	draw the frame ;
>> endfor;
>> save the position of all objects ;
>> % File second2.pdf
>> Read (access) the position of all objects from second 1(after frame 30);
>> for frame =31 to 60 :
>> 	simulate position of objects ;
>> 	draw the frame ;
>> endfor;
>> save the position of all objects ;
>> Etc...
>> Does someone has an idea of how to do such a thing ? I can read text file in Context with no problem and integrate them in the animation process with \startMPinclusions (…) \stopMPinclusions, but I did not find a way to write information from MetaFun to a text file. Is Lua the solution ?
> there's always an idea ...

Great ! And thanks… 

Of course, it takes me some time to understand, and a little reading in the metafun xl manual,  but now I catch it ! 
> 
> \starttext
> \startMPpage
> runscript("metapost.setparameterset('test', table.load('mydata.lua') or { })") ;
> path b ; b := getparameterpath "test" "test_c";
> draw fullcircle scaled 3cm ;
> draw b scaled .4;
> % show(b);
> getparameters "test" [
> test_a = 123,
> test_b = (456,789),
> test_c = (boundingbox currentpicture)
> ] ;
> runscript("table.save('mydata.lua',(metapost.getparameterset('test')))") ;
> \stopMPpage
> \stoptext
> 
> but maybe i need abetter one .. a proper save/restore in the tuc file or so, but you can play with this first
> 
> Hans

If someones in the futur needs more explanation (like me!), here is my minimal working example derived from the one of Hans:
First page, we assign a value to the pair « ThePosition[2]  », write the value in a file (« TheMagicalFile.lua »), change the value in memory in the second page, and then read it from the external file in the third page. Of course, it works ! And this could run in separates process of course. 

% ————————————
\starttext
\startMPpage
pair ThePosition[];
ThePosition[1] := (0,0) ;
ThePosition[2] := (10,10) ;
draw ThePosition[1] -- ThePosition[2] ;
% save the parameters in the object MyData
getparameters "MyData" [
pos_a = ThePosition[1],
pos_b = ThePosition[2]
] ;
% Save this object in a file 
runscript("table.save('TheMagicalFile.lua',(metapost.getparameterset('MyData')))") ;
\stopMPpage

% We modify the value of ThePosition[2] 
\startMPpage
ThePosition[2] := (10,0) ;
draw ThePosition[1] -- ThePosition[2] ;
\stopMPpage

% We now read the value of ThePosition[2] from the file 
\startMPpage
% Save the data from the external file 
runscript("metapost.setparameterset('MyData', table.load('TheMagicalFile.lua') or { })") ;
ThePosition[2] := getparameter "MyData" "pos_b" ; 
draw ThePosition[1] -- ThePosition[2] ;
\stopMPpage

\stoptext
% ————————————

Complementary question : I saw that the « pos_a » « pos_b » of the example could not take numerical value, for example pos_1, pos_2.. How can I use metafun to write a macro to save the contents of hundreds of variable in this object, something like :

getparameters "MyData » [
	for i=1 upto 100 :
		pos_decimal(i) = ThePosition[i],
	endfor ;
] ;

If this is possible, then this is wonderful and open news doors ! 

Thanks again,
Fabrice.

> 
> 
> -----------------------------------------------------------------
> Hans Hagen | PRAGMA ADE
> Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
> tel: 038 477 53 69 | www.pragma-ade.nl <http://www.pragma-ade.nl/> | www.pragma-pod.nl <http://www.pragma-pod.nl/>
> -----------------------------------------------------------------
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
> 
> maillist : ntg-context@ntg.nl <mailto:ntg-context@ntg.nl> / https://www.ntg.nl/mailman/listinfo/ntg-context <https://www.ntg.nl/mailman/listinfo/ntg-context>
> webpage : https://www.pragma-ade.nl <https://www.pragma-ade.nl/> / http://context.aanhet.net <http://context.aanhet.net/>
> archive : https://bitbucket.org/phg/context-mirror/commits/ <https://bitbucket.org/phg/context-mirror/commits/>
> wiki : https://contextgarden.net <https://contextgarden.net/>
> ___________________________________________________________________________________


[-- Attachment #1.2: Type: text/html, Size: 37537 bytes --]

[-- Attachment #2: Type: text/plain, Size: 496 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Metafun : save memory content ?
  2022-07-19  3:28   ` Fabrice L via ntg-context
@ 2022-07-19 11:42     ` Bruce Horrocks via ntg-context
  2022-07-20  3:01       ` Fabrice L via ntg-context
  0 siblings, 1 reply; 8+ messages in thread
From: Bruce Horrocks via ntg-context @ 2022-07-19 11:42 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Bruce Horrocks



> On 19 Jul 2022, at 04:28, Fabrice L via ntg-context <ntg-context@ntg.nl> wrote:
> 
> Complementary question : I saw that the « pos_a » « pos_b » of the example could not take numerical value, for example pos_1, pos_2.. How can I use metafun to write a macro to save the contents of hundreds of variable in this object, something like :
> 
> getparameters "MyData » [
> 	for i=1 upto 100 :
> 		pos_decimal(i) = ThePosition[i],
> 	endfor ;
> ] ;
> 
> If this is possible, then this is wonderful and open news doors ! 

I've adapted this example from the metafun-p manual by adding some "write" statements.

\starttext
\startMPpage
      
numeric done[][], i, j, n ; n := 0 ;
forever :
	i := round(uniformdeviate(10)) ;
	j := round(uniformdeviate(10)) ;
	if unknown done[i][j] :
		drawdot
			(i*cm,j*cm)
			withpen pencircle scaled 0.5cm
			withcolor darkred;
		n := n + 1 ;
		done[i][j] := n ;
		
		write "done[" & tostring(i) & "][" & tostring(j) & "] := " & tostring(n) & " ;" to "mp_test_file.txt" ;

	fi ;
	exitif n = 10 ;
	
endfor ;

write EOF to "mp_test_file.txt" ;
   
\stopMPpage
\stoptext

This results in the expected graphic but also a local file (in the same directory as the source) called "mp_test_file.txt" that contains:

done[9][1] := 1 ;
done[4][3] := 2 ;
done[5][3] := 3 ;
done[5][1] := 4 ;
done[2][9] := 5 ;
done[8][6] := 6 ;
done[9][10] := 7 ;
done[9][7] := 8 ;
done[10][2] := 9 ;
done[6][5] := 10 ;

I think, with a bit of judicious use of "write" statements to add some ConTeXt / MP setup code, it would be possible to include the output from the previous run as MP source of the next run.

Hope this helps, or at least provides some food for thought.

(Note you need to delete the mp_test_file.txt before re-running else it fails.)

—
Bruce Horrocks
Hampshire, UK

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Metafun : save memory content ?
  2022-07-19 11:42     ` Bruce Horrocks via ntg-context
@ 2022-07-20  3:01       ` Fabrice L via ntg-context
  0 siblings, 0 replies; 8+ messages in thread
From: Fabrice L via ntg-context @ 2022-07-20  3:01 UTC (permalink / raw)
  To: Bruce Horrocks; +Cc: Fabrice L, mailing list for ConTeXt users

Dear Bruce, dear list, 

> Le 19 juill. 2022 à 07:42, Bruce Horrocks <ntg@scorecrow.com> a écrit :
> 
>> On 19 Jul 2022, at 04:28, Fabrice L via ntg-context <ntg-context@ntg.nl> wrote:
>> 
>> Complementary question : I saw that the « pos_a » « pos_b » of the example could not take numerical value, for example pos_1, pos_2.. How can I use metafun to write a macro to save the contents of hundreds of variable in this object, something like :
>> 
>> getparameters "MyData » [
>> 	for i=1 upto 100 :
>> 		pos_decimal(i) = ThePosition[i],
>> 	endfor ;
>> ] ;
>> 
>> If this is possible, then this is wonderful and open news doors ! 
> 
> I've adapted this example from the metafun-p manual by adding some "write" statements.

I have read the manuals, but not enough it seems ! I missed this part ! I completely miss the «write to » and « read from » instructions. Even a research on the web before posting did not return something interesting. I guess I did not used the good terms, because now, I find some ressources about these functions ! 
> 
> \starttext
> \startMPpage
> 
> numeric done[][], i, j, n ; n := 0 ;
> forever :
> 	i := round(uniformdeviate(10)) ;
> 	j := round(uniformdeviate(10)) ;
> 	if unknown done[i][j] :
> 		drawdot
> 			(i*cm,j*cm)
> 			withpen pencircle scaled 0.5cm
> 			withcolor darkred;
> 		n := n + 1 ;
> 		done[i][j] := n ;
> 		
> 		write "done[" & tostring(i) & "][" & tostring(j) & "] := " & tostring(n) & " ;" to "mp_test_file.txt" ;
> 
> 	fi ;
> 	exitif n = 10 ;
> 	
> endfor ;
> 
> write EOF to "mp_test_file.txt" ;
> 
> \stopMPpage
> \stoptext
> 
> This results in the expected graphic but also a local file (in the same directory as the source) called "mp_test_file.txt" that contains:
> 
> done[9][1] := 1 ;
> done[4][3] := 2 ;
> done[5][3] := 3 ;
> done[5][1] := 4 ;
> done[2][9] := 5 ;
> done[8][6] := 6 ;
> done[9][10] := 7 ;
> done[9][7] := 8 ;
> done[10][2] := 9 ;
> done[6][5] := 10 ;
> 
> I think, with a bit of judicious use of "write" statements to add some ConTeXt / MP setup code, it would be possible to include the output from the previous run as MP source of the next run.
> 
> Hope this helps, or at least provides some food for thought.

Totally, a lot of food  ! Here is my yesterday example with these functions : 

\starttext
\startMPpage
pair ThePosition[];
ThePosition[1] := (0,0) ;
ThePosition[2] := (10,10) ;
draw ThePosition[1] -- ThePosition[2] ;

% Save the position into a file "bruce.mp"
for i=1 upto 2:
  write "ThePosition[" & tostring(i) & "] := (" & tostring(xpart(ThePosition[i])) & ","& tostring(ypart(ThePosition[i])) &") ;" to "bruce.mp" ;
endfor;
write EOF to "bruce.mp" ;
\stopMPpage

% The external file would like like (comment added manually here):
% ThePosition[1] := (0,0) ;
% ThePosition[2] := (10,10) ;

% We modify the value of ThePosition[2] 
\startMPpage
ThePosition[2] := (10,0) ;
draw ThePosition[1] -- ThePosition[2] ;
\stopMPpage

% We now read the value of ThePosition[2] from the file 
\startMPpage
input "bruce.mp";
% Save the data from the external file 
draw ThePosition[1] -- ThePosition[2] ;

\stopMPpage

\stoptext

> 
> (Note you need to delete the mp_test_file.txt before re-running else it fails.)

So this totally do the trick : once again, magic is happening ! 
Note that in my installation, I do not need to delete the external file. 
> 
> —
> Bruce Horrocks
> Hampshire, UK

Thanks a lot ! 
Fabrice. 
> 

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-07-20  3:01 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-18 13:20 Metafun : save memory content ? Fabrice L via ntg-context
2022-07-18 14:39 ` Henning Hraban Ramm via ntg-context
2022-07-18 15:45   ` Hans Hagen via ntg-context
2022-07-18 16:31 ` Hans Hagen via ntg-context
2022-07-18 17:53   ` Hans van der Meer via ntg-context
2022-07-19  3:28   ` Fabrice L via ntg-context
2022-07-19 11:42     ` Bruce Horrocks via ntg-context
2022-07-20  3:01       ` Fabrice L via ntg-context

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).