torch_compute package

Submodules

torch_compute.batch_evaluate_sdf module

batch_evaluate(expr_set: List[object], sketcher, coords=None)

Evaluates a batch of expressions to generate signed distance fields (SDFs) using a sketcher.

Parameters:
  • expr_set (List[object]) – A list containing expressions, transformations, inversions, parameters, and batch size limits.

  • sketcher (Sketcher) – The sketcher object for creating SDFs.

  • coords (optional) – Custom coordinates for evaluation. Defaults to None.

Returns:

A tensor of SDFs for each expression in the batch.

Return type:

th.Tensor

create_evaluation_batches(compiled_expr_list: List[object], convert_to_cuda=True)

Creates evaluation batches from a list of compiled expressions.

This function processes a list of compiled expressions and organizes them into batches for evaluation. It consolidates transformation matrices, inversion matrices, and parameters for each primitive type, and optionally converts them to CUDA tensors.

Parameters:
  • compiled_expr_list (List[object]) – A list where each element is a tuple containing the expression, primitive transformation matrix, primitive inversion matrix, and primitive parameters.

  • convert_to_cuda (bool, optional) – If True, converts the matrices and parameters to CUDA tensors. Defaults to True.

Returns:

A tuple containing:
  • all_expressions (List): A list of all expressions.

  • all_prim_transforms (dict): A dictionary with primitive types as keys and their concatenated transformation matrices as values.

  • all_prim_inversion (dict): A dictionary with primitive types as keys and their concatenated inversion matrices as values.

  • all_prim_param (dict): A dictionary with primitive types as keys and a list of their parameters as values.

  • batch_limiter (List[dict]): A list of dictionaries for each expression indicating the batch size for each primitive type.

Return type:

tuple

torch_compute.color_functions module

apply_color(occupancy, color)

Applies a color to an occupancy grid.

Parameters:
  • occupancy (Tensor) – An occupancy grid tensor.

  • color (Tensor) – A color tensor.

Returns:

A colored canvas tensor.

Return type:

Tensor

destination_atop(source, destination)

Computes the destination-atop composite of source and destination images.

Parameters:
  • source (Tensor) – Source and destination image tensors.

  • destination (Tensor) – Source and destination image tensors.

Returns:

The result of applying the destination-atop composite operation.

Return type:

Tensor

destination_in(source, destination)

Computes the destination-in composite of source and destination images.

Parameters:
  • source (Tensor) – Source and destination image tensors.

  • destination (Tensor) – Source and destination image tensors.

Returns:

The result of applying the destination-in composite operation.

Return type:

Tensor

destination_out(source, destination)

Computes the destination-out composite of source and destination images.

Parameters:
  • source (Tensor) – Source and destination image tensors.

  • destination (Tensor) – Source and destination image tensors.

Returns:

The result of applying the destination-out composite operation.

Return type:

Tensor

destination_over(source, destination)

Computes the destination-over composite of source and destination images.

Parameters:
  • source (Tensor) – Source and destination image tensors.

  • destination (Tensor) – Source and destination image tensors.

Returns:

The result of applying the destination-over composite operation.

Return type:

Tensor

get_premultiplied_form(source, destination)

Converts source and destination color tensors to their premultiplied form.

Parameters:
  • source (Tensor) – Source and destination color tensors.

  • destination (Tensor) – Source and destination color tensors.

Returns:

Tensors in premultiplied form.

Return type:

Tuple[Tensor, Tensor]

get_unmultiplied_form(result)

Converts a premultiplied color tensor to its unmultiplied form.

Parameters:

result (Tensor) – Premultiplied color tensor.

Returns:

Unmultiplied color tensor.

Return type:

Tensor

modify_color(color_canvas, new_color)

Modifies the color of a color canvas.

Parameters:
  • color_canvas (Tensor) – A color canvas tensor.

  • new_color (Tensor) – New color tensor to apply.

Returns:

A modified color canvas tensor.

Return type:

Tensor

modify_opacity(color_canvas, opacity)

Modifies the opacity of a color canvas.

Parameters:
  • color_canvas (Tensor) – A color canvas tensor.

  • opacity (float) – Desired opacity level.

Returns:

A modified color canvas tensor.

Return type:

Tensor

source_atop(source, destination)

Computes the source-atop composite of source and destination images.

Parameters:
  • source (Tensor) – Source and destination image tensors.

  • destination (Tensor) – Source and destination image tensors.

Returns:

The result of applying the source-atop composite operation.

Return type:

Tensor

source_in(source, destination)

Computes the source-in composite of source and destination images.

Parameters:
  • source (Tensor) – Source and destination image tensors.

  • destination (Tensor) – Source and destination image tensors.

Returns:

The result of applying the source-in composite operation.

Return type:

Tensor

source_out(source, destination)

Computes the source-out composite of source and destination images.

Parameters:
  • source (Tensor) – Source and destination image tensors.

  • destination (Tensor) – Source and destination image tensors.

Returns:

The result of applying the source-out composite operation.

Return type:

Tensor

source_over(source, destination)

Computes the source-over composite of source and destination images.

Parameters:
  • source (Tensor) – Source and destination image tensors.

  • destination (Tensor) – Source and destination image tensors.

Returns:

The result of applying the source-over composite operation.

Return type:

Tensor

source_over_seq(*args)

Sequentially applies the source-over composite operation on a sequence of images. employed with executing macros in SVG expressions.

Parameters:

*args (Tensors) – A sequence of image tensors.

Returns:

The result of sequentially applying source-over compositing.

Return type:

Tensor

svg_xor(source, destination)

Computes the XOR composite (as defined in SVG) of source and destination images.

Parameters:
  • source (Tensor) – Source and destination image tensors.

  • destination (Tensor) – Source and destination image tensors.

Returns:

The result of applying the SVG XOR composite operation.

Return type:

Tensor

torch_compute.common module

torch_compute.compile_expression module

compile_expr(expression: GLExpr, sketcher: Sketcher | None = None, rectify_transform=False)

Compiles a GL expression into a format suitable for batch evaluation, gathering transformations, primitive parameters, and handling difference and complement operations.

This function traverses the expression tree to extract and organize necessary data for rendering the expression. It accounts for transformations, inversion modes, and primitive parameters, and resolves any complex operations like Difference and Complement.

Parameters:
  • expression (GLExpr) – The GL expression to be compiled.

  • sketcher (Sketcher, optional) – The sketcher object used for affine transformations. Defaults to None.

  • rectify_transform (bool, optional) – Flag to determine if transformations should be rectified. Defaults to RECTIFY_TRANSFORM.

Returns:

A tuple containing the compiled expression, primitive transformations, primitive inversions, and primitive parameters. These are organized to facilitate batch evaluation of the expression.

Return type:

tuple

create_compiled_expr(expression, sketcher, resolve_to_dnf=False, convert_to_cpu=True, rectify_transform=False)

Compiles an expression and optionally converts it to Disjunctive Normal Form (DNF) and to CPU.

Parameters:
  • expression – The expression to compile.

  • sketcher – The sketcher object used in compilation.

  • resolve_to_dnf (bool) – If True, converts the expression to DNF.

  • convert_to_cpu (bool) – If True, converts tensors to CPU tensors.

  • rectify_transform (bool) – If True, rectifies transformations during compilation.

Returns:

A tuple containing the compiled expression, transforms, inversions, and parameters.

expr_prim_count(expression: GLExpr)

Get the number of primitives of each kind in the expression.

expr_to_dnf(expression, max_expr_size=500)

Converts an expression to its Disjunctive Normal Form (DNF) using graph transformation rules.

Parameters:
  • expression – The expression to convert.

  • max_expr_size (int) – The maximum allowed size of the expression during conversion.

Returns:

A sympy expression in DNF.

expr_to_graph(expression)

Converts a sympy expression to a rustworkx graph. This is valid only for compiled graphs. Used for simplifying the expression to DNF form.

Parameters:

expression – A sympy expression representing a mathematical or logical construct.

Returns:

