Signed Distance Fields 1.0
SDFTree Class Reference

Signed distance field construction tree. More...

#include <sdf.h>

Inheritance diagram for SDFTree:
AnalyticScalarField

Public Member Functions

 SDFTree (SDFNode *)
 Constructor.
 ~SDFTree ()
 Destructor.
void ComputeCost (const Vector &) const
 Compute the number of recursive calls needed to evaluate the field at a point.
double Value (const Vector &) const
 Computes the signed distance to the surface.
Vector Gradient (const Vector &) const
 Compute the gradient of the field.
Vector Normal (const Vector &) const
 Compute the normal of the field.
bool Inside (const Vector &) const
 Check if a point is inside or outside.
Color GetColor (const Vector &) const
 Compute the color.
double K () const
 Computes the Lipschitz constant of the construction tree.
Box GetBox () const
 Get the bounding box of the construction tree.
void Union (SDFNode *)
 Apply an union operator between the root and paremeter.
void Intersection (SDFNode *)
 Apply an intersection operator between the root and paremeter.
void Difference (SDFNode *)
 Apply a difference operator between the root and paremeter.
void SmoothDifference (SDFNode *, double)
 Apply a smooth difference operator between the root and paremeter.
void SmoothUnion (SDFNode *, double)
 Apply a smooth union operator between the root and paremeter.
void AddSphere (const Vector &, double, double)
 Adds a sphere.
bool SphereTrace (const Ray &, const double &, const double &, const double &, double &, int &, const double &=0.001) const
 Find the first intersection between a ray and the object.
bool Intersect (const Ray &, Vector &) const
 Intersection with the construction tree, using sphere tracing.
virtual int Memory () const
 Computes the memory used by the construction tree.
int Nodes () const
 Returns the number of nodes in the tree.
SDFNode * Root () const
 Returns the root of the tree.
QString TimingsGradientAnalytic (int) const
 Computes the timings (in ms) for the analytic (if available) gradient function of the construction tree.
QString TimingsGradientNumerical (int) const
 Computes the timings (in ms) for the numerical gradient function of the construction tree.
QString Timings (int) const
 Computes the timings (in ms) for the field function of the construction tree.
QString Timings (Box, int) const
 Compute the time taken to evaluate the tree at n random points.
QString GetShader (QVector< float > &floatData, bool materialShader=true, bool costShader=true, bool gradShader=true, bool log=true) const
 Create the GLSL code of the tree.
QString GetShaderIndented (bool costShader=true, bool gradShader=true) const
 create the GLSL code to render the SDF using sphereTracing.glsl
QImage photobox (int width=512, int height=512, int nb_sample=8192, QString defines="", bool auto_center=true, int *render_time=nullptr, bool log=true, int nb_bounce=3, float fov=50, float znear=0.1, float zfar=500, Matrix4 mat_v=Matrix4::Translate(Vector(0, 0, -4)) *Matrix4::Rotation(Vector(0.6, 0.4, 0)) *Matrix4::Rotation(Vector(-Math::Pi/2, 0, 0))) const
 Render the construction tree with the GPU path tracer (photobox).
QImage photobox (QString defines="", Photobox_args args=Photobox_args()) const
 Render the construction tree with the GPU path tracer (photobox), with all the parameters gathered in a structure.
void ImportJson (const QString &)
 Replace the root by the node described in a JSON file.
void ExportJson (const QString &)
 Write the tree in a JSON file, in a compact and indented form.

Static Public Member Functions

static void ResetCalls ()
 Reset the number of function calls.
static int Calls ()
 Returns the number of calls to Value().
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 setComputeNbrecursiveCalls (bool computeRecCalls)
 Set the static boolean computeNbrecursiveCalls to allow computation of nb of recursive call in the function Value.
static SDFTree * CreateCandlestick ()
 Create the procedural model of a candlestick.
static SDFTree * Chandelier ()
 Create the procedural model of a chandelier.
static SDFTree * Chandelier5 ()
 Create the procedural model of a second chandelier, four-fold symmetric and bounded by an envelope.
static SDFNode * Fountain ()
 Create the procedural model of a fountain.
static SDFNode * WineGlass ()
 Creates a wine glass: a rounded base, a stem made of smoothly blended spheres, and a cup obtained by revolving a quadric curve set.
static SDFNode * DesignTube ()
 Create the procedural model of a tube-shaped design object.
static SDFNode * CAD ()
 Create the procedural model of a mechanical part.
static SDFNode * Cad2 ()
 Create the procedural model of a second mechanical part.
static SDFNode * Lamp ()
 Create the procedural model of a lamp.

Protected Attributes

SDFNode * root = nullptr
 Root node.

Static Protected Attributes

static int calls = 0
 Number of call to SDFTree::Value().

Detailed Description

Signed distance field construction tree.

Owns the root SDFNode and exposes evaluation, sphere-tracing, and GLSL code generation. Follows the same sign convention as SDFNode: negative inside the surface, positive outside.

Constructor & Destructor Documentation

◆ SDFTree()

SDFTree::SDFTree ( SDFNode * r)
explicit

Constructor.

Parameters
rRoot node.

◆ ~SDFTree()

SDFTree::~SDFTree ( )

Destructor.

Recursively deletes the entire node hierarchy rooted at root. The tree takes ownership of the root node passed to its constructor; callers must not delete it manually nor share it with another owner.

Member Function Documentation

◆ AddSphere()

void SDFTree::AddSphere ( const Vector & c,
double r,
double e )

Adds a sphere.

Parameters
cCenter.
rRadius.
eEnergy.

◆ ComputeCost()

void SDFTree::ComputeCost ( const Vector & p) const

Compute the number of recursive calls needed to evaluate the field at a point.

The calls are added to the counter returned by recursiveCalls().

Parameters
pPoint.

◆ Difference()

void SDFTree::Difference ( SDFNode * n)

Apply a difference operator between the root and paremeter.

Parameters
nnode.

◆ ExportJson()

void SDFTree::ExportJson ( const QString & path)

Write the tree in a JSON file, in a compact and indented form.

Parameters
pathPath of the file.

◆ GetColor()

Color SDFTree::GetColor ( const Vector & p) const

Compute the color.

Returns the default grey color of SDFNode::GetColor() if the tree is empty.

Parameters
pPoint.

◆ GetShader()

QString SDFTree::GetShader ( QVector< float > & floatData,
bool materialShader = true,
bool costShader = true,
bool gradShader = true,
bool log = true ) const

Create the GLSL code of the tree.

Author
HUBERT-BRIERRE Pierre

◆ GetShaderIndented()

QString SDFTree::GetShaderIndented ( bool costShader = true,
bool gradShader = true ) const

create the GLSL code to render the SDF using sphereTracing.glsl

Author
HUBERT-BRIERRE Pierre

◆ Gradient()

Vector SDFTree::Gradient ( const Vector & p) const
virtual

Compute the gradient of the field.

Parameters
pPoint.

Reimplemented from AnalyticScalarField.

◆ ImportJson()

void SDFTree::ImportJson ( const QString & path)

Replace the root by the node described in a JSON file.

If the file does not exist or cannot be opened, a message is printed and the tree is left unchanged. Otherwise the previous root is deleted.

Parameters
pathPath of the file.

◆ Inside()

bool SDFTree::Inside ( const Vector & p) const

Check if a point is inside or outside.

Parameters
pPoint.

◆ Intersection()

void SDFTree::Intersection ( SDFNode * n)

Apply an intersection operator between the root and paremeter.

Parameters
nnode to be intersected with root.

◆ Normal()

Vector SDFTree::Normal ( const Vector & p) const
virtual

Compute the normal of the field.

Parameters
pPoint.

Reimplemented from AnalyticScalarField.

◆ photobox() [1/2]

QImage SDFTree::photobox ( int width = 512,
int height = 512,
int nb_samples = 8192,
QString defines = "",
bool auto_center = true,
int * render_time = nullptr,
bool log = true,
int nb_bounce = 3,
float fov = 50,
float znear = 0.1,
float zfar = 500,
Matrix4 mat_v = Matrix4::Translate(Vector(0, 0, -4)) * Matrix4::Rotation(Vector(0.6, 0.4, 0)) * Matrix4::Rotation(Vector(-Math::Pi / 2, 0, 0)) ) const

Render the construction tree with the GPU path tracer (photobox).

The tree is converted into GLSL with GetShader(), then rendered by compute_photobox(), framed on the bounding box of the tree, see GetBox().

An OpenGL 4.5 context must be current, and the environment variable ARCHESLIBDIR must be set so that the shader LibSigned/LibSigned/Shaders/photobox.glsl can be found.

