Path.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/HashMap.h>
  28. #include <AK/Optional.h>
  29. #include <AK/Vector.h>
  30. #include <LibGfx/FloatPoint.h>
  31. #include <LibGfx/Forward.h>
  32. namespace Gfx {
  33. class Path {
  34. public:
  35. struct Segment {
  36. enum class Type {
  37. Invalid,
  38. MoveTo,
  39. LineTo,
  40. QuadraticBezierCurveTo,
  41. };
  42. Type type { Type::Invalid };
  43. FloatPoint point;
  44. Optional<FloatPoint> through {};
  45. };
  46. Path() { }
  47. void move_to(const FloatPoint& point)
  48. {
  49. m_segments.append({ Segment::Type::MoveTo, point });
  50. }
  51. void line_to(const FloatPoint& point)
  52. {
  53. m_segments.append({ Segment::Type::LineTo, point });
  54. invalidate_split_lines();
  55. }
  56. void quadratic_bezier_curve_to(const FloatPoint& through, const FloatPoint& point)
  57. {
  58. m_segments.append({ Segment::Type::QuadraticBezierCurveTo, point, through });
  59. invalidate_split_lines();
  60. }
  61. void close();
  62. struct LineSegment {
  63. Point from, to;
  64. float inverse_slope;
  65. float x_of_minimum_y;
  66. float maximum_y;
  67. float minimum_y;
  68. float x;
  69. };
  70. enum ShapeKind {
  71. Simple,
  72. Complex,
  73. };
  74. const Vector<Segment>& segments() const { return m_segments; }
  75. Vector<LineSegment> split_lines(ShapeKind);
  76. const auto& split_lines()
  77. {
  78. if (m_split_lines.has_value())
  79. return m_split_lines.value();
  80. segmentize_path();
  81. ASSERT(m_split_lines.has_value());
  82. return m_split_lines.value();
  83. }
  84. String to_string() const;
  85. private:
  86. void invalidate_split_lines()
  87. {
  88. m_split_lines.clear();
  89. m_graph_node_map.clear();
  90. }
  91. void segmentize_path();
  92. void generate_path_graph();
  93. bool is_part_of_closed_polygon(const Point& p0, const Point& p1);
  94. static unsigned hash_line(const Point& from, const Point& to);
  95. Vector<Segment> m_segments;
  96. Optional<Vector<LineSegment>> m_split_lines {};
  97. struct PathGraphNode {
  98. PathGraphNode(u32 hash, const LineSegment& line)
  99. : hash(hash)
  100. , line(line)
  101. {
  102. }
  103. Vector<const PathGraphNode*> children;
  104. u32 hash;
  105. LineSegment line;
  106. };
  107. Optional<HashMap<u32, OwnPtr<PathGraphNode>>> m_graph_node_map;
  108. };
  109. inline const LogStream& operator<<(const LogStream& stream, const Path& path)
  110. {
  111. return stream << path.to_string();
  112. }
  113. }