- add sources.
[platform/framework/web/crosswalk.git] / src / ui / gfx / quad_f.cc
1 // Copyright (c) 2012 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 #include "ui/gfx/quad_f.h"
6
7 #include <limits>
8
9 #include "base/strings/stringprintf.h"
10
11 namespace gfx {
12
13 void QuadF::operator=(const RectF& rect) {
14   p1_ = PointF(rect.x(), rect.y());
15   p2_ = PointF(rect.right(), rect.y());
16   p3_ = PointF(rect.right(), rect.bottom());
17   p4_ = PointF(rect.x(), rect.bottom());
18 }
19
20 std::string QuadF::ToString() const {
21   return base::StringPrintf("%s;%s;%s;%s",
22                             p1_.ToString().c_str(),
23                             p2_.ToString().c_str(),
24                             p3_.ToString().c_str(),
25                             p4_.ToString().c_str());
26 }
27
28 static inline bool WithinEpsilon(float a, float b) {
29   return std::abs(a - b) < std::numeric_limits<float>::epsilon();
30 }
31
32 bool QuadF::IsRectilinear() const {
33   return
34       (WithinEpsilon(p1_.x(), p2_.x()) && WithinEpsilon(p2_.y(), p3_.y()) &&
35        WithinEpsilon(p3_.x(), p4_.x()) && WithinEpsilon(p4_.y(), p1_.y())) ||
36       (WithinEpsilon(p1_.y(), p2_.y()) && WithinEpsilon(p2_.x(), p3_.x()) &&
37        WithinEpsilon(p3_.y(), p4_.y()) && WithinEpsilon(p4_.x(), p1_.x()));
38 }
39
40 bool QuadF::IsCounterClockwise() const {
41   // This math computes the signed area of the quad. Positive area
42   // indicates the quad is clockwise; negative area indicates the quad is
43   // counter-clockwise. Note carefully: this is backwards from conventional
44   // math because our geometric space uses screen coordiantes with y-axis
45   // pointing downards.
46   // Reference: http://mathworld.wolfram.com/PolygonArea.html
47
48   // Up-cast to double so this cannot overflow.
49   double determinant1 = static_cast<double>(p1_.x()) * p2_.y()
50       - static_cast<double>(p2_.x()) * p1_.y();
51   double determinant2 = static_cast<double>(p2_.x()) * p3_.y()
52       - static_cast<double>(p3_.x()) * p2_.y();
53   double determinant3 = static_cast<double>(p3_.x()) * p4_.y()
54       - static_cast<double>(p4_.x()) * p3_.y();
55   double determinant4 = static_cast<double>(p4_.x()) * p1_.y()
56       - static_cast<double>(p1_.x()) * p4_.y();
57
58   return determinant1 + determinant2 + determinant3 + determinant4 < 0;
59 }
60
61 static inline bool PointIsInTriangle(const PointF& point,
62                                      const PointF& r1,
63                                      const PointF& r2,
64                                      const PointF& r3) {
65   // Compute the barycentric coordinates of |point| relative to the triangle
66   // (r1, r2, r3). This algorithm comes from Christer Ericson's Real-Time
67   // Collision Detection.
68   Vector2dF v0 = r2 - r1;
69   Vector2dF v1 = r3 - r1;
70   Vector2dF v2 = point - r1;
71
72   double dot00 = DotProduct(v0, v0);
73   double dot01 = DotProduct(v0, v1);
74   double dot11 = DotProduct(v1, v1);
75   double dot20 = DotProduct(v2, v0);
76   double dot21 = DotProduct(v2, v1);
77
78   double denom = dot00 * dot11 - dot01 * dot01;
79
80   double v = (dot11 * dot20 - dot01 * dot21) / denom;
81   double w = (dot00 * dot21 - dot01 * dot20) / denom;
82   double u = 1 - v - w;
83
84   // Use the barycentric coordinates to test if |point| is inside the
85   // triangle (r1, r2, r2).
86   return (v >= 0) && (w >= 0) && (u >= 0);
87 }
88
89 bool QuadF::Contains(const PointF& point) const {
90   return PointIsInTriangle(point, p1_, p2_, p3_)
91       || PointIsInTriangle(point, p1_, p3_, p4_);
92 }
93
94 void QuadF::Scale(float x_scale, float y_scale) {
95   p1_.Scale(x_scale, y_scale);
96   p2_.Scale(x_scale, y_scale);
97   p3_.Scale(x_scale, y_scale);
98   p4_.Scale(x_scale, y_scale);
99 }
100
101 void QuadF::operator+=(const Vector2dF& rhs) {
102   p1_ += rhs;
103   p2_ += rhs;
104   p3_ += rhs;
105   p4_ += rhs;
106 }
107
108 void QuadF::operator-=(const Vector2dF& rhs) {
109   p1_ -= rhs;
110   p2_ -= rhs;
111   p3_ -= rhs;
112   p4_ -= rhs;
113 }
114
115 QuadF operator+(const QuadF& lhs, const Vector2dF& rhs) {
116   QuadF result = lhs;
117   result += rhs;
118   return result;
119 }
120
121 QuadF operator-(const QuadF& lhs, const Vector2dF& rhs) {
122   QuadF result = lhs;
123   result -= rhs;
124   return result;
125 }
126
127 }  // namespace gfx