Example:

SDFTree tree(new SDFSphere(Sphere(3.0)));
QImage image = tree.photobox(512, 512, 1024, "");
image.save("sphere.png");
SDFSphere(const Vector &, double)
Create a sphere scalar field.
Definition sdfsphere.cpp:21
SDFTree(SDFNode *)
Constructor.
Definition sdftree.cpp:41
constexpr Sphere()
Parameters
width,heightSize of the image in pixels.
nb_samplesNumber of samples per pixel; the rendering time is roughly proportional to it.
definesGLSL code appended to the shader of the tree, typically #define directives such as "#define DISK\n" to show the iso-value disk (see photobox.glsl).
auto_centerIf true, the bounding box of the tree is centered and scaled to fit the unit box, and the floor is placed at the bottom of the box; if false, the scene is rendered as is with the floor at z=-1.
render_timeIf not null, receives the GPU rendering time in milliseconds.
logIf true, prints progress and timings on the standard output.
nb_bounceMaximum number of light bounces.
fovVertical field of view, in degrees.
znearNear plane distance.
zfarFar plane distance.
mat_vView matrix of the camera.
Returns
The rendered image, in QImage::Format_RGB32.
See also
photobox(QString, Photobox_args) const

◆ photobox() [2/2]

QImage SDFTree::photobox ( QString defines = "",
Photobox_args args = Photobox_args() ) const

Render the construction tree with the GPU path tracer (photobox), with all the parameters gathered in a structure.

Contrary to the other overload, the framing uses Photobox_args::box and not the box of the tree: set it explicitly, for instance with args.box = GetBox();, otherwise the default unit box is used. This overload also gives access to cropping, see Photobox_args::crop_min and Photobox_args::crop_max.

Parameters
definesGLSL code appended to the shader of the tree, typically #define directives.
argsRendering parameters.
Returns
The rendered image, in QImage::Format_RGB32.
See also
photobox(int, int, int, QString, bool, int*, bool, int, float, float, float, Matrix4) const

◆ SmoothDifference()

void SDFTree::SmoothDifference ( SDFNode * n,
double t )

Apply a smooth difference operator between the root and paremeter.

Parameters
nnode.
tSmoothing radius.

◆ SmoothUnion()

void SDFTree::SmoothUnion ( SDFNode * n,
double t )

Apply a smooth union operator between the root and paremeter.

Parameters
nnode.
tSmoothing radius.

◆ SphereTrace()

bool SDFTree::SphereTrace ( const Ray & ray,
const double & k,
const double & a,
const double & b,
double & t,
int & s,
const double & epsilon = 0.001 ) const
virtual

Find the first intersection between a ray and the object.

Parameters
rayThe ray
kLipschitz constant.
a,bSearching interval.
tReturned intersection depth.
sReturned step count.
epsilonMinimum stepping distance.

Reimplemented from AnalyticScalarField.

◆ Timings() [1/2]

QString SDFTree::Timings ( Box bbox,
int n ) const

Compute the time taken to evaluate the tree at n random points.

Parameters
nNumber of calls.
bboxBounding box for the random points.
Returns
A string with the elapsed milliseconds for n calls.

◆ Timings() [2/2]

QString SDFTree::Timings ( int n) const

Computes the timings (in ms) for the field function of the construction tree.

Parameters
nNumber of calls
Returns
A string with the elapsed milliseconds for n calls.

◆ TimingsGradientAnalytic()

QString SDFTree::TimingsGradientAnalytic ( int n) const

Computes the timings (in ms) for the analytic (if available) gradient function of the construction tree.

Parameters
nNumber of calls
Returns
A string with the elapsed milliseconds for n calls.

◆ TimingsGradientNumerical()

QString SDFTree::TimingsGradientNumerical ( int n) const

Computes the timings (in ms) for the numerical gradient function of the construction tree.

Parameters
nNumber of calls
Returns
A string with the elapsed milliseconds for n calls.

◆ Union()

void SDFTree::Union ( SDFNode * n)

Apply an union operator between the root and paremeter.

Parameters
nnode to be unioned with root.

◆ Value()

double SDFTree::Value ( const Vector & p) const
virtual

Computes the signed distance to the surface.

Parameters
pPoint.

Reimplemented from AnalyticScalarField.