SortReverseAndStore.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. JPH_NAMESPACE_BEGIN
  6. /// This function will sort values from high to low and only keep the ones that are less than inMaxValue
  7. /// @param inValues Values to be sorted
  8. /// @param inMaxValue Values need to be less than this to keep them
  9. /// @param ioIdentifiers 4 identifiers that will be sorted in the same way as the values
  10. /// @param outValues The values are stored here from high to low
  11. /// @return The number of values that were kept
  12. JPH_INLINE int SortReverseAndStore(Vec4Arg inValues, float inMaxValue, UVec4 &ioIdentifiers, float *outValues)
  13. {
  14. // Sort so that highest values are first (we want to first process closer hits and we process stack top to bottom)
  15. Vec4::sSort4Reverse(inValues, ioIdentifiers);
  16. // Count how many results are less than the max value
  17. UVec4 closer = Vec4::sLess(inValues, Vec4::sReplicate(inMaxValue));
  18. int num_results = closer.CountTrues();
  19. // Shift the values so that only the ones that are less than max are kept
  20. inValues = inValues.ReinterpretAsInt().ShiftComponents4Minus(num_results).ReinterpretAsFloat();
  21. ioIdentifiers = ioIdentifiers.ShiftComponents4Minus(num_results);
  22. // Store the values
  23. inValues.StoreFloat4((Float4 *)outValues);
  24. return num_results;
  25. }
  26. /// Shift the elements so that the identifiers that correspond with the trues in inValue come first
  27. /// @param inValue Values to test for true or false
  28. /// @param ioIdentifiers the identifiers that are shifted, on return they are shifted
  29. /// @return The number of trues
  30. JPH_INLINE int CountAndSortTrues(UVec4Arg inValue, UVec4 &ioIdentifiers)
  31. {
  32. // Sort the hits
  33. ioIdentifiers = UVec4::sSort4True(inValue, ioIdentifiers);
  34. // Return the amount of hits
  35. return inValue.CountTrues();
  36. }
  37. JPH_NAMESPACE_END