-
-
Notifications
You must be signed in to change notification settings - Fork 55
Add Cone primitive #257
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
Draft
ffreyer
wants to merge
5
commits into
master
Choose a base branch
from
ff/arrows
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add Cone primitive #257
Changes from all commits
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,114 @@ | ||
""" | ||
Cone{T}(origin::Point3, tip::Point3, radius) | ||
|
||
A Cone is a cylinder where one end has a radius of 0. It is defined by an | ||
`origin` with a finite `radius` which linearly decreases to 0 at the `tip`. | ||
""" | ||
struct Cone{T} <: GeometryPrimitive{3, T} | ||
origin::Point3{T} | ||
tip::Point3{T} | ||
radius::T | ||
end | ||
|
||
function Cone(origin::Point3{T1}, tip::Point3{T2}, radius::T3) where {T1, T2, T3} | ||
T = promote_type(T1, T2, T3) | ||
return Cone{T}(origin, tip, radius) | ||
end | ||
|
||
origin(c::Cone) = c.origin | ||
extremity(c::Cone) = c.tip | ||
radius(c::Cone) = c.radius | ||
height(c::Cone) = norm(c.tip - c.origin) | ||
direction(c::Cone) = (c.tip .- c.origin) ./ height(c) | ||
|
||
function rotation(c::Cone{T}) where {T} | ||
d3 = direction(c) | ||
u = Vec{3, T}(d3[1], d3[2], d3[3]) | ||
if abs(u[1]) > 0 || abs(u[2]) > 0 | ||
v = Vec{3, T}(u[2], -u[1], T(0)) | ||
else | ||
v = Vec{3, T}(T(0), -u[3], u[2]) | ||
end | ||
v = normalize(v) | ||
w = Vec{3, T}(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 Mat{3, 3, T}(v..., w..., u...) | ||
end | ||
|
||
function coordinates(c::Cone{T}, nvertices=30) where {T} | ||
nvertices += isodd(nvertices) | ||
nhalf = div(nvertices, 2) | ||
|
||
R = rotation(c) | ||
step = 2pi / nhalf | ||
|
||
ps = Vector{Point3{T}}(undef, nhalf + 2) | ||
for i in 1:nhalf | ||
phi = (i-1) * step | ||
ps[i] = R * Point3{T}(c.radius * cos(phi), c.radius * sin(phi), 0) + c.origin | ||
end | ||
ps[end-1] = c.tip | ||
ps[end] = c.origin | ||
|
||
return ps | ||
end | ||
|
||
function normals(c::Cone, nvertices = 30) | ||
nvertices += isodd(nvertices) | ||
nhalf = div(nvertices, 2) | ||
|
||
R = rotation(c) | ||
step = 2pi / nhalf | ||
|
||
ns = Vector{Vec3f}(undef, nhalf + 2) | ||
# shell at origin | ||
# normals are angled in z direction due to change in radius (from radius to 0) | ||
# This can be calculated from triangles | ||
z = radius(c) / height(c) | ||
norm = 1.0 / sqrt(1 + z*z) | ||
for i in 1:nhalf | ||
phi = (i-1) * step | ||
ns[i] = R * (norm * Vec3f(cos(phi), sin(phi), z)) | ||
end | ||
|
||
# tip - this is undefined / should be all ring angles at once | ||
# for rendering it is useful to define this as Vec3f(0), because tip normal | ||
# has no useful value to contribute to the interpolated fragment normal | ||
ns[end-1] = Vec3f(0) | ||
|
||
# cap | ||
ns[end] = Vec3f(normalize(c.origin - c.tip)) | ||
|
||
faces = Vector{GLTriangleFace}(undef, nvertices) | ||
|
||
# shell | ||
for i in 1:nhalf | ||
faces[i] = GLTriangleFace(i, mod1(i+1, nhalf), nhalf+1) | ||
end | ||
|
||
# cap | ||
for i in 1:nhalf | ||
faces[i+nhalf] = GLTriangleFace(nhalf + 2) | ||
end | ||
|
||
return FaceView(ns, faces) | ||
end | ||
|
||
function faces(::Cone, facets=30) | ||
nvertices = facets + isodd(facets) | ||
nhalf = div(nvertices, 2) | ||
|
||
faces = Vector{GLTriangleFace}(undef, nvertices) | ||
|
||
# shell | ||
for i in 1:nhalf | ||
faces[i] = GLTriangleFace(i, mod1(i+1, nhalf), nhalf+1) | ||
end | ||
|
||
# cap | ||
for i in 1:nhalf | ||
faces[i+nhalf] = GLTriangleFace(i, mod1(i+1, nhalf), nhalf+2) | ||
end | ||
|
||
return faces | ||
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
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.
This fixes seams on the mesh. The left mesh uses triangles where the tip vertex is duplicated for each triangle with a different normal (average of bottom normals). The right uses one (0,0,0) vertex at the tip:

You get the same seams with quads instead of triangles and even if you give the tip a finite radius, i.e. with a conical frustum. (?)