Debian Codegraph @

texmf-dist/tex/latex/tikz-3dplot/tikz-3dplot.sty

raw
1 %% tikz-3dplot.sty
2 %% Copyright 2010 Jeffrey D. Hein
3 %
4 % This work may be distributed and/or modified under the
5 % conditions of the LaTeX Project Public License, either version 1.3
6 % of this license or (at your option) any later version.
7 % The latest version of this license is in
8 % http://www.latex-project.org/lppl.txt
9 % and version 1.3 or later is part of all distributions of LaTeX
10 % version 2005/12/01 or later.
11 %
12 % This work has the LPPL maintenance status `maintained'.
13 %
14 % The Current Maintainer of this work is Jeffrey D. Hein.
15 %
16 % This work consists of the files 3dplot.sty and 3dplot_documentation.tex
17
18 %Description
19 %-----------
20
21 %tikz-3dplot.sty - package for plotting three dimensional axes and coordinates in TikZ. The user can specify orientation of the display, and also define rotated coordinate systems within the 3d display coordinate system.
22
23 %Created 2009-11-07 by Jeff Hein. Last updated: 2010-07-30
24 %----------------------------------------------------------
25
26 %Requirements
27 %------------
28 %This requires the tikz package.
29
30 %Update Notes
31 %------------
32
33 %see the documentation for update notes
34
35
36 \RequirePackage{pgf}
37 \RequirePackage{ifthen}
38
39 %Style definitions and includes
40 %------------------------------
41 \usetikzlibrary{calc,3d,arrows}
42
43 \tikzset{tdplot_screen_coords/.style={x={(1 cm,0 cm)},y={(0 cm, 1 cm)},z={(-1 cm, -1 cm)}}}%
44
45 \pgfmathsetmacro{\tdplotlowerphi}{0}
46 \pgfmathsetmacro{\tdplotupperphi}{360}
47 \pgfmathsetmacro{\tdplotlowertheta}{0}
48 \pgfmathsetmacro{\tdplotuppertheta}{180}
49 %\pgfmathsetmacro{\tdplotlinewidth}{.25pt}
50
51 %Commands
52 %--------
53
54 %\tdplotsinandcos{sin}{cos}{theta}
55 %determines the sin and cos of the specified angle (in degrees).
56 %#1: returns sin(#3)
57 %#2: returns cos(#3)
58 %#3: user-specified angle
59 \newcommand{\tdplotsinandcos}[3]{%
60 \pgfmathsetmacro{#1}{sin(#3)}%
61 \pgfmathsetmacro{#2}{cos(#3)}%
62 }
63
64 %\tdplotmult{result}{multiplicand}{multiplicator}
65 %determines the multiplication of specified values.
66 %#1: returns #2*#3
67 %#2: user-specified multiplicand
68 %#3: user-specified multiplicator
69 \newcommand{\tdplotmult}[3]{%
70 \pgfmathsetmacro{#1}{#2*#3}%
71 }
72
73 %\tdplotdiv{result}{dividend}{divisor}
74 %determines the division of specified values.
75 %#1: returns #2/#3
76 %TODO: handle divide by zero?
77 %#2: user-specified dividend
78 %#3: user-specified divisor
79 \newcommand{\tdplotdiv}[3]{%
80 \pgfmathsetmacro{#1}{#2/#3}%
81 }
82
83 %\tdplotcheckdiff{first value}{right value}{tolerance}{true code}{false code}
84 %compares two values to within specified tolerance. Executes either the true code or false code depending on the comparison result.
85 %#1: value 1 to compare
86 %#2: value 2 to compare
87 %#3: tolerance
88 %#4: true condition result
89 %#5: false condition result
90 \newcommand{\tdplotcheckdiff}[5]{%
91 %
92 %\pgfmathsubtract{#2}{#1}
93 %\pgfmathparse{ abs(#2 - #1)}
94
95 %\typeout{ #2, #1, \pgfmathresult, #3 }
96
97 \pgfmathparse{ abs(#2 - #1)<#3 }
98
99 %\typeout{ \pgfmathresult }
100 \ifthenelse{\equal{\pgfmathresult}{1}}{#4}{#5}
101 }
102
103 %\tdplotsetdisplay{\theta_d}{\phi_d}
104 %generates the coordinate transformation for defining a TikZ 3d plot display coordinates.
105 %#1: user-specified \theta_d, defining the angle through which the system is rotated about the x-axis. 0 points the z-axis "out of the page", while 90 points the z-axis vertically upward on the page.
106 %#2: user-specified \phi_d, defining the angle through which the system is rotated about the z-axis. 0 points the x-axis to the right.
107 \newcommand{\tdplotsetmaincoords}[2]{%
108 %perform some trig for the display transformation
109 %
110 %
111 %store the user-specified angles for possible future use
112 \pgfmathsetmacro{\tdplotmaintheta}{#1}
113 \pgfmathsetmacro{\tdplotmainphi}{#2}
114 %
115 %
116 \tdplotcalctransformmainscreen
117 %
118 %now here is where the output is performed
119 \tikzset{tdplot_main_coords/.style={x={(\raarot cm,\rbarot cm)},y={(\rabrot cm, \rbbrot cm)},z={(\racrot cm, \rbcrot cm)}}}%
120 }
121
122
123 %This performs the calculation to define the main coordinate frame orientation style, and is also used to transform a coordinate from the main coordinate frame the the screen coordinate frame
124 \newcommand{\tdplotcalctransformmainscreen}{%
125 %
126 %TODO: choose less obvious macro names? or look into scoping?
127 \tdplotsinandcos{\sintheta}{\costheta}{\tdplotmaintheta}%
128 \tdplotsinandcos{\sinphi}{\cosphi}{\tdplotmainphi}%
129 %
130 \tdplotmult{\stsp}{\sintheta}{\sinphi}%
131 \tdplotmult{\stcp}{\sintheta}{\cosphi}%
132 \tdplotmult{\ctsp}{\costheta}{\sinphi}%
133 \tdplotmult{\ctcp}{\costheta}{\cosphi}%
134 %
135 %determine rotation matrix elements for display transformation
136 \pgfmathsetmacro{\raarot}{\cosphi}%
137 \pgfmathsetmacro{\rabrot}{\sinphi}%
138 %NOTE: \rac is zero for this rotation, where z^c always points vertical on the page
139 \pgfmathsetmacro{\racrot}{0}%
140 \pgfmathsetmacro{\rbarot}{-\ctsp}%
141 \pgfmathsetmacro{\rbbrot}{\ctcp}%
142 \pgfmathsetmacro{\rbcrot}{\sintheta}%
143 %NOTE: third row of rotation matrix not needed for display since screen z is always flat on the page. It is, however, needed when performing further transformations using the Euler transformation.
144 \pgfmathsetmacro{\rcarot}{\stsp}%
145 \pgfmathsetmacro{\rcbrot}{-\stcp}%
146 \pgfmathsetmacro{\rccrot}{\costheta}%
147 %
148 }
149
150
151
152
153 %determines the rotation matrix for transformation from the rotation coordinate frame to the main coordinate frame. This also defines the rotation to produce the rotated coordinate frame.
154 \newcommand{\tdplotcalctransformrotmain}{%
155 %perform some trig for the Euler transformation
156 \tdplotsinandcos{\sinalpha}{\cosalpha}{\tdplotalpha}
157 \tdplotsinandcos{\sinbeta}{\cosbeta}{\tdplotbeta}
158 \tdplotsinandcos{\singamma}{\cosgamma}{\tdplotgamma}
159 %
160 \tdplotmult{\sasb}{\sinalpha}{\sinbeta}
161 \tdplotmult{\sbsg}{\sinbeta}{\singamma}
162 \tdplotmult{\sasg}{\sinalpha}{\singamma}
163 \tdplotmult{\sasbsg}{\sasb}{\singamma}
164 %
165 \tdplotmult{\sacb}{\sinalpha}{\cosbeta}
166 \tdplotmult{\sacg}{\sinalpha}{\cosgamma}
167 \tdplotmult{\sbcg}{\sinbeta}{\cosgamma}
168 \tdplotmult{\sacbsg}{\sacb}{\singamma}
169 \tdplotmult{\sacbcg}{\sacb}{\cosgamma}
170 %
171 \tdplotmult{\casb}{\cosalpha}{\sinbeta}
172 \tdplotmult{\cacb}{\cosalpha}{\cosbeta}
173 \tdplotmult{\cacg}{\cosalpha}{\cosgamma}
174 \tdplotmult{\casg}{\cosalpha}{\singamma}
175 %
176 \tdplotmult{\cacbsg}{\cacb}{\singamma}
177 \tdplotmult{\cacbcg}{\cacb}{\cosgamma}
178 %
179 %determine rotation matrix elements for Euler transformation
180 \pgfmathsetmacro{\raaeul}{\cacbcg - \sasg}
181 \pgfmathsetmacro{\rabeul}{-\cacbsg - \sacg}
182 \pgfmathsetmacro{\raceul}{\casb}
183 \pgfmathsetmacro{\rbaeul}{\sacbcg + \casg}
184 \pgfmathsetmacro{\rbbeul}{-\sacbsg + \cacg}
185 \pgfmathsetmacro{\rbceul}{\sasb}
186 \pgfmathsetmacro{\rcaeul}{-\sbcg}
187 \pgfmathsetmacro{\rcbeul}{\sbsg}
188 \pgfmathsetmacro{\rcceul}{\cosbeta}
189 %
190 %DEBUG: display euler matrix elements
191 %\raaeul\ \rabeul\ \raceul
192 %
193 %\rbaeul\ \rbbeul\ \rbceul
194 %
195 %\rcaeul\ \rcbeul\ \rcceul
196 }
197
198
199 %determines the rotation matrix for transformation from the main coordinate frame to the rotated coordinate frame.
200 \newcommand{\tdplotcalctransformmainrot}{%
201 %perform some trig for the Euler transformation
202 \tdplotsinandcos{\sinalpha}{\cosalpha}{\tdplotalpha}
203 \tdplotsinandcos{\sinbeta}{\cosbeta}{\tdplotbeta}
204 \tdplotsinandcos{\singamma}{\cosgamma}{\tdplotgamma}
205 %
206 \tdplotmult{\sasb}{\sinalpha}{\sinbeta}
207 \tdplotmult{\sbsg}{\sinbeta}{\singamma}
208 \tdplotmult{\sasg}{\sinalpha}{\singamma}
209 \tdplotmult{\sasbsg}{\sasb}{\singamma}
210 %
211 \tdplotmult{\sacb}{\sinalpha}{\cosbeta}
212 \tdplotmult{\sacg}{\sinalpha}{\cosgamma}
213 \tdplotmult{\sbcg}{\sinbeta}{\cosgamma}
214 \tdplotmult{\sacbsg}{\sacb}{\singamma}
215 \tdplotmult{\sacbcg}{\sacb}{\cosgamma}
216 %
217 \tdplotmult{\casb}{\cosalpha}{\sinbeta}
218 \tdplotmult{\cacb}{\cosalpha}{\cosbeta}
219 \tdplotmult{\cacg}{\cosalpha}{\cosgamma}
220 \tdplotmult{\casg}{\cosalpha}{\singamma}
221 %
222 \tdplotmult{\cacbsg}{\cacb}{\singamma}
223 \tdplotmult{\cacbcg}{\cacb}{\cosgamma}
224 %
225 %determine rotation matrix elements for Euler transformation
226 \pgfmathsetmacro{\raaeul}{\cacbcg - \sasg}
227 \pgfmathsetmacro{\rabeul}{\sacbcg + \casg}
228 \pgfmathsetmacro{\raceul}{-\sbcg}
229 \pgfmathsetmacro{\rbaeul}{-\cacbsg - \sacg}
230 \pgfmathsetmacro{\rbbeul}{-\sacbsg + \cacg}
231 \pgfmathsetmacro{\rbceul}{\sbsg}
232 \pgfmathsetmacro{\rcaeul}{\casb}
233 \pgfmathsetmacro{\rcbeul}{\sasb}
234 \pgfmathsetmacro{\rcceul}{\cosbeta}
235 %
236 %DEBUG: display euler matrix elements
237 %\raaeul\ \rabeul\ \raceul
238 %
239 %\rbaeul\ \rbbeul\ \rbceul
240 %
241 %\rcaeul\ \rcbeul\ \rcceul
242 }
243
244 %transforms a coordinate from the main coordinate frame to the rotated coordinate frame
245 \newcommand{\tdplottransformmainrot}[3]{%
246 \tdplotcalctransformmainrot
247
248 \pgfmathsetmacro{\tdplotresx}{\raaeul * #1 + \rabeul * #2 + \raceul * #3}
249 \pgfmathsetmacro{\tdplotresy}{\rbaeul * #1 + \rbbeul * #2 + \rbceul * #3}
250 \pgfmathsetmacro{\tdplotresz}{\rcaeul * #1 + \rcbeul * #2 + \rcceul * #3}
251 }
252
253 %transforms a coordinate from the rotated coordinate frame to the main coordinate frame
254 \newcommand{\tdplottransformrotmain}[3]{%
255 \tdplotcalctransformrotmain
256
257 \pgfmathsetmacro{\tdplotresx}{\raaeul * #1 + \rabeul * #2 + \raceul * #3}
258 \pgfmathsetmacro{\tdplotresy}{\rbaeul * #1 + \rbbeul * #2 + \rbceul * #3}
259 \pgfmathsetmacro{\tdplotresz}{\rcaeul * #1 + \rcbeul * #2 + \rcceul * #3}
260 }
261
262
263 %transforms a coordinate from the main coordinate frame to the rotated coordinate frame
264 \newcommand{\tdplottransformmainscreen}[3]{%
265 \tdplotcalctransformmainscreen
266
267 \pgfmathsetmacro{\tdplotresx}{\raarot * #1 + \rabrot * #2 + \racrot * #3}
268 \pgfmathsetmacro{\tdplotresy}{\rbarot * #1 + \rbbrot * #2 + \rbcrot * #3}
269 }
270
271
272
273 %\tdplotsetrotatedcoords{\alpha}{\beta}{\gamma}
274 %generates the coordinate transformation for the rotated coordinate system within the display coordinate system. This should be called only after the display coordinate system has been defined. If the display coordinate system changes, this will have to be updated.
275 %#1: user-specified euler angle \alpha.
276 %#2: user-specified euler angle \beta.
277 %#3: user-specified euler angle \gamma.
278 \newcommand{\tdplotsetrotatedcoords}[3]{%
279 %
280 \pgfmathsetmacro{\tdplotalpha}{#1}
281 \pgfmathsetmacro{\tdplotbeta}{#2}
282 \pgfmathsetmacro{\tdplotgamma}{#3}
283 %
284 \tdplotcalctransformrotmain
285
286 %
287 %now, determine master rotation matrix to define euler-rotated coordinates within the display coordinate frame
288 \tdplotmult{\raaeaa}{\raarot}{\raaeul}
289 \tdplotmult{\rabeba}{\rabrot}{\rbaeul}
290 \tdplotmult{\raceca}{\racrot}{\rcaeul}
291 %
292 \tdplotmult{\raaeab}{\raarot}{\rabeul}
293 \tdplotmult{\rabebb}{\rabrot}{\rbbeul}
294 \tdplotmult{\racecb}{\racrot}{\rcbeul}
295 %
296 \tdplotmult{\raaeac}{\raarot}{\raceul}
297 \tdplotmult{\rabebc}{\rabrot}{\rbceul}
298 \tdplotmult{\racecc}{\racrot}{\rcceul}
299 %
300 \tdplotmult{\rbaeaa}{\rbarot}{\raaeul}
301 \tdplotmult{\rbbeba}{\rbbrot}{\rbaeul}
302 \tdplotmult{\rbceca}{\rbcrot}{\rcaeul}
303 %
304 \tdplotmult{\rbaeab}{\rbarot}{\rabeul}
305 \tdplotmult{\rbbebb}{\rbbrot}{\rbbeul}
306 \tdplotmult{\rbcecb}{\rbcrot}{\rcbeul}
307 %
308 \tdplotmult{\rbaeac}{\rbarot}{\raceul}
309 \tdplotmult{\rbbebc}{\rbbrot}{\rbceul}
310 \tdplotmult{\rbcecc}{\rbcrot}{\rcceul}
311 %
312 %the third row is not needed for screen display
313 %\tdplotmult{\rcaeaa}{\rcarot}{\raaeul}
314 %\tdplotmult{\rcbeba}{\rcbrot}{\rbaeul}
315 %\tdplotmult{\rcceca}{\rccrot}{\rcaeul}
316 %
317 %\tdplotmult{\rcaeab}{\rcarot}{\rabeul}
318 %\tdplotmult{\rcbebb}{\rcbrot}{\rbbeul}
319 %\tdplotmult{\rccecb}{\rccrot}{\rcbeul}
320 %
321 %\tdplotmult{\rcaeac}{\rcarot}{\raceul}
322 %\tdplotmult{\rcbebc}{\rcbrot}{\rbceul}
323 %\tdplotmult{\rccecc}{\rccrot}{\rcceul}
324 %
325 %set up the master rotation matrix elements
326 \pgfmathsetmacro{\raarc}{\raaeaa + \rabeba + \raceca}
327 \pgfmathsetmacro{\rabrc}{\raaeab + \rabebb + \racecb}
328 \pgfmathsetmacro{\racrc}{\raaeac + \rabebc + \racecc}
329 \pgfmathsetmacro{\rbarc}{\rbaeaa + \rbbeba + \rbceca}
330 \pgfmathsetmacro{\rbbrc}{\rbaeab + \rbbebb + \rbcecb}
331 \pgfmathsetmacro{\rbcrc}{\rbaeac + \rbbebc + \rbcecc}
332 %the third row is not needed for screen display
333 %\pgfmathsetmacro{\rcarc}{\rcaeaa + \rcbeba + \rcceca}
334 %\pgfmathsetmacro{\rcbrc}{\rcaeab + \rcbebb + \rccecb}
335 %\pgfmathsetmacro{\rccrc}{\rcaeac + \rcbebc + \rccecc}
336 %
337 %DEBUG: display master matrix elements
338 %\raarc\ \rabrc\ \racrc
339 %
340 %\rbarc\ \rbbrc\ \rbcrc
341 %
342 %\rcarc\ \rcbrc\ \rccrc
343 %
344 \tikzset{tdplot_rotated_coords/.append style={x={(\raarc cm,\rbarc cm)},y={(\rabrc cm, \rbbrc cm)},z={(\racrc cm, \rbcrc cm)}}}%
345 }
346
347 %\tdplotsetrotatedcoordsorigin{point}
348 %this translates the rotated coordinate system to the specified point.
349 %#1: user-specified coordinate
350 \newcommand{\tdplotsetrotatedcoordsorigin}[1]{%
351 %\pgfmathsetmacro{\tdplotrotatedcoordsorigin}{#1}%
352 \tikzset{tdplot_rotated_coords/.append style={shift=#1}}%
353 }
354
355 %\tdplotresetrotatedcoordsorigin
356 %this resets the rotated coordinate system translation back to the origin of the main coordinate system
357 \newcommand{\tdplotresetrotatedcoordsorigin}{%
358 %\pgfmathsetmacro{\tdplotrotatedcoordsorigin}{#1}%
359 \tikzset{tdplot_rotated_coords/.append style={shift={(0,0,0)}}}%
360 }
361
362 %\tdplotsetthetaplanecoords{\phi}
363 %this places the rotated coordinate system such that it's x'-y' plane coincides with a "theta plane" for the main coordinate system: This plane contains the z axis, and lies at angle \phi from the x axis.
364 %#1: user-specified \phi angle from x-axis
365 \newcommand{\tdplotsetthetaplanecoords}[1]{%
366 %
367 \tdplotresetrotatedcoordsorigin
368 \tdplotsetrotatedcoords{270 + #1}{270}{0}%
369 }
370 %note: the following rotation permutes the x, y, and z axes in forward progression. Any value of \alpha greater than 270 will rotate the axes further, allowing for easy selection of the ``theta plane''.
371 %\tdplotsetrotatedcoords{270}{270}{00}
372
373 %note: the following rotation permutes the x,y, and z axes in backward progression.
374 %\tdplotsetrotatedcoords{0}{90}{90}
375
376 %\tdplotsetrotatedthetaplanecoords{\phi'}
377 %this places the rotated coordinate system into the "theta plane" for the current rotated coordinate system, at user-specified angle \phi'. Note that it replaces the current rotated coordinate system
378 %#1: user-specified \phi' angle from x'-axis
379 \newcommand{\tdplotsetrotatedthetaplanecoords}[1]{%
380 \tdplotsetrotatedcoords{\tdplotalpha}{\tdplotbeta}{\tdplotgamma + #1}%
381 %
382 %permute the coordinates
383 \tikzset{tdplot_rotated_coords/.append style={y={(\raarc cm,\rbarc cm)},z={(\rabrc cm, \rbbrc cm)},x={(\racrc cm, \rbcrc cm)}}}%
384 }
385
386 %\tdplotsetcoord{point}{r}{theta}{phi}
387 %sets a 3d point using spherical polar coordinates. This also generates xy, xz, and yz projections of this point using appropriately named points
388 %#1: name of point to set
389 %#2: user-specified r coordinate
390 %#3: user-specified \theta coordinate
391 %#4: user-specified \phi coordinate
392 \newcommand{\tdplotsetcoord}[4]{%
393 %
394 %do some trig to determine angular part of coordinate
395 \tdplotsinandcos{\sinthetavec}{\costhetavec}{#3}%
396 \tdplotsinandcos{\sinphivec}{\cosphivec}{#4}%
397 \tdplotmult{\stcpv}{\sinthetavec}{\cosphivec}%
398 \tdplotmult{\stspv}{\sinthetavec}{\sinphivec}%
399 %
400 %assign the point
401 \coordinate (#1) at ($#2*(\stcpv,\stspv,\costhetavec)$);
402 %assign the xy, xz, and yz projections of the point
403 \coordinate (#1xy) at ($#2*(\stcpv,\stspv,0)$);
404 \coordinate (#1xz) at ($#2*(\stcpv,0,\costhetavec)$);
405 \coordinate (#1yz) at ($#2*(0,\stspv,\costhetavec)$);
406 %assign the x, y, and z projections of the point
407 \coordinate (#1x) at ($#2*(\stcpv,0,0)$);
408 \coordinate (#1y) at ($#2*(0,\stspv,0)$);
409 \coordinate (#1z) at ($#2*(0,0,\costhetavec)$);
410 }
411
412 \newcommand{\tdplotsimplesetcoord}[4]{%
413 %
414 %do some trig to determine angular part of coordinate
415 \tdplotsinandcos{\sinthetavec}{\costhetavec}{#3}%
416 \tdplotsinandcos{\sinphivec}{\cosphivec}{#4}%
417 \tdplotmult{\stcpv}{\sinthetavec}{\cosphivec}%
418 \tdplotmult{\stspv}{\sinthetavec}{\sinphivec}%
419 %
420 %assign the point
421 \coordinate (#1) at ($#2*(\stcpv,\stspv,\costhetavec)$);
422 }
423
424
425 %\tdplotdrawarc[coordinate system, draw styles]{center}{r}{angle start}{angle end}{label options}{label}
426 %draws an arc and puts a label in the center with specified node options
427 %#1: Optional, specifies the coordinate system and any draw style
428 %#2: center point of arc
429 %#3: radius of arc
430 %#4: start angle
431 %#5: end angle
432 %#6: label options
433 %#7: label
434 % \tdplotdrawarc{(O)}{0.2}{0}{\phivec}{anchor=north}{$\phi$}
435 \newcommand{\tdplotdrawarc}[7][tdplot_main_coords]{%
436 \pgfmathsetmacro{\tdplottemp}{#5 + #4}
437 \tdplotdiv{\tdplottemp}{\tdplottemp}{2}
438 \path[#1] #2 + (\tdplottemp:#3) node[#6]{#7};
439 \draw[#1] #2 + (#4:#3) arc (#4:#5:#3);
440 }
441
442 \def\tdplotdefinepoints(#1,#2,#3)(#4,#5,#6)(#7,#8,#9){%
443 \pgfmathsetmacro{\tdplotvertexx}{#1}
444 \pgfmathsetmacro{\tdplotvertexy}{#2}
445 \pgfmathsetmacro{\tdplotvertexz}{#3}
446 \pgfmathsetmacro{\tdplotax}{#4}
447 \pgfmathsetmacro{\tdplotay}{#5}
448 \pgfmathsetmacro{\tdplotaz}{#6}
449 \pgfmathsetmacro{\tdplotbx}{#7}
450 \pgfmathsetmacro{\tdplotby}{#8}
451 \pgfmathsetmacro{\tdplotbz}{#9}
452 }%
453
454
455 %draws an arc using three specified points
456 %\tdplotdrawpolytopearc[thick]{1}{anchor=west}{$\theta$}
457 \newcommand{\tdplotdrawpolytopearc}[4][]{%
458
459 %determine vector lengths
460 \pgfmathsetmacro{\ax}{\tdplotax - \tdplotvertexx}
461 \pgfmathsetmacro{\ay}{\tdplotay - \tdplotvertexy}
462 \pgfmathsetmacro{\az}{\tdplotaz - \tdplotvertexz}
463
464 \pgfmathsetmacro{\bx}{\tdplotbx - \tdplotvertexx}
465 \pgfmathsetmacro{\by}{\tdplotby - \tdplotvertexy}
466 \pgfmathsetmacro{\bz}{\tdplotbz - \tdplotvertexz}
467
468 %determine normal to vectors
469 \tdplotcrossprod(\ax,\ay,\az)(\bx,\by,\bz)
470
471 %DEBUG: show the cross product
472 %\draw[->,blue] (\tdplotvertexx,\tdplotvertexy,\tdplotvertexz)
473 -- ++(\tdplotresx,\tdplotresy,\tdplotresz);
474
475 %get angles for this vector
476 \tdplotgetpolarcoords{\tdplotresx}{\tdplotresy}{\tdplotresz}
477
478 \typeout{angles for cross product: phi: \tdplotresphi theta: \tdplotrestheta}
479
480 %place the rotated coordinate system so that the z' axis points along this vector
481 \tdplotsetrotatedcoords{\tdplotresphi}{\tdplotrestheta}{0}
482 \coordinate (Vertex) at (\tdplotvertexx,\tdplotvertexy,\tdplotvertexz);
483 \tdplotsetrotatedcoordsorigin{(Vertex)}
484
485 %DEBUG: show the rotated coordinate system
486 %\draw[thick,tdplot_rotated_coords,->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x'$};
487 %\draw[thick,tdplot_rotated_coords,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y'$};
488 %\draw[thick,tdplot_rotated_coords,->] (0,0,0) -- (0,0,1) node[anchor=south]{$z'$};
489
490 %calculate the start angle of the arc
491 \tdplottransformmainrot{\ax}{\ay}{\az}
492 \tdplotgetpolarcoords{\tdplotresx}{\tdplotresy}{\tdplotresz}
493 \pgfmathsetmacro{\tdplotstartphi}{\tdplotresphi}
494
495
496 %calculate the end angle of the arc
497 \tdplottransformmainrot{\bx}{\by}{\bz}
498 \tdplotgetpolarcoords{\tdplotresx}{\tdplotresy}{\tdplotresz}
499
500 %draw the arc
501 \pgfmathparse{\tdplotstartphi < \tdplotresphi}
502 \ifthenelse{\equal{\pgfmathresult}{1}}%
503 {}%
504 {
505 \pgfmathsetmacro{\tdplotstartphi}{\tdplotstartphi - 360}
506 }
507
508 %\typeout{startphi: \tdplotstartphi}
509 %\typeout{endphi: \tdplotresphi}
510
511 \draw[tdplot_rotated_coords,#1] (0,0,0) + (\tdplotstartphi:#2) arc (\tdplotstartphi:\tdplotresphi:#2);
512
513 \pgfmathsetmacro{\tdplotresphi}{(\tdplotresphi + \tdplotstartphi)/2}
514
515 \draw[tdplot_rotated_coords] (0,0,0) + (\tdplotresphi:#2) node[#3]{#4};
516 }
517
518
519 % \tdplotsphericalsurfaceplot[fill mode]{theta step size}{phi step size}{r}
520 %draws a surface in spherical polar coordinates given by r(\theta,\phi), where angular ranges and steps are defined.
521 %#1: draw styles
522 %#2: theta segments
523 %#3: phi segments
524 %#4: r(\tdplottheta,\tdplotphi)
525 %#5: stroke color
526 %#6: fill color
527 %#7: x axis instructions, to be rendered when \phi = 0
528 %#8: y axis instructions, to be rendered when \phi = 90
529 %#9: z axis instructions, to be rendered when plotting is half done
530 %TODO: fix axes drawing function when the main display axis phi is at 0, 90, etc.
531 \newcommand{\tdplotsphericalsurfaceplot}[9][literal]{%
532 %
533 \typeout{3dplot: processing 3d surface plot...}
534
535 %retrieve the angular step sizes
536 \pgfmathsetmacro{\origviewthetastep}{360/#2}
537 \pgfmathsetmacro{\origviewphistep}{360/#3}
538
539 %determing the angular starting point, based on the step size and display orientation
540 \pgfmathparse{ mod(90 + \tdplotmainphi,\origviewphistep)}
541 \pgfmathsetmacro{\originalphi}{90 + \tdplotmainphi - \pgfmathresult}
542
543 \pgfmathparse{ mod(\tdplotmaintheta,\origviewthetastep)}
544 \pgfmathsetmacro{\originaltheta}{\tdplotmaintheta - \pgfmathresult}
545
546
547 %this fudge factor helps when properly rendering x and y axes
548 \pgfmathsetmacro{\tdplotsuperfudge}{\originaltheta > 90}
549
550 %draw back part of shape, doing left and right side individually
551 \foreach \leftright in {1,-1}
552 {
553 \pgfmathsetmacro{\viewphistart}{\originalphi}
554 \pgfmathsetmacro{\viewphistep}{\leftright * \origviewphistep}
555 \pgfmathsetmacro{\viewphiinc}{\viewphistart + \viewphistep}
556 \pgfmathsetmacro{\viewphiend}{\viewphistart + \leftright * 90 - \viewphistep}
557
558 %sweep over phi
559 \foreach \curphi in{\viewphistart,\viewphiinc,...,\viewphiend}
560 {
561 %draw upper and lower parts individually, starting at the point opposite display angle
562 \foreach \topbottom in {1,-1}
563 {
564 \pgfmathsetmacro{\viewthetastep}{\topbottom * \origviewthetastep}
565 \pgfmathsetmacro{\viewthetastart}{180 - \originaltheta}
566
567 \ifnum \topbottom=1
568 \pgfmathsetmacro{\viewthetaend}{180 - \origviewthetastep}
569 \else
570 \pgfmathsetmacro{\viewthetaend}{\origviewthetastep}
571 \fi
572 \pgfmathsetmacro{\viewthetainc}{\viewthetastart + \viewthetastep}
573
574 %perform the rendering of each slice of phi over a range of theta angles
575 \tdplotdosurfaceplot{#4}{#7}{#8}{#5}{#6}{#1}
576 }
577 }
578 }
579
580 %now that the back half is done, draw the z axis
581 \begin{scope}[opacity=1]
582 #9 %draw z axis content
583 \end{scope}
584
585 %next, draw front part of shape, doing left and right sides individually. This is essentially the same process as before
586 \pgfmathsetmacro{\tdplotsuperfudge}{\originaltheta < 90}
587 \foreach \leftright in {1,-1}
588 {
589 \pgfmathsetmacro{\viewphistep}{\leftright * \origviewphistep}
590 \pgfmathsetmacro{\viewphistart}{\originalphi + \leftright * 90}
591 \pgfmathsetmacro{\viewphiinc}{\viewphistart + \viewphistep}
592 \pgfmathsetmacro{\viewphiend}{\viewphistart + \leftright * 90 - \viewphistep}
593
594 %sweep over phi
595 \foreach \curphi in{\viewphistart,\viewphiinc,...,\viewphiend}
596 {
597 %draw upper and lower parts individually, starting at the either the north or south pole
598 \foreach \topbottom in {1,-1}
599 {
600 \pgfmathsetmacro{\viewthetastep}{-\topbottom * \origviewthetastep}
601 \ifnum \topbottom=1
602 \pgfmathsetmacro{\viewthetastart}{180}
603 \else
604 \pgfmathsetmacro{\viewthetastart}{0}
605 \fi
606
607 \pgfmathsetmacro{\viewthetaend}{\originaltheta - \viewthetastep}
608 \pgfmathsetmacro{\viewthetainc}{\viewthetastart + \viewthetastep}
609
610 %perform the rendering
611 \tdplotdosurfaceplot{#4}{#7}{#8}{#5}{#6}{#1}
612 }
613 }
614 }
615 }
616
617 %sets the angular range of the polar plot to user-specified values
618 \newcommand{\tdplotsetpolarplotrange}[4]{%
619 \pgfmathsetmacro{\tdplotlowerphi}{#3}
620 \pgfmathsetmacro{\tdplotupperphi}{#4}
621 \pgfmathsetmacro{\tdplotlowertheta}{#1}
622 \pgfmathsetmacro{\tdplotuppertheta}{#2}
623 }
624
625 \newcommand{\tdplotresetpolarplotrange}{%
626 \pgfmathsetmacro{\tdplotlowerphi}{0}
627 \pgfmathsetmacro{\tdplotupperphi}{360}
628 \pgfmathsetmacro{\tdplotlowertheta}{0}
629 \pgfmathsetmacro{\tdplotuppertheta}{180}
630 }
631
632
633
634 %internal command, performs the actual rendering for the \tdplotsphericalsurfaceplot command
635 %TODO: find proper syntax and format for internal commands not intended to be used by the general user
636 \newcommand{\tdplotdosurfaceplot}[6]{%
637
638 \pgfmathsetmacro{\nextphi}{\curphi + \tdplotsuperfudge*\viewphistep}
639
640 \begin{scope}[opacity=1]
641
642 %\typeout{ ----------------------------------- }
643
644 %check if the current phi angle is in position to draw the x axis
645 \tdplotcheckdiff{\nextphi}{360}{\origviewphistep}{#2}{}
646 \tdplotcheckdiff{\nextphi}{0}{\origviewphistep}{#2}{}
647
648 %check if the current phi angle is in position to draw the y axis
649 \tdplotcheckdiff{\nextphi}{90}{\origviewphistep}{#3}{}
650 \tdplotcheckdiff{\nextphi}{450}{\origviewphistep}{#3}{}
651 \end{scope}
652
653 %do the theta sweep
654 \foreach \curtheta in{\viewthetastart,\viewthetainc,...,\viewthetaend}
655 {
656
657 %convert phi, theta coords into longitude, latitude to make use of pgfpointspherical coordinates
658 \pgfmathsetmacro{\curlongitude}{90 - \curphi}
659 \pgfmathsetmacro{\curlatitude}{90 - \curtheta}
660
661 %If sweeping to the right, shift the value of phi to the lower value of phi.
662 \ifthenelse{\equal{\leftright}{-1.0}}%
663 {%
664 \pgfmathsetmacro{\curphi}{\curphi - \origviewphistep}
665 }{}
666 %\fi
667
668 \pgfmathsetmacro{\tdplottheta}{mod(\curtheta,360)}
669 \pgfmathsetmacro{\tdplotphi}{mod(\curphi,360)}
670
671 \pgfmathparse{\tdplotphi < 0}
672 \ifthenelse{\equal{\pgfmathresult}{1}}{
673 \pgfmathsetmacro{\tdplotphi}{\tdplotphi + 360}
674 }{}%
675
676 %test to see if this value is within the specified angular range
677 \pgfmathparse{\tdplottheta > \tdplotuppertheta}
678 \pgfmathsetmacro{\logictest}{1 - \pgfmathresult}
679
680 \pgfmathparse{\tdplottheta < \tdplotlowertheta}
681 \pgfmathsetmacro{\logictest}{\logictest * (1 - \pgfmathresult)}
682
683 \pgfmathsetmacro{\tdplottheta}{\tdplottheta + \viewthetastep}
684 \pgfmathparse{\tdplottheta > \tdplotuppertheta}
685 \pgfmathsetmacro{\logictest}{\logictest * (1 - \pgfmathresult)}
686
687 \pgfmathparse{\tdplottheta < \tdplotlowertheta}
688 \pgfmathsetmacro{\logictest}{\logictest * (1 - \pgfmathresult)}
689
690 \pgfmathparse{\tdplotphi > \tdplotupperphi}
691 \pgfmathsetmacro{\logictest}{\logictest * (1 - \pgfmathresult)}
692
693 \pgfmathparse{\tdplotphi < \tdplotlowerphi}
694 \pgfmathsetmacro{\logictest}{\logictest * (1 - \pgfmathresult)}
695
696 \pgfmathsetmacro{\tdplotphi}{\tdplotphi + \viewphistep}
697
698 \pgfmathparse{\tdplotphi < 0}
699 \ifthenelse{\equal{\pgfmathresult}{1}}{
700 \pgfmathsetmacro{\tdplotphi}{\tdplotphi + 360}
701 }{}%
702
703 \pgfmathparse{\tdplotphi > \tdplotupperphi}
704 \pgfmathsetmacro{\logictest}{\logictest * (1 - \pgfmathresult)}
705
706 \pgfmathparse{\tdplotphi < \tdplotlowerphi}
707 \pgfmathsetmacro{\logictest}{\logictest * (1 - \pgfmathresult)}
708
709
710 \pgfmathsetmacro{\tdplottheta}{\curtheta}
711 \pgfmathsetmacro{\tdplotphi}{\curphi}
712
713 %if using fill color parametric to angles
714 \ifthenelse{\equal{#6}{parametricfill}}{%
715 %and, if it's being plotted
716 \ifthenelse{\equal{\logictest}{1.0}}{%
717 \pgfmathsetmacro{\radius}{#1}
718 \pgfmathsetmacro{\tdplotr}{\radius*360} %factor of 360 lets the radius change hue through one full cycle for each unit radius
719
720 \pgfmathlessthan{\radius}{0}
721 \pgfmathsetmacro{\phaseshift}{180 * \pgfmathresult}
722
723 \pgfmathsetmacro{\colorarg}{#5}
724 \pgfmathsetmacro{\colorarg}{\colorarg + \phaseshift}
725 \pgfmathsetmacro{\colorarg}{mod(\colorarg,360)}
726
727 \pgfmathlessthan{\colorarg}{0}
728 \pgfmathsetmacro{\colorarg}{\colorarg + 360*\pgfmathresult}
729
730 \pgfmathdivide{\colorarg}{360}
731 \definecolor{tdplotfillcolor}{hsb}{\pgfmathresult, 1, 1}
732 \color{tdplotfillcolor}
733 }{}%
734 }%
735 {%
736 \pgfsetfillcolor{#5}
737 }
738 \pgfsetstrokecolor{#4}
739
740 \ifthenelse{\equal{\leftright}{-1.0}}%
741 {%
742 \pgfmathsetmacro{\curphi}{\curphi + \origviewphistep}
743 }{}
744 %\fi
745
746 %if this section is being plotted
747 \ifthenelse{\equal{\logictest}{1.0}}{%
748 \pgfmathsetmacro{\radius}{abs(#1)}
749 \pgfpathmoveto{\pgfpointspherical{\curlongitude}{\curlatitude}{\radius}}
750
751 \pgfmathsetmacro{\tdplotphi}{\curphi + \viewphistep}
752 \pgfmathsetmacro{\radius}{abs(#1)}
753 \pgfpathlineto{\pgfpointspherical{\curlongitude - \viewphistep}{\curlatitude}{\radius}}
754
755 \pgfmathsetmacro{\tdplottheta}{\curtheta + \viewthetastep}
756 \pgfmathsetmacro{\radius}{abs(#1)}
757 \pgfpathlineto{\pgfpointspherical{\curlongitude - \viewphistep}{\curlatitude - \viewthetastep}{\radius}}
758
759 \pgfmathsetmacro{\tdplotphi}{\curphi}
760 \pgfmathsetmacro{\radius}{abs(#1)}
761 \pgfpathlineto{\pgfpointspherical{\curlongitude}{\curlatitude - \viewthetastep}{\radius}}
762 \pgfpathclose
763
764 \pgfusepath{fill,stroke}
765 }{}
766 }
767 }
768
769 %\tdplotshowargcolorguide{x position}{y position}{x size}{y size}
770 %#1: screen x position
771 %#2: screen y position
772 %#3: x size (susceptible to scale)
773 %#4: y size (susceptible to scale)
774 \newcommand{\tdplotshowargcolorguide}[4]{
775
776 \pgfmathsetmacro{\tdplotx}{#1}
777 \pgfmathsetmacro{\tdploty}{#2}
778 \pgfmathsetmacro{\tdplothuestep}{5}
779 \pgfmathsetmacro{\tdplotxsize}{#3}
780 \pgfmathsetmacro{\tdplotysize}{#4}
781
782 \pgfmathsetmacro{\tdplotyscale}{\tdplotysize/360}
783
784 \foreach \tdplotphi in {0,\tdplothuestep,...,360}
785 {
786 \pgfmathdivide{\tdplotphi}{360}
787 \definecolor{tdplotfillcolor}{hsb}{\pgfmathresult, 1, 1}
788 \color{tdplotfillcolor}
789
790 \pgfmathsetmacro{\tdplotstarty}{\tdploty + \tdplotphi * \tdplotyscale}
791 \pgfmathsetmacro{\tdplotstopy}{\tdplotstarty + \tdplothuestep * \tdplotyscale}
792 \pgfmathsetmacro{\tdplotstartx}{\tdplotx}
793 \pgfmathsetmacro{\tdplotstopx}{\tdplotx + \tdplotxsize}
794 \filldraw[tdplot_screen_coords] (\tdplotstartx,\tdplotstarty) rectangle (\tdplotstopx,\tdplotstopy);
795 }
796
797 \pgfmathsetmacro{\tdplotstopy}{\tdploty + (360+\tdplothuestep)*\tdplotyscale }
798 \pgfmathsetmacro{\tdplotstopx}{\tdplotx + \tdplotxsize}
799
800 \draw[tdplot_screen_coords] (\tdplotx,\tdploty) rectangle (\tdplotstopx,\tdplotstopy);
801
802 \node[tdplot_screen_coords,anchor=west,xshift=5pt] at (\tdplotstopx,\tdploty) {$0$};
803 \node[tdplot_screen_coords,anchor=west,xshift=5pt] at (\tdplotstopx,\tdplotstopy) {$2\pi$};
804
805 \pgfmathsetmacro{\tdplotstopy}{\tdploty + (360+\tdplothuestep)/2*\tdplotyscale }
806 \node[tdplot_screen_coords,anchor=west, xshift=5pt] at (\tdplotstopx, \tdplotstopy) {$\pi$};
807 }
808
809
810 %\tdplotgetpolarcoords{\vx}{\vy}{\vz}
811 %determines the theta and phi angle associated with the specified x, y, and z components of a vector
812 \newcommand{\tdplotgetpolarcoords}[3]{%
813 %
814 \pgfmathsetmacro{\vxcalc}{#1}
815 \pgfmathsetmacro{\vycalc}{#2}
816 \pgfmathsetmacro{\vzcalc}{#3}
817 %
818 \pgfmathsetmacro{\vcalc}{ sqrt((\vxcalc)^2 + (\vycalc)^2 + (\vzcalc)^2) }
819 % \pgfmathsetmacro{\vcalc}{ (\vxcalc^2 + \vycalc^2 + \vzcalc^2)^.5 }
820
821 \pgfmathsetmacro{\vxycalc}{ sqrt((\vxcalc)^2 + (\vycalc)^2) }
822 % \pgfmathsetmacro{\vxycalc}{ (\vxcalc^2 + \vycalc^2)^.5 }
823
824 \pgfmathsetmacro{\tdplotrestheta}{asin(\vxycalc/\vcalc)}
825 %
826 %check for angles larger than 90
827 \pgfmathparse{\vzcalc < 0}
828 \ifthenelse{\equal{\pgfmathresult}{1}}%
829 {%
830 \pgfmathsetmacro{\tdplotrestheta}{180 - \tdplotrestheta}
831 }
832 {}
833 %
834 %check for special case: vx = 0
835 \ifthenelse{\equal{\vxcalc}{0.0}}%
836 {%
837 %check the sign of vy, and set angle appropriately
838 \pgfmathparse{\vycalc < 0}
839 \ifthenelse{\equal{\pgfmathresult}{1}}%
840 {%
841 \pgfmathsetmacro{\tdplotresphi}{270}
842 }
843 {%
844 \pgfmathparse{\vycalc > 0}
845 \ifthenelse{\equal{\pgfmathresult}{1}}%
846 {%
847 %\typeout{\vycalc}
848 \pgfmathsetmacro{\tdplotresphi}{90}
849 }
850 {%
851 %\typeout{OVER HERE!!!!}
852 \pgfmathsetmacro{\tdplotresphi}{0}
853 }
854 }
855 }
856 {%
857 %perform the arctan calculation
858 \pgfmathsetmacro{\tdplotresphi}{atan(\vycalc/\vxcalc)}
859 %
860 %check if vx is less than zero, to properly identify the quadrant
861 \pgfmathparse{\vxcalc < 0}
862 %\typeout{x: \vxcalc\ y: \vycalc\ Raw phi: \tdplotresphi\ math result: \pgfmathresult}
863 \ifthenelse{\equal{\pgfmathresult}{1}}%
864 {%
865 %\typeout{HERE!}
866 %increase by half a rotation if necessary
867 \pgfmathsetmacro{\tdplotresphi}{\tdplotresphi+180}
868 }
869 {
870 %\typeout{SPOON!}
871 }
872
873 %ensure the angle lies between 0 and 360 degrees
874 \pgfmathparse{\tdplotresphi < 0}
875 \ifthenelse{\equal{\pgfmathresult}{1}}%
876 {%
877 \pgfmathsetmacro{\tdplotresphi}{\tdplotresphi+360}
878 }
879 {}
880 }
881 }
882
883
884 % \tdplotcrossprod(\ax,\ay,\az)(\bx,\by,\bz)
885 \def\tdplotcrossprod(#1,#2,#3)(#4,#5,#6){%
886 \pgfmathsetmacro{\tdplotresx}{#2 * #6 - #3 * #5}
887 \pgfmathsetmacro{\tdplotresy}{#3 * #4 - #1 * #6}
888 \pgfmathsetmacro{\tdplotresz}{#1 * #5 - #2 * #4}
889
890 }
891
892 %\newcommand{\tdplottransform
893 %
894 %
895 %\newcommand{\tdplotgetplane}{
896 %
897 %}
898
899
900 %Notes
901 %-----
902
903 %PROBLEM:
904 %the line-by-line coordinate transformation does not accept predefined points. It only works with literal points. Example:
905 %\begin{tikzpicture}[smooth]
906 % \draw plot coordinates{(1,0)(2,0.5)(3,0)(3,1)};
907 % \draw[x={(0cm,1cm)},y={(1cm,0cm)},color=red] plot coordinates{(1,0)(2,0.5)(3,0)(3,1)};
908 %\end{tikzpicture}
909 %
910 %\begin{tikzpicture}[smooth]
911 % \coordinate (A) at (1,0);
912 % \coordinate (B) at (2,0.5);
913 % \coordinate (C) at (3,0);
914 % \coordinate (D) at (3,1);
915 % \draw plot coordinates{(A)(B)(C)(D)};
916 % \draw[x={(0cm,1cm)},y={(1cm,0cm)},color=red] plot coordinates{(A)(B)(C)(D)};
917 %\end{tikzpicture}
918 %SOLUTION: none so far, other than use literal points.
919
920
921 %PROBLEM:
922 % \node[tdplot_rotated_coords,anchor=south west] at (\thetavec/2:.5){$\theta$};
923 %it looks like the \node command can't be placed properly when a shift={} is defined in a style?
924 %SOLUTION: Use something like this instead:
925 %\draw[tdplot_rotated_coords] (O) + (\thetavec/2:.5) node[anchor=south west]{$\theta$};
926
927
928 %PROBLEM:
929 %\draw[-stealth,color=orange] (0,0,0) -- (xyz spherical cs:radius=.5,longitude=60,latitude=120);
930 %this gives the compile error: Undefined control sequence. <argument> \tikz@cs@radius. Not sure if this is due to some missing code in the TikZ 3d library.
931 %SOLUTION: none found yet.