OMNeT++ API 6.1
Discrete Event Simulation Library
osgutil.h
1 //==========================================================================
2 // OSGUTIL.H - header for
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_OSGUTIL_H
17 #define __OMNETPP_OSGUTIL_H
18 
19 #include <omnetpp.h>
20 
21 #ifdef WITH_OSG
22 
23 #include <osg/Group>
24 
25 namespace omnetpp {
26 
27 // IMPORTANT: This class must NOT be used within the simulation kernel, so
28 // the sim. kernel doesn't have a linker dependency on the OSG libraries.
29 
56 class cObjectOsgNode : public osg::Group
57 {
58  protected:
59  int componentId; // 0=none
60  const cObject *object; // object pointer; if componentId!=0, it takes precedence
61 
62  protected:
63  virtual ~cObjectOsgNode() {}
64 
65  public:
66  cObjectOsgNode() : componentId(0), object(nullptr) {}
67  cObjectOsgNode(const cObject *object) : componentId(0), object(nullptr) { setObject(object);}
68  cObjectOsgNode(const cObjectOsgNode& node, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
69  META_Node(osg, cObjectOsgNode);
70 
71  const cObject *getObject() const;
72  void setObject(const cObject *obj);
73 };
74 
75 inline cObjectOsgNode::cObjectOsgNode(const cObjectOsgNode& node, const osg::CopyOp& copyop) : Group(node, copyop)
76 {
77  componentId = node.componentId;
78  object = node.object;
79 }
80 
81 inline const cObject *cObjectOsgNode::getObject() const
82 {
83  return componentId != 0 ? getSimulation()->getComponent(componentId) : object;
84 }
85 
86 inline void cObjectOsgNode::setObject(const cObject *obj)
87 {
88  // if the object is a component, store ID instead so we can avoid having
89  // a dangling pointer when it's deleted
90  if (const cComponent *component = dynamic_cast<const cComponent*>(obj)) {
91  componentId = component->getId();
92  object = nullptr;
93  }
94  else {
95  componentId = 0;
96  object = obj;
97  }
98 }
99 
100 } // namespace omnetpp
101 
102 #endif // WITH_OSG
103 
104 #endif
105 
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::cSimulation::getComponent
cComponent * getComponent(int id) const
Definition: csimulation.h:234
omnetpp::getSimulation
cSimulation * getSimulation()
Returns the currently active simulation, or nullptr if there is none.
Definition: csimulation.h:608
omnetpp::cObjectOsgNode
An osg::Group for defining correspondence of a 3D object to an OMNeT++ object.
Definition: osgutil.h:56