88from pytissueoptics .rayscattering .display .profiles import Profile1D , ProfileFactory
99from pytissueoptics .rayscattering .display .viewer import PointCloudStyle , Viewer , Visibility
1010from pytissueoptics .rayscattering .display .views import View2D
11- from pytissueoptics .rayscattering .energyLogging import EnergyLogger , PointCloud , PointCloudFactory
11+ from pytissueoptics .rayscattering .energyLogging import EnergyLogger , PointCloud
1212from pytissueoptics .rayscattering .scatteringScene import ScatteringScene
1313from pytissueoptics .rayscattering .source import Source
1414from pytissueoptics .scene .geometry import BoundingBox
@@ -112,14 +112,11 @@ def testWhenShow3DWithSource_shouldDisplaySource(self):
112112 self .mock3DViewer .show .assert_called_once ()
113113
114114 def testWhenShow3DWithDefaultPointCloud_shouldDisplayPointCloudOfSolidsAndSurfaceLeaving (self ):
115- mockPointCloudFactory = mock (PointCloudFactory )
116115 aPointCloud = PointCloud (
117- solidPoints = np .array ([[0.5 , 0 , 0 , 0 ]]), surfacePoints = np .array ([[1 , 0 , 0 , 0 ], [- 1 , 0 , 0 , 0 ]])
116+ solidPoints = np .array ([[0.5 , 0 , 0 , 0 , 0 ]]), surfacePoints = np .array ([[1 , 0 , 0 , 0 , 0 ], [- 1 , 0 , 0 , 0 , 0 ]])
118117 )
119- when (mockPointCloudFactory ).getPointCloud (...).thenReturn (aPointCloud )
120- self .viewer ._pointCloudFactory = mockPointCloudFactory
121-
122- self .viewer .show3D (visibility = Visibility .POINT_CLOUD )
118+ with self ._mockPointCloud (aPointCloud ):
119+ self .viewer .show3D (visibility = Visibility .POINT_CLOUD )
123120
124121 self .mock3DViewer .addDataPoints .assert_called ()
125122 addedSolidPoints = self .mock3DViewer .addDataPoints .call_args_list [0 ][0 ][0 ]
@@ -130,25 +127,21 @@ def testWhenShow3DWithDefaultPointCloud_shouldDisplayPointCloudOfSolidsAndSurfac
130127 self .mock3DViewer .show .assert_called_once ()
131128
132129 def testGivenNoData_whenShow3DWithPointCloud_shouldNotDisplayPointCloud (self ):
133- mockPointCloudFactory = mock (PointCloudFactory )
134130 aPointCloud = PointCloud ()
135- when (mockPointCloudFactory ).getPointCloud (...).thenReturn (aPointCloud )
136- self .viewer ._pointCloudFactory = mockPointCloudFactory
137-
138- self .viewer .show3D (visibility = Visibility .POINT_CLOUD )
131+ with self ._mockPointCloud (aPointCloud ):
132+ self .viewer .show3D (visibility = Visibility .POINT_CLOUD )
139133
140134 self .mock3DViewer .addDataPoints .assert_not_called ()
141135 self .mock3DViewer .show .assert_called_once ()
142136
143137 def testWhenShow3DWithSurfacePointCloud_shouldOnlyDisplaySurfacePoints (self ):
144- mockPointCloudFactory = mock (PointCloudFactory )
145138 aPointCloud = PointCloud (
146- solidPoints = np .array ([[0.5 , 0 , 0 , 0 ]]), surfacePoints = np .array ([[1 , 0 , 0 , 0 ], [- 1 , 0 , 0 , 0 ]])
139+ solidPoints = np .array ([[0.5 , 0 , 0 , 0 , 0 ]]), surfacePoints = np .array ([[1 , 0 , 0 , 0 , 0 ], [- 1 , 0 , 0 , 0 , 0 ]])
147140 )
148- when ( mockPointCloudFactory ). getPointCloud (...). thenReturn ( aPointCloud )
149- self .viewer ._pointCloudFactory = mockPointCloudFactory
150-
151- self . viewer . show3D ( visibility = Visibility . POINT_CLOUD , pointCloudStyle = PointCloudStyle ( showSolidPoints = False ) )
141+ with self . _mockPointCloud ( aPointCloud ):
142+ self .viewer .show3D (
143+ visibility = Visibility . POINT_CLOUD , pointCloudStyle = PointCloudStyle ( showSolidPoints = False )
144+ )
152145
153146 self .mock3DViewer .addDataPoints .assert_called_once ()
154147 self .assertTrue (
@@ -157,19 +150,16 @@ def testWhenShow3DWithSurfacePointCloud_shouldOnlyDisplaySurfacePoints(self):
157150 self .mock3DViewer .show .assert_called_once ()
158151
159152 def testWhenShow3DWithEnteringSurfacePointCloud_shouldOnlyDisplayEnteringSurfacePoints (self ):
160- mockPointCloudFactory = mock (PointCloudFactory )
161153 aPointCloud = PointCloud (
162- solidPoints = np .array ([[0.5 , 0 , 0 , 0 ]]), surfacePoints = np .array ([[1 , 0 , 0 , 0 ], [- 1 , 1 , 1 , 1 ]])
163- )
164- when (mockPointCloudFactory ).getPointCloud (...).thenReturn (aPointCloud )
165- self .viewer ._pointCloudFactory = mockPointCloudFactory
166-
167- self .viewer .show3D (
168- visibility = Visibility .POINT_CLOUD ,
169- pointCloudStyle = PointCloudStyle (
170- showSolidPoints = False , showSurfacePointsLeaving = False , showSurfacePointsEntering = True
171- ),
154+ solidPoints = np .array ([[0.5 , 0 , 0 , 0 , 0 ]]), surfacePoints = np .array ([[1 , 0 , 0 , 0 , 0 ], [- 1 , 1 , 1 , 0 , 1 ]])
172155 )
156+ with self ._mockPointCloud (aPointCloud ):
157+ self .viewer .show3D (
158+ visibility = Visibility .POINT_CLOUD ,
159+ pointCloudStyle = PointCloudStyle (
160+ showSolidPoints = False , showSurfacePointsLeaving = False , showSurfacePointsEntering = True
161+ ),
162+ )
173163
174164 self .mock3DViewer .addDataPoints .assert_called_once ()
175165 self .assertTrue (
@@ -206,49 +196,38 @@ def testWhenShow3DWithViewsIndexList_shouldAdd2DImageOfTheseViewsInThe3DDisplay(
206196 self .mock3DViewer .show .assert_called_once ()
207197
208198 def testGiven3DLogger_whenShow3DDefault_shouldDisplayEverythingExceptViews (self ):
209- mockPointCloudFactory = mock (PointCloudFactory )
210199 aPointCloud = PointCloud ()
211- when (mockPointCloudFactory ).getPointCloud (...).thenReturn (aPointCloud )
212- self .viewer ._pointCloudFactory = mockPointCloudFactory
213200
214- self .viewer .show3D ()
201+ with self ._mockPointCloud (aPointCloud ):
202+ self .viewer .show3D ()
215203
216204 verify (self .source , times = 1 ).addToViewer (...)
217205 verify (self .scene , times = 1 ).addToViewer (...)
218- verify (mockPointCloudFactory , times = 1 ).getPointCloud (...)
219206 self .mock3DViewer .addImage .assert_not_called ()
220207 self .mock3DViewer .show .assert_called_once ()
221208
222209 def testGiven2DLogger_whenShow3DDefault_shouldDisplayEverythingExceptPointCloud (self ):
223210 self ._givenLoggerWithXSceneView ()
224211 self .logger .has3D = False
225212
226- mockPointCloudFactory = mock (PointCloudFactory )
227- when (mockPointCloudFactory ).getPointCloud (...).thenReturn ()
228- self .viewer ._pointCloudFactory = mockPointCloudFactory
229-
230- self .viewer .show3D ()
213+ with self ._mockPointCloud (PointCloud ()):
214+ self .viewer .show3D ()
231215
232216 verify (self .source , times = 1 ).addToViewer (...)
233217 verify (self .scene , times = 1 ).addToViewer (...)
234- verify (mockPointCloudFactory , times = 0 ).getPointCloud (...)
235218 self .mock3DViewer .addImage .assert_called ()
236219 self .mock3DViewer .show .assert_called_once ()
237220
238221 def testGiven2DLogger_whenShow3DWithDefault3DVisibility_shouldWarnAndDisplayDefault2D (self ):
239222 self ._givenLoggerWithXSceneView ()
240223 self .logger .has3D = False
241224
242- mockPointCloudFactory = mock (PointCloudFactory )
243- when (mockPointCloudFactory ).getPointCloud (...).thenReturn ()
244- self .viewer ._pointCloudFactory = mockPointCloudFactory
245-
246- with self .assertWarns (UserWarning ):
247- self .viewer .show3D (visibility = Visibility .DEFAULT_3D )
225+ with self ._mockPointCloud (PointCloud ()):
226+ with self .assertWarns (UserWarning ):
227+ self .viewer .show3D (visibility = Visibility .DEFAULT_3D )
248228
249229 verify (self .source , times = 1 ).addToViewer (...)
250230 verify (self .scene , times = 1 ).addToViewer (...)
251- verify (mockPointCloudFactory , times = 0 ).getPointCloud (...)
252231 self .mock3DViewer .addImage .assert_called ()
253232 self .mock3DViewer .show .assert_called_once ()
254233
@@ -260,3 +239,10 @@ def _givenLoggerWithXSceneView(self):
260239 self .logger .views = [sceneView ]
261240 when (self .logger ).updateView (sceneView ).thenReturn ()
262241 when (self .scene ).getBoundingBox ().thenReturn (BoundingBox ([- 2 , 2 ], [- 2 , 2 ], [0 , 5 ]))
242+
243+ @staticmethod
244+ def _mockPointCloud (pointCloud : PointCloud ):
245+ return patch (
246+ "pytissueoptics.rayscattering.display.viewer.PointCloudFactory.getPointCloud" ,
247+ return_value = pointCloud ,
248+ )
0 commit comments