DHART
Loading...
Searching...
No Matches
cost_algorithms.h
Go to the documentation of this file.
1
8
9#ifndef COST_ALGORITHMS_H
10#define COST_ALGORITHMS_H
11
12#include <vector>
13#include <string>
14
15#include "Constants.h"
16
17namespace HF::SpatialStructures {
18 struct Node;
19 struct Edge;
20 struct EdgeSet;
21 struct IntEdge;
22 class Graph;
23 struct Subgraph;
24}
25
42 double to_radians(double degrees);
43
67 double to_degrees(double radians);
68
93 template <size_t dimension, typename float_precision = float>
94 float_precision euclidean_distance(float_precision point_a[], float_precision point_b[]) {
95 float_precision sum = 0;
96
97 for (size_t i = 0; i < dimension; i++) {
98 float_precision difference = point_b[i] - point_a[i];
99 sum += std::pow(difference, 2.0);
100 }
101
102 return std::sqrt(sum);
103 }
104
131 template <size_t dimension, typename float_precision = float>
132 float_precision dot_product(float_precision vec_U[], float_precision vec_V[]) {
133 float_precision sum = 0;
134
135 for (size_t i = 0; i < dimension; i++) {
136 sum += vec_U[i] * vec_V[i];
137 }
138
139 return sum;
140 }
141
168 template <size_t dimension, typename float_precision = float>
169 bool is_perpendicular(float_precision vec_U[], float_precision vec_V[]) {
170 // Mathematically,
171 // two vectors are perpendicular if their dot product is
172 // equal to zero. But since it is a mortal sin to test
173 // floating point numbers for equality (using operator==) --
174 // we can test if the dot product of
175 // vector_a and vector_b is 'close enough' to zero,
176 // by determining if our dot_product calculation
177 // is less than our ROUNDING_PRECISION constant.
178 // (which is 0.0001)
179 return std::abs(dot_product<dimension, float_precision>(vec_U, vec_V))
181 }
182
230 template <size_t dimension, typename float_precision = float>
231 std::vector<Edge> GetPerpendicularEdges(const Subgraph& sg, const Node& child_node_a) {
232 std::vector<Edge> perpendicular_edges;
233
234 auto& parent_node = sg.m_parent;
235 auto& edges = sg.m_edges;
236
237 for (Edge edge_b : edges) {
238 Node child_node_b = edge_b.child;
239 // We iterate over all children by parent_node.
240 // The goal is to compare the vector formed by parent_node and child_a,
241 // with the vectors formed by parent_node and all other child node by this parent, all other child_b's.
242
243 /*
244 std::cout << "parent " << parent_node.id << " has child " << edge_b.child.id
245 << " with data " << edge_b.score << std::endl;
246 */
247
248 if (child_node_a != child_node_b) {
249 //
250 // Here -- we should assess the step_type field for edge_b.
251 //
252 // /// <summary> Describes the type of step an edge connects to. </summary>
253 // enum STEP {
254 // NOT_CONNECTED = 0, ///< No connection between parent and child.
255 // NONE = 1, ///< Parent and child are on the same plane and no step is required.
256 // UP = 2, ///< A step up is required to get from parent to child.
257 // DOWN = 3, ///< A step down is required to get from parent to child.
258 // OVER = 4 ///< A step over something is required to get from parent to child.
259 // };
260 //
261
262 // Retrieve the { x, y, z } components of the vectors formed by
263 // parent_node and child_node_a
264 // parent_node and child_node_b
265 auto vector_a = parent_node.directionTo(child_node_a);
266 auto vector_b = parent_node.directionTo(child_node_b);
267
268 if (is_perpendicular<dimension, float_precision>(vector_a.data(), vector_b.data())) {
269 // If this evaluates true,
270 // we add edge_b to perpendicular_edges.
271 perpendicular_edges.push_back(edge_b);
272 }
273 }
274 else {
275 /*
276 // If child_node_b is the same as the child_node we passed in, skip it.
277 std::cout << " *** SKIPPED ***" << std::endl;
278 */
279 }
280 }
281
282 return perpendicular_edges;
283 }
284
326 std::vector<IntEdge> CalculateCrossSlope(const Subgraph& sg);
327
366 std::vector<std::vector<IntEdge>> CalculateCrossSlope(const Graph& g);
367
368
369
412
451 std::vector<EdgeSet> CalculateEnergyExpenditure(const Graph& g);
452
453 /*
454 \summary Calculates the slope between two nodes
455 \param parent A Node
456 \param child A Node
457 \returns A double of the Angle
458 */
459 double CalculateSlope(Node& parent, Node& child);
460}
461
462#endif
Contains definitions for the HF::SpatialStructures namespace.
Contains standard fundamental data structures for representing space used throughout DHARTAPI.
constexpr float ROUNDING_PRECISION
Minimum value that can be represented in DHART_API.
Definition: Constants.h:30
A Subgraph consists of a parent Node m_parent and a container of Edge m_edges such that all Edge in m...
Definition: graph.h:364
Node m_parent
The parent node from which all Edge in m_edges extend.
Definition: graph.h:365
std::vector< Edge > m_edges
The edges that extend from m_parent.
Definition: graph.h:366
float_precision euclidean_distance(float_precision point_a[], float_precision point_b[])
bool is_perpendicular(float_precision vec_U[], float_precision vec_V[])
std::vector< IntEdge > CalculateCrossSlope(const Subgraph &sg)
EdgeSet CalculateEnergyExpenditure(const Subgraph &sg)
double CalculateSlope(Node &parent, Node &child)
std::vector< Edge > GetPerpendicularEdges(const Subgraph &sg, const Node &child_node_a)
float_precision dot_product(float_precision vec_U[], float_precision vec_V[])
A connection to a child node.
Definition: Edge.h:29
A collection of edges and a parent.
Definition: Edge.h:77
A Graph of nodes connected by edges that supports both integers and HF::SpatialStructures::Node.
Definition: graph.h:486
A point in space with an ID.
Definition: node.h:38
std::array< float, 3 > directionTo(const Node &n2) const
Get the direction between this node and another node
Definition: node.cpp:131