From d0bcf7aa9b69387f144f2bee6d8fa52358c5ec7a Mon Sep 17 00:00:00 2001 From: osa Date: Tue, 11 Sep 2018 21:56:14 +0300 Subject: [PATCH] 0030131: Foundation Classes - support of Linear builder for 2D BVH trees BVH_LinearBuilder and BVH_RadixSorter now accept N==2. NCollection_Vec2/3/4 - added missing division by vec operators. --- src/BVH/BVH.cxx | 4 +++ src/BVH/BVH_LinearBuilder.hxx | 2 +- src/BVH/BVH_RadixSorter.hxx | 48 ++++++++++++---------------- src/NCollection/NCollection_Vec2.hxx | 16 ++++++++++ src/NCollection/NCollection_Vec3.hxx | 17 ++++++++++ src/NCollection/NCollection_Vec4.hxx | 18 +++++++++++ 6 files changed, 76 insertions(+), 29 deletions(-) diff --git a/src/BVH/BVH.cxx b/src/BVH/BVH.cxx index 451fd0c19d..671844e50c 100644 --- a/src/BVH/BVH.cxx +++ b/src/BVH/BVH.cxx @@ -105,15 +105,19 @@ template class BVH_QuickSorter; template class BVH_QuickSorter; template class BVH_QuickSorter; +template class BVH_RadixSorter; template class BVH_RadixSorter; template class BVH_RadixSorter; +template class BVH_RadixSorter; template class BVH_RadixSorter; template class BVH_RadixSorter; +template class BVH_LinearBuilder; template class BVH_LinearBuilder; template class BVH_LinearBuilder; +template class BVH_LinearBuilder; template class BVH_LinearBuilder; template class BVH_LinearBuilder; diff --git a/src/BVH/BVH_LinearBuilder.hxx b/src/BVH/BVH_LinearBuilder.hxx index c2aee79389..35e20d46f4 100644 --- a/src/BVH/BVH_LinearBuilder.hxx +++ b/src/BVH/BVH_LinearBuilder.hxx @@ -308,7 +308,7 @@ void BVH_LinearBuilder::Build (BVH_Set* theSet, BVH_Tree* theBVH, const BVH_Box& theBox) const { - Standard_STATIC_ASSERT (N == 3 || N == 4); + Standard_STATIC_ASSERT (N == 2 || N == 3 || N == 4); const Standard_Integer aSetSize = theSet->Size(); if (theBVH == NULL || aSetSize == 0) { diff --git a/src/BVH/BVH_RadixSorter.hxx b/src/BVH/BVH_RadixSorter.hxx index 83acb45e65..4425a74231 100644 --- a/src/BVH/BVH_RadixSorter.hxx +++ b/src/BVH/BVH_RadixSorter.hxx @@ -173,18 +173,18 @@ namespace BVH template void BVH_RadixSorter::Perform (BVH_Set* theSet, const Standard_Integer theStart, const Standard_Integer theFinal) { - Standard_STATIC_ASSERT (N == 3 || N == 4); + Standard_STATIC_ASSERT (N == 2 || N == 3 || N == 4); - const Standard_Integer aDimensionX = 1024; - const Standard_Integer aDimensionY = 1024; - const Standard_Integer aDimensionZ = 1024; + const Standard_Integer aDimension = 1024; + const Standard_Integer aNbEffComp = N == 2 ? 2 : 3; // 4th component is ignored const BVH_VecNt aSceneMin = myBox.CornerMin(); const BVH_VecNt aSceneMax = myBox.CornerMax(); - const T aReverseSizeX = static_cast (aDimensionX) / Max (static_cast (BVH::THE_NODE_MIN_SIZE), aSceneMax.x() - aSceneMin.x()); - const T aReverseSizeY = static_cast (aDimensionY) / Max (static_cast (BVH::THE_NODE_MIN_SIZE), aSceneMax.y() - aSceneMin.y()); - const T aReverseSizeZ = static_cast (aDimensionZ) / Max (static_cast (BVH::THE_NODE_MIN_SIZE), aSceneMax.z() - aSceneMin.z()); + BVH_VecNt aNodeMinSizeVecT (static_cast(BVH::THE_NODE_MIN_SIZE)); + BVH::BoxMinMax::CwiseMax (aNodeMinSizeVecT, aSceneMax - aSceneMin); + + const BVH_VecNt aReverseSize = BVH_VecNt (static_cast(aDimension)) / aNodeMinSizeVecT; myEncodedLinks = new NCollection_Shared >(theStart, theFinal); @@ -192,32 +192,24 @@ void BVH_RadixSorter::Perform (BVH_Set* theSet, const Standard_Integ for (Standard_Integer aPrimIdx = theStart; aPrimIdx <= theFinal; ++aPrimIdx) { const BVH_VecNt aCenter = theSet->Box (aPrimIdx).Center(); + const BVH_VecNt aVoxelF = (aCenter - aSceneMin) * aReverseSize; - Standard_Integer aVoxelX = BVH::IntFloor ((aCenter.x() - aSceneMin.x()) * aReverseSizeX); - Standard_Integer aVoxelY = BVH::IntFloor ((aCenter.y() - aSceneMin.y()) * aReverseSizeY); - Standard_Integer aVoxelZ = BVH::IntFloor ((aCenter.z() - aSceneMin.z()) * aReverseSizeZ); + Standard_Integer aMortonCode = 0; + for (Standard_Integer aCompIter = 0; aCompIter < aNbEffComp; ++aCompIter) + { + Standard_Integer aVoxel = BVH::IntFloor (BVH::VecComp::Get (aVoxelF, aCompIter)); - aVoxelX = Max (0, Min (aVoxelX, aDimensionX - 1)); - aVoxelY = Max (0, Min (aVoxelY, aDimensionY - 1)); - aVoxelZ = Max (0, Min (aVoxelZ, aDimensionZ - 1)); + aVoxel = Max (0, Min (aVoxel, aDimension - 1)); - aVoxelX = (aVoxelX | (aVoxelX << 16)) & 0x030000FF; - aVoxelX = (aVoxelX | (aVoxelX << 8)) & 0x0300F00F; - aVoxelX = (aVoxelX | (aVoxelX << 4)) & 0x030C30C3; - aVoxelX = (aVoxelX | (aVoxelX << 2)) & 0x09249249; + aVoxel = (aVoxel | (aVoxel << 16)) & 0x030000FF; + aVoxel = (aVoxel | (aVoxel << 8)) & 0x0300F00F; + aVoxel = (aVoxel | (aVoxel << 4)) & 0x030C30C3; + aVoxel = (aVoxel | (aVoxel << 2)) & 0x09249249; - aVoxelY = (aVoxelY | (aVoxelY << 16)) & 0x030000FF; - aVoxelY = (aVoxelY | (aVoxelY << 8)) & 0x0300F00F; - aVoxelY = (aVoxelY | (aVoxelY << 4)) & 0x030C30C3; - aVoxelY = (aVoxelY | (aVoxelY << 2)) & 0x09249249; + aMortonCode |= (aVoxel << aCompIter); + } - aVoxelZ = (aVoxelZ | (aVoxelZ << 16)) & 0x030000FF; - aVoxelZ = (aVoxelZ | (aVoxelZ << 8)) & 0x0300F00F; - aVoxelZ = (aVoxelZ | (aVoxelZ << 4)) & 0x030C30C3; - aVoxelZ = (aVoxelZ | (aVoxelZ << 2)) & 0x09249249; - - myEncodedLinks->ChangeValue (aPrimIdx) = BVH_EncodedLink ( - aVoxelX | (aVoxelY << 1) | (aVoxelZ << 2), aPrimIdx); + myEncodedLinks->ChangeValue (aPrimIdx) = BVH_EncodedLink (aMortonCode, aPrimIdx); } // Step 2 -- Sort primitives by their Morton codes using radix sort diff --git a/src/NCollection/NCollection_Vec2.hxx b/src/NCollection/NCollection_Vec2.hxx index bb74d07def..c1ebb0509a 100644 --- a/src/NCollection/NCollection_Vec2.hxx +++ b/src/NCollection/NCollection_Vec2.hxx @@ -217,6 +217,14 @@ public: return *this; } + //! Compute per-component division. + NCollection_Vec2& operator/= (const NCollection_Vec2& theRight) + { + v[0] /= theRight.v[0]; + v[1] /= theRight.v[1]; + return *this; + } + //! Compute per-component multiplication by scale factor. NCollection_Vec2 operator* (const Element_t theFactor) const { @@ -230,6 +238,14 @@ public: v[1] / theInvFactor); } + //! Compute per-component division. + friend NCollection_Vec2 operator/ (const NCollection_Vec2& theLeft, + const NCollection_Vec2& theRight) + { + return NCollection_Vec2 (theLeft.v[0] / theRight.v[0], + theLeft.v[1] / theRight.v[1]); + } + //! Computes the dot product. Element_t Dot (const NCollection_Vec2& theOther) const { diff --git a/src/NCollection/NCollection_Vec3.hxx b/src/NCollection/NCollection_Vec3.hxx index 165b6d5801..7a4e45b9f8 100644 --- a/src/NCollection/NCollection_Vec3.hxx +++ b/src/NCollection/NCollection_Vec3.hxx @@ -290,6 +290,15 @@ public: return *this; } + //! Compute per-component division. + NCollection_Vec3& operator/= (const NCollection_Vec3& theRight) + { + v[0] /= theRight.v[0]; + v[1] /= theRight.v[1]; + v[2] /= theRight.v[2]; + return *this; + } + //! Compute per-component division by scale factor. NCollection_Vec3 operator/ (const Element_t theInvFactor) const { @@ -297,6 +306,14 @@ public: return aResult /= theInvFactor; } + //! Compute per-component division. + friend NCollection_Vec3 operator/ (const NCollection_Vec3& theLeft, + const NCollection_Vec3& theRight) + { + NCollection_Vec3 aResult = NCollection_Vec3 (theLeft); + return aResult /= theRight; + } + //! Computes the dot product. Element_t Dot (const NCollection_Vec3& theOther) const { diff --git a/src/NCollection/NCollection_Vec4.hxx b/src/NCollection/NCollection_Vec4.hxx index c3cb93a712..f049585df3 100644 --- a/src/NCollection/NCollection_Vec4.hxx +++ b/src/NCollection/NCollection_Vec4.hxx @@ -336,6 +336,16 @@ public: return *this; } + //! Compute per-component division. + NCollection_Vec4& operator/= (const NCollection_Vec4& theRight) + { + v[0] /= theRight.v[0]; + v[1] /= theRight.v[1]; + v[2] /= theRight.v[2]; + v[3] /= theRight.v[3]; + return *this; + } + //! Compute per-component division by scale factor. NCollection_Vec4 operator/ (const Element_t theInvFactor) { @@ -343,6 +353,14 @@ public: return aResult /= theInvFactor; } + //! Compute per-component division. + friend NCollection_Vec4 operator/ (const NCollection_Vec4& theLeft, + const NCollection_Vec4& theRight) + { + NCollection_Vec4 aResult = NCollection_Vec4 (theLeft); + return aResult /= theRight; + } + private: Element_t v[4]; //!< define the vector as array to avoid structure alignment issues