%D \module
%D   [       file=m-asymptote,
%D        version=2017.03.24,
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=Asymptote,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% \enabletrackers[sandbox,buffers.run]

%D Currently asymptote assumes \MKII\ but better is to only support \MKIV. A problem
%D is that asymptote has this hard coded while for \CONTEXT\ it's better to stick to
%D something simple:
%D
%D \starttyping
%D \starttext
%D
%D \startasymptotepage
%D     some label
%D \stopasymptotepage
%D
%D \startasymptotepage
%D     some label
%D \stopasymptotepage
%D
%D \stoptext
%D \stoptyping
%D
%D Which is then run with:
%D
%D \starttyping
%D context --nonstopmode --purge somefile.tex
%D \stoptyping
%D
%D For the moment add "settings.prc=true;" to a graphic that is meant to produce that
%D format. See end of file for some examples.

\registerctxluafile{m-asymptote}{}

\unprotect

\unexpanded\def\asymptote
  {\dodoubleempty\module_asymptote}

\unexpanded\def\module_asymptote[#1][#2]%
  {\iffirstargument
     \begingroup
     \getdummyparameters[#2]%
     \edef\lasttypesetbuffer{\ctxlua{moduledata.asympote.process("asymptote:#1","\dummyparameter\c!type")}}%
     \externalfigure
       [\lasttypesetbuffer]%
       [\c!controls=\lasttypesetbuffer,#2]%
     \endgroup
   \fi}

\unexpanded\def\startasymptote[#1]%
  {\begingroup
   \dostartbuffer[asymptote:#1][startasymptote][stopasymptote]}

\unexpanded\def\stopasymptote
  {\endgroup}

\definefittingpage
  [asymptotepage]
  [\c!align=\v!normal]

\protect

\continueifinputfile{m-asymptote.mkiv}

\starttext

\startasymptote[demo-1]
settings.prc = true ;

import graph3 ;
import palette ;

size(6cm,6cm) ;
size3(5cm,0) ;

currentprojection = orthographic(3,-6,12) ;
currentlight = light(8,10,2) ;

real g(pair z) { return 1-z.x^2-z.y^2 ; }
real f(pair z) { return -2z.x+2 ; }

real x(real t) { return t ; }
real y(real t) { return 0 ; }
real z(real t) { return 1-t^2 ; }
real a(real t) { return 1 ; }
real b(real t) { return t ; }
real c(real t) { return -t^2 ; }

path3 p=graph(x,y,z,-2,2,operator ..) ;
path3 o=graph(a,b,c,-2,2,operator ..) ;

surface r = surface(f, (0,-1), (2,1), nx = 3, Spline) ;
surface s = surface(g, (-2,-2), (2,2), nx = 5, Spline) ;

path3 q = (-2,-2,-7) -- (2,-2,-7) -- (2,2,-7) -- (-2,2,-7) -- cycle ;

draw(q) ;

draw(p, blue+thick(), Arrow3) ;
draw(o, blue+thick(), Arrow3) ;

draw(s, lightgray + opacity(0.8), nolight, meshpen = black + thick()) ;
draw(r, lightgray + opacity(0.8), nolight, meshpen = black + thick()) ;

draw((1,0,0) -- (2,0,-2), black, Arrow3) ;
draw((1,0,0) -- (1,1,0), black, Arrow3) ;
\stopasymptote

\startasymptote[demo-2]
size(6cm,6cm) ;

fill((1cm,2cm) -- (3cm,3cm) -- (4cm,0cm) -- cycle);
\stopasymptote

% see end of grph-inc.mkiv for some more options:

\startluaparameterset[demo-1]
    toolbar = true,
    tree = false,
    view = 'ortho'
\stopluaparameterset

\asymptote[demo-1][type=prc,width=5cm,height=5cm,frame=on,display=yes,controls=yes,preview=yes]
\asymptote[demo-2][type=pdf,width=8cm,frame=on]

% \typebuffer[asymptote:demo-1]

% \startasymptotepage
%     test
% \stopasymptotepage

\stoptext
