Commit 8e312f81 authored by Kenneth Moreland's avatar Kenneth Moreland
Browse files

Add MODE_EXCLUDE to FieldSelection

This allows you to turn off the selection of fields rather than turn
them on. It could be helpful, for example, if you were isosurfacing on a
single isovalue and didn't want to pass the field you are contouring on
(because it's all the same value).
parent 9c51de8d
......@@ -41,11 +41,12 @@ public:
{
MODE_NONE,
MODE_ALL,
MODE_SELECTED
MODE_SELECT,
MODE_EXCLUDE
};
VTKM_CONT
FieldSelection(ModeEnum mode = MODE_SELECTED)
FieldSelection(ModeEnum mode = MODE_SELECT)
: Mode(mode)
{
}
......@@ -53,8 +54,8 @@ public:
/// Use this constructor create a field selection given the field names e.g.
/// `FieldSelection({"field_one", "field_two"})`.
VTKM_CONT
FieldSelection(std::initializer_list<std::string> fields)
: Mode(MODE_SELECTED)
FieldSelection(std::initializer_list<std::string> fields, ModeEnum mode = MODE_SELECT)
: Mode(mode)
{
for (const std::string& afield : fields)
{
......@@ -66,13 +67,14 @@ public:
/// associations e.g.
/// @code{cpp}
/// FieldSelection({
/// {"field_one", vtkm::cont::Field::ASSOC_POINTS},
/// {"field_two", vtkm::cont::Field::ASSOC_CELL_SET} });
/// std::pair{"field_one", vtkm::cont::Field::ASSOC_POINTS},
/// std::pair{"field_two", vtkm::cont::Field::ASSOC_CELL_SET} });
/// @endcode
VTKM_CONT
FieldSelection(
std::initializer_list<std::pair<std::string, vtkm::cont::Field::AssociationEnum>> fields)
: Mode(MODE_SELECTED)
std::initializer_list<std::pair<std::string, vtkm::cont::Field::AssociationEnum>> fields,
ModeEnum mode = MODE_SELECT)
: Mode(mode)
{
for (const auto& item : fields)
{
......@@ -103,30 +105,17 @@ public:
case MODE_ALL:
return true;
case MODE_SELECTED:
case MODE_SELECT:
default:
if (this->Fields.find(Field(name, association)) != this->Fields.end())
{
return true;
}
// if not exact match, let's lookup for ASSOC_ANY.
for (const auto& aField : this->Fields)
{
if (aField.Name == name)
{
if (aField.Association == vtkm::cont::Field::ASSOC_ANY ||
association == vtkm::cont::Field::ASSOC_ANY)
{
return true;
}
}
}
return false;
return this->HasField(name, association);
case MODE_EXCLUDE:
return !this->HasField(name, association);
}
}
//@{
/// Add fields to map. Note, if Mode is not MODE_SELECTED, then adding fields
/// Add fields to map. Note, if Mode is not MODE_SELECT, then adding fields
/// will have no impact of the fields that will be mapped.
VTKM_CONT
void AddField(const vtkm::cont::Field& inputField)
......@@ -142,6 +131,38 @@ public:
}
//@}
/// Returns true if the input field has been added to this selection.
/// Note that depending on the mode of this selection, the result of HasField
/// is not necessarily the same as IsFieldSelected. (If the mode is MODE_SELECT,
/// then the result of the two will be the same.)
VTKM_CONT
bool HasField(const vtkm::cont::Field& inputField) const
{
return this->HasField(inputField.GetName(), inputField.GetAssociation());
}
bool HasField(const std::string& name,
vtkm::cont::Field::AssociationEnum association = vtkm::cont::Field::ASSOC_ANY) const
{
if (this->Fields.find(Field(name, association)) != this->Fields.end())
{
return true;
}
// if not exact match, let's lookup for ASSOC_ANY.
for (const auto& aField : this->Fields)
{
if (aField.Name == name)
{
if (aField.Association == vtkm::cont::Field::ASSOC_ANY ||
association == vtkm::cont::Field::ASSOC_ANY)
{
return true;
}
}
}
return false;
}
/// Clear all fields added using `AddField`.
VTKM_CONT
void ClearFields() { this->Fields.clear(); }
......
......@@ -26,7 +26,7 @@ namespace
void TestFieldSelection()
{
{
// empty field selection, everything should be false.
std::cout << "empty field selection, everything should be false." << std::endl;
vtkm::filter::FieldSelection selection;
VTKM_TEST_ASSERT(selection.IsFieldSelected("foo") == false, "field selection failed.");
VTKM_TEST_ASSERT(selection.IsFieldSelected("bar", vtkm::cont::Field::ASSOC_POINTS) == false,
......@@ -34,7 +34,7 @@ void TestFieldSelection()
}
{
// field selection with select all, everything should be true.
std::cout << "field selection with select all, everything should be true." << std::endl;
vtkm::filter::FieldSelection selection(vtkm::filter::FieldSelection::MODE_ALL);
VTKM_TEST_ASSERT(selection.IsFieldSelected("foo") == true, "field selection failed.");
VTKM_TEST_ASSERT(selection.IsFieldSelected("bar", vtkm::cont::Field::ASSOC_POINTS) == true,
......@@ -42,7 +42,7 @@ void TestFieldSelection()
}
{
// field selection with select none, everything should be false no matter what.
std::cout << "field selection with select none, everything should be false." << std::endl;
vtkm::filter::FieldSelection selection(vtkm::filter::FieldSelection::MODE_NONE);
VTKM_TEST_ASSERT(selection.IsFieldSelected("foo") == false, "field selection failed.");
VTKM_TEST_ASSERT(selection.IsFieldSelected("bar", vtkm::cont::Field::ASSOC_POINTS) == false,
......@@ -50,7 +50,7 @@ void TestFieldSelection()
}
{
// field selection with specific fields selected.
std::cout << "field selection with specific fields selected." << std::endl;
vtkm::filter::FieldSelection selection;
selection.AddField("foo");
selection.AddField("bar", vtkm::cont::Field::ASSOC_CELL_SET);
......@@ -65,7 +65,7 @@ void TestFieldSelection()
}
{
// field selection with specific fields selected.
std::cout << "field selection with specific fields selected." << std::endl;
vtkm::filter::FieldSelection selection{ "foo", "bar" };
VTKM_TEST_ASSERT(selection.IsFieldSelected("foo") == true, "field selection failed.");
VTKM_TEST_ASSERT(selection.IsFieldSelected("foo", vtkm::cont::Field::ASSOC_POINTS) == true,
......@@ -78,7 +78,7 @@ void TestFieldSelection()
}
{
// field selection with specific fields selected.
std::cout << "field selection with specific fields selected." << std::endl;
using pair_type = std::pair<std::string, vtkm::cont::Field::AssociationEnum>;
vtkm::filter::FieldSelection selection{ pair_type{ "foo", vtkm::cont::Field::ASSOC_ANY },
pair_type{ "bar", vtkm::cont::Field::ASSOC_CELL_SET } };
......@@ -91,6 +91,24 @@ void TestFieldSelection()
"field selection failed.");
VTKM_TEST_ASSERT(selection.IsFieldSelected("bar") == true, "field selection failed.");
}
{
std::cout << "field selection with specific fields excluded." << std::endl;
using pair_type = std::pair<std::string, vtkm::cont::Field::AssociationEnum>;
vtkm::filter::FieldSelection selection(
{ pair_type{ "foo", vtkm::cont::Field::ASSOC_ANY },
pair_type{ "bar", vtkm::cont::Field::ASSOC_CELL_SET } },
vtkm::filter::FieldSelection::MODE_EXCLUDE);
VTKM_TEST_ASSERT(selection.IsFieldSelected("foo") == false, "field selection failed.");
VTKM_TEST_ASSERT(selection.IsFieldSelected("foo", vtkm::cont::Field::ASSOC_POINTS) == false,
"field selection failed.");
VTKM_TEST_ASSERT(selection.IsFieldSelected("bar", vtkm::cont::Field::ASSOC_POINTS) == true,
"field selection failed.");
VTKM_TEST_ASSERT(selection.IsFieldSelected("bar", vtkm::cont::Field::ASSOC_CELL_SET) == false,
"field selection failed.");
VTKM_TEST_ASSERT(selection.IsFieldSelected("bar") == false, "field selection failed.");
VTKM_TEST_ASSERT(selection.IsFieldSelected("baz") == true, "field selection failed.");
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment