OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "media/audio/point.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_split.h" |
| 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/stringprintf.h" |
| 12 |
| 13 namespace media { |
| 14 |
| 15 Point::Point() : x_(0), y_(0), z_(0){}; |
| 16 Point::Point(float x, float y, float z) : x_(x), y_(y), z_(z) {} |
| 17 |
| 18 bool Point::IsValid() const { |
| 19 return std::isfinite(x_) && std::isfinite(y_) && std::isfinite(z_); |
| 20 } |
| 21 |
| 22 std::string Point::ToString() const { |
| 23 return base::StringPrintf("x=%.3f, y=%.3f, z=%.3f", x_, y_, z_); |
| 24 } |
| 25 |
| 26 std::vector<Point> ParsePointsFromString(const std::string& points_string) { |
| 27 std::vector<Point> points; |
| 28 if (points_string.empty()) |
| 29 return points; |
| 30 |
| 31 const auto& tokens = |
| 32 base::SplitString(points_string, base::kWhitespaceASCII, |
| 33 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 34 if (tokens.size() < 3 || tokens.size() % 3 != 0) { |
| 35 LOG(ERROR) << "Malformed points string: " << points_string; |
| 36 return points; |
| 37 } |
| 38 |
| 39 std::vector<float> float_tokens; |
| 40 float_tokens.reserve(tokens.size()); |
| 41 for (const auto& token : tokens) { |
| 42 double float_token; |
| 43 if (!base::StringToDouble(token, &float_token)) { |
| 44 LOG(ERROR) << "Unable to convert token=" << token |
| 45 << " to double from points string: " << points_string; |
| 46 return points; |
| 47 } |
| 48 float_tokens.push_back(float_token); |
| 49 } |
| 50 |
| 51 points.reserve(float_tokens.size() / 3); |
| 52 for (size_t i = 0; i < float_tokens.size(); i += 3) { |
| 53 points.push_back( |
| 54 Point(float_tokens[i + 0], float_tokens[i + 1], float_tokens[i + 2])); |
| 55 } |
| 56 |
| 57 return points; |
| 58 } |
| 59 |
| 60 } // namespace media |
OLD | NEW |