Signed Distance Fields 1.0
SDFEnvelope Class Reference

Envelope operator: replaces a costly tree by a cheap bounding proxy when the point is far enough. More...

#include <sdf.h>

Inheritance diagram for SDFEnvelope:
SDFBinary SDFNode

Public Member Functions

 SDFEnvelope (SDFNode *, SDFNode *, double=1.0, bool=false)
 Create an envelope node.
 SDFEnvelope (const QJsonValue &)
QJsonValue ToJson () const
 Conversion to JavaScript Object Notation.
virtual ~SDFEnvelope ()
 Destructor.
virtual double Signed (const Vector &) const
 Compute the signed distance.
double BID (const Vector &) const
 Compute a lower bound of the signed distance.
void ComputeCostSigned (const Vector &)
 Compute the number of recursive calls to Signed().
QString GetShader (PrimitiveManager &, std::list< QString > &, bool &) const
 Compute the GLSL code to render the node.
QString GetGradientShader (PrimitiveManager &, std::list< QString > &, bool &) const
 Compute the GLSL code to compute the gradient the sdf.
QString GetCostShader (PrimitiveManager &, std::list< QString > &, bool &) const
 Compute the GLSL code to compute the cost of a call to sdf.
virtual int Memory () const
 Return the size of a sub-tree.
virtual SDFNode * Copy () const
 Deep copy.
virtual double Cost (const Vector &p) const
 Cost of the evaluation of the field at a point: the cost of the node plus the costs of the two sub-trees.
virtual SDFNode * extract_proxy (int=0)
 Replace each sub-tree by a copy of its proxy, see SDFNode::extract_proxy().
Public Member Functions inherited from SDFBinary
 SDFBinary (SDFNode *, SDFNode *)
 Create a binary node.
virtual ~SDFBinary ()
 Recursively destroy the sub-trees.
virtual double K (const Box &) const
 Return the local Lipschitz constant.
virtual void SetShaderId (PrimitiveManager &)
 Set up an id for generating GLSL code.
virtual void ResetShaderId ()
 Reset the id for generating GLSL code.
int Nodes () const
 Compute the number of nodes in the sub-tree.
SDFNode * add_proxy ()
 Create a copy of the node where each sub-tree is replaced by its version with proxies, then bounded by the cheapest proxy, see SDFNode::add_proxy_root().
Public Member Functions inherited from SDFNode
 SDFNode ()
 Create a generic node.
virtual ~SDFNode ()
 Destructor.
virtual Vector Gradient (const Vector &) const
 Compute the gradient of the field.
virtual bool Inside (const Vector &) const
 Check if a point is inside or outside.
virtual Color GetColor (const Vector &) const
 Computes the color.
virtual double K () const
 Return the Lipschitz constant of the node.
Box GetBox () const
 Return the bounding box of the node.
virtual double Cost (int=10000) const
 Estimate the average cost of the evaluation of the field over the box of the node.
virtual double Cost (Box, int=10000) const
 Estimate the average cost of the evaluation of the field over a box.
SDFNode * add_proxy_root ()
 Create a copy of the node, bounded by the cheapest proxy if there is one.

Additional Inherited Members

Static Public Member Functions inherited from SDFNode
static SDFNode * FromJson (const QJsonValue &json)
 Conversion from JavaScript Object Notation.
static SDFNode * FromJson (const QByteArray &json)
 Create a node from its JSON description.
static void ResetrecursiveCalls ()
 Reset the recursive call property of the tree.
static int recursiveCalls ()
 Return the number of recursive calls counted by the ComputeCost*() functions since the last reset.
static void addrecursiveCalls (int)
 Add a number to the _recursiveCalls counter.
static SDFNode * Stalactite (const Vector &p, const double &h, const double &r, bool noise=true, Xoshiro256 &random=Xoshiro256::X512, const double &largeur=0.25, const double &espacement=0.61)
 Procedural parameterized stalactite.
static SDFNode * SpherePieces ()
 Sphere carved, placed on a cylinder. Nice scene for testing sharp features reconstruction.
Protected Attributes inherited from SDFBinary
SDFNode * e [2] = { nullptr,nullptr }
 Sub-trees.
Protected Attributes inherited from SDFNode
Box box = Box::Infinity
 Box bounding the surface.
double k = 1.0
 Lipschitz constant.
int cost = 1
 Relative computation cost.
int id = -1
 Id to optimize the glsl code generation (avoid repetition when looking twice the same node).
Static Protected Attributes inherited from SDFNode
static constexpr double Epsilon = 0.01
 Small constant used for bounding volumes and as finite-difference step in Gradient().
static int _recursiveCalls = 0
 Recursive calls counted by ComputeCost*().

Detailed Description

Envelope operator: replaces a costly tree by a cheap bounding proxy when the point is far enough.

