toolbox3d

package
v0.2.18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2021 License: BSD-2-Clause Imports: 9 Imported by: 5

Documentation

Overview

Package toolbox3d provides a collection of parts for building practical 3D models for 3D prints.

Index

Constants

View Source
const (
	AxisX = 0
	AxisY = 1
	AxisZ = 2
)
View Source
const (
	DefaultSqueezeRatio = 0.1
	DefaultPinchPower   = 0.25
)

Variables

This section is empty.

Functions

func HeightMapToSolid added in v0.2.8

func HeightMapToSolid(hm *HeightMap) model3d.Solid

HeightMapToSolid creates a 3D solid representing the volume under a height map and above the Z plane.

func HeightMapToSolidBidir added in v0.2.8

func HeightMapToSolidBidir(hm *HeightMap) model3d.Solid

HeightMapToSolidBidir is like HeightMapToSolid, but it mirrors the solid across the Z plane to make it symmetric.

func L1LineJoin added in v0.2.13

func L1LineJoin(r float64, lines ...model3d.Segment) model3d.Solid

L1LineJoin is like LineJoin, but uses L1 distance.

func LineJoin added in v0.2.11

func LineJoin(r float64, lines ...model3d.Segment) model3d.Solid

LineJoin creates a Solid containing all points within a distance d of any line segments in a list.

func Teardrop3D added in v0.2.8

func Teardrop3D(p1, p2 model3d.Coord3D, radius float64) model3d.Solid

Teardrop3D creates a 3D teardrop by extending a profile Teardrop2D between p1 and p2.

If possible, the point of the teardrop will be facing into the positive Z direction to avoid supports.

func TriangularBall added in v0.2.13

func TriangularBall(thickness float64, p model3d.Coord3D) model3d.Solid

TriangularBall creates a solid that is true within a given L1 distance from a point p.

func TriangularLine added in v0.2.13

func TriangularLine(thickness float64, p1, p2 model3d.Coord3D) model3d.Solid

TriangularLine creates a solid that is true within a given L1 distance of a segment, with the exception that it is always false past the endpoints.

To "smooth" the endpoints, use TriangularBall().

func TriangularPolygon added in v0.2.13

func TriangularPolygon(thickness float64, close bool, p ...model3d.Coord3D) model3d.Solid

TriangularPolygon is similar to L1LineJoin, but only adds L1 balls to the connections between segments, rather than to all endpoints.

If close is true, then the last point is connected to the first. Otherwise, the endpoints are "cut off", i.e. points past the endpoints will not be inside the solid, even if they are within thickness distance of the endpoint. To achieve "rounded" tips, use TriangularBall() at the endpoints.

Types

type Axis

type Axis int

type AxisPinch added in v0.2.4

type AxisPinch struct {
	// The axis to pinch along.
	Axis Axis

	// Bounds on the axis to pinch.
	Min float64
	Max float64

	// Power controls how much pinching is performed.
	// A Power of 1 means no pinching.
	// Higher powers perform spatial pinching.
	// Lower powers un-pinch a region, moving coordinates
	// further from the center.
	// Reciprocal powers undo each other.
	Power float64
}

AxisPinch is similar to AxisSqueeze, except that it does not affect space outside of the pinched region.

Coordinates within the pinched region are pulled to the center of the region by following some polynomial.

AxisPinch can be used to prevent jagged edges on the tops and bottoms of marching cubes solids, by pinching the uneven edges into a much flatter surface.

func (*AxisPinch) Apply added in v0.2.4

func (a *AxisPinch) Apply(c model3d.Coord3D) model3d.Coord3D

Apply pinches the coordinate.

func (*AxisPinch) ApplyBounds added in v0.2.4

func (a *AxisPinch) ApplyBounds(min, max model3d.Coord3D) (newMin, newMax model3d.Coord3D)

ApplyBounds pinches the bounds.

func (*AxisPinch) Inverse added in v0.2.4

func (a *AxisPinch) Inverse() model3d.Transform

Inverse creates an AxisPinch that undoes the pinch performed by a.

type AxisSqueeze

type AxisSqueeze struct {
	// The axis to compress.
	Axis Axis

	// Bounds on the axis to compress.
	Min float64
	Max float64

	// This is (new length / old length).
	// For example, if we use a squeeze ratio of 0.1,
	// then squeezing 2 inches will bring it down to
	// 0.2 inches.
	Ratio float64
}

