Skip to content

Commit

Permalink
add rgg generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mschimek committed Nov 25, 2024
1 parent 844dc0f commit 1d29d53
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 44 deletions.
11 changes: 11 additions & 0 deletions kagen/generators/geometric/rgg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,15 @@ std::unique_ptr<Generator>
RGG3DFactory::Create(const PGeneratorConfig& config, const PEID rank, const PEID size) const {
return std::make_unique<RGG3D>(config, rank, size);
}
void RGG::PushWeightIfRequested(
const EdgeWeightConfig& config, const double& squared_distance, const double& squared_radius) {
const bool use_euclidean_edge_weights = config.generator_type == EdgeWeightGeneratorType::EUCLIDEAN_DISTANCE;
if (!use_euclidean_edge_weights) {
return;
}
const auto normalized_euclidean_distance = std::sqrt(squared_distance / squared_radius);
const SInt weight_range = config.weight_range_end - config.weight_range_begin;
const SSInt weight = config.weight_range_begin + static_cast<SSInt>(weight_range * normalized_euclidean_distance);
PushEdgeWeight(weight);
};
} // namespace kagen
8 changes: 8 additions & 0 deletions kagen/generators/geometric/rgg.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "kagen/generators/generator.h"

#include <cmath>

namespace kagen {
class RGG2DFactory : public GeneratorFactory {
public:
Expand All @@ -16,4 +18,10 @@ class RGG3DFactory : public GeneratorFactory {

std::unique_ptr<Generator> Create(const PGeneratorConfig& config, PEID rank, PEID size) const final;
};

class RGG : public virtual Generator {
protected:
void
PushWeightIfRequested(const EdgeWeightConfig& config, const double& squared_distance, const double& squared_radius);
};
} // namespace kagen
22 changes: 4 additions & 18 deletions kagen/generators/geometric/rgg/rgg_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,6 @@ void RGG2D::GenerateGridEdges(
// Generate edges
// Same cell

// push euclidean distance scaled to the requested weight range
auto PushWeightIfRequested = [&](const auto& squared_distance, const auto& squared_radius) {
const auto& config = config_.edge_weights;
const bool use_euclidean_edge_weights = config.generator_type == EdgeWeightGeneratorType::EUCLIDEAN_DISTANCE;
if (!use_euclidean_edge_weights) {
return;
}
const auto normalized_euclidean_distance = std::sqrt(squared_distance / squared_radius);
const SInt weight_range = config.weight_range_end - config.weight_range_begin;
const SSInt weight =
config.weight_range_begin + static_cast<SSInt>(weight_range * normalized_euclidean_distance);
PushEdgeWeight(weight);
};

if (first_chunk_id == second_chunk_id && first_cell_id == second_cell_id) {
for (SInt i = 0; i < vertices_first.size(); ++i) {
const Vertex& v1 = vertices_first[i];
Expand All @@ -122,9 +108,9 @@ void RGG2D::GenerateGridEdges(
const auto squared_distance = PGGeometry<LPFloat>::SquaredEuclideanDistance(v1, v2);
if (squared_distance <= target_r_) {
PushEdge(std::get<2>(v1), std::get<2>(v2));
PushWeightIfRequested(squared_distance, target_r_);
PushWeightIfRequested(config_.edge_weights, squared_distance, target_r_);
PushEdge(std::get<2>(v2), std::get<2>(v1));
PushWeightIfRequested(squared_distance, target_r_);
PushWeightIfRequested(config_.edge_weights, squared_distance, target_r_);
}
}
}
Expand All @@ -136,10 +122,10 @@ void RGG2D::GenerateGridEdges(
const auto squared_distance = PGGeometry<LPFloat>::SquaredEuclideanDistance(v1, v2);
if (squared_distance <= target_r_) {
PushEdge(std::get<2>(v1), std::get<2>(v2));
PushWeightIfRequested(squared_distance, target_r_);
PushWeightIfRequested(config_.edge_weights, squared_distance, target_r_);
if (IsLocalChunk(second_chunk_id)) {
PushEdge(std::get<2>(v2), std::get<2>(v1));
PushWeightIfRequested(squared_distance, target_r_);
PushWeightIfRequested(config_.edge_weights, squared_distance, target_r_);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion kagen/generators/geometric/rgg/rgg_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#pragma once

#include "kagen/generators/geometric/geometric_2d.h"
#include "kagen/generators/geometric/rgg.h"

namespace kagen {
class RGG2D : public Geometric2D {
class RGG2D : public Geometric2D, RGG {
public:
RGG2D(const PGeneratorConfig& config, PEID rank, PEID size);

Expand Down
36 changes: 12 additions & 24 deletions kagen/generators/geometric/rgg/rgg_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,51 +109,39 @@ void RGG3D::GenerateGridEdges(
// Same cell

// push euclidean distance scaled to the requested weight range
auto PushWeightIfRequested = [&](const auto& squared_distance, const auto& squared_radius) {
const auto& config = config_.edge_weights;
const bool use_euclidean_edge_weights = config.generator_type == EdgeWeightGeneratorType::EUCLIDEAN_DISTANCE;
if (!use_euclidean_edge_weights) {
return;
}
const auto normalized_euclidean_distance = std::sqrt(squared_distance / squared_radius);
const SInt weight_range = config.weight_range_end - config.weight_range_begin;
const SSInt weight =
config.weight_range_begin + static_cast<SSInt>(weight_range * normalized_euclidean_distance);
PushEdgeWeight(weight);
};
if (first_chunk_id == second_chunk_id && first_cell_id == second_cell_id) {
for (SInt i = 0; i < vertices_first.size(); ++i) {
const Vertex& v1 = vertices_first[i];
for (SInt j = i + 1; j < vertices_second.size(); ++j) {
const Vertex& v2 = vertices_second[j];
// Euclidean distance
LPFloat x = std::get<0>(v1) - std::get<0>(v2);
LPFloat y = std::get<1>(v1) - std::get<1>(v2);
LPFloat z = std::get<2>(v1) - std::get<2>(v2);
LPFloat x = std::get<0>(v1) - std::get<0>(v2);
LPFloat y = std::get<1>(v1) - std::get<1>(v2);
LPFloat z = std::get<2>(v1) - std::get<2>(v2);
const auto squared_distance = x * x + y * y + z * z;
if (squared_distance <= target_r_) {
PushEdge(std::get<3>(v1), std::get<3>(v2));
PushWeightIfRequested(squared_distance, target_r_);
PushWeightIfRequested(config_.edge_weights, squared_distance, target_r_);
PushEdge(std::get<3>(v2), std::get<3>(v1));
PushWeightIfRequested(squared_distance, target_r_);
PushWeightIfRequested(config_.edge_weights, squared_distance, target_r_);
}
}
}
} else {
for (SInt i = 0; i < vertices_first.size(); ++i) {
const Vertex& v1 = vertices_first[i];
for (SInt j = 0; j < vertices_second.size(); ++j) {
const Vertex& v2 = vertices_second[j];
LPFloat x = std::get<0>(v1) - std::get<0>(v2);
LPFloat y = std::get<1>(v1) - std::get<1>(v2);
LPFloat z = std::get<2>(v1) - std::get<2>(v2);
const auto squared_distance = x * x + y * y + z * z;
const Vertex& v2 = vertices_second[j];
LPFloat x = std::get<0>(v1) - std::get<0>(v2);
LPFloat y = std::get<1>(v1) - std::get<1>(v2);
LPFloat z = std::get<2>(v1) - std::get<2>(v2);
const auto squared_distance = x * x + y * y + z * z;
if (squared_distance <= target_r_) {
PushEdge(std::get<3>(v1), std::get<3>(v2));
PushWeightIfRequested(squared_distance, target_r_);
PushWeightIfRequested(config_.edge_weights, squared_distance, target_r_);
if (IsLocalChunk(second_chunk_id)) {
PushEdge(std::get<3>(v2), std::get<3>(v1));
PushWeightIfRequested(squared_distance, target_r_);
PushWeightIfRequested(config_.edge_weights, squared_distance, target_r_);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion kagen/generators/geometric/rgg/rgg_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#pragma once

#include "kagen/generators/geometric/geometric_3d.h"
#include "kagen/generators/geometric/rgg.h"

namespace kagen {
class RGG3D : public Geometric3D {
class RGG3D : public Geometric3D, RGG {
public:
RGG3D(const PGeneratorConfig& config, const PEID rank, const PEID size);

Expand Down

0 comments on commit 1d29d53

Please sign in to comment.