Repository: DCC-Lab/RayTracing

Issue:
MatrixGroup: intermediateConjugates gives duplicates
When we want the intermediate conjugates of some matrix groups, we have a list with duplicates. Is it normal?

Example:
```python
path = ImagingPath(System4f(10, 5))
print(path.intermediateConjugates())
```
the output is:
```python
[[30.0, -0.5], [30.0, -0.5]]
```
@dccote I feel like it is redundant information.

Comments:

Patch:
diff --git a/raytracing/figure.py b/raytracing/figure.py
index a0b9014..dee5eb4 100644
--- a/raytracing/figure.py
+++ b/raytracing/figure.py
@@ -180,6 +180,8 @@ class Figure:
         conjugates = self.path.intermediateConjugates()
         if len(conjugates) != 0:
             for (planePosition, magnification) in conjugates:
+                if not 0 <= planePosition <= self.path.L:
+                    continue
                 magnification = abs(magnification)
                 if displayRange < self.path._objectHeight * magnification:
                     displayRange = self.path._objectHeight * magnification
diff --git a/raytracing/matrixgroup.py b/raytracing/matrixgroup.py
index e6d9e57..0270ed3 100644
--- a/raytracing/matrixgroup.py
+++ b/raytracing/matrixgroup.py
@@ -262,7 +262,10 @@ class MatrixGroup(Matrix):
                 planePosition = transferMatrix.L + distance
                 if planePosition != 0 and conjugate is not None:
                     magnification = conjugate.A
-                    planes.append([planePosition, magnification])
+                    if any([isclose(pos, planePosition) and isclose(mag, magnification) for pos, mag in planes]):
+                        continue
+                    else:
+                        planes.append([planePosition, magnification])
         return planes
 
     def trace(self, inputRay):


Test patch:
diff --git a/raytracing/tests/envtest.py b/raytracing/tests/envtest.py
index c20889c..5a57a3b 100644
--- a/raytracing/tests/envtest.py
+++ b/raytracing/tests/envtest.py
@@ -5,7 +5,6 @@ import tempfile
 
 class RaytracingTestCase(unittest.TestCase):
     tempDir = os.path.join(tempfile.gettempdir(), "tempDir")
-    removeAlreadyExists = False
 
     def __init__(self, tests=()):
         super(RaytracingTestCase, self).__init__(tests)
@@ -13,13 +12,7 @@ class RaytracingTestCase(unittest.TestCase):
     @classmethod
     def createTempDirectory(cls):
         if os.path.exists(cls.tempDir):
-            if cls.removeAlreadyExists:
-                cls.deleteTempDirectory()
-            else:
-                fulltempDir = os.path.join(os.getcwd(), cls.tempDir)
-                msg = f"'{fulltempDir}' directory already exists. " \
-                    f"Please set RaytracingTestCase.removeAlreadyExists to True if you want to delete this directory."
-                raise FileExistsError(msg)
+            cls.deleteTempDirectory()
         os.mkdir(cls.tempDir)
 
     @classmethod
diff --git a/raytracing/tests/testsEnvtest.py b/raytracing/tests/testsEnvtest.py
index dc54551..cb0ae26 100644
--- a/raytracing/tests/testsEnvtest.py
+++ b/raytracing/tests/testsEnvtest.py
@@ -55,27 +55,8 @@ class TestEnvtestClass(unittest.TestCase):
     def testTemptempDir(self):
         self.assertTrue(self.tempDir.endswith("tempDir"))
 
-    def testRemoveAlreadyExistsFalse(self):
-        envtest.RaytracingTestCase.removeAlreadyExists = False
-        self.assertFalse(envtest.RaytracingTestCase.removeAlreadyExists)
-
-    def testRemoveAlreadyExistsTrue(self):
-        envtest.RaytracingTestCase.removeAlreadyExists = True
-        self.assertTrue(envtest.RaytracingTestCase.removeAlreadyExists)
-
-    def testCreateTempDirectoryAlreadyExistsDoNotDelete(self):
-        msg = f"'{self.tempDir}' directory already exists. Please set RaytracingTestCase.removeAlreadyExists to True if"
-        msg += f" you want to delete this directory."
-        envtest.RaytracingTestCase.removeAlreadyExists = False
-        os.mkdir(self.tempDir)
-        with self.assertRaises(FileExistsError) as context:
-            envtest.RaytracingTestCase.createTempDirectory()
-        self.assertEqual(str(context.exception), msg)
-        os.rmdir(self.tempDir)  # Delete the directory!
-
     def testCreateTempDirectoryAlreadyExistsAndDelete(self):
         tempDir = envtest.RaytracingTestCase.tempDir
-        envtest.RaytracingTestCase.removeAlreadyExists = True
         os.mkdir(tempDir)
         try:
             envtest.RaytracingTestCase.createTempDirectory()
diff --git a/raytracing/tests/testsFigure.py b/raytracing/tests/testsFigure.py
index daf1098..0c29ffd 100644
--- a/raytracing/tests/testsFigure.py
+++ b/raytracing/tests/testsFigure.py
@@ -14,12 +14,12 @@ class TestFigure(unittest.TestCase):
 
         self.assertEqual(path.figure.displayRange(), largestDiameter)
 
-    def testDisplayRange(self):
+    def testDisplayRangeImageOutOfView(self):
         path = ImagingPath()
         path.append(Space(2))
         path.append(CurvedMirror(-5, 10))
 
-        self.assertAlmostEqual(path.figure.displayRange(), 5 * 10)
+        self.assertAlmostEqual(path.figure.displayRange(), 20)
 
         path.objectHeight = 1
         self.assertEqual(path.figure.displayRange(), 10)
diff --git a/raytracing/tests/testsMatrixGroup.py b/raytracing/tests/testsMatrixGroup.py
index d6554e5..3f20888 100644
--- a/raytracing/tests/testsMatrixGroup.py
+++ b/raytracing/tests/testsMatrixGroup.py
@@ -275,6 +275,12 @@ class TestMatrixGroup(envtest.RaytracingTestCase):
         self.assertAlmostEqual(intermediateConjugates[0][0], results[0][0])
         self.assertAlmostEqual(intermediateConjugates[0][1], results[0][1])
 
+    def testIntermediateConjugatesDuplicates(self):
+        elements = [Space(10), Lens(10), Space(15), Lens(5), Space(5)]
+        mg = MatrixGroup(elements)
+        intermediateConj = mg.intermediateConjugates()
+        self.assertListEqual(intermediateConj, [[30.0, -0.5]])
+
     def testHasFiniteApertutreDiameter(self):
         space = Space(10, 1.2541255)
         mg = MatrixGroup([space])