Atoms Crowd  7.0.0
CompareNodes.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 registerCompareNodes();
21 
22  template <typename T>
23  class CompareNode : public Node
24  {
25  public:
26 
27  inline bool compareValues(const T& A, const T& B, int op);
28 
29  static unsigned int staticTypeId();
30 
31  unsigned int typeId() const { return CompareNode<T>::staticTypeId(); };
32 
33  static std::string staticTypeStr();
34 
35  virtual std::string typeStr() const { return CompareNode<T>::staticTypeStr(); };
36 
37  virtual Node* clone();
38 
39  static Node* creator() { return new CompareNode<T>(); }
40 
41  CompareNode();
42 
43  virtual ~CompareNode();
44 
45  virtual bool compute(const ComputeData* computeData);
46 
47  private:
48 
49  PortTemplate<T> *m_in1;
50 
51  PortTemplate<T> *m_in2;
52 
53  LongPort* m_compType;
54 
55  PortTemplate<bool> *m_out;
56  };
57 
58  template <typename T>
59  inline bool CompareNode<T>::compareValues(const T& A, const T& B, int op)
60  {
61  switch (op)
62  {
63  case 0:
64  return A == B;
65  case 1:
66  return A != B;
67  case 2:
68  return A < B;
69  case 3:
70  return A > B;
71  case 4:
72  return A <= B;
73  case 5:
74  return A >= B;
75  default:
76  return A == B;
77  }
78  }
79 
80  template<>
81  inline bool CompareNode<AtomsMath::Vector3>::compareValues(const AtomsMath::Vector3& A, const AtomsMath::Vector3& B, int op)
82  {
83  switch (op)
84  {
85  case 0:
86  return A == B;
87  case 1:
88  return A != B;
89  default:
90  return false;
91  }
92  }
93 
94  template<>
95  inline bool CompareNode<AtomsMath::Vector2>::compareValues(const AtomsMath::Vector2& A, const AtomsMath::Vector2& B, int op)
96  {
97  switch (op)
98  {
99  case 0:
100  return A == B;
101  case 1:
102  return A != B;
103  default:
104  return false;
105  }
106  }
107 
108  template<>
109  inline bool CompareNode<AtomsMath::Quaternion>::compareValues(const AtomsMath::Quaternion& A, const AtomsMath::Quaternion& B, int op)
110  {
111  switch (op)
112  {
113  case 0:
114  return A == B;
115  case 1:
116  return A != B;
117  default:
118  return false;
119  }
120  }
121 
122  template<>
123  inline bool CompareNode<AtomsMath::Matrix>::compareValues(const AtomsMath::Matrix& A, const AtomsMath::Matrix& B, int op)
124  {
125  switch (op)
126  {
127  case 0:
128  return A == B;
129  case 1:
130  return A != B;
131  default:
132  return false;
133  }
134  }
135 
136  template <typename T>
138  Node* node = new CompareNode<T>();
139  if (!node)
140  return nullptr;
141  node->setName(name());
142 
143  for (auto portIt = inputPortCBegin(); portIt != inputPortCEnd(); ++portIt)
144  {
145  if ((*portIt)->numConnections() > 0)
146  continue;
147  Port *port = node->getInputPort((*portIt)->name());
148  port->copyValue(*portIt);
149  }
150  return node;
151  };
152 
153  template <typename T>
154  bool CompareNode<T>::compute(const ComputeData* computeData)
155  {
156 
157  m_out->set(compareValues(m_in1->getRef(), m_in2->getRef(), m_compType->get()));
158  return true;
159  }
160 
161  template <typename T>
163  {
164  }
165 
166  template <typename T>
167  CompareNode<T>::CompareNode()
168  {
169  m_in1 = new PortTemplate<T>("in1");
170  m_in2 = new PortTemplate<T>("in2");
171  m_compType = new LongPort("operation");
172  m_out = new PortTemplate<bool>("out");
173 
174  m_compType->set(0);
175 
176  addInputPort(m_in1);
177  addInputPort(m_in2);
178  addInputPort(m_compType);
179  addOutputPort(m_out);
180  }
181 
182  ATOMS_IMPLEMENT_NODE(CompareNode<bool>, CompareBool, COMPARE_BOOL_NODE_ID)
183  ATOMS_IMPLEMENT_NODE(CompareNode<long>, CompareLong, COMPARE_LONG_NODE_ID)
184  ATOMS_IMPLEMENT_NODE(CompareNode<double>, CompareDouble, COMPARE_DOUBLE_NODE_ID)
185  ATOMS_IMPLEMENT_NODE(CompareNode<std::string>, CompareString, COMPARE_STRING_NODE_ID)
186  ATOMS_IMPLEMENT_NODE(CompareNode<AtomsMath::Matrix>, CompareMatrix, COMPARE_MATRIX_NODE_ID)
187  ATOMS_IMPLEMENT_NODE(CompareNode<AtomsMath::Vector3>, CompareVector, COMPARE_VECTOR_NODE_ID)
188  ATOMS_IMPLEMENT_NODE(CompareNode<AtomsMath::Quaternion>, CompareQuaternion, COMPARE_QUATERNION_NODE_ID)
189 }
Definition: CompareNodes.h:24
virtual bool compute(const ComputeData *computeData)
Compute function.
Definition: CompareNodes.h:154
virtual std::string typeStr() const
Type string.
Definition: CompareNodes.h:35
unsigned int typeId() const
Type id.
Definition: CompareNodes.h:31
virtual Node * clone()
clone object
Definition: CompareNodes.h:137
Definition: Node.h:21
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