1 // Created on: 2014-09-06
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <Standard_Assert.hxx>
18 #include <BVH_Triangulation.hxx>
21 // On Windows, function TryEnterCriticalSection has appeared in Windows NT
22 // and is surrounded by #ifdef in MS VC++ 7.1 headers.
23 // Thus to use it we need to define appropriate macro saying that we will
24 // run on Windows NT 4.0 at least
25 #if defined(_WIN32) && !defined(_WIN32_WINNT)
26 #define _WIN32_WINNT 0x0501
32 // =======================================================================
33 // function : BVH_DistanceField
35 // =======================================================================
36 template<class T, int N>
37 BVH_DistanceField<T, N>::BVH_DistanceField (const Standard_Integer theMaximumSize,
38 const Standard_Boolean theComputeSign)
39 : myMaximumSize (theMaximumSize),
40 myComputeSign (theComputeSign)
42 Standard_STATIC_ASSERT (N == 3 || N == 4);
44 myVoxelData = new T[myMaximumSize * myMaximumSize * myMaximumSize];
47 // =======================================================================
48 // function : ~BVH_DistanceField
50 // =======================================================================
51 template<class T, int N>
52 BVH_DistanceField<T, N>::~BVH_DistanceField()
54 delete [] myVoxelData;
57 #if defined (_WIN32) && defined (max)
63 #define BVH_DOT3(A, B) (A.x() * B.x() + A.y() * B.y() + A.z() * B.z())
67 //=======================================================================
68 //function : DistanceToBox
69 //purpose : Computes squared distance from point to box
70 //=======================================================================
71 template<class T, int N>
72 T DistanceToBox (const typename VectorType<T, N>::Type& thePnt,
73 const typename VectorType<T, N>::Type& theMin,
74 const typename VectorType<T, N>::Type& theMax)
76 Standard_STATIC_ASSERT (N == 3 || N == 4);
78 T aNearestX = Min (Max (thePnt.x(), theMin.x()), theMax.x());
79 T aNearestY = Min (Max (thePnt.y(), theMin.y()), theMax.y());
80 T aNearestZ = Min (Max (thePnt.z(), theMin.z()), theMax.z());
82 if (aNearestX == thePnt.x()
83 && aNearestY == thePnt.y()
84 && aNearestZ == thePnt.z())
86 return static_cast<T> (0);
89 aNearestX -= thePnt.x();
90 aNearestY -= thePnt.y();
91 aNearestZ -= thePnt.z();
93 return aNearestX * aNearestX +
94 aNearestY * aNearestY +
95 aNearestZ * aNearestZ;
98 //=======================================================================
99 //function : DirectionToNearestPoint
100 //purpose : Computes squared distance from point to triangle
101 // ======================================================================
102 template<class T, int N>
103 typename VectorType<T, N>::Type DirectionToNearestPoint (
104 const typename VectorType<T, N>::Type& thePoint,
105 const typename VectorType<T, N>::Type& theVertA,
106 const typename VectorType<T, N>::Type& theVertB,
107 const typename VectorType<T, N>::Type& theVertC)
109 Standard_STATIC_ASSERT (N == 3 || N == 4);
111 const typename VectorType<T, N>::Type aAB = theVertB - theVertA;
112 const typename VectorType<T, N>::Type aAC = theVertC - theVertA;
113 const typename VectorType<T, N>::Type aAP = thePoint - theVertA;
115 const T aABdotAP = BVH_DOT3 (aAB, aAP);
116 const T aACdotAP = BVH_DOT3 (aAC, aAP);
118 if (aABdotAP <= static_cast<T> (0) && aACdotAP <= static_cast<T> (0))
123 const typename VectorType<T, N>::Type aBC = theVertC - theVertB;
124 const typename VectorType<T, N>::Type aBP = thePoint - theVertB;
126 const T aBAdotBP = -BVH_DOT3 (aAB, aBP);
127 const T aBCdotBP = BVH_DOT3 (aBC, aBP);
129 if (aBAdotBP <= static_cast<T> (0) && aBCdotBP <= static_cast<T> (0))
134 const typename VectorType<T, N>::Type aCP = thePoint - theVertC;
136 const T aCBdotCP = -BVH_DOT3 (aBC, aCP);
137 const T aCAdotCP = -BVH_DOT3 (aAC, aCP);
139 if (aCAdotCP <= static_cast<T> (0) && aCBdotCP <= static_cast<T> (0))
144 const T aACdotBP = BVH_DOT3 (aAC, aBP);
146 const T aVC = aABdotAP * aACdotBP + aBAdotBP * aACdotAP;
148 if (aVC <= static_cast<T> (0) && aABdotAP >= static_cast<T> (0) && aBAdotBP >= static_cast<T> (0))
150 return aAP - aAB * (aABdotAP / (aABdotAP + aBAdotBP));
153 const T aABdotCP = BVH_DOT3 (aAB, aCP);
155 const T aVA = aBAdotBP * aCAdotCP - aABdotCP * aACdotBP;
157 if (aVA <= static_cast<T> (0) && aBCdotBP >= static_cast<T> (0) && aCBdotCP >= static_cast<T> (0))
159 return aBP - aBC * (aBCdotBP / (aBCdotBP + aCBdotCP));
162 const T aVB = aABdotCP * aACdotAP + aABdotAP * aCAdotCP;
164 if (aVB <= static_cast<T> (0) && aACdotAP >= static_cast<T> (0) && aCAdotCP >= static_cast<T> (0))
166 return aAP - aAC * (aACdotAP / (aACdotAP + aCAdotCP));
169 const T aNorm = static_cast<T> (1.0) / (aVA + aVB + aVC);
171 const T aU = aVA * aNorm;
172 const T aV = aVB * aNorm;
174 return thePoint - (theVertA * aU + theVertB * aV + theVertC * (static_cast<T> (1.0) - aU - aV));
177 //=======================================================================
178 //function : SquareDistanceToObject
179 //purpose : Computes squared distance from point to BVH triangulation
180 //=======================================================================
181 template<class T, int N>
182 T SquareDistanceToObject (BVH_Object<T, N>* theObject,
183 const typename VectorType<T, N>::Type& thePnt, Standard_Boolean& theIsOutside)
185 Standard_STATIC_ASSERT (N == 3 || N == 4);
187 T aMinDistance = std::numeric_limits<T>::max();
189 BVH_Triangulation<T, N>* aTriangulation =
190 dynamic_cast<BVH_Triangulation<T, N>*> (theObject);
192 Standard_ASSERT_RETURN (aTriangulation != NULL,
193 "Error: Unsupported BVH object (non triangulation)", aMinDistance);
195 const NCollection_Handle<BVH_Tree<T, N> >& aBVH = aTriangulation->BVH();
199 return Standard_False;
202 std::pair<Standard_Integer, T> aStack[32];
204 Standard_Integer aHead = -1;
205 Standard_Integer aNode = 0; // root node
209 BVH_Vec4i aData = aBVH->NodeInfoBuffer()[aNode];
211 if (aData.x() == 0) // if inner node
213 const T aDistToLft = DistanceToBox<T, N> (thePnt,
214 aBVH->MinPoint (aData.y()),
215 aBVH->MaxPoint (aData.y()));
217 const T aDistToRgh = DistanceToBox<T, N> (thePnt,
218 aBVH->MinPoint (aData.z()),
219 aBVH->MaxPoint (aData.z()));
221 const Standard_Boolean aHitLft = aDistToLft <= aMinDistance;
222 const Standard_Boolean aHitRgh = aDistToRgh <= aMinDistance;
224 if (aHitLft & aHitRgh)
226 aNode = (aDistToLft < aDistToRgh) ? aData.y() : aData.z();
228 aStack[++aHead] = std::pair<Standard_Integer, T> (
229 aDistToLft < aDistToRgh ? aData.z() : aData.y(), Max (aDistToLft, aDistToRgh));
233 if (aHitLft | aHitRgh)
235 aNode = aHitLft ? aData.y() : aData.z();
242 std::pair<Standard_Integer, T>& anInfo = aStack[aHead--];
244 while (anInfo.second > aMinDistance)
249 anInfo = aStack[aHead--];
252 aNode = anInfo.first;
258 for (Standard_Integer aTrgIdx = aData.y(); aTrgIdx <= aData.z(); ++aTrgIdx)
260 const BVH_Vec4i aTriangle = aTriangulation->Elements[aTrgIdx];
262 const typename VectorType<T, N>::Type aVertex0 = aTriangulation->Vertices[aTriangle.x()];
263 const typename VectorType<T, N>::Type aVertex1 = aTriangulation->Vertices[aTriangle.y()];
264 const typename VectorType<T, N>::Type aVertex2 = aTriangulation->Vertices[aTriangle.z()];
266 const typename VectorType<T, N>::Type aDirection =
267 DirectionToNearestPoint<T, N> (thePnt, aVertex0, aVertex1, aVertex2);
269 const T aDistance = BVH_DOT3 (aDirection, aDirection);
271 if (aDistance < aMinDistance)
273 aMinDistance = aDistance;
275 typename VectorType<T, N>::Type aTrgEdges[] = { aVertex1 - aVertex0,
276 aVertex2 - aVertex0 };
278 typename VectorType<T, N>::Type aTrgNormal;
280 aTrgNormal.x() = aTrgEdges[0].y() * aTrgEdges[1].z() - aTrgEdges[0].z() * aTrgEdges[1].y();
281 aTrgNormal.y() = aTrgEdges[0].z() * aTrgEdges[1].x() - aTrgEdges[0].x() * aTrgEdges[1].z();
282 aTrgNormal.z() = aTrgEdges[0].x() * aTrgEdges[1].y() - aTrgEdges[0].y() * aTrgEdges[1].x();
284 theIsOutside = BVH_DOT3 (aTrgNormal, aDirection) > 0;
291 std::pair<Standard_Integer, T>& anInfo = aStack[aHead--];
293 while (anInfo.second > aMinDistance)
298 anInfo = aStack[aHead--];
301 aNode = anInfo.first;
306 //=======================================================================
307 //function : SquareDistanceToGeomerty
308 //purpose : Computes squared distance from point to BVH geometry
309 //=======================================================================
310 template<class T, int N>
311 T SquareDistanceToGeomerty (BVH_Geometry<T, N>& theGeometry,
312 const typename VectorType<T, N>::Type& thePnt, Standard_Boolean& theIsOutside)
314 Standard_STATIC_ASSERT (N == 3 || N == 4);
316 const NCollection_Handle<BVH_Tree<T, N> >& aBVH = theGeometry.BVH();
320 return Standard_False;
323 std::pair<Standard_Integer, T> aStack[32];
325 Standard_Integer aHead = -1;
326 Standard_Integer aNode = 0; // root node
328 T aMinDistance = std::numeric_limits<T>::max();
332 BVH_Vec4i aData = aBVH->NodeInfoBuffer()[aNode];
334 if (aData.x() == 0) // if inner node
336 const T aDistToLft = DistanceToBox<T, N> (thePnt,
337 aBVH->MinPoint (aData.y()),
338 aBVH->MaxPoint (aData.y()));
340 const T aDistToRgh = DistanceToBox<T, N> (thePnt,
341 aBVH->MinPoint (aData.z()),
342 aBVH->MaxPoint (aData.z()));
344 const Standard_Boolean aHitLft = aDistToLft <= aMinDistance;
345 const Standard_Boolean aHitRgh = aDistToRgh <= aMinDistance;
347 if (aHitLft & aHitRgh)
349 aNode = (aDistToLft < aDistToRgh) ? aData.y() : aData.z();
351 aStack[++aHead] = std::pair<Standard_Integer, T> (
352 aDistToLft < aDistToRgh ? aData.z() : aData.y(), Max (aDistToLft, aDistToRgh));
356 if (aHitLft | aHitRgh)
358 aNode = aHitLft ? aData.y() : aData.z();
365 std::pair<Standard_Integer, T>& anInfo = aStack[aHead--];
367 while (anInfo.second > aMinDistance)
372 anInfo = aStack[aHead--];
375 aNode = anInfo.first;
381 Standard_Boolean isOutside = Standard_True;
383 const T aDistance = SquareDistanceToObject (
384 theGeometry.Objects()(aNode).operator->(), thePnt, isOutside);
386 if (aDistance < aMinDistance)
388 aMinDistance = aDistance;
389 theIsOutside = isOutside;
395 std::pair<Standard_Integer, T>& anInfo = aStack[aHead--];
397 while (anInfo.second > aMinDistance)
402 anInfo = aStack[aHead--];
405 aNode = anInfo.first;
415 //! Tool object for parallel construction of distance field (uses Intel TBB).
416 template<class T, int N>
417 class BVH_ParallelDistanceFieldBuilder
421 //! Input BVH geometry.
422 BVH_Geometry<T, N>* myGeometry;
424 //! Output distance field.
425 BVH_DistanceField<T, N>* myOutField;
429 BVH_ParallelDistanceFieldBuilder (BVH_DistanceField<T, N>* theOutField, BVH_Geometry<T, N>* theGeometry)
430 : myGeometry (theGeometry),
431 myOutField (theOutField)
436 void operator() (const tbb::blocked_range<Standard_Integer>& theRange) const
438 myOutField->BuildSlices (*myGeometry, theRange.begin(), theRange.end());
444 // =======================================================================
445 // function : BuildSlices
446 // purpose : Performs building of distance field for the given Z slices
447 // =======================================================================
448 template<class T, int N>
449 void BVH_DistanceField<T, N>::BuildSlices (BVH_Geometry<T, N>& theGeometry,
450 const Standard_Integer theStartSlice, const Standard_Integer theFinalSlice)
452 for (Standard_Integer aZ = theStartSlice; aZ < theFinalSlice; ++aZ)
454 for (Standard_Integer aY = 0; aY < myDimensionY; ++aY)
456 for (Standard_Integer aX = 0; aX < myDimensionX; ++aX)
460 aCenter.x() = myCornerMin.x() + myVoxelSize.x() * (aX + static_cast<T> (0.5));
461 aCenter.y() = myCornerMin.y() + myVoxelSize.y() * (aY + static_cast<T> (0.5));
462 aCenter.z() = myCornerMin.z() + myVoxelSize.z() * (aZ + static_cast<T> (0.5));
464 Standard_Boolean isOutside = Standard_True;
466 const T aDistance = sqrt (
467 BVH::SquareDistanceToGeomerty<T, N> (theGeometry, aCenter, isOutside));
469 Voxel (aX, aY, aZ) = (!myComputeSign || isOutside) ? aDistance : -aDistance;
475 // =======================================================================
477 // purpose : Builds 3D distance field from BVH geometry
478 // =======================================================================
479 template<class T, int N>
480 Standard_Boolean BVH_DistanceField<T, N>::Build (BVH_Geometry<T, N>& theGeometry)
482 if (theGeometry.Size() == 0)
484 return Standard_False;
487 const BVH_VecNt aGlobalBoxSize = theGeometry.Box().Size();
489 const T aMaxBoxSide = Max (Max (aGlobalBoxSize.x(), aGlobalBoxSize.y()), aGlobalBoxSize.z());
491 myDimensionX = static_cast<Standard_Integer> (myMaximumSize * aGlobalBoxSize.x() / aMaxBoxSide);
492 myDimensionY = static_cast<Standard_Integer> (myMaximumSize * aGlobalBoxSize.y() / aMaxBoxSide);
493 myDimensionZ = static_cast<Standard_Integer> (myMaximumSize * aGlobalBoxSize.z() / aMaxBoxSide);
495 myDimensionX = Min (myMaximumSize, Max (myDimensionX, 16));
496 myDimensionY = Min (myMaximumSize, Max (myDimensionY, 16));
497 myDimensionZ = Min (myMaximumSize, Max (myDimensionZ, 16));
499 const BVH_VecNt aGlobalBoxMin = theGeometry.Box().CornerMin();
500 const BVH_VecNt aGlobalBoxMax = theGeometry.Box().CornerMax();
502 const Standard_Integer aVoxelOffset = 2;
504 myCornerMin.x() = aGlobalBoxMin.x() - aVoxelOffset * aGlobalBoxSize.x() / (myDimensionX - 2 * aVoxelOffset);
505 myCornerMin.y() = aGlobalBoxMin.y() - aVoxelOffset * aGlobalBoxSize.y() / (myDimensionY - 2 * aVoxelOffset);
506 myCornerMin.z() = aGlobalBoxMin.z() - aVoxelOffset * aGlobalBoxSize.z() / (myDimensionZ - 2 * aVoxelOffset);
508 myCornerMax.x() = aGlobalBoxMax.x() + aVoxelOffset * aGlobalBoxSize.x() / (myDimensionX - 2 * aVoxelOffset);
509 myCornerMax.y() = aGlobalBoxMax.y() + aVoxelOffset * aGlobalBoxSize.y() / (myDimensionY - 2 * aVoxelOffset);
510 myCornerMax.z() = aGlobalBoxMax.z() + aVoxelOffset * aGlobalBoxSize.z() / (myDimensionZ - 2 * aVoxelOffset);
512 myVoxelSize.x() = (myCornerMax.x() - myCornerMin.x()) / myDimensionX;
513 myVoxelSize.y() = (myCornerMax.y() - myCornerMin.y()) / myDimensionY;
514 myVoxelSize.z() = (myCornerMax.z() - myCornerMin.z()) / myDimensionZ;
518 tbb::parallel_for (tbb::blocked_range<Standard_Integer> (0, myDimensionZ),
519 BVH_ParallelDistanceFieldBuilder<T, N> (this, &theGeometry));
523 BuildSlices (theGeometry, 0, myDimensionZ);
527 return Standard_True;