Examples of topology preserving transforms I was trying to think of a transformation in Euclidean space which is topology preserving but not affine. I fact I could not think of even a non affine isometric transform in Euclidean space. 
Here topology preserving transform means $d(x,y)<d(a,b) \iff d(f(x),f(y))<d(f(a),f(b))$ and $d(x,y)=d(a,b) \iff d(f(x),f(y))=d(f(a),f(b))$ 
 A: 
$\textbf{Theorem:}$ Let $f:\mathbb R^n\longrightarrow \mathbb R^n$ have the property
  $$d(x,y)=d(a,b) \implies d(f(x),f(y))=d(f(a),f(b)). \tag{Eq}$$
  In addition, suppose that $f$ is continuous or that $f$ satisfies 
  $$d(x,y) < d(a,b) \implies d(f(x),f(y)) < d(f(a),f(b)). \tag{Ineq}$$
  Then $f$ is affine.

Notice that the properties in the statement are only implications (rather than 'if and only if' statement).
Moreover, observe that by property $(\text{Eq})$


*

*$f$ sends spheres centered on $p$ to spheres centered on $f(p)$.

*$f$ takes regular simplices to regular simplices.

*If $f(a)\neq f(x)$, then $f$ sends the bisecting hyperplane between $a$ and $x$ to the bisecting hyperplane between $f(a)$ and $f(x)$.


Proof of Theorem:
Without loss of generality, assume that $f(0) = 0$.
If $f$ itself is constant, then it is obviously affine.
Suppose then that $f$ is not constant and let $x\neq 0$, so that $f(x)\neq 0$.

Proposition $\textbf{1}$: $f(x/2) = f(x)/2$

Proof of Proposition $\textit{1}$:
If $n=1$, we have that $|x-x/2| = |x/2-0|$ which implies via property $(\text{Eq})$ that $f(x/2)$ is equidistant to $f(x)$ and $f(0)=0$.
Because $n=1$, this means $f$ is the midpoint of the segment joining $0$ and $f(x)$, which implies the claim.
Suppose hence that $n\geqslant 2$.
Consider a regular $n$-simplex $S$ such that one of its vertices is $x/2$ and the other $n$ vertices $v_1,v_2,\dots,v_n$ lie on $\partial B(0;\lVert x\rVert)$ and are equidistant to $x$.
Then $f(S)$ is a regular $n$-simplex such that one of its vertices, $f(x/2)$, lies on the hyperplane bisecting $[0,f(x)]$ and the $n$ others lie on $B(0;\lVert f(x)\rVert)$ and are equidistant to $f(x)$.
It follows that $f(x/2) = f(x)/2$. $\square$

This takes a bit of geometry; can you convince yourself that this is true? 
  Consider
$\quad(1)$ How do two $n$-spheres intersect? and
  $\quad(2)$ Given an $(n-1)$ simplex in $\mathbb R^n$, what points can one add to the set as a vertex to turn it into an $n$-simplex?

Proof of Theorem (continued):
Let $\mathcal D$ be the set of dyadic rationals.
We may repeat the process of the claim to conclude that for $q\in[0,1]\cap \mathcal D$ we have $f(qx)=qf(x)$.

Proposition $\textbf{2}$: For each $t\in [0,1]$, $\lVert f(tx) \rVert= t\lVert f(x)\rVert$.

Proof of Proposition $\textit{2}$:
Because the dyadic rationals are dense in the reals, the proposition follows immediately from the assumption of continuity.
If we don't require that $f$ be continuous, we resort to the property $(\text{Ineq})$.
For every $r \in [0,1]\setminus\mathcal D$, we can find $q_0, q_1\in [0,1]\cap\mathcal D$ arbitrarily close to $r$ with $q_0<r<q_1$.
Then, by $(\text{Ineq})$,
\begin{array}{crcccl}
&\lVert q_0x-0\rVert &<& \lVert rx-0\rVert &<& \lVert q_1x-0\rVert
\\
\implies&q_0f(x) = \lVert f(q_0x)\rVert &<& \lVert f(rx)\rVert &<& \lVert f(q_1x)\rVert = q_1f(x)
.\end{array}
Letting $q_0\to r$ from below and $q_1\to r$ from above, we conclude that $\lVert f(rx)\rVert =  r \lVert f(x)\rVert$, as desired. $\square$
Proof of Theorem (continued):
We now want to show that $\lVert f(tx) \rVert= t\lVert f(x)\rVert$ holds for $t > 1$.
To that end, take $y=sx$ for some dyadic rational $s > 1$ and repeat the entire process, with $y$ instead of $x$.
Notice that the product of two dyadic rationals is once again a dyadic rational.
We'll have that $f(qy) = qf(y)$ for all $q\in[0,1]\cap\mathcal D$.
Now, $qy = qs \cdot x$ and for $q$ small enough $qs \in [0,1]\cap\mathcal D$.
Then, with $q>0$ small enough,
$$qf(y) = qf(sx) = qsf(x) \implies f(sx) = sf(x).$$
We may now once again use our additional hypothesis and the density of the dyadic rationals to conclude that $\lVert f(tx) \rVert= t\lVert f(x)\rVert$ holds for all $t \geqslant 0$.
Finally, consider $g= \frac{\lVert x\rVert}{\lVert f(x)\rVert} f$ and notice that $g$ also satisfies property $\text{Eq}$.
We show that $g$ is an isometry, that is, $\lVert g(u) - g(v)\rVert = \lVert u - v\rVert$ for all $u,v\in\mathbb R^n$.
Indeed, for all $u,v\in\mathbb R^n$ we have by property $\text{Eq}$
\begin{align}
\lVert u-v\rVert = \left\lVert \frac{\lVert u-v\rVert}{\lVert x \rVert} x 
 - 0\right\rVert
