Generic filters also for edge pair, edge and text collections

This commit is contained in:
Matthias Koefferlein
2024-01-26 11:49:15 +01:00
parent 2dca4158f2
commit 9fbc926d67
9 changed files with 586 additions and 140 deletions
+43
View File
@@ -30,6 +30,20 @@ def csort(s)
s.split(/(?<=\));(?=\()/).sort.join(";")
end
class PerpendicularEdgesFilter < RBA::EdgePairFilter
# Constructor
def initialize
self.is_isotropic_and_scale_invariant # orientation and scale do not matter
end
# Select edge pairs where the edges are perpendicular
def selected(edge_pair)
return edge_pair.first.d.sprod_sign(edge_pair.second.d) == 0
end
end
class DBEdgePairs_TestClass < TestBase
# Basics
@@ -331,6 +345,35 @@ class DBEdgePairs_TestClass < TestBase
end
# Generic filters
def test_generic_filters
# Some basic tests for the filter class
f = PerpendicularEdgesFilter::new
assert_equal(f.wants_variants?, true)
f.wants_variants = false
assert_equal(f.wants_variants?, false)
# Smoke test
f.is_isotropic
f.is_scale_invariant
# Some application
f = PerpendicularEdgesFilter::new
edge_pairs = RBA::EdgePairs::new
edge_pairs.insert(RBA::EdgePair::new([0, 0, 100, 0], [0, 100, 0, 300 ]))
edge_pairs.insert(RBA::EdgePair::new([200, 0, 300, 0], [200, 100, 220, 300 ]))
assert_equal(edge_pairs.filtered(f).to_s, "(0,0;100,0)/(0,100;0,300)")
assert_equal(edge_pairs.to_s, "(0,0;100,0)/(0,100;0,300);(200,0;300,0)/(200,100;220,300)")
edge_pairs.filter(f)
assert_equal(edge_pairs.to_s, "(0,0;100,0)/(0,100;0,300)")
end
end
+47
View File
@@ -30,6 +30,21 @@ def csort(s)
s.split(/(?<=\));(?=\()/).sort.join(";")
end
class ParallelFilter < RBA::EdgeFilter
# Constructor
def initialize(ref_edge)
self.is_scale_invariant # orientation matters, but scale does not
@ref_edge = ref_edge
end
# Select only parallel ones
def selected(edge)
return edge.is_parallel?(@ref_edge)
end
end
class DBEdges_TestClass < TestBase
# Basics
@@ -749,6 +764,38 @@ class DBEdges_TestClass < TestBase
end
# Generic filters
def test_generic_filters
# Some basic tests for the filter class
f = ParallelFilter::new(RBA::Edge::new(0, 0, 100, 100))
assert_equal(f.wants_variants?, true)
f.wants_variants = false
assert_equal(f.wants_variants?, false)
assert_equal(f.requires_raw_input, false)
f.requires_raw_input = false
assert_equal(f.requires_raw_input, false)
# Smoke test
f.is_isotropic
f.is_scale_invariant
# Some application
f = ParallelFilter::new(RBA::Edge::new(0, 0, 100, 100))
edges = RBA::Edges::new
edges.insert(RBA::Edge::new(100, 0, 200, 100))
edges.insert(RBA::Edge::new(100, 100, 100, 200))
assert_equal(edges.filtered(f).to_s, "(100,0;200,100)")
assert_equal(edges.to_s, "(100,0;200,100);(100,100;100,200)")
edges.filter(f)
assert_equal(edges.to_s, "(100,0;200,100)")
end
end
load("test_epilogue.rb")
+3
View File
@@ -1227,6 +1227,9 @@ class DBRegion_TestClass < TestBase
region.insert(RBA::Box::new(200, 0, 300, 100))
assert_equal(region.filtered(TriangleFilter::new).to_s, "(0,0;100,100;100,0)")
assert_equal(region.to_s, "(0,0;100,100;100,0);(200,0;200,100;300,100;300,0)")
region.filter(TriangleFilter::new)
assert_equal(region.to_s, "(0,0;100,100;100,0)")
end
+45
View File
@@ -30,6 +30,21 @@ def csort(s)
s.split(/(?<=\));(?=\()/).sort.join(";")
end
class TextStringLengthFilter < RBA::TextFilter
# Constructor
def initialize(string_length)
self.is_isotropic_and_scale_invariant # orientation and scale do not matter
@string_length = string_length
end
# Select texts with given string length
def selected(text)
return text.string.size == @string_length
end
end
class DBTexts_TestClass < TestBase
# Basics
@@ -324,6 +339,36 @@ class DBTexts_TestClass < TestBase
end
# Generic filters
def test_generic_filters
# Some basic tests for the filter class
f = TextStringLengthFilter::new(3)
assert_equal(f.wants_variants?, true)
f.wants_variants = false
assert_equal(f.wants_variants?, false)
# Smoke test
f.is_isotropic
f.is_scale_invariant
# Some application
f = TextStringLengthFilter::new(3)
texts = RBA::Texts::new
texts.insert(RBA::Text::new("long", [ RBA::Trans::M45, 10, 0 ]))
texts.insert(RBA::Text::new("tla", [ RBA::Trans::R0, 0, 0 ]))
texts.insert(RBA::Text::new("00", [ RBA::Trans::R90, 0, 20 ]))
assert_equal(texts.filtered(f).to_s, "('tla',r0 0,0)")
assert_equal(texts.to_s, "('long',m45 10,0);('tla',r0 0,0);('00',r90 0,20)")
texts.filter(f)
assert_equal(texts.to_s, "('tla',r0 0,0)")
end
end