File size: 19,243 Bytes
97c3405
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import OAI.Combinatorics.Crossing.DrawingDefinition
import Mathlib.Algebra.BigOperators.Group.Finset.Basic
import Mathlib.Analysis.SpecialFunctions.Complex.Arg
import Mathlib.Data.Set.Card
import Mathlib.Order.Lattice.Nat
import Mathlib.Topology.OpenPartialHomeomorph.Defs
import Mathlib.Topology.Path

namespace OAI

/-! Continuous graph drawings, proper spatial crossings, incidence, and endpoint properties. -/

section

/-!

The coordinate plane has its usual topology; every Euclidean circle below is
specified by squaredLength, not by the product norm.
-/

noncomputable section
open Set
open scoped BigOperators Classical

namespace Paper170

abbrev Plane := ℝ × ℝ

def squaredLength (x : Plane) : ℝ := x.1 ^ 2 + x.2 ^ 2

def det2 (x y : Plane) : ℝ := x.1 * y.2 - x.2 * y.1

def segmentPoint (a b : Plane) (t : ℝ) : Plane := (1 - t) • a + t • b

/-- One chosen orientation of each edge of a finite simple graph. -/
structure Graph (V E : Type) where
  src : E → V
  dst : E → V
  loopless : ∀ e, src e ≠ dst e
  simple : ∀ e f, ((src e = src f ∧ dst e = dst f) ∨
    (src e = dst f ∧ dst e = src f)) → e = f

