1#!/pxrpythonsubst
2#
3# Copyright 2016 Pixar
4#
5# Licensed under the Apache License, Version 2.0 (the "Apache License")
6# with the following modification; you may not use this file except in
7# compliance with the Apache License and the following modification to it:
8# Section 6. Trademarks. is deleted and replaced with:
9#
10# 6. Trademarks. This License does not grant permission to use the trade
11#    names, trademarks, service marks, or product names of the Licensor
12#    and its affiliates, except as required to comply with Section 4(c) of
13#    the License and to reproduce the content of the NOTICE file.
14#
15# You may obtain a copy of the Apache License at
16#
17#     http://www.apache.org/licenses/LICENSE-2.0
18#
19# Unless required by applicable law or agreed to in writing, software
20# distributed under the Apache License with the above modification is
21# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22# KIND, either express or implied. See the Apache License for the specific
23# language governing permissions and limitations under the Apache License.
24#
25from __future__ import division
26
27import sys
28import unittest
29import math
30from pxr import Gf
31
32def err( msg ):
33    return "ERROR: " + msg + " failed"
34
35class TestGfLine(unittest.TestCase):
36
37    def test_Constructors(self):
38        self.assertIsInstance(Gf.Line(), Gf.Line)
39        self.assertIsInstance(Gf.Line(Gf.Vec3d(), Gf.Vec3d()), Gf.Line)
40
41    def test_Properties(self):
42        l = Gf.Line()
43        l.Set(Gf.Vec3d(1,2,3), Gf.Vec3d(2,3,4))
44        self.assertTrue(l.GetPoint(0) == Gf.Vec3d(1,2,3) and \
45            l.direction == Gf.Vec3d(2,3,4).GetNormalized(), err("Set"))
46
47        l.Set(Gf.Vec3d(0,0,0), Gf.Vec3d(0,1,0))
48        self.assertTrue(l.GetPoint(0.5) == Gf.Vec3d(0, 0.5, 0) and \
49            l.GetPoint(1.0) == Gf.Vec3d(0, 1, 0), err("GetPoint"))
50
51        l.Set(Gf.Vec3d(1,2,3), Gf.Vec3d(2,3,4))
52        l.direction = Gf.Vec3d(1, 1, 1)
53        self.assertEqual(l.direction, Gf.Vec3d(1, 1, 1).GetNormalized(), err("direction"))
54
55    def test_Methods(self):
56        l = Gf.Line(Gf.Vec3d(0, 0, 0), Gf.Vec3d(1, 1, 1))
57        (point, t) = l.FindClosestPoint(Gf.Vec3d(0.5, 0.5, 1))
58        self.assertTrue(Gf.IsClose(point, Gf.Vec3d(2./3, 2./3, 2./3), 0.00001), err("FindClosestPoint"))
59        self.assertTrue(Gf.IsClose(t, 1.1547, 0.0001), err("FindClosestPoint"))
60
61        # (parallel case)
62        l1 = Gf.Line(Gf.Vec3d(0, 0, 0), Gf.Vec3d(1, 1, 1))
63        l2 = Gf.Line(Gf.Vec3d(1, 0, 0), Gf.Vec3d(1, 1, 1))
64        self.assertEqual(Gf.FindClosestPoints(l1, l2)[0], False, err("FindClosestPoints"))
65
66        l1 = Gf.Line(Gf.Vec3d(0, 0, 0), Gf.Vec3d(1, 1, 1))
67        l2 = Gf.Line(Gf.Vec3d(1, 0, 0), Gf.Vec3d(1, -1, 1))
68        (intersects, p1, p2, t1, t2) = Gf.FindClosestPoints(l1, l2)
69        self.assertTrue(intersects, err("FindClosestPoints"))
70        self.assertTrue(Gf.IsClose(p1, Gf.Vec3d(0.25, 0.25, 0.25), 0.00001), err("FindClosestPoints"))
71        self.assertTrue(Gf.IsClose(p2, Gf.Vec3d(0.75, 0.25, -0.25), 0.00001), err("FindClosestPoints"))
72        self.assertTrue(Gf.IsClose(t1, 0.433012701892, 0.00001))
73        self.assertTrue(Gf.IsClose(t2, -0.433012701892, 0.00001))
74
75
76    def test_Operators(self):
77        l1 = Gf.Line(Gf.Vec3d(0, 0, 0), Gf.Vec3d(1, 1, 1))
78        l2 = Gf.Line(Gf.Vec3d(0, 0, 0), Gf.Vec3d(1, 1, 1))
79        self.assertEqual(l1, l2, err("equality"))
80
81        l1 = Gf.Line(Gf.Vec3d(0, 0, 0), Gf.Vec3d(1, 2, 3))
82        l2 = Gf.Line(Gf.Vec3d(0, 0, 0), Gf.Vec3d(1, 1, 1))
83        self.assertNotEqual(l1, 2, err("inequality"))
84
85        self.assertEqual(l1, eval(repr(l1)), err("repr"))
86        self.assertTrue(len(str(Gf.Line())), err("str"))
87
88if __name__ == '__main__':
89    unittest.main()
90