1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24
25 #include "pxr/pxr.h"
26 #include "pxr/base/gf/line.h"
27 #include "pxr/base/tf/pyUtils.h"
28 #include "pxr/base/tf/wrapTypeHelpers.h"
29
30 #include <boost/python/class.hpp>
31 #include <boost/python/def.hpp>
32 #include <boost/python/copy_const_reference.hpp>
33 #include <boost/python/operators.hpp>
34 #include <boost/python/return_arg.hpp>
35 #include <boost/python/tuple.hpp>
36
37 #include <string>
38
39 using namespace boost::python;
40
41 using std::string;
42
43 PXR_NAMESPACE_USING_DIRECTIVE
44
45 namespace {
46
_Repr(GfLine const & self)47 static string _Repr(GfLine const &self) {
48 return TF_PY_REPR_PREFIX + "Line(" + TfPyRepr(self.GetPoint(0.0)) + ", " +
49 TfPyRepr(self.GetDirection()) + ")";
50 }
51
52
53 static tuple
FindClosestPointsHelper(const GfLine & l1,const GfLine & l2)54 FindClosestPointsHelper( const GfLine &l1, const GfLine &l2 )
55 {
56 GfVec3d p1(0), p2(0);
57 double t1 = 0.0, t2 = 0.0;
58 bool result = GfFindClosestPoints( l1, l2, &p1, &p2, &t1, &t2 );
59 return boost::python::make_tuple( result, p1, p2, t1, t2 );
60 }
61
62 static tuple
FindClosestPointHelper(const GfLine & self,const GfVec3d & point)63 FindClosestPointHelper( const GfLine &self, const GfVec3d &point )
64 {
65 double t;
66 GfVec3d result = self.FindClosestPoint( point, &t );
67 return boost::python::make_tuple( result, t );
68 }
69
70 static void
SetDirectionHelper(GfLine & self,const GfVec3d & dir)71 SetDirectionHelper( GfLine &self, const GfVec3d &dir )
72 {
73 self.Set( self.GetPoint(0.0), dir );
74 }
75
76 } // anonymous namespace
77
wrapLine()78 void wrapLine()
79 {
80 typedef GfLine This;
81
82 def("FindClosestPoints", FindClosestPointsHelper,
83 "FindClosestPoints( l1, l2 ) -> tuple<intersects = bool, p1 = GfVec3d, p2 = GfVec3d,"
84 " t1 = double, t2 = double>\n"
85 "\n"
86 "l1 : GfLine\n"
87 "l2 : GfLine\n"
88 "\n"
89 "Computes the closest points between two lines, returning a "
90 "tuple. The first item in the tuple is true if the lines"
91 "intersect. The two points are returned in p1 and p2. The "
92 "parametric distance of each point on the lines is returned "
93 "in t1 and t2.\n"
94 "----------------------------------------------------------------------" );
95
96 class_<This>( "Line", "Line class", init<>() )
97 .def(init< const GfVec3d &, const GfVec3d & >())
98
99 .def( TfTypePythonClass() )
100
101 .def("Set", &This::Set, return_self<>())
102
103 .def("GetPoint", &This::GetPoint)
104
105 .def("GetDirection", &This::GetDirection,
106 return_value_policy<copy_const_reference>())
107
108 .add_property( "direction",
109 make_function(&This::GetDirection,
110 return_value_policy
111 <copy_const_reference>()),
112 SetDirectionHelper )
113
114 .def( "FindClosestPoint", FindClosestPointHelper )
115
116 .def( str(self) )
117 .def( self == self )
118 .def( self != self )
119
120 .def("__repr__", _Repr)
121
122 ;
123
124 }
125