Chromium Code Reviews| 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..00d865b570e7fd18f33687d900329e453d1b0834 |
| --- /dev/null |
| +++ b/ash/common/system/chromeos/palette/tools/laser_pointer_points.cc |
| @@ -0,0 +1,151 @@ |
| +// 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 <limits.h> |
| + |
| +#include "ui/gfx/geometry/cubic_bezier.h" |
| + |
| +namespace ash { |
| +namespace { |
| + |
| +float DistanceBetweenMousePoints(const LaserPointerPoints::LaserPoint& point1, |
|
jdufault
2016/08/16 19:33:56
Make this take gfx::Point instances
sammiequon
2016/08/16 23:18:54
Removed.
|
| + const LaserPointerPoints::LaserPoint& point2) { |
| + return (point1.location - point2.location).Length(); |
| +} |
| + |
| +} // namespace |
| + |
| +LaserPointerPoints::LaserPointerPoints(base::TimeDelta life_duration) { |
| + life_duration_ = life_duration; |
|
jdufault
2016/08/16 19:33:56
Initialize in ctor list, ie, : life_duration_(life
sammiequon
2016/08/16 23:18:54
Done.
|
| +} |
| + |
| +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.creation_time = pointTime; |
| + |
| + if (points_.empty()) |
| + bounds_.SetRect(point.x(), point.y(), 0, 0); |
| + else { |
| + std::vector<LaserPoint> generatedPoints; |
| + // GenerateCurvePoints(newPoint, generatedPoints); |
|
jdufault
2016/08/16 19:33:56
Please remove GenerateCurvePoints as well as this
sammiequon
2016/08/16 23:18:54
Done.
|
| + 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) { |
| + // This will do nothing if the point is within the current bounds. |
| + gfx::Point minPoint(bounds_.origin()); |
| + gfx::Point maxPoint(bounds_.bottom_right()); |
| + minPoint = gfx::Point(std::min(minPoint.x(), point.x()), |
|
jdufault
2016/08/16 19:33:56
min_point
sammiequon
2016/08/16 23:18:54
Done.
|
| + std::min(minPoint.y(), point.y())); |
| + maxPoint = gfx::Point(std::max(maxPoint.x(), point.x()), |
|
jdufault
2016/08/16 19:33:56
max_point
sammiequon
2016/08/16 23:18:54
Done.
|
| + std::max(maxPoint.y(), point.y())); |
| + bounds_ = gfx::BoundingRect(minPoint, maxPoint); |
| +} |
| + |
| +void LaserPointerPoints::ClearOldPoints() { |
| + if (!points_.empty()) { |
| + LaserPoint newest = points_.back(); |
| + gfx::Point minPoint = bounds_.origin(); |
|
jdufault
2016/08/16 19:33:56
min_point
sammiequon
2016/08/16 23:18:54
Done.
|
| + gfx::Point maxPoint = bounds_.bottom_right(); |
|
jdufault
2016/08/16 19:33:56
max_point
sammiequon
2016/08/16 23:18:54
Done.
|
| + for (auto it = points_.begin(); it != points_.end(); it++) { |
| + if (newest.creation_time - it->creation_time > life_duration_) { |
| + minPoint.set_x(std::min(minPoint.x(), it->location.x())); |
|
jdufault
2016/08/16 19:33:56
Should this be std::max?
sammiequon
2016/08/16 23:18:54
Removed.
|
| + minPoint.set_y(std::min(minPoint.y(), it->location.y())); |
| + maxPoint.set_x(std::max(maxPoint.x(), it->location.x())); |
|
jdufault
2016/08/16 19:33:55
Should this be std::min?
sammiequon
2016/08/16 23:18:54
Removed.
|
| + maxPoint.set_y(std::max(maxPoint.y(), it->location.y())); |
| + points_.erase(it); |
|
jdufault
2016/08/16 19:33:56
erase invalidates the it iterator, which means tha
sammiequon
2016/08/16 23:18:54
Would this way work? I chose this way so we can st
|
| + } else { |
| + // Since the points are sorted by time we can end the loop early once |
| + // the condition fails. |
| + break; |
| + } |
| + } |
| + bounds_ = gfx::BoundingRect(minPoint, maxPoint); |
|
jdufault
2016/08/16 19:33:56
How large is the points_ array? If it's not massiv
sammiequon
2016/08/16 23:18:54
Around 40. Re-added Recalculate bounds.
|
| + } |
| +} |
| + |
| +void LaserPointerPoints::GenerateCurvePoints( |
| + const LaserPoint& new_point, |
| + std::vector<LaserPoint>& generated_points) { |
| + if (points_.empty()) |
| + return; |
| + |
| + LaserPoint last_point = points_.back(); |
| + gfx::CubicBezier bezier = gfx::CubicBezier(0.25, 0.0, 0.75, 1.0); |
| + double distance = double{DistanceBetweenMousePoints(new_point, last_point)}; |
| + |
| + // Generate some equidistant points using the bezier. |
| + for (float j = 1; j <= distance - 1; j += 3) { |
| + double normalized = 1 - ((distance - j) / distance); |
| + base::Time generated_time = |
| + last_point.creation_time + |
| + base::TimeDelta::FromMillisecondsD( |
| + normalized * |
| + (new_point.creation_time - last_point.creation_time) |
| + .InMillisecondsF()); |
| + LaserPoint generated_point; |
| + float current_x = |
| + last_point.location.x() + |
| + normalized * (new_point.location.x() - last_point.location.x()); |
| + float current_y = last_point.location.y() + |
| + float{bezier.Solve(normalized)} * |
| + (new_point.location.y() - last_point.location.y()); |
| + generated_point.creation_time = generated_time; |
| + generated_point.location = gfx::Point(current_x, current_y); |
| + generated_points.push_back(generated_point); |
| + } |
| +} |
| +} // namespace ash |