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 "util/logger.h"
10#include "util/keyboard.h"
11#include "lidar_view.h"
12#include "geo/midpoint.h"
13#include "view_config.h"
14
15#define CIRCLE_RADIUS 3
16
17LidarView::LidarView(std::vector<icp::Vector> source,
18 std::vector<icp::Vector> destination, const std::string method,
19 const icp::ICP::Config& config)
20 : source(source), destination(destination), keyboard(false), iterations{} {
21 icp = icp::ICP::from_method(method, config);
22 icp->begin(source, destination, icp::RBTransform());
23}
24
26 icp.release();
27}
28
29void LidarView::on_event(const SDL_Event& event) {
30 bool space_before = keyboard.query(SDLK_SPACE);
31 bool d_before = keyboard.query(SDLK_d);
32 keyboard.update(event);
33 bool space_after = keyboard.query(SDLK_SPACE);
34 bool d_after = keyboard.query(SDLK_d);
35
36 if (!space_before && space_after) {
37 is_iterating = !is_iterating;
38 }
39 if (!d_before && d_after) {
40 std::cerr << "DEBUG PRINT:\n";
41 std::cerr << "icp->current_transform() = "
42 << icp->current_transform().to_string() << '\n';
43 std::cerr << "icp->calculate_cost() = " << icp->calculate_cost()
44 << '\n';
45 std::cerr << "iterations = " << iterations << '\n';
46 }
47}
48
49void LidarView::draw(SDL_Renderer* renderer, const SDL_Rect* frame __unused,
50 double dtime __unused) {
52 SDL_SetRenderDrawColor(renderer, 100, 100, 100, 255);
53 } else {
54 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
55 }
56 SDL_RenderClear(renderer);
57
58 SDL_SetRenderDrawColor(renderer, 0, 0, 255, SDL_ALPHA_OPAQUE);
59 for (const icp::Vector& point: destination) {
60 SDL_DrawCircle(renderer, point[0] + view_config::x_displace,
62 }
63
64 SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
65 for (const icp::Vector& point: source) {
66 icp::Vector result = icp->current_transform().apply_to(point);
67 SDL_DrawCircle(renderer, result[0] + view_config::x_displace,
69 }
70
71 icp::Vector a_cm =
72 icp->current_transform().apply_to(icp::get_centroid(source));
73 SDL_SetRenderDrawColor(renderer, 255, 0, 0, 10);
74 SDL_DrawCircle(renderer, a_cm.x() + view_config::x_displace,
75 a_cm.y() + view_config::y_displace, 20);
76
77 icp::Vector b_cm = icp::get_centroid(destination);
78 SDL_SetRenderDrawColor(renderer, 0, 0, 255, 10);
79 SDL_DrawCircle(renderer, b_cm.x() + view_config::x_displace,
80 b_cm.y() + view_config::y_displace, 20);
81
82 if (is_iterating) {
83 icp->iterate();
84 iterations++;
85 }
86}
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:97
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:112
#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