Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/project-tools-refactor' …
Browse files Browse the repository at this point in the history
…into feature/project-tools-refactor

# Conflicts:
#	README.md
  • Loading branch information
balazsdukai committed May 22, 2024
2 parents 1d22e2e + 745f94c commit 5805bf9
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 21 deletions.
49 changes: 47 additions & 2 deletions include/roofer/common/Raster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
// #include <cpl_conv.h>
// #include <ogr_spatialref.h>
namespace RasterTools {
enum alg {MIN,MAX};
enum alg {MIN,MAX,ZERO};
class Raster
{
public:
Expand All @@ -41,15 +41,53 @@ namespace RasterTools {
void operator=(const Raster& r);
Raster(){};
~Raster(){};
/**
* Prefills the raster array based on the specified method.
* @param[in] a The method to be used for prefilling the raster arrays: MIN, MAX or ZERO.
*/
void prefill_arrays(alg a);
bool add_point(double x, double y, double z, alg a);
/**
* Adds a value to the raster at the specified x and y coordinates.
* @param[in] x x coordinate of the point
* @param[in] y y coordinate of the point
* @param[in] val value to be added
* @return True if the value was added, false if the point is outside the raster.
*/
bool add_value(double x, double y, double val);
/**
* Checks if the point is within the raster.
* @param[in] x x coordinate of the point
* @param[in] y y coordinate of the point
* @return True if the point is within the raster, false otherwise.
*/
bool check_point(double x, double y);
void add_raster(double x, double y, double z, alg a);
//void add_raster(double x, double y, double z, alg a); // this seems unused. Should it be removed?

/**
* Gets the row number for the given x and y coordinates.
* @param[in] x x coordinate of the point
* @param[in] y y coordinate of the point
* @return The row number.
*/
size_t getRow(double x, double y) const;
/**
* Gets the col number for the given x and y coordinates.
* @param[in] x x coordinate of the point
* @param[in] y y coordinate of the point
* @return The col number.
*/
size_t getCol(double x, double y) const;
size_t getLinearCoord(double x, double y) const;
size_t getLinearCoord(size_t r, size_t c) const;
std::array<double,2> getColRowCoord(double x, double y) const;
/**
* Get the 3D point at the center of the input raster cell.
*
* @param[in] col the column of the cell
* @param[in] row the row of the cell
* @return the 3D point in the center of the cell
*/
point3d getPointFromRasterCoords(size_t col, size_t row) const;
double getNoDataVal() {return noDataVal_;};
double sample(double &x, double &y);
Expand Down Expand Up @@ -147,6 +185,13 @@ namespace RasterTools {
void avg(double &x, double &y, double &val);
void min(double &x, double &y, double &val);
void max(double &x, double &y, double &val);
/**
* Adds the input value to the value at the given coordinate of the raster.
* @param[in] val value to be added
* @param[in] x x coordinate
* @param[in] y y coordinate
*/
void add(double &x, double &y, double &val);
// void cnt(double &x, double &y);
// OGRSpatialReference oSRS;
};
Expand Down
18 changes: 17 additions & 1 deletion src/core/common/Raster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ namespace RasterTools {
void Raster::prefill_arrays(alg a){
if (a==MIN)
noDataVal_ = std::numeric_limits<float>::max();
else
else if (a==MAX)
noDataVal_ = -std::numeric_limits<float>::max();
else {
noDataVal_ = 0;
}

std::fill(vals_->begin(), vals_->end(), noDataVal_);
// std::fill(counts_->begin(), counts_->end(), 0);
Expand All @@ -76,6 +79,13 @@ namespace RasterTools {
}
return first;
}

bool Raster::add_value(double x, double y, double val)
{
bool first = (*vals_)[getLinearCoord(x,y)]==noDataVal_;
add(x,y,val);
return first;
}
bool Raster::check_point(double x, double y)
{
auto col = getCol(x,y);
Expand Down Expand Up @@ -105,6 +115,12 @@ namespace RasterTools {
if ((*vals_)[c]<val) (*vals_)[c] = val;
}

inline void Raster::add(double &x, double &y, double &val)
{
size_t c = getLinearCoord(x,y);
(*vals_)[c] = (*vals_)[c]+val;
}

// inline void Raster::cnt(double &x, double &y)
// {
// size_t c = getLinearCoord(x,y);
Expand Down
49 changes: 34 additions & 15 deletions src/extra/misc/PointcloudRasteriser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
#include <roofer/misc/PointcloudRasteriser.hpp>

namespace roofer {

void RasterisePointcloud(
PointCollection& pointcloud,
LinearRing& footprint,
ImageMap& image_bundle,
// RasterTools::Raster& heightfield,
float cellsize
) {
bool use_footprint = true;
//TODO: this is always true?
bool use_footprint = true;
Box box;
if (use_footprint) {
box = footprint.box();
Expand All @@ -29,19 +30,23 @@ namespace roofer {
RasterTools::Raster r_max(cellsize, boxmin[0], boxmax[0], boxmin[1], boxmax[1]);
r_max.prefill_arrays(RasterTools::MAX);

RasterTools::Raster r_min(r_max), r_fp(r_max);
RasterTools::Raster r_min(r_max), r_fp(r_max), r_grp(r_max), r_ground_points(r_max), r_non_ground_points(r_max);
r_min.prefill_arrays(RasterTools::MIN);
r_fp.prefill_arrays(RasterTools::MAX);
r_grp.prefill_arrays(RasterTools::ZERO);
r_ground_points.prefill_arrays(RasterTools::ZERO);
r_non_ground_points.prefill_arrays(RasterTools::ZERO);

std::vector<std::vector<float>> buckets(r_max.dimx_*r_max.dimy_);


if (use_footprint) {
auto exterior = build_grid(footprint);
std::vector<pGridSet> holes;
for (auto& hole : footprint.interior_rings()) {
holes.push_back(build_grid(hole));
}

for (size_t col = 0; col < r_fp.dimx_; ++col) {
for (size_t row = 0; row < r_fp.dimy_; ++row) {
auto p = r_fp.getPointFromRasterCoords(col, row);
Expand All @@ -64,9 +69,19 @@ namespace roofer {
delete exterior;
for (auto& hole: holes) delete hole;
}

for(auto& p : pointcloud) {

auto classification = pointcloud.attributes.get_if<int>("classification");
for(size_t pi=0; pi < pointcloud.size(); ++pi) {
auto& p = pointcloud[pi];
auto& c = (*classification)[pi];
if (r_max.check_point(p[0], p[1])) {
if (c == 2){
r_ground_points.add_value(p[0], p[1], 1);
}
else if (c == 6){
r_non_ground_points.add_value(p[0], p[1], 1);
}

r_max.add_point(p[0], p[1], p[2], RasterTools::MAX);
r_min.add_point(p[0], p[1], p[2], RasterTools::MIN);
buckets[ r_max.getLinearCoord(p[0],p[1]) ].push_back(p[2]);
Expand All @@ -76,28 +91,28 @@ namespace roofer {
// PointCollection grid_points;
// vec1f values;
// double nodata = r_max.getNoDataVal();
image_bundle["fp"] = Image{};
image_bundle["max"] = Image{};
image_bundle["min"] = Image{};
image_bundle["cnt"] = Image{};
image_bundle["med"] = Image{};
image_bundle["avg"] = Image{};
image_bundle["var"] = Image{};

image_bundle["max"].dim_x = r_max.dimx_;
image_bundle["max"].dim_y = r_max.dimy_;
image_bundle["max"].min_x = r_max.minx_;
image_bundle["max"].min_y = r_max.miny_;
image_bundle["max"].cellsize = r_max.cellSize_;
image_bundle["max"].nodataval = r_max.noDataVal_;
image_bundle["max"].array = *r_max.vals_;

image_bundle["min"] = image_bundle["max"];
image_bundle["min"].nodataval = r_min.noDataVal_;
image_bundle["min"].array = *r_min.vals_;
image_bundle["fp"] = image_bundle["max"];
image_bundle["fp"].array = *r_fp.vals_;
image_bundle["cnt"] = image_bundle["max"], image_bundle["med"] = image_bundle["max"], image_bundle["avg"] = image_bundle["max"], image_bundle["var"] = image_bundle["max"];

image_bundle["cnt"] = image_bundle["max"], image_bundle["med"] = image_bundle["max"], image_bundle["avg"] = image_bundle["max"], image_bundle["var"] = image_bundle["max"], image_bundle["grp"] = image_bundle["max"];
image_bundle["gp"] = image_bundle["max"];
image_bundle["gp"].nodataval = r_ground_points.noDataVal_;
image_bundle["gp"].array = *r_ground_points.vals_;
image_bundle["ngp"] = image_bundle["max"];
image_bundle["ngp"].nodataval = r_non_ground_points.noDataVal_;
image_bundle["ngp"].array = *r_non_ground_points.vals_;

for(size_t row=0; row<r_max.dimy_ ; ++row) {
for(size_t col=0; col<r_max.dimx_ ; ++col) {
// auto p = r_max.getPointFromRasterCoords(col,row);
Expand All @@ -112,11 +127,13 @@ namespace roofer {
image_bundle["med"].array[lc] = image_bundle["med"].nodataval;
image_bundle["avg"].array[lc] = image_bundle["avg"].nodataval;
image_bundle["var"].array[lc] = image_bundle["var"].nodataval;
image_bundle["grp"].array[lc] = image_bundle["grp"].nodataval;
} else {
std::sort(buck.begin(), buck.end());
image_bundle["cnt"].array[lc] = buck.size();
image_bundle["med"].array[lc] = buck[ buck.size()/2 ];
image_bundle["avg"].array[lc] = std::accumulate(buck.begin(), buck.end(), 0) / buck.size();
image_bundle["grp"].array[lc] = abs(image_bundle["gp"].array[lc]-image_bundle["ngp"].array[lc])/(image_bundle["gp"].array[lc]+image_bundle["ngp"].array[lc]);
int ssum = 0;
for(auto& z : buck) {
ssum += std::pow(z-image_bundle["avg"].array[lc], 2);
Expand All @@ -125,6 +142,8 @@ namespace roofer {
}
}
}
image_bundle.erase("gp");
image_bundle.erase("ngp");
}


Expand Down
8 changes: 5 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
set(CONFIG_DIR "${CMAKE_CURRENT_SOURCE_DIR}/config")
set(DATA_URL_ROOT "https://data.3dgi.xyz/roofer-test-data")
set(DATA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/data")
set(TEST_ENVIRONMENT
"PROJ_DATA=${CMAKE_BINARY_DIR}/vcpkg_installed/${VCPKG_TARGET_TRIPLET}/share/proj;GDAL_DATA=${CMAKE_BINARY_DIR}/vcpkg_installed/${VCPKG_TARGET_TRIPLET}/share/gdal"
)
if (DEFINED VCPKG_TOOLCHAIN)
set(TEST_ENVIRONMENT
"PROJ_DATA=${CMAKE_BINARY_DIR}/vcpkg_installed/${VCPKG_TARGET_TRIPLET}/share/proj;GDAL_DATA=${CMAKE_BINARY_DIR}/vcpkg_installed/${VCPKG_TARGET_TRIPLET}/share/gdal"
)
endif()

FetchContent_Declare(
wippolder
Expand Down

0 comments on commit 5805bf9

Please sign in to comment.