Tree — material

Material grading, blending, and convolution nodes.

class Convolution : public Unary
#include <convolution.h>

A node that applies a convolution kernel to the material of the child node.

The convolution kernel is a 3D matrix of integers. When evaluated, the kernel is multiplied by the neighbors of the current voxel. The result is then used to determine the new material of the voxel using a weighted stochastic process. The weights are determined by the values of the kernel. The voxel sizes is needed to determine the neighbors of the current voxel, but in theory does not have to match the voxel size of the sample space if you are fine with down sampling on the convolution.

Note

The kernel must be odd in all dimensions.

Note

The node runs in O(kx * ky * kz) time, where kx, ky, and kz are the dimensions of the kernel.

Note

This node is unary and must have a single child.

Public Types

typedef std::vector<std::vector<std::vector<size_t>>> Kernel

A 3D matrix of integers is the kernel.

Public Functions

virtual std::pair<glm::vec3, glm::vec3> bounding_box() override

virtual std::shared_ptr<Node> clone() override

See also

Node::clone

Convolution()

Default constructor, uses.

Convolution(Kernel kernel)

Constructor.

Parameters:

kernel – The convolution kernel

Convolution(Kernel kernel, const std::shared_ptr<Node> &child)

Constructor with child.

Parameters:
  • kernel – The convolution kernel

  • child – The child node

virtual std::unordered_map<uint8_t, float> distribution(double x, double y, double z)

virtual double evaluate(double x, double y, double z) final

See also

Node::evaluate

virtual std::vector<uint8_t> material_list() override

/see Node::material_list

virtual void prepare(const glm::vec3 &voxel_size, double interior_bandwidth, double exterior_bandwidth) override

Calls the child’s prepare function and then caches the child’s material list.

See also

Node::prepare

Public Static Functions

static Kernel DefaultFlatKernel(int x, int y, int z)

Creates a kernel that averages all values.

This means all values are one including the center value.

Parameters:
  • x – The x dimension of the kernel

  • y – The y dimension of the kernel

  • z – The z dimension of the kernel

Returns:

The kernel

static Kernel DefaultPassKernel(int x, int y, int z)

Creates a kernel that passes all values through.

This means all values are zero except for the center value which is one.

Parameters:
  • x – The x dimension of the kernel

  • y – The y dimension of the kernel

  • z – The z dimension of the kernel

Returns:

The kernel

Private Functions

std::unordered_map<uint8_t, float> compute_convolved_distribution(double x, double y, double z)
int computeNewMaterial(std::unordered_map<uint8_t, float> &distribution)

Computes the new material of the voxel using a weighted stochastic process.

Parameters:

distribution – a pointer to he distribution of materials, essentially the probability of each material. A sort of histogram. This is always MAX_MATERIALS size

Returns:

The new material id

Private Members

std::vector<uint8_t> m_cached_child_materials

We cache the child material list to avoid calling it every time in the material() function.

Kernel m_kernel

The convolution kernel.

vec3 m_voxel_size

The voxel size of the sample space.

class FGrade : public Unary
#include <f_grade.h>

A node that applies grading functions to a child.

The grading functions are probability density functions that must all sum to 1 for any given point. The functions are evaluated and the material is chosen based on the discrete probability of the location in space. The syntax for the expressions must follow the same format as the Function class

See also

Function. The node has two mode:

  • Probability mode: The probability of each material is computed at a location in space and the material is chosen based on a stochastic process.

  • Threshold mode: The probability of each material is computed at a location in space and the material is chosen based on the highest.

Note

This node is unary and must have a single child.

Note

This node runs in O(n) time where n is tokens in the longest expression

Public Functions

virtual std::pair<glm::vec3, glm::vec3> bounding_box() override

virtual std::shared_ptr<Node> clone() override

See also

Node::clone()

virtual std::unordered_map<uint8_t, float> distribution(double x, double y, double z) final

virtual double evaluate(double x, double y, double z) final

See also

Node::evaluate()

FGrade()

Default constructor.

FGrade(const std::vector<std::function<double(double x, double y, double z, double rho, double phic, double r, double theta, double phis, double d)>> &functions, std::vector<uint8_t> materials, bool prob_mode = true, const std::shared_ptr<Node> &child = nullptr)

Constructor.

Parameters:
  • functions – A list of function pointers that are probability density functions

  • materials – A list of materials that correspond to the expressions

  • prob_mode – If true, the node will run in probability mode. If false, the node will run in threshold mode.

  • child – The optional child node

FGrade(std::vector<std::string> functions, std::vector<uint8_t> materials, bool prob_mode = true, const std::shared_ptr<Node> &child = nullptr)

Constructor.

Parameters:
  • functions – A list of expressions that are probability density functions

  • materials – A list of materials that correspond to the expressions

  • prob_mode – If true, the node will run in probability mode. If false, the node will run in threshold mode.

  • child – The optional child node

virtual std::vector<uint8_t> material_list() override

virtual void prepare(const glm::vec3 &voxel_size, double interior_bandwidth, double exterior_bandwidth) final

Validates the supplied functions and prepares the parsers.

See also

Node::prepare()

Private Functions

void prepare_evaluators()

Private Members

double m_d
std::vector<std::shared_ptr<ExpressionEvaluator>> m_evaluators
std::vector<std::string> m_function_strings

The expressions.

std::vector<std::function<double(double x, double y, double z, double rho, double phic, double r, double theta, double phis, double d)>> m_functions
std::vector<uint8_t> m_materials

The materials.

double m_phic
double m_phis
bool m_prepared = false
bool m_prob_mode = true

The mode.

double m_r
double m_rho
double m_theta
bool m_use_string_evaluators = true
double m_x
double m_y
double m_z
class Blend : public Unary
#include <blend.h>

Public Functions

Blend(double radius)
Blend(double radius, const std::shared_ptr<Node> &child)
virtual std::pair<glm::vec3, glm::vec3> bounding_box() override
virtual std::shared_ptr<Node> clone() override

Clones the node and returns a new instance of the node.

Note

This may be called before or after prepare() is called, however it is always more efficient to call it after. This is because the prepare() function must be called on all clones of the node separately. If you call prepare() first and then clone the node, the clone() operation will copy from memory instead of re-reading from disk.

Returns:

A new instance of the node

virtual std::unordered_map<uint8_t, float> distribution(double x, double y, double z) override

Returns the material distribution at the given point.

Parameters:
  • x – The x coordinate of the point to evaluate.

  • y – The y coordinate of the point to evaluate.

  • z – The z coordinate of the point to evaluate.

Returns:

A list of pairs each containing the material ID and the probability distribution of that material.

virtual double evaluate(double x, double y, double z) override

Evaluates the node at the given point.

Parameters:
  • x – The x coordinate of the point to evaluate.

  • y – The y coordinate of the point to evaluate.

  • z – The z coordinate of the point to evaluate.

Returns:

The distance to the surface at the given point.

virtual std::vector<uint8_t> material_list() override

Returns a list of all the materials used by the node and its children.

Returns:

A list of all the material IDs used by the node and its children.

virtual void prepare(const glm::vec3 &voxel_size, double interior_bandwidth, double exterior_bandwidth) override

See also

Node::prepare()

Private Members

double m_radius
vec3 m_voxel_size = vec3(1.0f)