\defFunction in PStricks / pst-solides3d /

I am reading the pst-solides3d documentation and trying to understand the following codes that produce the attached image. It is hard to understand how the \defFunction works, and what are the meanings of syntax such as {t cos 3 mul} or {F} CourbeR2+. Where can I find these? PStricks docs?

I marked the answer below that addressed all these questions. One reply below mentioned that Asymptote might be a better choice for 3D vector graphics!

 \psset{unit=0.5}
\psset{lightsrc=viewpoint,viewpoint=50 60 25 rtp2xyz,Decran=50}
\begin{pspicture}(-9,-4)(4,8)
\defFunction{F}(t){t cos 3 mul}{t sin 3 mul}{}
\defFunction{G}(t){t cos}{t sin}{}
\psSolid[object=grille,base=-6 6 -4 4,action=draw]%
\psSolid[object=prisme,
h=8,fillcolor=yellow,
RotX=90,ngrid=8 18,
base=0 180 {F} CourbeR2+
180 0 {G} CourbeR2+](0,4,0)
\axesIIID(3,4,3)(8,6,7)
\end{pspicture}


How does \defFunction work?

In the form you use it, it takes 5 mandatory arguments that specify a parameterised version of a three-dimensional function. More specifically,

\defFunction{<name>}(<var>){<x>}{<y>}{<z>}


defines a function <name> that varies in the X direction according to <x>, in the Y direction according to <y> and in the Z direction according to <z>. All three functions can be defined in terms of a <var>iable. So,

\defFunction{F}(t){t cos 3 mul}{t sin 3 mul}{}


defines F(x(t), y(t)) where x(t) = 3 cos(t) and y(t) = 3 sin(t) (no z(t) is specified).

What are the meanings of syntax such as {t cos 3 mul} or {F} CourbeR2+?

The syntax is referred to as Reverse Polish Notation (RPN) where, in essence, the operators follow their operands. So, the algebraic use of 3 + 4 is rewritten as 3 4 + in RPN with the operator (+) following the operands (3 and 4). Some further explanation by means of examples. Here's an XKCD explanation:

"During the 1970s and 1980s, Hewlett-Packard used RPN in all of their desktop and hand-held calculators. In computer science, reverse Polish notation is used in stack-oriented programming languages such as Forth and PostScript." pstricks ordinarily uses PostScript \special commands, and one can even insert arbitrary PostScript code (see \psverb, for example), so its use of RPN comes naturally.

A more intuitive representation is possible via the optional [algebraic] method.

The option

base = 0 180 {F} CourbeR2+
180 0 {G} CourbeR2+


to \psSolid specifies the range of t for the function F as 0 (t-min) through 180 (t-max) (degrees), and that of G as 180 (t-min) through 0 (t-max), after which the function CourbeR2+ is called in both instances. In fact, CourbeR2+ takes 3 arguments and in RPN has the syntax

tmin tmax {X} CourbeR2+


The same with algebraic notation, which can be used with \defFunction[algebraic]:

\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-solides3d}
\begin{document}

\psset{unit=0.5,lightsrc=viewpoint,viewpoint=50 60 25 rtp2xyz,Decran=50}
\begin{pspicture}(-9,-4)(4,8)
\psSolid[object=grille,base=-6 6 -4 4,action=draw]%
\defFunction[algebraic]{F}(t){3*cos(t)}{3*sin(t)}{}
\defFunction[algebraic]{G}(t){cos(t)}{sin(t)}{}
\psSolid[object=prisme,h=8,fillcolor=yellow,RotX=90,ngrid=8 18,
base=0 Pi {F} CourbeR2+ Pi 0 {G} CourbeR2+](0,4,0)
\axesIIID(3,4,3)(8,6,7)
\end{pspicture}

\end{document}


\defFunction has one optional and 5 mandatory parameters: the name, the variable and the three definitions for x,y,z. CourbeR2+ is an internal PostScript function which runs a given function: 0 Pi {F} CourbeR2+ run F(t) with tmin=0 and tmax=Pi in a two dimensional system.

Here is a way to feed this with a more mundane notation. The \typeout is just to confirm that the original functions get reproduced. Whether or not "RPN" really stands for "reverse Polish notation", I don't know, I always thought this was a bad joke, but I am not sure (seems not to be a joke).

\documentclass{standalone}
\usepackage{pst-solides3d,infix-RPN}
\begin{document}
\psset{unit=0.5}
\psset{lightsrc=viewpoint,viewpoint=50 60 25 rtp2xyz,Decran=50}
\begin{pspicture}(-9,-4)(9,8)
\infixtoRPN{cos(t)*3}%
\edef\RPNone{\RPN}
\infixtoRPN{sin(t)*3}%
\edef\RPNtwo{\RPN}
\typeout{\RPNone,\RPNtwo}
\defFunction{F}(t){\RPNone}{\RPNtwo}{}
\defFunction{G}(t){t cos}{t sin}{}
\psSolid[object=grille,base=-6 6 -4 4,action=draw]%
\psSolid[object=prisme,
h=8,fillcolor=yellow,
RotX=90,ngrid=8 18,
base=0 180 {F} CourbeR2+
180 0 {G} CourbeR2+](0,4,0)
\axesIIID(3,4,3)(8,6,7)
\end{pspicture}
\end{document}


