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 21533 times

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

Tags:

User avatar
Stefan Kottwitz
Site Admin
Posts: 9445
Joined: Mon Mar 10, 2008 9:44 pm

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
Site admin

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

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

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

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

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

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

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

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

  1. \documentclass{article}
  2. \usepackage{tikz}
  3. \usetikzlibrary{calc,fadings,decorations.pathreplacing}
  4. \usepackage[active,tightpage]{preview}
  5. \PreviewEnvironment{tikzpicture}
  6. \setlength\PreviewBorder{5pt}
  7. \pagestyle{empty}
  8.  
  9. %% helper macros
  10. \newcommand\pgfmathsinandcos[3]{%
  11. \pgfmathsetmacro#1{sin(#3)}%
  12. \pgfmathsetmacro#2{cos(#3)}%
  13. }
  14. \newcommand\LongitudePlane[3][current plane]{%
  15. \pgfmathsinandcos\sinEl\cosEl{#2} % elevation
  16. \pgfmathsinandcos\sint\cost{#3} % azimuth
  17. \tikzset{#1/.style={cm={\cost,\sint*\sinEl,0,\cosEl,(0,0)}}}
  18. }
  19. \newcommand\LatitudePlane[3][current plane]{%
  20. \pgfmathsinandcos\sinEl\cosEl{#2} % elevation
  21. \pgfmathsinandcos\sint\cost{#3} % latitude
  22. \pgfmathsetmacro\yshift{\cosEl*\sint}
  23. \tikzset{#1/.style={cm={\cost,0,0,\cost*\sinEl,(0,\yshift)}}} %
  24. }
  25. \newcommand\DrawLongitudeCircle[2][1]{
  26. \LongitudePlane{\angEl}{#2}
  27. \tikzset{current plane/.prefix style={scale=#1}}
  28. % angle of "visibility"
  29. \pgfmathsetmacro\angVis{atan(sin(#2)*cos(\angEl)/sin(\angEl))} %
  30. \draw[current plane] (\angVis:1) arc (\angVis:\angVis+180:1);
  31. \draw[current plane,dashed] (\angVis-180:1) arc (\angVis-180:\angVis:1);
  32. }
  33. \newcommand\DrawLatitudeCircle[2][1]{
  34. \LatitudePlane{\angEl}{#2}
  35. \tikzset{current plane/.prefix style={scale=#1}}
  36. \pgfmathsetmacro\sinVis{sin(#2)/cos(#2)*sin(\angEl)/cos(\angEl)}
  37. % angle of "visibility"
  38. \pgfmathsetmacro\angVis{asin(min(1,max(\sinVis,-1)))}
  39. \draw[current plane] (\angVis:1) arc (\angVis:-\angVis-180:1);
  40. \draw[current plane,dashed] (180-\angVis:1) arc (180-\angVis:\angVis:1);
  41. }
  42.  
  43. %% document-wide tikz options and styles
  44. \tikzset{%
  45. >=latex, % option for nice arrows
  46. inner sep=0pt,%
  47. outer sep=2pt,%
  48. mark coordinate/.style={inner sep=0pt,outer sep=0pt,minimum size=3pt,fill=black,circle}%
  49. }
  50.  
  51. \begin{document}
  52.  
  53. \begin{tikzpicture} % CENT
  54.  
  55. %% some definitions
  56. \def\R{4} % sphere radius
  57. \def\angEl{35} % elevation angle
  58. \def\angAz{-105} % azimuth angle
  59. \def\angPhi{-40} % longitude of point P
  60. \def\angBeta{25} % latitude of point P
  61.  
  62. %% working planes
  63. \pgfmathsetmacro\H{\R*cos(\angEl)} % distance to north pole
  64. \tikzset{xyplane/.style={cm={cos(\angAz),sin(\angAz)*sin(\angEl),-sin(\angAz),cos(\angAz)*sin(\angEl),(0,-\H)}}}
  65. \LongitudePlane[xzplane]{\angEl}{\angAz}
  66. \LongitudePlane[pzplane]{\angEl}{\angPhi}
  67. \LongitudePlane[yzplane]{\angEl}{\angAz+90}
  68. \LatitudePlane[equator]{\angEl}{0}
  69.  
  70. %% draw xyplane and sphere
  71. \fill[ball color=white] (0,0) circle (\R); % 3D lighting effect
  72. \draw (0,0) circle (\R);
  73.  
  74. %% draw meridians and latitude circles
  75. \DrawLatitudeCircle[\R]{0} % equator
  76. %\DrawLatitudeCircle[\R]{\angBeta}
  77. \DrawLongitudeCircle[\R]{\angAz} % xzplane
  78. \DrawLongitudeCircle[\R]{\angAz+90} % yzplane
  79. \DrawLongitudeCircle[\R]{\angPhi} % pzplane
  80.  
  81. \end{tikzpicture}
  82. \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: 4044
Joined: Thu Nov 01, 2012 4:08 pm

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 4 guests