Parcourir la source

Add methods to find the cap_min and cap_max of a Panorama

Christian Proust il y a 9 ans
Parent
commit
98b4dc29e4
1 fichiers modifiés avec 39 ajouts et 0 suppressions
  1. 39 0
      panorama/models.py

+ 39 - 0
panorama/models.py

@@ -213,6 +213,45 @@ class Panorama(ReferencePoint):
                  "elevation": self.elevation(r.reference_point)}
                 for r in self.panorama_references.all()]
 
+    def cap_min(self):
+        return self._cap_minmax(True)
+
+    def cap_max(self):
+        return self._cap_minmax(False)
+
+    def _cap_minmax(self, ismin=True):
+        """Return the cap on the border of the image.
+
+        :param ismin: True if the min cap should be processed False if it is the
+        max.
+
+        @return None if the image is looping or if the image have less than two
+        references.
+        """
+        if self.loop:
+            return None
+
+        it = self.panorama_references.order_by(
+                'x' if ismin else '-x').iterator()
+
+        try:
+            ref1 = next(it)
+            ref2 = next(it)
+        except StopIteration:
+            return None
+
+        cap1 = self.bearing(ref1.reference_point)
+        cap2 = self.bearing(ref2.reference_point)
+        target_x = 0 if ismin else self.image_width
+
+        # For circulary issues
+        if cap2 < cap1:
+            cap2 += 360
+
+        target_cap =  cap1 + (target_x - ref1.x) * (cap2 - cap1) / \
+                (ref2.x - ref1.x)
+        return target_cap % 360
+
     def __str__(self):
         return "Panorama : " + self.name