From 372d751fa64967443adcc655bed2bf3d89512822 Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Tue, 17 Nov 2015 21:18:26 -0500 Subject: [PATCH] Added alpha compositing scheme to Python API Plot class to highlight domains --- openmc/plots.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/openmc/plots.py b/openmc/plots.py index acc7d4d13..d04f9a036 100644 --- a/openmc/plots.py +++ b/openmc/plots.py @@ -270,7 +270,7 @@ class Plot(object): Params ------ geometry : openmc.Geometry - The geometry for which the plot is created + The geometry for which the plot is defined seed : Integral The random number seed used to generate the color scheme @@ -284,7 +284,7 @@ class Plot(object): if self.color is 'mat': domains = geometry.get_all_materials() else: - domains = geometry.get_all_materials() + domains = geometry.get_all_cells() # Set the seed for the random number generator np.random.seed(seed) @@ -295,9 +295,63 @@ class Plot(object): r = np.random.randint(0, 255) g = np.random.randint(0, 255) b = np.random.randint(0, 255) - self.col_spec[domain.id] = (r, g, b) + self.col_spec[domain] = (r, g, b) + def highlight_domains(self, geometry, domains, seed=1, + alpha=0.5, background='grey'): + """Use alpha compositing to highlight one or more domains in the plot. + + This routine generates a color scheme and applies alpha compositing + to make all domains except the highlighted ones partially transparent. + + Params + ------ + geometry : openmc.Geometry + The geometry for which the plot is defined + domains : Iterable of Integral + A collection of the domain IDs to highlight in the plot + seed : Integral + The random number seed used to generate the color scheme + alpha : Real in [0,1] + The value to apply in alpha compisiting + background : 3-tuple of Integral or 'white' or 'black' or 'grey' + The background color to apply in alpha compisiting + + """ + + cv.check_iterable_type('domains', domains, Integral) + cv.check_type('alpha', alpha, Real) + cv.check_greater_than('alpha', alpha, 0., equality=True) + cv.check_less_than('alpha', alpha, 1., equality=True) + + # Get a background (R,G,B) tuple to apply in alpha compositing + if isinstance(background, basestring): + if background == 'white': + background = (255, 255, 255) + elif background == 'black': + background = (0, 0, 0) + elif background == 'grey': + background = (160, 160, 160) + else: + msg = 'The background "{}" is not defined'.format(background) + raise ValueError(msg) + + cv.check_iterable_type('background', background, Integral) + + # Generate a color scheme + self.colorize(geometry, seed) + + # Apply alpha compositing to the colors for all domains + # other than those the user wishes to highlight + for domain_id in self.col_spec: + if domain_id not in domains: + r, g, b = self.col_spec[domain_id] + r = int(((1-alpha) * background[0]) + (alpha * r)) + g = int(((1-alpha) * background[1]) + (alpha * g)) + b = int(((1-alpha) * background[2]) + (alpha * b)) + self._col_spec[domain_id] = (r, g, b) + def get_plot_xml(self): """Return XML representation of the plot