LaTeX forum ⇒ Graphics, Figures & TablesSome PGF/TikZ 3D drawings

Information and discussion about graphics, figures & tables in LaTeX documents.
User avatar
T3.
Posts: 208
Joined: Fri Mar 14, 2008 12:58 pm

Some PGF/TikZ 3D drawings

Postby T3. » Thu Aug 07, 2008 3:33 pm

Some drawings inspired by the thread at comp.text.tex about how to convert some hand drawn pictures into programmatic 3D sketches. The sketches present stereographic and cylindrical map projections and they pose some interesting challenges for doing them with a 2D drawing package like PGF/TikZ.

The main idea is to draw in selected 3D planes and then project onto the canvas coordinate system with an appriopriate transformation. Some highlights:
  • parametrization of drawings
  • usage of pgf math engine for calculation of projection transformations and
    transitions points from visible (solid lines) to invisible (dashed lines) on
    meridians and latitude circles
  • definition of 3D plane transformation with expanded styles so that they are robust
    against redefinition of macros used in their construction
  • usage of named coordinates (nodes) for definition of characteristic points in
    local coordinate systems so that they are accessible outside of their plane of
    definition
  • calculation of intersections points with TikZ intersection coordinate system
  • usage of 'to' path operation instead of 'arc' for marking angles to allow for
    easy positioning of text labels on the curve
  • 3D lighting effects with shading

Enjoy!

Cheers,

Tomek

EDIT: uploaded an updated version that fixes some incompatibilities with the upcoming PGF version

EDIT 2: By courtesy of Kjell those examples are now also a part of his gallery.

globe-1.png
Stereographic projection
globe-1.png (70.4 KiB) Viewed 27874 times

globe-2.png
Cylindrical projection
globe-2.png (60.76 KiB) Viewed 27869 times
Attachments
globe.tex
LaTeX source code
(9.69 KiB) Downloaded 2382 times
Last edited by T3. on Fri Aug 08, 2008 5:36 pm, edited 3 times in total.

Recommended reading 2021:

LaTeXguide.org • LaTeX-Cookbook.net
LaTeX Beginner's Guide LaTeX Cookbook
User avatar
Stefan Kottwitz
Site Admin
Posts: 9989
Joined: Mon Mar 10, 2008 9:44 pm

Some PGF/TikZ 3D drawings

Postby Stefan Kottwitz » Thu Aug 07, 2008 4:08 pm

Hi Tomek,

that's great work! I hope it will appear among the examples of Kjells gallery.
I compiled it and noticed only a Overfull hbox warning, perhaps that could be fixed just to perfect it.
Btw. the Adobe Reader displayed it very well, the evince reader could not display the shading properly, just for info.

Stefan
LaTeX.org admin

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Some PGF/TikZ 3D drawings

Postby localghost » Thu Aug 07, 2008 4:29 pm

Thanks a bunch for this great contribution. There seems to be some kind of flame war in this topic on comp.text.tex - pgf/TikZ vs PSTricks. The first thought that came to me was that these drawings could be done with Sketch. I made that tool successfully running under Linux and Windows and already used it for less complex 3D drawings with pgf/TikZ. I hope there once will be a real 3D engine for this package.


Best regards
Thorsten¹
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

User avatar
T3.
Posts: 208
Joined: Fri Mar 14, 2008 12:58 pm

Some PGF/TikZ 3D drawings

Postby T3. » Thu Aug 07, 2008 4:31 pm

Stefan_K wrote:that's great work! I hope it will appear among the examples of Kjells gallery.
Thanks. And I have already sent it to Kjell. Now, I am competing for a case of beer with some other pstricks submissions ;)

Stefan_K wrote:I compiled it and noticed only a Overfull hbox warning, perhaps that could be fixed just to perfect it.
Btw. the Adobe Reader displayed it very well, the evince reader could not display the shading properly, just for info.
I don't know what's causing the warning. Perhaps one of the pictures is too large. Anyway, I've already spent way too much time on those drawings (but I've also learned a lot, so it was worth it).

About shadings: there is also a transparent shading used for one of the pictures but it's commented out in the source because it displayed only in Adobe Reader. There is some cool stuff you can do with TikZ.

BTW, is there a limit on the number of attachments? There are two more pictures in the source but I couldn't attach more then three files.

Cheers

Tomek

User avatar
T3.
Posts: 208
Joined: Fri Mar 14, 2008 12:58 pm

Some PGF/TikZ 3D drawings