\implies
\lVert g(u)-g(v)\rVert
=
\left\lVert g\left(\frac{\lVert u-v\rVert}{\lVert x \rVert} x\right) 
 - g(0)\right\rVert
.\end{align}
Now, $g(0)=0$ and hence 
\begin{align}
\lVert g(u)-g(v)\rVert
&=
\left\lVert g\left(\frac{\lVert u-v\rVert}{\lVert x \rVert} x\right)\right\rVert
\\&=
\left\lVert \frac{\lVert x\rVert}{\lVert f(x)\rVert}\, f\left(\frac{\lVert u-v\rVert}{\lVert x \rVert} x\right)\right\rVert
\\&=
\frac{\lVert x\rVert}{\lVert f(x)\rVert}\, \left\lVert f\left(\frac{\lVert u-v\rVert}{\lVert x \rVert}\,x\right)\right\rVert
\\&=
\frac{\lVert x\rVert}{\lVert f(x)\rVert}\, \frac{\lVert u-v\rVert}{\lVert x \rVert}\,\lVert f(x)\rVert = \lVert u-v\rVert,
\end{align}
as we sought to prove.
At this point we have that $g$ is an isometry of $\mathbb R^n$ that fixes the origin.
It is a well known result (and a good exercise!) to show that $g$ is linear, and it then follows that $f$ is affine, which concludes the proof of the Theorem. $\square$
A: Such a map of a Euclidean space $\mathbb{R}^n$ into itself must be affine. (Aside: I would not call it "topology preserving", because "topology" is a well-known structure, which is preserved precisely by homeomorphisms. A better name would be "distance-order-preserving" or "distance-monotone". See Monotone Maps, Sphericity and Bounded Second Eigenvalue by Yonatan Bilu and Nati Linial.)
Proof: For a number $t\ge 0$ let $g(t) = d(f(x), f(y))$ where $d(x, y)=t$; the function $g$ is well defined and is strictly increasing by the assumption on $f$. Scaling $f$ if necessary, we can arrange $g(1)=1$. 
Consider three collinear points $x, y, z$ such that $d(x, z) = t+s$ where $t=d(x, y)$ and $s=d(y, z)$. By the triangle inequality, 
$$g(t+s) = d(f(x), f(z)) \le d(f(x), f(y)) + d(f(y), f(z)) = g(t)+g(s) \tag1$$
i.e., $g$ is subadditive.
Small scales
When $t>0$ is small, the maximal cardinality of a $t$-separated subset of a ball $B$ of radius $1$ is $\alpha_n t^{-n} + o(t^{-n})$. (Being $t$-separated means the distance between any two points of $E$ is at least $t$. The coefficient $\alpha_n$ depends only on $n$.)  Note that $f(B)$ is contained in a ball of radius $1$, and contains $f(E)$, which is $g(t)$-separated. Hence, 
$$
\alpha_n g(t)^{-n} + o(g(t)^{-n}) \ge \alpha_n t^{-n} + o(t^{-n}) 
$$
which yields 
$$\limsup_{t\to0} \frac{g(t)}{t} \le 1\tag 2$$
If $g(t)>t$ for some $t$, then by subadditivity 
$$\frac{g(t/k)}{t/k} \ge \frac{g(t)}{t} > 1$$ 
for all $k\in\mathbb{N}$, contradicting (2). Thus 
$$g(t) \le t \quad \text{ for all }t \tag3$$ 
Large scales
When $t$ is large, the maximal cardinality of a $1$-separated subset of a ball $B$ of radius $t$ is $\alpha_n t^n + o(t^n)$. Note that $f(B)$ is contained in a ball of radius $g(t)$, and contains $f(E)$, which is $1$-separated. Hence, 
$$
\alpha_n g(t)^n + o(g(t)^n) \ge \alpha_n t^n + o(t^n) 
$$
which yields 
$$\liminf_{t\to\infty} \frac{g(t)}{t} \ge 1\tag 4$$
If $g(t)<t$ for some $t$, then by subadditivity 
$$\frac{g(kt)}{kt} \le \frac{g(t)}{t} < 1$$ 
for all $k\in\mathbb{N}$, contradicting (4). Thus 
$$g(t) \ge t \quad \text{ for all }t \tag5$$ 
Conclusion
By (3) and (5), $g(t)=t$ for all $t$, meaning $f$ is an isometry. And every isometry of $\mathbb{R}^n$ is affine.