AxisSqueeze is a coordinate transformation which squeezes some section of space into a much smaller amount of space along some axis.

AxisSqueeze can be used to efficiently produce meshes which are mostly uniform along some axis, for example a tall cylinder.

func (*AxisSqueeze) Apply

Apply squeezes the coordinate.

func (*AxisSqueeze) ApplyBounds

func (a *AxisSqueeze) ApplyBounds(min, max model3d.Coord3D) (newMin, newMax model3d.Coord3D)

ApplyBounds squeezes the bounds.

func (*AxisSqueeze) Inverse

func (a *AxisSqueeze) Inverse() model3d.Transform

Inverse creates an AxisSqueeze that undoes the squeeze performed by a.

type Equirect

type Equirect struct {
	// contains filtered or unexported fields
}

An Equirect is an equirectangular bitmap representing colors on a sphere.

It can be used, for example, to aid in implementing a 3D polar function.

func NewEquirect

func NewEquirect(img image.Image) *Equirect

NewEquirect creates an Equirect from an image. It is assumed that the top of the image is north (positive latitude), the bottom of the image is south, the left is west (negative longitude), the right is east (positive longitude).

func (*Equirect) At

func (e *Equirect) At(g model3d.GeoCoord) color.Color

At gets the color at the given GeoCoord.

type GearProfile

type GearProfile interface {
	model2d.Solid
	PitchRadius() float64
}

func InvoluteGearProfile

func InvoluteGearProfile(pressureAngle, module, clearance float64, numTeeth int) GearProfile

InvoluteGearProfile creates a GearProfile for a standard involute gear with the given specs.

func InvoluteGearProfileSizes

func InvoluteGearProfileSizes(pressureAngle, module, addendum, dedendum float64,
	numTeeth int) GearProfile

InvoluteGearProfileSizes creates an involute gear profile using different parameters than InvoluteGearProfile.

type GridSearch2D added in v0.2.11

type GridSearch2D struct {
	// Number of points to test along x and y axes.
	XStops int
	YStops int

	// Number of times to recursively search around the
	// best point in the grid.
	Recursions int
}

A GridSearch2D implements 2D grid search for minimizing or maximizing 2D functions.

func (*GridSearch2D) MaxSDF added in v0.2.11

func (g *GridSearch2D) MaxSDF(s model2d.SDF) (model2d.Coord, float64)

MaxSDF finds the point with maximal SDF and returns it, along with the SDF value.

func (*GridSearch2D) Maximize added in v0.2.11

func (g *GridSearch2D) Maximize(min, max model2d.Coord,
	f func(model2d.Coord) float64) (model2d.Coord, float64)

Maximize uses grid search to find the maximum value of f within the given bounds.

Returns the point and its function value.

func (*GridSearch2D) Minimize added in v0.2.11

func (g *GridSearch2D) Minimize(min, max model2d.Coord,
	f func(model2d.Coord) float64) (model2d.Coord, float64)

Minimize uses grid search to find the minimum value of f within the given bounds.

Returns the point and its function value.

type HeightMap added in v0.2.8

type HeightMap struct {
	// 2D boundaries of the grid.
	Min model2d.Coord
	Max model2d.Coord

	// Spacing of the grid elements.
	Delta float64

	// Row-major data storing the squared heights at every
	// grid element.
	Rows int
	Cols int
	Data []float64
}

A HeightMap maps a 2D grid of points to non-negative Z values.

The HeightMap can be updated by adding hemispheres and other HeightMaps. These operations are union operators, in that they never reduce the height at any given grid point.

The HeightMap automatically performs interpolation for reads to provide the appearance of a continuous curve.

func NewHeightMap added in v0.2.8

func NewHeightMap(min, max model2d.Coord, maxSize int) *HeightMap

NewHeightMap fills a rectangular 2D region with a height map that starts out at zero height.

The maxSize argument limits the number of rows and columns, and will be the greater of the two dimensions in the data grid.

func (*HeightMap) AddHeightMap added in v0.2.8

func (h *HeightMap) AddHeightMap(h1 *HeightMap) bool

AddHeightMap writes the union of h and h1 to h.

This is optimized for the case when h and h1 are laid out exactly the same, with the same grid spacing and boundaries.

One use case for this API is to combine multiple height maps that were generated on different Goroutines.

Returns true if h1 modified h, or false otherwise.

func (*HeightMap) AddSphere added in v0.2.8

func (h *HeightMap) AddSphere(center model2d.Coord, radius float64) bool

AddSphere adds a hemisphere to the height map, updating any cells that were lower than the corresponding point on the hemisphere.

Returns true if the sphere changed the height map in any way, or false if the sphere was already covered.

func (*HeightMap) AddSphereFill added in v0.2.11

func (h *HeightMap) AddSphereFill(center model2d.Coord, radius, sphereRadius float64) bool

AddSphereFill fills a circle with spheres of a bounded radius.

The sphereRadius argument determines the maximum radius for a sphere. The radius argument determines the radius of the circle to fill with spheres.

Returns true if the spheres changed the height map in any way, or false if the spheres were covered.

func (*HeightMap) AddSpheresSDF added in v0.2.11

func (h *HeightMap) AddSpheresSDF(p model2d.PointSDF, numSpheres int, eps, maxRadius float64)

AddSpheresSDF fills a 2D signed distance function with spheres that touch the edges of the SDF. This creates a smooth, 3D version of the 2D model.

The numSpheres argument specifies the number of spheres to sample inside the shape.

The eps argument is a small value used to determine the medial axis. Smaller values are more sensitive to jagged edges of the collider. See model2d.ProjectMedialAxis().

The maxRadius argument, if non-zero, is used to limit the height of the resulting object. See HeightMap.AddSphereFill().

func (*HeightMap) Copy added in v0.2.8

func (h *HeightMap) Copy() *HeightMap

Copy creates a deep copy of h.

func (*HeightMap) HeightSquaredAt added in v0.2.8

func (h *HeightMap) HeightSquaredAt(c model2d.Coord) float64

HeightSquaredAt gets the interpolated square of the height at any coordinate.

The coordinate may be out of bounds.

func (*HeightMap) HigherAt added in v0.2.8

func (h *HeightMap) HigherAt(c model2d.Coord, height float64) bool

HigherAt checks if the height map is higher than a given height at the given 2D coordinate. Returns true if the height map is higher.

The coordinate may be out of bounds.

func (*HeightMap) MaxHeight added in v0.2.8

func (h *HeightMap) MaxHeight() float64

MaxHeight gets the maximum height at any cell in the height map.

func (*HeightMap) Mesh added in v0.2.14

func (h *HeightMap) Mesh() *model3d.Mesh

Mesh generates a solid mesh containing the volume under the height map but above the Z axis.

func (*HeightMap) MeshBidir added in v0.2.14

func (h *HeightMap) MeshBidir() *model3d.Mesh

Mesh generates a mesh for the surface by reflecting it across the Z axis. This is like Mesh(), but with a symmetrical base rather than a flat one.

func (*HeightMap) SetHeightSquaredAt added in v0.2.14

func (h *HeightMap) SetHeightSquaredAt(c model2d.Coord, hs float64)

SetHeightSquaredAt sets the squared height at the index closest to the given coordinate.

type HelicalGear

type HelicalGear struct {
	P1      model3d.Coord3D
	P2      model3d.Coord3D
	Profile GearProfile
	Angle   float64
}

func (*HelicalGear) Contains

func (h *HelicalGear) Contains(c model3d.Coord3D) bool

func (*HelicalGear) Max

func (h *HelicalGear) Max() model3d.Coord3D

func (*HelicalGear) Min

func (h *HelicalGear) Min() model3d.Coord3D

type LineSearch added in v0.2.13

type LineSearch struct {
	// Number of points to test along the input space.
	Stops int

	// Number of times to recursively search around the
	// best point on the line.
	Recursions int
}

A LineSearch implements a 1D line search for minimizing or maximizing 1D functions.

func (*LineSearch) CurveBounds added in v0.2.13

func (l *LineSearch) CurveBounds(min, max float64, f func(float64) model2d.Coord) (model2d.Coord,
	model2d.Coord)

CurveBounds approximates the bounding box of a parametric curve, such as a 2D bezier curve.

The min and max arguments specify the minimum and maximum argument to pass to f, which is typically in the range [0, 1] for Bezier curves.

func (*LineSearch) Maximize added in v0.2.13

func (l *LineSearch) Maximize(min, max float64, f func(float64) float64) (x, fVal float64)

func (*LineSearch) Minimize added in v0.2.13

func (l *LineSearch) Minimize(min, max float64, f func(float64) float64) (x, fVal float64)

type Ramp

type Ramp struct {
	model3d.Solid

	// P1 is the tip of the ramp, where the scale is 0.
	// Any point further in the direction of P1 will have
	// a scale of zero.
	P1 model3d.Coord3D

	// P2 is the base of the ramp, where the scale is 1.
	// Any point further in the direction of P2 will have
	// a scale of one.
	P2 model3d.Coord3D
}

A Ramp wraps an existing solid and gradually increases the scale of the solid from 0% to 100% along a given axis.

This makes it easier to make shapes like upside-down pyramids and cones for use in FDM printing without supports.

func (*Ramp) Contains

func (r *Ramp) Contains(c model3d.Coord3D) bool

type RectSet added in v0.2.11

type RectSet struct {
	// contains filtered or unexported fields
}

A RectSet maintains the set of all points contained in a union of rectangular volumes.

The exact list of original rectangles is not preserved, but the information about the combined solid is.

A RectSet may use up to O(N^3) memory, where N is the number of contained rectangular volumes. In particular, usage is proportional to X*Y*Z, where X, Y and Z are the number of unique x, y, and z coordinates.

func NewRectSet added in v0.2.11

func NewRectSet() *RectSet

NewRectSet creates an empty RectSet.

func (*RectSet) Add added in v0.2.11

func (r *RectSet) Add(rect *model3d.Rect)

Add adds a rectangular volume to the set.

func (*RectSet) AddRectSet added in v0.2.11

func (r *RectSet) AddRectSet(r1 *RectSet)

AddRectSet adds another RectSet's volume to the set.

func (*RectSet) ExactMesh added in v0.2.11

func (r *RectSet) ExactMesh() *model3d.Mesh

ExactMesh creates a 3D mesh from the set of rects.

The returned mesh is not guaranteed to be manifold. For example, it is possible to create two rects that touch at a single vertex or edge, in which case there will be a singularity in the resulting mesh.

To create a manifold mesh, use Mesh().

func (*RectSet) Max added in v0.2.11

func (r *RectSet) Max() model3d.Coord3D

Min gets the maximum of the bounding box containing the set.

func (*RectSet) Mesh added in v0.2.11

func (r *RectSet) Mesh() *model3d.Mesh

Mesh creates a manifold 3D mesh from the set of rects.

func (*RectSet) Min added in v0.2.11

func (r *RectSet) Min() model3d.Coord3D

Min gets the minimum of the bounding box containing the set.

func (*RectSet) Remove added in v0.2.11

func (r *RectSet) Remove(rect *model3d.Rect)

Remove subtracts a rectangular volume from the set.

func (*RectSet) RemoveRectSet added in v0.2.11

func (r *RectSet) RemoveRectSet(r1 *RectSet)

RemoveRectSet subtracts another RectSet's volume from the set.

func (*RectSet) Solid added in v0.2.11

func (r *RectSet) Solid() model3d.Solid

Solid creates a 3D solid from the set.

type ScrewSolid

type ScrewSolid struct {
	// P1 is the center of the start of the screw.
	P1 model3d.Coord3D

	// P2 is the center of the end of the screw.
	P2 model3d.Coord3D

	// Radius is the maximum radius of the screw,
	// including grooves.
	Radius float64

	// GrooveSize is the size of the grooves.
	// This may not exceed Radius.
	GrooveSize float64

	// Pointed can be set to true to indicate that the tip
	// at the P2 end should be cut off at a 45 degree
	// angle (in the shape of a cone).
	// Can be used for internal screw holes to avoid
	// support.
	Pointed bool
}

A ScrewSolid is a model3d.Solid implementation of screws. It can also be used for screw holes, by combining it with model3d.SubtractedSolid.

Screws are similar to cylinders, so many of the fields are analogous to model3d.CylinderSolid.

func (*ScrewSolid) Contains

func (s *ScrewSolid) Contains(c model3d.Coord3D) bool

func (*ScrewSolid) Max