A rustworkx PyDiGraph representing the structure of the expression.

get_rule_match(graph, only_simplify=False)

Identifies a rule match in a graph that represents an expression.

Parameters:
  • graph – The graph representation of the expression.

  • only_simplify (bool) – If True, only simplification rules are applied.

Returns:

A match if a rule can be applied, else None.

graph_to_expr(graph)

Converts a rustworkx graph back to a sympy expression. Used to get the expression back after converting to the DNF form.

Parameters:

graph – A rustworkx PyDiGraph generated from a sympy expression.

Returns:

A sympy expression reconstructed from the graph.

resolve_rule(graph, resolve_rule)

Applies a rule to transform a graph representing an expression.

Parameters:
  • graph – The graph representation of the expression.

  • resolve_rule – The rule to be applied to the graph.

Returns:

The graph after applying the transformation rule.

torch_compute.evaluate_expression module

expr_to_colored_canvas(expression: GLExpr, sketcher: Sketcher, rectify_transform=False, relaxed_occupancy=False, temperature=0.0, coords=None)

TODO: This function is to be tested.

expr_to_sdf(expression: GLFunction, sketcher: Sketcher, secondary_sketche: Sketcher | None = None, rectify_transform: bool = False, coords: Tensor | None = None)

Converts a GeoLIPI SDF expression into a Signed Distance Field (SDF) using a sketcher. This function is faster than recursive_evaluate as it evaluates the expression using a stack-based approach. However, it does not support all GeoLIPI operations, notably higher-order primitives, and certain modifiers.

Parameters:
  • expression (GLExpr) – The GLExpr expression to be converted to an SDF.

  • sketcher (Sketcher) – The primary sketcher object used for generating SDFs.

  • rectify_transform (bool) – Flag to apply rectified transformations. Defaults to RECTIFY_TRANSFORM.

  • secondary_sketcher (Sketcher, optional) – Secondary sketcher - Never used.

  • coords (Tensor, optional) – Custom coordinates to use for the SDF generation.

Returns:

The generated SDF corresponding to the input expression.

Return type:

Tensor

parse_tensor_from_expr(expression, params)
recursive_evaluate(expression: GLFunction, sketcher: Sketcher, secondary_sketcher: Sketcher | None = None, initialize: bool = True, rectify_transform: bool = False, coords: Tensor | None = None, tracked_scale: Tensor | None = None, relaxed_occupancy: bool = False, relax_temperature: float = 0.0)

Recursively evaluates a GeoLIPI expression to generate a signed distance field (SDF) or a color canvas.

This function can handles all GeoLIPI operations but is slower than the other evaluation methods.

Parameters:
  • expression (GLFunction) – The GLFunction expression to evaluate.

  • sketcher (Sketcher) – Primary sketcher object for SDF or color generation.

  • secondary_sketcher (Sketcher, optional) – Secondary sketcher for higher-order primitives.

  • initialize (bool) – Flag to initialize coordinates and scale if True. Used for the first call.

  • rectify_transform (bool) – Flag to rectify transformations.

  • coords (th.Tensor, optional) – Coordinates for evaluation. If None, generated from sketcher.

  • tracked_scale (th.Tensor, optional) – Scale tracking tensor. If None, generated from sketcher.

  • relaxed_occupancy (bool) – Flag to use relaxed occupancy for soft SDFs. Useful with Parameter Optimization of SVG expressions.

  • relax_temperature (float) – Temperature parameter for relaxed occupancy. Defaults to 0.0.

Returns:

The resulting SDF or color canvas from evaluating the expression.

Return type:

th.Tensor

smoothen_sdf(execution, temperature)

torch_compute.sdf_functions_2d module

ndot(vec_a, vec_b)
sdf2d_arc(points, angle, ra, rb)

Computes the SDF for a 2D arc.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • angle (Tensor) – Central angle of the arc, shape [batch, 1].

  • ra (Tensor) – Radius of the arc, shape [batch, 1].

  • rb (Tensor) – Width of the arc, shape [batch, 1].

Returns:

The SDF values for the arc.

Return type:

Tensor

sdf2d_blobby_cross(points, he)

Computes the SDF for a 2D ‘blobby’ cross shape.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • he (Tensor) – Height parameter influencing the shape, shape [batch, 1].

Returns:

The SDF values for the blobby cross.

Return type:

Tensor

sdf2d_box(points, size)

Computes the SDF for a 2D axis-aligned rectangle.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • size (Tensor) – Dimensions of the box, shape [batch, 2].

Returns:

The SDF values for the rectangle.

Return type:

Tensor

sdf2d_circle(points, radius)

Computes the signed distance field for a 2D circle.

Parameters:
  • points (Tensor) – The coordinates at which to evaluate the SDF, shape [batch, num_points, 2].

  • radius (Tensor) – The radius of the circle, shape [batch, 1].

Returns:

The calculated SDF values for each point.

Return type:

Tensor

sdf2d_circle_wave(points, tb, ra)

Computes the SDF for a circle wave pattern in 2D.

Parameters:
  • points (Tensor) – Input points to evaluate the SDF, shape [batch, num_points, 2].

  • tb (Tensor) – Parameter tensor for the wave, shape [batch, 1].

  • ra (Tensor) – Radius of the circles, shape [batch, 1].

Returns:

The SDF values of the circle wave pattern at the input points.

Return type:

Tensor

sdf2d_cool_s(points)

Computes the SDF for a ‘cool S’ shape in 2D.

Parameters:

points (Tensor) – Input points to evaluate the SDF, shape [batch, num_points, 2].

Returns:

The SDF values of the ‘cool S’ shape at the input points.

Return type:

Tensor

sdf2d_cross(points, b, r)

Computes the SDF for a 2D cross.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • b (Tensor) – Size of the cross’ arms, shape [batch, 2].

  • r (Tensor) – Radius of the rounding at the cross’ center, shape [batch, 1].

Returns:

The SDF values for the cross.

Return type:

Tensor

sdf2d_cut_disk(points, r, h)

Computes the SDF for a 2D cut disk (circular segment).

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • r (Tensor) – Radius of the disk, shape [batch, 1].

  • h (Tensor) – Height of the cut segment, shape [batch, 1].

Returns:

The SDF values for the cut disk.

Return type:

Tensor

sdf2d_egg(points, ra, rb)

Computes the SDF for a 2D egg shape.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • ra (Tensor) – Radius along one axis, shape [batch, 1].

  • rb (Tensor) – Radius along the other axis, shape [batch, 1].

Returns:

The SDF values for the egg shape.

Return type:

Tensor

sdf2d_ellipse(points, ab)

Computes the SDF for a 2D ellipse.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • ab (Tensor) – Dimensions (semi-axes lengths) of the ellipse, shape [batch, 2].

Returns:

The SDF values for the ellipse.

Return type:

Tensor

sdf2d_equilateral_triangle(points, side_length)

Computes the SDF for a 2D equilateral triangle.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • side_length (Tensor) – Length of each side of the triangle, shape [batch, 1].

Returns:

The SDF values for the equilateral triangle.

Return type:

Tensor

sdf2d_heart(points)

Computes the SDF for a 2D heart shape.

Parameters:

points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

Returns:

The SDF values for the heart shape.

Return type:

Tensor

sdf2d_hexagram(points, r)

Computes the SDF for a 2D hexagram (six-pointed star).

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • r (Tensor) – Radius of the circumscribing circle of the hexagram, shape [batch, 1].

Returns:

The SDF values for the hexagram.

Return type:

Tensor

sdf2d_horse_shoe(points, angle, r, w)

Computes the SDF for a 2D horseshoe shape.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • angle (Tensor) – Central angle of the horseshoe, shape [batch, 1].

  • r (Tensor) – Radius of the horseshoe, shape [batch, 1].

  • w (Tensor) – Width of the horseshoe, shape [batch, 1].

Returns:

The SDF values for the horseshoe shape.

Return type:

Tensor

sdf2d_hyperbola(points, k, he)

