iterative closest points
CEV ICP algorithm library
Loading...
Searching...
No Matches
lidar_view.cpp
Go to the documentation of this file.
1/*
2 * @author Ethan Uppal
3 * @copyright Copyright (C) 2024 Ethan Uppal. All rights reserved.
4 */
5
6#include <cassert>
7#include <cstdlib>
8#include <random>
9#include <sdlwrapper/util/logger.h>
10#include <sdlwrapper/util/keyboard.h>
11#include <sdlwrapper/geo/midpoint.h>
12#include "lidar_view.h"
13#include "view_config.h"
14
15#define CIRCLE_RADIUS 3
16
17LidarView::LidarView(std::vector<icp::Vector> source, std::vector<icp::Vector> destination,
18 const std::string method, const icp::ICP::Config& config)
19 : source(source),
20 destination(destination),
21 keyboard(false),
22 is_iterating(false),
23 iterations(0) {
24 icp = icp::ICP::from_method(method, config);
25 icp->begin(source, destination, icp::RBTransform());
26}
27
29 icp.release();
30}
31
32void LidarView::step() {
33 icp->iterate();
34 iterations++;
35}
36
37void LidarView::on_event(const SDL_Event& event) {
38 bool space_before = keyboard.query(SDLK_SPACE);
39 bool d_before = keyboard.query(SDLK_d);
40 bool i_before = keyboard.query(SDLK_i);
41 keyboard.update(event);
42 bool space_after = keyboard.query(SDLK_SPACE);
43 bool d_after = keyboard.query(SDLK_d);
44 bool i_after = keyboard.query(SDLK_i);
45
46 if (!space_before && space_after) {
47 is_iterating = !is_iterating;
48 }
49 if (!i_before && i_after) {
50 step();
51 }
52 if (!d_before && d_after) {
53 std::cerr << "DEBUG PRINT:\n";
54 std::cerr << "icp->current_transform() = " << icp->current_transform().to_string() << '\n';
55 std::cerr << "icp->calculate_cost() = " << icp->calculate_cost() << '\n';
56 std::cerr << "iterations = " << iterations << '\n';
57 }
58}
59
60void LidarView::draw(SDL_Renderer* renderer, const SDL_Rect* frame, double dtime) {
62 SDL_SetRenderDrawColor(renderer, 100, 100, 100, 255);
63 } else {
64 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
65 }
66 SDL_RenderClear(renderer);
67
68 SDL_SetRenderDrawColor(renderer, 0, 0, 255, SDL_ALPHA_OPAQUE);
69 for (const icp::Vector& point: destination) {
70 SDL_DrawCircle(renderer, point[0] + view_config::x_displace,
72 }
73
74 SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
75 for (const icp::Vector& point: source) {
76 icp::Vector result = icp->current_transform().apply_to(point);
77 SDL_DrawCircle(renderer, result[0] + view_config::x_displace,
79 }
80
81 icp::Vector a_cm = icp->current_transform().apply_to(icp::get_centroid(source));
82 SDL_SetRenderDrawColor(renderer, 255, 0, 0, 10);
83 SDL_DrawCircle(renderer, a_cm.x() + view_config::x_displace, a_cm.y() + view_config::y_displace,
84 20);
85
86 icp::Vector b_cm = icp::get_centroid(destination);
87 SDL_SetRenderDrawColor(renderer, 0, 0, 255, 10);
88 SDL_DrawCircle(renderer, b_cm.x() + view_config::x_displace, b_cm.y() + view_config::y_displace,
89 20);
90
91 if (is_iterating) {
92 step();
93 }
94}
void on_event(const SDL_Event &event) override
LidarView(std::vector< icp::Vector > source, std::vector< icp::Vector > destination, const std::string method, const icp::ICP::Config &config=icp::ICP::Config())
Constructs a new lidar view visualizing ICP (by method method) on the given instance (source and dest...
~LidarView() noexcept override
void draw(SDL_Renderer *renderer, const SDL_Rect *frame, double dtime) override
Configuration for ICP instances.
Definition icp.h:92
static std::unique_ptr< ICP > from_method(std::string name, const Config &params=Config())
Factory constructor for the ICP method name with configuration config.
Definition icp.cpp:98
#define CIRCLE_RADIUS
Definition geo.cpp:9
Eigen::Vector2d Vector
Definition geo.h:15
Vector get_centroid(const std::vector< Vector > &points)
Definition geo.cpp:10
bool use_light_background
double y_displace
double x_displace
Rigid-body transformation.
Definition geo.h:19