-
-
Notifications
You must be signed in to change notification settings - Fork 21.5k
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
Remove duplicate code between intersects_ray
and intersects_segment
#100511
base: master
Are you sure you want to change the base?
Conversation
1ee3708
to
021dfc0
Compare
@lawnjelly here the old functions now wrap a call to the new ones as you suggested in #100475. I believe it's helpful here. |
021dfc0
to
a8958ef
Compare
Resolved requested changes. Thanks @AThousandShips for your torough review. |
@@ -159,7 +159,7 @@ bool AABB::find_intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, bool | |||
} | |||
tmax = t2; | |||
} | |||
if (tmin > tmax) { | |||
if (tmin > tmax || (p_max_distance > 0 && tmin > p_max_distance)) { |
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.
Are we now paying the price for this check even when not used? (p_max_distance
defaulted to 0?)
I can see the p_max_distance > 0
should be optimized out but does the second term tmin > p_max_distance
?
(It's a while since I looked at this so can't remember. If it doesn't BTW, then templating the function can be handy for things like that, pass a bool
template parameter of whether to do the extra check, and it will compile 2 separate versions with and without the check.)
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.
Are we now paying the price for this check even when not used? (
p_max_distance
defaulted to 0?)I can see the
p_max_distance > 0
should be optimized out but does the second termtmin > p_max_distance
?
Yes we are. I believe nothing can be optimized out right now because the code is in the .cpp.
Will consider templating the function, unless just moving it to the header file allows the compiler to optimize it down.
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.
Afaik compiler can optimize out functions in the cpp if called from the same translation unit (i.e. it has the opportunity to inline different versions of called functions), putting stuff in the header allows stuff to be optimized from a different translation unit. (Link time optimization can theoretically also remove this limit but may not be as smart as in the same translation unit.)
Afaik usually this should mean wrapper functions in the same header / cpp will aggressively optimize.
Always worth checking the disassembly to see what is going on, or e.g. profile release version and watch the hot spots.
Agree, but it does mean that any changes to behaviour could cause knock on bugs. I haven't looked in detail but are there any changes to behaviour here, or is it purely moving code around? |
a8958ef
to
160e979
Compare
Added |
160e979
to
fc21954
Compare
Pushed some small changes in relation to the below.
Generally speaking the intention is to preserve the exact original behavior. TLDR :
Unfold details
|
Also, I realized that defaulting I'm not fully clear on whether it's a real problem. Any views welcome. Alternatives could be :
|
In
AABB
,Plane
,Geometry3D
[Edit : andTriangleMesh
] functionsray_intersects_*
andsegment_intersects_*
involve almost identical code. In some cases both flavors even diverged over time which complexifies maintenance.This PR :
ray_intersects_*
with an additional parameterdistance
that clamps the raycast in the same fashion assegment_intersects_*
.distance
is defaulted to0.0
which enables infinite raycastingsegment_intersects_*
wrap calls toray_intersects_*
. It avoids duplication and opens the way to eventually deprecatingintersects_segment_*
in the future, as they suffer from numerical precision issues (see related issue FixBVH::ray_query()
numerical precision #100475)Geometry3D::ray_intersects_*
counterparts tosegment_intersects_sphere
,_cylinder
and_convex
AABB
andGeometry3D
to validate the above changes