Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 5bace44

Browse files
authored
[Impeller] Move SeparatedVector2 to impeller/geometry. (#53264)
Follow-up for #53210. Also rename from `Ray` to `SeparatedVector2`, which is a more accurate name for what it is.
1 parent e4fdb26 commit 5bace44

File tree

6 files changed

+146
-40
lines changed

6 files changed

+146
-40
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42151,6 +42151,8 @@ ORIGIN: ../../../flutter/impeller/geometry/rect.cc + ../../../flutter/LICENSE
4215142151
ORIGIN: ../../../flutter/impeller/geometry/rect.h + ../../../flutter/LICENSE
4215242152
ORIGIN: ../../../flutter/impeller/geometry/saturated_math.h + ../../../flutter/LICENSE
4215342153
ORIGIN: ../../../flutter/impeller/geometry/scalar.h + ../../../flutter/LICENSE
42154+
ORIGIN: ../../../flutter/impeller/geometry/separated_vector.cc + ../../../flutter/LICENSE
42155+
ORIGIN: ../../../flutter/impeller/geometry/separated_vector.h + ../../../flutter/LICENSE
4215442156
ORIGIN: ../../../flutter/impeller/geometry/shear.cc + ../../../flutter/LICENSE
4215542157
ORIGIN: ../../../flutter/impeller/geometry/shear.h + ../../../flutter/LICENSE
4215642158
ORIGIN: ../../../flutter/impeller/geometry/sigma.cc + ../../../flutter/LICENSE
@@ -45017,6 +45019,8 @@ FILE: ../../../flutter/impeller/geometry/rect.cc
4501745019
FILE: ../../../flutter/impeller/geometry/rect.h
4501845020
FILE: ../../../flutter/impeller/geometry/saturated_math.h
4501945021
FILE: ../../../flutter/impeller/geometry/scalar.h
45022+
FILE: ../../../flutter/impeller/geometry/separated_vector.cc
45023+
FILE: ../../../flutter/impeller/geometry/separated_vector.h
4502045024
FILE: ../../../flutter/impeller/geometry/shear.cc
4502145025
FILE: ../../../flutter/impeller/geometry/shear.h
4502245026
FILE: ../../../flutter/impeller/geometry/sigma.cc

impeller/entity/geometry/stroke_path_geometry.cc

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "impeller/geometry/constants.h"
1111
#include "impeller/geometry/path_builder.h"
1212
#include "impeller/geometry/path_component.h"
13+
#include "impeller/geometry/separated_vector.h"
1314

1415
namespace impeller {
1516
using VS = SolidFillVertexShader;
@@ -163,45 +164,13 @@ class StrokeGenerator {
163164
}
164165
}
165166

166-
/// @brief Represents a ray in 2D space.
167-
///
168-
/// This is a simple convenience struct for handling polyline offset
169-
/// values when generating stroke geometry. For performance reasons,
170-
/// it's sometimes adventageous to track the direction and magnitude
171-
/// for offsets separately.
172-
struct Ray {
173-
/// The normalized direction of the ray.
174-
Vector2 direction;
175-
176-
/// The magnitude of the ray.
177-
Scalar magnitude = 0.0;
178-
179-
/// Returns the vector representation of the ray.
180-
Vector2 GetVector() const { return direction * magnitude; }
181-
182-
/// Returns the scalar alignment of the two rays.
183-
///
184-
/// Domain: [-1, 1]
185-
/// A value of 1 indicates the rays are parallel and pointing in the same
186-
/// direction. A value of -1 indicates the rays are parallel and pointing in
187-
/// opposite directions. A value of 0 indicates the rays are perpendicular.
188-
Scalar GetAlignment(const Ray& other) const {
189-
return direction.Dot(other.direction);
190-
}
191-
192-
/// Returns the scalar angle between the two rays.
193-
Radians AngleTo(const Ray& other) const {
194-
return direction.AngleTo(other.direction);
195-
}
196-
};
197-
198167
/// Computes offset by calculating the direction from point_i - 1 to point_i
199168
/// if point_i is within `contour_start_point_i` and `contour_end_point_i`;
200169
/// Otherwise, it uses direction from contour.
201-
Ray ComputeOffset(const size_t point_i,
202-
const size_t contour_start_point_i,
203-
const size_t contour_end_point_i,
204-
const Path::PolylineContour& contour) const {
170+
SeparatedVector2 ComputeOffset(const size_t point_i,
171+
const size_t contour_start_point_i,
172+
const size_t contour_end_point_i,
173+
const Path::PolylineContour& contour) const {
205174
Point direction;
206175
if (point_i >= contour_end_point_i) {
207176
direction = contour.end_direction;
@@ -211,8 +180,8 @@ class StrokeGenerator {
211180
direction = (polyline.GetPoint(point_i) - polyline.GetPoint(point_i - 1))
212181
.Normalize();
213182
}
214-
return {.direction = Vector2{-direction.y, direction.x},
215-
.magnitude = stroke_width * 0.5f};
183+
return SeparatedVector2(Vector2{-direction.y, direction.x},
184+
stroke_width * 0.5f);
216185
}
217186

218187
void AddVerticesForLinearComponent(VertexWriter& vtx_builder,
@@ -338,8 +307,8 @@ class StrokeGenerator {
338307
const CapProc<VertexWriter>& cap_proc;
339308
const Scalar scale;
340309

341-
Ray previous_offset;
342-
Ray offset;
310+
SeparatedVector2 previous_offset;
311+
SeparatedVector2 offset;
343312
SolidFillVertexShader::PerVertexData vtx;
344313
};
345314

impeller/geometry/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ impeller_component("geometry") {
3131
"rect.h",
3232
"saturated_math.h",
3333
"scalar.h",
34+
"separated_vector.cc",
35+
"separated_vector.h",
3436
"shear.cc",
3537
"shear.h",
3638
"sigma.cc",

impeller/geometry/geometry_unittests.cc

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "impeller/geometry/point.h"
2020
#include "impeller/geometry/rect.h"
2121
#include "impeller/geometry/scalar.h"
22+
#include "impeller/geometry/separated_vector.h"
2223
#include "impeller/geometry/size.h"
2324

2425
// TODO(zanderso): https://github.com/flutter/flutter/issues/127701
@@ -1141,6 +1142,56 @@ TEST(GeometryTest, Vector4Lerp) {
11411142
ASSERT_VECTOR4_NEAR(result, expected);
11421143
}
11431144

1145+
TEST(GeometryTest, SeparatedVector2NormalizesWithConstructor) {
1146+
SeparatedVector2 v(Vector2(10, 0));
1147+
ASSERT_POINT_NEAR(v.direction, Vector2(1, 0));
1148+
ASSERT_NEAR(v.magnitude, 10, kEhCloseEnough);
1149+
}
1150+
1151+
TEST(GeometryTest, SeparatedVector2GetVector) {
1152+
SeparatedVector2 v(Vector2(10, 0));
1153+
ASSERT_POINT_NEAR(v.GetVector(), Vector2(10, 0));
1154+
}
1155+
1156+
TEST(GeometryTest, SeparatedVector2GetAlignment) {
1157+
// Parallel
1158+
{
1159+
SeparatedVector2 v(Vector2(10, 0));
1160+
Scalar actual = v.GetAlignment(SeparatedVector2(Vector2(5, 0)));
1161+
ASSERT_NEAR(actual, 1, kEhCloseEnough);
1162+
}
1163+
1164+
// Perpendicular
1165+
{
1166+
SeparatedVector2 v(Vector2(10, 0));
1167+
Scalar actual = v.GetAlignment(SeparatedVector2(Vector2(0, 5)));
1168+
ASSERT_NEAR(actual, 0, kEhCloseEnough);
1169+
}
1170+
1171+
// Opposite parallel
1172+
{
1173+
SeparatedVector2 v(Vector2(0, 10));
1174+
Scalar actual = v.GetAlignment(SeparatedVector2(Vector2(0, -5)));
1175+
ASSERT_NEAR(actual, -1, kEhCloseEnough);
1176+
}
1177+
}
1178+
1179+
TEST(GeometryTest, SeparatedVector2AngleTo) {
1180+
{
1181+
SeparatedVector2 v(Vector2(10, 0));
1182+
Radians actual = v.AngleTo(SeparatedVector2(Vector2(5, 0)));
1183+
Radians expected = Radians{0};
1184+
ASSERT_NEAR(actual.radians, expected.radians, kEhCloseEnough);
1185+
}
1186+
1187+
{
1188+
SeparatedVector2 v(Vector2(10, 0));
1189+
Radians actual = v.AngleTo(SeparatedVector2(Vector2(0, -5)));
1190+
Radians expected = Radians{-kPi / 2};
1191+
ASSERT_NEAR(actual.radians, expected.radians, kEhCloseEnough);
1192+
}
1193+
}
1194+
11441195
TEST(GeometryTest, CanUseVector3AssignmentOperators) {
11451196
{
11461197
Vector3 p(1, 2, 4);

impeller/geometry/separated_vector.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2013 The Flutter 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 "separated_vector.h"
6+
7+
namespace impeller {
8+
9+
SeparatedVector2::SeparatedVector2() = default;
10+
11+
SeparatedVector2::SeparatedVector2(Vector2 direction, Scalar magnitude)
12+
: direction(direction), magnitude(magnitude){};
13+
14+
SeparatedVector2::SeparatedVector2(Vector2 vector)
15+
: direction(vector.Normalize()), magnitude(vector.GetLength()){};
16+
17+
Vector2 SeparatedVector2::GetVector() const {
18+
return direction * magnitude;
19+
}
20+
21+
Scalar SeparatedVector2::GetAlignment(const SeparatedVector2& other) const {
22+
return direction.Dot(other.direction);
23+
}
24+
25+
Radians SeparatedVector2::AngleTo(const SeparatedVector2& other) const {
26+
return direction.AngleTo(other.direction);
27+
}
28+
29+
} // namespace impeller

impeller/geometry/separated_vector.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2013 The Flutter 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+
#ifndef FLUTTER_IMPELLER_GEOMETRY_SEPARATED_VECTOR_H_
6+
#define FLUTTER_IMPELLER_GEOMETRY_SEPARATED_VECTOR_H_
7+
8+
#include "impeller/geometry/point.h"
9+
10+
#include "impeller/geometry/scalar.h"
11+
12+
namespace impeller {
13+
14+
/// @brief A Vector2, broken down as a separate magnitude and direction.
15+
/// Assumes that the direction given is normalized.
16+
///
17+
/// This is a simple convenience struct for handling polyline offset
18+
/// values when generating stroke geometry. For performance reasons,
19+
/// it's sometimes adventageous to track the direction and magnitude
20+
/// for offsets separately.
21+
struct SeparatedVector2 {
22+
/// The normalized direction of the vector.
23+
Vector2 direction;
24+
25+
/// The magnitude of the vector.
26+
Scalar magnitude = 0.0;
27+
28+
SeparatedVector2();
29+
SeparatedVector2(Vector2 direction, Scalar magnitude);
30+
explicit SeparatedVector2(Vector2 vector);
31+
32+
/// Returns the vector representation of the vector.
33+
Vector2 GetVector() const;
34+
35+
/// Returns the scalar alignment of the two vectors.
36+
/// In other words, the dot product of the two normalized vectors.
37+
///
38+
/// Range: [-1, 1]
39+
/// A value of 1 indicates the directions are parallel and pointing in the
40+
/// same direction. A value of -1 indicates the vectors are parallel and
41+
/// pointing in opposite directions. A value of 0 indicates the vectors are
42+
/// perpendicular.
43+
Scalar GetAlignment(const SeparatedVector2& other) const;
44+
45+
/// Returns the scalar angle between the two rays.
46+
Radians AngleTo(const SeparatedVector2& other) const;
47+
};
48+
49+
#endif // FLUTTER_IMPELLER_GEOMETRY_SEPARATED_VECTOR_H_
50+
51+
} // namespace impeller

0 commit comments

Comments
 (0)