BufferParameters.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. export default class BufferParameters {
  2. constructor() {
  3. BufferParameters.constructor_.apply(this, arguments)
  4. }
  5. static constructor_() {
  6. this._quadrantSegments = BufferParameters.DEFAULT_QUADRANT_SEGMENTS
  7. this._endCapStyle = BufferParameters.CAP_ROUND
  8. this._joinStyle = BufferParameters.JOIN_ROUND
  9. this._mitreLimit = BufferParameters.DEFAULT_MITRE_LIMIT
  10. this._isSingleSided = false
  11. this._simplifyFactor = BufferParameters.DEFAULT_SIMPLIFY_FACTOR
  12. if (arguments.length === 0) {} else if (arguments.length === 1) {
  13. const quadrantSegments = arguments[0]
  14. this.setQuadrantSegments(quadrantSegments)
  15. } else if (arguments.length === 2) {
  16. const quadrantSegments = arguments[0], endCapStyle = arguments[1]
  17. this.setQuadrantSegments(quadrantSegments)
  18. this.setEndCapStyle(endCapStyle)
  19. } else if (arguments.length === 4) {
  20. const quadrantSegments = arguments[0], endCapStyle = arguments[1], joinStyle = arguments[2], mitreLimit = arguments[3]
  21. this.setQuadrantSegments(quadrantSegments)
  22. this.setEndCapStyle(endCapStyle)
  23. this.setJoinStyle(joinStyle)
  24. this.setMitreLimit(mitreLimit)
  25. }
  26. }
  27. static bufferDistanceError(quadSegs) {
  28. const alpha = Math.PI / 2.0 / quadSegs
  29. return 1 - Math.cos(alpha / 2.0)
  30. }
  31. getEndCapStyle() {
  32. return this._endCapStyle
  33. }
  34. isSingleSided() {
  35. return this._isSingleSided
  36. }
  37. setQuadrantSegments(quadSegs) {
  38. this._quadrantSegments = quadSegs
  39. if (this._quadrantSegments === 0) this._joinStyle = BufferParameters.JOIN_BEVEL
  40. if (this._quadrantSegments < 0) {
  41. this._joinStyle = BufferParameters.JOIN_MITRE
  42. this._mitreLimit = Math.abs(this._quadrantSegments)
  43. }
  44. if (quadSegs <= 0)
  45. this._quadrantSegments = 1
  46. if (this._joinStyle !== BufferParameters.JOIN_ROUND)
  47. this._quadrantSegments = BufferParameters.DEFAULT_QUADRANT_SEGMENTS
  48. }
  49. getJoinStyle() {
  50. return this._joinStyle
  51. }
  52. setJoinStyle(joinStyle) {
  53. this._joinStyle = joinStyle
  54. }
  55. setSimplifyFactor(simplifyFactor) {
  56. this._simplifyFactor = simplifyFactor < 0 ? 0 : simplifyFactor
  57. }
  58. getSimplifyFactor() {
  59. return this._simplifyFactor
  60. }
  61. getQuadrantSegments() {
  62. return this._quadrantSegments
  63. }
  64. setEndCapStyle(endCapStyle) {
  65. this._endCapStyle = endCapStyle
  66. }
  67. getMitreLimit() {
  68. return this._mitreLimit
  69. }
  70. setMitreLimit(mitreLimit) {
  71. this._mitreLimit = mitreLimit
  72. }
  73. setSingleSided(isSingleSided) {
  74. this._isSingleSided = isSingleSided
  75. }
  76. }
  77. BufferParameters.CAP_ROUND = 1
  78. BufferParameters.CAP_FLAT = 2
  79. BufferParameters.CAP_SQUARE = 3
  80. BufferParameters.JOIN_ROUND = 1
  81. BufferParameters.JOIN_MITRE = 2
  82. BufferParameters.JOIN_BEVEL = 3
  83. BufferParameters.DEFAULT_QUADRANT_SEGMENTS = 8
  84. BufferParameters.DEFAULT_MITRE_LIMIT = 5.0
  85. BufferParameters.DEFAULT_SIMPLIFY_FACTOR = 0.01