Skip to content

Commit dc86f3b

Browse files
Ink Open Sourcecopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 917418755
1 parent bc9ef9d commit dc86f3b

23 files changed

Lines changed: 575 additions & 1 deletion

ink/brush/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ cc_test(
6969
":type_matchers",
7070
"//ink/color",
7171
"//ink/geometry:angle",
72+
"@com_google_absl//absl/hash:hash_testing",
7273
"@com_google_absl//absl/log:absl_check",
7374
"@com_google_absl//absl/status",
7475
"@com_google_absl//absl/status:status_matchers",
@@ -106,6 +107,7 @@ cc_test(
106107
":fuzz_domains",
107108
"//ink/geometry:mesh_format",
108109
"@com_google_absl//absl/container:flat_hash_set",
110+
"@com_google_absl//absl/hash:hash_testing",
109111
"@com_google_absl//absl/status",
110112
"@com_google_absl//absl/status:status_matchers",
111113
"@com_google_absl//absl/strings",
@@ -158,6 +160,8 @@ cc_test(
158160
"//ink/geometry:angle",
159161
"//ink/geometry:point",
160162
"//ink/types:duration",
163+
"@com_google_absl//absl/hash",
164+
"@com_google_absl//absl/hash:hash_testing",
161165
"@com_google_absl//absl/status",
162166
"@com_google_absl//absl/status:status_matchers",
163167
"@com_google_absl//absl/status:statusor",

ink/brush/brush.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ class Brush {
8989
sink.Append(brush.ToFormattedString());
9090
}
9191

92+
bool operator==(const Brush& other) const = default;
93+
94+
template <typename H>
95+
friend H AbslHashValue(H h, const Brush& brush) {
96+
return H::combine(std::move(h), brush.family_, brush.color_, brush.size_,
97+
brush.epsilon_);
98+
}
99+
92100
private:
93101
Brush(const BrushFamily& family, const Color& color, float size,
94102
float epsilon);

ink/brush/brush_behavior.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define INK_STROKES_BRUSH_BRUSH_BEHAVIOR_H_
1717

1818
#include <array>
19+
#include <cstdint>
1920
#include <string>
2021
#include <variant>
2122
#include <vector>
@@ -378,6 +379,12 @@ struct BrushBehavior {
378379

379380
friend bool operator==(const EnabledToolTypes&,
380381
const EnabledToolTypes&) = default;
382+
383+
template <typename H>
384+
friend H AbslHashValue(H h, const EnabledToolTypes& ett) {
385+
return H::combine(std::move(h), ett.unknown, ett.mouse, ett.touch,
386+
ett.stylus);
387+
}
381388
};
382389

383390
static constexpr EnabledToolTypes kAllToolTypes = {
@@ -461,6 +468,12 @@ struct BrushBehavior {
461468
std::array<float, 2> source_value_range;
462469

463470
friend bool operator==(const SourceNode&, const SourceNode&) = default;
471+
472+
template <typename H>
473+
friend H AbslHashValue(H h, const SourceNode& sn) {
474+
return H::combine(std::move(h), sn.source,
475+
sn.source_out_of_range_behavior, sn.source_value_range);
476+
}
464477
};
465478

466479
// Value node for producing a constant value.
@@ -471,6 +484,11 @@ struct BrushBehavior {
471484
float value;
472485

473486
friend bool operator==(const ConstantNode&, const ConstantNode&) = default;
487+
488+
template <typename H>
489+
friend H AbslHashValue(H h, const ConstantNode& cn) {
490+
return H::combine(std::move(h), cn.value);
491+
}
474492
};
475493

476494
// Value node for producing a continuous random noise function with values
@@ -492,6 +510,11 @@ struct BrushBehavior {
492510
float base_period;
493511

494512
friend bool operator==(const NoiseNode&, const NoiseNode&) = default;
513+
514+
template <typename H>
515+
friend H AbslHashValue(H h, const NoiseNode& nn) {
516+
return H::combine(std::move(h), nn.seed, nn.vary_over, nn.base_period);
517+
}
495518
};
496519

497520
//////////////////////////
@@ -509,6 +532,11 @@ struct BrushBehavior {
509532

510533
friend bool operator==(const ToolTypeFilterNode&,
511534
const ToolTypeFilterNode&) = default;
535+
536+
template <typename H>
537+
friend H AbslHashValue(H h, const ToolTypeFilterNode& ttfn) {
538+
return H::combine(std::move(h), ttfn.enabled_tool_types);
539+
}
512540
};
513541

514542
////////////////////////////
@@ -534,6 +562,11 @@ struct BrushBehavior {
534562
float damping_gap;
535563

536564
friend bool operator==(const DampingNode&, const DampingNode&) = default;
565+
566+
template <typename H>
567+
friend H AbslHashValue(H h, const DampingNode& dn) {
568+
return H::combine(std::move(h), dn.damping_source, dn.damping_gap);
569+
}
537570
};
538571

539572
// Value node for mapping a value through a response curve.
@@ -546,6 +579,11 @@ struct BrushBehavior {
546579
EasingFunction response_curve;
547580

548581
friend bool operator==(const ResponseNode&, const ResponseNode&) = default;
582+
583+
template <typename H>
584+
friend H AbslHashValue(H h, const ResponseNode& rn) {
585+
return H::combine(std::move(h), rn.response_curve);
586+
}
549587
};
550588

551589
// Value node for integrating an input value over time or distance.
@@ -571,6 +609,13 @@ struct BrushBehavior {
571609
std::array<float, 2> integral_value_range;
572610

573611
friend bool operator==(const IntegralNode&, const IntegralNode&) = default;
612+
613+
template <typename H>
614+
friend H AbslHashValue(H h, const IntegralNode& in) {
615+
return H::combine(std::move(h), in.integrate_over,
616+
in.integral_out_of_range_behavior,
617+
in.integral_value_range);
618+
}
574619
};
575620

576621
// Value node for combining two other values with a binary operation.
@@ -583,6 +628,11 @@ struct BrushBehavior {
583628
BinaryOp operation;
584629

585630
friend bool operator==(const BinaryOpNode&, const BinaryOpNode&) = default;
631+
632+
template <typename H>
633+
friend H AbslHashValue(H h, const BinaryOpNode& bon) {
634+
return H::combine(std::move(h), bon.operation);
635+
}
586636
};
587637

588638
// Value node for interpolating to/from a range of two values.
@@ -596,6 +646,11 @@ struct BrushBehavior {
596646

597647
friend bool operator==(const InterpolationNode&,
598648
const InterpolationNode&) = default;
649+
650+
template <typename H>
651+
friend H AbslHashValue(H h, const InterpolationNode& in) {
652+
return H::combine(std::move(h), in.interpolation);
653+
}
599654
};
600655

601656
//////////////////////
@@ -617,6 +672,11 @@ struct BrushBehavior {
617672
std::array<float, 2> target_modifier_range;
618673

619674
friend bool operator==(const TargetNode&, const TargetNode&) = default;
675+
676+
template <typename H>
677+
friend H AbslHashValue(H h, const TargetNode& tn) {
678+
return H::combine(std::move(h), tn.target, tn.target_modifier_range);
679+
}
620680
};
621681

622682
// Terminal node that consumes two input values (angle and magnitude), forming
@@ -638,6 +698,12 @@ struct BrushBehavior {
638698

639699
friend bool operator==(const PolarTargetNode&,
640700
const PolarTargetNode&) = default;
701+
702+
template <typename H>
703+
friend H AbslHashValue(H h, const PolarTargetNode& ptn) {
704+
return H::combine(std::move(h), ptn.target, ptn.angle_range,
705+
ptn.magnitude_range);
706+
}
641707
};
642708

643709
// A single node in a behavior's graph. Each node type is either a "value
@@ -659,6 +725,11 @@ struct BrushBehavior {
659725
std::string developer_comment;
660726

661727
friend bool operator==(const BrushBehavior&, const BrushBehavior&) = default;
728+
729+
template <typename H>
730+
friend H AbslHashValue(H h, const BrushBehavior& behavior) {
731+
return H::combine(std::move(h), behavior.nodes, behavior.developer_comment);
732+
}
662733
};
663734

664735
namespace brush_internal {

ink/brush/brush_coat.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ namespace ink {
3939
struct BrushCoat {
4040
BrushTip tip;
4141
absl::InlinedVector<BrushPaint, 1> paint_preferences = {BrushPaint{}};
42+
43+
bool operator==(const BrushCoat&) const = default;
44+
45+
template <typename H>
46+
friend H AbslHashValue(H h, const BrushCoat& coat) {
47+
return H::combine(std::move(h), coat.tip, coat.paint_preferences);
48+
}
4249
};
4350

4451
namespace brush_internal {

ink/brush/brush_coat_test.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "gtest/gtest.h"
2121
#include "fuzztest/fuzztest.h"
2222
#include "absl/container/flat_hash_set.h"
23+
#include "absl/hash/hash_testing.h"
2324
#include "absl/status/status.h"
2425
#include "absl/status/status_matchers.h"
2526
#include "absl/strings/str_cat.h"
@@ -42,6 +43,43 @@ using ::testing::UnorderedElementsAre;
4243

4344
constexpr absl::string_view kTestTextureId = "test-paint";
4445

46+
TEST(BrushCoatTest, Equality) {
47+
BrushCoat coat1, coat2;
48+
EXPECT_EQ(coat1, coat2);
49+
50+
coat1.tip.pinch = 0.5;
51+
EXPECT_NE(coat1, coat2);
52+
coat2.tip.pinch = 0.5;
53+
EXPECT_EQ(coat1, coat2);
54+
55+
coat1.paint_preferences.push_back(BrushPaint{});
56+
EXPECT_NE(coat1, coat2);
57+
coat2.paint_preferences.push_back(BrushPaint{});
58+
EXPECT_EQ(coat1, coat2);
59+
60+
coat1.paint_preferences[0] =
61+
BrushPaint{.self_overlap = BrushPaint::SelfOverlap::kDiscard};
62+
EXPECT_NE(coat1, coat2);
63+
coat2.paint_preferences[0] =
64+
BrushPaint{.self_overlap = BrushPaint::SelfOverlap::kDiscard};
65+
EXPECT_EQ(coat1, coat2);
66+
}
67+
68+
TEST(BrushCoatTest, AbslHash) {
69+
BrushPaint paint_a = {.self_overlap = BrushPaint::SelfOverlap::kDiscard};
70+
BrushPaint paint_b = {};
71+
BrushTip tip_a = {.pinch = 0.1f};
72+
BrushTip tip_b = {.pinch = 0.2f};
73+
74+
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
75+
{BrushCoat{}, BrushCoat{.tip = tip_a}, BrushCoat{.tip = tip_b},
76+
BrushCoat{.paint_preferences = {paint_a}},
77+
BrushCoat{.paint_preferences = {paint_b}},
78+
BrushCoat{.paint_preferences = {paint_a, paint_b}},
79+
BrushCoat{.tip = tip_a, .paint_preferences = {paint_a}},
80+
BrushCoat{.tip = tip_b, .paint_preferences = {paint_b}}}));
81+
}
82+
4583
TEST(BrushCoatTest, Stringify) {
4684
EXPECT_EQ(absl::StrCat(BrushCoat{.tip = BrushTip{}}),
4785
"BrushCoat{tip=BrushTip{scale=<1, 1>, corner_rounding=1}, "

ink/brush/brush_family.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ class BrushFamily {
4242
// the modeled inputs. This can be useful as a point of comparison for other
4343
// input models, or for callers who wish to do their own input modeling prior
4444
// to passing inputs into Ink.
45-
struct PassthroughModel {};
45+
struct PassthroughModel {
46+
bool operator==(const PassthroughModel&) const = default;
47+
template <typename H>
48+
friend H AbslHashValue(H h, const PassthroughModel&) {
49+
return h;
50+
}
51+
};
4652

4753
// Averages nearby inputs together within a sliding time window. To be valid,
4854
// the window size must be finite and strictly positive, and the upsampling
@@ -57,6 +63,14 @@ class BrushFamily {
5763
// inserted between them. Set this to `Duration32::Infinite()` to disable
5864
// upsampling.
5965
Duration32 upsampling_period = Duration32::Seconds(1.0 / 180.0);
66+
67+
bool operator==(const SlidingWindowModel&) const = default;
68+
69+
template <typename H>
70+
friend H AbslHashValue(H h, const SlidingWindowModel& model) {
71+
return H::combine(std::move(h), model.window_size,
72+
model.upsampling_period);
73+
}
6074
};
6175

6276
// Specifies a model for turning a sequence of raw hardware inputs (e.g. from
@@ -86,6 +100,12 @@ class BrushFamily {
86100
std::string developer_comment;
87101

88102
bool operator==(const Metadata&) const = default;
103+
104+
template <typename H>
105+
friend H AbslHashValue(H h, const Metadata& metadata) {
106+
return H::combine(std::move(h), metadata.client_brush_family_id,
107+
metadata.developer_comment);
108+
}
89109
};
90110

91111
// Returns the default `InputModel` that will be used by
@@ -160,6 +180,15 @@ class BrushFamily {
160180
sink.Append(family.ToFormattedString());
161181
}
162182

183+
bool operator==(const BrushFamily& other) const = default;
184+
185+
template <typename H>
186+
friend H AbslHashValue(H h, const BrushFamily& family) {
187+
return H::combine(std::move(h), family.coats_, family.input_model_,
188+
family.metadata_,
189+
family.opaque_decoded_proto_bytes_with_fallbacks_);
190+
}
191+
163192
friend class BrushFamilyInternalAccessor;
164193

165194
private:

0 commit comments

Comments
 (0)