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