1

In the following TikZ figure:

\documentclass[border=5pt]{standalone}

\usepackage{tikz} \usetikzlibrary{3d}

\makeatletter \def\tikz@lib@cuboid@get#1{\pgfkeysvalueof{/tikz/cuboid/#1}}

\def\tikz@lib@cuboid@setup{% \pgfmathsetlengthmacro{\vxx}% {\tikz@lib@cuboid@get{xscale}cos(\tikz@lib@cuboid@get{xangle})1cm} \pgfmathsetlengthmacro{\vxy}% {\tikz@lib@cuboid@get{xscale}sin(\tikz@lib@cuboid@get{xangle})1cm} \pgfmathsetlengthmacro{\vyx}% {\tikz@lib@cuboid@get{yscale}cos(\tikz@lib@cuboid@get{yangle})1cm} \pgfmathsetlengthmacro{\vyy}% {\tikz@lib@cuboid@get{yscale}sin(\tikz@lib@cuboid@get{yangle})1cm} \pgfmathsetlengthmacro{\vzx}% {\tikz@lib@cuboid@get{zscale}cos(\tikz@lib@cuboid@get{zangle})1cm} \pgfmathsetlengthmacro{\vzy}% {\tikz@lib@cuboid@get{zscale}sin(\tikz@lib@cuboid@get{zangle})1cm} }

\def\tikz@lib@cuboid@draw#1--#2--#3\pgf@stop{% \begin{scope}[join=bevel,x={(\vxx,\vxy)},y={(\vyx,\vyy)},z={(\vzx,\vzy)}] % first fill the faces with global and individual style % then draw the grids \begin{scope}[canvas is yz plane at x=#1] \draw[cuboid/all faces,cuboid/edges,cuboid/right face] (0,0) -- ++(#2,0) -- ++(0,-#3) -- ++(-#2,0) -- cycle; \draw[cuboid/all grids,cuboid/right grid] (0,0) grid (#2,-#3); \end{scope} \begin{scope}[canvas is xy plane at z=0] \draw[cuboid/all faces,cuboid/edges,cuboid/front face] (0,0) -- ++(#1,0) -- ++(0,#2) -- ++(-#1,0) -- cycle; \draw[cuboid/all grids,cuboid/front grid] (0,0) grid (#1,#2); \end{scope} \begin{scope}[canvas is xz plane at y=#2] \draw[cuboid/all faces,cuboid/edges,cuboid/top face] (0,0) -- ++(#1,0) -- ++(0,-#3) -- ++(-#1,0) -- cycle; \draw[cuboid/all grids,cuboid/top grid] (0,0) grid (#1,-#3); \end{scope} % now, draw the hidden edges \draw[cuboid/hidden edges] (0,#2,-#3) -- (0,0,-#3) -- (0,0,0) (0,0,-#3) -- ++(#1,0,0); % finally, draw the visible edges \begin{scope}[canvas is yz plane at x=#1] \draw[cuboid/all faces,cuboid/right face,cuboid/edges,fill opacity=0] (0,0) -- ++(#2,0) -- ++(0,-#3) -- ++(-#2,0) -- cycle; \end{scope} \begin{scope}[canvas is xy plane at z=0] \draw[cuboid/all faces,cuboid/front face,cuboid/edges,fill opacity=0] (0,0) -- ++(#1,0) -- ++(0,#2) -- ++(-#1,0) -- cycle; \end{scope} \begin{scope}[canvas is xz plane at y=#2] \draw[cuboid/all faces,cuboid/top face,cuboid/edges,fill opacity=0] (0,0) -- ++(#1,0) -- ++(0,-#3) -- ++(-#1,0) -- cycle; \end{scope} % define the anchors: 8 vertices \path (0,#2,0) coordinate (-left top front) coordinate (-left front top) coordinate (-top left front) coordinate (-top front left) coordinate (-front top left) coordinate (-front left top); \path (0,#2,-#3) coordinate (-left top rear) coordinate (-left rear top) coordinate (-top left rear) coordinate (-top rear left) coordinate (-rear top left) coordinate (-rear left top); \path (0,0,-#3) coordinate (-left bottom rear) coordinate (-left rear bottom) coordinate (-bottom left rear) coordinate (-bottom rear left) coordinate (-rear bottom left) coordinate (-rear left bottom); \path (0,0,0) coordinate (-left bottom front) coordinate (-left front bottom) coordinate (-bottom left front) coordinate (-bottom front left) coordinate (-front bottom left) coordinate (-front left bottom); \path (#1,#2,0) coordinate (-right top front) coordinate (-right front top) coordinate (-top right front) coordinate (-top front right) coordinate (-front top right) coordinate (-front right top); \path (#1,#2,-#3) coordinate (-right top rear) coordinate (-right rear top) coordinate (-top right rear) coordinate (-top rear right) coordinate (-rear top right) coordinate (-rear right top); \path (#1,0,-#3) coordinate (-right bottom rear) coordinate (-right rear bottom) coordinate (-bottom right rear) coordinate (-bottom rear right) coordinate (-rear bottom right) coordinate (-rear right bottom); \path (#1,0,0) coordinate (-right bottom front) coordinate (-right front bottom) coordinate (-bottom right front) coordinate (-bottom front right) coordinate (-front bottom right) coordinate (-front right bottom); % centers of the 6 faces \coordinate (-left center) at (0,.5#2,-.5#3); \coordinate (-right center) at (#1,.5#2,-.5#3); \coordinate (-top center) at (.5#1,#2,-.5#3); \coordinate (-bottom center) at (.5#1,0,-.5#3); \coordinate (-front center) at (.5#1,.5#2,0); \coordinate (-rear center) at (.5#1,.5#2,-#3); % center of the cuboid \coordinate (-center) at (.5#1,.5#2,-.5#3); % centers of the 12 edges \path (0,#2,-.5#3) coordinate (-left top center) coordinate (-top left center); \path (.5#1,#2,-#3) coordinate (-top rear center) coordinate (-rear top center); \path (#1,#2,-.5#3) coordinate (-right top center) coordinate (-top right center); \path (.5#1,#2,0) coordinate (-top front center) coordinate (-front top center); \path (0,0,-.5#3) coordinate (-left bottom center) coordinate (-bottom left center); \path (.5#1,0,-#3) coordinate (-bottom rear center) coordinate (-rear bottom center); \path (#1,0,-.5#3) coordinate (-right bottom center) coordinate (-bottom right center); \path (.5#1,0,0) coordinate (-bottom front center) coordinate (-front bottom center); \path (0,.5#2,0) coordinate (-left front center) coordinate (-front left center); \path (0,.5#2,-#3) coordinate (-left rear center) coordinate (-rear left center); \path (#1,.5#2,0) coordinate (-right front center) coordinate (-front right center); \path (#1,.5*#2,-#3) coordinate (-right rear center) coordinate (-rear right center); \end{scope} }

