Index: media/audio/point.cc |
diff --git a/media/audio/point.cc b/media/audio/point.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1b45dfcb447b698f50fbd7eb0d9ac83bfa75a28d |
--- /dev/null |
+++ b/media/audio/point.cc |
@@ -0,0 +1,60 @@ |
+// Copyright 2015 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 "media/audio/point.h" |
+ |
+#include "base/logging.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_split.h" |
+#include "base/strings/string_util.h" |
+#include "base/strings/stringprintf.h" |
+ |
+namespace media { |
+ |
+Point::Point() : x_(0), y_(0), z_(0){}; |
+Point::Point(float x, float y, float z) : x_(x), y_(y), z_(z) {} |
+ |
+bool Point::IsValid() const { |
+ return std::isfinite(x_) && std::isfinite(y_) && std::isfinite(z_); |
+} |
+ |
+std::string Point::ToString() const { |
+ return base::StringPrintf("x=%.3f, y=%.3f, z=%.3f", x_, y_, z_); |
+} |
+ |
+std::vector<Point> ParsePointsFromString(const std::string& points_string) { |
+ std::vector<Point> points; |
+ if (points_string.empty()) |
+ return points; |
+ |
+ const auto& tokens = |
+ base::SplitString(points_string, base::kWhitespaceASCII, |
+ base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
+ if (tokens.size() < 3 || tokens.size() % 3 != 0) { |
+ LOG(ERROR) << "Malformed points string: " << points_string; |
+ return points; |
+ } |
+ |
+ std::vector<float> float_tokens; |
+ float_tokens.reserve(tokens.size()); |
+ for (const auto& token : tokens) { |
+ double float_token; |
+ if (!base::StringToDouble(token, &float_token)) { |
+ LOG(ERROR) << "Unable to convert token=" << token |
+ << " to double from points string: " << points_string; |
+ return points; |
+ } |
+ float_tokens.push_back(float_token); |
+ } |
+ |
+ points.reserve(float_tokens.size() / 3); |
+ for (size_t i = 0; i < float_tokens.size(); i += 3) { |
+ points.push_back( |
+ Point(float_tokens[i + 0], float_tokens[i + 1], float_tokens[i + 2])); |
+ } |
+ |
+ return points; |
+} |
+ |
+} // namespace media |