-
-
Notifications
You must be signed in to change notification settings - Fork 59
Primitives in separate folder #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # TODO: define common API for all primitives | ||
|
|
||
| # include implementations | ||
|
juliohm marked this conversation as resolved.
Outdated
|
||
| include("primitives/rectangles.jl") | ||
| include("primitives/spheres.jl") | ||
| include("primitives/cylinders.jl") | ||
| include("primitives/pyramids.jl") | ||
| include("primitives/particles.jl") | ||
|
juliohm marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| """ | ||
| Cylinder{N, T} | ||
|
|
||
| A `Cylinder` is a 2D rectangle or a 3D cylinder defined by its origin point, | ||
| its extremity and a radius. `origin`, `extremity` and `r`, must be specified. | ||
| """ | ||
| struct Cylinder{N, T} <: GeometryPrimitive{N, T} | ||
| origin::Point{N,T} | ||
| extremity::Point{N,T} | ||
| r::T | ||
| end | ||
|
|
||
| """ | ||
| Cylinder2{T} | ||
| Cylinder3{T} | ||
|
|
||
| A `Cylinder2` or `Cylinder3` is a 2D/3D cylinder defined by its origin point, | ||
| its extremity and a radius. `origin`, `extremity` and `r`, must be specified. | ||
| """ | ||
| const Cylinder2{T} = Cylinder{2, T} | ||
| const Cylinder3{T} = Cylinder{3, T} | ||
|
|
||
| origin(c::Cylinder{N, T}) where {N, T} = c.origin | ||
| extremity(c::Cylinder{N, T}) where {N, T} = c.extremity | ||
| radius(c::Cylinder{N, T}) where {N, T} = c.r | ||
| height(c::Cylinder{N, T}) where {N, T} = norm(c.extremity - c.origin) | ||
| direction(c::Cylinder{N, T}) where {N, T} = (c.extremity .- c.origin) ./ height(c) | ||
|
|
||
| function rotation(c::Cylinder{2, T}) where T | ||
| d2 = direction(c); u = @SVector [d2[1], d2[2], T(0)] | ||
| v = @MVector [u[2], -u[1], T(0)] | ||
| normalize!(v) | ||
| return hcat(v, u, @SVector T[0, 0, 1]) | ||
| end | ||
|
|
||
| function rotation(c::Cylinder{3, T}) where T | ||
| d3 = direction(c); u = @SVector [d3[1], d3[2], d3[3]] | ||
| if abs(u[1]) > 0 || abs(u[2]) > 0 | ||
| v = @MVector [u[2], -u[1], T(0)] | ||
| else | ||
| v = @MVector [T(0), -u[3], u[2]] | ||
| end | ||
| normalize!(v) | ||
| w = @SVector [u[2] * v[3] - u[3] * v[2], -u[1] * v[3] + u[3] * v[1], u[1] * v[2] - u[2] * v[1]] | ||
| return hcat(v, w, u) | ||
| end | ||
|
|
||
| function coordinates(c::Cylinder{2, T}, nvertices=(2, 2)) where T | ||
| r = Rect(c.origin[1] - c.r/2, c.origin[2], c.r, height(c)) | ||
| M = rotation(c) | ||
| points = coordinates(r, nvertices) | ||
| vo = to_pointn(Point3{T}, origin(c)) | ||
| return (M * (to_pointn(Point3{T}, point) .- vo) .+ vo for point in points) | ||
| end | ||
|
|
||
| function faces(sphere::Cylinder{2}, nvertices=(2, 2)) | ||
| return faces(Rect(0, 0, 1, 1), nvertices) | ||
| end | ||
|
|
||
| function coordinates(c::Cylinder{3, T}, nvertices=30) where T | ||
| if isodd(nvertices) | ||
| nvertices = 2 * (nvertices ÷ 2) | ||
| end | ||
| nvertices = max(8, nvertices); | ||
| nbv = nvertices ÷ 2 | ||
|
|
||
| M = rotation(c) | ||
| h = height(c) | ||
| range = 1:(2 * nbv + 2) | ||
| function inner(i) | ||
| if i == length(range) | ||
| return c.extremity | ||
| elseif i == length(range) - 1 | ||
| return origin(c) | ||
| else | ||
| phi = T((2π * (((i + 1) ÷ 2) - 1)) / nbv) | ||
| up = ifelse(isodd(i), 0, h) | ||
| return (M * Point(c.r * cos(phi), c.r * sin(phi), up)) .+ c.origin | ||
| end | ||
| end | ||
|
|
||
| return (inner(i) for i in range) | ||
| end | ||
|
|
||
| function faces(c::Cylinder{3}, facets=30) | ||
| isodd(facets) ? facets = 2 * div(facets, 2) : nothing | ||
| facets < 8 ? facets = 8 : nothing; nbv = Int(facets / 2) | ||
| indexes = Vector{TriangleFace{Int}}(undef, facets) | ||
| index = 1 | ||
| for j = 1:(nbv-1) | ||
| indexes[index] = (index + 2, index + 1, index) | ||
| indexes[index + 1] = ( index + 3, index + 1, index + 2) | ||
| index += 2 | ||
| end | ||
| indexes[index] = (1, index + 1, index) | ||
| indexes[index + 1] = (2, index + 1, 1) | ||
|
|
||
| for i = 1:length(indexes) | ||
| i%2 == 1 ? push!(indexes, (indexes[i][1], indexes[i][3], 2*nbv+1)) : push!(indexes,(indexes[i][2], indexes[i][1], 2*nbv+2)) | ||
| end | ||
| return indexes | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| struct Particle{N, T} <: GeometryPrimitive{N, T} | ||
| position::Point{N, T} | ||
| velocity::Vec{N, T} | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| struct Pyramid{T} <: GeometryPrimitive{3, T} | ||
| middle::Point{3, T} | ||
| length::T | ||
| width ::T | ||
| end | ||
|
|
||
| function coordinates(p::Pyramid{T}, nvertices=nothing) where {T} | ||
| leftup = Point{3, T}(-p.width , p.width, 0) / 2 | ||
| leftdown = Point(-p.width, -p.width, 0) / 2 | ||
| tip = Point{3, T}(p.middle + Point{3, T}(0, 0, p.length)) | ||
| lu = Point{3, T}(p.middle + leftup) | ||
| ld = Point{3, T}(p.middle + leftdown) | ||
| ru = Point{3, T}(p.middle - leftdown) | ||
| rd = Point{3, T}(p.middle - leftup) | ||
| return Point{3, T}[ | ||
| tip, rd, ru, | ||
| tip, ru, lu, | ||
| tip, lu, ld, | ||
| tip, ld, rd, | ||
| rd, ru, lu, | ||
| lu, ld, rd | ||
| ] | ||
| end | ||
|
|
||
| function faces(r::Pyramid, nvertices=nothing) where FT | ||
| return (TriangleFace(triangle) for triangle in TupleView{3}(1:18)) | ||
| end | ||
|
juliohm marked this conversation as resolved.
Outdated
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this file also be moved, or be renamed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. Appreciate if anyone can suggest a better name. By reading the contents I couldn't figure out a good name.