Debian Codegraph @

packages/seacas/libraries/ioss/src/visualization/catalyst/phactori/Operation/PhactoriFollowSurfaceSide1.py

raw
1 # Copyright(C) 1999-2020, 2024 National Technology & Engineering Solutions
2 # of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 # NTESS, the U.S. Government retains certain rights in this software.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
8 #
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 #
17 # * Neither the name of NTESS nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 from phactori import *
34 from paraview.simple import *
35 import sys
36 from .PhactoriParaviewMultiBlockRecursion import *
37 from .PhactoriVectorLibrary import *
38
39 #phactori_combine_to_single_python_file_subpiece_begin_1
40
41 global gCellStatusTrack
42 gCellStatusTrack = []
43 for ii in range(0,25):
44 gCellStatusTrack.append(0)
45
46 class CellEdgeAngleMetricParameters():
47 def __init__(self):
48 self.numPointsPerBlock = None
49 self.numCellsPerBlock = None
50 self.UseSmallestAngle = True
51 self.OffsetIndex = 0
52
53 def Initialize(self, inUseSmallestAngle, inOffsetIndex):
54 self.numPointsPerBlock = []
55 self.numCellsPerBlock = []
56 self.UseSmallestAngle = inUseSmallestAngle
57 self.OffsetIndex = inOffsetIndex
58
59 class PhactoriFollowSurfaceSide1(PhactoriOperationSpecifics):
60 """experimental filter to find the lengths of the edges of cells in the mesh"""
61 def __init__(self):
62 PhactoriOperationSpecifics.__init__(self)
63 self.inPvFilter = None
64 self.pfcelProgrammableFilter = None
65 self.mProgFilterString = None
66 self.ProgrammableFilterOutputCellVariableName = "surfacestatus"
67 self.UseSmallestAngle = True
68 self.OffsetIndex = 0
69
70 def ParseParametersFromJson(self, inJson):
71
72 keyval3 = "output nth smallest angle"
73 keyval4 = "output nth largest angle"
74 if keyval3 in inJson:
75 self.UseSmallestAngle = True
76 self.OffsetIndex = inJson[keyval3]
77 elif keyval4 in inJson:
78 self.UseSmallestAngle = False
79 self.OffsetIndex = inJson[keyval3]
80 else:
81 self.UseSmallestAngle = True
82 self.OffsetIndex = 0
83
84 keyval10 = "output cell variable name"
85 if keyval10 in inJson:
86 self.ProgrammableFilterOutputCellVariableName = inJson[keyval10]
87
88 def CreateParaViewFilter(self, inInputFilter):
89 """create the MergeBlocks filter for ParaView"""
90 if PhactoriDbg(100):
91 myDebugPrint3("PhactoriFollowSurfaceSide1.CreateParaViewFilter "
92 "entered\n", 100)
93 self.inPvFilter = inInputFilter
94
95 #savedActiveSource = GetActiveSource()
96
97 UpdatePipelineWithCurrentTimeArgument(inInputFilter)
98 #newParaViewFilter = MergeBlocks(inInputFilter)
99
100 #SetActiveSource(newParaViewFilter)
101 #UpdatePipelineWithCurrentTimeArgument(newParaViewFilter)
102 #SetActiveSource(savedActiveSource)
103
104 self.pfcelProgrammableFilter = ProgrammableFilter(Input = self.inPvFilter)
105 self.pfcelProgrammableFilter.CopyArrays = 1
106 self.CreateProgrammableFilterString()
107 self.pfcelProgrammableFilter.Script = self.mProgFilterString
108 self.pfcelProgrammableFilter.UpdatePipeline()
109
110 if PhactoriDbg(100):
111 myDebugPrint3("PhactoriFollowSurfaceSide1.CreateParaViewFilter "
112 "returning\n", 100)
113
114 return self.pfcelProgrammableFilter
115
116 #@staticmethod
117 #def IncrementItemInCellsTouchCountMap(onePt, cellsTouchCountMap):
118 # xx = onePt[0]
119 # yy = onePt[1]
120 # zz = onePt[2]
121 # if xx not in cellsTouchCountMap:
122 # cellsTouchCountMap[xx] = {}
123 # lvlx = cellsTouchCountMap[xx]
124 # if yy not in lvlx:
125 # lvlx[yy] = {}
126 # lvly = lvlx[yy]
127 # if zz not in lvly:
128 # lvly[zz] = 0
129 # lvlz = lvly[zz]
130 # lvly[zz] = lvlz + 1
131
132 @staticmethod
133 def IncrementTouchingCellCountForAllPointsInOneCell(inInputCsData, inParameters, inCellIndex,
134 cellsTouchingPointCount):
135 oneCell = inInputCsData.GetCell(inCellIndex)
136
137 numPoints = oneCell.GetNumberOfPoints()
138 pointIds = oneCell.GetPointIds()
139 onePt = [0.0,0.0,0.0]
140 for ii in range(0, numPoints):
141 onePointId = pointIds.GetId(ii)
142 cellsTouchingPointCount[onePointId] += 1
143 inInputCsData.GetPoint(onePointId, onePt)
144
145 @staticmethod
146 def GetEdgeLength(inInputCsData, oneFace, edgeIndex):
147 oneEdge = oneFace.GetEdge(edgeIndex)
148 edgePointIds = oneEdge.GetPointIds()
149 ptA = inInputCsData.GetPoint(edgePointIds.GetId(0))
150 ptB = inInputCsData.GetPoint(edgePointIds.GetId(1))
151 xx = ptB[0] - ptA[0]
152 yy = ptB[1] - ptA[1]
153 zz = ptB[2] - ptA[2]
154 return math.sqrt(xx*xx + yy*yy + zz*zz)
155
156 @staticmethod
157 def GetFaceSize(inInputCsData, oneFace):
158 numFaceEdges = oneFace.GetNumberOfEdges()
159 if numFaceEdges != 4:
160 myDebugPrint3AndException("numFaceEdges != 4 error\n")
161 #use perimeter as a stand-in for area on non quads
162 retVal = 0
163 for ii in range(0, numFaceEdges):
164 retVal += GetEdgeLength(inInputCsData, oneFace, ii)
165 else:
166 #assume roughly rectangular calculation for size
167 side0Len = PhactoriFollowSurfaceSide1.GetEdgeLength(inInputCsData, oneFace, 0)
168 side1Len = PhactoriFollowSurfaceSide1.GetEdgeLength(inInputCsData, oneFace, 1)
169 side2Len = PhactoriFollowSurfaceSide1.GetEdgeLength(inInputCsData, oneFace, 2)
170 side3Len = PhactoriFollowSurfaceSide1.GetEdgeLength(inInputCsData, oneFace, 3)
171 retVal = (side0Len + side2Len) * (side1Len * side3Len)
172 return retVal
173
174 @staticmethod
175 def GetSortedFaceSizeList(inInputCsData, oneCell):
176 faceSizeList = []
177 numFaces = oneCell.GetNumberOfFaces()
178 for faceNdx in range(0, numFaces):
179 oneFace = oneCell.GetFace(faceNdx)
180 faceSizeList.append(PhactoriFollowSurfaceSide1.GetFaceSize(inInputCsData, oneFace))
181 return sorted(faceSizeList)
182
183 @staticmethod
184 def IsFaceOneOfTwoLargestOnCell(inInputCsData, oneCell, oneFace):
185 SortedFaceSizeList = PhactoriFollowSurfaceSide1.GetSortedFaceSizeList(inInputCsData, oneCell)
186 testFaceSize = PhactoriFollowSurfaceSide1.GetFaceSize(inInputCsData, oneFace)
187 if PhactoriDbg(100):
188 myDebugPrint3("testFaceSize: " + str(testFaceSize) + " SortedFaceSizeList:\n" + str(SortedFaceSizeList) + "\n")
189 #for ii, ss in enumerate(SortedFaceSizeList):
190 # myDebugPrint3(str(ii) + ": " + str(ss) +":\n")
191 if len(SortedFaceSizeList) < 2:
192 #shouldn't happen
193 myDebugPrint3AndException("len(SortedFaceSizeList) < 2 error\n")
194 return False
195 if PhactoriDbg(100):
196 myDebugPrint3("testFaceSize >= SortedFaceSizeList[-2]: " + str(testFaceSize >= SortedFaceSizeList[-2]) + "\n")
197 return (testFaceSize >= SortedFaceSizeList[-2])
198
199 @staticmethod
200 def SurfaceCornerCellFaceFacesExterior(oneFace, cellsTouchingPointCount):
201 numFacePoints = oneFace.GetNumberOfPoints()
202 facePointIds = oneFace.GetPointIds()
203 numOutsideVertices = 0
204 for ii in range(0, numFacePoints):
205 if cellsTouchingPointCount[ii] <= 0:
206 return True
207 return False
208
209 @staticmethod
210 def TestOutSideSurfaceCornerCellForMaxAreaFacing(
211 inInputCsData, oneCell, pointIds, cellsTouchingPointCount):
212 #faces with 4 2 2 0 need to be tested
213 numFaces = oneCell.GetNumberOfFaces()
214 for faceIndex in range(0, numFaces):
215 oneFace = oneCell.GetFace(faceIndex)
216 if PhactoriFollowSurfaceSide1.SurfaceCornerCellFaceFacesExterior(oneFace, cellsTouchingPointCount):
217 if PhactoriFollowSurfaceSide1.IsFaceOneOfTwoLargestOnCell(inInputCsData, oneCell, oneFace):
218 #this cell is facing the big area cell side
219 return 7
220 return 4
221
222 @staticmethod
223 def SurfaceEdgeCellFaceFacesExterior(oneFace, cellsTouchingPointCount):
224 numFacePoints = oneFace.GetNumberOfPoints()
225 facePointIds = oneFace.GetPointIds()
226 numOutsideVertices = 0
227 for ii in range(0, numFacePoints):
228 if cellsTouchingPointCount[ii] <= 2:
229 numOutsideVertices += 1
230 return (numOutsideVertices >= 2)
231
232 @staticmethod
233 def TestOutSideSurfaceEdgeCellForMaxAreaFacing(
234 inInputCsData, oneCell, pointIds, cellsTouchingPointCount):
235 #faces with 4 4 2 2 need to be tested
236 numFaces = oneCell.GetNumberOfFaces()
237 for faceIndex in range(0, numFaces):
238 oneFace = oneCell.GetFace(faceIndex)
239 if PhactoriFollowSurfaceSide1.SurfaceEdgeCellFaceFacesExterior(oneFace, cellsTouchingPointCount):
240 if PhactoriFollowSurfaceSide1.IsFaceOneOfTwoLargestOnCell(inInputCsData, oneCell, oneFace):
241 #this cell is facing the big area cell side
242 return 6
243 return 3
244
245 @staticmethod
246 def TestOutSideSurfaceCellForMaxAreaFacing(
247 inInputCsData, oneCell, pointIds, cellsTouchingPointCount):
248 #find face which face faces exterior
249 numFaces = oneCell.GetNumberOfFaces()
250 if PhactoriDbg(100):
251 for faceIndex in range(0, numFaces):
252 oneFace = oneCell.GetFace(faceIndex)
253 numFacePoints = oneFace.GetNumberOfPoints()
254 facePtNdxs = oneFace.GetPointIds()
255 nbrCntList = []
256 for jj in range(0, numFacePoints):
257 ptNdx = facePtNdxs.GetId(jj)
258 nbrCntList.append(cellsTouchingPointCount[ptNdx])
259 if PhactoriDbg(100):
260 myDebugPrint3("faceIndex " + str(faceIndex) + " nbrCntList " + str(nbrCntList) + "\n")
261 for faceIndex in range(0, numFaces):
262 if PhactoriDbg(100):
263 myDebugPrint3("testing faceIndex " + str(faceIndex) + "\n")
264 oneFace = oneCell.GetFace(faceIndex)
265 numFacePoints = oneFace.GetNumberOfPoints()
266 facePtNdxs = oneFace.GetPointIds()
267 for jj in range(0, numFacePoints):
268 ptNdx = facePtNdxs.GetId(jj)
269 if PhactoriDbg(100):
270 myDebugPrint3("jj " + str(jj) + " ctpc " + str(cellsTouchingPointCount[ptNdx]) + "\n")
271 if cellsTouchingPointCount[ptNdx] != 4:
272 #this is not the outward facing face
273 if PhactoriDbg(100):
274 myDebugPrint3("not 4, breaking loop\n")
275 break
276 if jj >= numFacePoints:
277 #this is the outward facing face
278 if PhactoriDbg(100):
279 myDebugPrint3("all 4s, this is it, breaking loop\n")
280 break
281 if PhactoriDbg(100):
282 myDebugPrint3("not all 4s, this not it, continuing loop\n")
283 if PhactoriDbg(100):
284 myDebugPrint3("faceIndex 4 4 4 4: " + str(faceIndex) + "\n")
285 if faceIndex >= numFaces:
286 #shouldn't happen, didn't find face with all 4's
287 myDebugPrint3AndException("faceIndex >= numFaces error\n")
288 return -2
289 #now see if this outward facing face is one of the two largest
290 if PhactoriFollowSurfaceSide1.IsFaceOneOfTwoLargestOnCell(inInputCsData, oneCell, oneFace):
291 #this cell is on the surface and the outward facing side is large
292 return 14 + faceIndex
293 else:
294 return 8 + faceIndex
295
296 @staticmethod
297 def FindSurfaceStatusForOneCell(inInputCsData, inParameters, inCellIndex,
298 cellsTouchingPointCount):
299 oneCell = inInputCsData.GetCell(inCellIndex)
300
301 #cells with all 8 are interior
302 #cells with 4 and 8 are outside surface
303 #cells with 2 (and no 0) are outside edge
304 #cells with 0 are corner
305
306 #if cell is outside surface (4 and 8), if the face which is all 4 is the
307 #largest or second largest area, then it is on the surface we want
308
309 #if the cell is edge (2 4 8), there should be two sides which are 2 2 4 4
310 #if either of these sides is the second or largest area then it is on the
311 #surface we want.
312
313 #if the cell is corner, there should be three sides which are 0 2 2 4
314 #if any of thee sides is the second or largest area then it is on the
315 #surface we want
316
317 numPoints = oneCell.GetNumberOfPoints()
318 pointIds = oneCell.GetPointIds()
319 minCtpc = 9
320 for ii in range(0, numPoints):
321 testCtpc = cellsTouchingPointCount[pointIds.GetId(ii)]
322 if testCtpc < minCtpc:
323 minCtpc = testCtpc
324
325 if minCtpc >= 8:
326 #interior cell
327 retStatus = 1
328 elif minCtpc >= 4:
329 #outside surface, not edge or corner
330 retStatus = 2
331 #retStatus = PhactoriFollowSurfaceSide1.TestOutSideSurfaceCellForMaxAreaFacing(
332 # inInputCsData, oneCell, pointIds, cellsTouchingPointCount)
333 elif minCtpc >= 2:
334 #outside surface edge, not corner
335 retStatus = 3
336 #retStatus = PhactoriFollowSurfaceSide1.TestOutSideSurfaceEdgeCellForMaxAreaFacing(
337 # inInputCsData, oneCell, pointIds, cellsTouchingPointCount)
338 elif minCtpc >= 0:
339 #outside surface corner
340 retStatus = 4
341 #retStatus = PhactoriFollowSurfaceSide1.TestOutSideSurfaceCornerCellForMaxAreaFacing(
342 # inInputCsData, oneCell, pointIds, cellsTouchingPointCount)
343 else:
344 #should not get here
345 myDebugPrint3AndException("bad minCtpc error\n")
346 retStatus = -1
347
348 global gCellStatusTrack
349 gCellStatusTrack[retStatus] += 1
350 if PhactoriDbg(100):
351 if inCellIndex % 1000 == 0:
352 myDebugPrint3("cell index: " + str(inCellIndex) + " status: " + str(retStatus) + "\n")
353 for ii, cnt in enumerate(gCellStatusTrack):
354 myDebugPrint3(str(ii) + ": " + str(cnt) + "\n")
355
356 return retStatus
357
358 def FindSurfaceCells(surfaceStatus):
359 retMap = {}
360 firstSurfaceCellIndex = -1
361 for oneCellIndex, oneCellStatus in enumerate(surfaceStatus):
362 if oneCellStatus > 1:
363 retMap[oneCellIndex] = 0
364 if firstSurfaceCellIndex < 0:
365 if (oneCellStatus == 2) or (oneCellStatus == 5):
366 #grab first non-edge non-corner surface cell to use as seed
367 firstSurfaceCellIndex = oneCellIndex
368 if PhactoriDbg(100):
369 myDebugPrint3("FindSurfaceCells retMap:\n")
370 for cellIndex, tag in retMap:
371 myDebugPrint3(str(cellIndex) + " " + str(surfaceStatus[cellIndex]) + "\n")
372 myDebugPrint3("FindSurfaceCells num items: " + len(retMap.keys()) + "\n")
373 myDebugPrint3("firstSurfaceCellIndex: " + str(firstSurfaceCellIndex) + "\n")
374 return retMap, firstSurfaceCellIndex
375
376 def FindWhichSurfaceCellsTouchEachPoint(inInputCsData, surfaceCellMap):
377 cellsTouchingPoints = {}
378 for cellIndex in surfaceCellMap.keys():
379 oneCell = inInputCsData.GetCell(cellIndex)
380 numPoints = oneCell.GetNumberOfPoints()
381 cellPointIds = oneCell.GetPointIds()
382 for ii in range(0, numPoints):
383 ptId = cellPointIds.GetId(ii)
384 if ptId not in cellsTouchingPoints:
385 cellsTouchingPoints[ptId] = []
386 cellsTouchingPoints[ptId].append(cellIndex)
387 if PhactoriDbg(100):
388 myDebugPrint3("cellsTouchingPoints:\n")
389 for ptId, cellList in cellsTouchingPoints:
390 myDebugPrint(str(ptId) + " " + str(celllist) + "\n")
391 return cellsTouchingPoints
392
393 def FollowSurfaceSideFromSeedCell(inInputCsData, seedCellIndex, surfaceStatus):
394 #get a list of all surface cells
395 surfaceCellMap, seedCellIndex = FindSurfaceCells(surfaceStatus)
396
397 #go through points, find cells attached to points
398 cellsTouchingPoints = FindWhichSurfaceCellsTouchEachPoint(inInputCsData, surfaceStatus, surfaceCellMap)
399
400 frontierCellList = [seedCellIndex]
401 surfaceCellMap[seedCellIndex] = 1
402 while len(frontierCellList) > 0:
403 testCellIndex = frontierCellList.pop()
404 if PhactoriDbg(100):
405 myDebugPrint3("frontierCellList:\n" + str(frontierCellList) + "\ndoing cell: " + str(testCellIndex) + "\n")
406 oneCell = inInputCsData.GetCell(testCellIndex)
407 numPoints = oneCell.GetNumberOfPoints()
408 cellPointIds = oneCell.GetPointIds()
409 for ii in range(0, numPoints):
410 ptId = cellPointIds.GetId(ii)
411 cellsTouchingThisPoint = cellsTouchingPoints[ptId]
412 if PhactoriDbg(100):
413 myDebugPrint3(str(ii) + " of " + str(numPoints) + " id " + str(ptId) + " cttp " + str(cellsTouchingThisPoint) + "\n")
414 for oneCellId, visitTag in cellsTouchingThisPoint:
415 if oneCellId not in surfaceCellMap:
416 if PhactoriDbg(100):
417 myDebugPrint3(str(oneCellId) + " not a surface cell\n")
418 continue
419 if visitTag > 0:
420 if PhactoriDbg(100):
421 myDebugPrint3(str(oneCellId) + " already visited\n")
422 continue
423 surfaceCellMap[oneCellId] += 1
424 #if this is not edge or corner, add to frontier
425 if (surfaceStatus[oneCellId] == 2) or (surfaceStatus[oneCellId] == 5):
426 if PhactoriDbg(100):
427 myDebugPrint3(str(oneCellId) + " tagged visited, adding to frontier\n")
428 frontierCellList.append(oneCellId)
429 else:
430 if PhactoriDbg(100):
431 myDebugPrint3(str(oneCellId) + " tagged visited, corner or edge, not added to frontier\n")
432
433 @staticmethod
434 def MarkCellStatusInBlock(recursionObject, inInputCsData, inParameters):
435 if PhactoriDbg(100):
436 myDebugPrint3("MarkCellStatusInBlock entered\n")
437
438 numCells = inInputCsData.GetNumberOfCells()
439 numPoints = inInputCsData.GetNumberOfPoints()
440 if PhactoriDbg(100):
441 myDebugPrint3("numCells: " + str(numCells) + \
442 " numPoints: " + str(numPoints) + "\n")
443 inParameters.numCellsPerBlock.append(numCells)
444 inParameters.numPointsPerBlock.append(numPoints)
445
446 cellsTouchingPointCount = []
447 for ii in range(0, numPoints):
448 cellsTouchingPointCount.append(0)
449
450 #see how many cells are touching each point
451 for ii in range(0, numCells):
452 #if PhactoriDbg(100):
453 # myDebugPrint3("cell index " + str(ii) + " of " + str(numCells) + "\n")
454 PhactoriFollowSurfaceSide1.IncrementTouchingCellCountForAllPointsInOneCell(
455 inInputCsData, inParameters, ii, cellsTouchingPointCount)
456
457 #see which cells have how many points with 8 or other cells
458 surfaceStatus = []
459 for ii in range(0, numCells):
460 #if PhactoriDbg(100):
461 # myDebugPrint3("(b) cell index " + str(ii) + " of " + str(numCells) + "\n")
462 oneCellStatus = PhactoriFollowSurfaceSide1.FindSurfaceStatusForOneCell(
463 inInputCsData, inParameters, ii, cellsTouchingPointCount)
464 surfaceStatus.append(oneCellStatus)
465
466 seedCellIndex = 0
467 surfaceSideCellIndices = FollowSurfaceSideFromSeedCell(inInputCsData, seedCellIndex, surfaceStatus)
468
469 if PhactoriDbg(100):
470 myDebugPrint3("MarkCellStatusInBlock returning\n")
471
472 def ExportOperationData(self, datadescription):
473 if PhactoriDbg(100):
474 myDebugPrint3("PhactoriFollowSurfaceSide1.ExportOperationData entered\n",
475 100)
476
477 UpdatePipelineWithCurrentTimeArgument(self.inPvFilter)
478
479 recursionObj = PhactoriParaviewMultiBlockRecursionControl()
480 recursionObj.mParameters = CellEdgeAngleMetricParameters()
481 recursionObj.mParameters.Initialize(self.UseSmallestAngle,
482 self.OffsetIndex)
483 recursionObj.mOperationToDoPerBlock = self.MarkCellStatusInBlock
484 PhactoriRecusivelyDoMethodPerBlockFromParaViewFilter(recursionObj, self.inPvFilter)
485
486 if PhactoriDbg(100):
487 myDebugPrint3("PhactoriFollowSurfaceSide1.ExportOperationData returning\n", 100)
488
489 def CreateProgrammableFilterString(self):
490
491 if self.mProgFilterString != None:
492 return
493
494 scriptLines = []
495
496 scriptLines.append("import math\n")
497 scriptLines.append("def IncrementTouchingCellCountForAllPointsInOneCell(inInputCsData, inCellIndex,\n")
498 scriptLines.append(" cellsTouchingPointCount):\n")
499 scriptLines.append(" oneCell = inInputCsData.GetCell(inCellIndex)\n")
500 scriptLines.append(" numPoints = oneCell.GetNumberOfPoints()\n")
501 scriptLines.append(" pointIds = oneCell.GetPointIds()\n")
502 scriptLines.append(" onePt = [0.0,0.0,0.0]\n")
503 scriptLines.append(" for ii in range(0, numPoints):\n")
504 scriptLines.append(" onePointId = pointIds.GetId(ii)\n")
505 scriptLines.append(" cellsTouchingPointCount[onePointId] += 1\n")
506 scriptLines.append(" inInputCsData.GetPoint(onePointId, onePt)\n")
507 scriptLines.append("def GetEdgeLength(inInputCsData, oneFace, edgeIndex):\n")
508 scriptLines.append(" oneEdge = oneFace.GetEdge(edgeIndex)\n")
509 scriptLines.append(" edgePointIds = oneEdge.GetPointIds()\n")
510 scriptLines.append(" ptA = inInputCsData.GetPoint(edgePointIds.GetId(0))\n")
511 scriptLines.append(" ptB = inInputCsData.GetPoint(edgePointIds.GetId(1))\n")
512 scriptLines.append(" xx = ptB[0] - ptA[0]\n")
513 scriptLines.append(" yy = ptB[1] - ptA[1]\n")
514 scriptLines.append(" zz = ptB[2] - ptA[2]\n")
515 scriptLines.append(" return math.sqrt(xx*xx + yy*yy + zz*zz)\n")
516 scriptLines.append("def GetFaceSize(inInputCsData, oneFace):\n")
517 scriptLines.append(" numFaceEdges = oneFace.GetNumberOfEdges()\n")
518 scriptLines.append(" if numFaceEdges != 4:\n")
519 scriptLines.append(" retVal = 0\n")
520 scriptLines.append(" for ii in range(0, numFaceEdges):\n")
521 scriptLines.append(" retVal += GetEdgeLength(inInputCsData, oneFace, ii)\n")
522 scriptLines.append(" else:\n")
523 scriptLines.append(" side0Len = GetEdgeLength(inInputCsData, oneFace, 0)\n")
524 scriptLines.append(" side1Len = GetEdgeLength(inInputCsData, oneFace, 1)\n")
525 scriptLines.append(" side2Len = GetEdgeLength(inInputCsData, oneFace, 2)\n")
526 scriptLines.append(" side3Len = GetEdgeLength(inInputCsData, oneFace, 3)\n")
527 scriptLines.append(" retVal = (side0Len + side2Len) * (side1Len * side3Len)\n")
528 scriptLines.append(" return retVal\n")
529 scriptLines.append("def GetSortedFaceSizeList(inInputCsData, oneCell):\n")
530 scriptLines.append(" faceSizeList = []\n")
531 scriptLines.append(" numFaces = oneCell.GetNumberOfFaces()\n")
532 scriptLines.append(" for faceNdx in range(0, numFaces):\n")
533 scriptLines.append(" oneFace = oneCell.GetFace(faceNdx)\n")
534 scriptLines.append(" faceSizeList.append(GetFaceSize(inInputCsData, oneFace))\n")
535 scriptLines.append(" return sorted(faceSizeList)\n")
536 scriptLines.append("def IsFaceOneOfTwoLargestOnCell(inInputCsData, oneCell, oneFace):\n")
537 scriptLines.append(" SortedFaceSizeList = GetSortedFaceSizeList(inInputCsData, oneCell)\n")
538 scriptLines.append(" testFaceSize = GetFaceSize(inInputCsData, oneFace)\n")
539 scriptLines.append(" if len(SortedFaceSizeList) < 2:\n")
540 scriptLines.append(" return False\n")
541 scriptLines.append(" return (testFaceSize >= SortedFaceSizeList[-2])\n")
542 scriptLines.append("def SurfaceCornerCellFaceFacesExterior(oneFace, cellsTouchingPointCount):\n")
543 scriptLines.append(" numFacePoints = oneFace.GetNumberOfPoints()\n")
544 scriptLines.append(" facePointIds = oneFace.GetPointIds()\n")
545 scriptLines.append(" numOutsideVertices = 0\n")
546 scriptLines.append(" for ii in range(0, numFacePoints):\n")
547 scriptLines.append(" if cellsTouchingPointCount[ii] <= 0:\n")
548 scriptLines.append(" return True\n")
549 scriptLines.append(" return False\n")
550 scriptLines.append("def TestOutSideSurfaceCornerCellForMaxAreaFacing(\n")
551 scriptLines.append(" inInputCsData, oneCell, pointIds, cellsTouchingPointCount):\n")
552 scriptLines.append(" numFaces = oneCell.GetNumberOfFaces()\n")
553 scriptLines.append(" for faceIndex in range(0, numFaces):\n")
554 scriptLines.append(" oneFace = oneCell.GetFace(faceIndex)\n")
555 scriptLines.append(" if SurfaceCornerCellFaceFacesExterior(oneFace, cellsTouchingPointCount):\n")
556 scriptLines.append(" if IsFaceOneOfTwoLargestOnCell(inInputCsData, oneCell, oneFace):\n")
557 scriptLines.append(" return 7\n")
558 scriptLines.append(" return 4\n")
559 scriptLines.append("def SurfaceEdgeCellFaceFacesExterior(oneFace, cellsTouchingPointCount):\n")
560 scriptLines.append(" numFacePoints = oneFace.GetNumberOfPoints()\n")
561 scriptLines.append(" facePointIds = oneFace.GetPointIds()\n")
562 scriptLines.append(" numOutsideVertices = 0\n")
563 scriptLines.append(" for ii in range(0, numFacePoints):\n")
564 scriptLines.append(" if cellsTouchingPointCount[ii] <= 2:\n")
565 scriptLines.append(" numOutsideVertices += 1\n")
566 scriptLines.append(" return (numOutsideVertices >= 2)\n")
567 scriptLines.append("def TestOutSideSurfaceEdgeCellForMaxAreaFacing(\n")
568 scriptLines.append(" inInputCsData, oneCell, pointIds, cellsTouchingPointCount):\n")
569 scriptLines.append(" numFaces = oneCell.GetNumberOfFaces()\n")
570 scriptLines.append(" for faceIndex in range(0, numFaces):\n")
571 scriptLines.append(" oneFace = oneCell.GetFace(faceIndex)\n")
572 scriptLines.append(" if SurfaceEdgeCellFaceFacesExterior(oneFace, cellsTouchingPointCount):\n")
573 scriptLines.append(" if IsFaceOneOfTwoLargestOnCell(inInputCsData, oneCell, oneFace):\n")
574 scriptLines.append(" return 6\n")
575 scriptLines.append(" return 3\n")
576 scriptLines.append("def TestOutSideSurfaceCellForMaxAreaFacing(\n")
577 scriptLines.append(" inInputCsData, oneCell, pointIds, cellsTouchingPointCount):\n")
578 scriptLines.append(" numFaces = oneCell.GetNumberOfFaces()\n")
579 scriptLines.append(" for faceIndex in range(0, numFaces):\n")
580 scriptLines.append(" oneFace = oneCell.GetFace(faceIndex)\n")
581 scriptLines.append(" numFacePoints = oneFace.GetNumberOfPoints()\n")
582 scriptLines.append(" facePtNdxs = oneFace.GetPointIds()\n")
583 scriptLines.append(" for jj in range(0, numFacePoints):\n")
584 scriptLines.append(" ptNdx = facePtNdxs.GetId(jj)\n")
585 scriptLines.append(" if cellsTouchingPointCount[ptNdx] != 4:\n")
586 scriptLines.append(" break\n")
587 scriptLines.append(" if jj >= numFacePoints:\n")
588 scriptLines.append(" break\n")
589 scriptLines.append(" if faceIndex >= numFaces:\n")
590 scriptLines.append(" return -2\n")
591 scriptLines.append(" if IsFaceOneOfTwoLargestOnCell(inInputCsData, oneCell, oneFace):\n")
592 scriptLines.append(" return 5\n")
593 scriptLines.append(" else:\n")
594 scriptLines.append(" return 2\n")
595 scriptLines.append("def FindSurfaceStatusForOneCell(inInputCsData, inCellIndex,\n")
596 scriptLines.append(" cellsTouchingPointCount):\n")
597 scriptLines.append(" oneCell = inInputCsData.GetCell(inCellIndex)\n")
598 scriptLines.append(" numPoints = oneCell.GetNumberOfPoints()\n")
599 scriptLines.append(" pointIds = oneCell.GetPointIds()\n")
600 scriptLines.append(" minCtpc = 9\n")
601 scriptLines.append(" for ii in range(0, numPoints):\n")
602 scriptLines.append(" testCtpc = cellsTouchingPointCount[pointIds.GetId(ii)]\n")
603 scriptLines.append(" if testCtpc < minCtpc:\n")
604 scriptLines.append(" minCtpc = testCtpc\n")
605 scriptLines.append(" if minCtpc >= 8:\n")
606 scriptLines.append(" retStatus = 1\n")
607 scriptLines.append(" elif minCtpc >= 4:\n")
608 scriptLines.append(" retStatus = TestOutSideSurfaceCellForMaxAreaFacing(\n")
609 scriptLines.append(" inInputCsData, oneCell, pointIds, cellsTouchingPointCount)\n")
610 scriptLines.append(" elif minCtpc >= 2:\n")
611 scriptLines.append(" retStatus = TestOutSideSurfaceEdgeCellForMaxAreaFacing(\n")
612 scriptLines.append(" inInputCsData, oneCell, pointIds, cellsTouchingPointCount)\n")
613 scriptLines.append(" elif minCtpc >= 0:\n")
614 scriptLines.append(" retStatus = TestOutSideSurfaceCornerCellForMaxAreaFacing(\n")
615 scriptLines.append(" inInputCsData, oneCell, pointIds, cellsTouchingPointCount)\n")
616 scriptLines.append(" else:\n")
617 scriptLines.append(" retStatus = -1\n")
618 scriptLines.append(" return retStatus\n")
619
620 scriptLines.append("localLeafVisitCount = 0\n")
621 scriptLines.append("def flatten(input, output):\n")
622 scriptLines.append(" # Copy the cells etc.\n")
623 scriptLines.append(" output.ShallowCopy(input)\n")
624 scriptLines.append(" numPoints = input.GetNumberOfPoints()\n")
625 scriptLines.append(" celldata = output.GetCellData()\n")
626 scriptLines.append(" numCells = input.GetNumberOfCells()\n")
627 scriptLines.append(" ncda = vtk.vtkIntArray()\n")
628 scriptLines.append(" ncda.SetNumberOfTuples(numCells)\n")
629 scriptLines.append(" numTuples = ncda.GetNumberOfTuples()\n")
630 scriptLines.append(" for ii in range(0, numTuples):\n")
631 scriptLines.append(" ncda.SetValue(ii, 0)\n")
632 scriptLines.append(" cellsTouchingPointCount = []\n")
633 scriptLines.append(" for ii in range(0, numPoints):\n")
634 scriptLines.append(" cellsTouchingPointCount.append(0)\n")
635 scriptLines.append(" for ii in range(0, numCells):\n")
636 scriptLines.append(" IncrementTouchingCellCountForAllPointsInOneCell(input, ii, cellsTouchingPointCount)\n")
637 scriptLines.append(" for ii in range(0, numCells):\n")
638 scriptLines.append(" oneCellSurfaceStatus = FindSurfaceStatusForOneCell(input, ii, cellsTouchingPointCount)\n")
639 scriptLines.append(" ncda.SetValue(ii, oneCellSurfaceStatus)\n")
640 scriptLines.append(" ncda.SetName('" + self.ProgrammableFilterOutputCellVariableName + "')\n")
641 scriptLines.append(" celldata.AddArray(ncda)\n")
642
643 scriptLines.append("input = self.GetInputDataObject(0, 0)\n")
644 scriptLines.append("output = self.GetOutputDataObject(0)\n")
645
646 scriptLines.append("if input.IsA('vtkMultiBlockDataSet'):\n")
647 scriptLines.append(" output.CopyStructure(input)\n")
648 scriptLines.append(" iter = input.NewIterator()\n")
649 scriptLines.append(" iter.UnRegister(None)\n")
650 scriptLines.append(" iter.InitTraversal()\n")
651 scriptLines.append(" while not iter.IsDoneWithTraversal():\n")
652 scriptLines.append(" localLeafVisitCount += 1\n")
653 scriptLines.append(" curInput = iter.GetCurrentDataObject()\n")
654 scriptLines.append(" curOutput = curInput.NewInstance()\n")
655 scriptLines.append(" curOutput.UnRegister(None)\n")
656 scriptLines.append(" output.SetDataSet(iter, curOutput)\n")
657 scriptLines.append(" flatten(curInput, curOutput)\n")
658 scriptLines.append(" iter.GoToNextItem();\n")
659 scriptLines.append("else:\n")
660 scriptLines.append(" flatten(input, output)\n")
661
662 self.mProgFilterString = "".join(scriptLines)
663
664 if PhactoriDbg(100):
665 myDebugPrint3("PhactoriFollowSurfaceSide1 constructed script:\n" + \
666 self.mProgFilterString)
667
668 #phactori_combine_to_single_python_file_subpiece_end_1