Atoms Crowd  7.0.0
IfNodes.h
1 #pragma once
2 // ===========================================================================
3 // Copyright (c) 2015 Toolchefs Ltd. All rights reserved.
4 //
5 // Use of this software is subject to the terms of the Toolchefs license
6 // agreement provided at the time of installation or download, or which
7 // otherwise accompanies this software in either electronic or hard copy form.
8 // ===========================================================================
9 
10 #include <AtomsGraph/Globals.h>
11 #include <AtomsGraph/Nodes/NodeIds.h>
12 #include <AtomsGraph/Node.h>
13 #include <AtomsGraph/Ports.h>
14 #include <AtomsUtils/AtomsMath.h>
15 #include <cmath>
16 #include <algorithm>
17 
18 namespace AtomsGraph
19 {
20  ATOMSGRAPH_EXPORT void registerIfNodes();
21 
22  template <typename T>
23  class IfNode : public Node
24  {
25  public:
26 
27  static unsigned int staticTypeId();
28 
29  unsigned int typeId() const { return IfNode<T>::staticTypeId(); };
30 
31  static std::string staticTypeStr();
32 
33  virtual std::string typeStr() const { return IfNode<T>::staticTypeStr(); };
34 
35  virtual Node* clone();
36 
37  static Node* creator() { return new IfNode<T>(); }
38 
39  IfNode();
40 
41  virtual ~IfNode();
42 
43  virtual bool compute(const ComputeData* computeData);
44 
45  private:
46  PortTemplate<bool> *m_inB;
47 
48  PortTemplate<T> *m_in1;
49  PortTemplate<T> *m_in2;
50 
51  PortTemplate<T> *m_out;
52  };
53 
54  template <typename T>
56  Node* node = new IfNode<T>();
57  if (!node)
58  return nullptr;
59  node->setName(name());
60 
61  for (auto portIt = inputPortCBegin(); portIt != inputPortCEnd(); ++portIt)
62  {
63  if ((*portIt)->numConnections() > 0)
64  continue;
65  Port *port = node->getInputPort((*portIt)->name());
66  port->copyValue(*portIt);
67  }
68  return node;
69  };
70 
71  template <typename T>
72  bool IfNode<T>::compute(const ComputeData* computeData)
73  {
74  if (m_inB->get())
75  {
76  m_out->set(m_in1->getRef());
77  }
78  else
79  {
80  m_out->set(m_in2->getRef());
81  }
82 
83  return true;
84  }
85 
86  template <typename T>
88  {
89  }
90 
91  template <typename T>
92  IfNode<T>::IfNode()
93  {
94  m_inB = new PortTemplate<bool>("if");
95  m_in1 = new PortTemplate<T>("in1");
96  m_in2 = new PortTemplate<T>("in2");
97  m_out = new PortTemplate<T>("out");
98 
99  addInputPort(m_inB);
100  addInputPort(m_in1);
101  addInputPort(m_in2);
102  addOutputPort(m_out);
103  }
104 
105  ATOMS_IMPLEMENT_NODE(IfNode<bool>, IfBool, IF_BOOL_NODE_ID)
106  ATOMS_IMPLEMENT_NODE(IfNode<long>, IfLong, IF_LONG_NODE_ID)
107  ATOMS_IMPLEMENT_NODE(IfNode<double>, IfDouble, IF_DOUBLE_NODE_ID)
108  ATOMS_IMPLEMENT_NODE(IfNode<std::string>, IfString, IF_STRING_NODE_ID)
109  ATOMS_IMPLEMENT_NODE(IfNode<AtomsMath::Matrix>, IfMatrix, IF_MATRIX_NODE_ID)
110  ATOMS_IMPLEMENT_NODE(IfNode<AtomsMath::Vector3>, IfVector, IF_VECTOR_NODE_ID)
111  ATOMS_IMPLEMENT_NODE(IfNode<AtomsMath::Quaternion>, IfQuaternion, IF_QUATERNION_NODE_ID)
112  ATOMS_IMPLEMENT_NODE(IfNode<AtomsUtils::Curve>, IfCurve, IF_CURVE_NODE_ID)
113 }
Definition: Node.h:21
Definition: IfNodes.h:24
unsigned int typeId() const
Type id.
Definition: IfNodes.h:29
virtual bool compute(const ComputeData *computeData)
Compute function.
Definition: IfNodes.h:72
virtual Node * clone()
clone object
Definition: IfNodes.h:55
virtual std::string typeStr() const
Type string.
Definition: IfNodes.h:33
Definition: Node.h:31
void setName(const std::string &name)
Sets the node name.
Definition: Node.impl.h:56
T * getInputPort(const std::string &name)
Gets input port.
Definition: Node.impl.h:84
BasePort class.
Definition: Port.h:26
virtual void copyValue(Port *other)
Copy the data from another port.
AtomsGraph namespace.
Definition: PosePort.h:15