StructArrays.jl icon indicating copy to clipboard operation
StructArrays.jl copied to clipboard

Allow different type for LazyRow index

Open touste opened this issue 5 years ago • 0 comments

Having the possibility to have a different type for the LazyRow index other than I

struct LazyRow{T, N, C, Ic, Ii}
    columns::StructArray{T, N, C, Ic} # a `Columns` object
    index::Ii
end

allows me to define a StructArray of points and a StructArray of triangles, with indexes being of type StaticVectors (a triangle always contains 3 points):

struct Point{T}
    position::SVector{3,T}
    force::SVector{3,T}
end

struct Triangle{Tp}
    points::Tp
end

points = StructArray([Point{Float64}(rand(3), zeros(3)) for _ in 1:10])

tris = StructArray([Triangle(LazyRow(points, @SVector [rand(1:10), rand(1:10), rand(1:10)])) for _ in 1:5])

I can now update the point forces in the triangles this way, which is really helpful in my case:

for t in tris
    pos = t.points.position
    f1 = sum.(pos)
    f2 = tan.(f1)
    f3 = sin.(f1)
    f = Vec3(f1, f2, f3)
    t.points.force += f
end

Does that make sense? Would that make some thing break somewhere?

Thanks

touste avatar Sep 17 '20 15:09 touste