summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/tikz-3dplot/tikz-3dplot.sty
blob: 819e8394fd4f4789dcc86d378af5f178eecc07bd (plain)
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
%% tikz-3dplot.sty
%% Copyright 2010 Jeffrey D. Hein
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Jeffrey D. Hein.
%
% This work consists of the files 3dplot.sty and 3dplot_documentation.tex

%Description
%-----------

%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.  

%Created 2009-11-07 by Jeff Hein.  Last updated: 2010-01-17
%----------------------------------------------------------

%Requirements
%------------
%This requires the tikz package.

%Update Notes
%------------

%2009-11-07: Created package from previous scratch work
%2009-11-18: Added a few macros for storing user-specified coordinate frame angles.  Useful for commands which would like to know what the current display perspective is.
%2009-11-21: Added spherical polar parametric surface plotting functionality with the \tdplotsphericalsurfaceplot command.
%2009-12-03: Fixed the range checking for polar parametric plotting.  It should now behave better around the range \phi = 0
%2010-01-17: renamed the package to tikz-3dplot.

%TODO list
%---------

%TODO: figure out how to work in a variable scope that doesn't interfere with other packages
%TODO: find a way to check if TikZ is loaded, and give a compile error if necessary
%TODO: find a way to use predefined coordinates in rotated or translated coordinate frames, instead of just literal coordinates
%TODO: generalize matrix math if such a package exists
%TODO: look into using TikZ spherical polar coordinates explicitly to streamline coordinate definitions.

\RequirePackage{pgf}
\RequirePackage{ifthen}

%Style definitions and includes
%------------------------------
\usetikzlibrary{calc,3d}

\tikzset{tdplot_screen_coords/.style={x={(1 cm,0 cm)},y={(0 cm, 1 cm)},z={(-1 cm, -1 cm)}}}%

\pgfmathsetmacro{\lowerphi}{0}
\pgfmathsetmacro{\upperphi}{360}
\pgfmathsetmacro{\lowertheta}{0}
\pgfmathsetmacro{\uppertheta}{180}


%Commands
%--------

