-
Notifications
You must be signed in to change notification settings - Fork 32
vtkpp #1141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
vtkpp #1141
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # | ||
| # | ||
| # | ||
|
|
||
| from .plot import plot | ||
|
|
||
| __all__ = ["plot"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # | ||
| # | ||
| # | ||
|
|
||
| import vtk | ||
|
|
||
|
|
||
| class PhaseOutput: | ||
| def __init__(self, **kwargs): | ||
| self.kwargs = {**kwargs} | ||
|
|
||
| def __iter__(self): | ||
| return self.kwargs.items().__iter__() | ||
|
|
||
|
|
||
| def surface_filter(output, **kwargs): | ||
| surface = vtk.vtkDataSetSurfaceFilter() | ||
| surface.SetInputConnection(output.GetOutputPort()) | ||
| return PhaseOutput(surface=surface) | ||
|
|
||
|
|
||
| def composite_data_geometry_filter(output, **kwargs): | ||
| geom = vtk.vtkCompositeDataGeometryFilter() | ||
| geom.SetInputConnection(output.GetOutputPort()) | ||
| return PhaseOutput(geom=geom) | ||
|
|
||
|
|
||
| def poly_data_mapper(output, **kwargs): | ||
| array_name = kwargs.get("array_name", "data") | ||
| mapper = vtk.vtkPolyDataMapper() | ||
| mapper.SetInputConnection(output.GetOutputPort()) | ||
| mapper.SelectColorArray(array_name) | ||
| mapper.SetScalarModeToUsePointData() | ||
| mapper.SetScalarRange( | ||
| output.GetOutput().GetPointData().GetArray(array_name).GetRange(0) | ||
| ) | ||
| return PhaseOutput(mapper=mapper) | ||
|
|
||
|
|
||
| def all_times_in(reader): | ||
| info = reader.GetOutputInformation(0) | ||
|
|
||
| time_key = vtk.vtkStreamingDemandDrivenPipeline.TIME_STEPS() | ||
|
|
||
| if not info.Has(time_key): | ||
| raise RuntimeError("VTK Error: cannot get times from file") | ||
|
|
||
| times = info.Get(time_key) | ||
| return times | ||
|
|
||
|
|
||
| def bounds_in(reader): | ||
| amr = reader.GetOutput() | ||
| bbox = vtk.vtkBoundingBox() | ||
|
|
||
| for level in range(amr.GetNumberOfLevels()): | ||
| for idx in range(amr.GetNumberOfDataSets(level)): | ||
| ds = amr.GetDataSet(level, idx) | ||
| if ds is not None: | ||
| b = [0.0] * 6 | ||
| ds.GetBounds(b) | ||
| bbox.AddBounds(b) | ||
|
|
||
| global_bounds = [0.0] * 6 | ||
| bbox.GetBounds(global_bounds) | ||
| return global_bounds | ||
|
|
||
|
|
||
| def _default_phases(): | ||
| # Typical use case for PHARE fields | ||
| return [surface_filter, composite_data_geometry_filter, poly_data_mapper] | ||
|
|
||
|
|
||
| class VtkFile: | ||
| def __init__(self, filename, time=None, array_name="data", phases=None): | ||
| phases = phases if phases is not None else _default_phases() | ||
| if len(phases) == 0: | ||
| raise RuntimeError("Error: Zero phases!") | ||
|
|
||
| self.array_name = array_name | ||
| self.reader = vtk.vtkHDFReader() | ||
| self.reader.SetFileName(filename) | ||
| self.reader.Update() | ||
| self.reader.UpdateInformation() | ||
|
|
||
| self.times = all_times_in(self.reader) | ||
| self.reader.UpdateTimeStep(time if time is not None else self.times[-1]) | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| _in = self.reader | ||
| for i in range(len(phases)): | ||
| ret = phases[i](_in, array_name=array_name) | ||
| for key, val in ret: | ||
| if hasattr(val, "Update"): | ||
| val.Update() | ||
| setattr(self, key, val) | ||
| _in = val | ||
|
|
||
| self.mapper = _in | ||
| self.bounds = bounds_in(self.reader) | ||
| self.spacing = self.reader.GetOutput().GetDataSet(0, 0).GetSpacing() | ||
|
PhilipDeegan marked this conversation as resolved.
|
||
|
|
||
|
|
||
| class VtkFieldFile(VtkFile): | ||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
|
|
||
|
|
||
| class VtkTensorFieldFile(VtkFile): | ||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # | ||
| # | ||
| # | ||
|
|
||
|
|
||
| import vtk | ||
| from .base import VtkTensorFieldFile | ||
|
|
||
|
|
||
| def plot(vtk_file, out_file="vtk.png"): | ||
| if isinstance(vtk_file, str): | ||
| vtk_file = VtkTensorFieldFile(vtk_file) | ||
| vtk_file.geom.GetOutput().GetPointData().SetActiveScalars("data") | ||
|
|
||
| colors = vtk.vtkNamedColors() | ||
|
|
||
| renderer = vtk.vtkRenderer() | ||
| renderer.AddActor(vtk.vtkActor(mapper=vtk_file.mapper)) | ||
| renderer.SetBackground(colors.GetColor3d("Silver")) | ||
|
|
||
| ren_win = vtk.vtkRenderWindow() | ||
| ren_win.AddRenderer(renderer) | ||
| ren_win.SetSize(800, 600) | ||
| ren_win.OffScreenRenderingOn() # do not flash image on screen | ||
|
|
||
| iren = vtk.vtkRenderWindowInteractor() | ||
| iren.SetRenderWindow(ren_win) | ||
|
|
||
| ren_win.Render() | ||
|
|
||
| w2if = vtk.vtkWindowToImageFilter() | ||
| w2if.SetInput(ren_win) | ||
| w2if.Update() | ||
|
|
||
| writer = vtk.vtkPNGWriter() | ||
| writer.SetFileName(out_file) | ||
| writer.SetInputConnection(w2if.GetOutputPort()) | ||
| writer.Write() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.