Skip to content

Commit

Permalink
Add diffuse and specular default particles (#61)
Browse files Browse the repository at this point in the history
* Add default diffuse particle class

* Fix missing dim in diff reflection

* fix missing dim in diffuse particle class

* Add specular particle and particle tests

* Bump version
  • Loading branch information
tobre1 authored Oct 29, 2024
1 parent ccf12cc commit 1fe0671
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
project(
ViennaRay
LANGUAGES CXX
VERSION 3.0.1)
VERSION 3.1.0)

# --------------------------------------------------------------------------------------------------------
# Library switches
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ We recommend using [CPM.cmake](https://github.com/cpm-cmake/CPM.cmake) to consum
* Installation with CPM

```cmake
CPMAddPackage("gh:viennatools/viennaray@3.0.1")
CPMAddPackage("gh:viennatools/viennaray@3.1.0")
```

* With a local installation
Expand Down
81 changes: 81 additions & 0 deletions include/viennaray/rayParticle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,85 @@ class TestParticle : public Particle<TestParticle<NumericType>, NumericType> {
void logData(DataLog<NumericType> &log) override final {}
};

template <typename NumericType, int D>
class DiffuseParticle
: public Particle<DiffuseParticle<NumericType, D>, NumericType> {
const NumericType stickingProbability_;
const std::string dataLabel_;

public:
DiffuseParticle(NumericType stickingProbability, std::string dataLabel)
: stickingProbability_(stickingProbability), dataLabel_(dataLabel) {}

std::pair<NumericType, Vec3D<NumericType>>
surfaceReflection(NumericType rayWeight, const Vec3D<NumericType> &rayDir,
const Vec3D<NumericType> &geomNormal,
const unsigned int primID, const int materialId,
const TracingData<NumericType> *globalData,
RNG &rngState) override final {
auto direction = ReflectionDiffuse<NumericType, D>(geomNormal, rngState);
return std::pair<NumericType, Vec3D<NumericType>>{stickingProbability_,
direction};
}

void surfaceCollision(NumericType rayWeight, const Vec3D<NumericType> &rayDir,
const Vec3D<NumericType> &geomNormal,
const unsigned int primID, const int materialId,
TracingData<NumericType> &localData,
const TracingData<NumericType> *globalData,
RNG &rngState) override final {
// collect data for this hit
localData.getVectorData(0)[primID] += rayWeight;
}

NumericType getSourceDistributionPower() const override final { return 1.; }

std::vector<std::string> getLocalDataLabels() const override final {
return {dataLabel_};
}
};

template <typename NumericType, int D>
class SpecularParticle
: public Particle<SpecularParticle<NumericType, D>, NumericType> {
const NumericType stickingProbability_;
const NumericType sourcePower_;
const std::string dataLabel_;

public:
SpecularParticle(NumericType stickingProbability, NumericType sourcePower,
std::string dataLabel)
: stickingProbability_(stickingProbability), sourcePower_(sourcePower),
dataLabel_(dataLabel) {}

std::pair<NumericType, Vec3D<NumericType>>
surfaceReflection(NumericType rayWeight, const Vec3D<NumericType> &rayDir,
const Vec3D<NumericType> &geomNormal,
const unsigned int primID, const int materialId,
const TracingData<NumericType> *globalData,
RNG &rngState) override final {
auto direction = ReflectionSpecular<NumericType, D>(rayDir, geomNormal);
return std::pair<NumericType, Vec3D<NumericType>>{stickingProbability_,
direction};
}

void surfaceCollision(NumericType rayWeight, const Vec3D<NumericType> &rayDir,
const Vec3D<NumericType> &geomNormal,
const unsigned int primID, const int materialId,
TracingData<NumericType> &localData,
const TracingData<NumericType> *globalData,
RNG &rngState) override final {
// collect data for this hit
localData.getVectorData(0)[primID] += rayWeight;
}

NumericType getSourceDistributionPower() const override final {
return sourcePower_;
}

std::vector<std::string> getLocalDataLabels() const override final {
return {dataLabel_};
}
};

} // namespace viennaray
7 changes: 7 additions & 0 deletions tests/particle/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
project(particle LANGUAGES CXX)

add_executable(${PROJECT_NAME} "${PROJECT_NAME}.cpp")
target_link_libraries(${PROJECT_NAME} PRIVATE ViennaRay)

add_dependencies(ViennaRay_Tests ${PROJECT_NAME})
add_test(NAME ${PROJECT_NAME} COMMAND $<TARGET_FILE:${PROJECT_NAME}>)
45 changes: 45 additions & 0 deletions tests/particle/particle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <rayParticle.hpp>
#include <rayTrace.hpp>

#include <vcTestAsserts.hpp>

namespace viennacore {

using namespace viennaray;

template <class NumericType, int D> void RunTest() {

{
auto particle =
std::make_unique<DiffuseParticle<NumericType, D>>(1., "test");

NumericType sourcePower = particle->getSourceDistributionPower();
VC_TEST_ASSERT(sourcePower == 1.);

auto labels = particle->getLocalDataLabels();
VC_TEST_ASSERT(labels.size() == 1);
VC_TEST_ASSERT(labels[0] == "test");

Trace<NumericType, D> tracer;
tracer.setParticleType(particle);
}

{
auto particle =
std::make_unique<SpecularParticle<NumericType, D>>(1., 100., "test");

NumericType sourcePower = particle->getSourceDistributionPower();
VC_TEST_ASSERT(sourcePower == 100.);

auto labels = particle->getLocalDataLabels();
VC_TEST_ASSERT(labels.size() == 1);
VC_TEST_ASSERT(labels[0] == "test");

Trace<NumericType, D> tracer;
tracer.setParticleType(particle);
}
}

} // namespace viennacore

int main() { VC_RUN_ALL_TESTS }

0 comments on commit 1fe0671

Please sign in to comment.