Index: ash/common/system/chromeos/palette/tools/laser_pointer_points.cc |
diff --git a/ash/common/system/chromeos/palette/tools/laser_pointer_points.cc b/ash/common/system/chromeos/palette/tools/laser_pointer_points.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..01b3368d48201afff1176af874f70bfd803296dc |
--- /dev/null |
+++ b/ash/common/system/chromeos/palette/tools/laser_pointer_points.cc |
@@ -0,0 +1,182 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ash/common/system/chromeos/palette/tools/laser_pointer_points.h" |
+ |
+#include "ui/gfx/geometry/cubic_bezier.h" |
+ |
+namespace ash { |
+ |
+LaserPointerPoints::LaserPointerPoints(base::TimeDelta life_duration) { |
+ life_duration_ = life_duration; |
+} |
+ |
+LaserPointerPoints::LaserPointerPoints(const LaserPointerPoints& old) { |
+ bounds_ = old.bounds_; |
+ points_ = old.points_; |
+ life_duration_ = old.life_duration_; |
+} |
+ |
+LaserPointerPoints::~LaserPointerPoints() {} |
+ |
+void LaserPointerPoints::AddPoint(const gfx::Point& point) { |
+ // Creates and adds the new point. Updates the collection and bounding box |
+ // accordingly. |
+ base::Time pointTime = base::Time::Now(); |
+ LaserPoint newPoint; |
+ newPoint.location = point; |
+ newPoint.creationTime = pointTime; |
+ |
+ if (points_.empty()) |
+ bounds_.SetRect(point.x(), point.y(), 0, 0); |
+ else { |
+ std::vector<LaserPoint> generatedPoints; |
+ // GenerateCurvePoints(newPoint, generatedPoints); |
+ for (size_t j = 0; j < generatedPoints.size(); j++) { |
+ points_.push_back(generatedPoints[j]); |
+ } |
+ CalculateBounds(newPoint.location); |
+ } |
+ points_.push_back(newPoint); |
+ ClearOldPoints(); |
+} |
+ |
+void LaserPointerPoints::Clear() { |
+ points_.clear(); |
+} |
+ |
+gfx::Rect LaserPointerPoints::GetBoundingBox() const { |
+ return bounds_; |
+} |
+ |
+LaserPointerPoints::LaserPoint LaserPointerPoints::GetOldest() const { |
+ if (!points_.empty()) |
+ return points_.front(); |
+ return LaserPoint(); |
+} |
+ |
+LaserPointerPoints::LaserPoint LaserPointerPoints::GetMostRecent() const { |
+ if (!points_.empty()) |
+ return points_.back(); |
+ return LaserPoint(); |
+} |
+ |
+bool LaserPointerPoints::IsEmpty() const { |
+ return points_.empty(); |
+} |
+ |
+int LaserPointerPoints::GetNumberOfPoints() const { |
+ return points_.size(); |
+} |
+ |
+std::deque<LaserPointerPoints::LaserPoint>::const_iterator |
+LaserPointerPoints::PointsStart() const { |
+ return points_.cbegin(); |
+} |
+ |
+std::deque<LaserPointerPoints::LaserPoint>::const_iterator |
+LaserPointerPoints::PointsEnd() const { |
+ return points_.cend(); |
+} |
+ |
+void LaserPointerPoints::CalculateBounds(const gfx::Point& point) { |
+ 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
|
+ oldBounds.SetRect(bounds_.x(), bounds_.y(), bounds_.width(), |
+ bounds_.height()); |
+ // This will do nothing if the point is within the current bounds. |
+ 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.
|
+ bool yChanged = false; |
+ if (point.x() <= oldBounds.x()) { |
+ bounds_.set_x(point.x()); |
+ xChanged = true; |
+ } |
+ if (point.y() <= oldBounds.y()) { |
+ bounds_.set_y(point.y()); |
+ yChanged = true; |
+ } |
+ if (point.x() >= oldBounds.right()) { |
+ bounds_.set_width(std::abs(point.x() - bounds_.x())); |
+ } else if (xChanged) |
+ bounds_.set_width(std::abs(oldBounds.right() - bounds_.x())); |
+ if (point.y() >= oldBounds.bottom()) { |
+ bounds_.set_height(std::abs(point.y() - bounds_.y())); |
+ } else if (yChanged) |
+ bounds_.set_height(std::abs(oldBounds.bottom() - bounds_.y())); |
+} |
+ |
+void LaserPointerPoints::ClearOldPoints() { |
+ if (!points_.empty()) { |
+ bool needRecalculateBounds = false; |
+ LaserPoint newest = points_.back(); |
+ 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
|
+ if (newest.creationTime - it->creationTime > life_duration_) { |
+ if (!needRecalculateBounds || it->location.x() <= bounds_.x() || |
+ it->location.x() >= bounds_.right() || |
+ it->location.y() <= bounds_.y() || |
+ it->location.y() >= bounds_.bottom()) { |
+ // If the point was on of the outermost points we have to recalculate |
+ // the bounds. |
+ needRecalculateBounds = true; |
+ } |
+ points_.erase(it); |
+ } else { |
+ // Since the points are sorted by time we can end the loop early once |
+ // the condition fails. |
+ break; |
+ } |
+ } |
+ if (needRecalculateBounds) |
+ RecalculateBounds(); |
+ } |
+} |
+ |
+float LaserPointerPoints::DistanceMousePoints(const LaserPoint& point1, |
+ const LaserPoint& point2) { |
+ return sqrt((point2.location.y() - point1.location.y()) * |
+ (point2.location.y() - point1.location.y()) + |
+ (point2.location.x() - point1.location.x()) * |
+ (point2.location.x() - point1.location.x())); |
+} |
+ |
+void LaserPointerPoints::GenerateCurvePoints( |
+ const LaserPoint& newPoint, |
jdufault
2016/08/12 19:57:58
new_point
sammiequon
2016/08/16 17:00:05
Done.
|
+ std::vector<LaserPoint>& generatedPoints) { |
jdufault
2016/08/12 19:57:58
generated_points
sammiequon
2016/08/16 17:00:05
Done.
|
+ if (points_.empty()) |
+ return; |
+ |
+ LaserPoint lastPoint = points_.back(); |
jdufault
2016/08/12 19:57:59
laser_point
sammiequon
2016/08/16 17:00:05
Done.
|
+ gfx::CubicBezier bezier = gfx::CubicBezier(0.25, 0.0, 0.75, 1.0); |
+ float distance = DistanceMousePoints(newPoint, lastPoint); |
+ |
+ // Generate some equidistant points using the bezier. |
+ for (float j = 1; j <= distance - 1; j += 3) { |
+ float normalized = 1 - ((distance - j) / distance); |
+ base::Time generatedTime = |
jdufault
2016/08/12 19:57:58
generated_time
sammiequon
2016/08/16 17:00:04
Done.
|
+ lastPoint.creationTime + |
+ base::TimeDelta::FromMillisecondsD(double{ |
+ 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.
|
+ .InMillisecondsF()}}); |
+ LaserPoint generatedPoint; |
jdufault
2016/08/12 19:57:58
generated_point
sammiequon
2016/08/16 17:00:04
Done.
|
+ float currentX = |
+ lastPoint.location.x() + |
+ normalized * (newPoint.location.x() - lastPoint.location.x()); |
+ float currentY = lastPoint.location.y() + |
+ float{bezier.Solve(normalized)} * |
+ (newPoint.location.y() - lastPoint.location.y()); |
+ generatedPoint.creationTime = generatedTime; |
+ generatedPoint.location = gfx::Point(currentX, currentY); |
+ generatedPoints.push_back(generatedPoint); |
+ } |
+} |
+ |
+void LaserPointerPoints::RecalculateBounds() { |
+ if (points_.empty()) |
+ bounds_.SetRect(0, 0, 0, 0); |
+ auto it = points_.begin(); |
+ bounds_.SetRect(it->location.x(), it->location.y(), 0, 0); |
+ for (; it != points_.end(); it++) { |
+ 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.
|
+ } |
+} |
+} // namespace ash |