在 tikz 3d 中转换节点坐标?

在 tikz 3d 中转换节点坐标?

考虑一下这个MWE:

\documentclass[%
12pt,
journal,
onecolumn,
twoside,
draftcls,
letterpaper,
]{IEEEtran}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{3d}
\usepackage{adjustbox}
\usepackage{setspace} %\singlespacing
\usepackage{caption}
\usepackage[nopar]{lipsum}
\usepackage{xstring}
% http://tex.stackexchange.com/a/26808/2595
\makeatletter
\def\unpacklipsum#1#2#3{%
  \count@=#1\relax
  \advance\count@\m@ne
  \def#3{}%
  \loop\ifnum\count@<#2\relax
    \advance\count@\@ne
    \edef#3{#3\csname lipsum@\romannumeral\count@\endcsname}%
  \repeat}
\makeatother
% http://tex.stackexchange.com/a/168754/2595
\def\loremnchars[#1]#2{%
  \unpacklipsum{#1}{#1}{\myunpacked}%
  \StrMid{\myunpacked}{1}{#2}% same as \StrLeft{\myunpacked}{#2}
}

\begin{document}

\loremnchars[1]{255} ...

\begin{center}
\singlespacing
\begin{adjustbox}{width=0.9\textwidth}
\def\angl{70}
\begin{tikzpicture}[
  x={({cos(\angl)*1cm},{sin(\angl)*1cm})},y={(1.0cm,0cm)},z={(0,1cm)},
  every node/.append style={
    %xslant=0.0,yslant=0.0,
    transform shape,
  },
]

  \tikzstyle{rr} = [draw,fill=gray,opacity=0.5];

  \begin{scope}[canvas is yx plane at z=0.0]
    \draw[rr] (0,0) rectangle (15,27);
    \node[draw,align=left,anchor=south west] (aa) at (3,3.5) {\loremnchars[6]{60} \\ \loremnchars[7]{60}};
  \end{scope}

  \begin{scope}[canvas is yx plane at z=2.0]
    \draw[rr] (0,0) rectangle (15,27);
    \node[] (tdrag) at (7,24) {};
    \begin{scope}[shift={(tdrag)},
    rotate=180,
    anchor=center,
    transform shape,
    ]
      \node[draw,align=left] {\loremnchars[2]{60} \\ \loremnchars[3]{60}};
    \end{scope}
  \end{scope}

  \begin{scope}[canvas is yx plane at z=4.0]
    \draw[rr] (0,0) rectangle (15,27);
    \node[draw,align=left,anchor=south west] (bb) at (3,5.5) {\loremnchars[8]{60} \\ \loremnchars[9]{60}};
  \end{scope}

% \draw[dashed,line width=2pt] (aa) -- (bb);
\draw[dashed,line width=2pt] let \p1=(aa), \p2=(bb) in (\x1,\y1) -- (\x2,\y2);      %L2
% \draw[dashed,line width=10pt] let \p1=(aa), \p2=(bb) in (\x1,\y1,0) -- (\x2,\y2,4); %L3

\end{tikzpicture}
\end{adjustbox}
\captionof{figure}[short]{
A short message; \loremnchars[5]{255} ...
}
\label{fig:test}
\end{center}

\end{document}

当我启用该行进行编译时%L2(如在 MWE 中),我得到了预期的结果(单击可查看完整尺寸):

测试21

但是,我想要一条连接线,从 开始(aa.west),然后在同一平面(z=0)上向左移动 5 pt,然后向上到 z=4,然后在同一平面上向左移动到(bb.west)

所以我想我只需使用let语法,从节点锚点中提取 x、y 坐标,然后简单地添加 z 坐标(以及中间点);但是简单地添加 z 坐标 - 如果取消注释行%L3和注释%L2- 就会绘制出一些奇怪的东西:

测试22

这条线似乎偏离了方向——我认为问题在于:坐标\x1用 表示pt,它们是转型坐标(最终在 2D 纸上);但由于某种原因,在 3D 节点使用中,它们可能仍然被解释为cm(如果我没记错的话,是默认的 TikZ 单位)。

无论原因是什么 - 有没有什么方法可以使用,以便我可以“转换”\x1\y1坐标,比如(aa.east)在“3D 坐标系”中,这样​​我就可以直接使用它们和手动指定的 Z 坐标?


编辑:显然这与 cm 转换有关;使用下面的代码(或多或少通过暴力破解找到),我得到了接近我想要的东西:

测试23

请注意,我必须将其转换为厘米,并反转 y 和 x 坐标才能获得此图像,这让我很困惑。但它似乎不太正确 - 节点(bb)应该直接位于上方(aa);但如果我沿着连接线,它看起来(bb)也在 y 轴上位移了??所以,还是希望得到一个正确的答案...

\documentclass[%
12pt,
journal,
onecolumn,
twoside,
draftcls,
letterpaper,
]{IEEEtran}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{3d}
\usepackage{adjustbox}
\usepackage{setspace} %\singlespacing
\usepackage{caption}
\usepackage[nopar]{lipsum}
\usepackage{xstring}
% http://tex.stackexchange.com/a/26808/2595
\makeatletter
\def\unpacklipsum#1#2#3{%
  \count@=#1\relax
  \advance\count@\m@ne
  \def#3{}%
  \loop\ifnum\count@<#2\relax
    \advance\count@\@ne
    \edef#3{#3\csname lipsum@\romannumeral\count@\endcsname}%
  \repeat}
\makeatother
% http://tex.stackexchange.com/a/168754/2595
\def\loremnchars[#1]#2{%
  \unpacklipsum{#1}{#1}{\myunpacked}%
  \StrMid{\myunpacked}{1}{#2}% same as \StrLeft{\myunpacked}{#2}
}

\makeatletter
% http://tex.stackexchange.com/questions/33703/extract-x-y-
\newcommand{\gettikzxy}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \global\edef#2{\the\pgf@x}%
  \global\edef#3{\the\pgf@y}%
  \typeout{ gettikzxy: x #2, y #3}
}
% http://tex.stackexchange.com/a/8337/2595
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}
\makeatother

\begin{document}

\loremnchars[1]{255} ...

\begin{center}
\singlespacing
\begin{adjustbox}{width=0.9\textwidth}
\def\angl{70}
\begin{tikzpicture}[
  x={({cos(\angl)*1cm},{sin(\angl)*1cm})},y={(1.0cm,0cm)},z={(0,1cm)},
  every node/.append style={
    %xslant=0.0,yslant=0.0,
    transform shape,
  },
]

  \tikzstyle{rr} = [draw,fill=gray,opacity=0.5];

  \begin{scope}[canvas is yx plane at z=0.0]
    \draw[rr] (0,0) rectangle (15,27);
    \node[draw,align=left,anchor=south west] (aa) at (3,3.5) {\loremnchars[6]{60} \\ \loremnchars[7]{60}};
    \gettikzxy{(aa.south west)}{\ax}{\ay}
  \end{scope}

  \begin{scope}[canvas is yx plane at z=2.0]
    \draw[rr] (0,0) rectangle (15,27);
    \node[] (tdrag) at (7,24) {};
    \begin{scope}[shift={(tdrag)},
    rotate=180,
    anchor=center,
    transform shape,
    ]
      \node[draw,align=left] {\loremnchars[2]{60} \\ \loremnchars[3]{60}};
    \end{scope}
  \end{scope}

  \begin{scope}[canvas is yx plane at z=4.0]
    \draw[rr] (0,0) rectangle (15,27);
    \node[draw,align=left,anchor=south west] (bb) at (3,5.5) {\loremnchars[8]{60} \\ \loremnchars[9]{60}};
    \gettikzxy{(bb.south west)}{\bx}{\by}
  \end{scope}

% \draw[dashed,line width=2pt] (aa) -- (bb);
% \draw[dashed,line width=2pt] let \p1=(aa), \p2=(bb) in (\x1,\y1) -- (\x2,\y2);      %L2
% \draw[dashed,line width=10pt] let \p1=(aa), \p2=(bb) in (\x1,\y1,0) -- (\x2,\y2,4); %L3

% \gettikzxy{(aa.south west)}{\ax}{\ay} % not here
\edef\cax{\convertto{cm}{\ax}}
\edef\cay{\convertto{cm}{\ay}}
\typeout{ : cax \cax, cay \cay }
\edef\cbx{\convertto{cm}{\bx}}
\edef\cby{\convertto{cm}{\by}}
\typeout{ : cbx \cbx, cby \cby }
\draw[dashed,line width=5pt] let
  \p1=(\cay,\cax,0),
  \p2=($(\p1)-(0pt,1pt,0)$),
  \p3=($(\p2)+(0,0,2)$),
  \p4=($(\p2)+(0,0,4)$),
  \p6=(\cby,\cbx,4),
  \p5=($(\p6)-(0,1pt,0)$)
  in
  (\p1) -- (\p2) node[fill]{} -- (\p3) node[fill]{} -- (\p4) node[fill]{} -- (\p5) -- (\p6) ;

\end{tikzpicture}
\end{adjustbox}
\captionof{figure}[short]{
A short message; \loremnchars[5]{255} ...
}
\label{fig:test}
\end{center}

\end{document}

答案1

是这样的吗?

在此处输入图片描述

这是通过

\draw[dashed,line width=2pt]  (aa.west) -- ++(-15pt,0)  --  ++ (0,0,4)  
                       -- ([xshift=-15pt]bb.west) -- (bb.west); 

代码:

\documentclass[%
12pt,
journal,
onecolumn,
twoside,
draftcls,
letterpaper,
]{IEEEtran}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{3d}
\usepackage{adjustbox}
\usepackage{setspace} %\singlespacing
\usepackage{capt-of}
\usepackage[nopar]{lipsum}
\usepackage{xstring}
% http://tex.stackexchange.com/a/26808/2595
\makeatletter
\def\unpacklipsum#1#2#3{%
  \count@=#1\relax
  \advance\count@\m@ne
  \def#3{}%
  \loop\ifnum\count@<#2\relax
    \advance\count@\@ne
    \edef#3{#3\csname lipsum@\romannumeral\count@\endcsname}%
  \repeat}
\makeatother
% http://tex.stackexchange.com/a/168754/2595
\def\loremnchars[#1]#2{%
  \unpacklipsum{#1}{#1}{\myunpacked}%
  \StrMid{\myunpacked}{1}{#2}% same as \StrLeft{\myunpacked}{#2}
}

\begin{document}

\loremnchars[1]{255} ...

\begin{center}
\singlespacing
\begin{adjustbox}{width=0.9\textwidth}
\def\angl{70}
\begin{tikzpicture}[
  x={({cos(\angl)*1cm},{sin(\angl)*1cm})},y={(1.0cm,0cm)},z={(0,1cm)},
  every node/.append style={
    %xslant=0.0,yslant=0.0,
    transform shape,
  },
]

  \tikzstyle{rr} = [draw,fill=gray,opacity=0.5];

  \begin{scope}[canvas is yx plane at z=0.0]
    \draw[rr] (0,0) rectangle (15,27);
    \node[draw,align=left,anchor=south west] (aa) at (3,3.5) {\loremnchars[6]{60} \\ \loremnchars[7]{60}};
  \end{scope}

  \begin{scope}[canvas is yx plane at z=2.0]
    \draw[rr] (0,0) rectangle (15,27);
    \node[] (tdrag) at (7,24) {};
    \begin{scope}[shift={(tdrag)},
    rotate=180,
    anchor=center,
    transform shape,
    ]
      \node[draw,align=left] {\loremnchars[2]{60} \\ \loremnchars[3]{60}};
    \end{scope}
  \end{scope}

  \begin{scope}[canvas is yx plane at z=4.0]
    \draw[rr] (0,0) rectangle (15,27);
    \node[draw,align=left,anchor=south west] (bb) at (3,5.5) {\loremnchars[8]{60} \\ \loremnchars[9]{60}};
  \end{scope}

% \draw[dashed,line width=2pt] (aa) -- (bb);
%\draw[line width=2pt] let \p1=(aa), \p2=(bb) in (\x1,\y1) ++(0,-50pt)  -- (\x2,\y2);      %L2
% \draw[dashed,line width=10pt] let \p1=(aa), \p2=(bb) in (\x1,\y1,0) -- (\x2,\y2,4); %L3
\draw[dashed,line width=2pt]  (aa.west) -- ++(-15pt,0)  --  ++ (0,0,4)  -- ([xshift=-15pt]bb.west) -- (bb.west);      %L2

\end{tikzpicture}
\end{adjustbox}
\captionof{figure}[short]{
A short message; \loremnchars[5]{255} ...
}
\label{fig:test}
\end{center}

\end{document}

另外,请注意,caption包与类不兼容 IEEEtran。要获取\captionof宏,您可以capt-of像我一样使用包。

相关内容