xyz.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # coding: utf-8
  2. from math import acos
  3. from math import degrees
  4. from math import sqrt
  5. from synergine2.exceptions import SynergineException
  6. from synergine2.simulation import Subject
  7. """
  8. Positions are exprimed as tuple: (x, y, z) Considering start point at top left:
  9. Z
  10. #
  11. #
  12. #
  13. #-------------> X
  14. |.
  15. | .
  16. | .
  17. |
  18. Y
  19. """
  20. COLLECTION_XYZ = 'COLLECTION_XYZ'
  21. NORTH = 11
  22. NORTH_EST = 12
  23. EST = 15
  24. SOUTH_EST = 18
  25. SOUTH = 17
  26. SOUTH_WEST = 16
  27. WEST = 13
  28. NORTH_WEST = 10
  29. DIRECTIONS = (
  30. NORTH,
  31. NORTH_EST,
  32. EST,
  33. SOUTH_EST,
  34. SOUTH,
  35. SOUTH_WEST,
  36. WEST,
  37. NORTH_WEST,
  38. )
  39. DIRECTION_FROM_NORTH_DEGREES = {
  40. (0, 22.5): NORTH,
  41. (22.5, 67): NORTH_EST,
  42. (67, 112.5): EST,
  43. (112.5, 157.5): SOUTH_EST,
  44. (157.5, 202.5): SOUTH,
  45. (202.5, 247.5): SOUTH_WEST,
  46. (247.5, 292.5): WEST,
  47. (292.5, 337.5): NORTH_WEST,
  48. (337.5, 360): NORTH,
  49. (337.5, 0): NORTH
  50. }
  51. DIRECTION_SLIGHTLY = {
  52. NORTH: (NORTH_WEST, NORTH, NORTH_EST),
  53. NORTH_EST: (NORTH, NORTH_EST, EST),
  54. EST: (NORTH_EST, EST, SOUTH_EST),
  55. SOUTH_EST: (EST, SOUTH_EST, SOUTH),
  56. SOUTH: (SOUTH_EST, SOUTH, SOUTH_WEST),
  57. SOUTH_WEST: (SOUTH, SOUTH_WEST, WEST),
  58. WEST: (SOUTH_WEST, WEST, NORTH_WEST),
  59. NORTH_WEST: (WEST, NORTH_WEST, NORTH),
  60. }
  61. DIRECTION_MODIFIERS = {
  62. NORTH_WEST: (-1, -1, 0),
  63. NORTH: (0, -1, 0),
  64. NORTH_EST: (1, -1, 0),
  65. WEST: (-1, 0, 0),
  66. EST: (1, 0, 0),
  67. SOUTH_WEST: (-1, 1, 0),
  68. SOUTH: (0, 1, 0),
  69. SOUTH_EST: (1, 1, 0),
  70. }
  71. class XYZException(SynergineException):
  72. pass
  73. class PositionNotPossible(XYZException):
  74. pass
  75. def get_degree_from_north(a, b):
  76. if a == b:
  77. return 0
  78. ax, ay = a[0], a[1]
  79. bx, by = b[0], b[1]
  80. Dx, Dy = ax, ay - 1
  81. ab = sqrt((bx - ax) ** 2 + (by - ay) ** 2)
  82. aD = sqrt((Dx - ax) ** 2 + (Dy - ay) ** 2)
  83. Db = sqrt((bx - Dx) ** 2 + (by - Dy) ** 2)
  84. degs = degrees(acos((ab ** 2 + aD ** 2 - Db ** 2) / (2 * ab * aD)))
  85. if bx < ax:
  86. return 360 - degs
  87. return degs
  88. class XYZSubjectMixinMetaClass(type):
  89. def __init__(cls, name, parents, attribs):
  90. super().__init__(name, parents, attribs)
  91. collections = getattr(cls, "collections", [])
  92. if COLLECTION_XYZ not in collections:
  93. collections.append(COLLECTION_XYZ)
  94. class XYZSubjectMixin(object, metaclass=XYZSubjectMixinMetaClass):
  95. def __init__(self, *args, **kwargs):
  96. """
  97. :param position: tuple with (x, y, z)
  98. """
  99. self._position = kwargs.pop('position')
  100. self.previous_direction = None
  101. super().__init__(*args, **kwargs)
  102. @property
  103. def position(self):
  104. return self._position
  105. @position.setter
  106. def position(self, value):
  107. self._position = value
  108. class XYZSubject(XYZSubjectMixin, Subject):
  109. pass
  110. class ProximityMixin(object):
  111. distance = 1
  112. feel_collections = [COLLECTION_XYZ]
  113. direction_round_decimals = 0
  114. distance_round_decimals = 2
  115. def have_to_check_position_is_possible(self) -> bool:
  116. return True
  117. def get_for_position(
  118. self,
  119. position,
  120. simulation: 'XYZSimulation',
  121. exclude_subject: Subject=None,
  122. ):
  123. subjects = []
  124. for feel_collection in self.feel_collections:
  125. # TODO: Optimiser en calculant directement les positions alentours et
  126. # en regardant si elles sont occupés dans subjects.xyz par un subject
  127. # etant dans fell_collection
  128. for subject in simulation.collections.get(feel_collection, []):
  129. if subject == exclude_subject:
  130. continue
  131. if self.have_to_check_position_is_possible() and not simulation.is_possible_position(subject.position):
  132. continue
  133. distance = round(
  134. self.get_distance_of(
  135. position=position,
  136. subject=subject,
  137. ),
  138. self.distance_round_decimals,
  139. )
  140. if distance <= self.distance and self.acceptable_subject(subject):
  141. direction = round(
  142. get_degree_from_north(
  143. position,
  144. subject.position,
  145. ),
  146. self.direction_round_decimals,
  147. )
  148. subjects.append({
  149. 'subject': subject,
  150. 'direction': direction,
  151. 'distance': distance,
  152. })
  153. return subjects
  154. @classmethod
  155. def get_distance_of(cls, position, subject: XYZSubjectMixin):
  156. from synergine2_xyz.utils import get_distance_between_points # cyclic import
  157. return get_distance_between_points(
  158. position,
  159. subject.position,
  160. )
  161. def acceptable_subject(self, subject: Subject) -> bool:
  162. return True
  163. def get_direction_from_north_degree(degree: float):
  164. for range, direction in DIRECTION_FROM_NORTH_DEGREES.items():
  165. if range[0] <= degree <= range[1]:
  166. return direction
  167. raise Exception('Degree {} out of range ({})'.format(
  168. degree,
  169. DIRECTION_FROM_NORTH_DEGREES,
  170. ))