2015-09-15 09:13:05 +00:00
|
|
|
//============================================================================
|
|
|
|
// Copyright (c) Kitware, Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
// See LICENSE.txt for details.
|
2019-04-15 23:24:21 +00:00
|
|
|
//
|
2015-09-15 09:13:05 +00:00
|
|
|
// This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
// PURPOSE. See the above copyright notice for more information.
|
|
|
|
//============================================================================
|
2015-09-14 07:27:32 +00:00
|
|
|
#ifndef VTKM_KERNEL_GAUSSIAN_H
|
|
|
|
#define VTKM_KERNEL_GAUSSIAN_H
|
|
|
|
|
2019-03-28 17:29:24 +00:00
|
|
|
#include <vtkm/worklet/splatkernels/KernelBase.h>
|
2015-09-14 07:27:32 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Gaussian kernel.
|
2018-01-30 00:24:31 +00:00
|
|
|
// Compact support is achieved by truncating the kernel beyond the cutoff radius
|
2015-09-14 07:27:32 +00:00
|
|
|
// This implementation uses a factor of 5 between smoothing length and cutoff
|
|
|
|
//
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace worklet
|
|
|
|
{
|
|
|
|
namespace splatkernels
|
|
|
|
{
|
2015-09-14 07:27:32 +00:00
|
|
|
|
|
|
|
template <int Dimensions>
|
2017-05-18 14:29:41 +00:00
|
|
|
struct Gaussian : public KernelBase<Gaussian<Dimensions>>
|
2015-09-14 07:27:32 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
// Constructor
|
|
|
|
// Calculate coefficients used repeatedly when evaluating the kernel
|
|
|
|
// value or gradient
|
|
|
|
VTKM_EXEC_CONT
|
|
|
|
Gaussian(double smoothingLength)
|
|
|
|
: KernelBase<Gaussian<Dimensions>>(smoothingLength)
|
|
|
|
{
|
|
|
|
Hinverse_ = 1.0 / smoothingLength;
|
|
|
|
Hinverse2_ = Hinverse_ * Hinverse_;
|
|
|
|
maxRadius_ = 5.0 * smoothingLength;
|
|
|
|
maxRadius2_ = maxRadius_ * maxRadius_;
|
|
|
|
//
|
2018-10-18 13:39:40 +00:00
|
|
|
norm_ = 1.0 / vtkm::Pow(M_PI, static_cast<double>(Dimensions) / 2.0);
|
2017-05-18 14:29:41 +00:00
|
|
|
scale_W_ = norm_ * PowerExpansion<Dimensions>(Hinverse_);
|
|
|
|
scale_GradW_ = -2.0 * PowerExpansion<Dimensions + 1>(Hinverse_) / norm_;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
// return the multiplier between smoothing length and max cutoff distance
|
2017-12-28 21:41:13 +00:00
|
|
|
VTKM_EXEC_CONT
|
2018-02-22 16:34:08 +00:00
|
|
|
constexpr double getDilationFactor() const { return 5.0; }
|
2017-05-18 14:29:41 +00:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
// compute w(h) for the given distance
|
|
|
|
VTKM_EXEC_CONT
|
|
|
|
double w(double distance) const
|
|
|
|
{
|
|
|
|
if (distance < maxDistance())
|
2015-09-14 07:27:32 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
// compute r/h
|
|
|
|
double normedDist = distance * Hinverse_;
|
|
|
|
// compute w(h)
|
2018-10-18 13:39:40 +00:00
|
|
|
return scale_W_ * vtkm::Exp(-normedDist * normedDist);
|
2015-09-14 07:27:32 +00:00
|
|
|
}
|
2017-05-18 14:29:41 +00:00
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
// compute w(h) for the given squared distance
|
|
|
|
VTKM_EXEC_CONT
|
|
|
|
double w2(double distance2) const
|
|
|
|
{
|
|
|
|
if (distance2 < maxSquaredDistance())
|
2015-09-14 07:27:32 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
// compute (r/h)^2
|
|
|
|
double normedDist = distance2 * Hinverse2_;
|
|
|
|
// compute w(h)
|
2018-10-18 13:39:40 +00:00
|
|
|
return scale_W_ * vtkm::Exp(-normedDist);
|
2015-09-14 07:27:32 +00:00
|
|
|
}
|
2017-05-18 14:29:41 +00:00
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
// compute w(h) for a variable h kernel
|
|
|
|
VTKM_EXEC_CONT
|
|
|
|
double w(double h, double distance) const
|
|
|
|
{
|
|
|
|
if (distance < maxDistance(h))
|
2015-09-14 07:27:32 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
double Hinverse = 1.0 / h;
|
|
|
|
double scale_W = norm_ * PowerExpansion<Dimensions>(Hinverse);
|
|
|
|
double Q = distance * Hinverse;
|
2015-09-14 07:27:32 +00:00
|
|
|
|
2018-10-18 13:39:40 +00:00
|
|
|
return scale_W * vtkm::Exp(-Q * Q);
|
2015-09-14 07:27:32 +00:00
|
|
|
}
|
2017-05-18 14:29:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
// compute w(h) for a variable h kernel using distance squared
|
|
|
|
VTKM_EXEC_CONT
|
|
|
|
double w2(double h, double distance2) const
|
|
|
|
{
|
|
|
|
if (distance2 < maxSquaredDistance(h))
|
2015-09-14 07:27:32 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
double Hinverse = 1.0 / h;
|
|
|
|
double scale_W = norm_ * PowerExpansion<Dimensions>(Hinverse);
|
|
|
|
double Q = distance2 * Hinverse * Hinverse;
|
2015-09-14 07:27:32 +00:00
|
|
|
|
2018-10-18 13:39:40 +00:00
|
|
|
return scale_W * vtkm::Exp(-Q);
|
2015-09-14 07:27:32 +00:00
|
|
|
}
|
2017-05-18 14:29:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
// Calculates the kernel derivative for a distance {x,y,z} vector
|
|
|
|
// from the centre
|
|
|
|
VTKM_EXEC_CONT
|
|
|
|
vector_type gradW(double distance, const vector_type& pos) const
|
|
|
|
{
|
|
|
|
double Q = distance * Hinverse_;
|
|
|
|
if (Q != 0.0)
|
2015-09-14 07:27:32 +00:00
|
|
|
{
|
2018-10-18 13:39:40 +00:00
|
|
|
return scale_GradW_ * vtkm::Exp(-Q * Q) * pos;
|
2015-09-14 07:27:32 +00:00
|
|
|
}
|
2017-05-18 14:29:41 +00:00
|
|
|
else
|
2015-09-14 07:27:32 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
return vector_type(0.0);
|
2015-09-14 07:27:32 +00:00
|
|
|
}
|
2017-05-18 14:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
// Calculates the kernel derivative for a distance {x,y,z} vector
|
|
|
|
// from the centre using a variable h
|
|
|
|
VTKM_EXEC_CONT
|
|
|
|
vector_type gradW(double h, double distance, const vector_type& pos) const
|
|
|
|
{
|
|
|
|
double Hinverse = 1.0 / h;
|
|
|
|
double scale_GradW = -2.0 * PowerExpansion<Dimensions + 1>(Hinverse) /
|
2018-10-18 13:39:40 +00:00
|
|
|
vtkm::Pow(M_PI, static_cast<double>(Dimensions) / 2.0);
|
2017-05-18 14:29:41 +00:00
|
|
|
double Q = distance * Hinverse;
|
|
|
|
|
|
|
|
//!!! check this due to the fitting offset
|
|
|
|
if (distance != 0.0)
|
2015-09-14 07:27:32 +00:00
|
|
|
{
|
2018-10-18 13:39:40 +00:00
|
|
|
return scale_GradW * vtkm::Exp(-Q * Q) * pos;
|
2015-09-14 07:27:32 +00:00
|
|
|
}
|
2017-05-18 14:29:41 +00:00
|
|
|
else
|
2015-09-14 07:27:32 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
return vector_type(0.0);
|
2015-09-14 07:27:32 +00:00
|
|
|
}
|
2017-05-18 14:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
// return the maximum distance at which this kernel is non zero
|
|
|
|
VTKM_EXEC_CONT
|
|
|
|
double maxDistance() const { return maxRadius_; }
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
// return the maximum distance at which this variable h kernel is non zero
|
|
|
|
VTKM_EXEC_CONT
|
|
|
|
double maxDistance(double h) const { return getDilationFactor() * h; }
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
// return the maximum distance at which this kernel is non zero
|
|
|
|
VTKM_EXEC_CONT
|
|
|
|
double maxSquaredDistance() const { return maxRadius2_; }
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
// return the maximum distance at which this kernel is non zero
|
|
|
|
VTKM_EXEC_CONT
|
|
|
|
double maxSquaredDistance(double h) const
|
|
|
|
{
|
|
|
|
return PowerExpansion<2>(getDilationFactor()) * h * h;
|
|
|
|
}
|
2015-09-14 07:27:32 +00:00
|
|
|
|
|
|
|
private:
|
2017-05-18 14:29:41 +00:00
|
|
|
double norm_;
|
|
|
|
double Hinverse_;
|
|
|
|
double Hinverse2_;
|
|
|
|
double maxRadius_;
|
|
|
|
double maxRadius2_;
|
|
|
|
double scale_W_;
|
|
|
|
double scale_GradW_;
|
2015-09-14 07:27:32 +00:00
|
|
|
};
|
2017-05-18 14:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-14 07:27:32 +00:00
|
|
|
|
|
|
|
#endif
|