-
Notifications
You must be signed in to change notification settings - Fork 148
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
CBA_fnc_getVolume not calculating volume correctly #984
Conversation
…r added to specify greater precision.
The math is right and nice usage of vector commands. But:
These are exactly the same, and if you carefully read the code, it is clear why: private _boundingBox = [boundingBox _object, boundingBoxReal _object] select _real; This ^ line is executed in the following order:
What you expected to happen was that only either private _boundingBox = call ([{boundingBox _object}, {boundingBoxReal _object}] select _real); or more commonly used although ugly af: private _boundingBox = if (_real) then {boundingBoxReal _object} else {boundingBox _object}; This is good news, because it means this function can be made even faster. Personally I don't see the reason not to just always use |
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.
broken optimization
I did compare select
call
if–else
The rationale for supporting both commands is simply that users might be interested in both. boundingBoxReal was introduced in Arma 3 version 0.50, but for some reason all BIS functions below introduced in Arma 3 version 1.00 use boundingBox when calculating bounding boxes. BIS_fnc_camera Without studying the functions further, it's probably safe to assume they don't calculate volume. However, there might still be use cases where the volume of boundingBox is desirable. Though if you consider it better to skip it we'll do. |
I see. I misunderstood you then. If both of these commands are so fast that executing both makes no noticable difference in speed, and only the returned value changes, then it is probably not worth it to worry about optimization. |
Although evaluating isn't noticeably different (sometimes slower, sometimes faster) than evaluating it is certainly true that just evaluating is faster than evaluating both
which for reasons of speed warrant two functions as you mentioned. The only problem is that the function you'd likely most want to use would then be |
I think it is fine how it is, even though I personally see no reason why anyone would ever even want to use bb instead of bbr. |
Neither do I, but sometimes people have inconceivable desires. Maybe we should ask BI why. If someone ever wants to use boundingBox when calculating volume they are welcome to request it. |
* master: Add Feature Camera Player EH (#982) German brands for JR and JAM components (#988) veteran29 Polish translation updates master (#986) German translation, part 3 (#981) German translation, part 2 (#980) German translation, part 1 (#979) add Joint Ammo Magazines A3 (#928) CBA_fnc_getVolume not calculating volume correctly (#984) ProjectileTracking - Stop tracking stationary objects (#985)
When merged this pull request will:
The commands boundingBox and boundingBoxReal return an array with the extreme points of a model in the format
[[x1, y1, z1], [x2, y2, z2]]
(left–back–bottom, right–front–top). The current implementation ofCBA_fnc_getVolume
returns the productx2 * y2 * z2
. That is the formula for the volume of a cuboid givenx1
,y1
, andz1
are all0
. However, for boundingBox and boundingBoxReal they are not0
but negative.The proposed implementation calculates vectorDiff between
[x2, y2, z2]
and[x1, y1, z1]
and returns the product of those elements. Similar calculations, for size instead of volume, can be seen in BIS_fnc_boundingBoxMarker (although that function takes for each element vectorDotProduct between the difference and its corresponding units vector—overcomplicated and slower) and BIS_fnc_objectHeight where the height is calculated asz2-z1
. It is worth noting however that in the vast majority of BIS functions abs is used on the difference between the elements, which appears completely unnecessary as of all to judge the difference cannot be negative.The proposed implementation uses boundingBoxReal by default for greater precision, while the current implementation uses boundingBox. This is motivated and does not break backwards compatability, as the current implementation does not return the volume in the first place.
The docstring does not explain the difference between boundingBox and boundingBoxReal other than in the example. This is intentional, as the user is assumed to have prior knowledge of those engine commands. Feel free to include it in the function description or parameter description if it is considered suitable.
As the proposed implementation is written from scratch the previous author has been omitted. If this is uncalled for, feel free to include the previous author for historical reasons.
Example output from the current and proposed functions on a Strider follows below.
Current implementation
[_strider] call CBA_fnc_getVolume
22.5603
Proposed implementation with less precision (using boundingBox)
[_strider, false] call CBA_fnc_getVolume
180.483
Proposed implementation with greater precision (using boundingBoxReal)
[_strider, true] call CBA_fnc_getVolume
104.446
[_strider] call CBA_fnc_getVolume
104.446