Skip to content

Commit 19abef7

Browse files
committed
Vector inverse node, circumference node, randomize array changes, grid fixes
1 parent d38017f commit 19abef7

File tree

7 files changed

+102
-9
lines changed

7 files changed

+102
-9
lines changed

generator/examples/tower.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

math/nodes.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ func init() {
1414

1515
refutil.RegisterType[Round](factory)
1616

17+
refutil.RegisterType[nodes.Struct[CircumferenceNode]](factory)
18+
1719
refutil.RegisterType[nodes.Struct[DifferenceNodeData[int]]](factory)
1820
refutil.RegisterType[nodes.Struct[DifferenceNodeData[float64]]](factory)
1921
refutil.RegisterType[nodes.Struct[DifferencesToArrayNodeData[int]]](factory)
@@ -216,3 +218,27 @@ func (cn RoundNodeData) Float() nodes.StructOutput[float64] {
216218
}
217219
return nodes.NewStructOutput(math.Round(cn.In.Value()))
218220
}
221+
222+
// ============================================================================
223+
224+
type CircumferenceNode struct {
225+
Radius nodes.Output[float64]
226+
}
227+
228+
func (cn CircumferenceNode) Description() string {
229+
return "Circumference of a circle"
230+
}
231+
232+
func (cn CircumferenceNode) Int() nodes.StructOutput[int] {
233+
if cn.Radius == nil {
234+
return nodes.NewStructOutput(0)
235+
}
236+
return nodes.NewStructOutput(int(math.Round(cn.Radius.Value() * 2 * math.Pi)))
237+
}
238+
239+
func (cn CircumferenceNode) Float() nodes.StructOutput[float64] {
240+
if cn.Radius == nil {
241+
return nodes.NewStructOutput(0.)
242+
}
243+
return nodes.NewStructOutput(math.Round(cn.Radius.Value() * 2 * math.Pi))
244+
}

math/trs/nodes.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,19 @@ type RandomizeArrayNodeData struct {
5959
ScaleMaximum nodes.Output[vector3.Float64]
6060
RotationMinimum nodes.Output[vector3.Float64]
6161
RotationMaximum nodes.Output[vector3.Float64]
62-
Samples nodes.Output[int]
62+
Array nodes.Output[[]TRS]
6363
}
6464