func (s *ScrewSolid) Max() model3d.Coord3D

func (*ScrewSolid) Min

func (s *ScrewSolid) Min() model3d.Coord3D

type SmartSqueeze added in v0.2.7

type SmartSqueeze struct {
	// Axis is the axis to squeeze along.
	Axis Axis

	// SqueezeRatio is the axis squeeze ratio to use.
	SqueezeRatio float64

	// PinchRange specifies how much space should be added
	// before and after a pinch to avoid singularities.
	// Should be small, but larger than the marching cubes
	// epsilon.
	PinchRange float64

	// PinchPower is the power for pinches.
	PinchPower float64

	// Ranges along the axis that cannot be squeezed.
	// Typically ranges should be added via AddUnsqueezable().
	Unsqueezable [][2]float64

	// Values of the axis at which a pinch should be used
	// to flatten plateaus.
	// Typically pinches will be added via AddPinch().
	Pinches []float64
}

SmartSqueeze creates a transformation to squeeze a model along a certain axis without affecting certain regions that don't lend themselves to squeezing.

func NewSmartSqueeze added in v0.2.13

func NewSmartSqueeze(axis Axis, squeezeRatio, pinchRange, pinchPower float64) *SmartSqueeze

NewSmartSqueeze creates a SmartSqueeze with the given parameters, using defaults when necessary.

If squeezeRatio is 0, DefaultSqueezeRatio is used. If pinchPower is 0, DefaultPinchPower is used.

func (*SmartSqueeze) AddPinch added in v0.2.7

func (s *SmartSqueeze) AddPinch(val float64)

AddPinch adds an axis value at which the coordinates should be squeezed to flatten plateaus.

func (*SmartSqueeze) AddUnsqueezable added in v0.2.7

func (s *SmartSqueeze) AddUnsqueezable(min, max float64)

AddUnsqueezable adds a range of axis values in which no squeezing should be performed.

func (*SmartSqueeze) MarchingCubesSearch added in v0.2.7

func (s *SmartSqueeze) MarchingCubesSearch(solid model3d.Solid, delta float64,
	iters int) *model3d.Mesh

MachingCubesSearch uses the smart squeeze to convert a solid into a mesh efficiently.

In particular, the model is transformed, meshified, and then the inverse transformation is applied.

For usage information, see model3d.MarchingCubesSearch.

func (*SmartSqueeze) Transform added in v0.2.7

func (s *SmartSqueeze) Transform(b model3d.Bounder) model3d.Transform

Transform creates a transformation for the squeezes and pinches, given the bounds of a model.

type SpurGear

type SpurGear struct {
	P1      model3d.Coord3D
	P2      model3d.Coord3D
	Profile GearProfile
}

func (*SpurGear) Contains

func (s *SpurGear) Contains(c model3d.Coord3D) bool

func (*SpurGear) Max

func (s *SpurGear) Max() model3d.Coord3D

func (*SpurGear) Min

func (s *SpurGear) Min() model3d.Coord3D

type Teardrop2D added in v0.2.8

type Teardrop2D struct {
	// Center is the center of the circle.
	Center model2d.Coord

	// Radius is the radius of the circle.
	Radius float64

	// Direction is the direction in which the tip of the
	// tangent triangle is pointing.
	// If this is the zero vector, then a unit vector in
	// the Y direction is used.
	Direction model2d.Coord
}

Teardrop2D is a 2D solid in a "teardrop" shape, i.e. a circle with a tangent triangle pointing off in one direction.

This can be used to cut circles out of shapes while avoiding support structures in FDM printing.

The shape, rendered in ASCII, looks like so:

            --(&)--
       @@@@/       (@@@@
    @@@                 @@@
  /@@                     @@.
 @@                         @@
 @@                         @@
&@                           @
 @%                         @@
 @@                         @@
  @@#                     @@@
    @@@                 @@@
      &@@             @@*
         @@&       @@@
           @@@   @@#
              @@@
               ^

func (*Teardrop2D) Contains added in v0.2.8

func (t *Teardrop2D) Contains(c model2d.Coord) bool

func (*Teardrop2D) Max added in v0.2.8

func (t *Teardrop2D) Max() model2d.Coord

func (*Teardrop2D) Min added in v0.2.8

func (t *Teardrop2D) Min() model2d.Coord

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL