1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 // File : BLSURFPlugin_Attractor.cxx
22 // Authors : Renaud Nédélec (OCC)
25 // The idea of the algorithm used to calculate the distance on a
26 // non-euclidian parametric surface has been found in the ref. below:
28 // Ref:"Accurate Anisotropic Fast Marching for Diffusion-Based Geodesic Tractography"
29 // S. Jbabdi, P. Bellec, R. Toro, Daunizeau, M. Pélégrini-Issac, and H. Benali1
32 #include "BLSURFPlugin_Attractor.hxx"
33 #include <utilities.h>
38 #include <ShapeAnalysis.hxx>
39 #include <ShapeConstruct_ProjectCurveOnSurface.hxx>
40 #include <Precision.hxx>
41 #include <GeomLib_IsPlanarSurface.hxx>
43 BLSURFPlugin_Attractor::BLSURFPlugin_Attractor ()
64 _isEmpty(true){ MESSAGE("construction of a void attractor"); }
66 BLSURFPlugin_Attractor::BLSURFPlugin_Attractor (const TopoDS_Face& Face, const TopoDS_Shape& Attractor, const std::string& attEntry)
90 _attractorShape = Attractor;
95 bool BLSURFPlugin_Attractor::init(){
100 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(_face);
102 _distance = &BLSURFPlugin_Attractor::_distanceFromMap;
104 if ( GeomLib_IsPlanarSurface( aSurf ).IsPlanar() &&
105 _attractorShape.ShapeType() == TopAbs_VERTEX )
107 // a specific case, the map is not needed
108 gp_Pnt P = BRep_Tool::Pnt( TopoDS::Vertex( _attractorShape ));
109 GeomAPI_ProjectPointOnSurf projector( P, aSurf );
110 if ( projector.IsDone() && projector.NbPoints() == 1 )
112 projector.LowerDistanceParameters(u0,v0);
114 _attractorPnt = aSurf->Value( u0,v0 );
116 _distance = &BLSURFPlugin_Attractor::_distanceFromPoint;
123 // Calculation of the bounds of the face
124 ShapeAnalysis::GetFaceUVBounds(_face,_u1,_u2,_v1,_v2);
129 for (i=0; i<=_gridU; i++){
130 _vectU.push_back(_u1+i*(_u2-_u1)/_gridU) ;
132 for (j=0; j<=_gridV; j++){
133 _vectV.push_back(_v1+j*(_v2-_v1)/_gridV) ;
136 // Initialization of _DMap and _known
137 std::vector<double> temp(_gridV+1,std::numeric_limits<double>::infinity()); // Set distance of all "far" points to Infinity
138 for (i=0; i<=_gridU; i++){
139 _DMap.push_back(temp);
141 std::vector<bool> temp2(_gridV+1,false);
142 for (i=0; i<=_gridU; i++){
143 _known.push_back(temp2);
147 // Determination of the starting points
148 TopExp_Explorer anEdgeExp(_attractorShape, TopAbs_EDGE, TopAbs_FACE);
149 TopExp_Explorer aVertExp(_attractorShape, TopAbs_VERTEX, TopAbs_EDGE);
151 for(; anEdgeExp.More(); anEdgeExp.Next()){
152 const TopoDS_Edge& anEdge = TopoDS::Edge(anEdgeExp.Current());
153 edgeInit(aSurf, anEdge);
156 for(; aVertExp.More(); aVertExp.Next()){
157 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aVertExp.Current());
159 gp_Pnt P = BRep_Tool::Pnt(aVertex);
160 GeomAPI_ProjectPointOnSurf projector( P, aSurf );
161 projector.LowerDistanceParameters(u0,v0);
162 i0 = floor ( (u0 - _u1) * _gridU / (_u2 - _u1) + 0.5 );
163 j0 = floor ( (v0 - _v1) * _gridV / (_v2 - _v1) + 0.5 );
164 TPnt[0]=0.; // Set the distance of the starting point to 0.
168 _trial.insert(TPnt); // Move starting point to _trial
174 void BLSURFPlugin_Attractor::edgeInit(Handle(Geom_Surface) theSurf, const TopoDS_Edge& anEdge){
180 Handle(Geom2d_Curve) aCurve2d;
181 Handle(Geom_Curve) aCurve3d = BRep_Tool::Curve (anEdge, first, last);
182 ShapeConstruct_ProjectCurveOnSurface curveProjector;
183 curveProjector.Init(theSurf, Precision::Confusion());
184 curveProjector.PerformAdvanced (aCurve3d, first, last, aCurve2d);
187 for (i=0; i<=N; i++){
188 P2 = aCurve2d->Value(first + i * (last-first) / N);
189 i0 = floor( (P2.X() - _u1) * _gridU / (_u2 - _u1) + 0.5 );
190 j0 = floor( (P2.Y() - _v1) * _gridV / (_v2 - _v1) + 0.5 );
200 void BLSURFPlugin_Attractor::SetParameters(double Start_Size, double End_Size, double Action_Radius, double Constant_Radius){
201 _startSize = Start_Size;
203 _actionRadius = Action_Radius;
204 _constantRadius = Constant_Radius;
207 double BLSURFPlugin_Attractor::_distanceFromPoint(double u, double v)
209 return _attractorPnt.Distance( _plane->Value( u, v ));
212 double BLSURFPlugin_Attractor::_distanceFromMap(double u, double v){
214 // MG-CADSurf seems to perform a linear interpolation so it's sufficient to give it a non-continuous distance map
215 int i = floor ( (u - _u1) * _gridU / (_u2 - _u1) + 0.5 );
216 int j = floor ( (v - _v1) * _gridV / (_v2 - _v1) + 0.5 );
221 double BLSURFPlugin_Attractor::GetSize(double u, double v)
223 const double attrDist = (this->*_distance)(u,v);
224 const double myDist = 0.5 * (attrDist - _constantRadius + fabs(attrDist - _constantRadius));
228 if (fabs(_actionRadius) <= std::numeric_limits<double>::epsilon()){
229 if (myDist <= std::numeric_limits<double>::epsilon()){
237 return _endSize - (_endSize - _startSize) * exp(- myDist * myDist / (_actionRadius * _actionRadius) );
241 return _startSize + ( 0.5 * (attrDist - _constantRadius + abs(attrDist - _constantRadius)) ) ;
248 void BLSURFPlugin_Attractor::BuildMap() {
250 MESSAGE("building the map");
257 double Guu, Gvv, Guv; // Components of the local metric tensor
262 IJ_Pnt Current_Pnt(2,0);
264 TTrialSet::iterator min;
265 TTrialSet::iterator found;
266 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(_face);
268 // While there are points in "Trial" (representing a kind of advancing front), loop on them -----------------------------------------------------------
269 while (_trial.size() > 0 ) {
270 min = _trial.begin(); // Get trial point with min distance from start
273 _known[i0][j0] = true; // Move it to "Known"
274 _trial.erase(min); // Remove it from "Trial"
276 // Loop on neighbours of the trial min --------------------------------------------------------------------------------------------------------------
277 for (i=i0 - 1 ; i <= i0 + 1 ; i++){
278 if (!aSurf->IsUPeriodic()){ // Periodic conditions in U
284 ip = (i + _gridU + 1) % (_gridU+1); // We get a periodic index :
285 for (j=j0 - 1 ; j <= j0 + 1 ; j++){ // ip=modulo(i,N+2) so that i=-1->ip=N; i=0 -> ip=0 ; ... ; i=N+1 -> ip=0;
286 if (!aSurf->IsVPeriodic()){ // Periodic conditions in V .
293 jp = (j + _gridV + 1) % (_gridV+1);
295 if (!_known[ip][jp]){ // If the distance is not known yet
296 aSurf->D1(_vectU[ip],_vectV[jp],P,D1U,D1V); // Calculate the metric tensor at (i,j)
297 // G(i,j) = | ||dS/du||**2 * |
298 // | <dS/du,dS/dv> ||dS/dv||**2 |
299 Guu = D1U.X()*D1U.X() + D1U.Y()*D1U.Y() + D1U.Z()*D1U.Z(); // Guu = ||dS/du||**2
300 Gvv = D1V.X()*D1V.X() + D1V.Y()*D1V.Y() + D1V.Z()*D1V.Z(); // Gvv = ||dS/dv||**2
301 Guv = D1U.X()*D1V.X() + D1U.Y()*D1V.Y() + D1U.Z()*D1V.Z(); // Guv = Gvu = < dS/du,dS/dv >
302 D_Ref = _DMap[ip][jp]; // Set a ref. distance of the point to its value in _DMap
303 TPnt[0] = D_Ref; // (may be infinite or uncertain)
306 Dist_changed = false;
308 // Loop on neighbours to calculate the min distance from them ---------------------------------------------------------------------------------
309 for (k=i - 1 ; k <= i + 1 ; k++){
310 if (!aSurf->IsUPeriodic()){ // Periodic conditions in U
317 kp = (k + _gridU + 1) % (_gridU+1); // periodic index
318 for (n=j - 1 ; n <= j + 1 ; n++){
319 if (!aSurf->IsVPeriodic()){ // Periodic conditions in V
326 np = (n + _gridV + 1) % (_gridV+1);
327 if (_known[kp][np]){ // If the distance of the neighbour is known
328 // Calculate the distance from (k,n)
329 du = (k-i) * (_u2 - _u1) / _gridU;
330 dv = (n-j) * (_v2 - _v1) / _gridV;
331 Dist = _DMap[kp][np] + sqrt( Guu * du*du + 2*Guv * du*dv + Gvv * dv*dv ); // ds**2 = du'Gdu + 2*du'Gdv + dv'Gdv (G is always symetrical)
332 if (Dist < D_Ref) { // If smaller than ref. distance -> update ref. distance
338 } // End of the loop on neighbours --------------------------------------------------------------------------------------------------------------
340 if (Dist_changed) { // If distance has been updated, update _trial
341 found=_trial.find(TPnt);
342 if (found != _trial.end()){
343 _trial.erase(found); // Erase the point if it was already in _trial
348 _DMap[ip][jp] = D_Ref; // Set it distance to the minimum distance found during the loop above
349 _trial.insert(TPnt); // Insert it (or reinsert it) in _trial
351 } // end if (!_known[ip][jp])
358 } // end of BuildMap()