Computes the SDF for a hyperbola in 2D.

Parameters:
  • points (Tensor) – Input points to evaluate the SDF, shape [batch, num_points, 2].

  • k (Tensor) – Scale factor for the hyperbola, shape [batch, 1].

  • he (Tensor) – Height parameter for the hyperbola, shape [batch, 1].

Returns:

The SDF values of the hyperbola at the input points.

Return type:

Tensor

sdf2d_isosceles_triangle(points, wi_hi)

Computes the SDF for a 2D isosceles triangle.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • wi_hi (Tensor) – Width and height of the triangle, shape [batch, 2].

Returns:

The SDF values for the isosceles triangle.

Return type:

Tensor

sdf2d_moon(points, d, ra, rb)

Computes the SDF for a 2D moon shape (crescent).

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • d (Tensor) – Distance between the centers of the circles forming the moon, shape [batch, 1].

  • ra (Tensor) – Radius of the first circle, shape [batch, 1].

  • rb (Tensor) – Radius of the second circle, shape [batch, 1].

Returns:

The SDF values for the moon shape.

Return type:

Tensor

sdf2d_no_param_circle(points)

Computes the SDF for a unit circle centered at the origin in 2D.

Parameters:

points (Tensor) – Input points to evaluate the SDF, shape [batch, num_points, 2].

Returns:

The SDF values of the unit circle at the input points.

Return type:

Tensor

sdf2d_no_param_rectangle(points)

Computes the SDF for a unit rectangle centered at the origin in 2D.

Parameters:

points (Tensor) – Input points to evaluate the SDF, shape [batch, num_points, 2].

Returns:

The SDF values of the unit rectangle at the input points.

Return type:

Tensor

sdf2d_no_param_triangle(points)

Computes the SDF for an equilateral triangle in 2D.

Parameters:

points (Tensor) – Input points to evaluate the SDF, shape [batch, num_points, 2].

Returns:

The SDF values of the equilateral triangle at the input points.

Return type:

Tensor

sdf2d_oriented_box(points, start_point, end_point, thickness)

Computes the SDF for a 2D oriented box (rotated rectangle).

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • start_point (Tensor) – One corner of the box, shape [batch, 2].

  • end_point (Tensor) – Opposite corner of the box, shape [batch, 2].

  • thickness (Tensor) – Thickness of the box, shape [batch, 1].

Returns:

The SDF values for the oriented box.

Return type:

Tensor

sdf2d_oriented_vesica(points, a, b, w)

Computes the SDF for a 2D oriented vesica piscis.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • a (Tensor) – One center of the vesica piscis, shape [batch, 2].

  • b (Tensor) – Other center of the vesica piscis, shape [batch, 2].

  • w (Tensor) – Width of the region connecting the circles, shape [batch, 1].

Returns:

The SDF values for the oriented vesica piscis.

Return type:

Tensor

sdf2d_parabola(points, k)

Computes the SDF for a 2D parabola.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • k (Tensor) – Parabola coefficient, shape [batch, 1].

Returns:

The SDF values for the parabola.

Return type:

Tensor

sdf2d_parabola_segment(points, wi, he)

Computes the SDF for a segment of a 2D parabola.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • wi (Tensor) – Width of the parabola segment, shape [batch, 1].

  • he (Tensor) – Height of the parabola segment, shape [batch, 1].

Returns:

The SDF values for the parabola segment.

Return type:

Tensor

sdf2d_parallelogram(points, width, height, skew)

Computes the SDF for a 2D parallelogram.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • width (Tensor) – Width of the parallelogram, shape [batch, 1].

  • height (Tensor) – Height of the parallelogram, shape [batch, 1].

  • skew (Tensor) – Skew factor, shape [batch, 1].

Returns:

The SDF values for the parallelogram.

Return type:

Tensor

sdf2d_pie(points, c, r)

Computes the SDF for a 2D pie (circular sector).

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • c (Tensor) – Central angle of the sector, shape [batch, 2].

  • r (Tensor) – Radius of the pie, shape [batch, 1].

Returns:

The SDF values for the pie.

Return type:

Tensor

sdf2d_polygon(points, verts)

Computes the SDF for a general 2D polygon.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • verts (Tensor) – Vertices of the polygon, shape [batch, num_vertices, 2].

Returns:

The SDF values for the polygon.

Return type:

Tensor

sdf2d_quadratic_bezier_curve(points, A, B, C)

Computes the SDF for a quadratic Bezier curve in 2D.

Parameters:
  • points (Tensor) – Input points to evaluate the SDF, shape [batch, num_points, 2].

  • A (Tensor) – Start point of the Bezier curve, shape [batch, 2].

  • B (Tensor) – Control point of the Bezier curve, shape [batch, 2].

  • C (Tensor) – End point of the Bezier curve, shape [batch, 2].

Returns:

The SDF values of the quadratic Bezier curve at the input points.

Return type:

Tensor

sdf2d_quadratic_circle(points)

Computes the signed distance function (SDF) of a quadratic circle in 2D.

Parameters:

points (Tensor) – Input points to evaluate the SDF, shape [batch, num_points, 2].

Returns:

The SDF values of the quadratic circle at the input points.

Return type:

Tensor

sdf2d_regular_hexagon(points, r)

Computes the SDF for a 2D regular hexagon.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • r (Tensor) – Radius of the circumscribing circle of the hexagon, shape [batch, 1].

Returns:

The SDF values for the regular hexagon.

Return type:

Tensor

sdf2d_regular_octagon(points, r)

Computes the SDF for a 2D regular octagon.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • r (Tensor) – Radius of the circumscribing circle of the octagon, shape [batch, 1].

Returns:

The SDF values for the regular octagon.

Return type:

Tensor

sdf2d_regular_pentagon(points, r)

Computes the SDF for a 2D regular pentagon.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • r (Tensor) – Radius of the circumscribing circle of the pentagon, shape [batch, 1].

Returns:

The SDF values for the regular pentagon.

Return type:

Tensor

sdf2d_regular_star(points, r, n, m)

Computes the SDF for a 2D regular star with specified points and symmetry.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • r (Tensor) – Radius of the star, shape [batch, 1].

  • n (Tensor) – Number of points of the star, shape [batch, 1].

  • m (Tensor) – Symmetry factor of the star, shape [batch, 1].

Returns:

The SDF values for the regular star.

Return type:

Tensor

sdf2d_rhombus(points, size)

Computes the SDF for a 2D rhombus (diamond shape).

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • size (Tensor) – Dimensions of the rhombus, shape [batch, 2].

Returns:

The SDF values for the rhombus.

Return type:

Tensor

sdf2d_rounded_box(points, bounds, radius)

Computes the SDF for a 2D rounded box (rectangle with rounded corners).

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • bounds (Tensor) – Dimensions of the box, shape [batch, 2].

  • radius (Tensor) – Radii of the rounded corners, shape [batch, 4].

Returns:

The SDF values for the rounded box.

Return type:

Tensor

sdf2d_rounded_cross(points, h)

Computes the SDF for a 2D rounded cross.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • h (Tensor) – Height of the arms of the cross, shape [batch, 1].

Returns:

The SDF values for the rounded cross.

Return type:

Tensor

sdf2d_rounded_x(points, w, r)

Computes the SDF for a 2D rounded ‘X’ shape.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • w (Tensor) – Width of the arms of the ‘X’, shape [batch, 1].

  • r (Tensor) – Radius of the rounding at the intersections, shape [batch, 1].

Returns:

The SDF values for the rounded ‘X’.

Return type:

Tensor

sdf2d_segment(points, start_point, end_point)

Computes the SDF for a line segment in 2D.

Parameters:
  • points (Tensor) – Input points to evaluate the SDF, shape [batch, num_points, 2].

  • start_point (Tensor) – Start point of the line segment, shape [batch, 2].

  • end_point (Tensor) – End point of the line segment, shape [batch, 2].

Returns:

The SDF values of the line segment at the input points.

Return type:

Tensor

