This repository has been archived by the owner on Nov 22, 2017. It is now read-only.
/
dcel.go
685 lines (602 loc) · 15 KB
/
dcel.go
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
package dcel
import (
"fmt"
"github.com/gonum/graph"
)
var (
dcelGraph *Graph
_ graph.Undirected = dcelGraph
)
// Graph implements the doubly-connected edge list data structure.
type Graph struct {
items Items
nodes map[int]Node
edges map[int]Edge
faces map[int]Face
nextNodeID int
nextEdgeID int
nextFaceID int
freeNodes map[int]struct{}
freeEdges map[int]struct{}
freeFaces map[int]struct{}
}
// New returns a new Graph. If items is nil, Base will be used.
func New(items Items) *Graph {
if items == nil {
items = Base{}
}
return &Graph{
items: items,
nodes: make(map[int]Node),
edges: make(map[int]Edge),
faces: make(map[int]Face),
freeNodes: make(map[int]struct{}),
freeEdges: make(map[int]struct{}),
freeFaces: make(map[int]struct{}),
}
}
// Node returns the node with the given id or nil if it does not exist within
// the graph.
func (g *Graph) Node(id int) Node { return g.nodes[id] }
// Face returns the face with the given id or nil if it does not exist within
// the graph.
func (g *Graph) Face(id int) Face { return g.faces[id] }
// Edge returns the edge with the given id or nil if it does not exist within
// the graph.
// func (g *Graph) Edge(id int) Edge { return g.edges[id] }
// Has returns whether a node with the id given by x.ID() exists within the graph.
func (g *Graph) Has(x graph.Node) bool {
return g.has(x.ID())
}
// has returns whether a node with the given id exists within the graph.
func (g *Graph) has(id int) bool {
_, exists := g.nodes[id]
return exists
}
// Nodes returns all the nodes in the graph.
func (g *Graph) Nodes() []graph.Node {
var nodes []graph.Node
for _, u := range g.nodes {
nodes = append(nodes, u)
}
return nodes
}
// Edges returns all the edges in the graph.
func (g *Graph) Edges() []graph.Edge {
var edges []graph.Edge
for _, e := range g.edges {
edges = append(edges, e)
}
return edges
}
// Faces returns all the faces in the graph.
func (g *Graph) Faces() []Face {
var faces []Face
for _, f := range g.faces {
faces = append(faces, f)
}
return faces
}
// From returns all neighbors of the node x.
func (g *Graph) From(x graph.Node) []graph.Node {
u := g.Node(x.ID())
if u == nil {
return nil
}
if u.Halfedge() == nil {
// Node n is isolated, so there are no neighbors.
return nil
}
var (
from []graph.Node
start = u.Halfedge().Twin() // An incoming halfedge to u.
)
for iter := start; ; {
from = append(from, iter.From())
iter = iter.Next().Twin()
if iter == start {
break
}
}
return from
}
// HasEdge returns whether an edge exists between nodes x and y.
func (g *Graph) HasEdge(x, y graph.Node) bool {
return g.Halfedge(x, y) != nil
}
// Edge returns the edge between x and y or nil if the nodes are not connected.
func (g *Graph) Edge(x, y graph.Node) graph.Edge {
return g.EdgeBetween(x, y)
}
// EdgeBetween returns the edge between x and y or nil if the nodes are not
// connected.
func (g *Graph) EdgeBetween(x, y graph.Node) graph.Edge {
he := g.Halfedge(x, y)
if he == nil {
return nil
}
return he.Edge()
}
// Halfedge returns the halfedge from x to y, or nil if the nodes are not
// connected by an edge or at least one is isolated.
func (g *Graph) Halfedge(x, y graph.Node) Halfedge {
u := g.Node(x.ID())
v := g.Node(y.ID())
if u == nil || v == nil {
// One of the nodes does not belong to the graph.
return nil
}
if u.Halfedge() == nil || v.Halfedge() == nil {
// At least one of the nodes is isolated.
return nil
}
start := u.Halfedge() // An outgoing halfedge from u.
for iter := start; ; {
if iter.Twin().From() == v {
return iter
}
iter = iter.Twin().Next()
if iter == start {
break
}
}
// If we get here, the nodes are not connected.
return nil
}
// NewNodeID returns a new node id unique within the graph.
func (g *Graph) NewNodeID() int {
if g.nextNodeID != maxInt {
id := g.nextNodeID
g.nextNodeID++
return id
}
// All node IDs have already been used. See if at least one has been
// released.
for id := range g.freeNodes {
return id
}
if len(g.nodes) == maxInt {
panic("dcel: graph too large")
}
// Resort to checking all positive integers to see if there is at least one
// unused.
for id := 0; id < maxInt; id++ {
if _, exists := g.nodes[id]; !exists {
return id
}
}
panic("dcel: no free node ID")
}
// AddNode adds a new, isolated node with the given id to the graph and returns it.
// AddNode panics if a node with same id already exists in the graph.
func (g *Graph) AddNode(id int) Node {
if g.has(id) {
panic(fmt.Sprintf("dcel: node ID collision: %d", id))
}
u := g.items.NewNode(id)
u.SetHalfedge(nil)
g.nodes[id] = u
delete(g.freeNodes, id)
g.nextNodeID = max(g.nextNodeID, id)
return u
}
// RemoveNode removes the node with ID given by x.ID() from the graph as well
// as any edges attached to it.
func (g *Graph) RemoveNode(x graph.Node) {
id := x.ID()
if !g.has(id) {
// Nothing to do.
return
}
// Remove any attached edges.
for _, h := range g.HalfedgesFrom(x) {
g.RemoveEdge(h.Edge())
}
g.Node(id).SetHalfedge(nil) // Avoid memory leaks.
delete(g.nodes, id)
if g.nextNodeID != 0 && id == g.nextNodeID {
g.nextNodeID--
}
g.freeNodes[id] = struct{}{}
}
// addEdge adds a new edge between nodes identified by x.ID() and y.ID() and
// returns its halfedge from x to y. If the nodes are not in the graph, they
// are added.
//
// addEdge panics if x.ID() == y.ID().
//
// It is not exported because edges cannot be added individualy, they are added
// only when adding faces.
func (g *Graph) addEdge(x, y graph.Node) (Halfedge, error) {
if x.ID() == y.ID() {
panic(fmt.Sprintf("dcel: trying to set a loop edge at node %d", x.ID()))
}
h := g.Halfedge(x, y)
if h != nil {
// Edge between x and y already exists, so return the halfedge.
return h, nil
}
// Add any missing node.
var u, v Node
if !g.Has(x) {
u = g.AddNode(x.ID())
} else {
u = g.Node(x.ID())
}
if !g.Has(y) {
v = g.AddNode(y.ID())
} else {
v = g.Node(y.ID())
}
// Allocate a new edge and attach it to the graph.
e := g.newEdge()
h1, h2 := e.Halfedges()
if err := attach(h1, u); err != nil {
return nil, err
}
if err := attach(h2, v); err != nil {
detach(h1)
return nil, err
}
id := e.ID()
g.edges[id] = e
delete(g.freeEdges, id)
g.nextEdgeID = max(g.nextEdgeID, id)
return h1, nil
}
// newEdge allocates a new, properly initialized Edge not connected to any
// node.
func (g *Graph) newEdge() Edge {
h1 := g.items.NewHalfedge()
h2 := g.items.NewHalfedge()
e := g.items.NewEdge(g.newEdgeID())
h1.SetFrom(nil)
h2.SetFrom(nil)
h1.SetTwin(h2)
h2.SetTwin(h1)
h1.SetNext(h2)
h2.SetNext(h1)
h1.SetPrev(h2)
h2.SetPrev(h1)
h1.SetFace(nil)
h2.SetFace(nil)
h1.SetEdge(e)
h2.SetEdge(e)
e.SetHalfedges(h1, h2)
return e
}
func attach(h Halfedge, u Node) error {
h.SetFrom(u)
if u.Halfedge() == nil {
// From node is isolated.
u.SetHalfedge(h)
h.SetPrev(h.Twin())
h.Twin().SetNext(h)
return nil
}
// From node is not isolated, so we must update its neighboring halfedges.
// First find a free (i.e., without an adjacent face) halfedge from u.
out := u.Halfedge()
for {
if out.Face() == nil {
break
}
out = out.Twin().Next()
if out == u.Halfedge() {
return fmt.Errorf("dcel: no free halfedge from node %d", u.ID())
}
}
// Adjust the connections.
in := out.Prev()
in.SetNext(h)
h.SetPrev(in)
h.Twin().SetNext(out)
out.SetPrev(h.Twin())
return nil
}
// RemoveEdge removes the edge between nodes identified by e.From and e.To and
// its adjacent faces from g.
func (g *Graph) RemoveEdge(e graph.Edge) {
h := g.Halfedge(e.From(), e.To())
if h == nil {
// Nothing to do.
return
}
// Remove any adjacent faces.
if h.Face() != nil {
g.RemoveFace(h.Face())
}
if h.Twin().Face() != nil {
g.RemoveFace(h.Twin().Face())
}
// Detach both halfedges from their From nodes and update affected
// halfedges.
detach(h)
detach(h.Twin())
id := h.Edge().ID()
delete(g.edges, id)
if g.nextEdgeID != 0 && id == g.nextEdgeID {
g.nextEdgeID--
}
g.freeEdges[id] = struct{}{}
}
func detach(h Halfedge) {
if h.Face() != nil {
panic("dcel: face not removed before detaching halfedge")
}
out := h.Twin().Next()
in := h.Prev()
from := h.From()
if from.Halfedge() == h {
// h is the halfedge referenced by its from node.
if out == h {
// It is also the only halfedge adjacent to the from node, so it
// will become isolated.
from.SetHalfedge(nil)
} else {
if out.Face() != nil {
panic("dcel: outgoing halfedge is not free")
}
from.SetHalfedge(out)
}
}
out.SetPrev(in)
in.SetNext(out)
// Avoid memory leaks.
// TODO(vladimir-ch): Consider having a pool of reusable Edges.
h.SetFrom(nil)
h.SetTwin(nil)
h.SetNext(nil)
h.SetPrev(nil)
h.SetEdge(nil)
}
// HasFace returns whether a face with the given id exists in the graph.
func (g *Graph) HasFace(id int) bool {
_, exists := g.faces[id]
return exists
}
// AddFace adds a new face with given ID and with vertices given by nodes.
// Any missing node or edge between two consecutive nodes will be added to the
// graph first.
//
// If the nodes are not pair-wise distinct, if two consecutive nodes are
// already connected by a halfedge with an adjacent Face, or if the existing
// graph topology does not permit adding the face, an error will be returned.
//
// AddFace panics if a face with the given id already exists in the graph or if
// the length of nodes is less than 3.
func (g *Graph) AddFace(id int, nodes ...graph.Node) error {
if g.HasFace(id) {
panic(fmt.Sprintf("dcel: face ID collision: %d", id))
}
if len(nodes) < 3 {
panic(fmt.Sprintf("dcel: cannot add face %d with only %d nodes", id, len(nodes)))
}
// Check that the nodes are pair-wise distinct.
for i, x := range nodes {
for j := i + 1; j < len(nodes); j++ {
if x.ID() == nodes[j].ID() {
return fmt.Errorf("dcel: cannot add face %d, duplicit node %d", id, x.ID())
}
}
}
// Collect (and add any missing) halfedges between consecutive nodes.
// Absent nodes are added in addEdge().
var hedges []Halfedge
for i, x := range nodes {
y := nodes[(i+1)%len(nodes)]
h, err := g.addEdge(x, y)
if err != nil {
return err
}
if h.Face() != nil {
return fmt.Errorf("dcel: cannot add face %d, halfedge from %d to %d is not free",
id, x.ID(), y.ID())
}
hedges = append(hedges, h)
}
// Reconnect the halfedges so the Next and Prev point to consecutive
// neighbors.
for i, h1 := range hedges {
h2 := hedges[(i+1)%len(hedges)]
if err := reconnect(h1, h2); err != nil {
return err
}
}
// Allocate new face and set its halfedge.
f := g.items.NewFace(id)
f.SetHalfedge(hedges[0])
// Set the face of adjacent halfedges.
for _, h := range hedges {
h.SetFace(f)
}
g.faces[id] = f
delete(g.freeFaces, id)
g.nextFaceID = max(g.nextFaceID, id)
return nil
}
// reconnect adjusts the halfedges around the shared node between in and out so
// that in.Next() == out and out.Prev() == in.
// It panics if in and out do not share a common node.
func reconnect(in, out Halfedge) error {
if in.Twin().From() != out.From() {
panic("dcel.reconnect: halfedges are not connected")
}
if in.Next() == out || out.Prev() == in {
if in.Next() != out || out.Prev() != in {
// This would be our bug.
panic(fmt.Sprintf("dcel.reconnect: halfedges around node %d are inconsistently connected",
out.From().ID()))
}
// in and out are already adjacent.
return nil
}
// Find a free incoming halfedge adjacent to the common node between
// out.Twin() and in.
var b Halfedge
for iter := out.Twin(); ; {
if iter.Face() == nil {
b = iter
break
}
iter = iter.Next().Twin()
if iter == in {
break
}
}
if b == nil {
return fmt.Errorf("dcel: halfedge reconnection failed around node %d", out.From().ID())
}
// Reconnect the halfedges.
inNext := in.Next()
outPrev := out.Prev()
bNext := b.Next()
in.SetNext(out)
out.SetPrev(in)
b.SetNext(inNext)
inNext.SetPrev(b)
outPrev.SetNext(bNext)
bNext.SetPrev(outPrev)
return nil
}
// RemoveFace disconnects f from g and sets its Halfedge to nil.
func (g *Graph) RemoveFace(f Face) {
id := f.ID()
if _, exists := g.faces[id]; !exists {
// Nothing to do, a face with such id does not exist in the graph.
return
}
// Disconnect the face from its adjacent halfedges.
for _, h := range g.HalfedgesAround(f) {
h.SetFace(nil)
}
f.SetHalfedge(nil)
delete(g.faces, id)
if g.nextFaceID != 0 && id == g.nextFaceID {
g.nextFaceID--
}
g.freeFaces[id] = struct{}{}
}
func (g *Graph) newEdgeID() int {
if g.nextEdgeID != maxInt {
id := g.nextEdgeID
g.nextEdgeID++
return id
}
// All edge IDs have already been used. See if at least one has been
// released.
for id := range g.freeEdges {
return id
}
if len(g.edges) == maxInt {
panic("dcel: graph too large")
}
// Resort to checking all positive integers to see if there is at least one
// unused.
for id := 0; id < maxInt; id++ {
if _, exists := g.edges[id]; !exists {
return id
}
}
panic("dcel: no free edge ID")
}
// NewFaceID returns a new face id unique within the graph.
func (g *Graph) NewFaceID() int {
if g.nextFaceID != maxInt {
id := g.nextFaceID
g.nextFaceID++
return id
}
// All face IDs have already been used. See if at least one has been
// released.
for id := range g.freeFaces {
return id
}
if len(g.faces) == maxInt {
panic("dcel: graph too large")
}
// Resort to checking all positive integers to see if there is at least one
// unused.
for id := 0; id < maxInt; id++ {
if _, exists := g.faces[id]; !exists {
return id
}
}
panic("dcel: no free face ID")
}
// HalfedgesFrom returns all halfedges whose From node is x.
func (g *Graph) HalfedgesFrom(x graph.Node) []Halfedge {
u := g.Node(x.ID())
if u == nil {
// The node does not belong to the graph.
return nil
}
if u.Halfedge() == nil {
// The node is isolated.
return nil
}
var (
hedges []Halfedge
start = u.Halfedge() // An outgoing halfedge from u.
)
for iter := start; ; {
hedges = append(hedges, iter)
iter = iter.Twin().Next()
if iter == start {
break
}
}
return hedges
}
// HalfedgesTo returns all halfedges whose Twin.From node is x.
func (g *Graph) HalfedgesTo(x graph.Node) []Halfedge {
u := g.Node(x.ID())
if u == nil {
// The node does not belong to the graph.
return nil
}
if u.Halfedge() == nil {
// The node is isolated.
return nil
}
var (
hedges []Halfedge
start = u.Halfedge().Twin() // An incoming halfedge to u.
)
for iter := start; ; {
hedges = append(hedges, iter)
iter = iter.Next().Twin()
if iter == start {
break
}
}
return hedges
}
// HalfedgesAround returns all halfedges adjacent to the given face.
func (g *Graph) HalfedgesAround(f Face) []Halfedge {
if _, exists := g.faces[f.ID()]; !exists {
return nil
}
if f.Halfedge() == nil {
return nil
}
var (
hedges []Halfedge
start = f.Halfedge()
)
for iter := start; ; {
hedges = append(hedges, iter)
iter = iter.Next()
if iter == start {
break
}
}
return hedges
}
func max(a, b int) int {
if a < b {
return b
}
return a
}
const maxInt int = int(^uint(0) >> 1)