abbrev CompleteEdge (n : ℕ) := {p : Fin n × Fin n // p.1 < p.2}

instance (n : ℕ) : Fintype (CompleteEdge n) := Fintype.ofFinite _

def completeGraph (n : ℕ) : Graph (Fin n) (CompleteEdge n) where
  src e := e.val.1
  dst e := e.val.2
  loopless e := ne_of_lt e.property
  simple e f h := by
    rcases h with h | h
    · exact Subtype.ext (Prod.ext h.1 h.2)
    · have he := e.property
      have hf := f.property
      rw [h.1, h.2] at he
      exact (lt_asymm he hf).elim

variable {V E : Type} (G : Graph V E)

namespace Graph

abbrev IncidentEdge (v : V) := {e : E // G.src e = v ∨ G.dst e = v}

end Graph

/-- Actual injective continuous edge arcs, with unrestricted routing. -/
structure ContinuousDrawing where
  vertex : V → Plane
  vertex_injective : Function.Injective vertex
  edge : ∀ e : E, Path (vertex (G.src e)) (vertex (G.dst e))
  edge_injective : ∀ e, Function.Injective (edge e)
  interior_avoids_vertices : ∀ e t, t ∈ Ioo (0 : unitInterval) 1 →
    ∀ v, edge e t ≠ vertex v

variable {G}

def edgeInterior (D : ContinuousDrawing G) (e : E) : Set Plane :=
  D.edge e '' Ioo (0 : unitInterval) 1

/-- A proper double crossing is locally homeomorphic to two coordinate axes. -/
structure ProperCrossing (A B : Set Plane) (x : Plane) : Prop where
  chart_exists : ∃ chart : OpenPartialHomeomorph Plane Plane,
    x ∈ chart.source ∧ chart x = (0, 0) ∧
    chart '' (chart.source ∩ A) = chart.target ∩ {y | y.2 = 0} ∧
    chart '' (chart.source ∩ B) = chart.target ∩ {y | y.1 = 0}

/-- Spatial intersection points. Different meetings of the same pair remain distinct. -/
def crossingPoints (D : ContinuousDrawing G) : Set Plane :=
  {x | ∃ e f : E, e ≠ f ∧ x ∈ edgeInterior D e ∧ x ∈ edgeInterior D f}

structure AdmissibleDrawing (G : Graph V E) extends ContinuousDrawing G where
  crossings_finite : (crossingPoints toContinuousDrawing).Finite
  crossings_proper : ∀ e f, e ≠ f → ∀ x,
    x ∈ edgeInterior toContinuousDrawing e → x ∈ edgeInterior toContinuousDrawing f →
      ProperCrossing (edgeInterior toContinuousDrawing e) (edgeInterior toContinuousDrawing f) x
  no_triples : ∀ e f g, e ≠ f → e ≠ g → f ≠ g →
    edgeInterior toContinuousDrawing e ∩ edgeInterior toContinuousDrawing f ∩
      edgeInterior toContinuousDrawing g = ∅

def crossingCount (D : AdmissibleDrawing G) : ℕ := D.crossings_finite.toFinset.card

/-- The minimum spatial crossing count over admissible drawings. -/
def ordinaryCrossingNumber (G : Graph V E) : ℕ := sInf (range (@crossingCount V E G))

/-- A finite affine parametrization of the actual path. -/
structure PolygonalParametrization {a b : Plane} (γ : Path a b) where
  pieces : ℕ
  positive : 0 < pieces
  knot : Fin (pieces + 1) → unitInterval
  increasing : StrictMono knot
  first : knot 0 = 0
  last : knot (Fin.last pieces) = 1
  affine : ∀ i : Fin pieces, ∀ t : unitInterval,
    knot i.castSucc ≤ t → t ≤ knot i.succ →
    γ t = segmentPoint (γ (knot i.castSucc)) (γ (knot i.succ))
      (((t : ℝ) - knot i.castSucc) / ((knot i.succ : ℝ) - knot i.castSucc))

def PolygonalParametrization.vertices {a b : Plane} {γ : Path a b}
    (P : PolygonalParametrization γ) (i : ℕ) : Plane :=
  γ (P.knot ⟨min i P.pieces, Nat.lt_succ_of_le (Nat.min_le_right _ _)⟩)

/-- A convenient subdivision of a normalized drawing. Avoiding subdivision
knots at crossings is additional presentation data, obtained by the normalization theorem. -/
structure NormalizedDrawing (G : Graph V E) extends AdmissibleDrawing G where
  polygonal : ∀ e, PolygonalParametrization (edge e)
  no_adjacent : ∀ e f, e ≠ f →
    (G.src e = G.src f ∨ G.src e = G.dst f ∨ G.dst e = G.src f ∨ G.dst e = G.dst f) →
    Disjoint (edgeInterior toContinuousDrawing e) (edgeInterior toContinuousDrawing f)
  crossings_not_knots : ∀ e (i : Fin ((polygonal e).pieces + 1)),
    edge e ((polygonal e).knot i) ∉ crossingPoints toContinuousDrawing

/-- Directed open-segment intersection, with the ordinary determinant sign. -/
def segmentIntersection (a b c d : Plane) : ℝ :=
  if ∃ s ∈ Ioo (0 : ℝ) 1, ∃ t ∈ Ioo (0 : ℝ) 1,
      segmentPoint a b s = segmentPoint c d t then
    if 0 < det2 (b - a) (d - c) then 1
    else if det2 (b - a) (d - c) < 0 then -1 else 0
  else 0

def interiorIntersection (N : NormalizedDrawing G) (e f : E) : ℝ :=
  if e = f then 0 else
    ∑ i ∈ Finset.range (N.polygonal e).pieces,
      ∑ j ∈ Finset.range (N.polygonal f).pieces,
        segmentIntersection ((N.polygonal e).vertices i) ((N.polygonal e).vertices (i + 1))
          ((N.polygonal f).vertices j) ((N.polygonal f).vertices (j + 1))

def outwardSpoke (N : NormalizedDrawing G) (v : V) (e : E) : Plane :=
  if G.src e = v then (N.polygonal e).vertices 1 - (N.polygonal e).vertices 0
  else (N.polygonal e).vertices ((N.polygonal e).pieces - 1) -
    (N.polygonal e).vertices (N.polygonal e).pieces

def spokeAngle (cut : ℝ) (a : Plane) : ℝ :=
  Complex.arg (Complex.exp (-(cut : ℂ) * Complex.I) * (⟨a.1, a.2⟩ : ℂ))

/-- The linear local orders are witnessed by the actual counterclockwise angles,
with a cut in a gap. No intersection identity is part of this data. -/
structure SpokeOrders (N : NormalizedDrawing G) where
  cut : V → ℝ
  order : ∀ v, LinearOrder (G.IncidentEdge v)
  cut_avoids : ∀ v (e : G.IncidentEdge v), spokeAngle (cut v) (outwardSpoke N v e.val) ≠ Real.pi
  compatible : ∀ v (e f : G.IncidentEdge v),
    @LT.lt _ (order v).toLT e f ↔
      spokeAngle (cut v) (outwardSpoke N v e.val) <
        spokeAngle (cut v) (outwardSpoke N v f.val)

def endpointSign (G : Graph V E) (v : V) (e : E) : ℝ :=
  if G.src e = v then 1 else if G.dst e = v then -1 else 0

def spokeComparison {N : NormalizedDrawing G} (O : SpokeOrders N) (v : V) (e f : E) : ℝ :=
  if he : G.src e = v ∨ G.dst e = v then if hf : G.src f = v ∨ G.dst f = v then
    if @LT.lt _ (O.order v).toLT ⟨e, he⟩ ⟨f, hf⟩ then 1
    else if @LT.lt _ (O.order v).toLT ⟨f, hf⟩ ⟨e, he⟩ then -1 else 0
  else 0 else 0

variable [Fintype V] [Fintype E]

def cycleIntersection {N : NormalizedDrawing G} (O : SpokeOrders N) (e f : E) : ℝ :=
  interiorIntersection N e f + (1 / 2 : ℝ) *
    ∑ v, endpointSign G v e * endpointSign G v f * spokeComparison O v e f

def edgePairing (J : E → E → ℝ) (z z' : E → ℝ) : ℝ :=
  ∑ e, ∑ f, z e * z' f * J e f

def boundary (G : Graph V E) (z : E → ℝ) (v : V) : ℝ :=
  ∑ e, z e * ((if G.dst e = v then 1 else 0) - (if G.src e = v then 1 else 0))

def completeEdgeVector {n : ℕ} (i p : Fin n) : CompleteEdge n → ℝ :=
  fun e => if e.val.1 = i ∧ e.val.2 = p then 1
    else if e.val.1 = p ∧ e.val.2 = i then -1 else 0

def rawCycleIntersection {n : ℕ} {N : NormalizedDrawing (completeGraph n)}
    (O : SpokeOrders N) (i p k q : Fin n) : ℝ :=
  edgePairing (cycleIntersection O) (completeEdgeVector i p) (completeEdgeVector k q)

/-- A simple combinatorial cycle with a chosen direction of traversal. -/
structure SimpleCycle (G : Graph V E) where
  length : ℕ
  three_le : 3 ≤ length
  vertex : Fin length → V
  vertex_injective : Function.Injective vertex
  edge : Fin length → E
  follows : ∀ i,
    let j : Fin length := ⟨(i.val + 1) % length,
      Nat.mod_lt _ (lt_of_lt_of_le (by decide : 0 < 3) three_le)⟩
    (G.src (edge i) = vertex i ∧ G.dst (edge i) = vertex j) ∨
      (G.dst (edge i) = vertex i ∧ G.src (edge i) = vertex j)

def SimpleCycle.edgeChain (C : SimpleCycle G) : E → ℝ :=
  fun e => ∑ i, if C.edge i = e then (if G.src e = C.vertex i then 1 else -1) else 0

def polygonIntersection (v w : ℕ → Plane) (n m : ℕ) : ℝ :=
  ∑ i ∈ Finset.range n, ∑ j ∈ Finset.range m,
    segmentIntersection (v i) (v (i + 1)) (w j) (w (j + 1))

/-- Only proper interior meetings between the two polygonal walks. -/
structure TransversePolygons (v w : ℕ → Plane) (n m : ℕ) : Prop where
  left_vertices_avoid : ∀ i, i ≤ n → ∀ j, j < m → ∀ t ∈ Icc (0 : ℝ) 1,
    v i ≠ segmentPoint (w j) (w (j + 1)) t
  right_vertices_avoid : ∀ j, j ≤ m → ∀ i, i < n → ∀ t ∈ Icc (0 : ℝ) 1,
    w j ≠ segmentPoint (v i) (v (i + 1)) t
  transverse : ∀ i, i < n → ∀ j, j < m →
    (∃ s ∈ Ioo (0 : ℝ) 1, ∃ t ∈ Ioo (0 : ℝ) 1,
      segmentPoint (v i) (v (i + 1)) s = segmentPoint (w j) (w (j + 1)) t) →
    det2 (v (i + 1) - v i) (w (j + 1) - w j) ≠ 0

def circleComparison (cut : ℝ) (a b : Plane) : ℝ :=
  if spokeAngle cut a < spokeAngle cut b then 1
  else if spokeAngle cut b < spokeAngle cut a then -1 else 0

def inversion (z : Plane) : Plane := (z.1 / squaredLength z, z.2 / squaredLength z)

end Paper170

end
end

section

/-! Converting actual nonvertex trace contacts to the exact crossing-point definition. -/
noncomputable section
open Zarankiewicz
variable {Vertex Edge : Type} {graph : Paper170.Graph Vertex Edge}

namespace Paper170Geometry
theorem edgeInterior_subset_range (D : Paper170.ContinuousDrawing graph) (e : Edge) :
    Paper170.edgeInterior D e ⊆ Set.range (D.edge e) := by
  rintro x ⟨t,_,he⟩
  exact ⟨t,he⟩

theorem edgeInterior_of_range_not_vertices (D : Paper170.ContinuousDrawing graph) (e : Edge)
    (x : Plane) (hx : x ∈ Set.range (D.edge e)) (hvertex : ∀ j, x ≠ D.vertex j) :
    x ∈ Paper170.edgeInterior D e := by
  obtain ⟨t,he⟩ := hx
  have ht0 : t ≠ 0 := by
    intro ht
    subst t
    exact hvertex _ (he.symm.trans (D.edge e).source)
  have ht1 : t ≠ 1 := by
    intro ht
    subst t
    exact hvertex _ (he.symm.trans (D.edge e).target)
  exact ⟨t,⟨lt_of_le_of_ne (show 0 ≤ t from t.property.1) (Ne.symm ht0),
    lt_of_le_of_ne (show t ≤ 1 from t.property.2) ht1⟩,he⟩

theorem edgeInterior_not_vertex (D : Paper170.ContinuousDrawing graph) (e : Edge)
    (x : Plane) (hx : x ∈ Paper170.edgeInterior D e) (j : Vertex) : x ≠ D.vertex j := by
  obtain ⟨t,ht,he⟩ := hx
  exact fun hj => D.interior_avoids_vertices e t ht j (he.trans hj)

theorem crossing_of_ranges_not_vertices (D : Paper170.ContinuousDrawing graph) (e f : Edge)
    (hef : e ≠ f) (x : Plane) (he : x ∈ Set.range (D.edge e)) (hf : x ∈ Set.range (D.edge f))
    (hv : ∀ j, x ≠ D.vertex j) : x ∈ Paper170.crossingPoints D :=
  ⟨e,f,hef,edgeInterior_of_range_not_vertices D e x he hv,edgeInterior_of_range_not_vertices D f x hf hv⟩

end Paper170Geometry

end
end

section

namespace Paper170Geometry
variable {Vertex Edge : Type} {graph : Paper170.Graph Vertex Edge}

theorem drawing_edge_vertex_parameter (D : Paper170.ContinuousDrawing graph) (e : Edge)
    (t : unitInterval) (v : Vertex) (he : D.edge e t=D.vertex v) :
    (t=0 ∧ v=(graph.src e)) ∨ (t=1 ∧ v=(graph.dst e)) := by
  by_cases ht0 : t=0
  · subst t
    exact Or.inl ⟨rfl,(D.vertex_injective ((D.edge e).source.symm.trans he)).symm⟩
  by_cases ht1 : t=1
  · subst t
    exact Or.inr ⟨rfl,(D.vertex_injective ((D.edge e).target.symm.trans he)).symm⟩
  exact False.elim (D.interior_avoids_vertices e t
    ⟨lt_of_le_of_ne t.property.1 (Ne.symm ht0),lt_of_le_of_ne t.property.2 ht1⟩ v he)

end Paper170Geometry

end

section

/-! A weaker INPUT for normalization: actual simple routes with finite contacts and no triples.
The endpoint's Paper170.AdmissibleDrawing and MainTarget definitions are unchanged. -/
noncomputable section
open Zarankiewicz
variable {Vertex Edge : Type} {graph : Paper170.Graph Vertex Edge}

namespace Paper170Geometry

structure FiniteIntersectionDrawing (graph : Paper170.Graph Vertex Edge) extends Paper170.ContinuousDrawing graph where
  crossings_finite : (Paper170.crossingPoints toContinuousDrawing).Finite
  no_triples : ∀ e f g, e ≠ f → e ≠ g → f ≠ g →
    Paper170.edgeInterior toContinuousDrawing e ∩ Paper170.edgeInterior toContinuousDrawing f ∩
      Paper170.edgeInterior toContinuousDrawing g = ∅

abbrev toFiniteIntersectionDrawing  (D : Paper170.AdmissibleDrawing graph) :
    FiniteIntersectionDrawing graph where
  toContinuousDrawing := D.toContinuousDrawing
  crossings_finite := D.crossings_finite
  no_triples := D.no_triples

instance admissibleToFiniteIntersectionDrawing  :
    CoeOut (Paper170.AdmissibleDrawing graph) (FiniteIntersectionDrawing graph) :=
  ⟨toFiniteIntersectionDrawing⟩

def finiteCrossingCount  (D : FiniteIntersectionDrawing graph) : ℕ :=
  D.crossings_finite.toFinset.card

theorem finiteCrossingCount_of_admissible  (D : Paper170.AdmissibleDrawing graph) :
    finiteCrossingCount (D : FiniteIntersectionDrawing graph)=Paper170.crossingCount D := rfl

theorem finiteCrossingCount_eq_ncard  (D : FiniteIntersectionDrawing graph) :
    finiteCrossingCount D=(Paper170.crossingPoints D.toContinuousDrawing).ncard :=
  (Set.ncard_eq_toFinset_card _ D.crossings_finite).symm

end Paper170Geometry

namespace Paper170.AdmissibleDrawing

abbrev toFiniteIntersectionDrawing {V E : Type} {G : Graph V E} (D : AdmissibleDrawing G) :
    Paper170Geometry.FiniteIntersectionDrawing G := Paper170Geometry.toFiniteIntersectionDrawing D

end Paper170.AdmissibleDrawing

end
end

section

/-! Finite point incidence for actual drawings, retaining repeated pair crossings. -/
noncomputable section
open Zarankiewicz
variable {Vertex Edge : Type} {graph : Paper170.Graph Vertex Edge}

namespace Paper170Geometry
open scoped BigOperators Classical
/-- A minimum exists once an actual admissible drawing has been constructed.
This is the well-ordering step; construction of a drawing remains a separate obligation. -/
theorem crossingCount_attainment (D₀ : Paper170.AdmissibleDrawing graph) :
    ∃ D : Paper170.AdmissibleDrawing graph, Paper170.crossingCount D = Paper170.ordinaryCrossingNumber graph := by
  exact csInf_mem (show (Set.range (@Paper170.crossingCount Vertex Edge graph)).Nonempty from
    ⟨Paper170.crossingCount D₀, D₀, rfl⟩)

theorem ordinaryCrossingNumber_le_count (D : Paper170.AdmissibleDrawing graph) :
    Paper170.ordinaryCrossingNumber graph ≤ Paper170.crossingCount D :=
  csInf_le' ⟨D, rfl⟩

theorem crossing_incident_iff (D : Paper170.AdmissibleDrawing graph) (x : Plane)
    (e f : Edge) (hef : e ≠ f)
    (he : x ∈ Paper170.edgeInterior D.toContinuousDrawing e)
    (hf : x ∈ Paper170.edgeInterior D.toContinuousDrawing f) (g : Edge) :
    x ∈ Paper170.edgeInterior D.toContinuousDrawing g ↔ g = e ∨ g = f := by
  constructor
  · intro hg
    by_contra hh
    have hge : e ≠ g := by tauto
    have hgf : f ≠ g := by tauto
    have hmem : x ∈ Paper170.edgeInterior D.toContinuousDrawing e ∩ Paper170.edgeInterior D.toContinuousDrawing f ∩
        Paper170.edgeInterior D.toContinuousDrawing g := ⟨⟨he,hf⟩,hg⟩
    rw [D.no_triples e f g hef hge hgf] at hmem
    exact hmem
  · rintro (rfl | rfl) <;> assumption

/-- Every spatial crossing has exactly two incident edges; no bound is imposed
on the number of points common to that edge pair. -/
theorem crossing_two_edges (D : Paper170.AdmissibleDrawing graph) (x : Plane)
    (hx : x ∈ Paper170.crossingPoints D.toContinuousDrawing) :
    ∃ e f : Edge, e ≠ f ∧
      ∀ g, x ∈ Paper170.edgeInterior D.toContinuousDrawing g ↔ g = e ∨ g = f := by
  obtain ⟨e,f,hef,he,hf⟩ := hx
  exact ⟨e,f,hef,crossing_incident_iff D x e f hef he hf⟩

/-- The actual finite set of crossing points for a specified ordered edge pair. -/
def pairCrossings (D : Paper170.AdmissibleDrawing graph) (e f : Edge) : Finset Plane :=
  D.crossings_finite.toFinset.filter (fun x => e ≠ f ∧
    x ∈ Paper170.edgeInterior D.toContinuousDrawing e ∧ x ∈ Paper170.edgeInterior D.toContinuousDrawing f)

theorem pairCrossings_self (D : Paper170.AdmissibleDrawing graph) (e : Edge) :
    pairCrossings D e e = ∅ := by
  simp [pairCrossings]

theorem pairCrossings_comm (D : Paper170.AdmissibleDrawing graph) (e f : Edge) :
    pairCrossings D e f = pairCrossings D f e := by
  ext x
  simp only [pairCrossings, Finset.mem_filter]
  tauto

variable [Fintype Edge]

/-- Each crossing point contributes two ordered edge pairs. -/
theorem crossing_ordered_incidence (D : Paper170.AdmissibleDrawing graph) (x : Plane)
    (hx : x ∈ Paper170.crossingPoints D.toContinuousDrawing) :
    (∑ e : Edge, ∑ f : Edge,
      if e ≠ f ∧ x ∈ Paper170.edgeInterior D.toContinuousDrawing e ∧
        x ∈ Paper170.edgeInterior D.toContinuousDrawing f then (1 : ℕ) else 0) = 2 := by
  classical
  obtain ⟨e,f,hef,hinc⟩ := crossing_two_edges D x hx
  simp_rw [hinc]
  have hp : ∀ a b : Edge,
      (a ≠ b ∧ (a = e ∨ a = f) ∧ (b = e ∨ b = f)) ↔
        ((a = e ∧ b = f) ∨ (a = f ∧ b = e)) := by
    intro a b
    constructor
    · rintro ⟨hab, rfl | rfl, rfl | rfl⟩ <;> tauto
    · rintro (⟨rfl,rfl⟩ | ⟨rfl,rfl⟩) <;> tauto
  simp_rw [hp]
  have hi : ∀ a b : Edge,
      (if (a = e ∧ b = f) ∨ (a = f ∧ b = e) then (1 : ℕ) else 0) =
        (if a = e ∧ b = f then 1 else 0) + (if a = f ∧ b = e then 1 else 0) := by
    intro a b
    split_ifs <;> simp_all
  simp_rw [hi, Finset.sum_add_distrib]
  simp [ite_and]

/-- Sum over ordered edge pairs equals twice the number of spatial crossing points. -/
theorem sum_pairCrossings_card (D : Paper170.AdmissibleDrawing graph) :
    (∑ e : Edge, ∑ f : Edge, (pairCrossings D e f).card) =
      2 * Paper170.crossingCount D := by
  classical
  simp only [pairCrossings, Finset.card_filter]
  rw [Finset.sum_comm]
  conv_lhs => arg 2; ext f; rw [Finset.sum_comm]
  rw [Finset.sum_comm]
  have he : ∀ x ∈ D.crossings_finite.toFinset,
      (∑ f : Edge, ∑ e : Edge,
        if e ≠ f ∧ x ∈ Paper170.edgeInterior D.toContinuousDrawing e ∧
          x ∈ Paper170.edgeInterior D.toContinuousDrawing f then (1 : ℕ) else 0) = 2 := by
    intro x hx
    rw [Finset.sum_comm]
    exact crossing_ordered_incidence D x (by simpa using hx)
  simp_rw [Finset.sum_congr rfl he]
  simp [Paper170.crossingCount, Nat.mul_comm]

end Paper170Geometry

end
end

end OAI