xyz.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 ProximityMixin(object):
  109. distance = 1
  110. feel_collections = [COLLECTION_XYZ]
  111. direction_round_decimals = 0
  112. distance_round_decimals = 2
  113. def have_to_check_position_is_possible(self) -> bool:
  114. return True
  115. def get_for_position(
  116. self,
  117. position,
  118. simulation: 'XYZSimulation',
  119. exclude_subject: Subject=None,
  120. ):
  121. subjects = []
  122. for feel_collection in self.feel_collections:
  123. # TODO: Optimiser en calculant directement les positions alentours et
  124. # en regardant si elles sont occupés dans subjects.xyz par un subject
  125. # etant dans fell_collection
  126. for subject in simulation.collections.get(feel_collection, []):
  127. if subject == exclude_subject:
  128. continue
  129. if self.have_to_check_position_is_possible() and not simulation.is_possible_position(subject.position):
  130. continue
  131. distance = round(
  132. self.get_distance_of(
  133. position=position,
  134. subject=subject,
  135. ),
  136. self.distance_round_decimals,
  137. )
  138. if distance <= self.distance and self.acceptable_subject(subject):
  139. direction = round(
  140. get_degree_from_north(
  141. position,
  142. subject.position,
  143. ),
  144. self.direction_round_decimals,
  145. )
  146. subjects.append({
  147. 'subject': subject,
  148. 'direction': direction,
  149. 'distance': distance,
  150. })
  151. return subjects
  152. @classmethod
  153. def get_distance_of(cls, position, subject: XYZSubjectMixin):
  154. from synergine2_xyz.utils import get_distance_between_points # cyclic import
  155. return get_distance_between_points(
  156. position,
  157. subject.position,
  158. )
  159. def acceptable_subject(self, subject: Subject) -> bool:
  160. return True
  161. def get_direction_from_north_degree(degree: float):
  162. for range, direction in DIRECTION_FROM_NORTH_DEGREES.items():
  163. if range[0] <= degree <= range[1]:
  164. return direction
  165. raise Exception('Degree {} out of range ({})'.format(
  166. degree,
  167. DIRECTION_FROM_NORTH_DEGREES,
  168. ))