Postby T3. » Thu Aug 07, 2008 5:04 pm

localghost wrote:Thanks a bunch for this contribution. There seems to be some kind of flame war in this topic on comp.text.tex - pgf/TikZ vs PSTricks.
That is quite polite exchange of opinions compared to some other threads I've seen on c.t.t. About PGF/TikZ vs. PSTricks - both packages are pretty powerful and have a similar set of features, so it largely comes down to a personal preference. PGF/TikZ is somewhat cleaner, IMO, have a native support for PDF and offers slightly better integration with TeX. So, that would be my recommendation to newcomers. PSTricks package, OTOH, packs more computing power since it can rely on PostScript engine for calculations and there are 3D extensions available for it.

localghost wrote:The first thought that came to me was that these drawings could be done with Sketch. I made that tool successfully running under Linux and Windows and already used it for less complex 3D drawings with pgf/TikZ. I hope there once will be a real 3D engine for this package.
I've looked at Sketch but I just didn't feel like learning yet another tool. It does look quite impressive but currently lacks 3D curves and 2nd degree solids, so it wouldn't be so useful for the above task. It supports only polygons, AFAIK, but has one neat feature that is lacking in tikz - the ability to define parts of the drawing for later usage and manipulation. Something like that would be a really nice addition to tikz. And I too hope for better 3D support in tikz.

Cheers,

Tomek

propell
Posts: 44
Joined: Fri May 30, 2008 11:16 am

Some PGF/TikZ 3D drawings

Postby propell » Fri Aug 08, 2008 11:38 am

Impressive work Tomek! Looking forward to add them to the gallery.

localghost wrote: PSTricks package, OTOH, packs more computing power since it can rely on PostScript engine for calculations ...


This will probably change when LuaTeX is released...

T3 wrote:I've looked at Sketch but I just didn't feel like learning yet another tool. It does look quite impressive but currently lacks 3D curves and 2nd degree solids, so it wouldn't be so useful for the above task. It supports only polygons, AFAIK, but has one neat feature that is lacking in tikz - the ability to define parts of the drawing for later usage and manipulation. Something like that would be a really nice addition to tikz. And I too hope for better 3D support in tikz.


Sketch is indeed a nice tool. I wrote an introduction to Sketch for tikz users some time ago. The lack of curves is a limitation though. The Asymptote graphic language has very good 3D support and is worth checking out. Unfortunately it lacks some of the tight integration with LaTeX that TikZ has.

If you don't want to learn the Sketch language you can "cheat" and use my Blender to Sketch exporter ;) . I have not used it much myself, but some users have created really stunning illustrations with it.

- Kjell Magne

User avatar
T3.
Posts: 208
Joined: Fri Mar 14, 2008 12:58 pm

Some PGF/TikZ 3D drawings

Postby T3. » Fri Aug 08, 2008 5:44 pm

propell wrote:Impressive work Tomek! Looking forward to add them to the gallery.
Thanks for all the positive feedback I've received so far¹. I think I should show some modesty (and honesty) here and say that a part of this appreciation (a large part) should be directed to PGF/TikZ developers for all the fine work they have done on this package. I think, that the high quality results that are achieved almost effortlessly with TikZ, can be attributed to a large extent to well thought out defaults and attention to details. Even powerful and expensive commercial packages often require much more sweat and tears from the user simply because they come with mediocre default behaviour.

propell wrote:
localghost wrote: PSTricks package, OTOH, packs more computing power since it can rely on PostScript engine for calculations ...


This will probably change when LuaTeX is released...
Yes, LuaTeX just can't get released soon enough, it seems. It will hopefully resolve a ton of other problems that are crippling TeX for a long time already.

Cheers,

Tomek

¹ I wasn't sure if I should show off like that in this forum in the first place.

User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Some PGF/TikZ 3D drawings

Postby gmedina » Sun Aug 10, 2008 4:29 am

T3. wrote:...
¹ I wasn't sure if I should show off like that in this forum in the first place.

Hi Tomek,

as far as I am concerned, you can show off as much as you like with beautiful contributions like those. Impressive!
1,1,2,3,5,8,13,21,34,55,89,144,233,...

startrucky
Posts: 11
Joined: Mon Aug 18, 2014 2:02 pm

Some PGF/TikZ 3D drawings

Postby startrucky » Mon Aug 18, 2014 2:26 pm

Hi there,
I am actually using this in a modified way which is working fine inwrite latex.
If though transfered to a latex document on my pc the latitude and longtitude circles dissappear and nor error is shown.
Has anybody ever encounter this kind of problem?
Kind regards
sty

