1 // Copyright 2018 Google LLC
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 // https://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 #ifndef THIRD_PARTY_QUIC_TRACE_TOOLS_GEOMETRY_UTIL_H_
16 #define THIRD_PARTY_QUIC_TRACE_TOOLS_GEOMETRY_UTIL_H_
17
18 #include <algorithm>
19 #include <cmath>
20 #include <ostream>
21
22 namespace quic_trace {
23 namespace render {
24
25 // A 2D-vector with pairwise operators defined. Named vec2 after the GLSL type
26 // of the same name and similar behavior.
27 struct alignas(8) vec2 {
28 float x;
29 float y;
30
vec2vec231 constexpr vec2() : x(0.f), y(0.f) {}
vec2vec232 constexpr vec2(float x, float y) : x(x), y(y) {}
33
34 vec2& operator+=(const vec2& other) {
35 x += other.x;
36 y += other.y;
37 return *this;
38 }
39 };
40
41 constexpr vec2 operator+(vec2 lhs, vec2 rhs) {
42 return vec2(lhs.x + rhs.x, lhs.y + rhs.y);
43 }
44 constexpr vec2 operator-(vec2 lhs, vec2 rhs) {
45 return vec2(lhs.x - rhs.x, lhs.y - rhs.y);
46 }
47 constexpr vec2 operator*(float lhs, vec2 rhs) {
48 return vec2(lhs * rhs.x, lhs * rhs.y);
49 }
50 constexpr vec2 operator*(vec2 lhs, vec2 rhs) {
51 return vec2(lhs.x * rhs.x, lhs.y * rhs.y);
52 }
53 constexpr vec2 operator/(vec2 lhs, vec2 rhs) {
54 return vec2(lhs.x / rhs.x, lhs.y / rhs.y);
55 }
56 constexpr vec2 operator/(vec2 lhs, float rhs) {
57 return vec2(lhs.x / rhs, lhs.y / rhs);
58 }
59 constexpr bool operator==(vec2 lhs, vec2 rhs) {
60 return lhs.x == rhs.x && lhs.y == rhs.y;
61 }
62 constexpr bool operator!=(vec2 lhs, vec2 rhs) {
63 return lhs.x != rhs.x || lhs.y != rhs.y;
64 }
65
PointwiseMin(vec2 a,vec2 b)66 inline vec2 PointwiseMin(vec2 a, vec2 b) {
67 return vec2(std::min(a.x, b.x), std::min(a.y, b.y));
68 }
PointwiseMax(vec2 a,vec2 b)69 inline vec2 PointwiseMax(vec2 a, vec2 b) {
70 return vec2(std::max(a.x, b.x), std::max(a.y, b.y));
71 }
72
73 inline std::ostream& operator<<(std::ostream& os, vec2 vector) {
74 os << "(" << vector.x << ", " << vector.y << ")";
75 return os;
76 }
77
DotProduct(vec2 a,vec2 b)78 inline float DotProduct(vec2 a, vec2 b) {
79 return a.x * b.x + a.y * b.y;
80 }
DistanceSquared(vec2 a,vec2 b)81 inline float DistanceSquared(vec2 a, vec2 b) {
82 const vec2 delta = a - b;
83 return DotProduct(delta, delta);
84 }
85
86 // A rectangle formed by vertices |origin| and |origin + size|.
87 struct Box {
88 vec2 origin;
89 vec2 size;
90 };
91
92 constexpr bool operator==(const Box& lhs, const Box& rhs) {
93 return lhs.origin == rhs.origin && lhs.size == rhs.size;
94 }
95 constexpr bool operator!=(const Box& lhs, const Box& rhs) {
96 return lhs.origin != rhs.origin || lhs.size != rhs.size;
97 }
98
BoundingBox(vec2 a,vec2 b)99 inline Box BoundingBox(vec2 a, vec2 b) {
100 return Box{PointwiseMin(a, b),
101 vec2(std::abs(a.x - b.x), std::abs(a.y - b.y))};
102 }
103
BoundingBox(const Box & a,const Box & b)104 inline Box BoundingBox(const Box& a, const Box& b) {
105 return BoundingBox(PointwiseMin(a.origin, b.origin),
106 PointwiseMax(a.origin + a.size, b.origin + b.size));
107 }
108
IntersectBoxes(const Box & a,const Box & b)109 inline Box IntersectBoxes(const Box& a, const Box& b) {
110 vec2 corner_low = PointwiseMax(a.origin, b.origin);
111 vec2 corner_high = PointwiseMin(a.origin + a.size, b.origin + b.size);
112 if (corner_low.x < corner_high.x && corner_low.y < corner_high.y) {
113 return BoundingBox(corner_low, corner_high);
114 } else {
115 return Box{};
116 }
117 }
118
IsInside(const Box & inner,const Box & outer)119 inline bool IsInside(const Box& inner, const Box& outer) {
120 const vec2 corner_inner = inner.origin + inner.size;
121 const vec2 corner_outer = outer.origin + outer.size;
122 return inner.origin.x >= outer.origin.x && inner.origin.y >= outer.origin.y &&
123 corner_inner.x <= corner_outer.x && corner_inner.y <= corner_outer.y;
124 }
125
IsInside(vec2 point,const Box & box)126 inline bool IsInside(vec2 point, const Box& box) {
127 return point.x >= box.origin.x && point.x <= box.origin.x + box.size.x &&
128 point.y >= box.origin.y && point.y <= box.origin.y + box.size.y;
129 }
130
BoxCenter(const Box & box)131 inline vec2 BoxCenter(const Box& box) {
132 return box.origin + box.size / 2;
133 }
134
135 } // namespace render
136 } // namespace quic_trace
137
138 #endif // THIRD_PARTY_QUIC_TRACE_TOOLS_GEOMETRY_UTIL_H_
139