The node has two children: the original tree \( e_0 \) and a simplified tree \( e_1 \) (the envelope, or proxy) that englobes it. The node was formerly named SDFBEIS, and the BEIS acronym is still found in some names (useBEIS uniform of the shaders, SDFTerrainLoDContinuousBEIS). The field is

\[ f(p) = \left\{ \begin{array}{ll} e_1(p) & \mbox{if } |e_1(p)| > |o| \mbox{ and } e_1(p) \, o > 0 \\ e_0(p) & \mbox{otherwise} \end{array} \right. \]

where \( o \) is the offset. With a positive offset, the envelope is used outside of the original tree, as soon as the point is farther than \( o \) from the envelope: the (possibly expensive) tree is only evaluated close to the surface. As the envelope englobes the tree, its distance is a lower bound of the distance to the tree, so that ray marching remains safe. With a negative offset, the test is symmetric and applies to the interior (the envelope is then englobed by the tree). The discontinuity of the field when the test switches from one branch to the other is not removed on the CPU, where Signed() ignores continuous; the GLSL code generated by GetShader() uses it to blend the two fields with \( \alpha = \mathrm{smoothstep}(0, o, e_1(p)) \) (the order of the bounds is reversed for a negative offset):

\[ f(p) = \alpha \, e_1(p) + (1-\alpha) \, e_0(p) \]

In the shaders, the uniform useBEIS disables the envelope: the tree is then always evaluated.

The bounding box is that of the original tree. The gradient is that of the original tree, GetGradientShader() ignoring the envelope.

Author
Hubert-Brierre Pierre

Constructor & Destructor Documentation

◆ SDFEnvelope() [1/2]

SDFEnvelope::SDFEnvelope ( SDFNode * a,
SDFNode * b,
double _offset = 1.0,
bool _continuous = false )
explicit

Create an envelope node.

Parameters
aThe original tree.
bThe simplified englobing tree (the envelope).
_offsetDistance to the envelope beyond which its distance is returned instead of that of the tree; it is negative for an envelope englobed by the tree.
_continuousBlend the two fields in the shader code to avoid the discontinuity (not used by Signed()).

◆ SDFEnvelope() [2/2]

SDFEnvelope::SDFEnvelope ( const QJsonValue & json)
explicit

Member Function Documentation

◆ BID()

double SDFEnvelope::BID ( const Vector & p) const
virtual

Compute a lower bound of the signed distance.

The bound is not continuous, not even monotonous, but is faster to compute than the actual distance.

Author
Hubert-Brierre Pierre
Parameters
pPoint.

Reimplemented from SDFNode.

◆ ComputeCostSigned()

void SDFEnvelope::ComputeCostSigned ( const Vector & p)
virtual

Compute the number of recursive calls to Signed().

Author
Hubert-Brierre Pierre

Reimplemented from SDFBinary.

◆ Cost()

double SDFEnvelope::Cost ( const Vector & p) const
virtual

Cost of the evaluation of the field at a point: the cost of the node plus the costs of the two sub-trees.

Parameters
pPoint.

Reimplemented from SDFBinary.

◆ extract_proxy()

SDFNode * SDFEnvelope::extract_proxy ( int rec_lvl = 0)
virtual

Replace each sub-tree by a copy of its proxy, see SDFNode::extract_proxy().

The node is modified in place and returned.

Parameters
rec_lvlRecursion level.

Reimplemented from SDFBinary.

◆ GetCostShader()

QString SDFEnvelope::GetCostShader ( PrimitiveManager & primitives,
std::list< QString > & dependency,
bool & valid_glsl ) const
virtual

Compute the GLSL code to compute the cost of a call to sdf.

Author
Hubert-Brierre Pierre

Reimplemented from SDFBinary.

◆ GetGradientShader()

QString SDFEnvelope::GetGradientShader ( PrimitiveManager & primitives,
std::list< QString > & dependency,
bool & valid_glsl ) const
virtual

Compute the GLSL code to compute the gradient the sdf.

Author
Hubert-Brierre Pierre

Reimplemented from SDFNode.

◆ GetShader()

QString SDFEnvelope::GetShader ( PrimitiveManager & primitives,
std::list< QString > & dependency,
bool & valid_glsl ) const
virtual

Compute the GLSL code to render the node.

Author
Hubert-Brierre Pierre

Reimplemented from SDFNode.

◆ Memory()

int SDFEnvelope::Memory ( ) const
virtual

Return the size of a sub-tree.

Reimplemented from SDFNode.

◆ Signed()

double SDFEnvelope::Signed ( const Vector & p) const
virtual

Compute the signed distance.

Parameters
pPoint.

Reimplemented from SDFNode.

◆ ToJson()

QJsonValue SDFEnvelope::ToJson ( ) const
virtual

Conversion to JavaScript Object Notation.

Reimplemented from SDFNode.