\documentclass{article} 
\usepackage{tikz} 
\usetikzlibrary{calc,fadings,decorations.pathreplacing} 
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}
\pagestyle{empty} 

%% helper macros
\newcommand\pgfmathsinandcos[3]{% 
  \pgfmathsetmacro#1{sin(#3)}% 
  \pgfmathsetmacro#2{cos(#3)}% 
} 
\newcommand\LongitudePlane[3][current plane]{%
  \pgfmathsinandcos\sinEl\cosEl{#2} % elevation
  \pgfmathsinandcos\sint\cost{#3} % azimuth
  \tikzset{#1/.style={cm={\cost,\sint*\sinEl,0,\cosEl,(0,0)}}}
}
\newcommand\LatitudePlane[3][current plane]{%
  \pgfmathsinandcos\sinEl\cosEl{#2} % elevation
  \pgfmathsinandcos\sint\cost{#3} % latitude
  \pgfmathsetmacro\yshift{\cosEl*\sint}
  \tikzset{#1/.style={cm={\cost,0,0,\cost*\sinEl,(0,\yshift)}}} % 
}
\newcommand\DrawLongitudeCircle[2][1]{
  \LongitudePlane{\angEl}{#2}
  \tikzset{current plane/.prefix style={scale=#1}}
   % angle of "visibility" 
  \pgfmathsetmacro\angVis{atan(sin(#2)*cos(\angEl)/sin(\angEl))} %
  \draw[current plane] (\angVis:1) arc (\angVis:\angVis+180:1); 
  \draw[current plane,dashed] (\angVis-180:1) arc (\angVis-180:\angVis:1); 
}
\newcommand\DrawLatitudeCircle[2][1]{
  \LatitudePlane{\angEl}{#2}
  \tikzset{current plane/.prefix style={scale=#1}}
  \pgfmathsetmacro\sinVis{sin(#2)/cos(#2)*sin(\angEl)/cos(\angEl)} 
  % angle of "visibility"
  \pgfmathsetmacro\angVis{asin(min(1,max(\sinVis,-1)))}  
  \draw[current plane] (\angVis:1) arc (\angVis:-\angVis-180:1); 
  \draw[current plane,dashed] (180-\angVis:1) arc (180-\angVis:\angVis:1);
}

%% document-wide tikz options and styles
\tikzset{%
  >=latex, % option for nice arrows 
  inner sep=0pt,%
  outer sep=2pt,%
  mark coordinate/.style={inner sep=0pt,outer sep=0pt,minimum size=3pt,fill=black,circle}%
}

\begin{document} 

\begin{tikzpicture} % CENT

%% some definitions 
\def\R{4} % sphere radius
\def\angEl{35} % elevation angle 
\def\angAz{-105} % azimuth angle 
\def\angPhi{-40} % longitude of point P 
\def\angBeta{25} % latitude of point P 

%% working planes
\pgfmathsetmacro\H{\R*cos(\angEl)} % distance to north pole
\tikzset{xyplane/.style={cm={cos(\angAz),sin(\angAz)*sin(\angEl),-sin(\angAz),cos(\angAz)*sin(\angEl),(0,-\H)}}}
\LongitudePlane[xzplane]{\angEl}{\angAz}
\LongitudePlane[pzplane]{\angEl}{\angPhi}
\LongitudePlane[yzplane]{\angEl}{\angAz+90}
\LatitudePlane[equator]{\angEl}{0}

%% draw xyplane and sphere
\fill[ball color=white] (0,0) circle (\R); % 3D lighting effect
\draw (0,0) circle (\R);

%% draw meridians and latitude circles
\DrawLatitudeCircle[\R]{0} % equator
%\DrawLatitudeCircle[\R]{\angBeta}
\DrawLongitudeCircle[\R]{\angAz} % xzplane
\DrawLongitudeCircle[\R]{\angAz+90} % yzplane
\DrawLongitudeCircle[\R]{\angPhi} % pzplane

\end{tikzpicture} 
\end{document} 


The code is changed to the remaining problem of now disordered nodes
Last edited by startrucky on Mon Aug 18, 2014 2:47 pm, edited 3 times in total.

User avatar
Johannes_B
Site Moderator
Posts: 4183
Joined: Thu Nov 01, 2012 4:08 pm

Some PGF/TikZ 3D drawings

Postby Johannes_B » Mon Aug 18, 2014 2:34 pm

The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 7 guests