Symbolic 3d coordinates in TikZ

Warning: answering this question may require some efforts. The purpose of question is to "teach TikZ 3d coordinates". What does that mean? If we define a coordinate in TikZ,

\path (<x>,<y>) coordinate(A);

this coordinate A gets associated with 2 lengths that specify the location. In any transformed (shifted, rotated, slanted) coordinate system, we can still refer to this coordinate and, say, draw an arrow to it. What is more important for this question, we can always work backwards and figure out what the relative location to another coordinate is, e.g. with the calc library

\path let \p1=(\$(A)-(B)\$),\n1={veclen(\x1,\y1)},\n2={atan2(\y1,\x1)} in <do something with this information>;

This is impossible in 3d since TikZ truncates the coordinates.

One possible way to deal with this has been proposed in this nice answer. This great, but does not quite work as smoothly as the above-mentioned calc syntax. Perhaps more importantly, one has to make extra efforts to store the 3d coordinates. Ideally, one would have something like

\path (x,y,z) coordinate(A);

and TikZ would remember the z coordinate as well.

Notice that this request may sound more innocent at first glance than it really is. In 2d, we have a predefined reference frame, the screen coordinates. Moreover, the rotations form an Abelian group, so it is less cumbersome to keep track of them and to invert them. The above-mentioned answer saves the coordinates in local frames, so it is impossible to compare coordinates in different frames. However, this would be instrumental for many applications, in which one switches into, say, canvas is xy plane at z=0. Ideally, an answer to this question should associate each symbolic point with some three lengths that are the coordinates in a cleverly chosen reference frame, and there should be means to determine the relative location of two points in a coordinate-independent way, similarly to veclen in 2d.

In the best of all worlds, an answer would also come with an appropriate parser that allows us to do scalar products, vector products, compute the norm of a vector and do matrix multiplications, i.e. orthogonal transformations. (I think that going beyond orthogonal transformations is a mess because then matrix inversion will be really cumbersome.) Some progress regarding parsing has been made in the answers of this question but again it is probably fair to say that this not yet as convenient as the 2d counterparts.

Answers may or may not be based on tikz-3dplot. (tikz-3dplot comes with nice orthonormal projections.) Of course, the best of all options would be something that also works with the Three point perspective library.

Note that some matrix operations have been implemented in the calculator package. It is an impressive package that many things, and its routines may be useful for the task here. Whether or not there exist other packages of this sort, I do not know.

• Been a long time since I thought about 3d coordinates in TikZ so I'm not aware of the current state of play, ages ago I proposed a system for working with them in tex.stackexchange.com/a/52627/86 I'm not sure how close it gets to what you're proposing. – Loop Space Aug 10 at 8:48
• So, in summary, you want TikZ to be able to (1) store all coordinates when a point is specified in 3D, (2) use these for computations and (3) apply affine transformations (or just Euclidean transformations) to 3D points? That sounds like it'd be very useful, and a lot of work indeed… – Circumscribe Aug 11 at 8:14
• I don't really understand why choosing a reference frame might be tricky or how the fact that rotations in 3D do not commute would a problem, however. The sensible way to keep track of transformations would probably be by storing a 3×3 transformation matrix and 3D vector for translations and to then perform the appropriate matrix multiplications whenever another transformation is applied. That's also how 2D transformations currently work (but the matrix is 2×2-and the vector has just 2 components). – Circumscribe Aug 11 at 8:18
• @Circumscribe Yes, exactly. Ideally there are already matrix multiplications implemented somewhere. Orthogonal should be sufficient because inverting the others with LaTeX may be too messy. One way to rephrase the question: imagine we draw two points in two different frames (e.g. \tdplotsetrotatedcoords) and I want to ask TikZ what the 3d distance (NOT the distance on the screen!) is, and differences between coordinate values in either of the frames (or another frame). – user121799 Aug 11 at 9:28
• I plan to integrate tikz-3dplot into PGF/TikZ at some point in the future (no ETA) with a better interface. However, I would have to contact the author of tikz-3dplot first and ask for permission. – Henri Menke Aug 13 at 0:19