Salome HOME
Bug fix in colinearize2D(): last seg not handled properly.
[tools/medcoupling.git] / src / MEDCoupling_Swig / MEDCouplingBasicsTest5.py
index 778604242ba316f2f764db2364d42bc80e765eff..ebb5bfe4d1ca5cf713a780946ce11d764937dcf4 100644 (file)
@@ -17,7 +17,6 @@
 #
 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 #
-
 from MEDCoupling import *
 import unittest
 from math import pi,e,sqrt,cos,sin
@@ -1462,7 +1461,7 @@ class MEDCouplingBasicsTest5(unittest.TestCase):
         self.assertEqual(f4.getMesh(),None)
         pass
 
-    # test a simple node to cell convertion of a field
+    # test a simple node to cell conversion of a field
     def testSwig2NodeToCellDiscretization1(self):
         f=MEDCouplingFieldDouble(ON_NODES) ; f.setTime(1.1,2,3)
         a1=DataArrayDouble(4) ; a1.iota()
@@ -1891,7 +1890,7 @@ class MEDCouplingBasicsTest5(unittest.TestCase):
         gaussCoords=refCoords[:] ; gaussCoords[14]=0.9999999999999 # change z of point #4 0.999... instead of 1. because with shape function it leads to division by 0. !
         fGauss.setGaussLocalizationOnType(NORM_PYRA13,refCoords,gaussCoords,weights)
         arrOfDisc2=fGauss.getLocalizationOfDiscr()
-        self.assertTrue(arrOfDisc2.isEqual(coo,1e-10)) # be less exigent 1e-10 instead of 1e-12 due to shape function sensitivity arount 0.,0.,1. !
+        self.assertTrue(arrOfDisc2.isEqual(coo,1e-10)) # be less exigent 1e-10 instead of 1e-12 due to shape function sensitivity around 0.,0.,1. !
         pass
 
     def testSwig2Tri7GP1(self):
@@ -2123,7 +2122,7 @@ class MEDCouplingBasicsTest5(unittest.TestCase):
         pass
 
     def testSwig2Colinearize2D3(self):
-        """ colinearize was too agressive, potentially producing cells with one edge """
+        """ colinearize was too aggressive, potentially producing cells with one edge """
         # Flat polygon  with 3 edges - nothing should happen (min number of edges for a linear polyg)
         coo = DataArrayDouble([0.0,0.0,  2.0,0.0,   1.5,0.0,  1.0,0.0,  0.5,0.0], 5,2)
         m = MEDCouplingUMesh("m", 2)
@@ -2171,6 +2170,23 @@ class MEDCouplingBasicsTest5(unittest.TestCase):
         self.assertTrue( m.getCoords()[8].isEqual( DataArrayDouble([(1.0,0.0)]), 1.0e-12 ) )
         self.assertEqual([0,5], m.getNodalConnectivityIndex().getValues())
 
+    def testSwig2Colinearize2D4(self):
+        """ From ALAMOS. Colinearize around last seg in the connectivity was buggy. """
+        mesh = MEDCouplingUMesh('C3', 2)
+        coo = DataArrayDouble([(-31.838400909874,21.557335816426),(-34.588400909874,16.794196095611),(-33.298676775512,19.225000000000),(-33.547226066398,19.368500000000),(-32.750140188627,22.083728734445),(-35.500140188627,17.320589013630),
+                               (-35.044270549250,17.057392554621),(-32.619779010901,22.008464673393),(-32.554667298175,21.970872408523),(-32.745177043525,22.080863261284),(-32.747658616076,22.082295997864),(-32.682478027213,22.044663967338)])
+        mesh.setCoords(coo)
+        c = DataArrayInt([32, 0, 1, 5, 4, 9, 7, 2, 6, 3, 10, 11, 8])
+        cI = DataArrayInt([0, 13])
+        mesh.setConnectivity(c, cI)
+        mesh.colinearize2D(1.0e-8)
+        coo = mesh.getCoords()
+        self.assertEqual(coo.getNumberOfTuples(), 13)
+        lstPt = coo[12]
+        self.assertAlmostEqual(lstPt[0,0], -32.29427054925)
+        self.assertAlmostEqual(lstPt[0,1], 21.8205322754351)
+        pass
+
     def testSwig2CheckAndPreparePermutation2(self):
         a=DataArrayInt([10003,9999999,5,67])
         self.assertTrue(DataArrayInt.CheckAndPreparePermutation(a).isEqual(DataArrayInt([2,3,0,1])))
@@ -3902,32 +3918,32 @@ class MEDCouplingBasicsTest5(unittest.TestCase):
         arr2=DataArrayInt([(0,1),(2,3),(4,5),(6,7),(8,9),(10,11),(12,13),(14,15),(16,17),(18,19),(20,21),(22,23)]) ; arr2.setInfoOnComponents(["aa","bbb"])
         f2.setArray(arr2) ; f2.setName("f1") ; f2.setTime(2.,3,4)
         #
-        self.assertTrue(f1.isEqual(f2,1e-12,0.))
+        self.assertTrue(f1.isEqual(f2,1e-12,0))
         f1.getArray()[:]*=2
-        self.assertTrue(not f1.isEqual(f2,1e-12,0.))
-        self.assertTrue(not f1.isEqualWithoutConsideringStr(f2,1e-12,0.))
+        self.assertTrue(not f1.isEqual(f2,1e-12,0))
+        self.assertTrue(not f1.isEqualWithoutConsideringStr(f2,1e-12,0))
         f1.getArray()[:]/=2
-        self.assertTrue(f1.isEqual(f2,1e-12,0.))
+        self.assertTrue(f1.isEqual(f2,1e-12,0))
         #
         f1.setName("F1")
-        self.assertTrue(not f1.isEqual(f2,1e-12,0.))
+        self.assertTrue(not f1.isEqual(f2,1e-12,0))
         f1.setName("f1")
-        self.assertTrue(f1.isEqual(f2,1e-12,0.))
+        self.assertTrue(f1.isEqual(f2,1e-12,0))
         #
         f1.getArray().setInfoOnComponents(["aa","bbbb"])
-        self.assertTrue(not f1.isEqual(f2,1e-12,0.))
-        self.assertTrue(f1.isEqualWithoutConsideringStr(f2,1e-12,0.))
+        self.assertTrue(not f1.isEqual(f2,1e-12,0))
+        self.assertTrue(f1.isEqualWithoutConsideringStr(f2,1e-12,0))
         f1.getArray().setInfoOnComponents(["aa","bbb"])
-        self.assertTrue(f1.isEqual(f2,1e-12,0.))
+        self.assertTrue(f1.isEqual(f2,1e-12,0))
         #
         f3=f2.deepCopy()
-        self.assertTrue(f1.isEqual(f3,1e-12,0.))
+        self.assertTrue(f1.isEqual(f3,1e-12,0))
         #
         for fd,expected in ((ON_NODES,False),(ON_CELLS,True)):
             f4=MEDCouplingFieldInt(fd) ; f4.setMesh(m2) ; f4.setTime(2.,3,4)
             arr4=DataArrayInt([(0,1),(2,3),(4,5),(6,7),(8,9),(10,11),(12,13),(14,15),(16,17),(18,19),(20,21),(22,23)]) ; arr4.setInfoOnComponents(["aa","bbb"])
             f4.setArray(arr4) ; f4.setName("f1")
-            self.assertEqual(f1.isEqual(f4,1e-12,0.),expected)
+            self.assertEqual(f1.isEqual(f4,1e-12,0),expected)
             pass
         pass
 
@@ -4304,7 +4320,7 @@ class MEDCouplingBasicsTest5(unittest.TestCase):
         mea=fieldOnCell.getMesh().getMeasureField(True).getArray(); mea.rearrange(48); mea2 = mea.sumPerTuple()
         self.assertTrue(mea2.isEqual(meaRef2,1e-9))
         pass
-    
+
     def testVoronoi3DSurf_1(self):
         tmp=MEDCouplingCMesh("mesh")
         arr=DataArrayDouble(5) ; arr.iota()
@@ -4474,7 +4490,7 @@ class MEDCouplingBasicsTest5(unittest.TestCase):
         self.assertTrue(f3.getMesh().getMeasureField(False).getArray().isEqual(ref,1e-12))
         self.assertTrue(f3.getArray().isEqual(DataArrayDouble([0,1,2,3]),1e-12))
         pass
-    
+
     def testVoronoi3D_4(self):
         """Idem testVoronoi3D_3 except that here quadratic cells are considered"""
         coo=DataArrayDouble([0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.5,0.0,0.0,0.0,0.5,0.0,0.5,0.5,0.5,0.5,0.0,0.5,0.0,0.0,0.5,0.0,0.5],10,3)
@@ -4621,8 +4637,16 @@ class MEDCouplingBasicsTest5(unittest.TestCase):
     def testUMeshComputeEnlargedNeighborsOfNodes(self):
         m=MEDCouplingCMesh() ; arr=DataArrayDouble(4) ; arr.iota() ; m.setCoords(arr,arr) ; m=m.buildUnstructured()
         a,b=m.computeEnlargedNeighborsOfNodes()
-        self.assertTrue(a.isEqual(DataArrayInt([1,4,5,0,2,4,5,6,1,3,5,6,7,2,6,7,0,1,5,8,9,0,1,2,4,6,8,9,10,1,2,3,5,7,9,10,11,2,3,6,10,11,4,5,9,12,13,4,5,6,8,10,12,13,14,5,6,7,9,11,13,14,15,6,7,10,14,15,8,9,13,8,9,10,12,14,9,10,11,13,15,10,11,14])))
-        self.assertTrue(b.isEqual(DataArrayInt([0,3,8,13,16,21,29,37,42,47,55,63,68,71,76,81,84])))
+        aExp=DataArrayInt([1,4,5,0,2,4,5,6,1,3,5,6,7,2,6,7,0,1,5,8,9,0,1,2,4,6,8,9,10,1,2,3,5,7,9,10,11,2,3,6,10,11,4,5,9,12,13,4,5,6,8,10,12,13,14,5,6,7,9,11,13,14,15,6,7,10,14,15,8,9,13,8,9,10,12,14,9,10,11,13,15,10,11,14])
+        bExp=DataArrayInt([0,3,8,13,16,21,29,37,42,47,55,63,68,71,76,81,84])
+        self.assertTrue(a.isEqual(aExp))
+        self.assertTrue(b.isEqual(bExp))
+        m2=m[[1,2,3]]
+        c,d=m2.computeEnlargedNeighborsOfNodes()
+        cExp=DataArrayInt([2,5,6,1,3,5,6,7,2,6,7,5,8,9,1,2,4,6,8,9,1,2,3,5,7,2,3,6,4,5,9,4,5,8])
+        dExp=DataArrayInt([0,0,3,8,11,14,20,25,28,31,34,34,34,34,34,34,34])
+        self.assertTrue(c.isEqual(cExp))
+        self.assertTrue(d.isEqual(dExp))
         pass
 
     def testDAIfindIdsExt1(self):
@@ -4767,7 +4791,22 @@ class MEDCouplingBasicsTest5(unittest.TestCase):
         m.insertNextCell(NORM_POLYGON,[0,1,2,3,4,5])
         self.assertTrue(m.computePlaneEquationOf3DFaces().isEqual(DataArrayDouble([0,1,0,-1],1,4),1e-12))
         pass
-    
+
+    def testSimplifyPolyhedra(self):
+        mesh = MEDCouplingUMesh('mesh', 3)
+        coo = DataArrayDouble([(-0.01225,-0.0212176,0.02),(-0.00634107,-0.0236652,0.02),(1.50019e-18,-0.0245,0.02),(0.00634107,-0.0236652,0.02),(0.01225,-0.0212176,0.02),(-0.0153864,-0.02665,0),(-0.00714085,-0.02665,0),(1.63184e-18,-0.02665,0),(0.00714085,-0.02665,0),(0.0153864,-0.02665,0),(-0.00714085,-0.02665,0.0101475),(1.63184e-18,-0.02665,0.013145),(0.00714085,-0.02665,0.0101475),(-0.013,-0.0225167,0.02),(-0.0067293,-0.0251141,0.02),(1.59204e-18,-0.026,0.02),(0.0067293,-0.0251141,0.02),(0.013,-0.0225167,0.02),(-0.0161658,-0.028,0),(-0.00750258,-0.028,0),(1.71451e-18,-0.028,0),(0.00750258,-0.028,0),(0.0161658,-0.028,0),(-0.00750258,-0.028,0.0105625),(1.71451e-18,-0.028,0.0136825),(0.00750258,-0.028,0.0105625)])
+        mesh.setCoords(coo)
+        c = DataArrayInt([31, 13, 14, 15, 16, 17, 4, 3, 2, 1, 0, -1, 18, 5, 6, 7, 8, 9, 22, 21, 20, 19, -1, 19, 23, 18, -1, 23, 14, 13, 18, -1, 20, 24, 23, 19, -1, 24, 15, 14, 23, -1, 21, 25, 24, 20, -1, 25, 16, 15, 24, -1, 22, 25, 21, -1, 22, 17, 16, 25, -1, 9, 4, 17, 22, -1, 8, 12, 9, -1, 12, 3, 4, 9, -1, 7, 11, 12, 8, -1, 11, 2, 3, 12, -1, 6, 10, 11, 7, -1, 10, 1, 2, 11, -1, 5, 10, 6, -1, 5, 0, 1, 10, -1, 18, 13, 0, 5])
+        cI = DataArrayInt([0, 108])
+        mesh.setConnectivity(c, cI)
+        mesh.simplifyPolyhedra(1.0e-8)
+        c, cI = mesh.getNodalConnectivity(), mesh.getNodalConnectivityIndex()
+        tgt_c = DataArrayInt([31, 23, 18, 19, 20, 21, 22, 25, 24, -1, 12, 9, 8, 7, 6, 5, 10, 11, -1, 13, 14, 15, 16, 17, 4, 3, 2, 1, 0, -1, 18, 5, 6, 7, 8, 9, 22, 21, 20, 19, -1, 23, 14, 13, 18, -1, 24, 15, 14, 23, -1, 25, 16, 15, 24, -1, 22, 17, 16, 25, -1, 9, 4, 17, 22, -1, 12, 3, 4, 9, -1, 11, 2, 3, 12, -1, 10, 1, 2, 11, -1, 5, 0, 1, 10, -1, 18, 13, 0, 5])
+        tgt_cI = DataArrayInt([0, 90])
+        self.assertEqual(c.getValues(), tgt_c.getValues())
+        self.assertEqual(cI.getValues(), tgt_cI.getValues())
+        pass
+
     pass
 
 if __name__ == '__main__':