Output is as in your example, of course.

As for your question in the comments: one can either learn French or look those things up in the manual. I regret to tell you that, even though I used pstricks for almost 20 years and was truly happy with it, I almost completely forgot all these things because I switched to tikz. This is not an attempt to convert you but I'd like to ask you at the following code:

\documentclass[tikz,3.14mm]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{60}{150}
\begin{tikzpicture}[tdplot_main_coords]
\draw[thick,fill=yellow,fill opacity=0.6] (3,-3,0)
-- (1,-3,0)  plot[variable=\x,domain=0:180] ({1*cos(\x)},-3,{1*sin(\x)})
-- (-3,-3,0) plot[variable=\x,domain=180:0] ({3*cos(\x)},-3,{3*sin(\x)})
;
\draw[very thick,-latex] (0,0,0) -- (9,0,0) node[pos=1.1] {$x$};
\draw[very thick,-latex] (0,0,0) -- (0,6,0) node[pos=1.1] {$y$};
\draw[very thick,-latex] (0,0,0) -- (0,0,6) node[pos=1.1] {$z$};
\foreach \X in {-6,...,5}
{\foreach \Y in {-3,...,2}
{\draw[thick] (\X,\Y) -- (\X+1,\Y) -- (\X+1,\Y+1) -- (\X,\Y+1) -- cycle;}
}
\draw[thick,fill=yellow,fill opacity=0.6] (3,3,0)
-- (1,3,0)  plot[variable=\x,domain=0:180] ({1*cos(\x)},3,{1*sin(\x)})
-- (-3,3,0) plot[variable=\x,domain=180:0] ({3*cos(\x)},3,{3*sin(\x)})
;
\pgfmathsetmacro{\xmax}{180}
\draw[thick] (3,3,0) --
(3,-3,0) -- plot[variable=\x,domain=0:\xmax,smooth] ({3*cos(\x)},-3,{3*sin(\x)}) --
({3*cos(\xmax)},3,{3*sin(\xmax)}) --
plot[variable=\x,domain=\xmax:0,smooth] ({3*cos(\x)},3,{3*sin(\x)});
\pgfmathsetmacro{\xmax}{135}
\shade let \p1=($(3,3,0)-(3,-3,0)$),\n1={atan2(\y1,\x1)} in
(3,3,0) --
(3,-3,0) -- plot[variable=\x,domain=0:\xmax,smooth] ({3*cos(\x)},-3,{3*sin(\x)}) --
({3*cos(\xmax)},3,{3*sin(\xmax)}) --
plot[variable=\x,domain=\xmax:0,smooth] ({3*cos(\x)},3,{3*sin(\x)}) --cycle;
\draw[very thick,-latex] (0,0,3) -- (0,0,6);
\end{tikzpicture}
\end{document}


I bet your reaction will be: OMG, this looks so much more complicated. Superficially, yes, but the reason is that I simply did not use any predefined macros for which I either need to learn a new language or scroll through a manual, you just need some basic things and then start drawing. (I should also add that this example is not a good one since pstricks has a better 3d engine than TikZ, which has none, but if you want real rather than fake 3d, you may use asymptote anyway.) So again sorry for not being able to explain these things better, I forgot many things and also did not know many things when I was using pstricks.

• Thanks! I think this explains the calculations inside the function very well! my three questions now should be: 1. what values does the function {F} take? when written as 0 180 {F} CourbeR2+, does {F} take 180 or CourbeR2+ as its variable? 2. what values does the function {F} return? 3. what does CourbeR2+ mean? Commented Sep 26, 2018 at 21:43
• The package infix-RPN is only useful for macros which cannot handle the algebraic notation.
– user2478
Commented Sep 27, 2018 at 5:36
• thanks a lot for taking the time to provide such insights! I am in search of a vector graphic language that can handle 3D rendering, perspective views, material textures/reflections etc. I just searched Asymptote and found that it may be a great choice! thanks for pointing that out! Commented Sep 27, 2018 at 17:27
• @Shuodao You're welcome. I personally love to use the asypictureB package for that (because it allows you to "smuggle" some parameters into the asymptote picture) and recommend, in addition to the official documentation, this great tutorial. Both the tutorial and the asypictureB package are due to Charles Staats.
– user121799
Commented Sep 27, 2018 at 17:31

For its calculations, Postscript uses Reverse Polish Notation (RPN), aka postfix notation, which doesn't require parentheses.

t cos means cos(t) and t cos 3 mul multiplies the previous result by 3, i.e. 3cos(t).

Note the algebraic option of pst-plot is understood by pt-solides3d

• Thanks! I saw some of these in PS codes. I still don't know why ppl would design the language to replace 3*2 as 3 2 mul... it is so much more difficult to type! Commented Sep 26, 2018 at 21:42