Salome HOME
fitShapePointsToMesh: option to split shape to adjust, do not take into account super...
authorPaul RASCLE <paul.rascle@openfields.fr>
Sun, 26 Jul 2020 10:06:49 +0000 (12:06 +0200)
committerYOANN AUDOUIN <B61570@dsp0851742.postes.calibre.edf.fr>
Fri, 30 Oct 2020 16:06:22 +0000 (17:06 +0100)
src/HYDROTools/shapesGroups.py

index 147a71e838d1fd4a5ca87cf9909b776a8d10c4c2..18dedea56ae3a3a7b25330e9142302fa090a6a44 100644 (file)
@@ -222,14 +222,15 @@ def exploreEdgeGroups(meshFile, outputDirectory="", offsetX=0., offsetY=0.):
         writeShapePoints(mcMesh, fullGrpName, nodeChains, outputDirectory, offsetX, offsetY)
 
 
-def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder=True):
+def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder=True, splitShapeAdjusted=True):
     """
     shapeFileToAdjust must be a closed line or polygon crossing freeBorderShapefile in 2 points.
-    Find in shapeFileToAdjust and in freeBorderShapefile the two closest corresponding points and move the points in shapeFileToAdjust to correspond to the points found in freeBorderShapefile. Split shapeFileToAdjust in two parts (inside or outside freeBorder). If requested, split freeBorderShapefile in two parts.
+    Find in shapeFileToAdjust and in freeBorderShapefile the two closest corresponding points and move the points in shapeFileToAdjust to correspond to the points found in freeBorderShapefile. Split shapeFileToAdjust in two parts (inside or outside freeBorder). If requested, split freeBorderShapefile in two parts. Same for shapeFileToAdjust.
     parameters:
     freeBorderShapefile: a set of free border lines, as generated by the functions freeBordersGroup and exploreEdgeGroups.
     shapefileToAdjust: a closed line or polygon, supposed to be drawn in qgis to pass as close as possible to the points to be connected, on the free border.
     splitFreeBorder: boolean default True
+    splitShapeAdjusted: boolean default True
     """
     print(" === fitShapePointsToMesh", freeBorderShapefile, shapefileToAdjust)
 
@@ -254,7 +255,14 @@ def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder
     shapes = sf.shapes()
     sfta = sf.shape(0)
     pdist =[]
+    x0 = -1.e30
+    y0 = -1.e30
     for i,p in enumerate(sfta.points):
+        d = math.sqrt((x0-p[0])*(x0-p[0]) + (y0-p[1])*(y0-p[1]))
+        x0 = p[0]
+        y0 = p[1]
+        if d < 1.e-5:
+            continue # do not take into account superposed points in shape to adjust
         dmin = 1.e30
         jmin = -1
         for j,pfb in enumerate(fbs.points):
@@ -273,6 +281,8 @@ def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder
     else:
         ifb1 = pdist[1][1]
         ifb2 = pdist[0][1]
+    print("i1, i2, len(sfta.points)", i1, i2, len(sfta.points))
+    print("ifb1, ifb2, len(fbs.points)", ifb1, ifb2, len(fbs.points))
 
     # --- write the adusted shapefile: free border closest points replaced with corresponding points
     #     on the free border. two polylines, one inside the domain, one outside
@@ -285,24 +295,39 @@ def fitShapePointsToMesh(freeBorderShapefile, shapefileToAdjust, splitFreeBorder
     w.shapeType = 3
     w.field('name', 'C')
 
-    chaincoords = []
-    chaincoords.append(fbs.points[ifb1])
-    for i in range(i1+1, i2):
-        chaincoords.append(sfta.points[i])
-    chaincoords.append(fbs.points[ifb2])
-    w.line([chaincoords])
-    w.record(chainName + '_0')
-
-    chaincoords = []
-    chaincoords.append(fbs.points[ifb2])
-    if i2+1 < len(sfta.points):
-        for i in range(i2+1, len(sfta.points)):
+    if splitShapeAdjusted:
+        chaincoords = []
+        chaincoords.append(fbs.points[ifb1])
+        for i in range(i1+1, i2):
+            chaincoords.append(sfta.points[i])
+        chaincoords.append(fbs.points[ifb2])
+        w.line([chaincoords])
+        w.record(chainName + '_0')
+
+        chaincoords = []
+        chaincoords.append(fbs.points[ifb2])
+        if i2+1 < len(sfta.points):
+            for i in range(i2+1, len(sfta.points)):
+                chaincoords.append(sfta.points[i])
+        for i in range(i1):
             chaincoords.append(sfta.points[i])
-    for i in range(i1):
-        chaincoords.append(sfta.points[i])
-    chaincoords.append(fbs.points[ifb1])
-    w.line([chaincoords])
-    w.record(chainName + '_1')
+        chaincoords.append(fbs.points[ifb1])
+        w.line([chaincoords])
+        w.record(chainName + '_1')
+    else:
+        chaincoords = []
+        chaincoords.append(fbs.points[ifb1])
+        for i in range(i1+1, i2):
+            chaincoords.append(sfta.points[i])
+        chaincoords.append(fbs.points[ifb2])
+        if i2+1 < len(sfta.points):
+            for i in range(i2+1, len(sfta.points)):
+                chaincoords.append(sfta.points[i])
+        for i in range(i1):
+            chaincoords.append(sfta.points[i])
+        w.line([chaincoords])
+        w.record(chainName)
+
     w.close()
 
     if splitFreeBorder: