1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2022 CEA/DEN, EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 # Author : Anthony Geay
25 from medcoupling import *
26 from paraview.simple import *
27 from MEDReaderHelper import WriteInTmpDir,RetriveBaseLine
30 """ This test focuses on GenerateVector part.
32 fname="testMEDReader10.med"
34 coo=DataArrayDouble([(0.,0.,0.),(1.,0.,0.),(2.,0.,0.),(3.,0.,0.),(0.,1.,0.),(1.,1.,0.),(2.,1.,0.),(3.,1.,0.),(0.,0.,1.),(1.,0.,1.),(2.,0.,1.),(3.,0.,1.),(0.,1.,1.),(1.,1.,1.),(2.,1.,1.),(3.,1.,1.)])
35 m=MEDCouplingUMesh("myMesh",3) ; m.setCoords(coo)
37 m.insertNextCell(NORM_HEXA8,[0,4,5,1,8,12,13,9]) ; m.insertNextCell(NORM_HEXA8,[1,5,6,2,9,13,14,10]) ; m.insertNextCell(NORM_HEXA8,[2,6,7,3,10,14,15,11])
38 f0=MEDCouplingFieldDouble(ON_NODES) ; f0.setName("f0NbComp1") ; f0.setMesh(m) ;
39 arr=DataArrayDouble(8) ; arr.iota() ; arr=DataArrayDouble.Aggregate(arr,arr+8) ; arr.setInfoOnComponents(["1st"])
41 f1=MEDCouplingFieldDouble(ON_NODES) ; f1.setName("f1NbComp2") ; f1.setMesh(m) ;
42 arr=DataArrayDouble([(-1.,-1.),(0.,0.5),(0.,0.5),(1.,-1.),(-1.,1.),(0.,-0.5),(0.,-0.5),(1.,1.)]) ; arr=DataArrayDouble.Aggregate(arr,arr) ; arr.setInfoOnComponents(["1st","2nd"])
44 f2=MEDCouplingFieldDouble(ON_NODES) ; f2.setName("f2NbComp3") ; f2.setMesh(m) ;
45 arr=DataArrayDouble([(-1.,-1.,-1.),(0.,0.5,0.),(0.,0.5,0.),(1.,-1.,-1.),(-1.,1.,-1.),(0.,-0.5,0.),(0.,-0.5,0.),(1.,1.,-1)]) ; arr2=arr[:] ; arr2[[0,3,4,7],2]=1.
46 arr=DataArrayDouble.Aggregate(arr,arr2) ; arr.setInfoOnComponents(["1st","2nd","3rd"])
48 f3=MEDCouplingFieldDouble(ON_NODES) ; f3.setName("f3NbComp4") ; f3.setMesh(m) ;
49 arr=DataArrayDouble([(-1.,-1.,-1.),(0.,0.5,0.),(0.,0.5,0.),(1.,-1.,-1.),(-1.,1.,-1.),(0.,-0.5,0.),(0.,-0.5,0.),(1.,1.,-1)]) ; arr2=arr[:] ; arr2[[0,3,4,7],2]=1.
50 arr=DataArrayDouble.Aggregate(arr,arr2) ; arr3=DataArrayDouble(16) ; arr3.iota() ; arr=DataArrayDouble.Meld([arr,arr3])
51 arr.setInfoOnComponents(["1st","2nd","3rd","4th"])
54 WriteMesh(fname,m,True)
55 WriteFieldUsingAlreadyWrittenMesh(fname,f0)
56 WriteFieldUsingAlreadyWrittenMesh(fname,f1)
57 WriteFieldUsingAlreadyWrittenMesh(fname,f2)
58 WriteFieldUsingAlreadyWrittenMesh(fname,f3)
63 def test(baseline_file):
64 fname = GenerateCase()
65 ################### MED write is done -> Go to MEDReader
66 myMedReader=MEDReader(FileNames=[fname])
67 keys=myMedReader.GetProperty("FieldsTreeInfo")[::2]
68 # list all the names of arrays that can be seen (including their spatial discretization)
69 arr_name_with_dis=[elt.split("/")[-1] for elt in keys]
70 # list all the names of arrays (Equal to those in the MED File)
71 arr_name=[elt.split(myMedReader.GetProperty("Separator").GetData())[0] for elt in arr_name_with_dis]
72 myMedReader.FieldsStatus=keys
73 myMedReader.VectorsProperty=1
75 if '-D' not in sys.argv:
76 RenderView1=GetRenderView()
77 DataRepresentation1 = Show()
78 DataRepresentation1.EdgeColor = [0.0, 0.0, 0.5]
79 DataRepresentation1.SelectionPointFieldDataArrayName='f0NbComp1'
80 DataRepresentation1.ScalarOpacityUnitDistance = 2.299619191183727
81 #DataRepresentation1.ExtractedBlockIndex = 1
82 DataRepresentation1.ScaleFactor = 0.3
83 RenderView1.CenterOfRotation = [1.5, 0.5, 0.5]
84 RenderView1.CameraPosition = [1.5, 0.5, 6.907227082229696]
85 RenderView1.CameraFocalPoint = [1.5, 0.5, 0.5]
86 RenderView1.CameraParallelScale = 1.6583123951777
87 DataRepresentation1.Representation = 'Wireframe'
89 wbv=WarpByVector(Input=myMedReader)
91 wbv.Vectors=['POINTS','f3NbComp4_Vector']
92 print( list(wbv.PointData.keys()) )
93 assert(list(wbv.PointData.keys())==['f0NbComp1','f1NbComp2','f1NbComp2_Vector','f2NbComp3','f3NbComp4','f3NbComp4_Vector','vtkGhostType'])
95 DataRepresentation2 = Show()
96 DataRepresentation2.EdgeColor = [0.0, 0.0, 0.5000076295109483]
97 DataRepresentation2.SelectionPointFieldDataArrayName = 'f0NbComp1'
98 DataRepresentation2.SelectionCellFieldDataArrayName = 'FamilyIdCell'
99 DataRepresentation2.ScalarOpacityUnitDistance = 4.546673931685981
100 #DataRepresentation2.ExtractedBlockIndex = 1
101 DataRepresentation2.ScaleFactor = 0.5
102 DataRepresentation1.Visibility = 0
103 DataRepresentation1.ColorArrayName = ('POINT_DATA', '')
104 DataRepresentation1.Visibility = 1
105 a4_f3NbComp4_PVLookupTable = GetLookupTableForArray( "f3NbComp4", 4, RGBPoints=[1.118033988749895, 0.23, 0.299, 0.754, 8.108851429645696, 0.865, 0.865, 0.865, 15.0996688705415, 0.706, 0.016, 0.15], VectorMode='Magnitude', NanColor=[0.25, 0.0, 0.0], ColorSpace='Diverging', ScalarRangeInitialized=1.0 )
106 a4_f3NbComp4_PiecewiseFunction = CreatePiecewiseFunction( Points=[1.118033988749895, 0.0, 0.5, 0.0, 15.0996688705415, 1.0, 0.5, 0.0] )
107 DataRepresentation1.ScalarOpacityFunction = a4_f3NbComp4_PiecewiseFunction
108 DataRepresentation1.LookupTable = a4_f3NbComp4_PVLookupTable
109 a4_f3NbComp4_PVLookupTable.ScalarOpacityFunction = a4_f3NbComp4_PiecewiseFunction
110 DataRepresentation2.ScalarOpacityFunction = a4_f3NbComp4_PiecewiseFunction
111 DataRepresentation2.ColorArrayName = ('POINT_DATA', 'f3NbComp4')
112 DataRepresentation2.LookupTable = a4_f3NbComp4_PVLookupTable
113 RenderView1.CameraViewUp = [0.32310237626907823, -0.28721735329781684, -0.901726702728644]
114 RenderView1.CameraPosition = [0.17151044563787257, 6.31903262423349, -1.8294921560963984]
115 RenderView1.CameraFocalPoint = [1.4999999999999996, 0.49999999999999944, 0.4999999999999996]
117 RenderView1.ViewSize =[300,300]
120 # compare with baseline image
121 import vtk.test.Testing
122 from vtk.util.misc import vtkGetTempDir
123 vtk.test.Testing.VTK_TEMP_DIR = vtk.util.misc.vtkGetTempDir()
124 vtk.test.Testing.compareImage(GetActiveView().GetRenderWindow(), baseline_file,
126 vtk.test.Testing.interact()
128 if __name__ == "__main__":
129 outImgName="testMEDReader10.png"
130 baseline_file = RetriveBaseLine(outImgName)