6565
func (tnd RandomizeArrayNodeData) Out() nodes.StructOutput[[]TRS] {
66+
if tnd.Array == nil {
67+
return nodes.NewStructOutput[[]TRS](nil)
68+
}
69+
70+
input := tnd.Array.Value()
71+
if len(input) == 0 {
72+
return nodes.NewStructOutput[[]TRS](nil)
73+
}
74+
6675
minT := nodes.TryGetOutputValue(tnd.TranslationMinimum, vector3.Zero[float64]())
6776
maxT := nodes.TryGetOutputValue(tnd.TranslationMaximum, vector3.Zero[float64]())
6877
rangeT := maxT.Sub(minT)
@@ -75,9 +84,9 @@ func (tnd RandomizeArrayNodeData) Out() nodes.StructOutput[[]TRS] {
7584
maxR := nodes.TryGetOutputValue(tnd.RotationMaximum, vector3.Zero[float64]())
7685
rangeR := maxR.Sub(minR)
7786

78-
samples := make([]TRS, max(nodes.TryGetOutputValue(tnd.Samples, 0), 0))
79-
for i := range samples {
80-
samples[i] = New(
87+
out := make([]TRS, len(input))
88+
for i := range out {
89+
sample := New(
8190
minT.Add(vector3.New(
8291
rangeT.X()*rand.Float64(),
8392
rangeT.Y()*rand.Float64(),
@@ -88,16 +97,16 @@ func (tnd RandomizeArrayNodeData) Out() nodes.StructOutput[[]TRS] {
8897
rangeR.Y()*rand.Float64(),
8998
rangeR.Z()*rand.Float64(),
9099
))),
91-
// quaternion.Identity(),
92100
minS.Add(vector3.New(
93101
rangeS.X()*rand.Float64(),
94102
rangeS.Y()*rand.Float64(),
95103
rangeS.Z()*rand.Float64(),
96104
)),
97105
)
106+
out[i] = input[i].Multiply(sample)
98107
}
99108

100-
return nodes.NewStructOutput(samples)
109+
return nodes.NewStructOutput(out)
101110
}
102111

103112
// ============================================================================

math/vector3/convenience.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,42 @@ func (cn Dot) Dot() nodes.StructOutput[float64] {
8787
func (cn Dot) DotDescription() string {
8888
return "The dot product of A and B. If either value is not set, then 0 is returned"
8989
}
90+
91+
// ============================================================================
92+
93+
type Inverse[T vector.Number] struct {
94+
Vector nodes.Output[vector3.Vector[T]]
95+
}
96+
97+
func (cn Inverse[T]) additive() vector3.Vector[float64] {
98+
if cn.Vector == nil {
99+
return vector3.Zero[float64]()
100+
}
101+
102+
return cn.Vector.Value().ToFloat64().Scale(-1)
103+
}
104+
105+
func (cn Inverse[T]) multiplicative() vector3.Vector[float64] {
106+
if cn.Vector == nil {
107+
return vector3.Zero[float64]()
108+
}
109+
110+
in := cn.Vector.Value().ToFloat64()
111+
return vector3.New(1./in.X(), 1./in.Y(), 1./in.Z())
112+
}
113+
114+
func (cn Inverse[T]) Additive() nodes.StructOutput[vector3.Vector[float64]] {
115+
return nodes.NewStructOutput(cn.additive())
116+
}
117+
118+
func (cn Inverse[T]) AdditiveInt() nodes.StructOutput[vector3.Vector[int]] {
119+
return nodes.NewStructOutput(cn.additive().ToInt())
120+
}
121+
122+
func (cn Inverse[T]) Multiplicative() nodes.StructOutput[vector3.Vector[float64]] {
123+
return nodes.NewStructOutput(cn.multiplicative())
124+
}
125+
126+
func (cn Inverse[T]) MultiplicativeInt() nodes.StructOutput[vector3.Vector[int]] {
127+
return nodes.NewStructOutput(cn.multiplicative().RoundToInt())
128+
}

math/vector3/nodes.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,8 @@ func init() {
5353
refutil.RegisterType[nodes.Struct[DistancesToNodes[float64]]](factory)
5454
refutil.RegisterType[nodes.Struct[DistancesToNodes[int]]](factory)
5555

56+
refutil.RegisterType[nodes.Struct[Inverse[int]]](factory)
57+
refutil.RegisterType[nodes.Struct[Inverse[float64]]](factory)
58+
5659
generator.RegisterTypes(factory)
5760
}

modeling/repeat/grid.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ func (g Grid) Vector2() []vector2.Float64 {
5555
bottomLeft = bottomLeft.SetY(0)
5656
}
5757

58-
for x := range g.Rows {
59-
for y := range g.Columns {
58+
for y := range g.Rows {
59+
for x := range g.Columns {
6060
inc := vector2.New(
6161
widthInc*float64(x),
6262
heightInc*float64(y),

modeling/repeat/grid_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ func TestGrid(t *testing.T) {
3131
rows: 0, column: 1, width: 1, height: 1,
3232
expect: []vector2.Float64{},
3333
},
34+
"vertical line": {
35+
rows: 3, column: 1, width: 1, height: 1,
36+
expect: []vector2.Float64{
37+
vector2.New(0., -0.5),
38+
vector2.New(0., 0.0),
39+
vector2.New(0., 0.5),
40+
},
41+
},
42+
"horizontal line": {
43+
rows: 1, column: 3, width: 1, height: 1,
44+
expect: []vector2.Float64{
45+
vector2.New(-0.5, 0.),
46+
vector2.New(0.0, 0.),
47+
vector2.New(0.5, 0.),
48+
},
49+
},
3450
"4 corners": {
3551
rows: 2, column: 2, width: 1, height: 1,
3652
expect: []vector2.Float64{

0 commit comments

Comments
 (0)