Being new at this I ran into several problems. First some code:
import vmath
type
Seg = array[4, Vec2]
Segs = array[4, Seg]
Spline = tuple[t: seq[float32], xy: seq[Vec2], v: seq[Vec2], a: seq[Vec2], lengt: seq[float32]]
proc bezier(segment: Seg, res: int): Spline =
#var t: seq[float32]
let
t1 = 1 / float32(res)
t2 = t1 * t1
t3 = t2 * t1
b0 = segment[0]
b1 = segment[1]
b2 = segment[2]
b3 = segment[3]
a = -b0 + 3 * b1 + -3 * b2 + b3
b = 3 * b0 + -6 * b1 + 3 * b2
c = -3 * b0 + 3 * b1
var
d = b0
fd1 = a * t3 + b * t2 + c * t1 #initial velocity
fd2 = 6 * a * t3 + 2 * b * t2 #initial acceleration
fd3 = 6 * a * t3 #acceleration change
seqt = @[float32(0.0)]
seqxy = @[d]
seqfd1 = @[fd1]
seqfd2 = @[fd2]
seqlen = @[length(d)]
for i in 1 .. res:
d += fd1
fd1 += fd2 #speed
fd2 += fd3 #acceleration
seqt.add(float32(i)*t1)
seqxy.add(d)
seqfd1.add(fd1)
seqfd2.add(fd2)
seqlen.add(length(d))
let spline: Spline = [t: seqt, xy: seqxy, v: seqfd1, a: seqfd2]
return spline
Error: undeclared identifier t after uncomment t I get: Error: cannot evaluate at compile time: t
Simple question, what to do.
Second question, in python I would use a dictionary to hold the several lists and work directly with that. What would a suitable container be with Nim. I tried tables (can't work with mixed type?), put it all in a seq (gets a bit cumbersome) and tuples so far.
I think you are just having an issue with the named tuple syntax. I don't think you can have the names in the constructor of a tuple. I feel like a tuple is a wrong thing to use here, just create a normal object
Fixed:
import vmath
type
Seg = array[4, Vec2]
Segs = array[4, Seg]
Spline = object
t: seq[float32]
xy: seq[Vec2]
v: seq[Vec2]
a: seq[Vec2]
lengt: seq[float32]
proc bezier(segment: Seg, res: int): Spline =
var t: seq[float32]
let
t1 = 1 / float32(res)
t2 = t1 * t1
t3 = t2 * t1
b0 = segment[0]
b1 = segment[1]
b2 = segment[2]
b3 = segment[3]
a = -b0 + 3 * b1 + -3 * b2 + b3
b = 3 * b0 + -6 * b1 + 3 * b2
c = -3 * b0 + 3 * b1
var
d = b0
fd1 = a * t3 + b * t2 + c * t1 #initial velocity
fd2 = 6 * a * t3 + 2 * b * t2 #initial acceleration
fd3 = 6 * a * t3 #acceleration change
seqt = @[float32(0.0)]
seqxy = @[d]
seqfd1 = @[fd1]
seqfd2 = @[fd2]
seqlen = @[length(d)]
for i in 1 .. res:
d += fd1
fd1 += fd2 #speed
fd2 += fd3 #acceleration
seqt.add(float32(i)*t1)
seqxy.add(d)
seqfd1.add(fd1)
seqfd2.add(fd2)
seqlen.add(length(d))
let spline = Spline(t: seqt, xy: seqxy, v: seqfd1, a: seqfd2)
return spline
Thanks for pointing out the distinction in use of table and object. That helps. In python I would probably do:
spline = {
't': [],
'xy': [],
'v': [],
'a': [],
'length': []
}
and then instead of seqfd1.add(fd1) in the final loop I'd directly use the dict:
spline['v'].append(fd1)
Using objects is the right thing to do here. You can always create a spline object and just:
spline.v.add(fd1)
import vmath
type
Seg = array[4, Vec2]
Spline = object
t: seq[float32]
xy: seq[Vec2]
v: seq[Vec2]
a: seq[Vec2]
length: seq[float32]
proc bezier(segment: Seg, res: int): Spline =
let
t1 = 1 / float32(res)
t2 = t1 * t1
t3 = t2 * t1
b0 = segment[0]
b1 = segment[1]
b2 = segment[2]
b3 = segment[3]
a = -b0 + 3 * b1 + -3 * b2 + b3
b = 3 * b0 + -6 * b1 + 3 * b2
c = -3 * b0 + 3 * b1
var
d = b0
fd1 = a * t3 + b * t2 + c * t1 #initial velocity
fd2 = 6 * a * t3 + 2 * b * t2 #initial acceleration
fd3 = 6 * a * t3 #acceleration change
var spline = Spline()
spline.t = @[float32(0.0)]
spline.xy = @[d]
spline.v = @[fd1]
spline.a = @[fd2]
spline.length = @[length(d)]
for i in 1 .. res:
d += fd1
fd1 += fd2 #speed
fd2 += fd3 #acceleration
spline.t.add(float32(i)*t1)
spline.xy.add(d)
spline.v.add(fd1)
spline.a.add(fd2)
spline.length.add(length(d))
return spline
import vmath
type
Seg = array[4, Vec2]
Spline = object
t: seq[float32]
xy: seq[Vec2]
v: seq[Vec2]
a: seq[Vec2]
length: seq[float32]
proc bezier(segment: Seg, res: int): Spline =
let
t1 = 1 / float32(res)
t2 = t1 * t1
t3 = t2 * t1
b0 = segment[0]
b1 = segment[1]
b2 = segment[2]
b3 = segment[3]
a = -b0 + 3 * b1 + -3 * b2 + b3
b = 3 * b0 + -6 * b1 + 3 * b2
c = -3 * b0 + 3 * b1
var
d = b0
fd1 = a * t3 + b * t2 + c * t1 #initial velocity
fd2 = 6 * a * t3 + 2 * b * t2 #initial acceleration
fd3 = 6 * a * t3 #acceleration change
for i in 0 .. res:
result.t.add(float32(i)*t1)
result.xy.add(d)
result.v.add(fd1)
result.a.add(fd2)
result.length.add(length(d))
d += fd1
fd1 += fd2 #speed
fd2 += fd3 #acceleration
https://peterme.net/tips-and-tricks-with-implicit-return-in-nim.html
Haha, you're going to fast for me :) my programming skills are 'minimal' and mostly POV-Ray SDL so I try to go step by step. I'll study!
Thanks.
In the end I kept it simple: https://gist.github.com/ingoogni/ed87bc92c5d2a2da5038dd368a238ac5
Thanks.