1 // Copyright 2018 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 // Note that the "real" testing of these methods is in s2loop_measures_test
19 // and s2polyline_measures_test. This file only checks the handling of
20 // multiple shapes and shapes of different dimensions.
21
22 #include "s2/s2shape_index_measures.h"
23
24 #include <gtest/gtest.h>
25 #include "s2/third_party/absl/memory/memory.h"
26 #include "s2/mutable_s2shape_index.h"
27 #include "s2/s2lax_polygon_shape.h"
28 #include "s2/s2lax_polyline_shape.h"
29 #include "s2/s2point_vector_shape.h"
30 #include "s2/s2pointutil.h"
31 #include "s2/s2text_format.h"
32
33 using absl::make_unique;
34 using s2textformat::MakeIndexOrDie;
35
36 namespace {
37
TEST(GetDimension,Empty)38 TEST(GetDimension, Empty) {
39 EXPECT_EQ(-1, S2::GetDimension(*MakeIndexOrDie("# #")));
40 }
41
TEST(GetDimension,Points)42 TEST(GetDimension, Points) {
43 EXPECT_EQ(0, S2::GetDimension(*MakeIndexOrDie("0:0 # #")));
44
45 // Create an index with an empty point set.
46 MutableS2ShapeIndex index;
47 index.Add(make_unique<S2PointVectorShape>());
48 EXPECT_EQ(0, S2::GetDimension(index));
49 }
50
TEST(GetDimension,PointsAndLines)51 TEST(GetDimension, PointsAndLines) {
52 EXPECT_EQ(1, S2::GetDimension(*MakeIndexOrDie("0:0 # 1:1, 1:2 #")));
53
54 // Note that a polyline with one vertex has no edges, so it is effectively
55 // empty for the purpose of testing GetDimension().
56 EXPECT_EQ(1, S2::GetDimension(*MakeIndexOrDie("0:0 # 1:1 #")));
57 }
58
TEST(GetDimension,PointsLinesAndPolygons)59 TEST(GetDimension, PointsLinesAndPolygons) {
60 EXPECT_EQ(2, S2::GetDimension(*MakeIndexOrDie(
61 "0:0 # 1:1, 2:2 # 3:3, 3:4, 4:3")));
62
63 EXPECT_EQ(2, S2::GetDimension(*MakeIndexOrDie("# # empty")));
64 }
65
TEST(GetNumPoints,Empty)66 TEST(GetNumPoints, Empty) {
67 EXPECT_EQ(0, S2::GetNumPoints(*MakeIndexOrDie("# #")));
68 }
69
TEST(GetNumPoints,TwoPoints)70 TEST(GetNumPoints, TwoPoints) {
71 EXPECT_EQ(2, S2::GetNumPoints(*MakeIndexOrDie("0:0 | 1:0 # #")));
72 }
73
TEST(GetNumPoints,LineAndPolygon)74 TEST(GetNumPoints, LineAndPolygon) {
75 EXPECT_EQ(0, S2::GetNumPoints(*MakeIndexOrDie(
76 "# 1:1, 1:2 # 0:3, 0:5, 2:5")));
77 }
78
TEST(GetLength,Empty)79 TEST(GetLength, Empty) {
80 EXPECT_EQ(S1Angle::Zero(), S2::GetLength(*MakeIndexOrDie("# #")));
81 }
82
TEST(GetLength,TwoLines)83 TEST(GetLength, TwoLines) {
84 EXPECT_EQ(S1Angle::Degrees(2), S2::GetLength(*MakeIndexOrDie(
85 "4:4 # 0:0, 1:0 | 1:0, 2:0 # 5:5, 5:6, 6:5")));
86 }
87
TEST(GetPerimeter,Empty)88 TEST(GetPerimeter, Empty) {
89 EXPECT_EQ(S1Angle::Zero(), S2::GetPerimeter(*MakeIndexOrDie("# #")));
90 }
91
TEST(GetPerimeter,DegeneratePolygon)92 TEST(GetPerimeter, DegeneratePolygon) {
93 EXPECT_DOUBLE_EQ(4.0, S2::GetPerimeter(*MakeIndexOrDie(
94 "4:4 # 0:0, 1:0 | 2:0, 3:0 # 0:1, 0:2, 0:3")).degrees());
95 }
96
TEST(GetArea,Empty)97 TEST(GetArea, Empty) {
98 EXPECT_EQ(0.0, S2::GetArea(*MakeIndexOrDie("# #")));
99 }
100
TEST(GetArea,TwoFullPolygons)101 TEST(GetArea, TwoFullPolygons) {
102 EXPECT_EQ(8 * M_PI, S2::GetArea(*MakeIndexOrDie("# # full | full")));
103 }
104
TEST(GetApproxArea,Empty)105 TEST(GetApproxArea, Empty) {
106 EXPECT_EQ(0.0, S2::GetApproxArea(*MakeIndexOrDie("# #")));
107 }
108
TEST(GetApproxArea,TwoFullPolygons)109 TEST(GetApproxArea, TwoFullPolygons) {
110 EXPECT_EQ(8 * M_PI, S2::GetApproxArea(*MakeIndexOrDie("# # full | full")));
111 }
112
TEST(GetCentroid,Empty)113 TEST(GetCentroid, Empty) {
114 EXPECT_EQ(S2Point(0, 0, 0), S2::GetCentroid(*MakeIndexOrDie("# #")));
115 }
116
TEST(GetCentroid,Points)117 TEST(GetCentroid, Points) {
118 EXPECT_EQ(S2Point(1, 1, 0),
119 S2::GetCentroid(*MakeIndexOrDie("0:0 | 0:90 # #")));
120 }
121
TEST(GetCentroid,Polyline)122 TEST(GetCentroid, Polyline) {
123 // Checks that points are ignored when computing the centroid.
124 EXPECT_TRUE(S2::ApproxEquals(
125 S2Point(1, 1, 0),
126 S2::GetCentroid(*MakeIndexOrDie("5:5 | 6:6 # 0:0, 0:90 #"))));
127 }
128
TEST(GetCentroid,Polygon)129 TEST(GetCentroid, Polygon) {
130 // Checks that points and polylines are ignored when computing the centroid.
131 EXPECT_TRUE(S2::ApproxEquals(
132 S2Point(M_PI_4, M_PI_4, M_PI_4),
133 S2::GetCentroid(*MakeIndexOrDie("5:5 # 6:6, 7:7 # 0:0, 0:90, 90:0"))));
134 }
135
136 } // namespace
137