SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
EntityRefArrangementOps.h
1 //=========================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //=========================================================================
10 #ifndef smtk_model_EntityRefArrangementOps_h
11 #define smtk_model_EntityRefArrangementOps_h
12 
13 #include "smtk/model/ArrangementKind.h"
14 #include "smtk/model/EntityRef.h"
15 
16 namespace smtk
17 {
18 namespace model
19 {
20 
24 class SMTKCORE_EXPORT EntityRefArrangementOps
25 {
26 public:
27  static int findSimpleRelationship(const EntityRef& a, ArrangementKind k, const EntityRef& b);
28  static int findOrAddSimpleRelationship(const EntityRef& a, ArrangementKind k, const EntityRef& b);
29  static int addSimpleRelationship(
30  const EntityRef& a,
32  const EntityRef& b,
33  bool find = true);
34 
36  template<typename T>
37  static T firstRelation(const EntityRef& c, ArrangementKind k)
38  {
39  EntityPtr entRec;
40  Arrangements* arr;
41  if (c.checkForArrangements(k, entRec, arr))
42  {
43  smtk::common::UUIDArray const& relations(entRec->relations());
44  for (Arrangements::iterator arrIt = arr->begin(); arrIt != arr->end(); ++arrIt)
45  {
46  std::vector<int>::iterator it;
47  for (it = arrIt->details().begin(); it != arrIt->details().end(); ++it)
48  {
49  return T(c.resource(), relations[*it]);
50  }
51  }
52  }
53  return T();
54  }
55 
61  template<typename T>
62  static void appendAllRelations(const EntityRef& c, ArrangementKind k, T& result)
63  {
64  EntityPtr entRec;
65  Arrangements* arr;
66  if (c.checkForArrangements(k, entRec, arr))
67  {
68  switch (k)
69  {
70  case HAS_USE:
71  if (isCellEntity(entRec->entityFlags()))
72  {
73  appendAllCellHasUseRelations(c.resource(), entRec, arr, result);
74  return;
75  }
76  else if (isShellEntity(entRec->entityFlags()))
77  {
78  appendAllShellHasUseRelations(c.resource(), entRec, arr, result);
79  return;
80  }
81  break;
82  case HAS_CELL:
83  if (isUseEntity(entRec->entityFlags()))
84  {
85  appendAllUseHasCellRelations(c.resource(), entRec, arr, result);
86  return;
87  }
88  break;
89  default:
90  break;
91  }
92  appendAllSimpleRelations(c.resource(), entRec, arr, result);
93  }
94  }
95 
98  template<typename T>
99  static void
101  {
102  smtk::common::UUIDArray const& relations(entRec->relations());
103  for (Arrangements::iterator arrIt = arr->begin(); arrIt != arr->end(); ++arrIt)
104  {
105  // Use HAS_CELL arrangements are specified as [relIdx, sense] tuples.
106  int relIdx, relSense;
107  if (arrIt->IndexAndSenseFromUseHasCell(relIdx, relSense) && relIdx >= 0)
108  {
109  typename T::value_type entry(resource, relations[relIdx]);
110  if (entry.isValid())
111  result.insert(result.end(), entry);
112  }
113  }
114  }
115  template<typename T>
116  static void
117  appendAllCellHasUseRelations(ResourcePtr resource, EntityPtr entRec, Arrangements* arr, T& result)
118  {
119  smtk::common::UUIDArray const& relations(entRec->relations());
120  for (Arrangements::iterator arrIt = arr->begin(); arrIt != arr->end(); ++arrIt)
121  {
122  // Cell HAS_USE arrangements are specified as [relIdx, sense, orientation] tuples.
123  int relIdx, relSense;
124  Orientation relOrient;
125  if (arrIt->IndexSenseAndOrientationFromCellHasUse(relIdx, relSense, relOrient) && relIdx >= 0)
126  {
127  typename T::value_type entry(resource, relations[relIdx]);
128  if (entry.isValid())
129  result.insert(result.end(), entry);
130  }
131  }
132  }
133  template<typename T>
134  static void appendAllShellHasUseRelations(
135  ResourcePtr resource,
136  EntityPtr entRec,
137  Arrangements* arr,
138  T& result)
139  {
140  smtk::common::UUIDArray const& relations(entRec->relations());
141  for (Arrangements::iterator arrIt = arr->begin(); arrIt != arr->end(); ++arrIt)
142  {
143  // Shell HAS_USE arrangements are specified as [min,max[ offset-ranges,
144  // not arrays of offset values.
145  int i0, i1;
146  arrIt->IndexRangeFromShellHasUse(i0, i1);
147  for (int i = i0; i < i1; ++i)
148  {
149  typename T::value_type entry(resource, relations[i]);
150  if (entry.isValid())
151  result.insert(result.end(), entry);
152  }
153  }
154  }
160  template<typename T, typename U>
162  ResourcePtr resource,
163  EntityPtr entRec,
164  Arrangements* arr,
165  T& result,
166  U& rangeDetector)
167  {
168  smtk::common::UUIDArray const& relations(entRec->relations());
169  for (Arrangements::iterator arrIt = arr->begin(); arrIt != arr->end(); ++arrIt)
170  {
171  // Shell HAS_USE arrangements are specified as [min,max[ offset-ranges,
172  // not arrays of offset values.
173  int i0, i1;
174  arrIt->IndexRangeFromShellHasUse(i0, i1);
175  for (int i = i0; i < i1; ++i)
176  {
177  typename T::value_type entry(resource, relations[i]);
178  rangeDetector.insert(i);
179  entRec->invalidateRelationByIndex(i);
180  if (entry.isValid())
181  {
182  result.insert(result.end(), entry);
183  }
184  }
185  }
186  arr->clear();
187  }
188  template<typename T>
189  static void
190  appendAllSimpleRelations(ResourcePtr resource, EntityPtr entRec, Arrangements* arr, T& result)
191  {
192  smtk::common::UUIDArray const& relations(entRec->relations());
193  for (Arrangements::iterator arrIt = arr->begin(); arrIt != arr->end(); ++arrIt)
194  {
195  std::vector<int>::iterator it;
196  for (it = arrIt->details().begin(); it != arrIt->details().end(); ++it)
197  {
198  if (*it < 0)
199  continue; // Ignore invalid indices
200  typename T::value_type entry(resource, relations[*it]);
201  if (entry.isValid())
202  {
203  result.insert(result.end(), entry);
204  }
205  }
206  }
207  }
208 };
209 
210 // What follows are methods of EntityRef that require EntityRefArrangementOps.
211 // This breaks an include-dependency cycle.
212 
213 template<typename T>
214 T EntityRef::embeddedEntities() const
215 {
216  T result;
218  return result;
219 }
220 
221 template<typename T>
223 {
224  T result;
226  return result;
227 }
228 
229 } // namespace model
230 } // namespace smtk
231 
232 #endif // smtk_model_EntityRefArrangementOps_h
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
smtk::model::EntityRef::resource
ResourcePtr resource()
Return the underlying resource the entityref references.
Definition: EntityRef.cxx:78
smtk::model::Orientation
Orientation
Constants that describe a cell-use's orientation relative to its parent cell.
Definition: ArrangementKind.h:39
smtk::model::EntityRefArrangementOps::appendAllUseHasCellRelations
static void appendAllUseHasCellRelations(ResourcePtr resource, EntityPtr entRec, Arrangements *arr, T &result)
Helper methods used by appendAllRelations.
Definition: EntityRefArrangementOps.h:100
smtk::model::EntityRefArrangementOps
A class to help obtain entityrefs from arrangement information.
Definition: EntityRefArrangementOps.h:24
smtk::model::Arrangements
std::vector< smtk::model::Arrangement > Arrangements
A vector of Arrangements is associated to each Manager entity.
Definition: PublicPointerDefs.h:141
smtk::model::HAS_USE
@ HAS_USE
How this cell's shells are combined into a single orientation for use by bordant cells.
Definition: ArrangementKind.h:54
smtk::model::EntityRef::instances
T instances() const
Return all the instances this object serves as a prototype for.
Definition: EntityRefArrangementOps.h:222
smtk::model::EntityRef::checkForArrangements
virtual bool checkForArrangements(ArrangementKind k, EntityPtr &entry, Arrangements *&arr) const
A wrapper around EntityRef::isValid() which also verifies an arrangement exists.
Definition: EntityRef.cxx:493
EntityRef.h
smtk::model::EntityPtr
smtk::shared_ptr< smtk::model::Entity > EntityPtr
Definition: PublicPointerDefs.h:423
smtk::common::UUIDArray
std::vector< smtk::common::UUID > UUIDArray
Definition: PublicPointerDefs.h:35
smtk::model::INCLUDES
@ INCLUDES
How another cell is contained in the interior of this cell.
Definition: ArrangementKind.h:51
smtk::model::ResourcePtr
smtk::shared_ptr< smtk::model::Resource > ResourcePtr
Definition: PublicPointerDefs.h:419
smtk::model::INSTANCED_BY
@ INSTANCED_BY
This entity has an instance (duplicate) that is the related entity.
Definition: ArrangementKind.h:61
smtk::model::HAS_CELL
@ HAS_CELL
How a use or shell is related to its cell.
Definition: ArrangementKind.h:52
smtk::model::EntityRefArrangementOps::popAllShellHasUseRelations
static void popAllShellHasUseRelations(ResourcePtr resource, EntityPtr entRec, Arrangements *arr, T &result, U &rangeDetector)
Invalidate all the relations participating in shell-has-use arrangements, clear the arrangements,...
Definition: EntityRefArrangementOps.h:161
smtk::model::ArrangementKind
ArrangementKind
Specification of how a cell's relations are arranged.
Definition: ArrangementKind.h:48
smtk::model::EntityRefArrangementOps::appendAllRelations
static void appendAllRelations(const EntityRef &c, ArrangementKind k, T &result)
Append all the relations of kind k to result.
Definition: EntityRefArrangementOps.h:62
smtk::model::EntityRef
A lightweight entityref pointing to a model entity's resource.
Definition: EntityRef.h:112
smtk::model::EntityRefArrangementOps::firstRelation
static T firstRelation(const EntityRef &c, ArrangementKind k)
Return the first relation of kind k as the specified entityref type T.
Definition: EntityRefArrangementOps.h:37