1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "core/geometry.h"
18
19#include <cmath>
20
21#include "base/logging.h"
22
23namespace android {
24namespace filterfw {
25
26float Point::Length() const {
27  return std::sqrt(x_ * x_ + y_ * y_);
28}
29
30bool Point::ScaleTo(float new_length) {
31  float length = Length();
32  if (length == 0.0f) {
33    return false;
34  }
35  x_ *= new_length / length;
36  y_ *= new_length / length;
37  return true;
38}
39
40float Point::Distance(const Point& p0, const Point& p1) {
41  Point diff = p1 - p0;
42  return diff.Length();
43}
44
45Point Point::operator+(const Point& other) const {
46  Point out;
47  out.x_ = x_ + other.x_;
48  out.y_ = y_ + other.y_;
49  return out;
50}
51
52Point Point::operator-(const Point& other) const {
53  Point out;
54  out.x_ = x_ - other.x_;
55  out.y_ = y_ - other.y_;
56  return out;
57}
58
59Point Point::operator*(float factor) const {
60  Point out;
61  out.x_ = factor * x_;
62  out.y_ = factor * y_;
63  return out;
64}
65
66void Point::Rotate90Clockwise() {
67  const float x = x_;
68  x_ = y_;
69  y_ = -x;
70}
71
72bool Rect::ExpandToAspectRatio(float ratio) {
73  if (width <= 0.0f || height <= 0.0f || ratio <= 0.0f) {
74    return false;
75  }
76
77  const float current_ratio = width / height;
78  if (current_ratio < ratio) {
79    const float dx = width * (ratio / current_ratio - 1.0f);
80    x -= dx / 2.0f;
81    width += dx;
82  } else {
83    const float dy = height * (current_ratio / ratio - 1.0f);
84    y -= dy / 2.0f;
85    height += dy;
86  }
87  return true;
88}
89
90bool Rect::ExpandToMinLength(float length) {
91  if (width <= 0.0f || height <= 0.0f || length <= 0.0f) {
92    return false;
93  }
94
95  const float current_length = width > height ? width : height;
96  if (length > current_length) {
97    const float dx = width * (length / current_length - 1.0f);
98    x -= dx / 2.0f;
99    width += dx;
100    const float dy = height * (length / current_length - 1.0f);
101    y -= dy / 2.0f;
102    height += dy;
103  }
104  return true;
105}
106
107bool Rect::ScaleWithLengthLimit(float factor, float max_length) {
108  if (width <= 0.0f || height <= 0.0f || factor <= 0.0f) {
109    return false;
110  }
111
112  const float current_length = width > height ? width : height;
113  if (current_length >= max_length) {
114    return true;
115  }
116
117  float f = factor;
118  if (current_length * f > max_length) {
119    f *= max_length / (current_length * f);
120  }
121
122  const float dx = width * (f - 1.0f);
123  x -= dx / 2.0f;
124  width += dx;
125  const float dy = height * (f - 1.0f);
126  y -= dy / 2.0f;
127  height += dy;
128  return true;
129}
130
131const Point& Quad::point(int ix) const {
132  ALOG_ASSERT(ix < static_cast<int>(points_.size()), "Access out of bounds");
133  return points_[ix];
134}
135
136} // namespace filterfw
137} // namespace android
138