%\tdplotsinandcos{sin}{cos}{theta}
%determines the sin and cos of the specified angle (in degrees).
%#1: returns sin(#3)
%#2: returns cos(#3)
%#3: user-specified angle
\newcommand{\tdplotsinandcos}[3]{%
\pgfmathsetmacro{#1}{sin(#3)}%
\pgfmathsetmacro{#2}{cos(#3)}%
}

%\tdplotmult{result}{multiplicand}{multiplicator}
%determines the multiplication of specified values.
%#1: returns #2*#3
%#2: user-specified multiplicand
%#3: user-specified multiplicator
\newcommand{\tdplotmult}[3]{%
\pgfmathsetmacro{#1}{#2*#3}%
}

%\tdplotdiv{result}{dividend}{divisor}
%determines the division of specified values.
%#1: returns #2/#3
%TODO: handle divide by zero?
%#2: user-specified dividend
%#3: user-specified divisor
\newcommand{\tdplotdiv}[3]{%
\pgfmathsetmacro{#1}{#2/#3}%
}

%\tdplotcheckdiff{first value}{right value}{tolerance}{true code}{false code}
%compares two values to within specified tolerance.  Executes either the true code or false code depending on the comparison result.
%#1: value 1 to compare
%#2: value 2 to compare
%#3: tolerance
%#4: true condition result
%#5: false condition result
\newcommand{\tdplotcheckdiff}[5]{%
%
	\pgfmathsubtract{#2}{#1}
	\pgfmathparse{ abs(#2 - #1)}
	\pgfmathparse{ abs(#2 - #1)<#3}
	\ifthenelse{\equal{\pgfmathresult}{1.0}}{#4}{#5}
}

%\tdplotsetdisplay{\theta_d}{\phi_d}
%generates the coordinate transformation for defining a TikZ 3d plot display coordinates.
%#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.
%#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.
\newcommand{\tdplotsetmaincoords}[2]{%
%perform some trig for the display transformation
%
%
%store the user-specified angles for possible future use
\pgfmathsetmacro{\tdplotmaintheta}{#1}
\pgfmathsetmacro{\tdplotmainphi}{#2}
%
%TODO: choose less obvious macro names?  or look into scoping?
\tdplotsinandcos{\sintheta}{\costheta}{\tdplotmaintheta}%
\tdplotsinandcos{\sinphi}{\cosphi}{\tdplotmainphi}%
%
\tdplotmult{\stsp}{\sintheta}{\sinphi}%
\tdplotmult{\stcp}{\sintheta}{\cosphi}%
\tdplotmult{\ctsp}{\costheta}{\sinphi}%
\tdplotmult{\ctcp}{\costheta}{\cosphi}%
%
%determine rotation matrix elements for display transformation
\pgfmathsetmacro{\raarot}{\cosphi}%
\pgfmathsetmacro{\rabrot}{\sinphi}%
%NOTE: \rac is zero for this rotation, where z^c always points vertical on the page
\pgfmathsetmacro{\racrot}{0}%
\pgfmathsetmacro{\rbarot}{-\ctsp}%
\pgfmathsetmacro{\rbbrot}{\ctcp}%
\pgfmathsetmacro{\rbcrot}{\sintheta}%
%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.
\pgfmathsetmacro{\rcarot}{\stsp}%
\pgfmathsetmacro{\rcbrot}{-\stcp}%
\pgfmathsetmacro{\rccrot}{\costheta}%
%
%now here is where the output is performed
\tikzset{tdplot_main_coords/.style={x={(\raarot cm,\rbarot cm)},y={(\rabrot cm, \rbbrot cm)},z={(\racrot cm, \rbcrot cm)}}}%
}


%\tdplotsetrotatedcoords{\alpha}{\beta}{\gamma}
%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.
%#1: user-specified euler angle \alpha.
%#2: user-specified euler angle \beta.
%#3: user-specified euler angle \gamma.
\newcommand{\tdplotsetrotatedcoords}[3]{%
%
\pgfmathsetmacro{\alphaeul}{#1}
\pgfmathsetmacro{\betaeul}{#2}
\pgfmathsetmacro{\gammaeul}{#3}
%
%perform some trig for the Euler transformation
\tdplotsinandcos{\sinalpha}{\cosalpha}{\alphaeul} 
\tdplotsinandcos{\sinbeta}{\cosbeta}{\betaeul}
\tdplotsinandcos{\singamma}{\cosgamma}{\gammaeul}
%
\tdplotmult{\sasb}{\sinalpha}{\sinbeta}
\tdplotmult{\sbsg}{\sinbeta}{\singamma}
\tdplotmult{\sasg}{\sinalpha}{\singamma}
\tdplotmult{\sasbsg}{\sasb}{\singamma}
%
\tdplotmult{\sacb}{\sinalpha}{\cosbeta}
\tdplotmult{\sacg}{\sinalpha}{\cosgamma}
\tdplotmult{\sbcg}{\sinbeta}{\cosgamma}
\tdplotmult{\sacbsg}{\sacb}{\singamma}
\tdplotmult{\sacbcg}{\sacb}{\cosgamma}
%
\tdplotmult{\casb}{\cosalpha}{\sinbeta}
\tdplotmult{\cacb}{\cosalpha}{\cosbeta}
\tdplotmult{\cacg}{\cosalpha}{\cosgamma}
\tdplotmult{\casg}{\cosalpha}{\singamma}
%
\tdplotmult{\cacbsg}{\cacb}{\singamma}
\tdplotmult{\cacbcg}{\cacb}{\cosgamma}
%
%determine rotation matrix elements for Euler transformation
\pgfmathsetmacro{\raaeul}{\cacbcg - \sasg}
\pgfmathsetmacro{\rabeul}{-\cacbsg - \sacg}
\pgfmathsetmacro{\raceul}{\casb}
\pgfmathsetmacro{\rbaeul}{\sacbcg + \casg}
\pgfmathsetmacro{\rbbeul}{-\sacbsg + \cacg}
\pgfmathsetmacro{\rbceul}{\sasb}
\pgfmathsetmacro{\rcaeul}{-\sbcg}
\pgfmathsetmacro{\rcbeul}{\sbsg}
\pgfmathsetmacro{\rcceul}{\cosbeta}
%
%DEBUG: display euler matrix elements
%\raaeul\ \rabeul\ \raceul
%
%\rbaeul\ \rbbeul\ \rbceul
%
%\rcaeul\ \rcbeul\ \rcceul
%
%now, determine master rotation matrix to define euler-rotated coordinates within the display coordinate frame
\tdplotmult{\raaeaa}{\raarot}{\raaeul}
\tdplotmult{\rabeba}{\rabrot}{\rbaeul}
\tdplotmult{\raceca}{\racrot}{\rcaeul}
%
\tdplotmult{\raaeab}{\raarot}{\rabeul}
\tdplotmult{\rabebb}{\rabrot}{\rbbeul}
\tdplotmult{\racecb}{\racrot}{\rcbeul}
%
\tdplotmult{\raaeac}{\raarot}{\raceul}
\tdplotmult{\rabebc}{\rabrot}{\rbceul}
\tdplotmult{\racecc}{\racrot}{\rcceul}
%
\tdplotmult{\rbaeaa}{\rbarot}{\raaeul}
\tdplotmult{\rbbeba}{\rbbrot}{\rbaeul}
\tdplotmult{\rbceca}{\rbcrot}{\rcaeul}
%
\tdplotmult{\rbaeab}{\rbarot}{\rabeul}
\tdplotmult{\rbbebb}{\rbbrot}{\rbbeul}
\tdplotmult{\rbcecb}{\rbcrot}{\rcbeul}
%
\tdplotmult{\rbaeac}{\rbarot}{\raceul}
\tdplotmult{\rbbebc}{\rbbrot}{\rbceul}
\tdplotmult{\rbcecc}{\rbcrot}{\rcceul}
%
%the third row is not needed for screen display
%\tdplotmult{\rcaeaa}{\rcarot}{\raaeul}
%\tdplotmult{\rcbeba}{\rcbrot}{\rbaeul}
%\tdplotmult{\rcceca}{\rccrot}{\rcaeul}
%
%\tdplotmult{\rcaeab}{\rcarot}{\rabeul}
%\tdplotmult{\rcbebb}{\rcbrot}{\rbbeul}
%\tdplotmult{\rccecb}{\rccrot}{\rcbeul}
%
%\tdplotmult{\rcaeac}{\rcarot}{\raceul}
%\tdplotmult{\rcbebc}{\rcbrot}{\rbceul}
%\tdplotmult{\rccecc}{\rccrot}{\rcceul}
%
%set up the master rotation matrix elements
\pgfmathsetmacro{\raarc}{\raaeaa + \rabeba + \raceca}
\pgfmathsetmacro{\rabrc}{\raaeab + \rabebb + \racecb}
\pgfmathsetmacro{\racrc}{\raaeac + \rabebc + \racecc}
\pgfmathsetmacro{\rbarc}{\rbaeaa + \rbbeba + \rbceca}
\pgfmathsetmacro{\rbbrc}{\rbaeab + \rbbebb + \rbcecb}
\pgfmathsetmacro{\rbcrc}{\rbaeac + \rbbebc + \rbcecc}
%the third row is not needed for screen display
%\pgfmathsetmacro{\rcarc}{\rcaeaa + \rcbeba + \rcceca}
%\pgfmathsetmacro{\rcbrc}{\rcaeab + \rcbebb + \rccecb}
%\pgfmathsetmacro{\rccrc}{\rcaeac + \rcbebc + \rccecc}
%
%DEBUG: display master matrix elements
%\raarc\ \rabrc\ \racrc
%
%\rbarc\ \rbbrc\ \rbcrc
%
%\rcarc\ \rcbrc\ \rccrc
%
\tikzset{tdplot_rotated_coords/.append style={x={(\raarc cm,\rbarc cm)},y={(\rabrc cm, \rbbrc cm)},z={(\racrc cm, \rbcrc cm)}}}%
}

%\tdplotsetrotatedcoordsorigin{point}
%this translates the rotated coordinate system to the specified point.
%#1: user-specified coordinate
\newcommand{\tdplotsetrotatedcoordsorigin}[1]{%
%\pgfmathsetmacro{\tdplotrotatedcoordsorigin}{#1}%
\tikzset{tdplot_rotated_coords/.append style={shift=#1}}%
}

%\tdplotresetrotatedcoordsorigin
%this resets the rotated coordinate system translation back to the origin of the main coordinate system
\newcommand{\tdplotresetrotatedcoordsorigin}{%
%\pgfmathsetmacro{\tdplotrotatedcoordsorigin}{#1}%
\tikzset{tdplot_rotated_coords/.append style={shift={(0,0,0)}}}%
}

%\tdplotsetthetaplanecoords{\phi}
%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.
%#1: user-specified \phi angle from x-axis
\newcommand{\tdplotsetthetaplanecoords}[1]{%
%
	\tdplotresetrotatedcoordsorigin
	\tdplotsetrotatedcoords{270 + #1}{270}{0}%
}
%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''.
%\tdplotsetrotatedcoords{270}{270}{00}

%note: the following rotation permutes the x,y, and z axes in backward progression.
%\tdplotsetrotatedcoords{0}{90}{90}

%\tdplotsetrotatedthetaplanecoords{\phi'}
%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
%#1: user-specified \phi' angle from x'-axis
\newcommand{\tdplotsetrotatedthetaplanecoords}[1]{%
	\tdplotsetrotatedcoords{\alphaeul}{\betaeul}{\gammaeul + #1}%
	%
	%permute the coordinates
	\tikzset{tdplot_rotated_coords/.append style={y={(\raarc cm,\rbarc cm)},z={(\rabrc cm, \rbbrc cm)},x={(\racrc cm, \rbcrc cm)}}}%
}

%\tdplotsetcoord{point}{r}{theta}{phi}
%sets a 3d point using spherical polar coordinates.  This also generates xy, xz, and yz projections of this point using appropriately named points
%#1: name of point to set
%#2: user-specified r coordinate
%#3: user-specified \theta coordinate
%#4: user-specified \phi coordinate
\newcommand{\tdplotsetcoord}[4]{%
%
%do some trig to determine angular part of coordinate
\tdplotsinandcos{\sinthetavec}{\costhetavec}{#3}%
\tdplotsinandcos{\sinphivec}{\cosphivec}{#4}%
\tdplotmult{\stcpv}{\sinthetavec}{\cosphivec}%
\tdplotmult{\stspv}{\sinthetavec}{\sinphivec}%
%
%assign the point
\coordinate (#1) at ($#2*(\stcpv,\stspv,\costhetavec)$);
%assign the xy, xz, and yz projections of the point
\coordinate (#1xy) at ($#2*(\stcpv,\stspv,0)$);
\coordinate (#1xz) at ($#2*(\stcpv,0,\costhetavec)$);
\coordinate (#1yz) at ($#2*(0,\stspv,\costhetavec)$);
%assign the x, y, and z projections of the point
\coordinate (#1x) at ($#2*(\stcpv,0,0)$);
\coordinate (#1y) at ($#2*(0,\stspv,0)$);
\coordinate (#1z) at ($#2*(0,0,\costhetavec)$);
}

\newcommand{\tdplotsimplesetcoord}[4]{%
%
%do some trig to determine angular part of coordinate
\tdplotsinandcos{\sinthetavec}{\costhetavec}{#3}%
\tdplotsinandcos{\sinphivec}{\cosphivec}{#4}%
\tdplotmult{\stcpv}{\sinthetavec}{\cosphivec}%
\tdplotmult{\stspv}{\sinthetavec}{\sinphivec}%
%
%assign the point
\coordinate (#1) at ($#2*(\stcpv,\stspv,\costhetavec)$);
}


%\tdplotdrawarc[coordinate system, draw styles]{center}{r}{angle start}{angle end}{label options}{label}
%draws an arc and puts a label in the center with specified node options
%#1: Optional, specifies the coordinate system and any draw style
%#2: center point of arc
%#3: radius of arc
%#4: start angle
%#5: end angle
%#6: label options
%#7: label
%	\tdplotdrawarc{(O)}{0.2}{0}{\phivec}{anchor=north}{$\phi$}
\newcommand{\tdplotdrawarc}[7][tdplot_main_coords]{%
\pgfmathsetmacro{\tdplottemp}{#5 + #4}
\tdplotdiv{\tdplottemp}{\tdplottemp}{2}
\draw[#1] #2 + (\tdplottemp:#3) node[#6]{#7};
\draw[#1] #2 + (#4:#3) arc (#4:#5:#3);
}

%	\tdplotsphericalsurfaceplot[fill mode]{theta step size}{phi step size}{r}
%draws a surface in spherical polar coordinates given by r(\theta,\phi), where angular ranges and steps are defined.  
%#1: draw styles
%#2: theta segments
%#3: phi segments
%#4: r(\tdplottheta,\tdplotphi)
%#5: stroke color
%#6: fill color
%#7: x axis instructions, to be rendered when \phi = 0
%#8: y axis instructions, to be rendered when \phi = 90
%#9: z axis instructions, to be rendered when plotting is half done
%TODO: fix axes drawing function when the main display axis phi is at 0, 90, etc.  
\newcommand{\tdplotsphericalsurfaceplot}[9][literal]{%
%
	\typeout{3dplot: processing 3d surface plot...}

	%retrieve the angular step sizes
	\pgfmathsetmacro{\origviewthetastep}{360/#2}
	\pgfmathsetmacro{\origviewphistep}{360/#3}

	%determing the angular starting point, based on the step size and display orientation
	\pgfmathparse{ mod(90 + \tdplotmainphi,\origviewphistep)}
	\pgfmathsetmacro{\originalphi}{90 + \tdplotmainphi - \pgfmathresult}

	\pgfmathparse{ mod(\tdplotmaintheta,\origviewthetastep)}
	\pgfmathsetmacro{\originaltheta}{\tdplotmaintheta - \pgfmathresult}


	%this fudge factor helps when properly rendering x and y axes
	\pgfmathsetmacro{\tdplotsuperfudge}{\originaltheta > 90}

	%draw back part of shape, doing left and right side individually
	\foreach \leftright in {1,-1}
	{
		\pgfmathsetmacro{\viewphistart}{\originalphi}
		\pgfmathsetmacro{\viewphistep}{\leftright * \origviewphistep}
		\pgfmathsetmacro{\viewphiinc}{\viewphistart + \viewphistep}
		\pgfmathsetmacro{\viewphiend}{\viewphistart + \leftright * 90 - \viewphistep}

		\foreach \curphi in{\viewphistart,\viewphiinc,...,\viewphiend}
		{
			%draw upper and lower parts individually, starting at the point opposite display angle
			\foreach \topbottom in {1,-1}
			{
				\pgfmathsetmacro{\viewthetastep}{\topbottom * \origviewthetastep}
				\pgfmathsetmacro{\viewthetastart}{180 - \originaltheta}

				\ifnum \topbottom=1
					\pgfmathsetmacro{\viewthetaend}{180 - \origviewthetastep}
				\else
					\pgfmathsetmacro{\viewthetaend}{\origviewthetastep}
				\fi
				\pgfmathsetmacro{\viewthetainc}{\viewthetastart + \viewthetastep}

				%perform the rendering 
				\tdplotdosurfaceplot{#4}{#7}{#8}{#5}{#6}{#1}
			}
		}
	}
	\begin{scope}[opacity=1]
		#9 %draw z axis content
	\end{scope}

	%draw front part of shape, doing left and right sides individually
	\pgfmathsetmacro{\tdplotsuperfudge}{\originaltheta < 90}
	\foreach \leftright in {1,-1}
	{
		\pgfmathsetmacro{\viewphistep}{\leftright * \origviewphistep}
		\pgfmathsetmacro{\viewphistart}{\originalphi + \leftright * 90}
		\pgfmathsetmacro{\viewphiinc}{\viewphistart + \viewphistep}
		\pgfmathsetmacro{\viewphiend}{\viewphistart + \leftright * 90 - \viewphistep}

		\foreach \curphi in{\viewphistart,\viewphiinc,...,\viewphiend}
		{
			%draw upper and lower parts individually, starting at the either the north or south pole
			\foreach \topbottom in {1,-1}
			{
				\pgfmathsetmacro{\viewthetastep}{-\topbottom * \origviewthetastep}
				\ifnum \topbottom=1
					\pgfmathsetmacro{\viewthetastart}{180}
				\else
					\pgfmathsetmacro{\viewthetastart}{0}
				\fi

				\pgfmathsetmacro{\viewthetaend}{\originaltheta - \viewthetastep}
				\pgfmathsetmacro{\viewthetainc}{\viewthetastart + \viewthetastep}

				%perform the rendering
				\tdplotdosurfaceplot{#4}{#7}{#8}{#5}{#6}{#1}
			}
		}
	}
}

%sets the angular range of the polar plot to user-specified values
\newcommand{\tdplotsetpolarplotrange}[4]{%
	\pgfmathsetmacro{\lowerphi}{#3}
	\pgfmathsetmacro{\upperphi}{#4}
	\pgfmathsetmacro{\lowertheta}{#1}
	\pgfmathsetmacro{\uppertheta}{#2}
}

\newcommand{\tdplotresetpolarplotrange}{%
	\pgfmathsetmacro{\lowerphi}{0}
	\pgfmathsetmacro{\upperphi}{360}
	\pgfmathsetmacro{\lowertheta}{0}
	\pgfmathsetmacro{\uppertheta}{180}
}



%internal command, performs the actual rendering for the \tdplotsphericalsurfaceplot command
%TODO: find proper syntax and format for internal commands not intended to be used by the general user
\newcommand{\tdplotdosurfaceplot}[6]{%

	\pgfmathsetmacro{\nextphi}{\curphi + \tdplotsuperfudge*\viewphistep}

	\begin{scope}[opacity=1]
		%check if the current phi angle is in position to draw the x axis
		\tdplotcheckdiff{\nextphi}{360}{\origviewphistep}{#2}{}
		\tdplotcheckdiff{\nextphi}{0}{\origviewphistep}{#2}{}

		%check if the current phi angle is in position to draw the y axis
		\tdplotcheckdiff{\nextphi}{90}{\origviewphistep}{#3}{}
		\tdplotcheckdiff{\nextphi}{450}{\origviewphistep}{#3}{}
	\end{scope}

	%do the theta sweep
	\foreach \curtheta in{\viewthetastart,\viewthetainc,...,\viewthetaend}
	{

		%convert phi, theta coords into longitude, latitude to make use of pgfpointspherical coordinates
		\pgfmathsetmacro{\curlongitude}{90 - \curphi}
		\pgfmathsetmacro{\curlatitude}{90 - \curtheta}

		%If sweeping to the right, shift the value of phi to the lower value of phi.
		\ifthenelse{\equal{\leftright}{-1.0}}%
			{%
				\pgfmathsetmacro{\curphi}{\curphi - \origviewphistep}
			}{}
		%\fi

		\pgfmathsetmacro{\tdplottheta}{mod(\curtheta,360)}
		\pgfmathsetmacro{\tdplotphi}{mod(\curphi,360)}

		\pgfmathparse{\tdplotphi < 0}
		\ifthenelse{\equal{\pgfmathresult}{1.0}}{
			\pgfmathsetmacro{\tdplotphi}{\tdplotphi + 360}
		}{}%

		%test to see if this value is within the specified angular range
		\pgfmathparse{\tdplottheta > \uppertheta}
		\pgfmathsetmacro{\logictest}{1 - \pgfmathresult}
		
		\pgfmathparse{\tdplottheta < \lowertheta}
		\pgfmathsetmacro{\logictest}{\logictest * (1 - \pgfmathresult)}

		\pgfmathsetmacro{\tdplottheta}{\tdplottheta + \viewthetastep}
		\pgfmathparse{\tdplottheta > \uppertheta}
		\pgfmathsetmacro{\logictest}{\logictest * (1 - \pgfmathresult)}
		
		\pgfmathparse{\tdplottheta < \lowertheta}
		\pgfmathsetmacro{\logictest}{\logictest * (1 - \pgfmathresult)}

		\pgfmathparse{\tdplotphi > \upperphi}
		\pgfmathsetmacro{\logictest}{\logictest * (1 - \pgfmathresult)}
		
		\pgfmathparse{\tdplotphi < \lowerphi}
		\pgfmathsetmacro{\logictest}{\logictest * (1 - \pgfmathresult)}

		\pgfmathsetmacro{\tdplotphi}{\tdplotphi + \viewphistep}

		\pgfmathparse{\tdplotphi < 0}
		\ifthenelse{\equal{\pgfmathresult}{1.0}}{
			\pgfmathsetmacro{\tdplotphi}{\tdplotphi + 360}
		}{}%

		\pgfmathparse{\tdplotphi > \upperphi}
		\pgfmathsetmacro{\logictest}{\logictest * (1 - \pgfmathresult)}
		
		\pgfmathparse{\tdplotphi < \lowerphi}
		\pgfmathsetmacro{\logictest}{\logictest * (1 - \pgfmathresult)}


		\pgfmathsetmacro{\tdplottheta}{\curtheta}
		\pgfmathsetmacro{\tdplotphi}{\curphi}


		\ifthenelse{\equal{#6}{parametricfill}}{%
			\pgfmathsetmacro{\radius}{#1}
			\pgfmathlessthan{\radius}{0}
			\pgfmathsetmacro{\phaseshift}{180 * \pgfmathresult}

			\pgfmathsetmacro{\colorarg}{#5}
			\pgfmathsetmacro{\colorarg}{\colorarg + \phaseshift}
			\pgfmathsetmacro{\colorarg}{mod(\colorarg,360)}

			\pgfmathlessthan{\colorarg}{0}
			\pgfmathsetmacro{\colorarg}{\colorarg + 360*\pgfmathresult}

			\pgfmathdivide{\colorarg}{360}
			\definecolor{tdplotfillcolor}{hsb}{\pgfmathresult, 1, 1}
			\color{tdplotfillcolor}
		}%
		{%
			\pgfsetfillcolor{#5}
		}
		\pgfsetstrokecolor{#4}

		\ifthenelse{\equal{\leftright}{-1.0}}%
			{%
			\pgfmathsetmacro{\curphi}{\curphi + \origviewphistep}
			}{}
		%\fi

		\ifthenelse{\equal{\logictest}{1.0}}{%
		\pgfmathsetmacro{\radius}{abs(#1)}
		\pgfpathmoveto{\pgfpointspherical{\curlongitude}{\curlatitude}{\radius}}

		\pgfmathsetmacro{\tdplotphi}{\curphi + \viewphistep}
		\pgfmathsetmacro{\radius}{abs(#1)}
		\pgfpathlineto{\pgfpointspherical{\curlongitude - \viewphistep}{\curlatitude}{\radius}}

		\pgfmathsetmacro{\tdplottheta}{\curtheta + \viewthetastep}
		\pgfmathsetmacro{\radius}{abs(#1)}
		\pgfpathlineto{\pgfpointspherical{\curlongitude - \viewphistep}{\curlatitude - \viewthetastep}{\radius}}

		\pgfmathsetmacro{\tdplotphi}{\curphi}
		\pgfmathsetmacro{\radius}{abs(#1)}
		\pgfpathlineto{\pgfpointspherical{\curlongitude}{\curlatitude - \viewthetastep}{\radius}}
		\pgfpathclose
	
		\pgfusepath{fill,stroke}
		}{}
	}
}

%\tdplotshowargcolorguide{x position}{y position}{x size}{y size}
%#1: screen x position
%#2: screen y position
%#3: x size (susceptible to scale)
%#4: y size (susceptible to scale)
\newcommand{\tdplotshowargcolorguide}[4]{

\pgfmathsetmacro{\tdplotx}{#1}
\pgfmathsetmacro{\tdploty}{#2}
\pgfmathsetmacro{\tdplothuestep}{5}
\pgfmathsetmacro{\tdplotxsize}{#3}
\pgfmathsetmacro{\tdplotysize}{#4}

\pgfmathsetmacro{\tdplotyscale}{\tdplotysize/360}

\foreach \tdplotphi in {0,\tdplothuestep,...,360}
{
	\pgfmathdivide{\tdplotphi}{360}
	\definecolor{tdplotfillcolor}{hsb}{\pgfmathresult, 1, 1}
	\color{tdplotfillcolor}
	
	\pgfmathsetmacro{\tdplotstarty}{\tdploty + \tdplotphi * \tdplotyscale}
	\pgfmathsetmacro{\tdplotstopy}{\tdplotstarty + \tdplothuestep * \tdplotyscale}
	\pgfmathsetmacro{\tdplotstartx}{\tdplotx}
	\pgfmathsetmacro{\tdplotstopx}{\tdplotx + \tdplotxsize}
	\filldraw[tdplot_screen_coords] (\tdplotstartx,\tdplotstarty) rectangle (\tdplotstopx,\tdplotstopy);
}

	\pgfmathsetmacro{\tdplotstopy}{\tdploty + (360+\tdplothuestep)*\tdplotyscale }
	\pgfmathsetmacro{\tdplotstopx}{\tdplotx + \tdplotxsize}

\draw[tdplot_screen_coords] (\tdplotx,\tdploty) rectangle (\tdplotstopx,\tdplotstopy);

\node[tdplot_screen_coords,anchor=west,xshift=5pt] at (\tdplotstopx,\tdploty) {$0$};
\node[tdplot_screen_coords,anchor=west,xshift=5pt] at (\tdplotstopx,\tdplotstopy) {$2\pi$};

	\pgfmathsetmacro{\tdplotstopy}{\tdploty + (360+\tdplothuestep)/2*\tdplotyscale }
\node[tdplot_screen_coords,anchor=west, xshift=5pt] at (\tdplotstopx, \tdplotstopy) {$\pi$};
}


%Notes
%-----

%PROBLEM:
%the line-by-line coordinate transformation does not accept predefined points.  It only works with literal points.  Example:
%\begin{tikzpicture}[smooth]
%	\draw plot coordinates{(1,0)(2,0.5)(3,0)(3,1)};
%	\draw[x={(0cm,1cm)},y={(1cm,0cm)},color=red] plot coordinates{(1,0)(2,0.5)(3,0)(3,1)};
%\end{tikzpicture}
%
%\begin{tikzpicture}[smooth]
%	\coordinate (A) at (1,0);
%	\coordinate (B) at (2,0.5);
%	\coordinate (C) at (3,0);
%	\coordinate (D) at (3,1);
%	\draw plot coordinates{(A)(B)(C)(D)};
%	\draw[x={(0cm,1cm)},y={(1cm,0cm)},color=red] plot coordinates{(A)(B)(C)(D)};
%\end{tikzpicture}
%SOLUTION: none so far, other than use literal points.


%PROBLEM:
%	\node[tdplot_rotated_coords,anchor=south west] at (\thetavec/2:.5){$\theta$};
%it looks like the \node command can't be placed properly when a shift={} is defined in a style?
%SOLUTION:  Use something like this instead:
%\draw[tdplot_rotated_coords] (O) + (\thetavec/2:.5) node[anchor=south west]{$\theta$};


%PROBLEM:
	%\draw[-stealth,color=orange] (0,0,0) -- (xyz spherical cs:radius=.5,longitude=60,latitude=120);
%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.
%SOLUTION: none found yet.