EdgeGraph.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import HashMap from '../../../../java/util/HashMap'
  2. import HalfEdge from './HalfEdge'
  3. export default class EdgeGraph {
  4. constructor() {
  5. EdgeGraph.constructor_.apply(this, arguments)
  6. }
  7. static constructor_() {
  8. this._vertexMap = new HashMap()
  9. }
  10. static isValidEdge(orig, dest) {
  11. const cmp = dest.compareTo(orig)
  12. return cmp !== 0
  13. }
  14. insert(orig, dest, eAdj) {
  15. const e = this.create(orig, dest)
  16. if (eAdj !== null)
  17. eAdj.insert(e)
  18. else
  19. this._vertexMap.put(orig, e)
  20. const eAdjDest = this._vertexMap.get(dest)
  21. if (eAdjDest !== null)
  22. eAdjDest.insert(e.sym())
  23. else
  24. this._vertexMap.put(dest, e.sym())
  25. return e
  26. }
  27. create(p0, p1) {
  28. const e0 = this.createEdge(p0)
  29. const e1 = this.createEdge(p1)
  30. e0.link(e1)
  31. return e0
  32. }
  33. createEdge(orig) {
  34. return new HalfEdge(orig)
  35. }
  36. addEdge(orig, dest) {
  37. if (!EdgeGraph.isValidEdge(orig, dest)) return null
  38. const eAdj = this._vertexMap.get(orig)
  39. let eSame = null
  40. if (eAdj !== null)
  41. eSame = eAdj.find(dest)
  42. if (eSame !== null)
  43. return eSame
  44. const e = this.insert(orig, dest, eAdj)
  45. return e
  46. }
  47. getVertexEdges() {
  48. return this._vertexMap.values()
  49. }
  50. findEdge(orig, dest) {
  51. const e = this._vertexMap.get(orig)
  52. if (e === null) return null
  53. return e.find(dest)
  54. }
  55. }