Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "ash/common/system/chromeos/palette/tools/laser_pointer_points.h" | |
| 6 | |
| 7 #include "ui/gfx/geometry/cubic_bezier.h" | |
| 8 | |
| 9 namespace ash { | |
| 10 | |
| 11 LaserPointerPoints::LaserPointerPoints(base::TimeDelta life_duration) { | |
| 12 life_duration_ = life_duration; | |
| 13 } | |
| 14 | |
| 15 LaserPointerPoints::LaserPointerPoints(const LaserPointerPoints& old) { | |
| 16 bounds_ = old.bounds_; | |
| 17 points_ = old.points_; | |
| 18 life_duration_ = old.life_duration_; | |
| 19 } | |
| 20 | |
| 21 LaserPointerPoints::~LaserPointerPoints() {} | |
| 22 | |
| 23 void LaserPointerPoints::AddPoint(const gfx::Point& point) { | |
| 24 // Creates and adds the new point. Updates the collection and bounding box | |
| 25 // accordingly. | |
| 26 base::Time pointTime = base::Time::Now(); | |
| 27 LaserPoint newPoint; | |
| 28 newPoint.location = point; | |
| 29 newPoint.creationTime = pointTime; | |
| 30 | |
| 31 if (points_.empty()) | |
| 32 bounds_.SetRect(point.x(), point.y(), 0, 0); | |
| 33 else { | |
| 34 std::vector<LaserPoint> generatedPoints; | |
| 35 // GenerateCurvePoints(newPoint, generatedPoints); | |
| 36 for (size_t j = 0; j < generatedPoints.size(); j++) { | |
| 37 points_.push_back(generatedPoints[j]); | |
| 38 } | |
| 39 CalculateBounds(newPoint.location); | |
| 40 } | |
| 41 points_.push_back(newPoint); | |
| 42 ClearOldPoints(); | |
| 43 } | |
| 44 | |
| 45 void LaserPointerPoints::Clear() { | |
| 46 points_.clear(); | |
| 47 } | |
| 48 | |
| 49 gfx::Rect LaserPointerPoints::GetBoundingBox() const { | |
| 50 return bounds_; | |
| 51 } | |
| 52 | |
| 53 LaserPointerPoints::LaserPoint LaserPointerPoints::GetOldest() const { | |
| 54 if (!points_.empty()) | |
| 55 return points_.front(); | |
| 56 return LaserPoint(); | |
| 57 } | |
| 58 | |
| 59 LaserPointerPoints::LaserPoint LaserPointerPoints::GetMostRecent() const { | |
| 60 if (!points_.empty()) | |
| 61 return points_.back(); | |
| 62 return LaserPoint(); | |
| 63 } | |
| 64 | |
| 65 bool LaserPointerPoints::IsEmpty() const { | |
| 66 return points_.empty(); | |
| 67 } | |
| 68 | |
| 69 int LaserPointerPoints::GetNumberOfPoints() const { | |
| 70 return points_.size(); | |
| 71 } | |
| 72 | |
| 73 std::deque<LaserPointerPoints::LaserPoint>::const_iterator | |
| 74 LaserPointerPoints::PointsStart() const { | |
| 75 return points_.cbegin(); | |
| 76 } | |
| 77 | |
| 78 std::deque<LaserPointerPoints::LaserPoint>::const_iterator | |
| 79 LaserPointerPoints::PointsEnd() const { | |
| 80 return points_.cend(); | |
| 81 } | |
| 82 | |
| 83 void LaserPointerPoints::CalculateBounds(const gfx::Point& point) { | |
| 84 gfx::Rect oldBounds; | |
|
jdufault
2016/08/12 19:57:58
This just expands the rect so it contains the poin
sammiequon
2016/08/16 17:00:05
I removed Recalculate bounds instead of CaculateBo
| |
| 85 oldBounds.SetRect(bounds_.x(), bounds_.y(), bounds_.width(), | |
| 86 bounds_.height()); | |
| 87 // This will do nothing if the point is within the current bounds. | |
| 88 bool xChanged = false; | |
|
jdufault
2016/08/12 19:57:58
Can we find the min/max points and use gfx::Boundi
sammiequon
2016/08/16 17:00:05
Done.
| |
| 89 bool yChanged = false; | |
| 90 if (point.x() <= oldBounds.x()) { | |
| 91 bounds_.set_x(point.x()); | |
| 92 xChanged = true; | |
| 93 } | |
| 94 if (point.y() <= oldBounds.y()) { | |
| 95 bounds_.set_y(point.y()); | |
| 96 yChanged = true; | |
| 97 } | |
| 98 if (point.x() >= oldBounds.right()) { | |
| 99 bounds_.set_width(std::abs(point.x() - bounds_.x())); | |
| 100 } else if (xChanged) | |
| 101 bounds_.set_width(std::abs(oldBounds.right() - bounds_.x())); | |
| 102 if (point.y() >= oldBounds.bottom()) { | |
| 103 bounds_.set_height(std::abs(point.y() - bounds_.y())); | |
| 104 } else if (yChanged) | |
| 105 bounds_.set_height(std::abs(oldBounds.bottom() - bounds_.y())); | |
| 106 } | |
| 107 | |
| 108 void LaserPointerPoints::ClearOldPoints() { | |
| 109 if (!points_.empty()) { | |
| 110 bool needRecalculateBounds = false; | |
| 111 LaserPoint newest = points_.back(); | |
| 112 for (auto it = points_.begin(); it != points_.end(); it++) { | |
|
jdufault
2016/08/12 19:57:58
It looks like this is effectively computing the bo
sammiequon
2016/08/16 17:00:05
I removed Recalculate bounds instead of CaculateBo
| |
| 113 if (newest.creationTime - it->creationTime > life_duration_) { | |
| 114 if (!needRecalculateBounds || it->location.x() <= bounds_.x() || | |
| 115 it->location.x() >= bounds_.right() || | |
| 116 it->location.y() <= bounds_.y() || | |
| 117 it->location.y() >= bounds_.bottom()) { | |
| 118 // If the point was on of the outermost points we have to recalculate | |
| 119 // the bounds. | |
| 120 needRecalculateBounds = true; | |
| 121 } | |
| 122 points_.erase(it); | |
| 123 } else { | |
| 124 // Since the points are sorted by time we can end the loop early once | |
| 125 // the condition fails. | |
| 126 break; | |
| 127 } | |
| 128 } | |
| 129 if (needRecalculateBounds) | |
| 130 RecalculateBounds(); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 float LaserPointerPoints::DistanceMousePoints(const LaserPoint& point1, | |
| 135 const LaserPoint& point2) { | |
| 136 return sqrt((point2.location.y() - point1.location.y()) * | |
| 137 (point2.location.y() - point1.location.y()) + | |
| 138 (point2.location.x() - point1.location.x()) * | |
| 139 (point2.location.x() - point1.location.x())); | |
| 140 } | |
| 141 | |
| 142 void LaserPointerPoints::GenerateCurvePoints( | |
| 143 const LaserPoint& newPoint, | |
|
jdufault
2016/08/12 19:57:58
new_point
sammiequon
2016/08/16 17:00:05
Done.
| |
| 144 std::vector<LaserPoint>& generatedPoints) { | |
|
jdufault
2016/08/12 19:57:58
generated_points
sammiequon
2016/08/16 17:00:05
Done.
| |
| 145 if (points_.empty()) | |
| 146 return; | |
| 147 | |
| 148 LaserPoint lastPoint = points_.back(); | |
|
jdufault
2016/08/12 19:57:59
laser_point
sammiequon
2016/08/16 17:00:05
Done.
| |
| 149 gfx::CubicBezier bezier = gfx::CubicBezier(0.25, 0.0, 0.75, 1.0); | |
| 150 float distance = DistanceMousePoints(newPoint, lastPoint); | |
| 151 | |
| 152 // Generate some equidistant points using the bezier. | |
| 153 for (float j = 1; j <= distance - 1; j += 3) { | |
| 154 float normalized = 1 - ((distance - j) / distance); | |
| 155 base::Time generatedTime = | |
|
jdufault
2016/08/12 19:57:58
generated_time
sammiequon
2016/08/16 17:00:04
Done.
| |
| 156 lastPoint.creationTime + | |
| 157 base::TimeDelta::FromMillisecondsD(double{ | |
| 158 normalized * float{(newPoint.creationTime - lastPoint.creationTime) | |
|
jdufault
2016/08/12 19:57:58
Store normalized as a double and all of the casts
sammiequon
2016/08/16 17:00:04
Done.
| |
| 159 .InMillisecondsF()}}); | |
| 160 LaserPoint generatedPoint; | |
|
jdufault
2016/08/12 19:57:58
generated_point
sammiequon
2016/08/16 17:00:04
Done.
| |
| 161 float currentX = | |
| 162 lastPoint.location.x() + | |
| 163 normalized * (newPoint.location.x() - lastPoint.location.x()); | |
| 164 float currentY = lastPoint.location.y() + | |
| 165 float{bezier.Solve(normalized)} * | |
| 166 (newPoint.location.y() - lastPoint.location.y()); | |
| 167 generatedPoint.creationTime = generatedTime; | |
| 168 generatedPoint.location = gfx::Point(currentX, currentY); | |
| 169 generatedPoints.push_back(generatedPoint); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 void LaserPointerPoints::RecalculateBounds() { | |
| 174 if (points_.empty()) | |
| 175 bounds_.SetRect(0, 0, 0, 0); | |
| 176 auto it = points_.begin(); | |
| 177 bounds_.SetRect(it->location.x(), it->location.y(), 0, 0); | |
| 178 for (; it != points_.end(); it++) { | |
| 179 CalculateBounds(it->location); | |
|
jdufault
2016/08/12 19:57:58
Find the min/max points and build bounding box fro
sammiequon
2016/08/16 17:00:04
Done.
| |
| 180 } | |
| 181 } | |
| 182 } // namespace ash | |
| OLD | NEW |