9

Is there any way to import a 3D file of an object (.stl for example) to draw a 2D projection of it into a TeX document ?

I have been surprised to don't find any subject about that, I was thinking a conversion with TikZ or Asymptote would be possible.

Any clue ?

2
  • 4
    I guess it is possible, see here (source here) for an example with Asymptote. The data file is a *.obj.
    – anderstood
    Commented Aug 6, 2014 at 21:51
  • Note that you can also convert *.svg drawings to tikz using inkscape, but you couldn't change of viewpoint.
    – anderstood
    Commented Aug 6, 2014 at 21:58

2 Answers 2

9

pgfplots supports it as part of its patch plot handler. It has two different choices as input syntax:

\begin{tikzpicture}
\begin{axis}
% FokkerDrI_layer_0.facetIdx.dat contains:
% # each row makes up one facet; it
% # consists of 0-based indices into
% # the vertex array
% 0    1     2 % triangle of vertices #0,#1 and #2
% 0    3     1 % triangle of vertices #0,#3 and #1
% 3    4     1
% 5    6     7
% 6    8     7
% 8    9     7
% 8    10      9
% ...
% while FokkerDrI_layer_0.vertices.dat contains
% 105.577      -19.7332   2.85249    % vertex #0
% 88.9233      -21.1254   13.0359    % vertex #1
% 89.2104      -22.1547   1.46467    % vertex #2
% 105.577      -17.2161   12.146
% 105.577      -10.6054   18.7567
% 105.577      7.98161   18.7567
% 105.577      14.5923   12.146
% ...
\addplot3[patch,shader=interp,
    patch table=
         {plotdata/FokkerDrI_layer_0.facetIdx.dat}]
    file
    {plotdata/FokkerDrI_layer_0.vertices.dat};
\end{axis}
\end{tikzpicture}

or

\begin{tikzpicture}
\begin{axis}[axis equal]
% FokkerDrI_layer_0.patches.dat contains:
% # each row is one vertex; three consecutive
% # vertices make one triangle (patch)
% 105.577    -19.7332    2.85249
% 88.9233    -21.1254    13.0359
% 89.2104    -22.1547    1.46467
% # end of facet 0
% 105.577    -19.7332    2.85249
% 105.577    -17.2161    12.146
% 88.9233    -21.1254    13.0359
% # end of facet 1
\addplot3[patch]
    file
    {plotdata/FokkerDrI_layer_0.patches.dat};
\end{axis}
\end{tikzpicture}

enter image description here

The model as such is one of several .3ds files of my own archive (I wrote a C++ program which converted the .3ds files into the format mentioned above).

The example is taken from the pgfplots manual (compare http://pgfplots.sourceforge.net/pgfplots.pdf section "Patch plots"). The data files are shipped with pgfplots.

pgfplots supports colormaps or patch plots with explicit color. Texture mapping or lighting is out of scope.

0
10

For example with pst-solides3d. If your data defines a function or simple 3d points you can plot the points with pst-plot3d. If the data describes a 3d solid then use it this way. Run it with xelatex:

\documentclass{minimal}
\usepackage[svgnames]{pstricks}
\usepackage{pst-solides3d}
\begin{document}

\psset{unit=0.4}
\begin{pspicture}(-4,-8)(6,6)
 \psset{lightsrc=30 -40 10,viewpoint=50 -50 20 rtp2xyz,Decran=50,
   RotX=90,sommets=(sommets_nefer.dat) run}
 \psSolid[object=new,fillcolor=AntiqueWhite,linewidth=0.5\pslinewidth,faces=(faces_nefer.dat) run]
\end{pspicture}

\end{document}

enter image description here

The datafiles are sommets_nefer.dat which defines the coordinates of the polygons and face_nefer.dat defines the area. Every coordinate triple is numbered with 0,1,2,... These numbers are used for the polygons. For example:

\psset{unit=0.75cm,lightsrc=10 -20 50,viewpoint=50 -20 30 rtp2xyz,Decran=50}
\begin{pspicture}(-5.5,-2)(6,6)
\psSolid[object=new,fillcolor=red!50,incolor=yellow,action=draw,
 sommets= 2 4 3  -2 4 3  -2 -4 3  2 -4 3  2 4 0 
         -2 4 0  -2 -4 0  2 -4 0  0 4 5   0 -4 5,
 faces={ [0 1 2 3][7 6 5 4][0 3 7 4][3 9 2][1 8 0] 
         [8 9 3 0][9 8 1 2][6 7 3 2][2 1 5 6]},
 num=all,show=all]
\end{pspicture}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .