https://mooseframework.inl.gov
CSGSurfaceList.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "CSGSurfaceList.h"
11 #include "CSGPlane.h"
12 #include "CSGSphere.h"
13 #include "CSGXCylinder.h"
14 #include "CSGYCylinder.h"
15 #include "CSGZCylinder.h"
16 
17 namespace CSG
18 {
19 
21 
23 {
24  for (const auto & [name, surf] : other_surface_list.getSurfaceListMap())
25  _surfaces.emplace(name, surf->clone());
26 }
27 
28 CSGSurface &
29 CSGSurfaceList::getSurface(const std::string & name) const
30 {
31  auto surf = _surfaces.find(name);
32  if (surf == _surfaces.end())
33  mooseError("No surface by name " + name + " exists in the geometry.");
34  else
35  return *(surf->second);
36 }
37 
38 std::vector<std::reference_wrapper<const CSGSurface>>
40 {
41  std::vector<std::reference_wrapper<const CSGSurface>> surfaces;
42  for (auto it = _surfaces.begin(); it != _surfaces.end(); ++it)
43  surfaces.push_back(*(it->second));
44  return surfaces;
45 }
46 
47 CSGSurface &
48 CSGSurfaceList::addSurface(std::unique_ptr<CSGSurface> surf, const bool ignore_identical_surface)
49 {
50  auto surf_name = surf->getName();
51  if (ignore_identical_surface)
52  // Check that surface already defined in _surfaces and if so, confirm it matches with input surf
53  if (auto it = _surfaces.find(surf_name); it != _surfaces.end())
54  {
55  if (*surf == *it->second)
56  return *it->second;
57  else
58  mooseError("Surface with name ",
59  surf_name,
60  " has the same name as an existing surface in CSGBase instance but cannot be "
61  "discarded as it is not an identical surface.");
62  }
63 
64  // Otherwise, add the surface to the surface list. At this point, we don't expect the surface
65  // to already be defined in the surface list
66  auto [it, inserted] = _surfaces.emplace(surf_name, std::move(surf));
67  if (!inserted)
68  mooseError("Surface with name " + surf_name + " already exists in geometry.");
69 
70  return *it->second;
71 }
72 
73 void
74 CSGSurfaceList::renameSurface(const CSGSurface & surface, const std::string & name)
75 {
76  // check that this surface passed in is actually in the same surface that is in the surface list
77  auto prev_name = surface.getName();
78  auto it = _surfaces.find(prev_name);
79  if (it == _surfaces.end() || it->second.get() != &surface)
80  mooseError("Surface " + prev_name + " cannot be renamed to " + name +
81  " as it does not exist in this CSGBase instance.");
82 
83  auto existing_surface = std::move(it->second);
84  existing_surface->setName(name);
85  _surfaces.erase(prev_name);
86  addSurface(std::move(existing_surface));
87 }
88 
89 bool
91 {
92  const auto all_surfs = this->getAllSurfaces();
93  const auto other_surfs = other.getAllSurfaces();
94 
95  // Check that same number of surfaces are defined in both lists
96  if (all_surfs.size() != other_surfs.size())
97  return false;
98 
99  // Iterate through each CSGSurface in list and check equality of surface
100  // with other list
101  for (const auto & surf : all_surfs)
102  {
103  const auto & surf_name = surf.get().getName();
104  if (!other.hasSurface(surf_name))
105  return false;
106  const auto & other_surf = other.getSurface(surf_name);
107  if (surf.get() != other_surf)
108  return false;
109  }
110  return true;
111 }
112 
113 bool
115 {
116  return !(*this == other);
117 }
118 
119 } // namespace CSG
bool operator!=(const CSGSurfaceList &other) const
Operator overload for checking if two CSGSurfaceList objects are not equal.
std::string name(const ElemQuality q)
const std::string & getName() const
Get the name of surface.
Definition: CSGSurface.h:95
bool hasSurface(const std::string &name) const
return whether surface with given name exists in surface list
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
CSGSurface & getSurface(const std::string &name) const
Get a surface by name.
std::vector< std::reference_wrapper< const CSGSurface > > getAllSurfaces() const
Get list of references to all surfaces in surface list.
std::unordered_map< std::string, std::unique_ptr< CSGSurface > > _surfaces
Mapping of surface names to pointers of stored surface objects.
void renameSurface(const CSGSurface &surface, const std::string &name)
rename the specified surface
bool operator==(const CSGSurfaceList &other) const
Operator overload for checking if two CSGSurfaceList objects are equal.
CSGSurface & addSurface(std::unique_ptr< CSGSurface > surf, const bool ignore_identical_surface=false)
add a surface object to existing SurfaceList.
std::unordered_map< std::string, std::unique_ptr< CSGSurface > > & getSurfaceListMap()
Get non-const map of all names to surfaces in surface list.
CSGSurfaceList()
Default constructor.
CSGSurfaceList is a container for storing CSGSurface objects in the CSGBase object.
CSGSurface creates an internal representation of a Constructive Solid Geometry (CSG) surface...
Definition: CSGSurface.h:26