1 // Copyright 2012 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 
16 // Author: ericv@google.com (Eric Veach)
17 
18 #include "s2/r2rect.h"
19 
20 #include <iosfwd>
21 
22 #include "base/logging.h"
23 #include "s2/r1interval.h"
24 #include "s2/r2.h"
25 
FromCenterSize(R2Point const & center,R2Point const & size)26 R2Rect R2Rect::FromCenterSize(R2Point const& center, R2Point const& size) {
27   return R2Rect(
28       R1Interval(center.x() - 0.5 * size.x(), center.x() + 0.5 * size.x()),
29       R1Interval(center.y() - 0.5 * size.y(), center.y() + 0.5 * size.y()));
30 }
31 
FromPointPair(R2Point const & p1,R2Point const & p2)32 R2Rect R2Rect::FromPointPair(R2Point const& p1, R2Point const& p2) {
33   return R2Rect(R1Interval::FromPointPair(p1.x(), p2.x()),
34                 R1Interval::FromPointPair(p1.y(), p2.y()));
35 }
36 
Contains(R2Rect const & other) const37 bool R2Rect::Contains(R2Rect const& other) const {
38   return x().Contains(other.x()) && y().Contains(other.y());
39 }
40 
InteriorContains(R2Rect const & other) const41 bool R2Rect::InteriorContains(R2Rect const& other) const {
42   return x().InteriorContains(other.x()) && y().InteriorContains(other.y());
43 }
44 
Intersects(R2Rect const & other) const45 bool R2Rect::Intersects(R2Rect const& other) const {
46   return x().Intersects(other.x()) && y().Intersects(other.y());
47 }
48 
InteriorIntersects(R2Rect const & other) const49 bool R2Rect::InteriorIntersects(R2Rect const& other) const {
50   return x().InteriorIntersects(other.x()) && y().InteriorIntersects(other.y());
51 }
52 
AddPoint(R2Point const & p)53 void R2Rect::AddPoint(R2Point const& p) {
54   bounds_[0].AddPoint(p[0]);
55   bounds_[1].AddPoint(p[1]);
56 }
57 
AddRect(R2Rect const & other)58 void R2Rect::AddRect(R2Rect const& other) {
59   bounds_[0].AddInterval(other[0]);
60   bounds_[1].AddInterval(other[1]);
61 }
62 
Project(R2Point const & p) const63 R2Point R2Rect::Project(R2Point const& p) const {
64   return R2Point(x().Project(p.x()), y().Project(p.y()));
65 }
66 
Expanded(R2Point const & margin) const67 R2Rect R2Rect::Expanded(R2Point const& margin) const {
68   R1Interval xx = x().Expanded(margin.x());
69   R1Interval yy = y().Expanded(margin.y());
70   if (xx.is_empty() || yy.is_empty())
71     return Empty();
72   return R2Rect(xx, yy);
73 }
74 
Union(R2Rect const & other) const75 R2Rect R2Rect::Union(R2Rect const& other) const {
76   return R2Rect(x().Union(other.x()), y().Union(other.y()));
77 }
78 
Intersection(R2Rect const & other) const79 R2Rect R2Rect::Intersection(R2Rect const& other) const {
80   R1Interval xx = x().Intersection(other.x());
81   R1Interval yy = y().Intersection(other.y());
82   if (xx.is_empty() || yy.is_empty())
83     return Empty();
84   return R2Rect(xx, yy);
85 }
86 
ApproxEquals(R2Rect const & other,double max_error) const87 bool R2Rect::ApproxEquals(R2Rect const& other, double max_error) const {
88   return (x().ApproxEquals(other.x(), max_error) &&
89           y().ApproxEquals(other.y(), max_error));
90 }
91 
operator <<(std::ostream & os,R2Rect const & r)92 std::ostream& operator<<(std::ostream& os, R2Rect const& r) {
93   return os << "[Lo" << r.lo() << ", Hi" << r.hi() << "]";
94 }
95