test_plane.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. from sympy.core.numbers import (Rational, pi)
  2. from sympy.core.singleton import S
  3. from sympy.core.symbol import (Dummy, symbols)
  4. from sympy.functions.elementary.miscellaneous import sqrt
  5. from sympy.functions.elementary.trigonometric import (asin, cos, sin)
  6. from sympy.geometry import Line, Point, Ray, Segment, Point3D, Line3D, Ray3D, Segment3D, Plane, Circle
  7. from sympy.geometry.util import are_coplanar
  8. from sympy.testing.pytest import raises
  9. def test_plane():
  10. x, y, z, u, v = symbols('x y z u v', real=True)
  11. p1 = Point3D(0, 0, 0)
  12. p2 = Point3D(1, 1, 1)
  13. p3 = Point3D(1, 2, 3)
  14. pl3 = Plane(p1, p2, p3)
  15. pl4 = Plane(p1, normal_vector=(1, 1, 1))
  16. pl4b = Plane(p1, p2)
  17. pl5 = Plane(p3, normal_vector=(1, 2, 3))
  18. pl6 = Plane(Point3D(2, 3, 7), normal_vector=(2, 2, 2))
  19. pl7 = Plane(Point3D(1, -5, -6), normal_vector=(1, -2, 1))
  20. pl8 = Plane(p1, normal_vector=(0, 0, 1))
  21. pl9 = Plane(p1, normal_vector=(0, 12, 0))
  22. pl10 = Plane(p1, normal_vector=(-2, 0, 0))
  23. pl11 = Plane(p2, normal_vector=(0, 0, 1))
  24. l1 = Line3D(Point3D(5, 0, 0), Point3D(1, -1, 1))
  25. l2 = Line3D(Point3D(0, -2, 0), Point3D(3, 1, 1))
  26. l3 = Line3D(Point3D(0, -1, 0), Point3D(5, -1, 9))
  27. raises(ValueError, lambda: Plane(p1, p1, p1))
  28. assert Plane(p1, p2, p3) != Plane(p1, p3, p2)
  29. assert Plane(p1, p2, p3).is_coplanar(Plane(p1, p3, p2))
  30. assert Plane(p1, p2, p3).is_coplanar(p1)
  31. assert Plane(p1, p2, p3).is_coplanar(Circle(p1, 1)) is False
  32. assert Plane(p1, normal_vector=(0, 0, 1)).is_coplanar(Circle(p1, 1))
  33. assert pl3 == Plane(Point3D(0, 0, 0), normal_vector=(1, -2, 1))
  34. assert pl3 != pl4
  35. assert pl4 == pl4b
  36. assert pl5 == Plane(Point3D(1, 2, 3), normal_vector=(1, 2, 3))
  37. assert pl5.equation(x, y, z) == x + 2*y + 3*z - 14
  38. assert pl3.equation(x, y, z) == x - 2*y + z
  39. assert pl3.p1 == p1
  40. assert pl4.p1 == p1
  41. assert pl5.p1 == p3
  42. assert pl4.normal_vector == (1, 1, 1)
  43. assert pl5.normal_vector == (1, 2, 3)
  44. assert p1 in pl3
  45. assert p1 in pl4
  46. assert p3 in pl5
  47. assert pl3.projection(Point(0, 0)) == p1
  48. p = pl3.projection(Point3D(1, 1, 0))
  49. assert p == Point3D(Rational(7, 6), Rational(2, 3), Rational(1, 6))
  50. assert p in pl3
  51. l = pl3.projection_line(Line(Point(0, 0), Point(1, 1)))
  52. assert l == Line3D(Point3D(0, 0, 0), Point3D(Rational(7, 6), Rational(2, 3), Rational(1, 6)))
  53. assert l in pl3
  54. # get a segment that does not intersect the plane which is also
  55. # parallel to pl3's normal veector
  56. t = Dummy()
  57. r = pl3.random_point()
  58. a = pl3.perpendicular_line(r).arbitrary_point(t)
  59. s = Segment3D(a.subs(t, 1), a.subs(t, 2))
  60. assert s.p1 not in pl3 and s.p2 not in pl3
  61. assert pl3.projection_line(s).equals(r)
  62. assert pl3.projection_line(Segment(Point(1, 0), Point(1, 1))) == \
  63. Segment3D(Point3D(Rational(5, 6), Rational(1, 3), Rational(-1, 6)), Point3D(Rational(7, 6), Rational(2, 3), Rational(1, 6)))
  64. assert pl6.projection_line(Ray(Point(1, 0), Point(1, 1))) == \
  65. Ray3D(Point3D(Rational(14, 3), Rational(11, 3), Rational(11, 3)), Point3D(Rational(13, 3), Rational(13, 3), Rational(10, 3)))
  66. assert pl3.perpendicular_line(r.args) == pl3.perpendicular_line(r)
  67. assert pl3.is_parallel(pl6) is False
  68. assert pl4.is_parallel(pl6)
  69. assert pl3.is_parallel(Line(p1, p2))
  70. assert pl6.is_parallel(l1) is False
  71. assert pl3.is_perpendicular(pl6)
  72. assert pl4.is_perpendicular(pl7)
  73. assert pl6.is_perpendicular(pl7)
  74. assert pl6.is_perpendicular(pl4) is False
  75. assert pl6.is_perpendicular(l1) is False
  76. assert pl6.is_perpendicular(Line((0, 0, 0), (1, 1, 1)))
  77. assert pl6.is_perpendicular((1, 1)) is False
  78. assert pl6.distance(pl6.arbitrary_point(u, v)) == 0
  79. assert pl7.distance(pl7.arbitrary_point(u, v)) == 0
  80. assert pl6.distance(pl6.arbitrary_point(t)) == 0
  81. assert pl7.distance(pl7.arbitrary_point(t)) == 0
  82. assert pl6.p1.distance(pl6.arbitrary_point(t)).simplify() == 1
  83. assert pl7.p1.distance(pl7.arbitrary_point(t)).simplify() == 1
  84. assert pl3.arbitrary_point(t) == Point3D(-sqrt(30)*sin(t)/30 + \
  85. 2*sqrt(5)*cos(t)/5, sqrt(30)*sin(t)/15 + sqrt(5)*cos(t)/5, sqrt(30)*sin(t)/6)
  86. assert pl3.arbitrary_point(u, v) == Point3D(2*u - v, u + 2*v, 5*v)
  87. assert pl7.distance(Point3D(1, 3, 5)) == 5*sqrt(6)/6
  88. assert pl6.distance(Point3D(0, 0, 0)) == 4*sqrt(3)
  89. assert pl6.distance(pl6.p1) == 0
  90. assert pl7.distance(pl6) == 0
  91. assert pl7.distance(l1) == 0
  92. assert pl6.distance(Segment3D(Point3D(2, 3, 1), Point3D(1, 3, 4))) == \
  93. pl6.distance(Point3D(1, 3, 4)) == 4*sqrt(3)/3
  94. assert pl6.distance(Segment3D(Point3D(1, 3, 4), Point3D(0, 3, 7))) == \
  95. pl6.distance(Point3D(0, 3, 7)) == 2*sqrt(3)/3
  96. assert pl6.distance(Segment3D(Point3D(0, 3, 7), Point3D(-1, 3, 10))) == 0
  97. assert pl6.distance(Segment3D(Point3D(-1, 3, 10), Point3D(-2, 3, 13))) == 0
  98. assert pl6.distance(Segment3D(Point3D(-2, 3, 13), Point3D(-3, 3, 16))) == \
  99. pl6.distance(Point3D(-2, 3, 13)) == 2*sqrt(3)/3
  100. assert pl6.distance(Plane(Point3D(5, 5, 5), normal_vector=(8, 8, 8))) == sqrt(3)
  101. assert pl6.distance(Ray3D(Point3D(1, 3, 4), direction_ratio=[1, 0, -3])) == 4*sqrt(3)/3
  102. assert pl6.distance(Ray3D(Point3D(2, 3, 1), direction_ratio=[-1, 0, 3])) == 0
  103. assert pl6.angle_between(pl3) == pi/2
  104. assert pl6.angle_between(pl6) == 0
  105. assert pl6.angle_between(pl4) == 0
  106. assert pl7.angle_between(Line3D(Point3D(2, 3, 5), Point3D(2, 4, 6))) == \
  107. -asin(sqrt(3)/6)
  108. assert pl6.angle_between(Ray3D(Point3D(2, 4, 1), Point3D(6, 5, 3))) == \
  109. asin(sqrt(7)/3)
  110. assert pl7.angle_between(Segment3D(Point3D(5, 6, 1), Point3D(1, 2, 4))) == \
  111. asin(7*sqrt(246)/246)
  112. assert are_coplanar(l1, l2, l3) is False
  113. assert are_coplanar(l1) is False
  114. assert are_coplanar(Point3D(2, 7, 2), Point3D(0, 0, 2),
  115. Point3D(1, 1, 2), Point3D(1, 2, 2))
  116. assert are_coplanar(Plane(p1, p2, p3), Plane(p1, p3, p2))
  117. assert Plane.are_concurrent(pl3, pl4, pl5) is False
  118. assert Plane.are_concurrent(pl6) is False
  119. raises(ValueError, lambda: Plane.are_concurrent(Point3D(0, 0, 0)))
  120. raises(ValueError, lambda: Plane((1, 2, 3), normal_vector=(0, 0, 0)))
  121. assert pl3.parallel_plane(Point3D(1, 2, 5)) == Plane(Point3D(1, 2, 5), \
  122. normal_vector=(1, -2, 1))
  123. # perpendicular_plane
  124. p = Plane((0, 0, 0), (1, 0, 0))
  125. # default
  126. assert p.perpendicular_plane() == Plane(Point3D(0, 0, 0), (0, 1, 0))
  127. # 1 pt
  128. assert p.perpendicular_plane(Point3D(1, 0, 1)) == \
  129. Plane(Point3D(1, 0, 1), (0, 1, 0))
  130. # pts as tuples
  131. assert p.perpendicular_plane((1, 0, 1), (1, 1, 1)) == \
  132. Plane(Point3D(1, 0, 1), (0, 0, -1))
  133. # more than two planes
  134. raises(ValueError, lambda: p.perpendicular_plane((1, 0, 1), (1, 1, 1), (1, 1, 0)))
  135. a, b = Point3D(0, 0, 0), Point3D(0, 1, 0)
  136. Z = (0, 0, 1)
  137. p = Plane(a, normal_vector=Z)
  138. # case 4
  139. assert p.perpendicular_plane(a, b) == Plane(a, (1, 0, 0))
  140. n = Point3D(*Z)
  141. # case 1
  142. assert p.perpendicular_plane(a, n) == Plane(a, (-1, 0, 0))
  143. # case 2
  144. assert Plane(a, normal_vector=b.args).perpendicular_plane(a, a + b) == \
  145. Plane(Point3D(0, 0, 0), (1, 0, 0))
  146. # case 1&3
  147. assert Plane(b, normal_vector=Z).perpendicular_plane(b, b + n) == \
  148. Plane(Point3D(0, 1, 0), (-1, 0, 0))
  149. # case 2&3
  150. assert Plane(b, normal_vector=b.args).perpendicular_plane(n, n + b) == \
  151. Plane(Point3D(0, 0, 1), (1, 0, 0))
  152. p = Plane(a, normal_vector=(0, 0, 1))
  153. assert p.perpendicular_plane() == Plane(a, normal_vector=(1, 0, 0))
  154. assert pl6.intersection(pl6) == [pl6]
  155. assert pl4.intersection(pl4.p1) == [pl4.p1]
  156. assert pl3.intersection(pl6) == [
  157. Line3D(Point3D(8, 4, 0), Point3D(2, 4, 6))]
  158. assert pl3.intersection(Line3D(Point3D(1,2,4), Point3D(4,4,2))) == [
  159. Point3D(2, Rational(8, 3), Rational(10, 3))]
  160. assert pl3.intersection(Plane(Point3D(6, 0, 0), normal_vector=(2, -5, 3))
  161. ) == [Line3D(Point3D(-24, -12, 0), Point3D(-25, -13, -1))]
  162. assert pl6.intersection(Ray3D(Point3D(2, 3, 1), Point3D(1, 3, 4))) == [
  163. Point3D(-1, 3, 10)]
  164. assert pl6.intersection(Segment3D(Point3D(2, 3, 1), Point3D(1, 3, 4))) == []
  165. assert pl7.intersection(Line(Point(2, 3), Point(4, 2))) == [
  166. Point3D(Rational(13, 2), Rational(3, 4), 0)]
  167. r = Ray(Point(2, 3), Point(4, 2))
  168. assert Plane((1,2,0), normal_vector=(0,0,1)).intersection(r) == [
  169. Ray3D(Point(2, 3), Point(4, 2))]
  170. assert pl9.intersection(pl8) == [Line3D(Point3D(0, 0, 0), Point3D(12, 0, 0))]
  171. assert pl10.intersection(pl11) == [Line3D(Point3D(0, 0, 1), Point3D(0, 2, 1))]
  172. assert pl4.intersection(pl8) == [Line3D(Point3D(0, 0, 0), Point3D(1, -1, 0))]
  173. assert pl11.intersection(pl8) == []
  174. assert pl9.intersection(pl11) == [Line3D(Point3D(0, 0, 1), Point3D(12, 0, 1))]
  175. assert pl9.intersection(pl4) == [Line3D(Point3D(0, 0, 0), Point3D(12, 0, -12))]
  176. assert pl3.random_point() in pl3
  177. assert pl3.random_point(seed=1) in pl3
  178. # test geometrical entity using equals
  179. assert pl4.intersection(pl4.p1)[0].equals(pl4.p1)
  180. assert pl3.intersection(pl6)[0].equals(Line3D(Point3D(8, 4, 0), Point3D(2, 4, 6)))
  181. pl8 = Plane((1, 2, 0), normal_vector=(0, 0, 1))
  182. assert pl8.intersection(Line3D(p1, (1, 12, 0)))[0].equals(Line((0, 0, 0), (0.1, 1.2, 0)))
  183. assert pl8.intersection(Ray3D(p1, (1, 12, 0)))[0].equals(Ray((0, 0, 0), (1, 12, 0)))
  184. assert pl8.intersection(Segment3D(p1, (21, 1, 0)))[0].equals(Segment3D(p1, (21, 1, 0)))
  185. assert pl8.intersection(Plane(p1, normal_vector=(0, 0, 112)))[0].equals(pl8)
  186. assert pl8.intersection(Plane(p1, normal_vector=(0, 12, 0)))[0].equals(
  187. Line3D(p1, direction_ratio=(112 * pi, 0, 0)))
  188. assert pl8.intersection(Plane(p1, normal_vector=(11, 0, 1)))[0].equals(
  189. Line3D(p1, direction_ratio=(0, -11, 0)))
  190. assert pl8.intersection(Plane(p1, normal_vector=(1, 0, 11)))[0].equals(
  191. Line3D(p1, direction_ratio=(0, 11, 0)))
  192. assert pl8.intersection(Plane(p1, normal_vector=(-1, -1, -11)))[0].equals(
  193. Line3D(p1, direction_ratio=(1, -1, 0)))
  194. assert pl3.random_point() in pl3
  195. assert len(pl8.intersection(Ray3D(Point3D(0, 2, 3), Point3D(1, 0, 3)))) == 0
  196. # check if two plane are equals
  197. assert pl6.intersection(pl6)[0].equals(pl6)
  198. assert pl8.equals(Plane(p1, normal_vector=(0, 12, 0))) is False
  199. assert pl8.equals(pl8)
  200. assert pl8.equals(Plane(p1, normal_vector=(0, 0, -12)))
  201. assert pl8.equals(Plane(p1, normal_vector=(0, 0, -12*sqrt(3))))
  202. assert pl8.equals(p1) is False
  203. # issue 8570
  204. l2 = Line3D(Point3D(Rational(50000004459633, 5000000000000),
  205. Rational(-891926590718643, 1000000000000000),
  206. Rational(231800966893633, 100000000000000)),
  207. Point3D(Rational(50000004459633, 50000000000000),
  208. Rational(-222981647679771, 250000000000000),
  209. Rational(231800966893633, 100000000000000)))
  210. p2 = Plane(Point3D(Rational(402775636372767, 100000000000000),
  211. Rational(-97224357654973, 100000000000000),
  212. Rational(216793600814789, 100000000000000)),
  213. (-S('9.00000087501922'), -S('4.81170658872543e-13'),
  214. S('0.0')))
  215. assert str([i.n(2) for i in p2.intersection(l2)]) == \
  216. '[Point3D(4.0, -0.89, 2.3)]'
  217. def test_dimension_normalization():
  218. A = Plane(Point3D(1, 1, 2), normal_vector=(1, 1, 1))
  219. b = Point(1, 1)
  220. assert A.projection(b) == Point(Rational(5, 3), Rational(5, 3), Rational(2, 3))
  221. a, b = Point(0, 0), Point3D(0, 1)
  222. Z = (0, 0, 1)
  223. p = Plane(a, normal_vector=Z)
  224. assert p.perpendicular_plane(a, b) == Plane(Point3D(0, 0, 0), (1, 0, 0))
  225. assert Plane((1, 2, 1), (2, 1, 0), (3, 1, 2)
  226. ).intersection((2, 1)) == [Point(2, 1, 0)]
  227. def test_parameter_value():
  228. t, u, v = symbols("t, u v")
  229. p1, p2, p3 = Point(0, 0, 0), Point(0, 0, 1), Point(0, 1, 0)
  230. p = Plane(p1, p2, p3)
  231. assert p.parameter_value((0, -3, 2), t) == {t: asin(2*sqrt(13)/13)}
  232. assert p.parameter_value((0, -3, 2), u, v) == {u: 3, v: 2}
  233. assert p.parameter_value(p1, t) == p1
  234. raises(ValueError, lambda: p.parameter_value((1, 0, 0), t))
  235. raises(ValueError, lambda: p.parameter_value(Line(Point(0, 0), Point(1, 1)), t))
  236. raises(ValueError, lambda: p.parameter_value((0, -3, 2), t, 1))