nodes

package
v0.0.0-...-e153092 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2024 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package nodes loads the Blackjack Luau scripts and makes them available for building up BJK AST trees with a simple connection API. See: https://github.com/setzer22/blackjack

Index

Constants

View Source
const (
	// Epsilon is a small number used for comparing float64s.
	Epsilon = 1e-5
)

Variables

View Source
var (
	// GenerateGoldenFilesPrefix is used only to generate testdata files to ensure
	// that Merge experiences no regressions on known, good merges.
	GenerateGoldenFilesPrefix string
)

Functions

func AboutEq

func AboutEq(a, b float64) bool

AboutEq returns true if a is within Epsilon of b.

func GenXform

func GenXform(normal, tangent, tr Vec3) *mat4.T

GenXform represents a rotation (defined by the normal and tangent vectors) about the origin, then translating it by tr into place. func GenXform(normal, tangent, tr Vec3) *Xform {

func Rotation

func Rotation(from, to Vec3) quaternion.T

Rotation calculates the rotation between two normalized vectors. From: https://physicsforgames.blogspot.com/2010/03/quaternion-tricks.html

func Vec3Dot

func Vec3Dot(v1, v2 Vec3) float64

Vec3Dot performs the dot product of v1 x v2 and returns a new vector.

func Vec3RotateAroundAxis

func Vec3RotateAroundAxis(angle float64) func(v1, v2 Vec3) Vec3

Vec3RotateAroundAxis, given an angle in radians, returns a function that operates on two Vec3s to rotate a point around an axis by the given angle.

Types

type Builder

type Builder struct {
	Nodes     map[string]*ast.Node
	NodeOrder []string
	Groups    map[string]*Builder

	ExternalParameters ast.ExternalParameters

	InputsAlreadyConnected map[string]string

	CheckUnusedGroupInputs bool
	// contains filtered or unexported fields
}

Builder represents a BJK builder.

func (*Builder) AddNode

func (b *Builder) AddNode(name string, args ...string) *Builder

AddNode adds a new node to the design with the optional args. A nodes's name always starts with the type of node that it is, whether it is a built-in type like 'MakeScalar' or a new group like 'CoilPair'. The type is followed by a dot ('.') then one or more optional (but recommended) label(s). A node generated from an instance of a Group uses the node's name, a dot, then the group's full name. When referring to inputs or outputs of a node, its full name is followed by a dot then the name of the input or output port. For example, "Helix.wire-1" or "Helix.wire-1.CoilPair.coils-1-2.start_angle".

func (*Builder) Build

func (b *Builder) Build() (*ast.BJK, error)

Builder builds the design and returns the result.

func (*Builder) Connect

func (b *Builder) Connect(from, to string) *Builder

Connect connects the `from` node.output_port to the `to` node.input_port.

func (*Builder) Input

func (b *Builder) Input(inputName, connectTo string) *Builder

Input is used within a group to connect one of its inputs to an internal input. It can only be used within a group.

func (*Builder) MergeMesh

func (b *Builder) MergeMesh(name string) *Builder

MergeMesh creates a new 'MergeMeshes' node if necessary to combine the last mesh with this current mesh. Typically, a design will end with a `MergeMesh` before the call to `Build` such that it is the last (and therefore "active") node in the graph network.

func (*Builder) NewGroup

func (b *Builder) NewGroup(fullName string, fn func(b *Builder) *Builder, args ...string) *Builder

NewGroup creates a new group of nodes and connections that can then later be instantiated one or more times to make a more complex design. If the fullName includes a dot ('.') and an instance name, then the creation of the group is immediately followed by a call to `AddNode` using that same group and instance name. Otherwise, if no dot is included, the `fullName` will be used as the name of the group.

func (*Builder) Output

func (b *Builder) Output(connectFrom, outputName string) *Builder

Output is used within a group to connect one of its outputs to the group output. It can only be used within a group.

type BuilderFunc

type BuilderFunc func(b *Builder) *Builder

BuilderFunc is a func used to build up a design.

type Client

type Client struct {
	Nodes map[string]*ast.Node
	// contains filtered or unexported fields
}

Client represents all the known nodes in Blackjack from its Luau bindings.

func New

func New(blackjackRepoPath string, debug bool) (*Client, error)

New creates a new instance of nodes.Client. blackjackRepoPath is either the absolute path to the Blackjack repo or is the relative-to-$HOME-dir path of the repo.

func (*Client) Close

func (c *Client) Close()

Close closes the current client.

func (*Client) Eval

func (c *Client) Eval(design *ast.BJK) (*Mesh, error)

Eval "evaluates" a BJK design using lua and returns a Mesh if one was generated.

func (*Client) GetScalar

func (c *Client) GetScalar(design *ast.BJK, nodeName string) (float64, error)

GetScalar gets the value of a scalar from a design and returns it.

func (*Client) NewBuilder

func (c *Client) NewBuilder() *Builder

NewBuilder returns a new BJK Builder.

func (*Client) ToObj

func (c *Client) ToObj(design *ast.BJK, filename string) error

ToObj "renders" a BJK design to a Wavefront obj file. It always swaps Y and Z because Blackjack is always Y-up and Blender is always Z-up. It also reverses every face order to preserve the normals correctly.

func (*Client) ToSTL

func (c *Client) ToSTL(design *ast.BJK, filename string, swapYZ bool) error

ToSTL "renders" a BJK design to a binary STL file.

type FaceT

type FaceT []VertIndexT

FaceT represents a face and is a slice of vertex indices.

type Mesh

type Mesh struct {
	// Do not manually add to Verts. Use AddVert instead.
	Verts []Vec3

	Normals  []Vec3  // optional - per-vert normals
	Tangents []Vec3  // optional - per-vert tangents
	Faces    []FaceT // optional - when used, Normals and Tangents are unused.
	// contains filtered or unexported fields
}

Mesh represents a mesh of points, edges, and faces.

func NewLine

func NewLine(v1, v2 *Vec3, numSegs int) *Mesh

NewLine creates a new mesh from two points, divided into numSegs.

func NewLineFromPoints

func NewLineFromPoints(pts []Vec3) *Mesh

NewLineFromPoints creates a new mesh with only verts.

func NewMesh

func NewMesh() *Mesh

NewMesh returns a new, empty mesh.

func NewMeshFromExtrudeAlongCurve

func NewMeshFromExtrudeAlongCurve(backbone, crossSection *Mesh, flip int) *Mesh

NewMeshFromExtrudeAlongCurve creates a new mesh by extruding the crossSection along the backbone. Note that extrude along curve in Blackjack does not make a face at the start or end of the curve.

func NewMeshFromLineWithNormals

func NewMeshFromLineWithNormals(points, normals, tangents []Vec3) *Mesh

NewMeshFromLineWithNormals creates a new mesh from points, normals, and tangents.

func NewMeshFromPolygons

func NewMeshFromPolygons(verts []Vec3, faces []FaceT) *Mesh

NewMeshFromPolygons creates a new mesh from points.

func NewPolygonFromPoints

func NewPolygonFromPoints(pts []Vec3) *Mesh

NewPolygonFromPoints creates a new mesh from points.

func ObjStrToMesh

func ObjStrToMesh(objData string) (*Mesh, error)

ObjStrToMesh converts a simple Wavefront obj file (passed as a string) to a Mesh. Note that it only supports the bare minimum verts and faces.

See: https://en.wikipedia.org/wiki/Wavefront_.obj_file

It always swaps Y and Z because Blackjack is always Y-up and Blender is always Z-up. It also reverses every face order to preserve the normals correctly.

func STLToMesh

func STLToMesh(filename string, swapYZ bool) (*Mesh, error)

STLToMesh reads an STL file and returns a new (triangulated) Mesh.

func (*Mesh) AddFace

func (m *Mesh) AddFace(verts []Vec3) FaceT

AddFace adds a face to a mesh and returns its FaceT. Note that some lua code could create a face where an edge has two vert indices are identical. AddFace prevents that from happening.

func (*Mesh) AddVert

func (m *Mesh) AddVert(v Vec3) VertIndexT

AddVert adds a vertex to a mesh (reusing existing vertices if possible) and returns its VertIndexT.

func (*Mesh) CalcFaceNormal

func (m *Mesh) CalcFaceNormal(face FaceT) Vec3

CalcFaceNormal calculates the normal of a face.

func (*Mesh) LerpAlongCurve

func (m *Mesh) LerpAlongCurve(t float64) *Vec3

LerpAlongCurve returns a Vec3 representing the percentage t (0 to 1) along a curve (the points in the mesh). It caches the length of the curve segments upon first use for later speedup.

func (*Mesh) Merge

func (dst *Mesh) Merge(src *Mesh)

Merge merges src into dst for Ops.merge(dst, src).

func (*Mesh) ToLVal

func (m *Mesh) ToLVal(ls *lua.LState) lua.LValue

func (*Mesh) WriteObj

func (m *Mesh) WriteObj(filename string) error

WriteObj writes a mesh to a simple Wavefront obj file, preserving only vertices and faces. It always swaps Y and Z because Blackjack is always Y-up and Blender is always Z-up. It also reverses every face order to preserve the normals correctly.

func (*Mesh) WriteSTL

func (m *Mesh) WriteSTL(filename string, swapYZ bool) error

WriteSTL writes the mesh to a new STL file.

type SelectionExpression

type SelectionExpression string

SelectionExpression represents a selection of vertices, edges, or faces.

type Vec3

type Vec3 struct {
	X float64
	Y float64
	Z float64
}

Vec3 represents a point in 3D space.

func NewVec3

func NewVec3(x, y, z float64) Vec3

NewVec3 returns a new Vec3.

func Vec3Add

func Vec3Add(v1, v2 Vec3) Vec3

Vec3Add adds two vectors and returns a new vector.

func Vec3Cross

func Vec3Cross(v1, v2 Vec3) Vec3

Vec3Cross performs the cross product of v1 x v2 and returns a new vector.

func Vec3Div

func Vec3Div(v1, v2 Vec3) Vec3

Vec3Div multiplies two vectors (element by element) and returns a new vector.

func Vec3Mul

func Vec3Mul(v1, v2 Vec3) Vec3

Vec3Mul divides two vectors (element by element) and returns a new vector.

func Vec3Sub

func Vec3Sub(v1, v2 Vec3) Vec3

Vec3Sub subtracts vector v2 from vector v1 and returns a new vector.

func (*Vec3) AboutEq

func (v *Vec3) AboutEq(otherVec Vec3) bool

AboutEq returns true if entire vector is within Epsilon of otherVec.

func (*Vec3) AboutZero

func (v *Vec3) AboutZero() bool

AboutZero returns true if entire vector is within Epsilon of (0,0,0)

func (Vec3) Add

func (v1 Vec3) Add(v2 Vec3) Vec3

Add adds two vectors and returns a new vector.

func (Vec3) Cross

func (v1 Vec3) Cross(v2 Vec3) Vec3

Cross returns the cross product of v1 x v2 and returns a new vector.

func (Vec3) Length

func (v Vec3) Length() float64

Length calculates the length of the vector.

func (Vec3) MulScalar

func (v Vec3) MulScalar(f float64) Vec3

MulScalar multiplies a Vec3 by a scalar value and returns a new vector. v is not altered.

func (Vec3) Negated

func (v Vec3) Negated() Vec3

Negated returns the opposite (negated) vector of v.

func (*Vec3) Normalize

func (v *Vec3) Normalize()

Normalize normalizes a vector in-place.

func (Vec3) Normalized

func (v Vec3) Normalized() Vec3

Normalized returns a new normalized Vec3.

func (Vec3) String

func (v Vec3) String() string

func (Vec3) Sub

func (v1 Vec3) Sub(v2 Vec3) Vec3

Sub subtracts vector v2 from vector v1 and returns a new vector.

func (Vec3) Xform

func (v Vec3) Xform(xform *mat4.T) Vec3

Xform applies the 4x4 transformation matrix to the provided vector and returns the result.

type VertIndexT

type VertIndexT int

VertIndexT represents a vertex index.

Jump to

Keyboard shortcuts

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