~tex-sx/tex-sx/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
% \iffalse meta-comment
%<*internal>
\iffalse
%</internal>
%<*readme>
----------------------------------------------------------------
tikz3d --- More general 3d capabilities for TikZ
E-mail: stacey@math.ntnu.no
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------

This package extends the 3 dimensional capabilities of TikZ.
%</readme>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
----------------------------------------------------------------
tikz3d --- More general 3d capabilities for TikZ
E-mail: stacey@math.ntnu.no
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------

\endpreamble
\postamble

Copyright (C) 2011 by Andrew Stacey <stacey@math.ntnu.no>

This work may be distributed and/or modified under the
conditions of the LaTeX Project Public License (LPPL), either
version 1.3c of this license or (at your option) any later
version.  The latest version of this license is in the file:

http://www.latex-project.org/lppl.txt

This work is "maintained" (as per LPPL maintenance status) by
Andrew Stacey.

This work consists of the file  tikz3d.dtx
and the derived files           tikz3d.ins,
                                tikz3d.pdf, and
                                tikz3d.sty.

\endpostamble
\usedir{tex/latex/tikz3d}
\generate{
  \file{\jobname.sty}{\from{\jobname.dtx}{package}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/latex/tikz3d}
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\usedir{doc/latex/demopkg}
\generate{
  \file{README.txt}{\from{\jobname.dtx}{readme}}
}
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>
%<*package>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tikz3d}[2011/06/03 v1.0 TikZ with extended 3D]
%</package>
%<*driver>
\documentclass{ltxdoc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
%\usepackage{morefloats}
\usepackage{tikz}
\usepackage{\jobname}
\usepackage[numbered]{hypdoc}
\definecolor{lstbgcolor}{rgb}{0.9,0.9,0.9} 
 
\usepackage{listings}
\lstloadlanguages{[LaTeX]TeX}
\lstset{breakatwhitespace=true,breaklines=true,language=TeX}
 
\usepackage{fancyvrb}

\newenvironment{example}
  {\VerbatimEnvironment
   \begin{VerbatimOut}[gobble=2]{example.out}}
  {\end{VerbatimOut}
   \begin{center}
%   \setlength{\parindent}{0pt}
   \fbox{\begin{minipage}{.9\linewidth}
     \lstset{breakatwhitespace=true,breaklines=true,language=TeX,basicstyle=\small}
     \lstinputlisting[]{example.out}
   \end{minipage}}

   \fbox{\begin{minipage}{.9\linewidth}
     \input{example.out}
   \end{minipage}}
\end{center}
}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{1.0}{2011/05/03}{Converted to DTX file}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \providecommand*{\url}{\texttt}
% \GetFileInfo{tikz3d.dtx}
% \title{The \textsf{tikz3d} package}
% \author{Andrew Stacey \\ \url{stacey@math.ntnu.no}}
% \date{\fileversion~from \filedate}
%
%
% \maketitle
%
% 
% \section{Introduction}
%
% There are many programs which convert data representing a 3 dimensional object to a 2 dimensional picture.
% A crude, but straightforward, measure of the ``true 3 dimensionality'' of such a program is to find the point at which the 3 dimensional data is transformed down to 2 dimensions.
% This produces a scale ranging from programs such as \Verb+blender+ to \Verb+TikZ/PGF+.
% I place TikZ at the bottom end of this scale because it converts the 3 dimensional data to 2 dimensions at the time of input.
% It is therefore not possible to do anything ``fancy'' with the 3 dimensional data.
% This package is designed to push TikZ a little further up that scale.
%
% The right way to do this would be to redesign TikZ (and much of PGF) to internally use 3 dimensional coordinates at all times, saving the conversion to 2 dimensions to the last minute when a path is ``baked''.
% However, that would be a massive undertaking.
% So we do this the wrong way, but hopefully right enough to be useful.
% Thus rather than trying to push TikZ as far as possible up the ``3 dimensional'' scale, we simply try to correct some of the more obvious failings of TikZ when it comes to dealing with 3 dimensions.
% This is subjective (and possibly argumentative).
% My list is the following:
%
% \begin{enumerate}
% \item The possibilities for converting from 3 dimensions to 2 are severely limited: only linear projections are allowed.
% Thus a cube in TikZ can look a little ``pushed out'' in the corners.
%
% \begin{example}
% \begin{center}
% \begin{tikzpicture}[scale=2,line join=round,ultra thick]
% \draw[fill=red] (1,1,1) -- (1,1,-1) -- (1,-1,-1) -- (1,-1,1) -- cycle;
% \draw[fill=green] (-1,1,1) -- (1,1,1) -- (1,-1,1) -- (-1,-1,1) -- cycle;
% \draw[fill=blue] (1,1,-1) -- (-1,1,-1) -- (-1,1,1) -- (1,1,1) -- cycle;
% \draw (1,-1,.33333) -- (1,1,.33333) -- (-1,1,.33333);
% \draw (1,-1,-.33333) -- (1,1,-.33333) -- (-1,1,-.33333);
% \draw (1,.33333,-1) -- (1,.33333,1) -- (-1,.33333,1);
% \draw (1,-.33333,-1) -- (1,-.33333,1) -- (-1,-.33333,1);
% \draw (.33333,1,-1) -- (.33333,1,1) -- (.33333,-1,1);
% \draw (-.33333,1,-1) -- (-.33333,1,1) -- (-.33333,-1,1);
% \end{tikzpicture}
% \end{center}
% \end{example}
%
% \item The basic path shapes (rectangles, circles, arcs) do not, or not easily, take into account the third dimension even if it is specified.
%
% \begin{example}
% \begin{center}
% \begin{tikzpicture}
% \draw (0,0,0) rectangle (2,2,2);
% \draw (2,0,0) arc[radius=1,start angle=180,delta angle=-180];
% \end{tikzpicture}
% \end{center}
% \end{example}
%
% \end{enumerate}
%
% This package extends TikZ's 3 dimension syntax as follows:
%
% \begin{enumerate}
% \item It is possible to specify more general transformations from 3 dimensions to 2 dimensions.
%
% \begin{example}
% \begin{center}
% \begin{tikzpicture}[scale=2,line join=round,ultra thick,3d/perspective eye={5,5,10}]
% \draw[fill=red] (3d cs:1,1,1) -- (3d cs:1,1,-1) -- (3d cs:1,-1,-1) -- (3d cs:1,-1,1) -- cycle;
% \draw[fill=green] (3d cs:-1,1,1) -- (3d cs:1,1,1) -- (3d cs:1,-1,1) -- (3d cs:-1,-1,1) -- cycle;
% \draw[fill=blue] (3d cs:1,1,-1) -- (3d cs:-1,1,-1) -- (3d cs:-1,1,1) -- (3d cs:1,1,1) -- cycle;
% \draw (3d cs:1,-1,.33333) -- (3d cs:1,1,.33333) -- (3d cs:-1,1,.33333);
% \draw (3d cs:1,-1,-.33333) -- (3d cs:1,1,-.33333) -- (3d cs:-1,1,-.33333);
% \draw (3d cs:1,.33333,-1) -- (3d cs:1,.33333,1) -- (3d cs:-1,.33333,1);
% \draw (3d cs:1,-.33333,-1) -- (3d cs:1,-.33333,1) -- (3d cs:-1,-.33333,1);
% \draw (3d cs:.33333,1,-1) -- (3d cs:.33333,1,1) -- (3d cs:.33333,-1,1);
% \draw (3d cs:-.33333,1,-1) -- (3d cs:-.33333,1,1) -- (3d cs:-.33333,-1,1);
% \end{tikzpicture}
% \end{center}
% \end{example}
%
% \item The basic shapes are adapted to 3 dimensions.
%
% \begin{example}
% \begin{center}
% \begin{tikzpicture}
% \draw (3d cs:3,0,-3) circle[3d circle,radius=1,3d/circle/x axis={0,0,1}];
% \draw (3d cs:3,0,-3) -- (+3d cs:0,0,1);
% \draw (3d cs:3,0,-3) -- (+3d cs:0,1,0);
% \draw (3d cs:0,0,0) to[3d rectangle] (3d cs:1,1,1);
% \draw (3d cs:2,0,0) arc[3d arc,radius=1,start angle=0,delta angle=180,3d/circle/x axis={0,0,1},3d/circle/y axis={1,0,0}];
% \end{tikzpicture}
% \end{center}
% \end{example}
%
% \end{enumerate}
%
% \section{Usage}
%
% This package defines a new coordinate system \Verb+3d+.
% To specify a coordinate using this system, write it as, for example, \Verb+(3d cs:1,1,1)+.
% As with the standard TikZ \Verb+xyz+ coordinate system, this gets converted to a 2 dimensional coordinate.
% However, unlike the standard system, it is possible to change the transformation used to convert from 3 dimensions to 2 dimensions.
%
% Where it gets a little complicated is with relative coordinates and saved coordinates (via nodes).
% With a linear transformation (as in the standard TikZ transformation), there is no difference between specifying relative coordinates in 3 dimensions as in 2 dimensions.
% But to allow the possibility of non-linear transformations, the method for handling relative coordinates needs to be a little more complicated.
% The system has to remember the original 3 dimensional coordinates and not the 2 dimensional result of the transformation.
% To handle this, instead of using the standard TikZ system for relative coordinates, you have to use the syntaxes \Verb+(+3d cs:1,1,1)+ and \Verb+(++3d cs:1,1,1)+.
%
% When saving a coordinate via a node, the 3 dimensional coordinate is also saved.
% To access the saved coordinate, use the syntax \Verb+(3d n cs:node.anchor)+.
% The node shape itself is not transformed, but the 3 dimensional coordinate of its centre can be used as a 3 dimensional coordinate later.
%
% \section{Limitations}
%
% This package works on the coordinate level.
% Therefore it is not guaranteed that actual paths will be transformed correctly.
% The transformation of a straight line will still be a straight line, and similarly that of a bezier curve.
% There are two difficulties with fixing this limitation.
% The first is that knowing what a straight line should become depends on the transformation being used.
% The second is that the underlying engine only knows about straight lines and bezier curves, and so any replacement will have to be phrased in terms of one of them.
%
% There are plenty of other limitations, but they all follow from the basic one above.
% Even though this package preserves the illusion of 3-dimensionality a little longer than previously, once the path is laid out then it is a 2 dimensional object.
% For example, any decorations will not take into account the third dimension.
% Similarly, line thickness will not vary (though if simulated by a \Verb+fill+ this would work).
%
% \section{Transformations}
%
% At the moment, the only transformation that is available is that of projection from a point on to the \(x-y\)-plane.
% The point from which the scene is projected is set by the key \Verb+3d/perspective eye+.
%
% \section{Paths}
%
% Certain paths have been modified to take in to account the available information on the third dimension.
% Specifically, there are 3-dimensional variants of the rectangle, the circle, and the arc.
%
% \StopEventually{}
%
% \section{Implementation}
%
% \iffalse
%<*package>
% \fi
%
%    \begin{macrocode}

\long\def\ge@addto@macro#1#2{%
  \begingroup
  \toks@\expandafter\expandafter\expandafter{\expandafter#1#2}%
  \xdef#1{\the\toks@}%
  \endgroup}

\newif\ifthd@save@point
\thd@save@pointtrue

\newif\ifthd@save@this@point
\thd@save@this@pointtrue

\newdimen\thd@x
\newdimen\thd@y
\newdimen\thd@z
\newdimen\thd@thisx
\newdimen\thd@thisy
\newdimen\thd@thisz
\newdimen\thd@xa
\newdimen\thd@ya
\newdimen\thd@za
\newdimen\thd@xb
\newdimen\thd@yb
\newdimen\thd@zb
\newdimen\thd@xc
\newdimen\thd@yc
\newdimen\thd@zc
\newdimen\thd@lastxsaved
\newdimen\thd@lastysaved
\newdimen\thd@lastzsaved
\newdimen\thd@curvebx
\newdimen\thd@curveby
\newdimen\thd@curvebz

\tikzset{
  every node/.style={3d/coordinate/save node},
  3d rectangle/.style={%
    3d/rectangle
  },
  3d arc/.style={%
    3d/arc
  },
  3d circle/.style={%
    3d/circle
  },
  3d/.is family,
  3d,
  3d projection/.is choice,
  3d projection/project from point/.code={%
    \let\thd@to@twd=\thd@persp@proj
  },
  perspective eye/x/.initial={10cm},
  perspective eye/y/.initial={5cm},
  perspective eye/z/.initial={20cm},
  perspective eye/.code={
    \thd@split@xyz#1\relax
    \edef\thd@temp{\the\thd@x}%
    \pgfkeyslet{/tikz/3d/perspective eye/x}{\thd@temp}%
    \edef\thd@temp{\the\thd@y}%
    \pgfkeyslet{/tikz/3d/perspective eye/y}{\thd@temp}%
    \edef\thd@temp{\the\thd@z}%
    \pgfkeyslet{/tikz/3d/perspective eye/z}{\thd@temp}%
  },
  rectangle/direction/.initial={1,0,0},
  rectangle/.style={
    to path={%
      \pgfextra{\thd@rectangle\tikztotarget}%
      \thd@path@cmd
    },
    3d/rectangle/.cd
  },
  circle/x axis/.initial={1,0,0},
  circle/y axis/.initial={0,1,0},
  circle/.code={%
    \let\pgfsyssoftpath@curveto=\thd@circle@pathcurveto
    \let\pgfsyssoftpath@moveto=\thd@circle@pathmoveto
  },
  arc/.code={%
    \let\pgfsyssoftpath@curveto=\thd@arc@pathcurveto
    \let\tikz@@@arcfinal=\thd@@@arcfinal
  },
  coordinate/.is family,
  coordinate,
  x length/.initial={1cm},
  y length/.initial={1cm},
  z length/.initial={1cm},
  x/.code={%
    \tikz@checkunit{#1}%
    \iftikz@isdimension
     \pgfmathsetlength{\thd@x}{#1}%
    \else
     \pgfmathsetlength{\thd@x}{#1 * \pgfkeysvalueof{/tikz/3d/coordinate/x length}}%
    \fi
  },
  y/.code={%
    \tikz@checkunit{#1}%
    \iftikz@isdimension
     \pgfmathsetlength{\thd@y}{#1}%
    \else
     \pgfmathsetlength{\thd@y}{#1 * \pgfkeysvalueof{/tikz/3d/coordinate/y length}}%
    \fi
  },
  z/.code={%
    \tikz@checkunit{#1}%
    \iftikz@isdimension
     \pgfmathsetlength{\thd@z}{#1}%
    \else
     \pgfmathsetlength{\thd@z}{#1 * \pgfkeysvalueof{/tikz/3d/coordinate/z length}}%
    \fi
  },
  save node/.code={%
    \tikz@addoption{%
      \expandafter\global\expandafter\let\csname thd@node@\tikz@fig@name\endcsname\thd@this@node
    }
  },
  new/.code={%
    \def\thd@next@coord{x}%
  },
  .unknown/.code={%
    \tikz@checkunit{\pgfkeyscurrentname}%
    \iftikz@isdimension
      \expandafter\pgfmathsetlength\csname thd@\thd@next@coord\endcsname{\pgfkeyscurrentname}%
    \else
      \expandafter\pgfmathsetlength\csname thd@\thd@next@coord\endcsname{\pgfkeyscurrentname * \pgfkeysvalueof{/tikz/3d/coordinate/\thd@next@coord\space length}}%
    \fi
    \edef\thd@next@coord{\csname thd@next@coord@\thd@next@coord\endcsname}%
  },
}

\def\thd@next@coord@x{y}
\def\thd@next@coord@y{z}
\def\thd@next@coord@z{x}

\def\thd@split@xyz#1,#2,#3\relax{%
  \tikz@checkunit{#1}%
  \iftikz@isdimension
   \pgfmathsetlength{\thd@x}{#1}%
  \else
   \pgfmathsetlength{\thd@x}{#1 *     \pgfkeysvalueof{/tikz/3d/coordinate/x length}}%
  \fi
  \tikz@checkunit{#2}%
  \iftikz@isdimension
   \pgfmathsetlength{\thd@y}{#2}%
  \else
   \pgfmathsetlength{\thd@y}{#2 *     \pgfkeysvalueof{/tikz/3d/coordinate/y length}}%
  \fi
  \tikz@checkunit{#3}%
  \iftikz@isdimension
   \pgfmathsetlength{\thd@z}{#3}%
  \else
   \pgfmathsetlength{\thd@z}{#3 *     \pgfkeysvalueof{/tikz/3d/coordinate/z length}}%
  \fi
}

\newcommand{\thdset}[1]{%
  \tikzset{3d/.cd,#1}%
}

\def\thd@save@point{%
  \ifthd@save@point
  \global\thd@lastxsaved=\thd@x%
  \global\thd@lastysaved=\thd@y%
  \global\thd@lastzsaved=\thd@z%
  \fi
}

\def\thd@save@this@point{%
  \ifthd@save@this@point
  \global\thd@thisx=\thd@x%
  \global\thd@thisy=\thd@y%
  \global\thd@thisz=\thd@z%
  \fi
}

\def\thd@this@point{%
  \thd@x=\thd@thisx%
  \thd@y=\thd@thisy%
  \thd@z=\thd@thisz%
}

\def\thd@lastsaved@point{%
  \thd@x=\thd@lastxsaved%
  \thd@y=\thd@lastysaved%
  \thd@z=\thd@lastzsaved%
}

\def\thd@save@curveB{%
  \global\thd@curvebx=\thd@thisx%
  \global\thd@curveby=\thd@thisy%
  \global\thd@curvebz=\thd@thisz%
}

\def\thd@curveB{%
  \thd@x=\thd@curvebx%
  \thd@y=\thd@curveby%
  \thd@z=\thd@curvebz%
}

\def\thd@make@global{%
  \global\thd@x=\thd@x%
  \global\thd@y=\thd@y%
  \global\thd@z=\thd@z%
}

\def\thd@point@origin{%
  \thd@x=0pt%
  \thd@y=0pt%
  \thd@z=0pt%
}

\def\thd@add#1#2{%
  #1%
  \thd@xa=\thd@x
  \thd@ya=\thd@y
  \thd@za=\thd@z
  #2%
  \advance\thd@x by \thd@xa
  \advance\thd@y by \thd@ya
  \advance\thd@z by \thd@za
}

\def\thd@diff#1#2{%
  #2%
  \thd@xa=\thd@x
  \thd@ya=\thd@y
  \thd@za=\thd@z
  #1%
  \advance\thd@x by -\thd@xa
  \advance\thd@y by -\thd@ya
  \advance\thd@z by -\thd@za
}

\def\thd@scale#1#2{%
  #1%
  \thd@x=#2\thd@x
  \thd@y=#2\thd@y
  \thd@z=#2\thd@z
}

% Orthogonal projection of #2 on to #1
\def\thd@proj#1#2{%
  #1%
  \thd@xa=\thd@x
  \thd@ya=\thd@y
  \thd@za=\thd@z
  #2%
  \thd@xb=\thd@x
  \thd@yb=\thd@y
  \thd@zb=\thd@z
  \pgfmathsetlength{\pgfutil@tempdima}{(\thd@xa * \thd@xb + \thd@ya * \thd@yb + \thd@za * \thd@zb) / (\thd@xa^2 + \thd@ya^2 + \thd@za^2)}%
  \pgfmathsetlength{\thd@x}{\thd@xa * \pgfutil@tempdima}%
  \pgfmathsetlength{\thd@y}{\thd@ya * \pgfutil@tempdima}%
  \pgfmathsetlength{\thd@z}{\thd@za * \pgfutil@tempdima}%
}

\def\thd@pt#1#2#3{%
  \pgfmathsetlength{\thd@x}{#1}%
  \pgfmathsetlength{\thd@y}{#2}%
  \pgfmathsetlength{\thd@z}{#3}%
}

\def\thd@qpt#1#2#3{%
  \thd@x=#1\relax%
  \thd@y=#2\relax%
  \thd@z=#3\relax%
}

\def\thd@process#1{%
  \begingroup
  #1%
  \global\thd@x=\thd@x
  \global\thd@y=\thd@y
  \global\thd@z=\thd@z
  \endgroup
}

\def\thd@persp@proj{%
  \pgfmathsetmacro{\thd@t}{\pgfkeysvalueof{/tikz/3d/perspective eye/z}/(\pgfkeysvalueof{/tikz/3d/perspective eye/z} - \thd@z)}%
  \pgfmathsetlength{\pgf@x}{(1 - \thd@t) * \pgfkeysvalueof{/tikz/3d/perspective eye/x} + \thd@t * \thd@x}%
  \pgfmathsetlength{\pgf@y}{(1 - \thd@t) * \pgfkeysvalueof{/tikz/3d/perspective eye/y} + \thd@t * \thd@y}%
}  

\let\thd@to@twd=\thd@persp@proj

\tikzdeclarecoordinatesystem{3d}{%
  \thd@point@origin
  \thdset{coordinate,new,#1}%
  \thd@make@global
  \thd@to@twd
  \thd@save@point
  \thd@save@node
  \thd@save@this@point
  \global\let\tikz@curveB=\thd@orig@tikz@curveB
}

\tikzdeclarecoordinatesystem{+3d}{%
  \thd@point@origin
  \thdset{coordinate,new,#1}%
  \thd@save@this@point
  \thd@add{\thd@qpt{\thd@x}{\thd@y}{\thd@z}}{\thd@lastsaved@point}%
  \thd@make@global
  \thd@to@twd
  \thd@save@node
  \global\let\tikz@curveB=\thd@tikz@curveB
}

\tikzdeclarecoordinatesystem{++3d}{%
  \thd@point@origin
  \thdset{coordinate,new,#1}%
  \thd@save@this@point
  \thd@add{\thd@qpt{\thd@x}{\thd@y}{\thd@z}}{\thd@lastsaved@point}%
  \thd@make@global
  \thd@to@twd
  \thd@save@point
  \thd@save@node
  \global\let\tikz@curveB=\thd@tikz@curveB
}

\def\thd@nodename#1.#2\relax{#1}

\tikzdeclarecoordinatesystem{3d n}{%
  \thd@point@origin
  \pgfutil@in@.{#1}% Ok, flag this
  \ifpgfutil@in@
    \tikz@calc@anchor#1\tikz@stop%
    \pgf@xa=\pgf@x
    \pgf@ya=\pgf@y
    \expandafter\tikz@calc@anchor\thd@nodename#1\relax.center\tikz@stop%
    \advance\pgf@xa by -\pgf@x
    \advance\pgf@ya by -\pgf@y
    \csname thd@node@\thd@nodename#1\relax\endcsname
    \thd@to@twd
    \advance\pgf@x by \pgf@xa
    \advance\pgf@y by \pgf@ya
  \else%
    \csname thd@node@#1\endcsname
    \tikz@calc@anchor#1.center\tikz@stop%
    \expandafter\ifx\csname pgf@sh@ns@#1\endcsname\tikz@coordinate@text%
    \else
      \tikz@shapebordertrue%
      \def\tikz@shapeborder@name{#1}%
    \fi%
    \thd@to@twd
  \fi%
  \thd@make@global
  \thd@save@point
  \thd@save@this@point
  \thd@save@node
}

\def\thd@save@node{%
  \xdef\thd@this@node{\noexpand\thd@qpt{\the\thd@x}{\the\thd@y}{\the\thd@z}}%
}

\let\thd@orig@tikz@curveC=\tikz@curveC

\def\thd@tikz@curveC#1{%
  \thd@add{\thd@curveB}{\thd@lastsaved@point}%
  \thd@to@twd
  \xdef\tikz@curve@second{\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
  \global\let\tikz@curveC=\thd@orig@tikz@curveC
  \tikz@curveC{#1}%
}

\let\thd@orig@tikz@curveB=\tikz@curveB

\def\thd@tikz@curveB#1{%
  \global\let\tikz@curveC=\thd@tikz@curveC
  \global\let\tikz@curveB=\thd@orig@tikz@curveB
  \thd@save@curveB
  \tikz@curveB{#1}%
}

\def\thd@rectangle#1{%
  \let\thd@path@cmd=\pgfutil@empty
  \thd@xc=\thd@x
  \thd@yc=\thd@y
  \thd@zc=\thd@z
  \thd@save@pointfalse
  \expandafter\tikz@scan@one@point\expandafter\pgfutil@gobble\expandafter(#1)\relax%
  \edef\thd@marshal{%
    \noexpand\thd@diff{\noexpand\thd@qpt{\the\thd@x}{\the\thd@y}{\the\thd@z}}{\noexpand\thd@qpt{\the\thd@xc}{\the\thd@yc}{\the\thd@zc}}}%
  \thd@process{\thd@marshal}%
  \thd@xa=\thd@x
  \thd@ya=\thd@y
  \thd@za=\thd@z
  \edef\thd@rect@dir{(3d cs:\pgfkeysvalueof{/tikz/3d/rectangle/direction})}
  \expandafter\tikz@scan@one@point\expandafter\pgfutil@firstofone\thd@rect@dir\relax
  \edef\thd@marshal{%
    \noexpand\thd@proj{\noexpand\thd@qpt{\the\thd@x}{\the\thd@y}{\the\thd@z}}{\noexpand\thd@qpt{\the\thd@xa}{\the\thd@ya}{\the\thd@za}}}%
  \thd@process{\thd@marshal}%
  \thd@xb=\thd@x
  \thd@yb=\thd@y
  \thd@zb=\thd@z
  \edef\thd@temp{ (+3d cs:0pt,0pt,0pt) -- (+3d cs:\the\thd@x,\the\thd@y,\the\thd@z) -- (+3d cs:\the\thd@xa,\the\thd@ya,\the\thd@za)}%
  \ge@addto@macro\thd@path@cmd\thd@temp
  \edef\thd@marshal{%
    \noexpand\thd@diff{\noexpand\thd@qpt{\the\thd@xa}{\the\thd@ya}{\the\thd@za}}{\noexpand\thd@qpt{\the\thd@xb}{\the\thd@yb}{\the\thd@zb}}}%
  \thd@process{\thd@marshal}%
  \edef\thd@temp{ -- (+3d cs:\the\thd@x,\the\thd@y,\the\thd@z) -- cycle (\tikztotarget)}%
  \ge@addto@macro\thd@path@cmd\thd@temp
  \thd@save@pointtrue
}

% Drop in for \Verb+\pgfsyssoftpath@curveto+ in \Verb+\pgf@arc+
% Interpret the coordinates in an x-y-z system
\let\thd@orig@pgfsyssoftpath@curveto=\pgfsyssoftpath@curveto
\let\thd@orig@pgfsyssoftpath@moveto=\pgfsyssoftpath@moveto
\def\thd@arc@pathcurveto#1#2#3#4#5#6{%
  \begingroup
  \thd@this@point
  \thd@xc=\thd@x
  \thd@yc=\thd@y
  \thd@zc=\thd@z
  \edef\thd@rect@dir{(3d cs:\pgfkeysvalueof{/tikz/3d/circle/x axis})}
  \expandafter\tikz@scan@one@point\expandafter\pgfutil@firstofone\thd@rect@dir\relax
  \thd@xa=\thd@x
  \thd@ya=\thd@y
  \thd@za=\thd@z
  \edef\thd@rect@dir{(3d cs:\pgfkeysvalueof{/tikz/3d/circle/y axis})}
  \expandafter\tikz@scan@one@point\expandafter\pgfutil@firstofone\thd@rect@dir\relax
  \thd@xb=\thd@x
  \thd@yb=\thd@y
  \thd@zb=\thd@z
  \pgfmathsetlength{\thd@x}{\thd@xc + (#1 - \pgf@path@lastx)/1cm * \thd@xa + (#2 - \pgf@path@lasty)/1cm * \thd@xb}
  \pgfmathsetlength{\thd@y}{\thd@yc + (#1 - \pgf@path@lastx)/1cm * \thd@ya + (#2 - \pgf@path@lasty)/1cm * \thd@yb}
  \pgfmathsetlength{\thd@z}{\thd@zc + (#1 - \pgf@path@lastx)/1cm * \thd@za + (#2 - \pgf@path@lasty)/1cm * \thd@zb}
  \thd@to@twd
% I don't understand why, but the transformation here is scaled
% up by 1cm
  \pgfmathsetlength{\pgf@x}{\pgf@x/1cm}%
  \pgfmathsetlength{\pgf@y}{\pgf@y/1cm}%
  \pgfpointtransformed{\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  \pgfmathsetlength{\thd@x}{\thd@xc + (#3 - \pgf@path@lastx)/1cm * \thd@xa + (#4 - \pgf@path@lasty)/1cm * \thd@xb}
  \pgfmathsetlength{\thd@y}{\thd@yc + (#3 - \pgf@path@lastx)/1cm * \thd@ya + (#4 - \pgf@path@lasty)/1cm * \thd@yb}
  \pgfmathsetlength{\thd@z}{\thd@zc + (#3 - \pgf@path@lastx)/1cm * \thd@za + (#4 - \pgf@path@lasty)/1cm * \thd@zb}
  \thd@to@twd
% I don't understand why, but the transformation here is scaled
% up by 1cm
  \pgfmathsetlength{\pgf@x}{\pgf@x/1cm}%
  \pgfmathsetlength{\pgf@y}{\pgf@y/1cm}%
  \pgfpointtransformed{\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
  \pgf@xc=\pgf@x
  \pgf@yc=\pgf@y
  \pgfmathsetlength{\thd@x}{\thd@xc + (#5 - \pgf@path@lastx)/1cm * \thd@xa + (#6 - \pgf@path@lasty)/1cm * \thd@xb}
  \pgfmathsetlength{\thd@y}{\thd@yc + (#5 - \pgf@path@lastx)/1cm * \thd@ya + (#6 - \pgf@path@lasty)/1cm * \thd@yb}
  \pgfmathsetlength{\thd@z}{\thd@zc + (#5 - \pgf@path@lastx)/1cm * \thd@za + (#6 - \pgf@path@lasty)/1cm * \thd@zb}
  \thd@save@point
  \thd@save@this@point
  \thd@to@twd
% I don't understand why, but the transformation here is scaled
% up by 1cm
  \pgfmathsetlength{\pgf@x}{\pgf@x/1cm}%
  \pgfmathsetlength{\pgf@y}{\pgf@y/1cm}%
  \pgfpointtransformed{\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
  \pgf@xb=\pgf@x
  \pgf@yb=\pgf@y
  \thd@orig@pgfsyssoftpath@curveto{\the\pgf@xa}{\the\pgf@ya}{\the\pgf@xc}{\the\pgf@yc}{\the\pgf@xb}{\the\pgf@yb}%
  \endgroup
}

\def\thd@circle@pathmoveto#1#2{%
  \begingroup
  \thd@save@pointfalse
  \thd@save@this@pointfalse
  \edef\thd@temp{%
    \noexpand\pgf@xa=#1\relax
    \noexpand\pgf@ya=#2\relax
  }%
  \thd@temp
  \thd@this@point% Corresponds to the centre of the circle in 3D-space
  \thd@xc=\thd@x
  \thd@yc=\thd@y
  \thd@zc=\thd@z
  \thd@to@twd
  \pgf@xb=\pgf@x
  \pgf@yb=\pgf@y
  \edef\thd@rect@dir{(3d cs:\pgfkeysvalueof{/tikz/3d/circle/x axis})}
  \expandafter\tikz@scan@one@point\expandafter\pgfutil@firstofone\thd@rect@dir\relax
  \thd@xa=\thd@x
  \thd@ya=\thd@y
  \thd@za=\thd@z
  \edef\thd@rect@dir{(3d cs:\pgfkeysvalueof{/tikz/3d/circle/y axis})}
  \expandafter\tikz@scan@one@point\expandafter\pgfutil@firstofone\thd@rect@dir\relax
  \thd@xb=\thd@x
  \thd@yb=\thd@y
  \thd@zb=\thd@z
  \pgfmathsetlength{\thd@x}{\thd@xc + (\pgf@xa - \pgf@xc)/1cm * \thd@xa + (\pgf@ya - \pgf@yc)/1cm * \thd@xb}
  \pgfmathsetlength{\thd@y}{\thd@yc + (\pgf@xa - \pgf@xc)/1cm * \thd@ya + (\pgf@ya - \pgf@yc)/1cm * \thd@yb}
  \pgfmathsetlength{\thd@z}{\thd@zc + (\pgf@xa - \pgf@xc)/1cm * \thd@za + (\pgf@ya - \pgf@yc)/1cm * \thd@zb}
  \thd@to@twd
% I don't understand why, but the transformation here is not scaled
% up by 1cm
  \pgfpointtransformed{\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
  \advance\pgf@x by -\pgf@xb
  \advance\pgf@y by -\pgf@yb
  \thd@orig@pgfsyssoftpath@moveto{\the\pgf@x}{\the\pgf@y}%
  \endgroup
}

\def\thd@circle@pathcurveto#1#2#3#4#5#6{%
  \begingroup
  \thd@save@pointfalse
  \thd@save@this@pointfalse
  \edef\thd@temp{%
    \noexpand\pgf@xa=#1\relax
    \noexpand\pgf@ya=#2\relax
    \noexpand\pgf@xc=#3\relax
    \noexpand\pgf@yc=#4\relax
    \noexpand\pgf@xb=#5\relax
    \noexpand\pgf@yb=#6\relax
    \noexpand\pgfutil@tempdima=\the\pgf@xc\relax
    \noexpand\pgfutil@tempdimb=\the\pgf@yc\relax
  }%
  \thd@temp
  \thd@this@point% Centre of the circle in 3D-space
  \thd@xc=\thd@x\relax
  \thd@yc=\thd@y\relax
  \thd@zc=\thd@z\relax
  \edef\thd@rect@dir{(3d cs:\pgfkeysvalueof{/tikz/3d/circle/x axis})}
  \expandafter\tikz@scan@one@point\expandafter\pgfutil@firstofone\thd@rect@dir\relax
  \thd@xa=\thd@x
  \thd@ya=\thd@y
  \thd@za=\thd@z
  \edef\thd@rect@dir{(3d cs:\pgfkeysvalueof{/tikz/3d/circle/y axis})}
  \expandafter\tikz@scan@one@point\expandafter\pgfutil@firstofone\thd@rect@dir\relax
  \thd@xb=\thd@x
  \thd@yb=\thd@y
  \thd@zb=\thd@z
  \pgfmathsetlength{\thd@x}{\thd@xc + (\pgf@xa - \pgfutil@tempdima)/1cm * \thd@xa + (\pgf@ya - \pgfutil@tempdimb)/1cm * \thd@xb}
  \pgfmathsetlength{\thd@y}{\thd@yc + (\pgf@xa - \pgfutil@tempdima)/1cm * \thd@ya + (\pgf@ya - \pgfutil@tempdimb)/1cm * \thd@yb}
  \pgfmathsetlength{\thd@z}{\thd@zc + (\pgf@xa - \pgfutil@tempdima)/1cm * \thd@za + (\pgf@ya - \pgfutil@tempdimb)/1cm * \thd@zb}
  \thd@to@twd
% I don't understand why, but the transformation here is not scaled
% up by 1cm
  \pgfpointtransformed{\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  \thd@this@point% Centre of the circle in 3D-space
  \thd@to@twd
  \advance\pgf@xa by -\pgf@x
  \advance\pgf@ya by -\pgf@y
  \pgfmathsetlength{\thd@x}{\thd@xc + (\pgf@xc - \pgfutil@tempdima)/1cm * \thd@xa + (\pgf@yc - \pgfutil@tempdimb)/1cm * \thd@xb}
  \pgfmathsetlength{\thd@y}{\thd@yc + (\pgf@xc - \pgfutil@tempdima)/1cm * \thd@ya + (\pgf@yc - \pgfutil@tempdimb)/1cm * \thd@yb}
  \pgfmathsetlength{\thd@z}{\thd@zc + (\pgf@xc - \pgfutil@tempdima)/1cm * \thd@za + (\pgf@yc - \pgfutil@tempdimb)/1cm * \thd@zb}
  \thd@to@twd
% I don't understand why, but the transformation here is not scaled
% up by 1cm
  \pgfpointtransformed{\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
  \pgf@xc=\pgf@x
  \pgf@yc=\pgf@y
  \thd@this@point% Centre of the circle in 3D-space
  \thd@to@twd
  \advance\pgf@xc by -\pgf@x
  \advance\pgf@yc by -\pgf@y
  \pgfmathsetlength{\thd@x}{\thd@xc + (\pgf@xb - \pgfutil@tempdima)/1cm * \thd@xa + (\pgf@yb - \pgfutil@tempdimb)/1cm * \thd@xb}
  \pgfmathsetlength{\thd@y}{\thd@yc + (\pgf@xb - \pgfutil@tempdima)/1cm * \thd@ya + (\pgf@yb - \pgfutil@tempdimb)/1cm * \thd@yb}
  \pgfmathsetlength{\thd@z}{\thd@zc + (\pgf@xb - \pgfutil@tempdima)/1cm * \thd@za + (\pgf@yb - \pgfutil@tempdimb)/1cm * \thd@zb}
  \thd@to@twd
% I don't understand why, but the transformation here is not scaled
% up by 1cm
  \pgfpointtransformed{\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
  \pgf@xb=\pgf@x
  \pgf@yb=\pgf@y
  \thd@this@point% Centre of the circle in 3D-space
  \thd@to@twd
  \advance\pgf@xb by -\pgf@x
  \advance\pgf@yb by -\pgf@y
  \edef\thd@temp{%
    \noexpand\thd@orig@pgfsyssoftpath@curveto{\the\pgf@xa}{\the\pgf@ya}{\the\pgf@xc}{\the\pgf@yc}{\the\pgf@xb}{\the\pgf@yb}}%
  \thd@temp
  \endgroup
}

\def\thd@@@arcfinal#1#2#3{%
  #1%
  \xdef\tikz@arc@save@first{\pgfqpoint{\the\tikz@lastx}{\the\tikz@lasty}}%
  \thd@lastsaved@point%
  \thd@to@twd%
  \xdef\tikz@arc@save@second{\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
}

%    \end{macrocode}
% \iffalse
%</package>
% \fi
%
% \Finale

\endinput