Commit d4a7127c authored by Lars Kruse's avatar Lars Kruse

some first tests for intersections (low-level) and tool collisions (high level)

parent 90beea36
# -*- coding: utf-8 -*-
"""
Copyright 2013 Lars Kruse <devel@sumpfralle.de>
This file is part of PyCAM.
PyCAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PyCAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import pycam.Test
import pycam.Geometry.intersection
from pycam.Geometry.utils import INFINITE
from pycam.Geometry.Triangle import Triangle
from pycam.Geometry.Line import Line
class CircleIntersections(pycam.Test.PycamTestCase):
"""Circle collisions"""
def setUp(self):
self._circle = {"center": (2, 1, 10), "axis": (0, 0, 1), "radius": 3}
def test_line(self):
"""Circle->Line collisions"""
func = pycam.Geometry.intersection.intersect_circle_line
func_args = [self._circle["center"], self._circle["axis"], self._circle["radius"], self._circle["radius"] ** 2]
# additional arguments: direction, edge
"""
edge = Line((-1, -1, 4), (5, 5, 4))
coll = func(*(func_args + [(0, 0, -1)] + [edge]))
# The collision point seems to be the middle of the line.
# This is technically not necessary, but the current algorithm does it this way.
self.assertCollisionEqual(((1.5, 1.5, 10), (1.5, 1.5, 4), 6), coll)
"""
"""
# line dips into circle
edge = Line((4, 1, 3), (10, 1, 5))
coll = func(*(func_args + [(0, 0, -1)] + [edge]))
#self.assertCollisionEqual(((-1, 1, 10), (-1, 1, 2), 8), coll)
self.assertCollisionEqual(((5, 1, 10), (5, 1, 3.3333333333), 6.666666666), coll)
# horizontally skewed line
edge = Line((2, 1, 3), (8, 1, 5))
coll = func(*(func_args + [(0, 0, -1)] + [edge]))
#self.assertCollisionEqual(((-1, 1, 10), (-1, 1, 2), 8), coll)
self.assertCollisionEqual(((5, 1, 10), (5, 1, 4), 6), coll)
"""
# line touches circle
edge = Line((10, 10, 4), (5, 1, 4))
coll = func(*(func_args + [(0, 0, -1)] + [edge]))
self.assertCollisionEqual(((5, 1, 10), (5, 1, 4), 6), coll)
# no collision
edge = Line((10, 10, 4), (5.001, 1, 4))
coll = func(*(func_args + [(0, 0, -1)] + [edge]))
self.assertCollisionEqual((None, None, INFINITE), coll)
def test_plane(self):
"""Circle->Plane collisions"""
func = pycam.Geometry.intersection.intersect_circle_plane
func_args = [self._circle["center"], self._circle["radius"]]
# additional arguments: direction, triangle
triangle = Triangle((0, 5, 3), (5, 0, 3), (0, 0, 3))
coll = func(*(func_args + [(0, 0, -1)] + [triangle]))
self.assertCollisionEqual(((2, 1, 10), (2, 1, 3), 7), coll)
# slightly skewed
triangle = Triangle((2, 5, 3), (2, 0, 3), (-4, 1, 6))
coll = func(*(func_args + [(0, 0, -1)] + [triangle]))
self.assertCollisionEqual(((-1, 1, 10), (-1, 1, 4.5), 5.5), coll)
# skewed and shifted
triangle = Triangle((14, 5, -3), (14, 0, -3), (8, 1, 0))
coll = func(*(func_args + [(0, 0, -1)] + [triangle]))
self.assertCollisionEqual(((-1, 1, 10), (-1, 1, 4.5), 5.5), coll)
# vertical triangle
triangle = Triangle((14, 5, -3), (14, 0, -3), (14, 1, -6))
coll = func(*(func_args + [(0, 0, -1)] + [triangle]))
self.assertCollisionEqual((None, None, INFINITE), coll)
def test_point(self):
"""Circle->Point collisions"""
func = pycam.Geometry.intersection.intersect_circle_point
func_args = [self._circle["center"], self._circle["axis"], self._circle["radius"], self._circle["radius"] ** 2]
# additional arguments: direction, point
coll = func(*(func_args + [(0, 0, -1)] + [(0, 0, 0)]))
self.assertCollisionEqual(((0, 0, 10), (0, 0, 0), 10), coll)
# the same, but upwards
coll = func(*(func_args + [(0, 0, 1)] + [(0, 0, 0)]))
self.assertCollisionEqual(((0, 0, 10), (0, 0, 0), -10), coll)
# barely touching the point
coll = func(*(func_args + [(0, 0, -1)] + [(5, 1, 2)]))
self.assertCollisionEqual(((5, 1, 10), (5, 1, 2), 8), coll)
# not touching the point
coll = func(*(func_args + [(0, 0, -1)] + [(5.001, 1, 2)]))
self.assertCollisionEqual((None, None, INFINITE), coll)
# point is already inside of the circle
coll = func(*(func_args + [(0, 0, -1)] + [(2, 1, 10)]))
self.assertCollisionEqual(((2, 1, 10), (2, 1, 10), 0), coll)
if __name__ == "__main__":
pycam.Test.main()
# -*- coding: utf-8 -*-
"""
Copyright 2013 Lars Kruse <devel@sumpfralle.de>
This file is part of PyCAM.
PyCAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PyCAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import pycam.Test
from pycam.Geometry.Triangle import Triangle
from pycam.Cutters.CylindricalCutter import CylindricalCutter
from pycam.Cutters.SphericalCutter import SphericalCutter
import math
class CylindricalCutterCollisions(pycam.Test.PycamTestCase):
"""Cylindrical cutter collisions"""
def _drop(self, radius, triangle):
return CylindricalCutter(radius, location=(0, 0, 0)).drop(triangle)
def test_drop(self):
"Drop"
# flat triangle
flat_triangle = Triangle((-2, 2, 3), (2, 0, 3), (-2, -2, 3))
self.assertVectorEqual(self._drop(3, flat_triangle), (0, 0, 3))
# skewed triangle
skewed_triangle = Triangle((-2, 2, 1), (2, 0, 3), (-2, -2, 1))
self.assertVectorEqual(self._drop(1, skewed_triangle), (0, 0, 2.5))
self.assertVectorEqual(self._drop(1.5, skewed_triangle), (0, 0, 2.75))
self.assertVectorEqual(self._drop(1.9, skewed_triangle), (0, 0, 2.95))
#self.assertVectorEqual(self._drop(2.0, skewed_triangle), (0, 0, 3))
#self.assertVectorEqual(self._drop(2.1, skewed_triangle), (0, 0, 3))
#self.assertVectorEqual(self._drop(3, skewed_triangle), (0, 0, 3))
class SphericalCutterCollisions(pycam.Test.PycamTestCase):
"""Spherical cutter collisions"""
def _drop(self, radius, triangle):
return SphericalCutter(radius, location=(0, 0, 0)).drop(triangle)
def test_drop(self):
"Drop"
# flat triangle
flat_triangle = Triangle((-2, 2, 3), (2, 0, 3), (-2, -2, 3))
self.assertVectorEqual(self._drop(3, flat_triangle), (0, 0, 3))
"""
Vertical shifting based on angle of skewed triangle:
radius * (1/math.cos(math.pi/4) - 1)
30 degree -> radius * 0.15470053837925146
45 degree -> radius * 0.4142135623730949
60 degree -> radius * 1.0
"""
# skewed triangle
factors = {30: (1.0 / math.cos(math.pi / 6) - 1),
45: (1.0 / math.cos(math.pi / 4) - 1),
60: (1.0 / math.cos(math.pi / 3) - 1),
}
triangles = {}
triangles[30] = Triangle((-2, 2, 2), (2, 0, 4), (-2, -2, 2))
triangles[45] = Triangle((-2, 2, 1), (2, 0, 5), (-2, -2, 1))
triangles[60] = Triangle((-2, 2, -1), (2, 0, 7), (-2, -2, -1))
test_skew = lambda radius, degree: self.assertVectorEqual(
self._drop(radius, triangles[degree]), (0, 0, 3 + factors[degree] * radius))
test_skew(0.1, 45)
#test_skew(0.1, 30)
#test_skew(0.1, 60)
test_skew(1, 45)
#test_skew(1, 30)
#test_skew(1, 60)
test_skew(1.9, 45)
#test_skew(1.9, 30)
#test_skew(1.9, 60)
test_skew(2.0, 45)
#test_skew(2.0, 30)
#test_skew(2.0, 60)
test_skew(2.1, 45)
#test_skew(2.1, 30)
#test_skew(2.1, 60)
#test_skew(3, 45)
#test_skew(3, 30)
#test_skew(3, 60)
if __name__ == "__main__":
pycam.Test.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment