9#include <ufo/map/color/map.hpp>
10#include <ufo/map/occupancy/map.hpp>
11#include <ufo/map/ufomap.hpp>
12#include <ufo/numeric/trans2.hpp>
13#include <ufo/vision/image.hpp>
15#include <ufo/execution/execution.hpp>
16#include <ufo/pcl/cloud.hpp>
17#include <ufo/vision/camera.hpp>
34#include <opencv2/core.hpp>
35#include <opencv2/highgui.hpp>
38#include <ufo/happly.h>
42int main(
int argc,
char* argv[])
57 Vec3f max(-100000, -100000, -100000);
59 happly::PLYData ply_in(RESOURCE_DIR
"/scene0000_00_vh_clean.ply");
60 auto v_pos = ply_in.getVertexPositions();
61 auto v_col = ply_in.getVertexColors();
62 auto f_ind = ply_in.getFaceIndices<std::size_t>();
63 std::unordered_map<HexCode, std::vector<Color>> points;
64 for (
auto f : f_ind) {
66 Vec3f(v_pos[f[1]][0], v_pos[f[1]][1], v_pos[f[1]][2]),
67 Vec3f(v_pos[f[2]][0], v_pos[f[2]][1], v_pos[f[2]][2]));
69 std::array c{Color(v_col[f[0]][0], v_col[f[0]][1], v_col[f[0]][2]),
70 Color(v_col[f[1]][0], v_col[f[1]][1], v_col[f[1]][2]),
71 Color(v_col[f[2]][0], v_col[f[2]][1], v_col[f[2]][2])};
73 for (std::size_t i{}; 3 > i; ++i) {
76 points[map.code(
Vec4f(t[i], 0.004f))].emplace_back(c[i]);
77 points[map.code(
Vec4f(t[i], 1.004f))].emplace_back(255, c[i].
green, c[i].
blue,
79 points[map.code(
Vec4f(t[i], 2.004f))].emplace_back(c[i].
red, 255, c[i].
blue,
81 points[map.code(
Vec4f(t[i], 3.004f))].emplace_back(c[i].
red, c[i].
green, 255,
86 auto f = std::async(std::launch::async, [&map, &points]() {
87 for (
auto const& [code, colors] : points) {
88 map.occupancySet(code, 1.0,
true);
89 map.colorSet(code, Color::blend(colors, ColorBlendingMode::SQ_MEAN),
true);
136 camera.vertical_fov =
radians(60.0f);
137 camera.near_clip = 0.01;
138 camera.far_clip = 100.0;
140 std::size_t rows = 720;
141 std::size_t cols = 1280;
143 std::size_t output_rows = rows / 1;
144 std::size_t output_cols = cols / 1;
151 std::vector<std::size_t> row_idx(rows);
152 std::iota(row_idx.begin(), row_idx.end(), 0);
154 std::vector<std::size_t> output_row_idx(output_rows);
155 std::iota(output_row_idx.begin(), output_row_idx.end(), 0);
159 cv::Mat img(output_rows, output_cols * 2, CV_8UC3);
161 double ufo_total_ms{};
162 double interpolate_total_ms{};
163 std::size_t iterations{};
165 float angle_x = 0.0f;
166 float angle_y = M_PI_2;
171 for (iterations = 1;
true; ++iterations) {
172 auto const t1 = std::chrono::high_resolution_clock::now();
174 if (0 == iterations % 50) {
181 Vec3f center(5.0f, 4.0f, 1.5f);
184 float cx = std::cos(angle_x);
185 float cy = std::cos(angle_y);
186 float sx = std::sin(angle_x);
187 float sy = std::sin(angle_y);
188 Vec3f offset =
Vec3f(sy * cx, sy * sx, cy) * std::exp(-zoom);
191 static_cast<Trans3f
>(lookAt<float, true>(center, center + offset, camera.up));
195 execution::par, w, camera, image,
196 [&map](
auto node,
Ray4 ,
float ) ->
bool {
197 return map.containsOccupied(node);
199 [&map](
auto node,
Ray4 ray,
200 float distance) -> std::pair<
bool, std::pair<Color, Color>> {
201 float max_distance = 7.0f;
202 return std::make_pair(
203 map.isLeaf(node) && map.containsOccupied(node),
204 std::make_pair(map.color(node),
205 Color(256 - 256 *
distance / max_distance,
206 256 - 256 *
distance / max_distance,
207 256 - 256 *
distance / max_distance)));
209 std::make_pair(Color{}, Color{}));
211 auto const t2 = std::chrono::high_resolution_clock::now();
213 double row_scale = image.rows() /
static_cast<double>(output_rows);
214 double col_scale = image.cols() /
static_cast<double>(output_cols);
216 auto f = [&](std::size_t i) {
217 for (std::size_t j{}; j < output_cols; ++j) {
218 std::size_t i_scaled = std::floor(i * row_scale);
219 std::size_t j_scaled = std::floor(j * col_scale);
220 img.at<cv::Vec3b>(i, j)[2] = image(i_scaled, j_scaled).first.red;
221 img.at<cv::Vec3b>(i, j)[1] = image(i_scaled, j_scaled).first.green;
222 img.at<cv::Vec3b>(i, j)[0] = image(i_scaled, j_scaled).first.blue;
223 img.at<cv::Vec3b>(i, output_cols + j)[2] = image(i_scaled, j_scaled).second.red;
224 img.at<cv::Vec3b>(i, output_cols + j)[1] = image(i_scaled, j_scaled).second.green;
225 img.at<cv::Vec3b>(i, output_cols + j)[0] = image(i_scaled, j_scaled).second.blue;
230 std::for_each(std::execution::par_unseq, output_row_idx.begin(), output_row_idx.end(),
234#pragma omp parallel for
236 for (std::size_t i = 0; i < output_rows; ++i) {
241 auto const t3 = std::chrono::high_resolution_clock::now();
242 std::chrono::duration<double, std::milli>
const ufo_ms = t2 - t1;
243 std::chrono::duration<double, std::milli>
const interpolate_ms = t3 - t2;
245 ufo_total_ms += ufo_ms.count();
246 interpolate_total_ms += interpolate_ms.count();
248 std::chrono::duration<double, std::milli>
const total_ms = t3 - t1;
250 std::cout << std::setw(28) << std::left
251 <<
"UFO took: " << (ufo_total_ms / iterations) <<
" ms\n";
252 std::cout << std::setw(28) << std::left
253 <<
"Interpolate took: " << (interpolate_total_ms / iterations) <<
" ms\n";
254 std::cout << std::setw(28) << std::left <<
"Total: "
255 << ((ufo_total_ms + interpolate_total_ms) / iterations) <<
" ms\n";
257 cv::imshow(
"UFOMap Rendered View", img);
259 << std::max(1,
static_cast<int>(1000 * (1.0 / fps) - total_ms.count()))
263 cv::waitKey(std::max(1,
static_cast<int>(1000 * (1.0 / fps) - total_ms.count())));
Image class for storing and manipulating 2D pixel data.
constexpr T radians(T deg) noexcept
Converts degrees to radians.
All vision-related classes and functions.
constexpr T green(Lrgb< T, Flags > color) noexcept
Returns the un-weighted green channel value.
constexpr Vec< Geometry::dimension(), typename Geometry::value_type > max(Geometry const &g)
Returns the maximum coordinate of the minimum spanning axis-aligned bounding box of a geometry.
constexpr T blue(Lrgb< T, Flags > color) noexcept
Returns the un-weighted blue channel value.
constexpr alpha_type_t< C > alpha(C color) noexcept
Returns the un-weighted alpha of color.
constexpr T red(Lrgb< T, Flags > color) noexcept
Returns the un-weighted red channel value.
constexpr Vec< Geometry::dimension(), typename Geometry::value_type > min(Geometry const &g)
Returns the minimum coordinate of the minimum spanning axis-aligned bounding box of a geometry.
constexpr auto distance(A const &a, B const &b)
Computes the minimum distance between two shapes.
Ray in Dim-dimensional space.
Triangle in Dim-dimensional space.