1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_VIZ_SERVICE_DISPLAY_DRAW_POLYGON_H_
6 #define COMPONENTS_VIZ_SERVICE_DISPLAY_DRAW_POLYGON_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "components/viz/service/viz_service_export.h"
12 #include "ui/gfx/geometry/point3_f.h"
13 #include "ui/gfx/geometry/quad_f.h"
14 #include "ui/gfx/geometry/rect_f.h"
15 #include "ui/gfx/geometry/vector3d_f.h"
16 #include "ui/gfx/transform.h"
17 
18 namespace viz {
19 class DrawQuad;
20 
21 class VIZ_SERVICE_EXPORT DrawPolygon {
22  public:
23   DrawPolygon();
24   ~DrawPolygon();
25 
26   DrawPolygon(const DrawQuad* original_ref,
27               const std::vector<gfx::Point3F>& in_points,
28               const gfx::Vector3dF& normal,
29               int draw_order_index = 0);
30   DrawPolygon(const DrawQuad* original_ref,
31               const gfx::RectF& visible_layer_rect,
32               const gfx::Transform& transform,
33               int draw_order_index = 0);
34 
35   // Split takes this DrawPolygon and splits it into two pieces that are on
36   // either side of |splitter|. Any edges of this polygon that cross the plane
37   // of |splitter| will have an intersection point that is shared by both
38   // polygons on either side.
39   // Split will only return true if it determines that we got back 2
40   // intersection points. Only when it returns true will front and back both be
41   // valid new polygons that are on opposite sides of the splitting plane.
42   void SplitPolygon(std::unique_ptr<DrawPolygon> polygon,
43                     std::unique_ptr<DrawPolygon>* front,
44                     std::unique_ptr<DrawPolygon>* back,
45                     bool* is_coplanar) const;
46   float SignedPointDistance(const gfx::Point3F& point) const;
47   void ToQuads2D(std::vector<gfx::QuadF>* quads) const;
48   void TransformToScreenSpace(const gfx::Transform& transform);
49   void TransformToLayerSpace(const gfx::Transform& inverse_transform);
50 
points()51   const std::vector<gfx::Point3F>& points() const { return points_; }
normal()52   const gfx::Vector3dF& normal() const { return normal_; }
original_ref()53   const DrawQuad* original_ref() const { return original_ref_; }
order_index()54   int order_index() const { return order_index_; }
is_split()55   bool is_split() const { return is_split_; }
56   std::unique_ptr<DrawPolygon> CreateCopy();
57 
58   // These are helper functions for testing.
59   void RecomputeNormalForTesting();
60   friend bool IsPlanarForTesting(const DrawPolygon& p);
61   friend bool IsConvexForTesting(const DrawPolygon& p);
62 
63  private:
64   void ApplyTransform(const gfx::Transform& transform);
65   void ApplyTransformToNormal(const gfx::Transform& transform);
66 
67   void ConstructNormal();
68 
69   std::vector<gfx::Point3F> points_;
70   // Normalized, necessitated by distance calculations and tests of coplanarity.
71   gfx::Vector3dF normal_;
72   // This is an index that can be used to test whether a quad comes before or
73   // after another in document order, useful for tie-breaking when it comes
74   // to coplanar surfaces.
75   int order_index_;
76   // The pointer to the original quad, which gives us all the drawing info
77   // we need.
78   // This DrawQuad is owned by the caller and its lifetime must be preserved
79   // as long as this DrawPolygon is alive.
80   const DrawQuad* original_ref_;
81   bool is_split_;
82 };
83 
84 }  // namespace viz
85 
86 #endif  // COMPONENTS_VIZ_SERVICE_DISPLAY_DRAW_POLYGON_H_
87