Skip to content
Snippets Groups Projects
Commit 62e784dd authored by Andrew Maclean's avatar Andrew Maclean
Browse files

Adding HighlightBadCells, MeshQuality, MatrixInverse, MatrixTranspose

parent 374e5c98
No related branches found
No related tags found
1 merge request!397C++ to python api 12
......@@ -17,7 +17,7 @@
#include <vtkUnstructuredGrid.h>
#include <iostream>
#include <string>
#include <map>
int main(int, char*[])
{
......@@ -55,13 +55,12 @@ int main(int, char*[])
for (vtkIdType i = 0; i < qualityArray->GetNumberOfTuples(); i++)
{
double val = qualityArray->GetValue(i);
cout << "value " << i << " : " << val << endl;
cout << "value " << i << " : " << qualityArray->GetValue(i) << endl;
}
const auto lowerThreshold = 0.02;
vtkNew<vtkThreshold> selectCells;
// selectCells->ThresholdByLower(.02);
selectCells->SetLowerThreshold(0.02);
selectCells->SetLowerThreshold(lowerThreshold);
selectCells->SetThresholdFunction(vtkThreshold::THRESHOLD_LOWER);
selectCells->SetInputArrayToProcess(0, 0, 0,
vtkDataObject::FIELD_ASSOCIATION_CELLS,
......@@ -69,6 +68,22 @@ int main(int, char*[])
selectCells->SetInputData(qualityMesh);
selectCells->Update();
std::map<int, double> badValues;
for (vtkIdType i = 0; i < qualityArray->GetNumberOfTuples(); i++)
{
auto val = qualityArray->GetValue(i);
if (val <= lowerThreshold)
{
badValues[i] = val;
}
}
std::cout << "Number of values <= " << lowerThreshold << " : "
<< badValues.size() << std::endl;
for (const auto& kv : badValues)
{
std::cout << "value " << kv.first << " : " << kv.second << std::endl;
}
vtkUnstructuredGrid* ug = selectCells->GetOutput();
// Create a mapper and actor.
......
......@@ -15,27 +15,10 @@
#include <vtkTriangleFilter.h>
#include <iostream>
#include <string>
void MakeLUT(vtkLookupTable* lut)
{
// Make the lookup table.
vtkNew<vtkColorSeries> colorSeries;
// Select a color scheme.
int colorSeriesEnum;
colorSeriesEnum = colorSeries->BREWER_DIVERGING_BROWN_BLUE_GREEN_9;
// colorSeriesEnum = colorSeries->BREWER_DIVERGING_SPECTRAL_10;
// colorSeriesEnum = colorSeries->BREWER_DIVERGING_SPECTRAL_3;
// colorSeriesEnum = colorSeries->BREWER_DIVERGING_PURPLE_ORANGE_9;
// colorSeriesEnum = colorSeries->BREWER_SEQUENTIAL_BLUE_PURPLE_9;
// colorSeriesEnum = colorSeries->BREWER_SEQUENTIAL_BLUE_GREEN_9;
// colorSeriesEnum = colorSeries->BREWER_QUALITATIVE_SET3;
// colorSeriesEnum = colorSeries->CITRUS;
colorSeries->SetColorScheme(colorSeriesEnum);
colorSeries->BuildLookupTable(lut);
lut->SetNanColor(1, 0, 0, 1);
}
namespace {
vtkNew<vtkLookupTable> MakeLUT();
} // namespace
int main(int, char*[])
{
......@@ -65,23 +48,23 @@ int main(int, char*[])
for (vtkIdType i = 0; i < qualityArray->GetNumberOfTuples(); i++)
{
double val = qualityArray->GetValue(i);
std::cout << "value " << i << " : " << val << std::endl;
std::cout << "value " << i << " : " << qualityArray->GetValue(i)
<< std::endl;
}
vtkNew<vtkPolyData> polydata;
polydata->ShallowCopy(qualityFilter->GetOutput());
auto scalarRange = polydata->GetScalarRange();
// Visualize
vtkNew<vtkLookupTable> lut;
MakeLUT(lut);
lut->SetTableRange(polydata->GetScalarRange());
auto lut = MakeLUT();
lut->SetTableRange(scalarRange);
lut->IndexedLookupOff();
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputData(polydata);
mapper->SetScalarRange(polydata->GetScalarRange());
mapper->SetScalarRange(scalarRange);
mapper->SetLookupTable(lut);
vtkNew<vtkActor> actor;
......@@ -103,3 +86,29 @@ int main(int, char*[])
return EXIT_SUCCESS;
}
namespace {
vtkNew<vtkLookupTable> MakeLUT()
{
// Make the lookup table.
vtkNew<vtkColorSeries> colorSeries;
// Select a color scheme.
int colorSeriesEnum;
colorSeriesEnum = colorSeries->BREWER_DIVERGING_BROWN_BLUE_GREEN_9;
// colorSeriesEnum = colorSeries->BREWER_DIVERGING_SPECTRAL_10;
// colorSeriesEnum = colorSeries->BREWER_DIVERGING_SPECTRAL_3;
// colorSeriesEnum = colorSeries->BREWER_DIVERGING_PURPLE_ORANGE_9;
// colorSeriesEnum = colorSeries->BREWER_SEQUENTIAL_BLUE_PURPLE_9;
// colorSeriesEnum = colorSeries->BREWER_SEQUENTIAL_BLUE_GREEN_9;
// colorSeriesEnum = colorSeries->BREWER_QUALITATIVE_SET3;
// colorSeriesEnum = colorSeries->CITRUS;
colorSeries->SetColorScheme(colorSeriesEnum);
vtkNew<vtkLookupTable> lut;
colorSeries->BuildLookupTable(lut);
lut->SetNanColor(1, 0, 0, 1);
return lut;
}
} // namespace
......@@ -277,9 +277,11 @@ This section includes examples of manipulating meshes.
[FillHoles](/PythonicAPI/Meshes/FillHoles) | Close holes in a mesh.
[FitToHeightMap](/PythonicAPI/Meshes/FitToHeightMap) | Drape a polydata over an elevation map.
[GreedyTerrainDecimation](/PythonicAPI/PolyData/GreedyTerrainDecimation) | Create a mesh from an ImageData
[HighlightBadCells](/PythonicAPI/PolyData/HighlightBadCells) |
[IdentifyHoles](/PythonicAPI/Meshes/IdentifyHoles) | Close holes in a mesh and identify the holes.
[ImplicitSelectionLoop](/PythonicAPI/PolyData/ImplicitSelectionLoop) | Select a region of a mesh with an implicit function.
[InterpolateFieldDataDemo](/PythonicAPI/Meshes/InterpolateFieldDataDemo) | Resample a fine grid and interpolate field data.
[MeshQuality](/PythonicAPI/PolyData/MeshQuality) |
[MatrixMathFilter](/PythonicAPI/Meshes/MatrixMathFilter) | Compute various quantities on cells and points in a mesh.
[PointInterpolator](/PythonicAPI/Meshes/PointInterpolator) | Plot a scalar field of points onto a PolyData surface.
......@@ -403,6 +405,12 @@ This section includes ?vtkUnstructuredGrid?.
## Math Operations
| Example Name | Description | Image |
| -------------- | ------------- | ------- |
[MatrixInverse](/PythonicAPI/Math/MatrixInverse) | Matrix inverse.
[MatrixTranspose](/PythonicAPI/Math/MatrixTranspose) | Matrix transpose.
## Graphs
| Example Name | Description | Image |
......
# !/usr/bin/env python3
import numpy as np
from vtkmodules.vtkCommonMath import vtkMatrix3x3
def main():
shape = (3, 3)
m = vtkMatrix3x3()
m.SetElement(2, 1, 2.0) # Set element (2,1) to 2.0
print('Original matrix:')
print_matrix(m.data, shape)
m.Invert()
print('Inverse:')
print_matrix(m.data, shape)
def print_matrix(m, shape):
data = np.array(m)
data = data.reshape(shape)
print(data)
if __name__ == '__main__':
main()
# !/usr/bin/env python3
import numpy as np
from vtkmodules.vtkCommonMath import vtkMatrix3x3
def main():
shape = (3, 3)
m = vtkMatrix3x3()
m.SetElement(2, 1, 2.0) # Set element (2,1) to 2.0
print('Original matrix:')
print_matrix(m.data, shape)
m.Transpose()
print('Transpose:')
print_matrix(m.data, shape)
def print_matrix(m, shape):
data = np.array(m)
data = data.reshape(shape)
print(data)
if __name__ == '__main__':
main()
# !/usr/bin/env python3
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonDataModel import (
vtkDataObject,
vtkDataSetAttributes
)
from vtkmodules.vtkFiltersCore import (
vtkThreshold,
vtkTriangleFilter
)
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkFiltersVerdict import vtkMeshQuality
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkDataSetMapper,
vtkRenderer,
vtkRenderWindow,
vtkRenderWindowInteractor
)
LOWER_THRESHOLD = 0.02
def main():
colors = vtkNamedColors()
sphere_source = vtkSphereSource()
triangle_filter = vtkTriangleFilter()
sphere_source >> triangle_filter
triangle_filter.Update()
# Create a mapper and actor.
sphere_mapper = vtkDataSetMapper()
sphere_source >> triangle_filter >> sphere_mapper
sphere_actor = vtkActor(mapper=sphere_mapper)
sphere_actor.property.color = colors.GetColor3d('MistyRose')
mesh = triangle_filter.update().output
print(f'There are {mesh.number_of_cells} cells.')
quality_filter = vtkMeshQuality(input_data=mesh)
quality_filter.SetTriangleQualityMeasureToArea()
quality_mesh = quality_filter.update().output
quality_array = quality_mesh.GetCellData().GetArray('Quality')
print(f'There are {quality_array.number_of_tuples} values.')
for i in range(0, quality_array.number_of_tuples):
print(f'value {i:2d} : {quality_array.GetValue(i):0.6f}')
select_cells = vtkThreshold(lower_threshold=LOWER_THRESHOLD, input_data=quality_mesh)
select_cells.SetThresholdFunction(vtkThreshold.THRESHOLD_LOWER)
select_cells.SetInputArrayToProcess(0, 0, 0, vtkDataObject.FIELD_ASSOCIATION_CELLS, vtkDataSetAttributes.SCALARS)
select_cells.update()
bad_values = dict()
for i in range(0, quality_array.number_of_tuples):
val = quality_array.GetValue(i)
if val <= LOWER_THRESHOLD:
bad_values[i] = val
print(f'Number of values <= {LOWER_THRESHOLD} : {len(bad_values)}')
for k, v in bad_values.items():
print(f'value {k} : {v:0.6f}')
ug = select_cells.output
# Create a mapper and actor.
mapper = vtkDataSetMapper(input_data=ug)
actor = vtkActor(mapper=mapper)
actor.property.SetRepresentationToWireframe()
actor.property.line_width = 5
actor.property.color = colors.GetColor3d('Red')
# Create a renderer, render window, and interactor.
renderer = vtkRenderer(background=colors.GetColor3d('SlateGray'))
render_window = vtkRenderWindow(window_name='HighlightBadCells')
render_window.AddRenderer(renderer)
render_window_interactor = vtkRenderWindowInteractor()
render_window_interactor.render_window = render_window
# Add the actors to the scene.
renderer.AddActor(actor)
renderer.AddActor(sphere_actor)
# Render and interact.
render_window.Render()
render_window_interactor.Start()
if __name__ == '__main__':
main()
### Description
This example uses one of many selectable methods to determine the quality of a mesh. In this case, we have selected to use the area of the triangles. We show how to retrieve the quality metric computed on each triangle after the process is completed.
# !/usr/bin/env python3
from dataclasses import dataclass
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import (
vtkColorSeries,
vtkNamedColors
)
from vtkmodules.vtkCommonCore import vtkLookupTable
from vtkmodules.vtkCommonDataModel import vtkPolyData
from vtkmodules.vtkFiltersCore import vtkTriangleFilter
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkFiltersVerdict import vtkMeshQuality
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkPolyDataMapper,
vtkRenderer,
vtkRenderWindow,
vtkRenderWindowInteractor,
)
def main():
colors = vtkNamedColors()
sphere_source = vtkSphereSource()
triangle_filter = vtkTriangleFilter()
sphere_source >> triangle_filter
triangle_filter.update()
mesh = triangle_filter.output
print(f'There are {mesh.number_of_cells} cells.')
quality_filter = vtkMeshQuality(input_data=mesh)
quality_filter.SetTriangleQualityMeasureToArea()
quality_filter.update()
quality_array = quality_filter.output.GetCellData().GetArray('Quality')
print(f'There are {quality_array.number_of_tuples} values.')
for i in range(0, quality_array.number_of_tuples):
print(f'value {i:2d} : {quality_array.GetValue(i):0.6f}')
polydata = vtkPolyData()
polydata.ShallowCopy(quality_filter.output)
scalar_range = polydata.scalar_range
# Visualize
lut = make_lut()
lut.SetTableRange(scalar_range)
lut.IndexedLookup = False
mapper = vtkPolyDataMapper(input_data=polydata, scalar_range=scalar_range, lookup_table=lut)
actor = vtkActor(mapper=mapper)
renderer = vtkRenderer(background=colors.GetColor3d('SlateGray'))
render_window = vtkRenderWindow(window_name='MeshQuality')
render_window.AddRenderer(renderer)
render_window_interactor = vtkRenderWindowInteractor()
render_window_interactor.render_window = render_window
renderer.AddActor(actor)
render_window.Render()
render_window_interactor.Start()
def make_lut():
"""
Make the lookup table.
:return: The lookup table.
"""
color_series = vtkColorSeries()
# Select a color scheme.
color_series_enum = color_series.BREWER_DIVERGING_BROWN_BLUE_GREEN_9
# color_series_enum = color_series.BREWER_DIVERGING_SPECTRAL_10
# color_series_enum = color_series.BREWER_DIVERGING_SPECTRAL_3
# color_series_enum = color_series.BREWER_DIVERGING_PURPLE_ORANGE_9
# color_series_enum = color_series.BREWER_SEQUENTIAL_BLUE_PURPLE_9
# color_series_enum = color_series.BREWER_SEQUENTIAL_BLUE_GREEN_9
# color_series_enum = color_series.BREWER_QUALITATIVE_SET3
# color_series_enum = color_series.CITRUS
lut = vtkLookupTable(scale=LookupTable.Scale.VTK_SCALE_LINEAR)
color_series.color_scheme = color_series_enum
color_series.BuildLookupTable(lut, color_series.ORDINAL)
lut.SetNanColor(1, 0, 0, 1)
return lut
@dataclass(frozen=True)
class LookupTable:
@dataclass(frozen=True)
class Scale:
VTK_SCALE_LINEAR: int = 0
VTK_SCALE_LOG10: int = 1
if __name__ == '__main__':
main()
src/Testing/Baseline/PythonicAPI/PolyData/TestHighlightBadCells.png

130 B

src/Testing/Baseline/PythonicAPI/PolyData/TestMeshQuality.png

130 B

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment