From 5091c79e96b22288d629a31c93aa71afab2b9782 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Wed, 27 Mar 2024 10:28:56 +0000
Subject: [PATCH] BUG: topoSet: allow use of 'set' as input for zones. Fixes
 #3126

---
 src/meshTools/topoSet/topoSets/cellZoneSet.C  | 57 ++++++++++++++-----
 src/meshTools/topoSet/topoSets/faceZoneSet.C  | 42 ++++++++++++--
 src/meshTools/topoSet/topoSets/pointZoneSet.C | 57 ++++++++++++++-----
 3 files changed, 124 insertions(+), 32 deletions(-)

diff --git a/src/meshTools/topoSet/topoSets/cellZoneSet.C b/src/meshTools/topoSet/topoSets/cellZoneSet.C
index 4fca3baacc0..fd36af0d2fa 100644
--- a/src/meshTools/topoSet/topoSets/cellZoneSet.C
+++ b/src/meshTools/topoSet/topoSets/cellZoneSet.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2017 OpenFOAM Foundation
-    Copyright (C) 2018-2022 OpenCFD Ltd.
+    Copyright (C) 2018-2022,2024 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -114,7 +114,12 @@ Foam::cellZoneSet::cellZoneSet
 :
     cellSet(mesh, name, set.size(), wOpt),
     mesh_(mesh),
-    addressing_(refCast<const cellZoneSet>(set).addressing())
+    addressing_
+    (
+        isA<const cellZoneSet>(set)
+      ? refCast<const cellZoneSet>(set).addressing()
+      : set.sortedToc()
+    )
 {
     updateSet();
 }
