1 // Copyright (C) 2007-2012 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.
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>
42 BLSURFPlugin_Attractor::BLSURFPlugin_Attractor ()
63 _isEmpty(true){ MESSAGE("construction of a void attractor"); }
65 BLSURFPlugin_Attractor::BLSURFPlugin_Attractor (const TopoDS_Face& Face, const TopoDS_Shape& Attractor, const std::string& attEntry)
89 _attractorShape = Attractor;
94 bool BLSURFPlugin_Attractor::init(){
99 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(_face);
101 // Calculation of the bounds of the face
102 ShapeAnalysis::GetFaceUVBounds(_face,_u1,_u2,_v1,_v2);
107 for (i=0; i<=_gridU; i++){
108 _vectU.push_back(_u1+i*(_u2-_u1)/_gridU) ;
110 for (j=0; j<=_gridV; j++){
111 _vectV.push_back(_v1+j*(_v2-_v1)/_gridV) ;
114 // Initialization of _DMap and _known
115 std::vector<double> temp(_gridV+1,std::numeric_limits<double>::infinity()); // Set distance of all "far" points to Infinity
116 for (i=0; i<=_gridU; i++){
117 _DMap.push_back(temp);
119 std::vector<bool> temp2(_gridV+1,false);
120 for (i=0; i<=_gridU; i++){
121 _known.push_back(temp2);
125 // Determination of the starting points
126 TopExp_Explorer anEdgeExp(_attractorShape, TopAbs_EDGE, TopAbs_FACE);
127 TopExp_Explorer aVertExp(_attractorShape, TopAbs_VERTEX, TopAbs_EDGE);
129 for(; anEdgeExp.More(); anEdgeExp.Next()){
130 const TopoDS_Edge& anEdge = TopoDS::Edge(anEdgeExp.Current());
131 edgeInit(aSurf, anEdge);
134 for(; aVertExp.More(); aVertExp.Next()){
135 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aVertExp.Current());
137 gp_Pnt P = BRep_Tool::Pnt(aVertex);
138 GeomAPI_ProjectPointOnSurf projector( P, aSurf );
139 projector.LowerDistanceParameters(u0,v0);
140 i0 = floor ( (u0 - _u1) * _gridU / (_u2 - _u1) + 0.5 );
141 j0 = floor ( (v0 - _v1) * _gridV / (_v2 - _v1) + 0.5 );
142 TPnt[0]=0.; // Set the distance of the starting point to 0.
146 _trial.insert(TPnt); // Move starting point to _trial
152 void BLSURFPlugin_Attractor::edgeInit(Handle(Geom_Surface) theSurf, const TopoDS_Edge& anEdge){
158 Handle(Geom2d_Curve) aCurve2d;
159 Handle(Geom_Curve) aCurve3d = BRep_Tool::Curve (anEdge, first, last);
160 ShapeConstruct_ProjectCurveOnSurface curveProjector;
161 curveProjector.Init(theSurf, Precision::Confusion());
162 curveProjector.PerformAdvanced (aCurve3d, first, last, aCurve2d);
165 for (i=0; i<=N; i++){
166 P2 = aCurve2d->Value(first + i * (last-first) / N);
167 i0 = floor( (P2.X() - _u1) * _gridU / (_u2 - _u1) + 0.5 );
168 j0 = floor( (P2.Y() - _v1) * _gridV / (_v2 - _v1) + 0.5 );
178 void BLSURFPlugin_Attractor::SetParameters(double Start_Size, double End_Size, double Action_Radius, double Constant_Radius){
179 _startSize = Start_Size;
181 _actionRadius = Action_Radius;
182 _constantRadius = Constant_Radius;
185 double BLSURFPlugin_Attractor::_distance(double u, double v){
187 // BLSURF seems to perform a linear interpolation so it's sufficient to give it a non-continuous distance map
188 int i = floor ( (u - _u1) * _gridU / (_u2 - _u1) + 0.5 );
189 int j = floor ( (v - _v1) * _gridV / (_v2 - _v1) + 0.5 );
195 double BLSURFPlugin_Attractor::GetSize(double u, double v){
196 double myDist = 0.5 * (_distance(u,v) - _constantRadius + fabs(_distance(u,v) - _constantRadius));
200 if (fabs(_actionRadius) <= std::numeric_limits<double>::epsilon()){
201 if (myDist <= std::numeric_limits<double>::epsilon()){
209 return _endSize - (_endSize - _startSize) * exp(- myDist * myDist / (_actionRadius * _actionRadius) );
213 return _startSize + ( 0.5 * (_distance(u,v) - _constantRadius + abs(_distance(u,v) - _constantRadius)) ) ;
219 void BLSURFPlugin_Attractor::BuildMap(){
221 MESSAGE("building the map");
228 double Guu, Gvv, Guv; // Components of the local metric tensor
233 IJ_Pnt Current_Pnt(2,0);
235 TTrialSet::iterator min;
236 TTrialSet::iterator found;
237 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(_face);
239 // While there are points in "Trial" (representing a kind of advancing front), loop on them -----------------------------------------------------------
240 while (_trial.size() > 0 ){
241 min = _trial.begin(); // Get trial point with min distance from start
244 _known[i0][j0] = true; // Move it to "Known"
245 _trial.erase(min); // Remove it from "Trial"
247 // Loop on neighbours of the trial min --------------------------------------------------------------------------------------------------------------
248 for (i=i0 - 1 ; i <= i0 + 1 ; i++){
249 if (!aSurf->IsUPeriodic()){ // Periodic conditions in U
255 ip = (i + _gridU + 1) % (_gridU+1); // We get a periodic index :
256 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;
257 if (!aSurf->IsVPeriodic()){ // Periodic conditions in V .
264 jp = (j + _gridV + 1) % (_gridV+1);
266 if (!_known[ip][jp]){ // If the distance is not known yet
267 aSurf->D1(_vectU[ip],_vectV[jp],P,D1U,D1V); // Calculate the metric tensor at (i,j)
268 // G(i,j) = | ||dS/du||**2 * |
269 // | <dS/du,dS/dv> ||dS/dv||**2 |
270 Guu = D1U.X()*D1U.X() + D1U.Y()*D1U.Y() + D1U.Z()*D1U.Z(); // Guu = ||dS/du||**2
271 Gvv = D1V.X()*D1V.X() + D1V.Y()*D1V.Y() + D1V.Z()*D1V.Z(); // Gvv = ||dS/dv||**2
272 Guv = D1U.X()*D1V.X() + D1U.Y()*D1V.Y() + D1U.Z()*D1V.Z(); // Guv = Gvu = < dS/du,dS/dv >
273 D_Ref = _DMap[ip][jp]; // Set a ref. distance of the point to its value in _DMap
274 TPnt[0] = D_Ref; // (may be infinite or uncertain)
277 Dist_changed = false;
279 // Loop on neighbours to calculate the min distance from them ---------------------------------------------------------------------------------
280 for (k=i - 1 ; k <= i + 1 ; k++){
281 if (!aSurf->IsUPeriodic()){ // Periodic conditions in U
288 kp = (k + _gridU + 1) % (_gridU+1); // periodic index
289 for (n=j - 1 ; n <= j + 1 ; n++){
290 if (!aSurf->IsVPeriodic()){ // Periodic conditions in V
297 np = (n + _gridV + 1) % (_gridV+1);
298 if (_known[kp][np]){ // If the distance of the neighbour is known
299 // Calculate the distance from (k,n)
300 du = (k-i) * (_u2 - _u1) / _gridU;
301 dv = (n-j) * (_v2 - _v1) / _gridV;
302 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)
303 if (Dist < D_Ref) { // If smaller than ref. distance -> update ref. distance
309 } // End of the loop on neighbours --------------------------------------------------------------------------------------------------------------
311 if (Dist_changed) { // If distance has been updated, update _trial
312 found=_trial.find(TPnt);
313 if (found != _trial.end()){
314 _trial.erase(found); // Erase the point if it was already in _trial
319 _DMap[ip][jp] = D_Ref; // Set it distance to the minimum distance found during the loop above
320 _trial.insert(TPnt); // Insert it (or reinsert it) in _trial
322 } // end if (!_known[ip][jp])
329 } // end of BuildMap()