Debian Codegraph @

python/src/geometry.cpp

raw
1 // Copyright (C) 2017 Chris N. Richardson and Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17
18 #include <memory>
19 #include <pybind11/pybind11.h>
20 #include <pybind11/eigen.h>
21 #include <pybind11/numpy.h>
22 #include <pybind11/stl.h>
23 #include <pybind11/operators.h>
24 #include <Eigen/Dense>
25
26 #include <dolfin/geometry/intersect.h>
27 #include <dolfin/geometry/BoundingBoxTree.h>
28 #include <dolfin/geometry/MeshPointIntersection.h>
29 #include <dolfin/geometry/CollisionPredicates.h>
30 #include <dolfin/geometry/IntersectionConstruction.h>
31 #include <dolfin/geometry/Point.h>
32 #include <dolfin/mesh/Mesh.h>
33
34 namespace py = pybind11;
35
36 namespace dolfin_wrappers
37 {
38
39 void geometry(py::module& m)
40 {
41 // dolfin::BoundingBoxTree
42 py::class_<dolfin::BoundingBoxTree, std::shared_ptr<dolfin::BoundingBoxTree>>
43 (m, "BoundingBoxTree")
44 .def(py::init<>())
45 .def("build", (void (dolfin::BoundingBoxTree::*)(const dolfin::Mesh&))
46 &dolfin::BoundingBoxTree::build)
47 .def("build", (void (dolfin::BoundingBoxTree::*)(const dolfin::Mesh&, std::size_t))
48 &dolfin::BoundingBoxTree::build)
49 .def("build", (void (dolfin::BoundingBoxTree::*)(const std::vector<dolfin::Point>&, std::size_t))
50 &dolfin::BoundingBoxTree::build)
51 .def("compute_collisions", (std::vector<unsigned int> (dolfin::BoundingBoxTree::*)(const dolfin::Point&) const)
52 &dolfin::BoundingBoxTree::compute_collisions)
53 .def("compute_collisions",
54 (std::pair<std::vector<unsigned int>, std::vector<unsigned int>>
55 (dolfin::BoundingBoxTree::*)(const dolfin::BoundingBoxTree&) const)
56 &dolfin::BoundingBoxTree::compute_collisions)
57 .def("compute_entity_collisions", (std::vector<unsigned int> (dolfin::BoundingBoxTree::*)(const dolfin::Point&) const)
58 &dolfin::BoundingBoxTree::compute_entity_collisions)
59 .def("compute_entity_collisions",
60 (std::pair<std::vector<unsigned int>, std::vector<unsigned int>>
61 (dolfin::BoundingBoxTree::*)(const dolfin::BoundingBoxTree&) const)
62 &dolfin::BoundingBoxTree::compute_entity_collisions)
63 .def("compute_first_collision", &dolfin::BoundingBoxTree::compute_first_collision)
64 .def("compute_first_entity_collision", &dolfin::BoundingBoxTree::compute_first_entity_collision)
65 .def("compute_closest_entity", &dolfin::BoundingBoxTree::compute_closest_entity);
66
67 // dolfin::Point
68 py::class_<dolfin::Point>(m, "Point")
69 .def(py::init<>())
70 .def(py::init<double, double, double>())
71 .def(py::init<double, double>())
72 .def(py::init<double>())
73 .def(py::init([](py::array_t<double> x)
74 {
75 auto b = x.request();
76 assert(b.shape.size() == 1);
77 assert(b.shape[0] <= 3);
78 return dolfin::Point(b.shape[0], x.data());
79 }))
80 .def("__getitem__", [](dolfin::Point& self, std::size_t index)
81 {
82 if (index > 2)
83 throw py::index_error("Out of range");
84 return self[index];
85 })
86 .def("__getitem__", [](const dolfin::Point& instance, py::slice slice)
87 {
88 std::size_t start, stop, step, slicelength;
89 if (!slice.compute(3, &start, &stop, &step, &slicelength))
90 throw py::error_already_set();
91
92 if (start != 0 or stop != 3 or step != 1)
93 throw std::range_error("Only full slices are supported");
94
95 return py::array_t<double>(3, instance.coordinates());
96 })
97 .def("__setitem__", [](dolfin::Point& self, std::size_t index, double value)
98 {
99 if (index > 2)
100 throw py::index_error("Out of range");
101 self[index] = value;
102 })
103 .def("__setitem__", [](dolfin::Point& instance, py::slice slice, py::array_t<double> values)
104 {
105 std::size_t start, stop, step, slicelength;
106 if (!slice.compute(3, &start, &stop, &step, &slicelength))
107 throw py::error_already_set();
108
109 if (start != 0 or stop != 3 or step != 1)
110 throw std::range_error("Only full slices are supported");
111
112 auto b = values.request();
113 if (b.ndim != 1)
114 throw std::range_error("Can only assign vector to a Point");
115 if (b.shape[0] != 3)
116 throw std::range_error("Can only assign vector of length 3 to a Point");
117
118 double* x = instance.coordinates();
119 std::copy_n(values.data(), 3, x);
120 })
121 .def(py::self + py::self)
122 .def(py::self - py::self)
123 .def(py::self == py::self)
124 .def(py::self * float())
125 .def(py::self / float())
126 .def("array",
127 [](dolfin::Point& self) { return Eigen::Vector3d(self.coordinates()); },
128 "Return copy of coordinate array")
129 .def("norm", &dolfin::Point::norm)
130 .def("x", &dolfin::Point::x)
131 .def("y", &dolfin::Point::y)
132 .def("z", &dolfin::Point::z)
133 .def("distance", &dolfin::Point::distance)
134 .def("dot", &dolfin::Point::dot);
135
136 // dolfin::MeshPointIntersection
137 py::class_<dolfin::MeshPointIntersection,
138 std::shared_ptr<dolfin::MeshPointIntersection>>
139 (m, "MeshPointIntersection")
140 .def("intersected_cells", &dolfin::MeshPointIntersection::intersected_cells);
141
142 // These classes are wrapped only to be able to write tests in python.
143 // They are not imported into the dolfin namespace in python, but must be accessed through
144 // dolfin.cpp.geometry
145 py::class_<dolfin::CollisionPredicates>(m, "CollisionPredicates")
146 .def_static("collides_segment_point_2d",
147 &dolfin::CollisionPredicates::collides_segment_point_2d)
148 .def_static("collides_triangle_point_2d",
149 &dolfin::CollisionPredicates::collides_triangle_point_2d)
150 .def_static("collides_triangle_triangle_2d",
151 &dolfin::CollisionPredicates::collides_triangle_triangle_2d)
152 .def_static("collides_segment_segment_2d",
153 &dolfin::CollisionPredicates::collides_segment_segment_2d);
154
155 py::class_<dolfin::IntersectionConstruction>(m, "IntersectionConstruction")
156 .def_static("intersection_triangle_triangle_2d",
157 &dolfin::IntersectionConstruction::intersection_triangle_triangle_2d)
158 .def_static("intersection_segment_segment_2d",
159 &dolfin::IntersectionConstruction::intersection_segment_segment_2d)
160 .def_static("intersection_triangle_segment_2d",
161 &dolfin::IntersectionConstruction::intersection_triangle_segment_2d);
162
163 // dolfin/geometry free functions
164 m.def("intersect", &dolfin::intersect);
165
166 }
167 }