\section{5. Basic topological operators}
Perhaps the main advantage of the quad edge data structure is that the construction and modification of arbitrary diagrams can be effected by as few as two basic topological operators, in contrast to the half-dozen or more required by the previous versions [\Braid, \MaSu].
The first operator is denoted by \prg{$e$ \pgets\ MakeEdge[]}. It takes no parameters, and returns an edge $e$ of a newly-created data structure representing a subdivision of the sphere (see fig. 5.1).
\capfig4.5cm{Figure 5.1. The result of \prg{MakeEdge}.}
Apart from orientation and direction, $e$ will be the only edge of the subdivision, and will not be a loop; we have $e\org\neq e\dest$, $e\lef=e\rif$, $e\lnext=e\rnext=e\sym$, and $e\onext=e\oprev=e$. To construct a loop, we may use \prg{$e$ \pgets\ MakeEdge[]\prot}; then we will have $e\org=e\dest$, $e\lef\neq e\rif$, $e\lnext=e\rnext=e$, and $e\onext=e\oprev=e\sym$.
The second operator is denoted by \prg{Splice[$a$, $b$]} and takes as parameters two edges $a$ and $b$, returning no value. This operation affects the two edge rings $a\org$ and $b\org$, and, independently, the two edge rings $a\lef$ and $b\lef$. In each case,
\begitems
\item if the two rings are distinct, \prg{Splice} will combine them into one;
\item if the two are exactly the same ring, \prg{Splice} will break it in two separate pieces;
\item if the two are the same ring taken with opposite orientations, \prg{Splice} will $\flip$ (and reverse the order) of a segment of that ring.
\enditems
The parameters $a$ and $b$ determine the place where the edge rings will be cut and joined. For the rings $a\org$ and $b\org$, the cuts will occur immediately {\it after} $a$ and $b$ (in counterclockwise order); for the rings $a\lef$ and $b\lef$, the cut will occur immediately {\it before} $a\rot$ and $b\rot$. Figure 5.2a illustrates this process for one of the simplest cases, when $a$ and $b$ have the same origin and distinct left faces. In this case \prg{Splice[$a$, $b$]} splits the common origin of $a$ and $b$ in two separate vertices, and joins their left faces. If the origins are distinct and the left faces are the same, the effect will be precisely the opposite: the vertices are joined and the left faces are split. Indeed, \prg{Splice} is its own inverse: if we perform \prg{Splice[$a$, $b$]} twice in a row we will get back the same subdivision.
\capfig7.5cm{\null\hfil$a\org = b\org$, $a\lef\neq b\lef$}
\capfig7.5cm{\null\hfil$a\org \neq b\org$, $a\lef= b\lef$}
\capfig0cm{Figure 5.2a. The effect of \prg{Splice}: trading a vertex for a face.}
Figure 5.2b illustrates the effect of \prg{Splice[$a$, $b$]} in the case where $a$ and $b$ have distinct left faces and distinct origins. In this case, \prg{Splice} will either join two components in a single one, or add an extra ``handle'' to the manifold, depending on whether $a$ and $b$ are in the same component or not. The quad-edge data structure contains no mechanism to distinguish between these two cases, or to keep track automatically of the components and connectivity of the manifold. There seems to be no general way of doing this at a bounded cost per operation; on the other hand, in many applications this problem is trivial or straightforward, so it is best to solve this problem independently for each case. Figure 5.2b also illustrates the case when both left faces and origins are distinct.
\capfig7.5cm{\null\hfil$a\org \neq b\org$, $a\lef\neq b\lef$}
\capfig7.5cm{\null\hfil$a\org = b\org$, $a\lef= b\lef$}
\capfig0cm{Figure 5.2b. The effect of \prg{Splice}: Changing the connectivity of the manifold.}
In the edge algebra, the $\org$ and $\lef$ rings of an edge $e$ are the orbits under $\onext$ of $e$ and $e\onext\rot$, respectively. The effect of \prg{Splice} can be described as the construction of a new edge algebra $A\pr=(E, E^\ast, \onext\pr, \rot, \flip)$ from an existing algebra $A=(E, E^\ast, \onext, \rot, \flip)$, where $\onext\pr$ is obtained from $\onext$ by redefining some of its values. The modifications needed to obtain the effect described above are actually quite simple. If we let $\alpha=a\onext\rot$ and $\beta=b\onext\rot$, basically all we have to do is to interchange the values of $a\onext$ with $b\onext$ and $\alpha\onext$ with $\beta\onext$. The apparently complex behavior of \prg{Splice} now can be recognized as the familiar effect of interchanging the \prg{next} links of two circular list nodes [\Knuth].
As one may well expect, to preserve the validity of the axioms F1--F5 and E1--E5 we may have to make some additional changes to the $\onext$ function. For example, whenever we redefine $e\onext\pr$ to be some edge $f$, we must also redefine $e\flip{(\onext\pr)}^{-1}$ to be $f\flip$, or, equivalently, $f\flip\onext\pr$ to be $e\flip$. So, \prg{Splice[$a$, $b$]} must perform at least the following changes in the function $\onext$:
\vfill\eject
$$\vcenter{\halign{\hfil\rt{$ # $}&\lft{$\null= # $}\cr
a\onext\pr&b\onext\cr
b\onext\pr&a\onext\cr\vsk3
\alpha\onext\pr&\beta\onext\cr
\beta\onext\pr&\alpha\onext\cr\vsk6}
\halign{\hfil\rt{$ # $}&\lft{$\null= # $}\cr
(b\onext\flip)\onext\pr&a\flip\cr
(a\onext\flip)\onext\pr&b\flip\cr\vsk3
(\beta\onext\flip)\onext\pr&\alpha\flip\cr
(\alpha\onext\flip)\onext\pr&\beta\flip\cr
}}\eqno(2)$$
Note that these equations reduce to $\onext\pr=\onext$ if $b=a$. Since $a\onext\pr=b\onext$, to satisfy axiom E5 we must have $a\in E$ iff $b\onext\in E$, which is equivalent to $a\in E$ iff $b\in E$. We will take this as a precondition for the validity of \prg{Splice[$a$, $b$]}: the effect of this operation is not defined if $a$ is a primal edge and $b$ is dual, or vice-versa\foot\dag{Note that if $a$ and $b$ lie in distinct subalgebras $A𡤊$ and $A𡤋$ of $A$, then the union of $A𡤊$ and the dual of $A𡤋$ is also a valid edge algebra. So, in practice we can always perform \prg{Splice[$a$, $b$]} when $a$ and $b$ lie in disjont data structures.}. Another problematic situation is when $b=a\onext\flip$: according to equations (2) we would have $a\onext\pr=a\onext\flip\onext=a\flip$, which contradicts F3. In this particular case, it is more convenient to define the effect of \prg{Splice[$a$, $b$]} as being null, i.e. $\onext\pr=\onext$. It turns out that, with only these two exceptions, the equations above always define a valid edge algebra:
\theorem{5.1}{If $A$ is an edge algebra, $a$ and $b$ are both primal or both dual, and $b\neq a\onext\flip$, then the algebra $A\pr$ obtained by performing the operation \prg{Splice[$a$, $b$]} on $A$ is also an edge algebra.}
\proof{Since \prg{Splice} does not affect $\flip$ and $\rot$, all axioms except F2, F3, E2 and E5 are automatically satisfied by $A\pr$.
\pp Since $a$ and $b$, are both primal or both dual, the same is true of $\alpha$ and $\beta$, $a\onext\flip$ and $b\onext\flip$, and $\alpha\onext\flip$ and $\beta\onext\flip$. Thus the assignments corresponding to \prg{Splice[$a$, $b$]} will not destroy E5.
\pp Now let's show E2 holds in $A\pr$, i.e. $e\rot\onext\pr\rot\onext\pr=e$. Let $X$ be the set of edges whose $\onext$ has been changed, i.e.
$$\halign{\rt{$ # $}&\lft{$ # $}\cr
X={\left\{\right.}\,&a, b,\cr\vsk3
\null& \alpha, \beta,\cr\vsk3
\null& a\onext\flip, b\onext\flip,\cr\vsk3
\null& \alpha\onext\flip, \beta\onext\flip\,{\left.\right\}}}.\cr}$$
First, if $e\rot\not\in X$, then $e\rot\onext\rot\not\in X\onext\rot=X$, and so
$$\eqalign{e\rot\onext\pr\rot\onext\pr&=e\rot\onext\rot\onext\pr\cr
\null&=(e\rot\onext\rot)\onext\cr
\null&=e.\cr}$$
\pp Now assume $e\rot\in X$. Notice that \prg{Splice[$a$, $b$]} does exactly the same thing as \prg{Splice[$b$, $a$]}, \prg{Splice[$\alpha$, $\beta$]}, and \prg{Splice[$a\onext\flip$, $b\onext\flip$]}, so without loss of generality we can assume $e\rot=a$. Then
$$\eqalign{e\rot\onext\pr\rot\onext\pr&=a\onext\pr\rot\onext\pr\cr
\null&=b\onext\rot\onext\pr\cr
\null&=\beta\onext\pr\cr
\null&=\alpha\onext\cr
\null&=a\onext\rot\onext\cr
\null&=e\rot\onext\rot\onext\cr
\null&=e.\cr}$$
\pp In a similar way we can prove F2. To conclude, let us prove F3, or $e\flip{(\onext\pr)}^n\neq e$ for all $n$. In other words, we have to show that $\flip$ always takes an $\onext\pr$ orbit to a different $\onext\pr$ orbit. It suffices to show this for the orbits of elements of $X$; in fact the symmetry of \prg{Splice} implies it is sufficient to show this for the orbit of $a$.
\pp Let $a\org=\seq{a𡤁\; a𡤂\; \ldots\; a←{m-1}\; a←m(=a)}$ be the orbit of $a$ under the original $\onext$. The orbit of $a\flip$ under $\onext$ is then $a\flip\org=\seq{a←m\pr\; a←{m-1}\pr\; \ldots\; a𡤂\pr\; a𡤁\pr}$, where $a←i\pr=a←i\flip$ for all $i$. These two orbits are disjoint; they cannot contain any of the edges $\alpha$, $\beta$, $\alpha\onext\flip$, or $\beta\onext\flip$, because they are in the dual subdivision. Furthermore, one contains $b$ if and only if the other contains $b\flip$. There are then only three cases to consider (see figure 5.3):
\capfig9cm{\null\hskip20pt Case 1.\hfil Case2.\hskip20pt.\hfilneg}
\capfig6cm{\null\hfil Case 3.}
\capfig0cm{Figure 5.3. The effect of \prg{Splice} on the $\onext$ orbits.}
\pp Case 1: the edge $b$ is neither in $a\org$ nor in $a\flip\org$. Then let $b\org=\seq{b𡤁\; b𡤂\; \ldots\; b←{n-1}\; b←n(=b)}$ and $b\flip\org=\seq{b←n\pr\;b←{n-1}\pr\; \ldots\; b𡤂\pr\;b𡤁\pr}$. According to (2), we will have $a←m\onext\pr=b𡤁$, $b←m\onext\pr=a𡤁$, $a𡤁\pr\onext\pr=b←m\pr$, $b𡤁\pr\onext\pr=a←m\pr$. Therefore, the orbits of $a$ and $a\flip$ under $\onext\pr$ will be $a\org\pr=\seq{a𡤁\; a𡤂\; \ldots\; a←{m-1}\; a←m(=a)\; b𡤁\; b𡤂\; \ldots\; b←{n-1}\; b←n(=b)}$ and $a\flip\org\pr=\seq{b←n\pr\;b←{n-1}\pr\; \ldots\; b𡤂\pr\;b𡤁\pr\;a←m\pr\; a←{m-1}\pr\; \ldots\; a𡤂\pr\; a𡤁\pr}$.
\pp Case 2: the edge $b$ occurs in $a\org$. Then $b=a←i$ for some $i$, $1\leq i\leq m$. After \prg{Splice} is executed we will have $a←m\onext\pr=a←{i+1}$, $a←i\onext\pr=a𡤁$, $a𡤁\pr\onext\pr=a←i\pr$, and $a←{i+1}\pr\onext\pr=a←m\pr$. If $i=m$ (i.e., if $a=b$) then $\onext\pr=\onext$ and we are done. If $i\neq m$ then under $\onext\pr$ the elements of $a\org$ and $a\flip\org$ will be split in the four orbits $b\org\pr=\seq{a𡤁\;a𡤂\;\ldots\;a←i}$, $a\org\pr=\seq{a←{i+1}\; a←{i+2}\; \ldots\;a←m}$, $a\flip\org\pr=\seq{a←m\pr\; a←{m-1}\pr\; \ldots\; a←{i+1}\pr}$, and $b\flip\org\pr=\seq{a←i\pr\;a←{i-1}\pr\;\ldots\;a𡤁}$.
\pp Case 3: the edge $b$ occurs in $a\flip\org$. Since $b\neq a\onext\flip=a𡤁\pr$, we have $b=a←i\pr$ for some $i$, $2\leq i\leq m$. After \prg{Splice} is executed we will have $a←m\onext\pr=a←{i-1}\pr$, $a←i\pr\onext\pr=a𡤁$, $a𡤁\pr\onext\pr=a←i$, and $a←{i-1}\onext\pr=a←m\pr$. Then the orbits of $\onext\pr$ containing those elements will be $a\org\pr=\seq{a←{i-1}\pr\; a←{i-2}\pr\; \ldots\; a𡤁\pr\; a←i\; a←{i+1}\;\ldots\;a←{m-1}\;a←m}$ and $a\flip\org\pr=\seq{a←m\pr\;a←{m-1}\pr\;\ldots\;a←{i+1}\pr\;a←{i}\pr\;a𡤁\; a𡤂\;\ldots\;a←{i-1}\;a←i}$.
\pp In all three cases, the orbits of $e$ and $e\flip$ under $\onext\pr$ will be disjoint, for all edges $e$.}
The proof of theorem 5.1 gives a precise description of the effect of \prg{Splice} on the edge rings. In particular, the dicussion for case 3 helps in the understanding of figure 5.3. In that case the effect of \prg{Splice} is to add or remove a ``cross cap'' to the manifold.
In terms of the data structure, the \prg{Splice} operation is even simpler. The identities $$a\onext\flip=a\onext\rot\flip\rot=\alpha\flip\rot$$ and $$\alpha\onext\flip=a\onext\rot\onext\flip=a\flip\rot$$ allow us to rewrite (2) as
$$\vcenter{\halign{\hskip20pt \rt{$ # $}&\lft{$\,\gets\, # $}& \hskip0.4cm \rt{$ # $}&\lft{$\,\gets\, # $}\cr
a\onext&b\onext&(a\flip\rot)\onext&\beta\flip\cr\vsk3
b\onext&a\onext&(b\flip\rot)\onext&\alpha\flip\cr\vsk6
\alpha\onext&\beta\onext&
(\alpha\flip\rot)\onext&b\flip\cr\vsk3
\beta\onext&\alpha\onext&
(\beta\flip\rot)\onext&a\flip\cr
}}\eqno(3)$$
Only one of the two assignments in each line of (3) is meaningful. The reason is that only one of the receiving $\onext$ fields actually exists in the structure; the value of the other is determined implicitly from existing links by the equations (1). If the $f$ bit of $a$ is 0, then $a\onext$ exists, and \prg{Splice} writes $b\onext$ into it. Otherwise $a\flip\rot$ has $f=0$, and we can assign $b\flip\rot\onext$ to $a\flip\rot\onext$. The same applies to $b$, $\alpha$ and $\beta$. Note that these assignments are simultaneous, that is, all right-hand sides are computed before any value is assigned to the left-hand-sides. In addition, these assignments should be preceded by a test of whether $b=a\onext\flip$, in which case they should not be executed at all. Note however that there is no need to check for $a=b$.
Further reductions in the code of \prg{Splice} occur in the the case of orientable manifolds, when we can use the simplified data structure without $\flip$ and the $f$ bits. In that case, the meaningful assignments are precisely those in the left column of (3), and the test for $b=a\onext\flip$ is meaningless.
\capfig7cm{\null\hfil$a\org = b\org\flip$, $a\lef= b\lef$}
\capfig7cm{\null\hfil$a\org = b\org\flip$, $a\lef= b\lef$\cp
$c\lef = b\lef\flip$, $c\org= b\org$}
\capfig7cm{\null\hfil$c\lef = b\lef\flip$, $c\org\neq b\org$.}
\capfig0cm{Figure 5.2c. The effect of \prg{Splice}: Adding or removing a cross-cap.}
As an example of the use of \prg{Splice}, consider the operation \prg{SwapDiagonal[$e$]} that we will be using later on: given an edge $e$ whose left and right faces are triangles, the problem is to delete $e$ and connect the other two vertices of the quadrilateral thus formed (see fig 5.4).
This is equivalent to
{\prog
\ \ \ a \pgets\ e\poprev;
\ \ \ b \pgets\ e\psym\poprev;
\ \ \ Splice[e, a]; Splice[e\psym, b];
\ \ \ Splice[e, a\plnext]; Splice[e\psym, b\plnext]
\endprog}
The first pair of \prg{Splice}s disconnects $e$ from the edge structure, and leaves it as the single edge of a separate spherical component. The last two \prg{Splice}s connect $e$ again at the required position.
\capfig5cm{Figure 5.4. The effect of \prg{SwapEdge[$e$]}.}
\theorem{5.1}{An arbitrary subdivision $P$ can be transformed into a collection of $\leftv EP\rightv$ isolated edges by the application of at most $2\leftv EP\rightv$ \prg{Splice} operations.}
\proof{Let $e$ be an arbitrary edge of $P$. Then the operations \prg{Splice[$e$, $e\oprev$]} and \prg{Splice[$e\sym$, $e\sym\oprev$]} will have the effect of removing $e$ from $P$ and placing it as an isolated edge on a separate manifold homeomorphic to the sphere. By repeating this for every edge the theorem follows.}
From this theorem and from the fact that \prg{Splice} is its own inverse, we can conclude that any simple subdivision $P$ can be constructed, in $O(\leftv EP\rightv)$ time and space, by using only the \prg{Splice} and \prg{MakeEdge} operations.
The \prg{Data} links are not affected by (and do not affect) the \prg{MakeEdge} and \prg{Splice} operations; if used at all, they can be set and updated, at any time after the edge is created, by plain assignment statements. Since they carry no topological information, there is no need to forbid or restrict assignments to them. Usually each application imposes geometrical or other constraints on the \prg{Data} fields that may be affected by changes in the topology. Some care is required when enforcing those constraints; for example, the operation of joining two vertices may change the geometrical parameters of a large number of edges and faces, and updating all the corresponding \prg{Data} fields every time may be too expensive. However, even in such applications it is frequently the case that we can defer those updates until they are really needed (so that their cost can be amortized over a large number of \prg{Splice}s), or initialize the \prg{Data} links right from the beginning with the values they must have in the final structure.