-
Notifications
You must be signed in to change notification settings - Fork 2
Add 3d ray intersect #269
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
base: master
Are you sure you want to change the base?
Add 3d ray intersect #269
Changes from 8 commits
99251b1
5f45534
2115990
4fd08d1
4d6516f
da464d5
756f013
eb2edce
544e4ac
ef97206
91a5b95
14f7d46
381cc89
6195ab4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ document[Symbol("Geometry")] = Symbol[] | |||||
| import StaticArrays | ||||||
| import MillerExtendedHarmonic: MXH | ||||||
| import LinearAlgebra | ||||||
| import Roots | ||||||
|
|
||||||
| """ | ||||||
| centroid(x::AbstractVector{<:T}, y::AbstractVector{<:T}) where {T<:Real} | ||||||
|
|
@@ -81,6 +82,130 @@ end | |||||
|
|
||||||
| @compat public revolution_volume | ||||||
| push!(document[Symbol("Geometry")], :revolution_volume) | ||||||
| """ | ||||||
| r_intersect_interval(x0::T, y0::T, dx::T, dy::T, r_min::T, r_max::T) where {T<:Real} | ||||||
|
|
||||||
| Finds intersection of ray starting at x0 and y0 with direction (dx,dy) | ||||||
| with reference major radius value r_ref | ||||||
|
|
||||||
| - `x0``, `y0``: Origin | ||||||
| - `dx``, `dy``: Normalized direction in x-y plane | ||||||
| - `r_min`, `r_max`: Edges of r_range | ||||||
|
|
||||||
| Returns interval [t1, t2] for which r_min <= r(t) <= r_max with r(t)^2 = (x+dx*t)^2 + (y+dy*t)^2 | ||||||
|
|
||||||
| """ | ||||||
|
|
||||||
| function solve_r_intersect(x0::T, y0::T, dx::T, dy::T, r_min::T, r_max::T) where {T<:Real} | ||||||
| r0 = sqrt(x0^2 + y0^2) | ||||||
| t_crossings = zeros(Float64, 4) | ||||||
| crossing = zeros(Float64, 4) # +1 -> into domain | ||||||
| # -1 -> out of | ||||||
| # 0 -> no crossing | ||||||
| into_domain = [1.0, -1.0] | ||||||
|
||||||
| into_domain = [1.0, -1.0] | |
| into_domain = Tuple([1.0, 1.0]) |
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.
into_domain = (1.0, 1.0)
Outdated
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.
[0.0 Inf] will give you a 1x2 Matrix
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.
| return [[0.0 Inf];] | |
| return [0.0 Inf] |
Outdated
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.
You want an empty array? Float64[] is more idiomatic.
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.
| return zeros(Float64, 0) | |
| return Float64[] |
Outdated
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.
If this ever becomes computationally intensive, there are plenty of redundant FLOPs here that could be avoided.
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 is run once per beam so it should not come up too often and cost negligible time compared to the actual raytracing.
Outdated
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.
Float64[]
Outdated
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.
| intervals = zeros(Float64, 0) | |
| intervals = Float64[] |
Outdated
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.
Make sure grazing intersections are properly handled.
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.
For my specific use-case grazing intersection can raise an error. A beam that hits the rectangular flux matrix parallel to one of its boundaries indicates an error in how the beam is set up.
Outdated
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.
sort![...] can't be right?
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.
| t_interval_z = sort![(z_min - z0) / dz, (z_max - z0) / dz] | |
| t_interval_z = sort([(z_min - z0) / dz, (z_max - z0) / dz]) |
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.
sort!([(z_min - z0) / dz, (z_max - z0) / dz]) at the every least, but sort(@SVector[(z_min - z0) / dz, (z_max - z0) / dz]) would be better.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using IMAS | ||
| using Test | ||
|
|
||
| @testset "Test ray_torus_intersect" begin | ||
| test_cases = [ | ||
| ([6.0,0.0,0.0], [-1.0,0.0,0.0], [1.0, 4.0], [-1.0, 1.0], [4.0 0.0 0.0], ) | ||
| ] | ||
| for (origin, direction, r_bounds, z_bounds, expected) in test_cases | ||
| @testset "origin=$origin, direction=$direction, r_bounds=$r_bounds, z_bounds=$z_bounds" begin | ||
| # println("--- x0=$x0, y0=$y0, dx=$dx, dy=$dy, r_min=$r_min, r_max=$r_max ---") | ||
| test_result = IMAS.ray_torus_intersect(origin, direction, r_bounds, z_bounds) | ||
| println("--- Result | expected: $test_result | $expected ---") | ||
| @test test_result ≈ expected | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| using IMAS | ||
| using Test | ||
|
|
||
| @testset "Test solve_r_intersect" begin | ||
| test_cases = [ | ||
| (3.0, 0.0, 0.0, 0.0, 1.0, 4.0, [[0.0 Inf];]), # ray is not moving, but starts on the grid | ||
| (6.0, 0.0, 0.0, 0.0, 1.0, 4.0, zeros(Float64, 0)), # ray is not moving, and starts off the grid | ||
| (6.0, 0.0, -1.0, 0.0, 1.0, 4.0, [2. 5.; 7. 10.]), # ray in x-direction | ||
| (0.0, 6.0, 0.0, -1.0, 2.0, 4.0, [2. 4.; 8. 10.]), # ray in y-direction | ||
| (3.0, 0.0, -1.0, 0.0, 1.0, 4.0, [0.0 2.; 4.0 7.0]), # ray in x-direction with start in torus | ||
| (6.0, 6.0, 0.0, -1.0, 2.0, 4.0, zeros(Float64, 0)), # ray in x-direction with offset causing no intersection | ||
| (3.0, 6.0, -3.0/5.0, -4.0/5.0, 1.0, 2.0, [[5.0 41.0/5.0];]), # Single crossing with x-y ray | ||
| (3.0, 8.0, -3.0/5.0, -4.0/5.0, 3.0, 4.0, [5.0 32.0/5.0; 10.0 57.0/5.0]) # double crossing with x-y ray | ||
| ] | ||
|
|
||
| for (x0, y0, dx, dy, r_min, r_max, expected) in test_cases | ||
| @testset "x0=$x0, y0=$y0, dx=$dx, dy=$dy, r_min=$r_min, r_max=$r_max" begin | ||
| # println("--- x0=$x0, y0=$y0, dx=$dx, dy=$dy, r_min=$r_min, r_max=$r_max ---") | ||
| test_result = IMAS.solve_r_intersect(x0, y0, dx, dy, r_min, r_max) | ||
| println("--- Result | expected: $test_result | $expected ---") | ||
| @test test_result ≈ expected | ||
| end | ||
| end | ||
| end |
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.
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 changed the size.