Repository: DCC-Lab/RayTracing

Issue:
ImagingPath and LaserPath methods in MatrixGroup raise exception.
They respectively return a ImagingPath instance and a LaserPath instance, but those classes are in other files not imported. It is also worth mentioning that they both inherit from MatrixGroup, so it is kind of incoherent to use them in MatrixGroup.

Question:
Are those methods really necessary in MatrixGroup?
- Yes: We have to find a way to import them without having a circular import problem.
- No: We should remove them.

Discussion:

Patch:
diff --git a/raytracing/matrixgroup.py b/raytracing/matrixgroup.py
index fe54cb8..b34038c 100644
--- a/raytracing/matrixgroup.py
+++ b/raytracing/matrixgroup.py
@@ -10,6 +10,7 @@ class MatrixGroup(Matrix):
     """
 
     def __init__(self, elements=None, label=""):
+        self.iteration = 0
         super(MatrixGroup, self).__init__(1, 0, 0, 1, label=label)
 
         self.elements = []
@@ -36,7 +37,7 @@ class MatrixGroup(Matrix):
         if len(self.elements) != 0:
             lastElement = self.elements[-1]
             if lastElement.backIndex != matrix.frontIndex:
-                if isinstance(matrix, Space): # For Space(), we fix it
+                if isinstance(matrix, Space):  # For Space(), we fix it
                     msg = "Fixing mismatched indices between last element and appended Space(). Use Space(d=someDistance, n=someIndex)."
                     warnings.warn(msg, UserWarning)
                     matrix.frontIndex = lastElement.backIndex
@@ -55,12 +56,6 @@ class MatrixGroup(Matrix):
         self.frontVertex = transferMatrix.frontVertex
         self.backVertex = transferMatrix.backVertex
 
-    def ImagingPath(self):
-        return ImagingPath(elements=self.elements, label=self.label)
-
-    def LaserPath(self):
-        return LaserPath(elements=self.elements, label=self.label)
-
     def transferMatrix(self, upTo=float('+Inf')):
         """ The transfer matrix between front edge and distance=upTo
 
@@ -227,3 +222,16 @@ class MatrixGroup(Matrix):
             axes.annotate(label, xy=(z, 0.0), xytext=(z, -halfHeight * 0.5),
                           xycoords='data', fontsize=12,
                           ha='center', va='bottom')
+
+    def __iter__(self):
+        self.iteration = 0
+        return self
+
+    def __next__(self):
+        if self.elements is None:
+            raise StopIteration
+        if self.iteration < len(self.elements):
+            element = self.elements[self.iteration]
+            self.iteration += 1
+            return element
+        raise StopIteration


Test patch:
diff --git a/raytracing/tests/testsMatrixGroup.py b/raytracing/tests/testsMatrixGroup.py
index 6d587cb..d473aa7 100644
--- a/raytracing/tests/testsMatrixGroup.py
+++ b/raytracing/tests/testsMatrixGroup.py
@@ -319,6 +319,11 @@ class TestMatrixGroup(unittest.TestCase):
         self.assertEqual(mg.D, supposedMatrix.D)
         self.assertEqual(mg.L, supposedMatrix.L)
 
+    def testInitWithAnotherMatrixGroup(self):
+        mg = MatrixGroup([Lens(5)])
+        mg2 = MatrixGroup(mg)
+        self.assertListEqual(mg.elements, mg2.elements)
+
 
 if __name__ == '__main__':
     unittest.main()