1 |
""" |
2 |
@author: John NGUI |
3 |
""" |
4 |
|
5 |
import vtk |
6 |
from mapper import DataSetMapper |
7 |
from lookuptable import LookupTable |
8 |
from actor import Actor3D |
9 |
from constant import Viewport, Color, Lut, ColorMode |
10 |
from streamlinemodule import StreamLineModule |
11 |
from tube import Tube |
12 |
from point import PointSource |
13 |
from outline import Outline |
14 |
from average import CellDataToPointData |
15 |
from position import GlobalPosition |
16 |
|
17 |
# NOTE: DataSetMapper, Actor3D, PointSource, StreamLineModule and Tube were |
18 |
# inherited to allow access to their public methods from the driver. |
19 |
class StreamLine(DataSetMapper, Actor3D, PointSource, StreamLineModule, Tube): |
20 |
""" |
21 |
Class that shows the direction of particles of a vector field using |
22 |
streamlines.The streamlines can either be colored or grey-scaled, |
23 |
depending on the lookup table used. If the streamlines are colored, |
24 |
there are two possible coloring modes: (1) using vector data or (2) |
25 |
using scalar data. |
26 |
""" |
27 |
|
28 |
# The SOUTH_WEST default viewport is used when there is only one viewport. |
29 |
# This saves the user from specifying the viewport when there is only one. |
30 |
# If no lut is specified, the color scheme will be used. |
31 |
def __init__(self, scene, data_collector, viewport = Viewport.SOUTH_WEST, |
32 |
color_mode = ColorMode.VECTOR, lut = Lut.COLOR, cell_to_point = False, |
33 |
outline = True): |
34 |
""" |
35 |
Initialise the StreamLine. |
36 |
|
37 |
@attention: The source can either be point or cell data. If the |
38 |
source is cell data, a conversion to point data may or may not be |
39 |
required, in order for the object to be rendered correctly. |
40 |
If a conversion is needed, the 'cell_to_point' flag must be set to |
41 |
'True', otherwise 'False' (which is the default). |
42 |
|
43 |
@type scene: L{Scene <scene.Scene>} object |
44 |
@param scene: Scene in which objects are to be rendered on |
45 |
@type data_collector: L{DataCollector <datacollector.DataCollector>} |
46 |
object |
47 |
@param data_collector: Deal with source of data for visualisation |
48 |
@type viewport: L{Viewport <constant.Viewport>} constant |
49 |
@param viewport: Viewport in which the object is to be rendered on |
50 |
@type color_mode: L{ColorMode <constant.ColorMode>} constant |
51 |
@param color_mode: Type of color mode |
52 |
@type lut : L{Lut <constant.Lut>} constant |
53 |
@param lut: Lookup table color scheme |
54 |
@type cell_to_point: Boolean |
55 |
@param cell_to_point: Converts cell data to point data (by averaging) |
56 |
@type outline: Boolean |
57 |
@param outline: Places an outline around the domain surface |
58 |
""" |
59 |
|
60 |
self.__scene = scene |
61 |
self.__data_collector = data_collector |
62 |
self.__viewport = viewport |
63 |
self.__color_mode = color_mode |
64 |
self.__lut = lut |
65 |
self.__cell_to_point = cell_to_point |
66 |
self.__outline = outline |
67 |
|
68 |
# Keeps track whether Streamline has been modified. |
69 |
self.__modified = True |
70 |
PointSource.__init__(self) |
71 |
StreamLineModule.__init__(self) |
72 |
Tube.__init__(self) |
73 |
DataSetMapper.__init__(self) |
74 |
Actor3D.__init__(self) |
75 |
scene._addVisualizationModules(self) |
76 |
|
77 |
# ----- Outline ----- |
78 |
|
79 |
# NOTE: Changes cannot be made to the Outline's properties from the |
80 |
# driver. |
81 |
if(self.__outline == True): |
82 |
outline = Outline(self.__data_collector._getDataCollectorOutput()) |
83 |
mapper = DataSetMapper() |
84 |
mapper._setupDataSetMapper(outline._getOutlineOutput()) |
85 |
|
86 |
actor3D = Actor3D() |
87 |
actor3D._setupActor3D(mapper._getDataSetMapper()) |
88 |
# Default outline color is black. |
89 |
actor3D.setColor(Color.BLACK) |
90 |
|
91 |
# Default line width is 1. |
92 |
actor3D._setLineWidth(1) |
93 |
self.__scene._addActor3D(self.__viewport, actor3D._getActor3D()) |
94 |
|
95 |
# ----- Streamline ----- |
96 |
|
97 |
# NOTE: Lookup table color mapping (color or grey scale) MUST be set |
98 |
# before DataSetMapper. If it is done after DataSetMapper, no effect |
99 |
# will take place. |
100 |
if(self.__lut == Lut.COLOR): # Colored lookup table. |
101 |
lookup_table = LookupTable() |
102 |
lookup_table._setTableValue() |
103 |
elif(self.__lut == Lut.GREY_SCALE): # Grey scaled lookup table. |
104 |
lookup_table = LookupTable() |
105 |
lookup_table._setLookupTableToGreyScale() |
106 |
|
107 |
if(self.__cell_to_point == True): # Converts cell data to point data. |
108 |
c2p = CellDataToPointData( |
109 |
self.__data_collector._getDataCollectorOutput()) |
110 |
self._setupPointSource(c2p._getCellToPointOutput()) |
111 |
self._setupStreamLineModule(c2p._getCellToPointOutput(), |
112 |
self._getPointSourceOutput()) |
113 |
elif(self.__cell_to_point == False): # No conversion happens. |
114 |
self._setupPointSource( |
115 |
self.__data_collector._getDataCollectorOutput()) |
116 |
self._setupStreamLineModule( |
117 |
self.__data_collector._getDataCollectorOutput(), |
118 |
self._getPointSourceOutput()) |
119 |
|
120 |
self._setupTube(self._getStreamLineModuleOutput()) |
121 |
self._setupDataSetMapper(self._getTubeOutput(), |
122 |
lookup_table._getLookupTable()) |
123 |
|
124 |
self._setupActor3D(self._getDataSetMapper()) |
125 |
self.__scene._addActor3D(self.__viewport, self._getActor3D()) |
126 |
|
127 |
def _isModified(self): |
128 |
""" |
129 |
Return whether the StreamLine or DataCollector has been modified. |
130 |
|
131 |
@rtype: Boolean |
132 |
@return: True or False |
133 |
""" |
134 |
|
135 |
return self.__modified or self.__data_collector._isModified() |
136 |
|
137 |
def _render(self): |
138 |
""" |
139 |
Render the streamline. |
140 |
""" |
141 |
|
142 |
if (self._isModified() == True): |
143 |
if(self._isPointSourceCenterSet() != True): |
144 |
center = self.__data_collector._getCenter() |
145 |
center = GlobalPosition(center[0], center[1], center[2]) |
146 |
self.setPointSourceCenter(center) |
147 |
|
148 |
self._setPointSourceCenter() |
149 |
|
150 |
if(self.__data_collector._isScalarSet() == True): |
151 |
self.__data_collector._setActiveScalar() |
152 |
if(self.__data_collector._isVectorSet() == True): |
153 |
self.__data_collector._setActiveVector() |
154 |
|
155 |
# Color streamline by vector. |
156 |
if(self.__color_mode == ColorMode.VECTOR): |
157 |
self._setScalarVisibilityOn() |
158 |
self._setSpeedScalarsOn() |
159 |
self._setScalarRange(self.__data_collector._getVectorRange()) |
160 |
# Color streamline by scalar. |
161 |
elif(self.__color_mode == ColorMode.SCALAR): |
162 |
self._setScalarVisibilityOn() |
163 |
self._setSpeedScalarsOff() |
164 |
self._setScalarRange(self.__data_collector._getScalarRange()) |
165 |
|
166 |
self.__modified = False |
167 |
|
168 |
|