Skip to content

Commit

Permalink
feat: Added dbscan shared library
Browse files Browse the repository at this point in the history
- This commit has removed the include/src folder with the dbscan headers
- This commit has created a dbscan shared library
- This commit has introduced the run.sh script in docker
- This commit has introduced the .gitignore file for C++ projects
- This commit has introduced the cmake folder with CMake modules
- This commit has updated the include statement for the new dbscan lib
  • Loading branch information
Daniel Tobon committed Jun 22, 2022
1 parent cd470a5 commit 883f249
Show file tree
Hide file tree
Showing 14 changed files with 2,587 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

# Folders
build/
19 changes: 19 additions & 0 deletions cmake/color.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
if(NOT WIN32)
string(ASCII 27 Esc)
set(ColourReset "${Esc}[m")
set(ColourBold "${Esc}[1m")
set(Red "${Esc}[31m")
set(Green "${Esc}[32m")
set(Yellow "${Esc}[33m")
set(Blue "${Esc}[34m")
set(Magenta "${Esc}[35m")
set(Cyan "${Esc}[36m")
set(White "${Esc}[37m")
set(BoldRed "${Esc}[1;31m")
set(BoldGreen "${Esc}[1;32m")
set(BoldYellow "${Esc}[1;33m")
set(BoldBlue "${Esc}[1;34m")
set(BoldMagenta "${Esc}[1;35m")
set(BoldCyan "${Esc}[1;36m")
set(BoldWhite "${Esc}[1;37m")
endif()
49 changes: 49 additions & 0 deletions dbscanlib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# #############################################################################
# LIBRARY NAME
# #############################################################################
set(LIBRARY_NAME dbscan)

# #############################################################################
# COMPILE LIBRARY
# #############################################################################
add_library(${LIBRARY_NAME} SHARED
src/cluster.cpp
src/dbScan.cpp
src/keypointcluster.cpp
src/OctreeGenerator.cpp
)

# #############################################################################
# LIBRARY PROPERTIES
# #############################################################################
set_target_properties(${LIBRARY_NAME} PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION 1
PUBLIC_HEADER ${CMAKE_CURRENT_LIST_DIR}/include/dbscan/dbScan.h
)

# #############################################################################
# LIBRARY HEADERS
# #############################################################################
target_include_directories(${LIBRARY_NAME} PUBLIC
${CMAKE_CURRENT_LIST_DIR}/include
)

# #############################################################################
# LIBRARY DEPENDENCIES
# #############################################################################
target_link_libraries(${LIBRARY_NAME} ${PCL_LIBRARIES})

# #############################################################################
# COMPILATION FLAGS
# #############################################################################
target_compile_features(${LIBRARY_NAME} PUBLIC cxx_std_17)
target_compile_options(${LIBRARY_NAME} PRIVATE -Wno-cpp -Wall -Wextra)

# #############################################################################
# INSTALL DIRECTORY
# #############################################################################
install(TARGETS ${LIBRARY_NAME}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
83 changes: 83 additions & 0 deletions dbscanlib/include/dbscan/HTRBasicDataStructures.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
*@file HTRBasicDataStructures.h
*Data structures that do not depend on external classes.
*
*/
#include <pcl/pcl_macros.h>
#include <pcl/point_types.h>
#define PCL_NO_PRECOMPILE
#pragma once
#ifndef HTR_BASIC_DATA_STRUCTURES_H
#define HTR_BASIC_DATA_STRUCTURES_H

/// Modified pcl point to include an id.
namespace pcl {

class mod_pointXYZ : public PointXYZRGB {
public:
mod_pointXYZ() {
x = y = z = 0;
id = 0;
}

mod_pointXYZ(float x, float y, float z) : PointXYZRGB(x, y, z) { id = 0; }
/*
mod_pointXYZ(unsigned int r, unsigned int g, unsigned int b) : PointXYZRGB(r,g,b){
id = 0;
}
*/
int id;
};
} // namespace pcl

namespace htr {
struct Index2D {
int x;
int y;
// Index2D():x(0),y(0){}
};

struct Point3D {
float x;
float y;
float z;

unsigned int r;
unsigned int g;
unsigned int b;

void initRandom() {
x = (rand() % 40);
y = (rand() % 40);
z = (rand() % 40);
}
};

struct FlaggedPoint3D {
Point3D point;
int flag;
};

struct DepthPixel {
int x;
int y;
float z;
};

struct LabeledPoint {
Point3D point;
int label;
};

struct CubeBoundary {
Point3D start;
Point3D end;
};

struct LinearBoundary {
float start;
float end;
};
} // namespace htr

#endif
142 changes: 142 additions & 0 deletions dbscanlib/include/dbscan/OctreeGenerator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/**
*@class OctreeGenerator
*Creates an octree from the point cloud data provided.
*
*/

#define _CRT_SECURE_NO_WARNINGS

#ifndef OCTREE_GENERATOR_H
#define OCTREE_GENERATOR_H

//#include <pcl/point_cloud.h>
#include <pcl/octree/octree.h>
// #include <pcl/octree/octree_impl.h>

#include <pcl/common/centroid.h>
#include <pcl/common/concatenate.h>
#include <pcl/common/copy_point.h>
#include <pcl/common/io.h>
#include <pcl/pcl_macros.h>
#include <pcl/point_types.h>

#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <locale> // std::locale, std::isdigit
#include <pcl/impl/point_types.hpp>
#include <string>
#include <vector>

#include "HTRBasicDataStructures.h"

namespace htr {

class OctreeGenerator {
public:
typedef pcl::PointCloud<pcl::mod_pointXYZ> CloudXYZ;
typedef pcl::octree::OctreePointCloudSearch<pcl::mod_pointXYZ> OctreeXYZSearch;
typedef pcl::octree::OctreePointCloudSearch<pcl::mod_pointXYZ>::LeafNode LeafNode;
typedef pcl::octree::OctreePointCloudSearch<pcl::mod_pointXYZ>::LeafNodeIterator LeafNodeIterator;
typedef htr::Point3D Point3D;

struct Voxel {
Point3D position;
float size;
};

OctreeGenerator();
~OctreeGenerator();

/// This is needed to remove the warning about alignment: object allocated on the heap may not be
/// aligned 16
/// Solution specific to microsoft.
#ifdef _MSC_VER
void *operator new(size_t size) {
void *p = _aligned_malloc(size, 16);
if (!p) throw std::bad_alloc();
return p;
}

void operator delete(void *p) {
OctreeGenerator *ptr = static_cast<OctreeGenerator *>(p);
_aligned_free(p);
}
#endif
inline CloudXYZ::Ptr getCloud() { return cloud; }
inline pcl::mod_pointXYZ getCloudCentroid() { return cloudCentroid; }
inline std::vector<Voxel> &getVoxels() { return octreeVoxels; }
inline std::vector<Point3D> &getCentroids() { return octreeCentroids; }
inline OctreeXYZSearch::Ptr getOctree() { return octree_p; }

void initRandomCloud(const float width, const float height, const float depth, const int numOfPoints);

template <typename T>
void initCloudFromVector(const std::vector<T> &points,
const pcl::PointCloud<pcl::PointXYZRGB>::Ptr &input_cloud);

void initOctree(const int resolution);

void extractPointsAtLevel(const int depth);
void stepExtractionLevel(const int step);

private:
unsigned int currentExtractionLevel;
pcl::PointCloud<pcl::mod_pointXYZ>::Ptr cloud;
OctreeXYZSearch::Ptr octree_p;

pcl::mod_pointXYZ cloudCentroid;

std::vector<Voxel> octreeVoxels;
std::vector<Point3D> octreeCentroids;

void calculateCloudCentroid();
};

/// Initializes pcl's cloud data structure from a vector of any type containing x, y, and z member
/// variables.
///@param[in] points The input data vector.
template <typename T>
void OctreeGenerator::initCloudFromVector(const std::vector<T> &points,
const pcl::PointCloud<pcl::PointXYZRGB>::Ptr &input_cloud) {
// Note: Width and Height are only used to store the cloud as an image.
// Source width and height can be used instead of a linear representation.
// pcl::copyPointCloud<pcl::PointXYZRGB, pcl::PointXYZRGB>(*input_cloud, *cloud);
// pcl::PointCloud<pcl::PointXYZRGB>::Ptr new_cloud(new pcl::PointCloud<pcl::PointXYZRGB>());
// pcl::copyPointCloud<pcl::mod_pointXYZ>(*input_cloud, cloud);
/*
for (const pcl::mod_pointXYZ &point : input_cloud) {
point.
cloud->points.push_back(point);
}
*/
// cloud_xyzrgb.reset(new pcl::PointCloud<pcl::PointXYZRGB>());
// pcl::copyPointCloud(*input_cloud, *cloud_xyzrgb);

// cloud_xyzrgb->width = input_cloud->points.size();
// cloud_xyzrgb->height = 1;

cloud->width = input_cloud->points.size();
cloud->height = 1;

cloud->points.resize(cloud->width * cloud->height);

for (size_t i = 0; i < cloud->points.size(); ++i) {
cloud->points[i].x = input_cloud->points[i].x;
cloud->points[i].y = input_cloud->points[i].y;
cloud->points[i].z = input_cloud->points[i].z;

uint8_t r_, g_, b_;
r_ = uint8_t(input_cloud->points[i].r);
g_ = uint8_t(input_cloud->points[i].g);
b_ = uint8_t(input_cloud->points[i].b);

uint32_t rgb_ = ((uint32_t)r_ << 16 | (uint32_t)g_ << 8 | (uint32_t)b_);
cloud->points[i].rgb = *reinterpret_cast<float *>(&rgb_);
}
calculateCloudCentroid();
}
} // namespace htr

#endif
30 changes: 30 additions & 0 deletions dbscanlib/include/dbscan/cluster.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
*@file cluster.h
*Cluster for 3d points.
*/

#ifndef CLUSTER
#define CLUSTER

#include "HTRBasicDataStructures.h"
#include "OctreeGenerator.h"

namespace dbScanSpace {
class cluster {
public:
std::vector<pcl::mod_pointXYZ> clusterPoints;
std::vector<htr::Point3D> clusterPoints3D;

pcl::mod_pointXYZ centroid;
htr::Point3D centroid3D;
bool visited;

cluster();
void calculateCentroid();
void toPoint3D();

private:
};
} // namespace dbScanSpace

#endif // CLUSTER
Loading

0 comments on commit 883f249

Please sign in to comment.