DHART
Loading...
Searching...
No Matches
view_analysis.cpp
Go to the documentation of this file.
1
7
8#include <view_analysis.h>
9
10#include <vector>
11#include <array>
12#include <cmath>
13
14#include <embree_raytracer.h>
15#include <RayRequest.h>
16#include <node.h>
17
18
19using namespace HF::RayTracer;
20using std::vector;
22#undef min
23#undef max
25
27 constexpr float ConvertToRadians(float num_in_degrees) {
28 return num_in_degrees * (static_cast<float>(M_PI) / 180.00f);
29 }
30
33 inline void Normalize(std::array<float, 3> & vec) {
34 float& x = vec[0];
35 float& y = vec[1];
36 float& z = vec[2];
37
38 float magnitude = sqrt(
39 pow(x, 2) + pow(y, 2) + pow(z, 2)
40 );
41 vec[0] /= magnitude;
42 vec[1] /= magnitude;
43 vec[2] /= magnitude;
44
45 }
46
62 inline bool AltitudeWithinRange(const std::array<float, 3>& vec, float max_angle, float min_angle) {
63 float x = vec[0]; float y = vec[1]; float z = vec[2];
64 float r = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
65 float phi = acos(z / r);
66
67 return (phi >= min_angle && phi <= max_angle);
68 }
69
84 vector<std::array<float, 3>> FibbonacciDist(int num_points, float upwards_fov, float downward_fov)
85 {
86
87 // Convert limits to radians
88 const float upperlimit = ConvertToRadians(upwards_fov);
89 const float lowerlimit = ConvertToRadians(downward_fov);
90
91 // Calculate maximum and minimum altitude
92 const float max_phi = static_cast<float>(M_PI_2) + lowerlimit;
93 const float min_phi = static_cast<float>(M_PI_2) - upperlimit;
94
95 const int n = num_points;
96 std::vector<std::array<float, 3>> out_points;
97 float offset = 2.0f / float(n);
98 const float increment = static_cast<float>(M_PI)* (3.0f - sqrtf(5.0f));
99
100 for (int i = 5; i < n + 5; i++) {
101 float y = ((float(i) * offset) - 1.0f) - (offset / 0.2f);
102 float r = sqrtf(1.0f - powf(y, 2));
103
104 float phi = (float(i + 1) * increment);
105 float x = cosf(phi) * r;
106 float z = sinf(phi) * r;
107
108 // Check for nans. These can sometimes occur at the beginning and end of the result set
109 // due to imprecision.
110 if (!isnan(x) && !isnan(y) && !isnan(z)) {
111 auto new_point = std::array<float, 3>{x, y, z};
112 Normalize(new_point);
113 if (AltitudeWithinRange(new_point, max_phi, min_phi))
114 out_points.emplace_back(new_point);
115 }
116 }
117 return out_points;
118
119 }
120
121 vector<std::array<float, 3>> FibbonacciDistributePoints(int num_points, float upwards_fov, float downward_fov)
122 {
123 // Generate initial set of directions
124 auto out_points = FibbonacciDist(num_points, upwards_fov, downward_fov);
125
126 // Calculate a percentage of points discarded.
127 int points_removed = num_points - out_points.size();
128 double percent_removed = static_cast<double>(points_removed) / static_cast<double>(num_points);
129
130 // Calculate a new total based on percentage of points removed
131 int new_out_points = (num_points / (1.0 - percent_removed));
132
133 // Generate a set of points with the new total.
134 out_points.clear();
135 return FibbonacciDist(new_out_points, upwards_fov, downward_fov);
136 }
137}
Contains definitions for the ViewAnalysis namespace.
Contains definitions for the EmbreeRayTracer
Contains definitions for the RayTracer namespace.
Contains definitions for the Node structure.
vector< std::array< float, 3 > > FibbonacciDistributePoints(int num_points, float upwards_fov, float downward_fov)
Evenly distribute a set of points around a sphere centered at the origin.
Cast rays to determine if and where they intersect geometry.
Analyze space from the perspective of observers within a 3D environment.
void Normalize(std::array< float, 3 > &vec)
Normalize a vector.
vector< std::array< float, 3 > > FibbonacciDist(int num_points, float upwards_fov, float downward_fov)
bool AltitudeWithinRange(const std::array< float, 3 > &vec, float max_angle, float min_angle)
constexpr float ConvertToRadians(float num_in_degrees)
Convert a number from degrees to radians.
A point in space with an ID.
Definition: node.h:38