[NTG-context] Metafun : save memory content ?

Fabrice L fabrice.alpha at gmail.com
Tue Jul 19 05:28:17 CEST 2022

> Le 18 juill. 2022 à 12:31, Hans Hagen via ntg-context <ntg-context at 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. 

% ————————————
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')))") ;

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

% We now read the value of ThePosition[2] from the file 
% 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] ;

% ————————————

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,

> -----------------------------------------------------------------
> 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 at ntg.nl <mailto:ntg-context at 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/>
> ___________________________________________________________________________________

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ntg.nl/pipermail/ntg-context/attachments/20220718/2094747e/attachment-0001.htm>

More information about the ntg-context mailing list