DHART
Loading...
Searching...
No Matches
node.cpp
Go to the documentation of this file.
1
7
8#include <node.h>
9#include <Constants.h>
10#include <exception>
11#include <cmath>
12
13namespace HF {
14 namespace SpatialStructures {
16 x = NAN;
17 y = NAN;
18 z = NAN;
19 id = NAN;
20 }
21
22 Node::Node(float x, float y, float z, int id) : x(x), y(y), z(z), id(id) {};
23
24 Node::Node(const std::array<float, 3>& position) {
25 x = position[0];
26 y = position[1];
27 z = position[2];
28 id = -1;
30 }
31
32 Node::Node(const std::array<float, 3>& position, NODE_TYPE t, int id) {
33 id = -1;
34 type = t;
35 x = position[0];
36 y = position[1];
37 z = position[2];
38 }
39
40 float Node::distanceTo(const Node& n2) const {
41 return
42 sqrtf(pow((x - n2.x), 2) +
43 pow((y - n2.y), 2) +
44 pow((z - n2.z), 2)
45 );
46 }
47
48 //TODO: This needs an actual angle formula
49 //float Node::angleTo(const Node& n2) const { return glm::angle(glm::normalize(getV3() - n2.getV3()), glm::vec3(0.0f, 0.0f, 1.0f)); }
50
51 float& Node::operator[](int i) {
52 switch (i) {
53 case 0:
54 return x;
55 break;
56 case 1:
57 return y;
58 break;
59 case 2:
60 return z;
61 break;
62 default:
63 throw std::exception(); //TODO: Make this a custom human factors exception
64 }
65 }
66
67 float Node::operator[](int i) const {
68 switch (i) {
69 case 0:
70 return x;
71 break;
72 case 1:
73 return y;
74 break;
75 case 2:
76 return z;
77 break;
78 default:
79 throw std::exception(); // TODO: Make this a custom human factors exception
80 }
81 }
82
83 bool Node::operator==(const Node& n2) const {
84 const float dist = sqrtf(powf(n2.x - x, 2) + powf(n2.y - y, 2) + powf(n2.z - z, 2));
85 return dist < ROUNDING_PRECISION;
86 }
87
88 //TODO: How will we handle this?
89 //inline bool Node::operator==(const Node& n2) const { return distanceTo(n2) <= ROUNDING_PRECISION(); }
90 //inline void Node::operator=(const std::array<float, 3>& n2) {
91 // x = n2[0];
92 // y = n2[1];
93 // z = n2[2];
94 //}
95
96 bool Node::operator!=(const Node& n2) const { return !operator==(n2); }
97
98 // inline expansion of operator- does not work
99 //inline Node Node::operator-(const Node& n2)const { return Node(x - n2.x, y - n2.y, z - n2.z); }
100 Node Node::operator-(const Node& n2) const { return Node(x - n2.x, y - n2.y, z - n2.z); }
101
102 // inline expansion of operator+ does not work
103
104 Node Node::operator+(const Node& n2) const { return Node(x + n2.x, y + n2.y, z + n2.z); }
105
106 // TODO: Dot product?
107 // inline expansion of operator* does not work
108 /*
109 inline Node Node::operator*(const Node& n2) const {
110 return Node(x * n2.x, y * n2.y, z * n2.z);
111 }
112 */
113
114 Node Node::operator*(const Node& n2) const {
115 return Node(x * n2.x, y * n2.y, z * n2.z);
116 }
117
118 bool Node::operator<(const Node& n2) const { return id < n2.id; }
119 bool Node::operator<(const Node& n2){return id < n2.id; } // This needs to exist for std sort
120 bool Node::operator>(const Node& n2) const { return id > n2.id; }
121
122 // Normalize the vector by reference
123 void Normalize(std::array<float, 3>& vector) {
124 auto magnitude = sqrtf(powf(vector[0], 2) + powf(vector[1], 2) + powf(vector[2], 2));
125 vector[0] = vector[0] / magnitude;
126 vector[1] = vector[1] / magnitude;
127 vector[2] = vector[2] / magnitude;
128 }
129
130 // Get the normalized direction between two points in space
131 std::array<float, 3> Node::directionTo(const Node& n2) const {
132 std::array<float, 3> direction_vector;
133 direction_vector[0] = n2.x - x;
134 direction_vector[1] = n2.y - y;
135 direction_vector[2] = n2.z - z;
136
137 Normalize(direction_vector);
138 return direction_vector;
139 }
140
141 std::array<float, 3> Node::getArray() const {
142 return std::array<float, 3>{x, y, z};
143 }
144 }
145}
Contains definitions for the HF::SpatialStructures namespace.
Contains definitions for the Node structure.
Perform human scale analysis on 3D environments.
constexpr float ROUNDING_PRECISION
Minimum value that can be represented in DHART_API.
Definition: Constants.h:30
NODE_TYPE
The type of node this is.
Definition: node.h:29
@ GRAPH
This node is a graph node.
Definition: node.h:30
void Normalize(std::array< float, 3 > &vector)
Definition: node.cpp:123
A point in space with an ID.
Definition: node.h:38
Node operator+(const Node &n2) const
Creates a new node from the vector addition of this node and n2.
Definition: node.cpp:104
Node operator*(const Node &n2) const
Creates a new node from the dot product of this node and n2.
Definition: node.cpp:114
bool operator<(const Node &n2) const
Determines if this node's id (an integer) is less than n2's id.
Definition: node.cpp:118
short type
Unused.
Definition: node.h:41
Node()
Default constructor. Every element contained is defaulted to NAN.
Definition: node.cpp:15
float z
Cartesian coordinates x, y, z.
Definition: node.h:40
std::array< float, 3 > getArray() const
Returns the x,y,z of this node as an array of 3 floats.
Definition: node.cpp:141
float & operator[](int i)
Directly access a nodes's position as if it were an array of 3 floats,
Definition: node.cpp:51
bool operator!=(const Node &n2) const
See operator==, checks if this node does NOT occupy the same space as n2.
Definition: node.cpp:96
int id
Node identifier.
Definition: node.h:42
bool operator>(const Node &n2) const
Determines if this node's id (an integer) is greater than n2's id
Definition: node.cpp:120
float distanceTo(const Node &n2) const
Calculate the distance between this node and n2.
Definition: node.cpp:40
std::array< float, 3 > directionTo(const Node &n2) const
Get the direction between this node and another node
Definition: node.cpp:131
Node operator-(const Node &n2) const
Creates a node from the vector subtraction of this node and n2's position.
Definition: node.cpp:100
bool operator==(const Node &n2) const
Check if this node occupies the same space as n2.
Definition: node.cpp:83