Debian Codegraph @

src/python/PyImath/PyImathLine.cpp

raw
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 // clang-format off
7
8 #include <Python.h>
9 #define BOOST_BIND_GLOBAL_PLACEHOLDERS
10 #include <boost/python.hpp>
11 #include <boost/python/make_constructor.hpp>
12 #include <boost/format.hpp>
13 #include <ImathLineAlgo.h>
14 #include <ImathMatrix.h>
15 #include "PyImathLine.h"
16 #include "PyImathDecorators.h"
17 #include "PyImathExport.h"
18 #include "PyImath.h"
19 #include "PyImathVec.h"
20 #include "PyImathMathExc.h"
21
22
23 namespace PyImath{
24 using namespace boost::python;
25 using namespace IMATH_NAMESPACE;
26
27 template <class T> struct LineName {static const char *value;};
28 template <> const char *LineName<float>::value = "Line3f";
29 template <> const char *LineName<double>::value = "Line3d";
30
31 template <class T>
32 static Line3<T> *
33 Line3_construct_default()
34 {
35 Vec3<T> point1(T (0), T(0), T(0));
36 Vec3<T> point2(T (1), T(0), T(0));
37
38 return new Line3<T>(point1, point2);
39 }
40
41 template <class T>
42 static Line3<T> *
43 Line3_tuple_construct(const tuple &t0, const tuple &t1)
44 {
45 Vec3<T> v0, v1;
46 if(t0.attr("__len__")() == 3 && t1.attr("__len__")() == 3)
47 {
48 v0.x = extract<T>(t0[0]);
49 v0.y = extract<T>(t0[1]);
50 v0.z = extract<T>(t0[2]);
51
52 v1.x = extract<T>(t1[0]);
53 v1.y = extract<T>(t1[1]);
54 v1.z = extract<T>(t1[2]);
55
56 return new Line3<T>(v0, v1);
57 }
58 else
59 throw std::invalid_argument ("Line3 expects tuple of length 3");
60 }
61
62 template <class T, class S>
63 static Line3<T> *
64 Line3_line_construct(const Line3<S> &line)
65 {
66 Line3<T> *l = new Line3<T>;
67 l->pos = line.pos;
68 l->dir = line.dir;
69
70 return l;
71 }
72
73 template <class T>
74 static void
75 set1(Line3<T> &line, const Vec3<T> &p0, const Vec3<T> &p1)
76 {
77 MATH_EXC_ON;
78 line.set (p0, p1);
79 }
80
81 template <class T>
82 static void
83 setTuple(Line3<T> &line, const tuple &t0, const tuple &t1)
84 {
85 MATH_EXC_ON;
86 Vec3<T> v0, v1;
87 if(t0.attr("__len__")() == 3 && t1.attr("__len__")() == 3)
88 {
89 v0.x = extract<T>(t0[0]);
90 v0.y = extract<T>(t0[1]);
91 v0.z = extract<T>(t0[2]);
92
93 v1.x = extract<T>(t1[0]);
94 v1.y = extract<T>(t1[1]);
95 v1.z = extract<T>(t1[2]);
96
97 line.set(v0, v1);
98 }
99 else
100 throw std::invalid_argument ("Line3 expects tuple of length 3");
101 }
102
103 template <class T>
104 static Vec3<T>
105 pointAt(Line3<T> &line, T t)
106 {
107 MATH_EXC_ON;
108 return line.operator()(t);
109 }
110
111 template <class T>
112 static T
113 distanceTo1(Line3<T> &line, Vec3<T> &p)
114 {
115 MATH_EXC_ON;
116 return line.distanceTo(p);
117 }
118
119 template <class T>
120 static T
121 distanceTo2(Line3<T> &line, Line3<T> &other)
122 {
123 MATH_EXC_ON;
124 return line.distanceTo(other);
125 }
126
127 template <class T>
128 static T
129 distanceToTuple(Line3<T> line, const tuple &t)
130 {
131 Vec3<T> v;
132 if(t.attr("__len__")() == 3)
133 {
134 v.x = extract<T>(t[0]);
135 v.y = extract<T>(t[1]);
136 v.z = extract<T>(t[2]);
137
138 return line.distanceTo(v);
139 }
140 else
141 throw std::invalid_argument ( "Line3 expects tuple of length 3");
142 }
143
144 template <class T>
145 static Vec3<T>
146 closestPointTo1(Line3<T> line, const Vec3<T> &p)
147 {
148 MATH_EXC_ON;
149 return line.closestPointTo(p);
150 }
151
152 template <class T>
153 static Vec3<T>
154 closestPointTo2(Line3<T> line, const Line3<T> &other)
155 {
156 MATH_EXC_ON;
157 return line.closestPointTo(other);
158 }
159
160 template <class T>
161 static Vec3<T>
162 closestPointToTuple(Line3<T> line, const tuple &t)
163 {
164 MATH_EXC_ON;
165 Vec3<T> v;
166 if(t.attr("__len__")() == 3)
167 {
168 v.x = extract<T>(t[0]);
169 v.y = extract<T>(t[1]);
170 v.z = extract<T>(t[2]);
171
172 return line.closestPointTo(v);
173 }
174 else
175 throw std::invalid_argument ( "Line3 expects tuple of length 3");
176 }
177
178 template <class T>
179 static Vec3<T>
180 getPosition(Line3<T> &line)
181 {
182 return line.pos;
183 }
184
185 template <class T>
186 static void
187 setPosition(Line3<T> &line, const Vec3<T> &pos)
188 {
189 line.pos = pos;
190 }
191
192 template <class T>
193 static void
194 setPositionTuple(Line3<T> &line, const tuple &t)
195 {
196 Vec3<T> pos;
197 if(t.attr("__len__")() == 3)
198 {
199 pos.x = extract<T>(t[0]);
200 pos.y = extract<T>(t[1]);
201 pos.z = extract<T>(t[2]);
202
203 line.pos = pos;
204 }
205 else
206 throw std::invalid_argument ( "Line3 expects tuple of length 3");
207 }
208
209 template <class T>
210 static Vec3<T>
211 getDirection(Line3<T> &line)
212 {
213 return line.dir;
214 }
215
216 template <class T>
217 static void
218 setDirection(Line3<T> &line, const Vec3<T> &dir)
219 {
220 MATH_EXC_ON;
221 line.dir = dir.normalized();
222 }
223
224 template <class T>
225 static void
226 setDirectionTuple(Line3<T> &line, const tuple &t)
227 {
228 MATH_EXC_ON;
229 Vec3<T> dir;
230 if(t.attr("__len__")() == 3)
231 {
232 dir.x = extract<T>(t[0]);
233 dir.y = extract<T>(t[1]);
234 dir.z = extract<T>(t[2]);
235
236 line.dir = dir.normalized();
237 }
238 else
239 throw std::invalid_argument ( "Line3 expects tuple of length 3");
240 }
241
242 template <class T>
243 static void
244 closestPoints1(Line3<T> &line1, const Line3<T> &line2, Vec3<T> &p0, Vec3<T> &p1)
245 {
246 MATH_EXC_ON;
247 IMATH_NAMESPACE::closestPoints(line1, line2, p0, p1);
248 }
249
250 template <class T>
251 static tuple
252 closestPoints2(Line3<T> &line1, const Line3<T> &line2)
253 {
254 MATH_EXC_ON;
255 Vec3<T> p0, p1;
256 IMATH_NAMESPACE::closestPoints(line1, line2, p0, p1);
257 tuple p0Tuple = make_tuple(p0.x,p0.y,p0.z);
258 tuple p1Tuple = make_tuple(p1.x,p1.y,p1.z);
259
260 #if !defined(_MSC_VER) || (_MSC_VER <= 1200)
261 tuple t = make_tuple(p0Tuple, p1Tuple);
262 return t;
263 #else
264 list v3;
265 v3.append(p0Tuple);
266 v3.append(p1Tuple);
267 return tuple(v3);
268 #endif
269 }
270
271 template <class T>
272 static Vec3<T>
273 closestVertex(Line3<T> &line, const Vec3<T> &v0, const Vec3<T> &v1, const Vec3<T> &v2)
274 {
275 MATH_EXC_ON;
276 return IMATH_NAMESPACE::closestVertex(v0, v1, v2, line);
277 }
278
279 template <class T>
280 static Vec3<T>
281 closestVertexTuple(Line3<T> &line, const tuple &t0, const tuple &t1, const tuple &t2)
282 {
283 MATH_EXC_ON;
284 if(t0.attr("__len__")() == 3 && t1.attr("__len__")() == 3 && t2.attr("__len__")() == 3)
285 {
286 Vec3<T> v0, v1, v2;
287 v0.x = extract<T>(t0[0]);
288 v0.y = extract<T>(t0[1]);
289 v0.z = extract<T>(t0[2]);
290
291 v1.x = extract<T>(t1[0]);
292 v1.y = extract<T>(t1[1]);
293 v1.z = extract<T>(t1[2]);
294
295 v2.x = extract<T>(t2[0]);
296 v2.y = extract<T>(t2[1]);
297 v2.z = extract<T>(t2[2]);
298
299 return IMATH_NAMESPACE::closestVertex(v0, v1, v2, line);
300 }
301 else
302 throw std::invalid_argument ( "Line3 expects tuple of length 3");
303 }
304
305 template <class T>
306 static bool
307 intersect1(Line3<T> &line, const Vec3<T> &v0, const Vec3<T> &v1, const Vec3<T> &v2,
308 Vec3<T> &pt, Vec3<T> &barycentric, bool &front)
309 {
310 MATH_EXC_ON;
311 return IMATH_NAMESPACE::intersect(line, v0, v1, v2, pt, barycentric, front);
312 }
313
314 template <class T>
315 static object
316 intersect2(Line3<T> &line, const Vec3<T> &v0, const Vec3<T> &v1, const Vec3<T> &v2)
317 {
318 MATH_EXC_ON;
319 Vec3<T> pt, bar;
320 bool front;
321
322 if(IMATH_NAMESPACE::intersect(line, v0, v1, v2, pt, bar, front))
323 {
324 tuple t = make_tuple(pt, bar, front);
325 return t;
326 }
327 else
328 {
329 return object();
330 }
331 }
332
333 template <class T>
334 static tuple
335 intersectTuple(Line3<T> &line, const tuple &t0, const tuple &t1, const tuple &t2)
336 {
337
338 if(t0.attr("__len__")() == 3 && t1.attr("__len__")() == 3 && t2.attr("__len__")() == 3)
339 {
340 Vec3<T> v0, v1, v2, pt, bar;
341 bool front;
342 v0.x = extract<T>(t0[0]);
343 v0.y = extract<T>(t0[1]);
344 v0.z = extract<T>(t0[2]);
345
346 v1.x = extract<T>(t1[0]);
347 v1.y = extract<T>(t1[1]);
348 v1.z = extract<T>(t1[2]);
349
350 v2.x = extract<T>(t2[0]);
351 v2.y = extract<T>(t2[1]);
352 v2.z = extract<T>(t2[2]);
353
354 if(IMATH_NAMESPACE::intersect(line, v0, v1, v2, pt, bar, front))
355 {
356 tuple t = make_tuple(pt, bar, front);
357 return t;
358 }
359 else
360 {
361 tuple t;
362 return t;
363 }
364 }
365 else
366 throw std::invalid_argument ( "Line3 expects tuple of length 3");
367 }
368
369 template <class T>
370 static Vec3<T>
371 rotatePoint(Line3<T> &line, const Vec3<T> &p, const T &r)
372 {
373 MATH_EXC_ON;
374 return IMATH_NAMESPACE::rotatePoint(p, line, r);
375 }
376
377 template <class T>
378 static Vec3<T>
379 rotatePointTuple(Line3<T> &line, const tuple &t, const T &r)
380 {
381 MATH_EXC_ON;
382 if(t.attr("__len__")() == 3)
383 {
384 Vec3<T> p;
385 p.x = extract<T>(t[0]);
386 p.y = extract<T>(t[1]);
387 p.z = extract<T>(t[2]);
388
389 return IMATH_NAMESPACE::rotatePoint(p, line, r);
390 }
391 else
392 throw std::invalid_argument ( "Line3 expects tuple of length 3");
393 }
394
395 template <class T>
396 static std::string Line3_repr(const Line3<T> &v)
397 {
398 typename return_by_value::apply <Vec3<T> >::type converter;
399
400 Vec3<T> v1 = v.pos;
401 Vec3<T> v2 = v.pos + v.dir;
402
403 handle<> v1h (converter (v.pos));
404 handle<> v1Repr (PYUTIL_OBJECT_REPR (v1h.get()));
405 std::string v1ReprStr = extract<std::string> (v1Repr.get());
406
407 handle<> v2h (converter (v.pos + v.dir));
408 handle<> v2Repr (PYUTIL_OBJECT_REPR (v2h.get()));
409 std::string v2ReprStr = extract<std::string> (v2Repr.get());
410
411 std::stringstream stream;
412 stream << LineName<T>::value << "(" << v1ReprStr << ", " << v2ReprStr << ")";
413 return stream.str();
414 }
415
416 template <class T>
417 static bool
418 equal(const Line3<T> &l1, const Line3<T> &l2)
419 {
420 if(l1.pos == l2.pos && l1.dir == l2.dir)
421 return true;
422 else
423 return false;
424 }
425
426 template <class T>
427 static bool
428 notequal(const Line3<T> &l1, const Line3<T> &l2)
429 {
430 if(l1.pos != l2.pos || l1.dir != l2.dir)
431 return true;
432 else
433 return false;
434 }
435
436 template <class T>
437 class_<Line3<T> >
438 register_Line()
439 {
440 const char *name = LineName<T>::value;
441
442 class_<Line3<T> > line_class(name);
443 line_class
444 .def("__init__", make_constructor(Line3_construct_default<T>), "initialize point to (0,0,0) and direction to (1,0,0)")
445 .def("__init__", make_constructor(Line3_tuple_construct<T>))
446 .def("__init__", make_constructor(Line3_line_construct<T,float>))
447 .def("__init__", make_constructor(Line3_line_construct<T,double>))
448 .def(init<const Vec3<float> &, const Vec3<float> &>("Line3(point1, point2) construction"))
449 .def(init<const Vec3<double> &, const Vec3<double> &>("Line3(point1, point2) construction"))
450 .def(self * Matrix44<T>())
451 .def("__eq__", &equal<T>)
452 .def("__ne__", &notequal<T>)
453
454 .def_readwrite("pos", &Line3<T>::pos)
455 .def_readwrite("dir", &Line3<T>::dir)
456
457 .def("pos", &getPosition<T>,
458 "l.pos() -- returns the start point of line l")
459
460 .def("dir", &getDirection<T>,
461 "l.dir() -- returns the direction of line l\n")
462
463 .def("setPos", &setPosition<T>,
464 "l.setPos(p) -- sets the start point of line l to p")
465 .def("setPos", &setPositionTuple<T>)
466
467 .def("setDir", &setDirection<T>,
468 "l.setDir(d) -- sets the direction of line l\n"
469 "to d.normalized().\n")
470 .def("setDir", &setDirectionTuple<T>)
471
472 .def("set", &set1<T>,
473 "l.set(p1, p2) -- sets the start point\n"
474 "and direction of line l by calling\n"
475 " l.setPos (p1)\n"
476 " l.setDir (p2 - p1)\n")
477
478 .def("set", &setTuple<T>)
479
480 .def("pointAt", &pointAt<T>,
481 "l.pointAt(t) -- returns l.pos() + t * l.dir()")
482
483 .def("distanceTo", &distanceTo1<T>,
484 "l.distanceTo(p) -- returns the distance from\n"
485 " line l to point p\n")
486
487 .def("distanceTo", &distanceTo2<T>,
488 "l1.distanceTo(l2) -- returns the distance from\n"
489 " line l1 to line l2\n")
490
491 .def("distanceTo", &distanceToTuple<T>)
492
493 .def("closestPointTo", &closestPointTo1<T>,
494 "l.closestPointTo(p) -- returns the point on\n"
495 " line l that is closest to point p\n"
496 "\n")
497
498 .def("closestPointTo", &closestPointToTuple<T>)
499 .def("closestPointTo", &closestPointTo2<T>,
500 "l1.closestPointTo(l2) -- returns the point on\n"
501 " line l1 that is closest to line l2\n")
502
503 .def("closestPoints", &closestPoints1<T>,
504 "l1.closestPoints(l2,p0,p1)")
505
506 .def("closestPoints", &closestPoints2<T>,
507 "l1.closestPoints(l2) -- returns a tuple with\n"
508 "two points:\n"
509 " (l1.closestPoint(l2), l2.closestPoint(l1)\n")
510
511 .def("closestTriangleVertex", &closestVertex<T>,
512 "l.closestTriangleVertex(v0, v1, v2) -- returns\n"
513 "a copy of v0, v1, or v2, depending on which is\n"
514 "closest to line l.\n")
515
516 .def("closestTriangleVertex", &closestVertexTuple<T>)
517 .def("intersectWithTriangle", &intersect2<T>)
518 .def("intersectWithTriangle", &intersect1<T>,
519 "l.intersectWithTriangle(v0, v1, v2) -- computes the\n"
520 "intersection of line l and triangle (v0, v1, v2).\n"
521 "\n"
522 "If the line and the triangle do not intersect,\n"
523 "None is returned.\n"
524 ""
525 "If the line and the triangle intersect, a tuple\n"
526 "(p, b, f) is returned:\n"
527 "\n"
528 " p intersection point in 3D space\n"
529 "\n"
530 " b intersection point in barycentric coordinates\n"
531 "\n"
532 " f 1 if the line hits the triangle from the\n"
533 " front (((v2-v1) % (v1-v2)) ^ l.dir() < 0),\n"
534 " 0 if the line hits the trianble from the\n"
535 " back\n"
536 "\n")
537 .def("intersectWithTriangle", &intersectTuple<T>)
538
539 .def("rotatePoint", &rotatePoint<T>,
540 "l.rotatePoint(p,r) -- rotates point p around\n"
541 "line by angle r (in radians), and returns the\n"
542 "result (p is not modified)\n")
543
544 .def("rotatePoint", &rotatePointTuple<T>)
545 .def("__repr__",&Line3_repr<T>)
546 ;
547
548 decoratecopy(line_class);
549
550 return line_class;
551 }
552
553 template PYIMATH_EXPORT class_<Line3<float> > register_Line<float>();
554 template PYIMATH_EXPORT class_<Line3<double> > register_Line<double>();
555
556 } // namespace PyImath
557
558