sdf2d_stairs(p, wh, n)

Computes the SDF for a 2D staircase.

Parameters:
  • p (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • wh (Tensor) – Width and height of each stair step, shape [batch, 2].

  • n (Tensor) – Number of steps, shape [batch, 1].

Returns:

The SDF values for the staircase.

Return type:

Tensor

sdf2d_star_5(points, r, rf)

Computes the SDF for a 2D five-pointed star.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • r (Tensor) – Outer radius of the star, shape [batch, 1].

  • rf (Tensor) – Factor to control the inner radius, shape [batch, 1].

Returns:

The SDF values for the five-pointed star.

Return type:

Tensor

sdf2d_trapezoid(points, r1, r2, height)

Computes the SDF for a 2D trapezoid.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • r1 (Tensor) – Radius at one end, shape [batch, 1].

  • r2 (Tensor) – Radius at the other end, shape [batch, 1].

  • height (Tensor) – Height of the trapezoid, shape [batch, 1].

Returns:

The SDF values for the trapezoid.

Return type:

Tensor

sdf2d_triangle(points, p0, p1, p2)

Computes the SDF for a general 2D triangle specified by three points.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • p0 (Tensor) – First vertex of the triangle, shape [batch, 2].

  • p1 (Tensor) – Second vertex of the triangle, shape [batch, 2].

  • p2 (Tensor) – Third vertex of the triangle, shape [batch, 2].

Returns:

The SDF values for the triangle.

Return type:

Tensor

sdf2d_tunnel(points, wh)

Computes the SDF for a 2D tunnel shape.

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • wh (Tensor) – Width and height of the tunnel, shape [batch, 2].

Returns:

The SDF values for the tunnel.

Return type:

Tensor

sdf2d_uneven_capsule(points, r1, r2, h)

Computes the SDF for a 2D uneven capsule (cylinder with hemispherical ends of different radii).

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • r1 (Tensor) – Radius of one hemispherical end, shape [batch, 1].

  • r2 (Tensor) – Radius of the other hemispherical end, shape [batch, 1].

  • h (Tensor) – Height of the capsule, shape [batch, 1].

Returns:

The SDF values for the uneven capsule.

Return type:

Tensor

sdf2d_vesica(points, r, d)

Computes the SDF for a 2D vesica piscis shape (intersection of two circles).

Parameters:
  • points (Tensor) – Coordinates for evaluation, shape [batch, num_points, 2].

  • r (Tensor) – Radius of each circle, shape [batch, 1].

  • d (Tensor) – Distance between the centers of the circles, shape [batch, 1].

Returns:

The SDF values for the vesica piscis.

Return type:

Tensor

torch_compute.sdf_functions_3d module

sdf3d_arbitrary_capped_cone(points, a, b, ra, rb)

Calculates the signed distance from 3D points to the surface of an arbitrarily oriented capped cone.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • a (torch.Tensor) – A tensor of shape [batch, 3] representing the apex of the cone.

  • b (torch.Tensor) – A tensor of shape [batch, 3] representing the base center of the cone.

  • ra (torch.Tensor) – A tensor of shape [batch, 1] representing the radius at the apex of the cone.

  • rb (torch.Tensor) – A tensor of shape [batch, 1] representing the radius at the base of the cone.

Returns:

A tensor containing the signed distances of each point to the arbitrarily oriented capped cone surface.

Return type:

torch.Tensor

sdf3d_arbitrary_capped_cylinder(points, a, b, r)

Calculates the signed distance from 3D points to the surface of an arbitrarily oriented capped cylinder.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • a (torch.Tensor) – A tensor of shape [batch, 3] representing one end of the cylinder.

  • b (torch.Tensor) – A tensor of shape [batch, 3] representing the other end of the cylinder.

  • r (torch.Tensor) – A tensor of shape [batch, 1] representing the radius of the cylinder.

Returns:

A tensor containing the signed distances of each point to the arbitrarily oriented capped cylinder surface.

Return type:

torch.Tensor

sdf3d_arbitrary_round_cone(points, a, b, r1, r2)

Calculates the signed distance from 3D points to the surface of an arbitrarily oriented round cone.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • a (torch.Tensor) – A tensor of shape [batch, 3] representing one end of the cone’s axis.

  • b (torch.Tensor) – A tensor of shape [batch, 3] representing the other end of the cone’s axis.

  • r1 (torch.Tensor) – A tensor of shape [batch, 1] representing the radius at one end of the cone.

  • r2 (torch.Tensor) – A tensor of shape [batch, 1] representing the radius at the other end of the cone.

Returns:

A tensor containing the signed distances of each point to the arbitrarily oriented round cone surface.

Return type:

torch.Tensor

sdf3d_box(points, size)

Calculates the signed distance from 3D points to the surface of an axis-aligned 3D box.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • size (torch.Tensor) – A tensor of shape [batch, 3] representing the half extents of the box.

Returns:

A tensor containing the signed distances of each point to the box surface.

Return type:

torch.Tensor

sdf3d_box_frame(points, b, e)

Calculates the signed distance from 3D points to the surface of a box frame.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • b (torch.Tensor) – A tensor of shape [batch, 3] representing the half extents of the box frame.

  • e (torch.Tensor) – A tensor of shape [batch, 1] representing the frame thickness.

Returns:

A tensor containing the signed distances of each point to the box frame surface.

Return type:

torch.Tensor

sdf3d_capped_cone(points, r1, r2, h)

Calculates the signed distance from 3D points to the surface of a capped cone.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • r1 (torch.Tensor) – A tensor of shape [batch, 1] representing the radius at the base of the cone.

  • r2 (torch.Tensor) – A tensor of shape [batch, 1] representing the radius at the top of the cone.

  • h (torch.Tensor) – A tensor of shape [batch, 1] representing the height of the cone.

Returns:

A tensor containing the signed distances of each point to the capped cone surface.

Return type:

torch.Tensor

sdf3d_capped_cylinder(points, h, r)

Calculates the signed distance from 3D points to the surface of a capped cylinder (aligned along the z-axis).

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • h (torch.Tensor) – A tensor of shape [batch, 1] representing the height of the cylinder.

  • r (torch.Tensor) – A tensor of shape [batch, 1] representing the radius of the cylinder.

Returns:

A tensor containing the signed distances of each point to the capped cylinder surface.

Return type:

torch.Tensor

sdf3d_capped_torus(points, angle, ra, rb)

Calculates the signed distance from 3D points to the surface of a capped torus.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • angle (torch.Tensor) – A tensor of shape [batch, 1] representing the angle of the cap.

  • ra (torch.Tensor) – A tensor of shape [batch, 1] representing the major radius of the torus.

  • rb (torch.Tensor) – A tensor of shape [batch, 1] representing the minor radius of the torus.

Returns:

A tensor containing the signed distances of each point to the capped torus surface.

Return type:

torch.Tensor

sdf3d_capsule(points, a, b, r)

Calculates the signed distance from 3D points to the surface of a capsule.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • a (torch.Tensor) – A tensor of shape [batch, 3] representing one end of the capsule’s centerline.

  • b (torch.Tensor) – A tensor of shape [batch, 3] representing the other end of the capsule’s centerline.

  • r (torch.Tensor) – A tensor of shape [batch, 1] representing the capsule’s radius.

Returns:

A tensor containing the signed distances of each point to the capsule surface.

Return type:

torch.Tensor

sdf3d_cone(points, angle, h)

Calculates the signed distance from 3D points to the surface of a cone.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • angle (torch.Tensor) – A tensor of shape [batch, 1] representing the cone’s angle.

  • h (torch.Tensor) – A tensor of shape [batch, 1] representing the cone’s height.

Returns:

A tensor containing the signed distances of each point to the cone surface.

Return type:

torch.Tensor

sdf3d_cut_hollow_sphere(points, r, h, t)

Calculates the signed distance from 3D points to the surface of a cut hollow sphere.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • r (torch.Tensor) – A tensor of shape [batch, 1] representing the outer radius of the hollow sphere.

  • h (torch.Tensor) – A tensor of shape [batch, 1] representing the height of the cut from the sphere’s base.

  • t (torch.Tensor) – A tensor of shape [batch, 1] representing the thickness of the hollow sphere.

Returns:

A tensor containing the signed distances of each point to the cut hollow sphere surface.

Return type:

torch.Tensor

sdf3d_cut_sphere(points, r, h)

Calculates the signed distance from 3D points to a cut sphere (a sphere with a cap removed).

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • r (torch.Tensor) – A tensor of shape [batch, 1] representing the radius of the sphere.

  • h (torch.Tensor) – A tensor of shape [batch, 1] representing the height of the cut from the sphere’s base.

Returns:

A tensor containing the signed distances of each point to the cut sphere surface.

Return type:

torch.Tensor

sdf3d_death_star(points, ra, rb, d)

Calculates the signed distance from 3D points to the surface of a ‘Death Star’-like shape.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • ra (torch.Tensor) – A tensor of shape [batch, 1] representing the outer radius of the main sphere.

  • rb (torch.Tensor) – A tensor of shape [batch, 1] representing the radius of the indented sphere (dish).

  • d (torch.Tensor) – A tensor of shape [batch, 1] representing the distance of the dish’s center from the main sphere’s center.

Returns:

A tensor containing the signed distances of each point to the ‘Death Star’ surface.

Return type:

torch.Tensor

sdf3d_hex_prism(points, h)

Calculates the signed distance from 3D points to the surface of a hexagonal prism.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • h (torch.Tensor) – A tensor of shape [batch, 2] representing the prism’s height and width.

Returns:

A tensor containing the signed distances of each point to the hexagonal prism surface.

Return type:

torch.Tensor

sdf3d_inexact_anisotropic_gaussian(points, center, axial_radii, scale_constant)

Computes an inexact signed distance field (SDF) for an anisotropic Gaussian shape in 3D. Reference: https://arxiv.org/pdf/1904.06447.pdf :param points: A tensor of shape [batch, num_points, 3] representing 3D points. :type points: torch.Tensor :param center: A tensor of shape [batch, 3] representing the center of the Gaussian distribution. :type center: torch.Tensor :param axial_radii: A tensor of shape [batch, 3] representing the axial radii of the Gaussian distribution. :type axial_radii: torch.Tensor :param scale_constant: A tensor of shape [batch, 1] representing the scale constant of the Gaussian distribution. :type scale_constant: torch.Tensor

Returns:

A tensor containing the signed distances of each point to the anisotropic Gaussian surface.

Return type:

torch.Tensor

sdf3d_inexact_cone(points, angle, h)

Calculates an approximate signed distance from 3D points to the surface of a cone.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • angle (torch.Tensor) – A tensor of shape [batch, 1] representing the cone’s angle.

  • h (torch.Tensor) – A tensor of shape [batch, 1] representing the cone’s height.

Returns:

A tensor containing the approximate signed distances of each point to the cone surface.

Return type:

torch.Tensor

sdf3d_inexact_ellipsoid(points, r)

Calculates an approximate signed distance from 3D points to the surface of an ellipsoid.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • r (torch.Tensor) – A tensor of shape [batch, 3] representing the radii of the ellipsoid along each axis.

Returns:

A tensor containing the approximate signed distances of each point to the ellipsoid surface.

Return type:

torch.Tensor

sdf3d_inexact_octahedron(points, s)

Computes an inexact signed distance field (SDF) for an octahedron shape in 3D.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • s (torch.Tensor) – A tensor of shape [batch, 1] representing the side length of the octahedron.

Returns:

A tensor containing the signed distances of each point to the octahedron surface.

Return type:

torch.Tensor

sdf3d_inexact_super_quadrics(points, skew_vec, epsilon_1, epsilon_2)

Computes an inexact signed distance field (SDF) for superquadric shapes in 3D. Reference: https://arxiv.org/pdf/2303.13190.pdf

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • skew_vec (torch.Tensor) – A tensor of shape [batch, 3] representing the skew parameters of the shape.

  • epsilon_1 (torch.Tensor) – A tensor of shape [batch, 1] representing the first epsilon parameter.

  • epsilon_2 (torch.Tensor) – A tensor of shape [batch, 1] representing the second epsilon parameter.

Returns:

A tensor containing the signed distances of each point to the superquadric surface.

Return type:

torch.Tensor

sdf3d_infinite_cone(points, angle)

Calculates the signed distance from 3D points to the surface of an infinite cone.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • angle (torch.Tensor) – A tensor of shape [batch, 1] representing the cone’s angle.

Returns:

A tensor containing the signed distances of each point to the infinite cone surface.

Return type:

torch.Tensor

sdf3d_infinite_cylinder(points, c)

Calculates the signed distance from 3D points to the surface of an infinite cylinder.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • c (torch.Tensor) – A tensor of shape [batch, 3] representing the cylinder’s center and radius (last component).

Returns:

A tensor containing the signed distances of each point to the infinite cylinder surface.

Return type:

torch.Tensor

Calculates the signed distance from 3D points to the surface of a link shape.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • le (torch.Tensor) – A tensor of shape [batch, 1] representing the length of the link’s straight section.

  • r1 (torch.Tensor) – A tensor of shape [batch, 1] representing the radius of the link’s first circular section.

  • r2 (torch.Tensor) – A tensor of shape [batch, 1] representing the radius of the link’s second circular section.

Returns:

A tensor containing the signed distances of each point to the link shape surface.

Return type:

torch.Tensor

sdf3d_no_param_cuboid(points)

Computes the signed distance field (SDF) for a cuboid shape with no additional parameters in 3D.

Parameters:

points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

Returns:

A tensor containing the signed distances of each point to the cuboid surface.

Return type:

torch.Tensor

sdf3d_no_param_cylinder(points)

Computes the signed distance field (SDF) for a cylinder shape with no additional parameters in 3D.

Parameters:

points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

Returns:

A tensor containing the signed distances of each point to the cylinder surface.

Return type:

torch.Tensor

sdf3d_no_param_sphere(points)

Computes the signed distance field (SDF) for a sphere shape with no additional parameters in 3D.

Parameters:

points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

Returns:

A tensor containing the signed distances of each point to the sphere surface.

Return type:

torch.Tensor

sdf3d_octahedron(points, s)

Computes the signed distance field (SDF) for an octahedron shape in 3D.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • s (torch.Tensor) – A tensor of shape [batch, 1] representing the side length of the octahedron.

Returns:

A tensor containing the signed distances of each point to the octahedron surface.

Return type:

torch.Tensor

sdf3d_plane(points, n, h)

Calculates the signed distance from 3D points to a plane.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • n (torch.Tensor) – A tensor of shape [batch, 3] representing the plane’s normal vector.

  • h (torch.Tensor) – A tensor of shape [batch, 1] representing the plane’s offset from the origin.

Returns:

A tensor containing the signed distances of each point to the plane.

Return type:

torch.Tensor

sdf3d_pyramid(points, h)

Computes the signed distance field (SDF) for a pyramid shape in 3D.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • h (torch.Tensor) – A tensor of shape [batch, 1] representing the height of the pyramid.

Returns:

A tensor containing the signed distances of each point to the pyramid surface.

Return type:

torch.Tensor

sdf3d_quadrilateral(points, a, b, c, d)

Computes the signed distance field (SDF) for a quadrilateral shape in 3D.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • a (torch.Tensor) – A tensor representing one vertex of the quadrilateral.

  • b (torch.Tensor) – A tensor representing another vertex of the quadrilateral.

  • c (torch.Tensor) – A tensor representing the third vertex of the quadrilateral.

  • d (torch.Tensor) – A tensor representing the fourth vertex of the quadrilateral.

Returns:

A tensor containing the signed distances of each point to the quadrilateral surface.

Return type:

torch.Tensor

sdf3d_revolved_vesica(points, a, b, w)

Calculates the signed distance from 3D points to the surface of a revolved Vesica Piscis shape.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • a (torch.Tensor) – A tensor of shape [batch, 3] representing the center of one sphere in the Vesica Piscis.

  • b (torch.Tensor) – A tensor of shape [batch, 3] representing the center of the other sphere in the Vesica Piscis.

  • w (torch.Tensor) – A tensor of shape [batch, 1] representing the radius of the spheres.

Returns:

A tensor containing the signed distances of each point to the revolved Vesica Piscis surface.

Return type:

torch.Tensor

sdf3d_rhombus(points, la, lb, h, ra)

Computes the signed distance field (SDF) for a rhombus shape in 3D.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • la (torch.Tensor) – A tensor representing the length of the first diagonal of the rhombus.

  • lb (torch.Tensor) – A tensor representing the length of the second diagonal of the rhombus.

  • h (torch.Tensor) – A tensor representing the height of the rhombus from its base.

  • ra (torch.Tensor) – A tensor representing the rounding radius of the rhombus edges.

Returns:

A tensor containing the signed distances of each point to the rhombus surface.

Return type:

torch.Tensor

sdf3d_round_cone(points, r1, r2, h)

Calculates the signed distance from 3D points to the surface of a round cone.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • r1 (torch.Tensor) – A tensor of shape [batch, 1] representing the radius at the base of the cone.

  • r2 (torch.Tensor) – A tensor of shape [batch, 1] representing the radius at the top of the cone.

  • h (torch.Tensor) – A tensor of shape [batch, 1] representing the height of the cone.

Returns:

A tensor containing the signed distances of each point to the round cone surface.

Return type:

torch.Tensor

sdf3d_rounded_box(points, size, radius)

Calculates the signed distance from 3D points to the surface of a rounded axis-aligned 3D box.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • size (torch.Tensor) – A tensor of shape [batch, 3] representing the half extents of the box.

  • radius (torch.Tensor) – A tensor of shape [batch, 1] representing the radius of rounding.

Returns:

A tensor containing the signed distances of each point to the rounded box surface.

Return type:

torch.Tensor

sdf3d_rounded_cylinder(points, ra, rb, h)

Calculates the signed distance from 3D points to the surface of a rounded cylinder.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • ra (torch.Tensor) – A tensor of shape [batch, 1] representing the radius of the cylinder’s circular faces.

  • rb (torch.Tensor) – A tensor of shape [batch, 1] representing the radius of the cylinder’s rounded edges.

  • h (torch.Tensor) – A tensor of shape [batch, 1] representing the height of the cylinder.

Returns:

A tensor containing the signed distances of each point to the rounded cylinder surface.

Return type:

torch.Tensor

sdf3d_solid_angle(points, angle, ra)

Calculates the signed distance from 3D points to a solid angle surface.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • angle (torch.Tensor) – A tensor of shape [batch, 1] representing the angle of the solid angle surface.

  • ra (torch.Tensor) – A tensor of shape [batch, 1] representing the radius of the solid angle surface.

Returns:

A tensor containing the signed distances of each point to the solid angle surface.

Return type:

torch.Tensor

sdf3d_sphere(points, radius)

Calculates the signed distance from 3D points to the surface of a sphere.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • radius (torch.Tensor) – A tensor of shape [batch, 1] representing the radii of spheres.

Returns:

A tensor containing the signed distances of each point to the sphere surface.

Return type:

torch.Tensor

sdf3d_torus(points, t)

Calculates the signed distance from 3D points to the surface of a torus.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • t (torch.Tensor) – A tensor of shape [batch, 2] representing the major (first component) and minor (second component) radii of the torus.

Returns:

A tensor containing the signed distances of each point to the torus surface.

Return type:

torch.Tensor

sdf3d_tri_prism(points, h)

Calculates the signed distance from 3D points to the surface of a triangular prism.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • h (torch.Tensor) – A tensor of shape [batch, 2] representing the prism’s height and base edge length.

Returns:

A tensor containing the signed distances of each point to the triangular prism surface.

Return type:

torch.Tensor

sdf3d_triangle(points, a, b, c)

Computes the signed distance field (SDF) for a triangle shape in 3D.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • a (torch.Tensor) – A tensor of shape [batch, 3] representing one vertex of the triangle.

  • b (torch.Tensor) – A tensor of shape [batch, 3] representing another vertex of the triangle.

  • c (torch.Tensor) – A tensor of shape [batch, 3] representing the third vertex of the triangle.

Returns:

A tensor containing the signed distances of each point to the triangle surface.

Return type:

torch.Tensor

sdf3d_vertical_capped_cylinder(points, h, r)

Calculates the signed distance from 3D points to the surface of a vertically-oriented capped cylinder.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • h (torch.Tensor) – A tensor of shape [batch, 1] representing the height of the cylinder.

  • r (torch.Tensor) – A tensor of shape [batch, 1] representing the radius of the cylinder.

Returns:

A tensor containing the signed distances of each point to the capped cylinder surface.

Return type:

torch.Tensor

sdf3d_vertical_capsule(points, h, r)

Calculates the signed distance from 3D points to the surface of a vertically-aligned capsule.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • h (torch.Tensor) – A tensor of shape [batch, 1] representing the capsule’s height.

  • r (torch.Tensor) – A tensor of shape [batch, 1] representing the capsule’s radius.

Returns:

A tensor containing the signed distances of each point to the vertical capsule surface.

Return type:

torch.Tensor

torch_compute.sdf_functions_higher module

cro(a, b)
dot2(tensor)
linear_curve_1d(points, point1, point2)
perpendicular_vectors(vec, normalize=True)
quadratic_curve_1d(points, param_a, param_b, param_c)
sdf3d_linear_extrude(points, start_point, end_point, theta, line_plane_normal=None)

Projects 3D points onto a line defined by start_point and end_point, then performs a linear extrusion with rotation by angle theta. Optionally, a normal to the line plane can be specified.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • start_point (torch.Tensor) – A tensor of shape [batch, 3] representing the start point of the line.

  • end_point (torch.Tensor) – A tensor of shape [batch, 3] representing the end point of the line.

  • theta (torch.Tensor) – A tensor of shape [batch, 1] representing the angle of rotation in radians.

  • line_plane_normal (torch.Tensor, optional) – A tensor of shape [batch, 3] representing the normal vector to the plane in which the line lies.

Returns:

  • torch.Tensor representing parameterized points after projection and rotation.

  • torch.Tensor representing the scale factor, based on the norm of the line vector.

Return type:

tuple

sdf3d_quadratic_bezier_extrude(points, start_point, control_point, end_point, theta, plane_normal=None)

Extrudes 3D points along a quadratic Bezier curve defined by start_point, control_point, and end_point, and rotates them by angle theta. Optionally, a normal to the plane containing the Bezier curve can be specified.

Based on: https://www.shadertoy.com/view/MlKcDD

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • start_point (torch.Tensor) – A tensor of shape [batch, 3] representing the start point of the Bezier curve.

  • control_point (torch.Tensor) – A tensor of shape [batch, 3] representing the control point of the Bezier curve.

  • end_point (torch.Tensor) – A tensor of shape [batch, 3] representing the end point of the Bezier curve.

  • theta (torch.Tensor) – A tensor of shape [batch, 1] representing the angle of rotation in radians.

  • plane_normal (torch.Tensor, optional) – A tensor of shape [batch, 3] representing the normal vector to the plane in which the Bezier curve lies.

Returns:

  • torch.Tensor representing parameterized points after projection and rotation.

  • torch.Tensor representing the scale factor, approximated based on the lengths of segments of the quadratic Bezier curve.

Return type:

tuple

sdf3d_revolution(points, o)

Converts 3D points into 2D points rotated about the z axis for evaluating the revolution of a 2D expression.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • o (torch.Tensor or float) – The offset from the origin for the revolution, applied in the xy-plane.

Returns:

  • torch.Tensor representing parameterized points after the revolution transformation. The x-component of the resulting points is the radial distance from the z-axis, the y-component is the original z-value, and the z-component is set to one.

  • Scalar value of 0, indicating no scale factor is applied in this transformation.

Return type:

tuple

sdf3d_simple_extrusion(points, h)

Performs a simple extrusion transformation of 3D points along the z-axis. The transformation maintains the xy-plane coordinates and scales the z-coordinate relative to the extrusion height.

Parameters:
  • points (torch.Tensor) – A tensor of shape [batch, num_points, 3] representing 3D points.

  • h (torch.Tensor) – A tensor of shape [batch, 1] representing the height of the extrusion.

Returns:

  • torch.Tensor representing parameterized points after the extrusion transformation. The xy-plane coordinates remain unchanged, while the z-coordinate is scaled relative to the extrusion height.

  • The height of the extrusion (h), representing the scale factor in the z-direction.

Return type:

tuple

torch_compute.sdf_operators module

sdf_complement(sdf_a)

Computes the complement of an SDF.

Parameters:

sdf_a (torch.Tensor) – The SDF for which the complement is calculated.

Returns:

The resulting SDF representing the complement of sdf_a.

Return type:

torch.Tensor

sdf_difference(sdf_a, sdf_b)

Computes the difference between two SDFs (sdf_a - sdf_b).

Parameters:
  • sdf_a (torch.Tensor) – The SDF from which the second SDF is subtracted.

  • sdf_b (torch.Tensor) – The SDF to be subtracted from the first SDF.

Returns:

The resulting SDF representing the difference between sdf_a and sdf_b.

Return type:

torch.Tensor

sdf_dilate(sdf_a, k)

Dilates an SDF by a factor k.

Parameters:
  • sdf_a (torch.Tensor) – The SDF to be dilated.

  • k (float) – The dilation factor.

Returns:

The resulting SDF after dilation.

Return type:

torch.Tensor

sdf_erode(sdf_a, k)

Erodes an SDF by a factor k.

Parameters:
  • sdf_a (torch.Tensor) – The SDF to be eroded.

  • k (float) – The erosion factor.

Returns:

The resulting SDF after erosion.

Return type:

torch.Tensor

sdf_intersection(*args)

Computes the intersection of multiple SDFs. The intersection operation finds the maximum SDF value at each point.

Parameters:

*args (tuple of torch.Tensor) – An arbitrary number of SDF tensors to be intersected.

Returns:

The resulting SDF representing the intersection of the input SDFs.

Return type:

torch.Tensor

sdf_null_op(points)

A null operation for SDFs.

Parameters:

points (torch.Tensor) – A tensor representing points in space.

Returns:

A tensor of zeros with a shape matching the first dimension of the input points.

Return type:

torch.Tensor

sdf_onion(sdf_a, k)

Creates an “onion” effect on an SDF by subtracting a constant k from the absolute value of the SDF.

Parameters:
  • sdf_a (torch.Tensor) – The SDF to be transformed.

  • k (float) – The onion factor.

Returns:

The resulting SDF after applying the onion effect.

Return type:

torch.Tensor

sdf_smooth_difference(sdf_a, sdf_b, k)

Computes a smooth difference between two SDFs using a smoothing parameter k.

Parameters:
  • sdf_a (torch.Tensor) – The first SDF.

  • sdf_b (torch.Tensor) – The second SDF.

  • k (float) – The smoothing parameter.

Returns:

The resulting SDF representing the smooth difference of sdf_a and sdf_b.

Return type:

torch.Tensor

sdf_smooth_intersection(sdf_a, sdf_b, k)

Computes a smooth intersection of two SDFs using a smoothing parameter k.

Parameters:
  • sdf_a (torch.Tensor) – The first SDF.

  • sdf_b (torch.Tensor) – The second SDF.

  • k (float) – The smoothing parameter.

Returns:

The resulting SDF representing the smooth intersection of sdf_a and sdf_b.

Return type:

torch.Tensor

sdf_smooth_union(sdf_a, sdf_b, k)

Computes a smooth union of two SDFs using a smoothing parameter k.

Parameters:
  • sdf_a (torch.Tensor) – The first SDF.

  • sdf_b (torch.Tensor) – The second SDF.

  • k (float) – The smoothing parameter.

Returns:

The resulting SDF representing the smooth union of sdf_a and sdf_b.

Return type:

torch.Tensor

sdf_switched_difference(sdf_a, sdf_b)

Computes the difference between two SDFs (sdf_b - sdf_a), opposite of sdf_difference.

Parameters:
  • sdf_a (torch.Tensor) – The SDF to be subtracted from the second SDF.

  • sdf_b (torch.Tensor) – The SDF from which the first SDF is subtracted.

Returns:

The resulting SDF representing the difference between sdf_b and sdf_a.

Return type:

torch.Tensor

sdf_union(*args)

Computes the union of multiple SDFs. The union operation finds the minimum SDF value at each point.

Parameters:

*args (tuple of torch.Tensor) – An arbitrary number of SDF tensors to be united.

Returns:

The resulting SDF representing the union of the input SDFs.

Return type:

torch.Tensor

torch_compute.sketcher module

class Sketcher(device: str = 'cuda', dtype: dtype = torch.float32, resolution: int = 64, mode: str = 'direct', n_dims: int = 3, coord_scale: float = 1.0)

Bases: object

The Sketcher class provides a framework for creating and manipulating a grid of coordinates in a specified number of dimensions. It supports operations such as scaling, translating, and generating homogenous coordinates. This class is designed to work with PyTorch tensors.

device

The PyTorch device (e.g., CUDA or CPU) used for tensor operations.

Type:

th.device

dtype

The data type for PyTorch tensors (e.g., float32).

Type:

th.dtype

resolution

The resolution of the coordinate grid.

Type:

int

mode

The mode of operation (currently unused).

Type:

str

n_dims

The number of dimensions for the coordinates.

Type:

int

coord_scale

The scale factor applied to the coordinates.

Type:

float

homogeneous_identity

An identity matrix in homogeneous coordinates.

Type:

th.Tensor

zero_mat

A zero matrix of size n_dims x n_dims.

Type:

th.Tensor

coords

The coordinate grid tensor.

Type:

th.Tensor

scale_identity

An identity scale tensor.

Type:

th.Tensor

translate_identity

An identity translate tensor.

Type:

th.Tensor

create_coords()
empty_sdf()
get_affine_identity()

Return an identity affine matrix.

get_base_coords()
get_color_canvas()
get_coords(transform, points)
get_homogenous_coords()
get_scale_identity()

Return an identity scale matrix.

get_translate_identity()

Return an identity translate matrix.

make_homogenous_coords(coords)

torch_compute.sphere_marcher module

class Renderer(resolution=(512, 512), num_iterations=2000, convergence_threshold=0.001, camera_params=None, light_setting=None, recursive_evaluator=True, compile_expression=False, device='cuda', dtype=torch.float32)

Bases: object

The Renderer class is designed to render 3D scenes based on provided signed distance function (SDF) expressions. It supports various rendering settings including camera parameters, lighting settings, and material properties. The class can handle both standard and recursive SDF evaluation and offers options for compiling expressions to reduce the rendering time.

dtype

The data type for PyTorch tensors.

Type:

th.dtype

device

The device (e.g., “cuda”, “cpu”) used for tensor operations.

Type:

str

resolution

The resolution of the rendered image.

Type:

tuple

camera_params

Parameters defining the camera settings.

Type:

dict

light_setting

Settings for the lighting conditions in the scene.

Type:

dict

num_iterations

Number of iterations for the rendering loop.

Type:

int

convergence_threshold

Threshold for convergence in the rendering process.

Type:

float

sketcher

An instance of the Sketcher class for coordinate manipulation.

Type:

Sketcher

recursive_evaluator

Flag to use recursive evaluation of SDFs.

Type:

bool

secondary_sketcher

Secondary Sketcher instance for 2D coordinates.

Type:

Sketcher or None

compile_expression

Flag to compile the SDF expressions for performance.

Type:

bool

create_ground_expr()
get_camera_matrix(resolution, f, *args, **kwargs)
get_camera_position(distance, azimuth, elevation, *args, **kwargs)
get_camera_rotation(camera_position, target_position, up_direction, *args, **kwargs)
get_rays(camera_position)
make_random_material()
render(sdf_expression, ground_expression=None, material_setting=None, finite_difference_epsilon=None, convergence_threshold=None, num_iterations=None)

Renders a 3D scene based on the provided signed distance function (SDF) expressions.

Parameters:
  • sdf_expression – The primary SDF expression defining the shapes in the scene.

  • ground_expression (optional) – An additional SDF expression for the ground plane or similar.

  • material_setting (optional) – Settings for the material properties of the rendered objects.

  • finite_difference_epsilon (optional) – The epsilon value used in finite difference calculations for normals.

  • convergence_threshold (optional) – Threshold for convergence in the rendering loop. Defaults to class attribute if None.

  • num_iterations (optional) – Number of iterations for the rendering loop. Defaults to class attribute if None.

Returns:

A tensor representing the rendered image, with shape corresponding to the specified resolution.

compute_normal(sdf_eval_func, surface_positions, finite_difference_epsilon=None)
compute_shadows(sdf_eval_func, surface_positions, surface_normals, light_directions, num_iterations, convergence_threshold, foreground_masks=None, bounding_radius=None)
phong_shading(surface_normals, view_directions, light_directions, light_ambient_color, light_diffuse_color, light_specular_color, material_ambient_color, material_diffuse_color, material_specular_color, material_emission_color, material_shininess)
render_expression(sdf_eval_func, ground_eval_func, ray_positions, ray_directions, camera_position, light_setting, material_setting, num_iterations, convergence_threshold, finite_difference_epsilon=None)

Renders a 3D scene using the provided sdf evaluation functions and rendering settings.

This function implements the rendering pipeline, including sphere tracing for surface detection, normal computation, Phong shading for color calculation, ground and shadow handling, and convergence checks.

Parameters:
  • sdf_eval_func – A function to evaluate the shape’s signed distance function (SDF).

  • ground_eval_func – A function to evaluate the ground SDF.

  • ray_positions – The starting positions of the rays.

  • ray_directions – The directions of the rays.

  • camera_position – The position of the camera.

  • light_setting – Settings for the scene’s lighting conditions.

  • material_setting – Settings for the material properties of the objects.

  • num_iterations (int) – Number of iterations for sphere tracing.

  • convergence_threshold (float) – Threshold for convergence in the sphere tracing.

  • finite_difference_epsilon (optional) – Epsilon value for finite difference in normal computation.

Returns:

A tensor representing the rendered image, with shading and lighting effects applied.

sphere_tracing(sdf_eval_func, ray_positions, ray_directions, num_iterations, convergence_threshold, foreground_masks=None, bounding_radius=None)

torch_compute.transforms module

get_affine_reflection_2D(matrix, param)

Applies a 2D reflection to the given affine transformation matrix based on a line of reflection.

Parameters:
  • matrix (torch.Tensor) – The affine transformation matrix to be modified.

  • param (list or torch.Tensor) – The reflection line vector.

Returns:

The modified affine transformation matrix.

Return type:

torch.Tensor

get_affine_reflection_3D(matrix, param)

Applies a 3D reflection to the given affine transformation matrix based on a plane of reflection.

Parameters:
  • matrix (torch.Tensor) – The affine transformation matrix to be modified.

  • param (list or torch.Tensor) – The reflection plane normal vector.

Returns:

The modified affine transformation matrix.

Return type:

torch.Tensor

get_affine_rotate_2D(matrix, param)

Applies a 2D rotation to the given affine transformation matrix.

Parameters:
  • matrix (torch.Tensor) – The affine transformation matrix to be modified.

  • param (float or torch.Tensor) – The rotation angle in radians.

Returns:

The modified affine transformation matrix.

Return type:

torch.Tensor

get_affine_rotate_euler_3D(matrix, param)

Applies a 3D rotation to the given affine transformation matrix using Euler angles.

Parameters:
  • matrix (torch.Tensor) – The affine transformation matrix to be modified.

  • param (list or torch.Tensor) – The Euler angles for rotation.

Returns:

The modified affine transformation matrix.

Return type:

torch.Tensor

get_affine_scale_2D(matrix, param)

Applies a 2D scaling to the given affine transformation matrix.

Parameters:
  • matrix (torch.Tensor) – The affine transformation matrix to be modified.

  • param (list or torch.Tensor) – The scaling factors [sx, sy].

Returns:

The modified affine transformation matrix.

Return type:

torch.Tensor

get_affine_scale_3D(matrix, param)

Applies a 3D scaling to the given affine transformation matrix.

Parameters:
  • matrix (torch.Tensor) – The affine transformation matrix to be modified.

  • param (list or torch.Tensor) – The scaling factors [sx, sy, sz].

Returns:

The modified affine transformation matrix.

Return type:

torch.Tensor

get_affine_shear_2D(matrix, param)

Applies a 2D shear transformation to the given affine matrix.

Parameters:
  • matrix (torch.Tensor) – The affine transformation matrix to be modified.

  • param (float or torch.Tensor) – The shear factor.

Returns:

The modified affine transformation matrix.

Return type:

torch.Tensor

get_affine_shear_3D(matrix, param)

Applies a 3D shear transformation to the given affine matrix.

Parameters:
  • matrix (torch.Tensor) – The affine transformation matrix to be modified.

  • param (list or torch.Tensor) – The shear factors for each axis.

Returns:

The modified affine transformation matrix.

Return type:

torch.Tensor

get_affine_translate_2D(matrix, param)

Applies a 2D translation to the given affine transformation matrix.

Parameters:
  • matrix (torch.Tensor) – The affine transformation matrix to be modified.

  • param (list or torch.Tensor) – The translation parameters [dx, dy].

Returns:

The modified affine transformation matrix.

Return type:

torch.Tensor

get_affine_translate_3D(matrix, param)

Applies a 3D translation to the given affine transformation matrix.

Parameters:
  • matrix (torch.Tensor) – The affine transformation matrix to be modified.

  • param (list or torch.Tensor) – The translation parameters [dx, dy, dz].

Returns:

The modified affine transformation matrix.

Return type:

torch.Tensor

position_cheap_bend(positions, k)

Applies a bending transformation to the given positions.

Parameters:
  • positions (torch.Tensor) – The original positions.

  • k (float) – The bend magnitude based on the x-coordinate.

Returns:

The bent positions.

Return type:

torch.Tensor

position_distort(positions, k)

Applies a random distortion to the given positions.

Parameters:
  • positions (torch.Tensor) – The original positions.

  • k (float) – The magnitude of the distortion.

Returns:

The distorted positions.

Return type:

torch.Tensor

position_twist(positions, k)

Applies a twisting transformation to the given positions.

Parameters:
  • positions (torch.Tensor) – The original positions.

  • k (float) – The twist magnitude based on the z-coordinate.

Returns:

The twisted positions.

Return type:

torch.Tensor

torch_compute.utils module

torch_compute.visualizer module

get_figure(sdf, input_type='SDF', res=128, point_count=2500, r=1)

Generates a 3D plot of points representing a Signed Distance Function (SDF) or occupancy grid.

This function visualizes the given SDF or occupancy grid as a scatter plot in 3D space. It selects points within the SDF or occupancy grid and displays them with random colors.

Parameters:
  • sdf (torch.Tensor or numpy.ndarray) – The SDF or occupancy grid to be visualized. This should be a 3D tensor or array.

  • input_type (str) – Type of input data. Either “SDF” for Signed Distance Function or any other string for occupancy grid.

  • res (int) – Resolution of the input grid. It’s assumed that the grid is a cube.

  • point_count (int) – The number of points to sample from the SDF or occupancy grid for visualization.

  • r (int) – The size of the markers in the scatter plot.

Returns:

A Plotly Figure object representing the 3D visualization.

Return type:

plotly.graph_objs.Figure