How to draw circle passing through three points in 3D?

How to draw circle passing through three points A,P, Q in this code.

\documentclass[border=3mm,12pt,tikz]{standalone}
\usepackage{fouriernc}
\usepackage{tikz,tikz-3dplot}
\tikzset{projection of point/.style args={(#1,#2,#3) on line through (#4,#5,#6)
and (#7,#8,#9)}{%
/utils/exec=\pgfmathsetmacro{\myprefactor}{((#1-#4)*(#7-#4)+(#2-#5)*(#8-#5)+(#3-#6)*(#9-#6))/((#7-#4)*(#7-#4)+(#8-#5)*(#8-#5)+(#9-#6)*(#9-#6))},
insert path={%
({#4+\myprefactor*(#7-#4)},{#5+\myprefactor*(#8-#5)},{#6+\myprefactor*(#9-#6)})}
}}
\begin{document}
\tdplotsetmaincoords{70}{110}
\begin{tikzpicture}[tdplot_main_coords]
\pgfmathsetmacro\a{4}
\pgfmathsetmacro\b{8}
\pgfmathsetmacro\h{7}
\pgfmathsetmacro\r{sqrt(\a*\a + \b*\b)/2}
% definitions
\path
coordinate(A) at (0,0,0)
coordinate (B) at (\a,0,0)
coordinate (C) at (\a,\b,0)
coordinate (D) at (0,\b,0)
coordinate (S) at (0,0,\h)
coordinate (I) at (\a/2,\b/2,0)   ;

\path[projection of point={(0,0,0) on line through (\a,0,0) and (0,0,\h)}]
coordinate  (P)
[projection of point={(0,0,0) on line through (0,\b,0) and (0,0,\h)}]
coordinate (Q);

\draw[dashed] (S) -- (A) (P) -- (B) (Q) --(D) (A) -- (C) (B) -- (D)
(A) -- (B) -- (C) -- (D)--cycle (P) -- (Q);
\draw (S) -- (P) (S) -- (Q) (S) -- (C);
\draw (I) circle (\r cm);
\draw[red, thick, dashed]  (A)-- (P) (A)-- (Q) ;
\foreach \point/\position in {A/left,B/left,C/below,S/above,D/right,I/left,P/left,Q/right}
{\fill (\point) circle (1.5pt);
\node[\position=3pt] at (\point) {$\point$};
}
\end{tikzpicture}
\end{document}


You can use 3dtools. The function 3d/circle on sphere can solve your question.

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{3dtools}
\tikzset{reverseclip/.style={insert path={(current bounding box.south west)rectangle (current bounding box.north east)} }}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,c/.style={circle,fill,inner sep=1pt},
3d/install view={phi=120,theta=65},declare function={a=4;b=8;h=7;}]
\path
(0,0,0) coordinate (A)
(a,0,0) coordinate (B)
(a,b,0) coordinate (C)
(0,b,0) coordinate (D)
(0,0,h) coordinate (S)
(a/2,b/2,0) coordinate (I);
\path[3d/line through={(S) and (B) named lSB},3d/line through={(S) and (D) named lSD},3d/line through={(S) and (C) named lSC} ];
\path [3d/project={(A) on lSB}] coordinate (P)
[3d/project={(A) on lSD}] coordinate (Q) [3d/project={(A) on lSC}] coordinate (M);
\pgfmathsetmacro{\myR}{tddistance("(I)","(A)")}
\path[save named path=sph,3d/screen coords] (I) circle[radius=\myR];
\path[3d/circumcircle center={A={(A)},B={(P)},C={(Q)}}] coordinate (T);
\pic[red,thick]{3d/circle on sphere={R=\myR,C={(I)}, P={(T)}}}; %circle APQ
\path pic{3d/circle on sphere={R=\myR,C={(I)}, P={(I)}}}; % circle ABC
\begin{scope}
\clip (S) -- (B) -- (D) -- cycle [reverseclip];
\end{scope}
\begin{scope}
\clip (S) -- (B) -- (D) -- cycle;
{(\p)node[c]{}+(\g:2.5mm) node{$\p$}};