\tikzset{ pics/cuboid/.style = { setup code = \tikz@lib@cuboid@setup, background code = \tikz@lib@cuboid@draw#1\pgf@stop }, pics/cuboid/.default={1--1--1}, cuboid/.is family, cuboid, all faces/.style={fill=white}, all grids/.style={draw=none}, front face/.style={}, front grid/.style={}, right face/.style={}, right grid/.style={}, top face/.style={}, top grid/.style={}, edges/.style={}, hidden edges/.style={draw=none}, xangle/.initial=0, yangle/.initial=90, zangle/.initial=210, xscale/.initial=1, yscale/.initial=1, zscale/.initial=0.5 }

\newcommand{\tikzcuboidreset}{ \tikzset{cuboid, all faces/.style={fill=white}, all grids/.style={draw=none}, front face/.style={}, front grid/.style={}, right face/.style={}, right grid/.style={}, top face/.style={}, top grid/.style={}, edges/.style={}, hidden edges/.style={draw=none}, xangle=-20, yangle=90, zangle=180, xscale=1, yscale=1, zscale=0.5 } }

\newcommand{\tikzcuboidset}{@ifstar\tikzcuboidset@star\tikzcuboidset@nostar} \newcommand{\tikzcuboidset@nostar}[1]{\tikzcuboidreset\tikzset{cuboid,#1}} \newcommand{\tikzcuboidset@star}[1]{\tikzset{cuboid,#1}} \makeatother

\begin{document} \begin{tikzpicture} \begin{scope} \node[black, draw, very thick, shift={(0,1)}, minimum height=1cm, minimum width=2cm] (R) {\Large Reader};

           \tikzcuboidset{hidden edges/.style={dashed}}
           \pic[very thick,black] (cuboid) at (4,0,0) {cuboid=3--3--3};

          \node[draw, red,fill=red,thick, minimum width=0.1cm, scale=0.75,  minimum height=0.5cm,label={[font=\large,text=red]20:A}] (A) at ([shift={(-1, 1)}] cuboid-front center)  {};
          \node[draw, red,fill=red,thick, minimum width=0.1cm, scale=0.75,  minimum height=0.5cm,label={[font=\large,text=red]-5:C}] (C) at ([shift={(-1, -0.5)}] cuboid-front center)  {};
          \node[draw, red,fill=red,thick, minimum width=0.1cm, scale=0.75,  minimum height=0.5cm,label={[font=\large,text=red]90:B}] (B) at ([shift={(1, -.2)}] cuboid-front center)  {};

          \draw[-latex, dashed, blue, very thick] (R.east) -- (A) node[pos=0.5, above] {\large{$d_1$}};
          \draw[-latex, dashed, blue, very thick] (R.east) -- (B) node[pos=0.4, above] {\large{$d_2$}};
          \draw[-latex, dashed, blue, very thick] (R.east) -- (C) node[pos=0.5, below] {\large{$d_3$}};
   \end{scope}


   \begin{scope}[shift={(0,-5)}]
     \node[black, draw, very thick, shift={(0,1)}, minimum height=1cm, minimum width=2cm] (R) {\Large Reader};
 \tikzcuboidset{hidden edges/.style={dashed}}
 \pic[very thick,black] (cuboid) at (4,0,0) {cuboid=3--1.3--6};

 \node[draw, red,fill=red,thick, minimum width=0.1cm, scale=0.75,  rotate =90, minimum height=0.5cm,label={[font=\large,text=red]20:A}] (A) at ([shift={(-1, -0)}] cuboid-top center)  {};
   \end{scope}

\end{tikzpicture} \end{document}

That results in the figure

enter image description here

I have used the solution of this previous post TikZ: Easy drawing of cuboids.

However, the rectangles A, B, and C do not have the perspective of the cuboid side where they are. Also, in the cuboid at the bottom is difficult to place those rectangles on the top face of the cuboid.

And, in the second cuboid, I would like a position like in this figure:

enter image description here

which is the scheme I would like to make.

Also, I do not understand the dimensions of the cuboid. The top cuboid is {cuboid=3--3--3} and the second cuboid {cuboid=3--1.3--6}, and I am not sure the two cuboids are the same but in different positions with respect to the Reader.

pablo
  • 451

1 Answers1

2

That's a lot of code and coordinates for a cuboid.

What you need though, is a coordinate system that lies on a face of the cuboid.

For this example all your red rectangles are on the front side so I install a coordinate system that has its origin in the bottom left corner and x goes to the bottom right corner and y goes to the top left corner.

This is what the key cuboid face does when given the name of the pic with the given cuboid:

\tikzset{
  cuboid face/.style={
    shift=(#1-left bottom front),
    x=(#1-right bottom front),
    y=(#1-left top front),
    canvas is xy plane at z=0,
  }
}

You can create these keys for all six faces, of course, but I couldn't really follow the naming scheme.

Code

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{3d}
\makeatletter
\def\tikz@lib@cuboid@get#1{\pgfkeysvalueof{/tikz/cuboid/#1}}
\def\tikz@lib@cuboid@setup{%
   \pgfmathsetlengthmacro{\vxx}%
      {\tikz@lib@cuboid@get{xscale}*cos(\tikz@lib@cuboid@get{xangle})*1cm}
   \pgfmathsetlengthmacro{\vxy}%
      {\tikz@lib@cuboid@get{xscale}*sin(\tikz@lib@cuboid@get{xangle})*1cm}
   \pgfmathsetlengthmacro{\vyx}%
      {\tikz@lib@cuboid@get{yscale}*cos(\tikz@lib@cuboid@get{yangle})*1cm}
   \pgfmathsetlengthmacro{\vyy}%
      {\tikz@lib@cuboid@get{yscale}*sin(\tikz@lib@cuboid@get{yangle})*1cm}
   \pgfmathsetlengthmacro{\vzx}%
      {\tikz@lib@cuboid@get{zscale}*cos(\tikz@lib@cuboid@get{zangle})*1cm}
   \pgfmathsetlengthmacro{\vzy}%
      {\tikz@lib@cuboid@get{zscale}*sin(\tikz@lib@cuboid@get{zangle})*1cm}
}
\def\tikz@lib@cuboid@draw#1--#2--#3\pgf@stop{%
    \begin{scope}[join=bevel,x={(\vxx,\vxy)},y={(\vyx,\vyy)},z={(\vzx,\vzy)}]
       % first fill the faces with global and individual style
       % then draw the grids
       \begin{scope}[canvas is yz plane at x=#1]
          \draw[cuboid/all faces,cuboid/edges,cuboid/right face] 
                (0,0) -- ++(#2,0) -- ++(0,-#3) -- ++(-#2,0) -- cycle;
          \draw[cuboid/all grids,cuboid/right grid] (0,0) grid (#2,-#3);
       \end{scope}
       \begin{scope}[canvas is xy plane at z=0]
          \draw[cuboid/all faces,cuboid/edges,cuboid/front face] 
                (0,0) -- ++(#1,0) --  ++(0,#2) -- ++(-#1,0) -- cycle;
          \draw[cuboid/all grids,cuboid/front grid] (0,0) grid (#1,#2);
       \end{scope}
       \begin{scope}[canvas is xz plane at y=#2]
          \draw[cuboid/all faces,cuboid/edges,cuboid/top face] 
                (0,0) -- ++(#1,0) --  ++(0,-#3) -- ++(-#1,0) -- cycle;
          \draw[cuboid/all grids,cuboid/top grid] (0,0) grid (#1,-#3);
       \end{scope}
       % now, draw the hidden edges
       \draw[cuboid/hidden edges] (0,#2,-#3) -- (0,0,-#3) -- (0,0,0) 
                (0,0,-#3) -- ++(#1,0,0);
       % finally, draw the visible edges 
       \begin{scope}[canvas is yz plane at x=#1]
          \draw[cuboid/all faces,cuboid/right face,cuboid/edges,fill opacity=0] 
                (0,0) -- ++(#2,0) -- ++(0,-#3) -- ++(-#2,0) -- cycle;
       \end{scope}
       \begin{scope}[canvas is xy plane at z=0]
          \draw[cuboid/all faces,cuboid/front face,cuboid/edges,fill opacity=0] 
                (0,0) -- ++(#1,0) --  ++(0,#2) -- ++(-#1,0) -- cycle;
       \end{scope}
       \begin{scope}[canvas is xz plane at y=#2]
          \draw[cuboid/all faces,cuboid/top face,cuboid/edges,fill opacity=0] 
                (0,0) -- ++(#1,0) --  ++(0,-#3) -- ++(-#1,0) -- cycle;
       \end{scope}
       % define the anchors: 8 vertices
       \path (0,#2,0) coordinate (-left top front)
                      coordinate (-left front top)
                      coordinate (-top left front)
                      coordinate (-top front left)
                      coordinate (-front top left)
                      coordinate (-front left top);
       \path (0,#2,-#3) coordinate (-left top rear)
                        coordinate (-left rear top)
                        coordinate (-top left rear)
                        coordinate (-top rear left)
                        coordinate (-rear top left)
                        coordinate (-rear left top);
       \path (0,0,-#3) coordinate (-left bottom rear)
                       coordinate (-left rear bottom)
                       coordinate (-bottom left rear)
                       coordinate (-bottom rear left)
                       coordinate (-rear bottom left)
                       coordinate (-rear left bottom);
       \path (0,0,0) coordinate (-left bottom front)
                     coordinate (-left front bottom)
                     coordinate (-bottom left front)
                     coordinate (-bottom front left)
                     coordinate (-front bottom left)
                     coordinate (-front left bottom);
       \path (#1,#2,0) coordinate (-right top front)
                       coordinate (-right front top)
                       coordinate (-top right front)
                       coordinate (-top front right)
                       coordinate (-front top right)
                       coordinate (-front right top);
       \path (#1,#2,-#3) coordinate (-right top rear)
                         coordinate (-right rear top)
                         coordinate (-top right rear)
                         coordinate (-top rear right)
                         coordinate (-rear top right)
                         coordinate (-rear right top);
       \path (#1,0,-#3) coordinate (-right bottom rear)
                        coordinate (-right rear bottom)
                        coordinate (-bottom right rear)
                        coordinate (-bottom rear right)
                        coordinate (-rear bottom right)
                        coordinate (-rear right bottom);
       \path (#1,0,0) coordinate (-right bottom front)
                      coordinate (-right front bottom)
                      coordinate (-bottom right front)
                      coordinate (-bottom front right)
                      coordinate (-front bottom right)
                      coordinate (-front right bottom);
       % centers of the 6 faces
       \coordinate (-left center) at (0,.5*#2,-.5*#3);
       \coordinate (-right center) at (#1,.5*#2,-.5*#3);
       \coordinate (-top center) at (.5*#1,#2,-.5*#3);
       \coordinate (-bottom center) at (.5*#1,0,-.5*#3);
       \coordinate (-front center) at (.5*#1,.5*#2,0);
       \coordinate (-rear center) at (.5*#1,.5*#2,-#3);
       % center of the cuboid
       \coordinate (-center) at (.5*#1,.5*#2,-.5*#3);
       % centers of the 12 edges
       \path (0,#2,-.5*#3) coordinate (-left top center) 
                           coordinate (-top left center);
       \path (.5*#1,#2,-#3) coordinate (-top rear center)
                            coordinate (-rear top center);
       \path (#1,#2,-.5*#3) coordinate (-right top center)
                            coordinate (-top right center);
       \path (.5*#1,#2,0) coordinate (-top front center)
                          coordinate (-front top center);
       \path (0,0,-.5*#3) coordinate (-left bottom center) 
                           coordinate (-bottom left center);
       \path (.5*#1,0,-#3) coordinate (-bottom rear center)
                            coordinate (-rear bottom center);
       \path (#1,0,-.5*#3) coordinate (-right bottom center)
                            coordinate (-bottom right center);
       \path (.5*#1,0,0) coordinate (-bottom front center)
                          coordinate (-front bottom center);
       \path (0,.5*#2,0) coordinate (-left front center) 
                           coordinate (-front left center);
       \path (0,.5*#2,-#3) coordinate (-left rear center)
                            coordinate (-rear left center);
       \path (#1,.5*#2,0) coordinate (-right front center)
                            coordinate (-front right center);
       \path (#1,.5*#2,-#3) coordinate (-right rear center)
                          coordinate (-rear right center);
    \end{scope}
}
\tikzset{
  pics/cuboid/.style = {
    setup code = \tikz@lib@cuboid@setup,
    background code = \tikz@lib@cuboid@draw#1\pgf@stop
  },
  pics/cuboid/.default={1--1--1},
  cuboid/.is family,
  cuboid,
  all faces/.style={fill=white},
  all grids/.style={draw=none},
  front face/.style={},
  front grid/.style={},
  right face/.style={},
  right grid/.style={},
  top face/.style={},
  top grid/.style={},
  edges/.style={},
  hidden edges/.style={draw=none},
  xangle/.initial=0,
  yangle/.initial=90,
  zangle/.initial=210,
  xscale/.initial=1,
  yscale/.initial=1,
  zscale/.initial=0.5}
\newcommand{\tikzcuboidreset}{
  \tikzset{cuboid,
    all faces/.style={fill=white},
    all grids/.style={draw=none},
    front face/.style={},
    front grid/.style={},
    right face/.style={},
    right grid/.style={},
    top face/.style={},
    top grid/.style={},
    edges/.style={},
    hidden edges/.style={draw=none},
    xangle=-20,
    yangle=90,
    zangle=180,
    xscale=1,
    yscale=1,
    zscale=0.5}}
\newcommand{\tikzcuboidset}{\@ifstar\tikzcuboidset@star\tikzcuboidset@nostar} 
\newcommand{\tikzcuboidset@nostar}[1]{\tikzcuboidreset\tikzset{cuboid,#1}}
\newcommand{\tikzcuboidset@star}[1]{\tikzset{cuboid,#1}}
\makeatother
\tikzset{
  cuboid face/.style={
    shift=(#1-left bottom front),
    x=(#1-right bottom front),
    y=(#1-left top front),
    canvas is xy plane at z=0,
  }
}
\begin{document}
\begin{tikzpicture}
\node[black, draw, very thick, shift={(0,1)}, minimum height=1cm, minimum width=2cm] (R) {\Large Reader};

\tikzcuboidset{hidden edges/.style={dashed}} \pic[very thick,black] (cuboid) at (4,0,0) {cuboid=3--3--3};

\foreach[count=\i] \p/\t in {(.3, .1)/A, (.7, .6)/B, (.25, .7)/C} \fill[red, cuboid face=cuboid] \p rectangle coordinate (\t) +(.05,.1) coordinate (@) (\t) node[above, scale=.4, transform shape] at (\t|-@) {\t} (R.east) edge[blue, dashed, very thick, -latex] node[above]{$d_\i$} (\t); \end{tikzpicture} \end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821