def test_intersectsRay(self): box = AxisAlignedBox(minimum=Vector(-5,-5,-5), maximum=Vector(5.0, 5.0, 5.0)) ray = Ray(Vector(-10.0, 0.0, 0.0), Vector(1.0, 0.0, 0.0)) result = box.intersectsRay(ray) self.assertNotEqual(False, result) self.assertEqual(5.0, result[0]) self.assertEqual(15.0, result[1]) ray = Ray(Vector(10.0, 0.0, 0.0), Vector(-1.0, 0.0, 0.0)) result = box.intersectsRay(ray) self.assertNotEqual(False, result) self.assertEqual(5.0, result[0]) self.assertEqual(15.0, result[1]) ray = Ray(Vector(0.0, -10.0, 0.0), Vector(0.0, 1.0, 0.0)) result = box.intersectsRay(ray) self.assertNotEqual(False, result) self.assertEqual(5.0, result[0]) self.assertEqual(15.0, result[1]) ray = Ray(Vector(0.0, 10.0, 0.0), Vector(0.0, -1.0, 0.0)) result = box.intersectsRay(ray) self.assertNotEqual(False, result) self.assertEqual(5.0, result[0]) self.assertEqual(15.0, result[1]) ray = Ray(Vector(0.0, 0.0, -10.0), Vector(0.0, 0.0, 1.0)) result = box.intersectsRay(ray) self.assertNotEqual(False, result) self.assertEqual(5.0, result[0]) self.assertEqual(15.0, result[1]) ray = Ray(Vector(0.0, 0.0, 10.0), Vector(0.0, 0.0, -1.0)) result = box.intersectsRay(ray) self.assertNotEqual(False, result) self.assertEqual(5.0, result[0]) self.assertEqual(15.0, result[1]) ray = Ray(Vector(15.0, 0.0, 0.0), Vector(0.0, 1.0, 0.0)) result = box.intersectsRay(ray) self.assertEqual(False, result) ray = Ray(Vector(15.0, 15.0, 0.0), Vector(-1.0, -1.0, 0.0)) result = box.intersectsRay(ray) self.assertNotEqual(False, result) self.assertEqual(10.0, result[0]) self.assertEqual(20.0, result[1]) ray = Ray(Vector(10.0, -15.0, 0.0), Vector(-1.0, 1.0, 0.0)) result = box.intersectsRay(ray) self.assertNotEqual(False, result) self.assertEqual(10.0, result[0]) self.assertEqual(15.0, result[1])
def getRay(self, x: float, y: float) -> Ray: window_x = ((x + 1) / 2) * self._window_width window_y = ((y + 1) / 2) * self._window_height view_x = (window_x / self._viewport_width) * 2 - 1 view_y = (window_y / self._viewport_height) * 2 - 1 inverted_projection = numpy.linalg.inv( self._projection_matrix.getData().copy()) transformation = self.getWorldTransformation().getData() near = numpy.array([view_x, -view_y, -1.0, 1.0], dtype=numpy.float32) near = numpy.dot(inverted_projection, near) near = numpy.dot(transformation, near) near = near[0:3] / near[3] far = numpy.array([view_x, -view_y, 1.0, 1.0], dtype=numpy.float32) far = numpy.dot(inverted_projection, far) far = numpy.dot(transformation, far) far = far[0:3] / far[3] direction = far - near direction /= numpy.linalg.norm(direction) return Ray(self.getWorldPosition(), Vector(-direction[0], -direction[1], -direction[2]))
def test_intersects(self): p = Plane(Vector.Unit_Y, 0.0) r = Ray(Vector(0, 10, 0), -Vector.Unit_Y) result = p.intersectsRay(r) self.assertNotEqual(False, result) self.assertEqual(10.0, result) r = Ray(Vector(0, -10, 0), Vector.Unit_Y) result = p.intersectsRay(r) self.assertNotEqual(False, result) self.assertEqual(10.0, result) r = Ray(Vector(0, 10, 0), Vector.Unit_Y) self.assertEqual(False, p.intersectsRay(r)) r = Ray(Vector(0, 0, 0), Vector.Unit_X) self.assertEqual(False, p.intersectsRay(r))
def getRay(self, x: float, y: float) -> Ray: """Get a ray from the camera into the world. This will create a ray from the camera's origin, passing through (x, y) on the near plane and continuing based on the projection matrix. :param x: The X coordinate on the near plane this ray should pass through. :param y: The Y coordinate on the near plane this ray should pass through. :return: A Ray object representing a ray from the camera origin through X, Y. :note The near-plane coordinates should be in normalized form, that is within (-1, 1). """ window_x = ((x + 1) / 2) * self._window_width window_y = ((y + 1) / 2) * self._window_height view_x = (window_x / self._viewport_width) * 2 - 1 view_y = (window_y / self._viewport_height) * 2 - 1 inverted_projection = numpy.linalg.inv( self._projection_matrix.getData().copy()) transformation = self.getWorldTransformation().getData() near = numpy.array([view_x, -view_y, -1.0, 1.0], dtype=numpy.float32) near = numpy.dot(inverted_projection, near) near = numpy.dot(transformation, near) near = near[0:3] / near[3] far = numpy.array([view_x, -view_y, 1.0, 1.0], dtype=numpy.float32) far = numpy.dot(inverted_projection, far) far = numpy.dot(transformation, far) far = far[0:3] / far[3] direction = far - near direction /= numpy.linalg.norm(direction) if self.isPerspective(): origin = self.getWorldPosition() direction = -direction else: # In orthographic mode, the origin is the click position on the plane where the camera resides, and that # plane is parallel to the near and the far planes. projection = numpy.array([view_x, -view_y, 0.0, 1.0], dtype=numpy.float32) projection = numpy.dot(inverted_projection, projection) projection = numpy.dot(transformation, projection) projection = projection[0:3] / projection[3] origin = Vector(data=projection) return Ray(origin, Vector(direction[0], direction[1], direction[2]))
def getRay(self, x: float, y: float) -> Ray: window_x = ((x + 1) / 2) * self._window_width window_y = ((y + 1) / 2) * self._window_height view_x = (window_x / self._viewport_width) * 2 - 1 view_y = (window_y / self._viewport_height) * 2 - 1 inverted_projection = numpy.linalg.inv( self._projection_matrix.getData().copy()) transformation = self.getWorldTransformation().getData() near = numpy.array([view_x, -view_y, -1.0, 1.0], dtype=numpy.float32) near = numpy.dot(inverted_projection, near) near = numpy.dot(transformation, near) near = near[0:3] / near[3] far = numpy.array([view_x, -view_y, 1.0, 1.0], dtype=numpy.float32) far = numpy.dot(inverted_projection, far) far = numpy.dot(transformation, far) far = far[0:3] / far[3] direction = far - near direction /= numpy.linalg.norm(direction) if self.isPerspective(): origin = self.getWorldPosition() direction = -direction else: # In orthographic mode, the origin is the click position on the plane where the camera resides, and that # plane is parallel to the near and the far planes. projection = numpy.array([view_x, -view_y, 0.0, 1.0], dtype=numpy.float32) projection = numpy.dot(inverted_projection, projection) projection = numpy.dot(transformation, projection) projection = projection[0:3] / projection[3] origin = Vector(data=projection) return Ray(origin, Vector(direction[0], direction[1], direction[2]))
# Copyright (c) 2015 Ultimaker B.V. # Uranium is released under the terms of the AGPLv3 or higher. from UM.Math.AxisAlignedBox import AxisAlignedBox from UM.Math.Ray import Ray from UM.Math.Vector import Vector @profile def intersects(box, ray): return box.intersectsRay(ray) ray = Ray(Vector(10, 10, 10), Vector(-1, -1, -1)) box = AxisAlignedBox(10, 10, 10) for i in range(100000): intersects(box, ray)