@@ -156,13 +161,27 @@ void Foam::cellZoneSet::subset(const topoSet& set)
 {
     DynamicList<label> newAddressing(addressing_.size());
 
-    const cellZoneSet& zoneSet = refCast<const cellZoneSet>(set);
+    const auto* setPtr = dynamic_cast<const cellZoneSet*>(&set);
 
-    for (const label celli : zoneSet.addressing())
+    if (setPtr)
     {
-        if (found(celli))
+        for (const label celli : setPtr->addressing())
         {
-            newAddressing.append(celli);
+            if (found(celli))
+            {
+                newAddressing.append(celli);
+            }
+        }
+    }
+    else
+    {
+        // Assume a cellSet
+        for (const label celli : refCast<const cellSet>(set).sortedToc())
+        {
+            if (found(celli))
+            {
+                newAddressing.append(celli);
+            }
         }
     }
 
@@ -192,13 +211,27 @@ void Foam::cellZoneSet::addSet(const topoSet& set)
 {
     DynamicList<label> newAddressing(addressing_);
 
-    const cellZoneSet& zoneSet = refCast<const cellZoneSet>(set);
+    const auto* setPtr = dynamic_cast<const cellZoneSet*>(&set);
 
-    for (const label celli : zoneSet.addressing())
+    if (setPtr)
     {
-        if (!found(celli))
+        for (const label celli : setPtr->addressing())
         {
-            newAddressing.append(celli);
+            if (!found(celli))
+            {
+                newAddressing.append(celli);
+            }
+        }
+    }
+    else
+    {
+        // Assume a cellSet
+        for (const label celli : refCast<const cellSet>(set).sortedToc())
+        {
+            if (!found(celli))
+            {
+                newAddressing.append(celli);
+            }
         }
     }
 
@@ -228,11 +261,9 @@ void Foam::cellZoneSet::subtractSet(const topoSet& set)
 {
     DynamicList<label> newAddressing(addressing_.size());
 
-    const cellZoneSet& zoneSet = refCast<const cellZoneSet>(set);
-
     for (const label celli : addressing_)
     {
-        if (!zoneSet.found(celli))
+        if (!set.found(celli))
         {
             // Not found in zoneSet so add
             newAddressing.append(celli);
diff --git a/src/meshTools/topoSet/topoSets/faceZoneSet.C b/src/meshTools/topoSet/topoSets/faceZoneSet.C
index 6b230325ead..73431e3890c 100644
--- a/src/meshTools/topoSet/topoSets/faceZoneSet.C
+++ b/src/meshTools/topoSet/topoSets/faceZoneSet.C
@@ -220,8 +220,18 @@ void Foam::faceZoneSet::subset
 
 void Foam::faceZoneSet::subset(const topoSet& set)
 {
-    const faceZoneSet& zoneSet = refCast<const faceZoneSet>(set);
-    subset(zoneSet.name(), zoneSet.addressing(), zoneSet.flipMap());
+    const auto* setPtr = dynamic_cast<const faceZoneSet*>(&set);
+
+    if (setPtr)
+    {
+        subset(setPtr->name(), setPtr->addressing(), setPtr->flipMap());
+    }
+    else
+    {
+        // Assume a faceSet. Ignore flipMap
+        const auto& fSet = refCast<const faceSet>(set);
+        subset(fSet.name(), fSet.sortedToc(), boolList::null());
+    }
 }
 
 
@@ -282,8 +292,18 @@ void Foam::faceZoneSet::addSet
 
 void Foam::faceZoneSet::addSet(const topoSet& set)
 {
-    const faceZoneSet& zoneSet = refCast<const faceZoneSet>(set);
-    addSet(zoneSet.name(), zoneSet.addressing(), zoneSet.flipMap());
+    const auto* setPtr = dynamic_cast<const faceZoneSet*>(&set);
+
+    if (setPtr)
+    {
+        addSet(setPtr->name(), setPtr->addressing(), setPtr->flipMap());
+    }
+    else
+    {
+        // Assume a faceSet. Ignore flipMap
+        const auto& fSet = refCast<const faceSet>(set);
+        addSet(fSet.name(), fSet.sortedToc(), boolList::null());
+    }
 }
 
 
@@ -346,8 +366,18 @@ void Foam::faceZoneSet::subtractSet
 
 void Foam::faceZoneSet::subtractSet(const topoSet& set)
 {
-    const faceZoneSet& zoneSet = refCast<const faceZoneSet>(set);
-    subtractSet(zoneSet.name(), zoneSet.addressing(), zoneSet.flipMap());
+    const auto* setPtr = dynamic_cast<const faceZoneSet*>(&set);
+
+    if (setPtr)
+    {
+        subtractSet(setPtr->name(), setPtr->addressing(), setPtr->flipMap());
+    }
+    else
+    {
+        // Assume a faceSet. Ignore flipMap
+        const auto& fSet = refCast<const faceSet>(set);
+        subtractSet(fSet.name(), fSet.sortedToc(), boolList::null());
+    }
 }
 
 
diff --git a/src/meshTools/topoSet/topoSets/pointZoneSet.C b/src/meshTools/topoSet/topoSets/pointZoneSet.C
index 752ab2524bd..1bcd5b503da 100644
--- a/src/meshTools/topoSet/topoSets/pointZoneSet.C
+++ b/src/meshTools/topoSet/topoSets/pointZoneSet.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2017 OpenFOAM Foundation
-    Copyright (C) 2018-2022 OpenCFD Ltd.
+    Copyright (C) 2018-2022,2024 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -115,7 +115,12 @@ Foam::pointZoneSet::pointZoneSet
 :
     pointSet(mesh, name, set.size(), wOpt),
     mesh_(mesh),
-    addressing_(refCast<const pointZoneSet>(set).addressing())
+    addressing_
+    (
+        isA<const pointZoneSet>(set)
+      ? refCast<const pointZoneSet>(set).addressing()
+      : set.sortedToc()
+    )
 {
     updateSet();
 }
@@ -156,13 +161,27 @@ void Foam::pointZoneSet::subset(const topoSet& set)
 {
     DynamicList<label> newAddressing(addressing_.size());
 
-    const pointZoneSet& zoneSet = refCast<const pointZoneSet>(set);
+    const auto* setPtr = dynamic_cast<const pointZoneSet*>(&set);
 
-    for (const label pointi : zoneSet.addressing())
+    if (setPtr)
     {
-        if (found(pointi))
+        for (const label pointi : setPtr->addressing())
         {
-            newAddressing.append(pointi);
+            if (found(pointi))
+            {
+                newAddressing.append(pointi);
+            }
+        }
+    }
+    else
+    {
+        // Assume a pointSet
+        for (const label pointi : refCast<const pointSet>(set).sortedToc())
+        {
+            if (found(pointi))
+            {
+                newAddressing.append(pointi);
+            }
         }
     }
 
@@ -192,13 +211,27 @@ void Foam::pointZoneSet::addSet(const topoSet& set)
 {
     DynamicList<label> newAddressing(addressing_);
 
-    const pointZoneSet& zoneSet = refCast<const pointZoneSet>(set);
+    const auto* setPtr = dynamic_cast<const pointZoneSet*>(&set);
 
-    for (const label pointi : zoneSet.addressing())
+    if (setPtr)
     {
-        if (!found(pointi))
+        for (const label pointi : setPtr->addressing())
         {
-            newAddressing.append(pointi);
+            if (!found(pointi))
+            {
+                newAddressing.append(pointi);
+            }
+        }
+    }
+    else
+    {
+        // Assume a pointSet
+        for (const label pointi : refCast<const pointSet>(set).sortedToc())
+        {
+            if (!found(pointi))
+            {
+                newAddressing.append(pointi);
+            }
         }
     }
 
@@ -228,11 +261,9 @@ void Foam::pointZoneSet::subtractSet(const topoSet& set)
 {
     DynamicList<label> newAddressing(addressing_.size());
 
-    const pointZoneSet& zoneSet = refCast<const pointZoneSet>(set);
-
     for (label pointi : addressing_)
     {
-        if (!zoneSet.found(pointi))
+        if (!set.found(pointi))
         {
             // Not found in zoneSet so add
             newAddressing.append(pointi);
-- 
GitLab