SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
EntityRef.h
Go to the documentation of this file.
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_EntityRef_h
11 #define smtk_model_EntityRef_h
12 
14 #include "smtk/CoreExports.h" // For EXPORT macro.
15 #include "smtk/PublicPointerDefs.h" // For WeakResourcePtr
16 #include "smtk/SystemConfig.h" // For type macros.
17 
18 #include "smtk/common/UUID.h"
19 
20 #include "smtk/model/Arrangement.h" // for ArrangementKind and Arrangements types
21 #include "smtk/model/AttributeAssignments.h" // for BitFlags type
22 #include "smtk/model/Entity.h" // for isValid() method
23 #include "smtk/model/EntityTypeBits.h" // for BitFlags type
24 #include "smtk/model/Events.h" // for ResourceEventRelationType type
25 #include "smtk/model/FloatData.h" // for Float, FloatData, ...
26 #include "smtk/model/IntegerData.h" // for Integer, IntegerData, ...
27 #include "smtk/model/StringData.h" // for String, StringData, ...
28 
29 #include <array>
30 #include <iostream>
31 #include <set>
32 #include <vector>
33 
34 namespace smtk
35 {
36 namespace mesh
37 {
38 class MeshSet;
39 }
40 } // namespace smtk
41 
43 #define SMTK_ENTITYREF_CLASS(thisclass, superclass, typecheck) \
44  SMTK_DERIVED_TYPE(thisclass, superclass); \
45  thisclass() {} \
46  thisclass(const EntityRef& other) \
47  : superclass(other) \
48  { \
49  } \
50  thisclass(smtk::model::ResourcePtr inResource, const smtk::common::UUID& entityId) \
51  : superclass(inResource, entityId) \
52  { \
53  } \
54  thisclass(EntityPtr src) \
55  { \
56  if (src) \
57  { \
58  m_resource = src->modelResource(); \
59  m_entity = src->id(); \
60  } \
61  } \
62  ~thisclass() override = default; /* Avoid warnings about non-virtual destructor */ \
63  bool isValid() const { return this->EntityRef::isValid(); } \
64  bool isValid(EntityPtr* entRec) const override \
65  { \
66  EntityPtr er; \
67  if (/* NB: EntityRef::isValid() may return true even when er == nullptr
68  * NOLINTNEXTLINE(bugprone-parent-virtual-call) */ \
69  this->EntityRef::isValid(&er) && er && smtk::model::typecheck(er->entityFlags())) \
70  { \
71  if (entRec) \
72  *entRec = er; \
73  return true; \
74  } \
75  return false; \
76  }
77 
78 namespace smtk
79 {
80 namespace model
81 {
82 
83 class EntityRef;
84 class Group;
85 class Model;
86 class Tessellation;
87 typedef std::set<EntityRef> EntityRefs;
88 typedef std::vector<EntityRef> EntityRefArray;
89 typedef std::vector<Group> Groups;
90 
94 enum Exclusions
95 {
96  Nothing = 0,
97  Rendering = (1 << 1),
98  ViewPresentation = (1 << 2),
100  Everything = (1 << 3) - 1
101 };
102 
113 class SMTKCORE_EXPORT EntityRef
114 {
115 public:
116  SMTK_BASE_TYPE(EntityRef);
117  EntityRef();
118  EntityRef(ResourcePtr resource, const smtk::common::UUID& entityId);
119  EntityRef(EntityPtr src);
120  // Add a virtual destructor to avoid clang warnings on macos 10.14:
121  virtual ~EntityRef() = default;
122 
123  bool setResource(ResourcePtr resource);
124  ResourcePtr resource();
125  const ResourcePtr resource() const;
126 
127  bool setEntity(const smtk::common::UUID& entityId);
128  const smtk::common::UUID& entity() const;
129 
130  smtk::model::EntityPtr entityRecord() const;
131  smtk::resource::ComponentPtr component() const;
132 
133  int dimension() const;
134  int dimensionBits() const;
135  void setDimensionBits(BitFlags dim);
136  BitFlags entityFlags() const;
137  std::string flagSummary(int form = 0) const;
138 
139  int maxParametricDimension() const;
140 
141  int embeddingDimension() const;
142 
143  std::string name() const;
144  void setName(const std::string& n);
145  std::string assignDefaultName(bool overwrite = false);
146 
147  bool hasVisibility() const;
148  bool visible() const;
149  void setVisible(bool vis);
150 
154  void setExclusions(bool v, int mask = Exclusions::Everything);
159  int exclusions(int mask = Exclusions::Everything) const;
160 
161  FloatList color() const;
162  bool hasColor() const;
163  void setColor(const FloatList& rgba);
164  void setColor(double r, double g, double b, double a = 1.);
165 
166  bool isValid() const;
167  virtual bool isValid(EntityPtr* entityRecord) const;
168  virtual bool checkForArrangements(ArrangementKind k, EntityPtr& entry, Arrangements*& arr) const;
169 
170  bool isCellEntity() const { return smtk::model::isCellEntity(this->entityFlags()); }
171  bool isUseEntity() const { return smtk::model::isUseEntity(this->entityFlags()); }
172  bool isShellEntity() const { return smtk::model::isShellEntity(this->entityFlags()); }
173  bool isGroup() const { return smtk::model::isGroup(this->entityFlags()); }
174  bool isModel() const { return smtk::model::isModel(this->entityFlags()); }
175  bool isInstance() const { return smtk::model::isInstance(this->entityFlags()); }
176  bool isSessionRef() const { return smtk::model::isSessionRef(this->entityFlags()); }
177  bool isAuxiliaryGeometry() const { return smtk::model::isAuxiliaryGeometry(this->entityFlags()); }
178  bool isConcept() const { return smtk::model::isConcept(this->entityFlags()); }
179 
180  bool isVertex() const { return smtk::model::isVertex(this->entityFlags()); }
181  bool isEdge() const { return smtk::model::isEdge(this->entityFlags()); }
182  bool isFace() const { return smtk::model::isFace(this->entityFlags()); }
183  bool isVolume() const { return smtk::model::isVolume(this->entityFlags()); }
184  bool isChain() const { return smtk::model::isChain(this->entityFlags()); }
185  bool isLoop() const { return smtk::model::isLoop(this->entityFlags()); }
186  bool isShell() const { return smtk::model::isShell(this->entityFlags()); }
187  bool isVertexUse() const { return smtk::model::isVertexUse(this->entityFlags()); }
188  bool isEdgeUse() const { return smtk::model::isEdgeUse(this->entityFlags()); }
189  bool isFaceUse() const { return smtk::model::isFaceUse(this->entityFlags()); }
190  bool isVolumeUse() const { return smtk::model::isVolumeUse(this->entityFlags()); }
191 
198  template<typename T>
199  T as() const
200  {
201  return T(*this);
202  }
203 
204  template<typename S, typename T>
205  static void EntityRefsFromUUIDs(S& result, ResourcePtr, const T& uids);
206 
207  template<typename S, typename T>
208  static void EntityRefsToUUIDs(S& uids, const T& entRefs);
209 
210  EntityRefs bordantEntities(int ofDimension = -2) const;
211  EntityRefs boundaryEntities(int ofDimension = -2) const;
212 
213  std::vector<double> boundingBox() const;
214  std::vector<double> unionBoundingBox(const std::vector<double>& b1, const std::vector<double>& b2)
215  const;
216  std::vector<double> unionBoundingBox(
217  const std::vector<double>& b1,
218  const std::array<double, 6>& b2) const;
219 
220  EntityRefs lowerDimensionalBoundaries(int lowerDimension);
221  EntityRefs higherDimensionalBordants(int higherDimension);
222  EntityRefs adjacentEntities(int ofDimension);
223 
224  template<typename T>
225  T relationsAs() const;
226  EntityRefs relations() const;
227  EntityRef& addRawRelation(const EntityRef& ent);
228  EntityRef& findOrAddRawRelation(const EntityRef& ent);
229  EntityRef& elideRawRelation(const EntityRef& ent);
230 
231  smtk::mesh::MeshSet meshTessellation() const;
232 
233  Tessellation* resetTessellation();
234  const Tessellation* hasTessellation() const;
235  const Tessellation* hasAnalysisMesh() const;
236  const Tessellation* gotMesh() const; //prefers the analaysis over the display
237  int setTessellation(const Tessellation* tess, int analysisMesh = 0, bool updateBBox = false);
238  int setTessellationAndBoundingBox(const Tessellation* tess, int analysisMesh = 0)
239  {
240  return this->setTessellation(tess, analysisMesh, true);
241  }
242  bool removeTessellation(bool removeGen = false);
243  void findEntitiesWithTessellation(
244  std::map<smtk::model::EntityRef, smtk::model::EntityRef>& entityrefMap,
245  std::set<smtk::model::EntityRef>& touched) const;
246  int tessellationGeneration() const;
247  bool setTessellationGeneration(int gen);
248  void setBoundingBox(const double bbox[6]);
249 
250  bool hasAttributes() const;
251  bool hasAttributes(smtk::attribute::ConstResourcePtr attRes) const;
252  bool hasAttribute(const smtk::common::UUID& attribId) const;
253  bool associateAttribute(
254  smtk::attribute::ResourcePtr attResource,
255  const smtk::common::UUID& attribId);
256  bool disassociateAttribute(
257  smtk::attribute::ResourcePtr attResource,
258  const smtk::common::UUID& attribId);
259  bool disassociateAttribute(
260  smtk::attribute::ResourcePtr attResource,
261  const smtk::common::UUID& attribId,
262  bool reverse);
263  bool disassociateAllAttributes(smtk::attribute::ResourcePtr attResource);
264  bool disassociateAllAttributes(smtk::attribute::ResourcePtr attResource, bool reverse);
266  template<typename T>
267  bool disassociateAttributes(const T& attribPtrs)
268  {
269  bool removedAny = false;
270  for (const auto& attribPtr : attribPtrs)
271  {
272  removedAny |= this->disassociateAttribute(attribPtr->attributeResource(), attribPtr->id());
273  }
274  return removedAny;
275  }
276 
282  smtk::attribute::Attributes attributes() const;
283 
284  // For T = {IntegerData, FloatData, StringData}:
285  template<typename T>
286  bool removeProperty(const std::string& name);
287 
288  void setFloatProperty(const std::string& propName, smtk::model::Float propValue);
289  void setFloatProperty(const std::string& propName, const smtk::model::FloatList& propValue);
290  smtk::model::FloatList const& floatProperty(const std::string& propName) const;
291  smtk::model::FloatList& floatProperty(const std::string& propName);
292  bool hasFloatProperty(const std::string& propName) const;
293  bool removeFloatProperty(const std::string& propName);
294  bool hasFloatProperties() const;
295  std::set<std::string> floatPropertyNames() const;
296 
297  void setStringProperty(const std::string& propName, const smtk::model::String& propValue);
298  void setStringProperty(const std::string& propName, const smtk::model::StringList& propValue);
299  smtk::model::StringList const& stringProperty(const std::string& propName) const;
300  smtk::model::StringList& stringProperty(const std::string& propName);
301  bool hasStringProperty(const std::string& propName) const;
302  bool removeStringProperty(const std::string& propName);
303  bool hasStringProperties() const;
304  std::set<std::string> stringPropertyNames() const;
305 
306  void setIntegerProperty(const std::string& propName, smtk::model::Integer propValue);
307  void setIntegerProperty(const std::string& propName, const smtk::model::IntegerList& propValue);
308  smtk::model::IntegerList const& integerProperty(const std::string& propName) const;
309  smtk::model::IntegerList& integerProperty(const std::string& propName);
310  bool hasIntegerProperty(const std::string& propName) const;
311  bool removeIntegerProperty(const std::string& propName);
312  bool hasIntegerProperties() const;
313  std::set<std::string> integerPropertyNames() const;
314 
315  int numberOfArrangementsOfKind(ArrangementKind k) const;
316  Arrangement* findArrangement(ArrangementKind k, int index);
317  const Arrangement* findArrangement(ArrangementKind k, int index) const;
318  bool clearArrangements();
319 
320  EntityRef relationFromArrangement(ArrangementKind k, int arrangementIndex, int offset) const;
321 
322  bool removeArrangement(ArrangementKind k, int index = -1);
323 
324  // Manage embedded_in/includes relationships
325  EntityRef& embedEntity(const EntityRef& thingToEmbed, bool checkExistence = true);
326  template<typename T>
327  EntityRef& embedEntities(const T& container, bool checkExistence = true);
328  bool isEmbedded(EntityRef& ent) const;
329  EntityRef embeddedIn() const;
330  bool unembedEntity(const EntityRef& thingToUnembed);
331  template<typename T>
332  EntityRef& unembedEntities(const T& container);
333  template<typename T>
334  T embeddedEntities() const;
335 
336  template<typename T>
337  T instances() const;
338 
339  Model owningModel() const;
340  SessionRef owningSession() const;
341  Groups containingGroups() const;
342 
343  bool isMember(const EntityRef& entity) const;
344  EntityRef memberOf() const;
345 
346  bool operator==(const EntityRef& other) const;
347  bool operator!=(const EntityRef& other) const;
348  bool operator<(const EntityRef& other) const;
349 
350  operator bool() const { return this->isValid(); }
351 
352  std::size_t hash() const;
353 
354 protected:
355  friend class Group;
356  friend class Model;
357  friend class SessionRef;
358 
359  smtk::model::WeakResourcePtr m_resource;
360  smtk::common::UUID m_entity;
361 
362  // Manage subset_of/superset_of relationships
363  EntityRef& addMemberEntity(const EntityRef& memberToAdd);
364  template<typename T>
365  EntityRef& addMemberEntities(T begin, T end);
366  EntityRef& removeMemberEntity(const EntityRef& memberToRemove);
367  EntityRef& removeMemberEntity(int indexOfMemberToRemove);
368  template<typename T>
369  EntityRef& removeMemberEntities(T begin, T end);
370 
371  ResourceEventRelationType subsetRelationType(const EntityRef& member) const;
372  ResourceEventRelationType embeddingRelationType(const EntityRef& embedded) const;
373 };
374 
375 SMTKCORE_EXPORT std::ostream& operator<<(std::ostream& os, const EntityRef& c);
376 
377 SMTKCORE_EXPORT std::size_t entityrefHash(const EntityRef& c);
378 
379 template<typename T>
380 T EntityRef::relationsAs() const
381 {
382  T result;
383  ResourcePtr mgr = m_resource.lock();
384  smtk::model::EntityPtr entRec;
385  if (!this->isValid(&entRec))
386  return result;
387 
388  smtk::common::UUIDArray::const_iterator it;
389  for (it = entRec->relations().begin(); it != entRec->relations().end(); ++it)
390  {
391  typename T::value_type entry(mgr, *it);
392  if (entry.isValid())
393  {
394  result.insert(result.end(), entry);
395  }
396  }
397  return result;
398 }
399 
400 template<typename S, typename T>
401 void EntityRef::EntityRefsFromUUIDs(S& result, ResourcePtr mgr, const T& uids)
402 {
403  for (typename T::const_iterator it = uids.begin(); it != uids.end(); ++it)
404  {
405  typename S::value_type entry(mgr, *it);
406  if (entry.isValid())
407  {
408  result.insert(result.end(), entry);
409  }
410  }
411 }
412 
413 template<typename S, typename T>
414 void EntityRef::EntityRefsToUUIDs(S& uids, const T& entRefs)
415 {
416  for (typename T::const_iterator it = entRefs.begin(); it != entRefs.end(); ++it)
417  {
418  if (it->entity())
419  {
420  uids.insert(uids.end(), it->entity());
421  }
422  }
423 }
424 
425 template<typename T>
426 EntityRef& EntityRef::embedEntities(const T& container, bool checkExistence)
427 {
428  for (typename T::const_iterator it = container.begin(); it != container.end(); ++it)
429  {
430  this->embedEntity(*it, checkExistence);
431  }
432  return *this;
433 }
434 
435 template<typename T>
436 EntityRef& EntityRef::unembedEntities(const T& container)
437 {
438  for (typename T::const_iterator it = container.begin(); it != container.end(); ++it)
439  {
440  this->unembedEntity(*it);
441  }
442  return *this;
443 }
444 
445 template<typename T>
446 EntityRef& EntityRef::addMemberEntities(T begin, T end)
447 {
448  for (T it = begin; it != end; ++it)
449  this->addMemberEntity(*it);
450  return *this;
451 }
452 
453 template<typename T>
454 EntityRef& EntityRef::removeMemberEntities(T begin, T end)
455 {
456  for (T it = begin; it != end; ++it)
457  this->removeMemberEntity(*it);
458  return *this;
459 }
460 
461 } // namespace model
462 } // namespace smtk
463 
464 #endif
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
smtk::model::EntityRef::isValid
bool isValid() const
Return whether the entityref is pointing to valid resource that contains the UUID of the entity.
Definition: EntityRef.cxx:459
smtk::model::ResourceEventRelationType
ResourceEventRelationType
Enumerate the types of relationships which may cause model resource events.
Definition: Events.h:77
PublicPointerDefs.h
smtk::model::Everything
@ Everything
All of the exclusions above apply (except Nothing).
Definition: EntityRef.h:99
smtk::model::Rendering
@ Rendering
This entity's tessellation will not be in model renderings(but may be used as a prototype for Instanc...
Definition: EntityRef.h:96
smtk::attribute::Attributes
std::vector< smtk::attribute::AttributePtr > Attributes
Definition: PublicPointerDefs.h:464
smtk::model::EntityRef::unembedEntity
bool unembedEntity(const EntityRef &thingToUnembed)
Unembed the specified thingToUnembed as an inclusion into this entityref's entity.
Definition: EntityRef.cxx:1605
smtk::model::Arrangement
Store an arrangement of solid model entities.
Definition: Arrangement.h:40
smtk::model::Model
A entityref subclass that provides methods specific to models.
Definition: Model.h:30
smtk::model::BitFlags
unsigned int BitFlags
The integer type used to hold bit values describing an entity's type.
Definition: EntityTypeBits.h:25
smtk::model::StringList
std::vector< String > StringList
Use vectors of String objects for holding string properties on model entities.
Definition: StringData.h:32
smtk::common::UUID
Definition: UUID.h:38
smtk::model::Arrangements
std::vector< smtk::model::Arrangement > Arrangements
A vector of Arrangements is associated to each Manager entity.
Definition: PublicPointerDefs.h:141
StringData.h
smtk::model::EntityRef::EntityRefsFromUUIDs
static void EntityRefsFromUUIDs(S &result, ResourcePtr, const T &uids)
Convert a set of UUIDs into a set of entityrefs referencing the same rsrc.
Definition: EntityRef.h:400
smtk::task::operator<<
std::ostream & operator<<(std::ostream &os, const State &s)
States may be appended to streams.
Definition: State.h:86
smtk::model::Tessellation
Store geometric information related to model entities.
Definition: Tessellation.h:91
smtk::model::EntityRef::relationsAs
T relationsAs() const
Return all of the entities related to this entityref.
Definition: EntityRef.h:379
smtk::model::EntityPtr
smtk::shared_ptr< smtk::model::Entity > EntityPtr
Definition: PublicPointerDefs.h:423
smtk::model::EntityRefArray
std::vector< smtk::model::EntityRef > EntityRefArray
Definition: PublicPointerDefs.h:166
smtk::model::Nothing
@ Nothing
This entity is excluded from nothing.
Definition: EntityRef.h:95
smtk::model::EntityRef::embedEntities
EntityRef & embedEntities(const T &container, bool checkExistence=true)
Embed each of the entities in the container inside this entity.
Definition: EntityRef.h:425
smtk::model::SessionRef
A entityref subclass that provides methods specific to entity use records.
Definition: SessionRef.h:28
smtk::model::EntityRef::embedEntity
EntityRef & embedEntity(const EntityRef &thingToEmbed, bool checkExistence=true)
Embed the specified thingToEmbed as an inclusion into this entityref's entity.
Definition: EntityRef.cxx:1577
smtk::model::WeakResourcePtr
smtk::weak_ptr< smtk::model::Resource > WeakResourcePtr
Definition: PublicPointerDefs.h:421
smtk::mesh::MeshSet
Definition: MeshSet.h:42
smtk::model::ViewPresentation
@ ViewPresentation
This entity will never be presented to users.
Definition: EntityRef.h:98
smtk::model::String
std::string String
Use std::string for holding string values.
Definition: StringData.h:30
smtk::model::EntityRef::removeMemberEntity
EntityRef & removeMemberEntity(const EntityRef &memberToRemove)
Remove the memberToRemove from this EntityRef.
Definition: EntityRef.cxx:1832
smtk::model::ResourcePtr
smtk::shared_ptr< smtk::model::Resource > ResourcePtr
Definition: PublicPointerDefs.h:419
smtk::model::Exclusions
Exclusions
Indicate an entity is excluded from which parts in smtk.
Definition: EntityRef.h:93
smtk::model::EntityRef::addMemberEntity
EntityRef & addMemberEntity(const EntityRef &memberToAdd)
Add an entity as a member of this entity without any membership constraint checks.
Definition: EntityRef.cxx:1815
smtk::attribute::ConstResourcePtr
smtk::shared_ptr< const smtk::attribute::Resource > ConstResourcePtr
Definition: PublicPointerDefs.h:606
smtk::model::ArrangementKind
ArrangementKind
Specification of how a cell's relations are arranged.
Definition: ArrangementKind.h:48
smtk::resource::ComponentPtr
smtk::shared_ptr< smtk::resource::Component > ComponentPtr
Definition: PublicPointerDefs.h:297
smtk::model::EntityRefs
std::set< smtk::model::EntityRef > EntityRefs
Definition: PublicPointerDefs.h:162
smtk::model::EntityRef
A lightweight entityref pointing to a model entity's resource.
Definition: EntityRef.h:112
smtk::attribute::ResourcePtr
smtk::shared_ptr< smtk::attribute::Resource > ResourcePtr
Definition: PublicPointerDefs.h:604
smtk::attribute::ConstDefinitionPtr
smtk::shared_ptr< const smtk::attribute::Definition > ConstDefinitionPtr
Definition: PublicPointerDefs.h:452