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 <sdlwrapper/util/logger.h>
9#include <sdlwrapper/util/keyboard.h>
10#include <sdlwrapper/geo/midpoint.h>
11#include "lidar_view.h"
12#include "view_config.h"
13
14#define CIRCLE_RADIUS 3
15
16LidarView::LidarView(std::vector<icp::Vector> source, std::vector<icp::Vector> destination,
17 std::unique_ptr<icp::ICP> icp)
18 : source(source),
19 destination(destination),
20 icp(std::move(icp)),
21 keyboard(false),
22 is_iterating(false),
23 iterations(0) {
24 this->icp->begin(source, destination, icp::RBTransform());
25}
26
27void LidarView::step() {
28 icp->iterate();
29 iterations++;
30}
31
32void LidarView::on_event(const SDL_Event& event) {
33 bool space_before = keyboard.query(SDLK_SPACE);
34 bool d_before = keyboard.query(SDLK_d);
35 bool i_before = keyboard.query(SDLK_i);
36 keyboard.update(event);
37 bool space_after = keyboard.query(SDLK_SPACE);
38 bool d_after = keyboard.query(SDLK_d);
39 bool i_after = keyboard.query(SDLK_i);
40
41 if (!space_before && space_after) {
42 is_iterating = !is_iterating;
43 }
44 if (!i_before && i_after) {
45 step();
46 }
47 if (!d_before && d_after) {
48 std::cerr << "DEBUG PRINT:\n";
49 std::cerr << "icp->current_transform() = " << icp->current_transform().to_string() << '\n';
50 std::cerr << "icp->calculate_cost() = " << icp->calculate_cost() << '\n';
51 std::cerr << "iterations = " << iterations << '\n';
52 }
53}
54
55void LidarView::draw(SDL_Renderer* renderer, [[maybe_unused]] const SDL_Rect* frame,
56 [[maybe_unused]] double dtime) {
58 SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
59 } else {
60 SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
61 }
62 SDL_RenderClear(renderer);
63
64 SDL_SetRenderDrawColor(renderer, 0, 0, 255, SDL_ALPHA_OPAQUE);
65 for (const icp::Vector& point: destination) {
66 SDL_DrawCircle(renderer, point[0] + view_config::x_displace,
68 }
69
70 SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
71 for (const icp::Vector& point: source) {
72 icp::Vector result = icp->current_transform().apply_to(point);
73 SDL_DrawCircle(renderer, result[0] + view_config::x_displace,
75 }
76
77 icp::Vector a_cm = icp->current_transform().apply_to(icp::get_centroid(source));
78 SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
79 SDL_DrawCircle(renderer, a_cm.x() + view_config::x_displace, a_cm.y() + view_config::y_displace,
80 20);
81
82 icp::Vector b_cm = icp::get_centroid(destination);
83 SDL_SetRenderDrawColor(renderer, 0, 0, 255, SDL_ALPHA_OPAQUE);
84 SDL_DrawCircle(renderer, b_cm.x() + view_config::x_displace, b_cm.y() + view_config::y_displace,
85 20);
86
87 if (is_iterating) {
88 step();
89 }
90}
void on_event(const SDL_Event &event) override
LidarView(std::vector< icp::Vector > source, std::vector< icp::Vector > destination, std::unique_ptr< icp::ICP > icp)
Constructs a new lidar view visualizing ICP (by method method) on the given instance (source and dest...
void draw(SDL_Renderer *renderer, const SDL_Rect *frame, double dtime) override
#define CIRCLE_RADIUS
Definition driver.h:7
Eigen::Vector2d Vector
Definition geo.h:14
Vector get_centroid(const std::vector< Vector > &points)
Definition geo.cpp:10
double y_displace
bool use_light_mode
double x_displace
Rigid-body transformation.
Definition geo.h:18