@@ -29,13 +29,13 @@ template <int LocalDim, int EmbedDim> class Polygon {
2929
3030 // constructors
3131 Polygon () noexcept = default ;
32- Polygon (const Eigen:: Matrix<double , Dynamic, Dynamic>& nodes) noexcept : triangulation_() {
32+ Polygon (const Matrix<double , Dynamic, Dynamic>& nodes) noexcept : triangulation_() {
3333 fdapde_assert (nodes.rows () > 0 && nodes.cols () == embed_dim);
3434 if (internals::are_2d_counterclockwise_sorted (nodes)) {
3535 triangulate_ (nodes);
3636 } else { // nodes are in clocwise order, reverse node ordering
3737 int n_nodes = nodes.rows ();
38- Eigen:: Matrix<double , Dynamic, Dynamic> reversed_nodes (n_nodes, embed_dim);
38+ Matrix<double , Dynamic, Dynamic> reversed_nodes (n_nodes, embed_dim);
3939 for (int i = 0 ; i < n_nodes; ++i) { reversed_nodes.row (i) = nodes.row (n_nodes - 1 - i); }
4040 triangulate_ (reversed_nodes);
4141 }
@@ -44,11 +44,9 @@ template <int LocalDim, int EmbedDim> class Polygon {
4444 Polygon (const Polygon&) noexcept = default ;
4545 Polygon (Polygon&&) noexcept = default ;
4646 // observers
47- const Eigen::Matrix<double , Dynamic, Dynamic>& nodes () const { return triangulation_.nodes (); }
48- const Eigen::Matrix<int , Dynamic, Dynamic, Eigen::RowMajor>& cells () const { return triangulation_.cells (); }
49- Eigen::Map<const Eigen::Matrix<int , Dynamic, Dynamic, Eigen::RowMajor>> edges () const {
50- return triangulation_.edges ();
51- }
47+ const Matrix<double , Dynamic, Dynamic>& nodes () const { return triangulation_.nodes (); }
48+ const Matrix<int , Dynamic, Dynamic>& cells () const { return triangulation_.cells (); }
49+ MatrixView<const int , Dynamic, Dynamic> edges () const { return triangulation_.edges (); }
5250 const Triangulation<local_dim, embed_dim>& triangulation () const { return triangulation_; }
5351 double measure () const { return triangulation_.measure (); }
5452 int n_nodes () const { return triangulation_.n_nodes (); }
@@ -57,35 +55,37 @@ template <int LocalDim, int EmbedDim> class Polygon {
5755 // test whether point p is contained in polygon
5856 template <int Rows, int Cols>
5957 requires ((Rows == embed_dim && Cols == 1 ) || (Cols == embed_dim && Rows == 1 ))
60- bool contains (const Eigen:: Matrix<double , Rows, Cols>& p) const {
58+ bool contains (const Matrix<double , Rows, Cols>& p) const {
6159 return triangulation_.locate (p) != -1 ;
6260 }
6361 // random sample points in polygon
64- Eigen:: Matrix<double , Dynamic, Dynamic> sample (int n_samples, int seed = random_seed) const {
62+ Matrix<double , Dynamic, Dynamic> sample (int n_samples, int seed = random_seed) const {
6563 return triangulation_.sample (n_samples, seed);
6664 }
6765 private:
6866 // perform polygon triangulation
69- void triangulate_ (const Eigen:: Matrix<double , Dynamic, Dynamic>& nodes) {
67+ void triangulate_ (const Matrix<double , Dynamic, Dynamic>& nodes) {
7068 std::vector<int > cells;
7169 // perform monotone partitioning
7270 std::vector<std::vector<int >> poly_partition = monotone_partition_ (nodes);
7371 // triangulate each monotone polygon
7472 for (const std::vector<int >& poly : poly_partition) {
75- std::vector<int > local_cells = triangulate_monotone_ (nodes (poly, Eigen::all));
73+ Matrix<double , Dynamic, Dynamic> local_nodes (poly.size (), nodes.cols ());
74+ for (int i = 0 , n = poly.size (); i < n; ++i) { local_nodes.row (i) = nodes.row (poly[i]); }
75+ std::vector<int > local_cells = triangulate_monotone_ (local_nodes);
7676 // move local node numbering to global node numbering
7777 for (std::size_t i = 0 ; i < local_cells.size (); ++i) { local_cells[i] = poly[local_cells[i]]; }
7878 cells.insert (cells.end (), local_cells.begin (), local_cells.end ());
7979 }
8080 // set-up face-based data structure
8181 triangulation_ = Triangulation<LocalDim, EmbedDim>(
82- nodes, Eigen::Map<Eigen::Matrix< int , Dynamic, Dynamic, Eigen::RowMajor> >(cells.data (), cells.size () / 3 , 3 ),
83- Eigen:: Matrix<int , Dynamic, 1 >::Ones (nodes.rows ()));
82+ nodes, MatrixView< int , Dynamic, Dynamic>(cells.data (), cells.size () / 3 , 3 ),
83+ Matrix<int , Dynamic, 1 >::Ones (nodes.rows ()));
8484 }
8585
8686 // partition an arbitrary polygon P into a set of monotone polygons (plane sweep approach, section 3.2 of De Berg,
8787 // M. (2000). Computational geometry: algorithms and applications. Springer Science & Business Media.)
88- std::vector<std::vector<int >> monotone_partition_ (const Eigen:: Matrix<double , Dynamic, Dynamic>& coords) {
88+ std::vector<std::vector<int >> monotone_partition_ (const Matrix<double , Dynamic, Dynamic>& coords) {
8989 using poly_t = DCEL<local_dim, embed_dim>;
9090 using halfedge_t = typename poly_t ::halfedge_t ;
9191 using halfedge_ptr_t = std::add_pointer_t <halfedge_t >;
@@ -253,7 +253,7 @@ template <int LocalDim, int EmbedDim> class Polygon {
253253 return monotone_partition;
254254 }
255255 // triangulate monotone polygon (returns a RowMajor ordered matrix of cells).
256- std::vector<int > triangulate_monotone_ (const Eigen:: Matrix<double , Dynamic, Dynamic>& nodes) {
256+ std::vector<int > triangulate_monotone_ (const Matrix<double , Dynamic, Dynamic>& nodes) {
257257 // every triangulation of a polygon of n points has n - 2 triangles (lemma 1.2.2 of (1))
258258 std::vector<int > cells;
259259 int n_nodes = nodes.rows ();
@@ -361,15 +361,15 @@ template <int LocalDim, int EmbedDim> class MultiPolygon {
361361
362362 MultiPolygon () : n_polygons_(0 ) { }
363363 // rings is a vector of matrix of coordinates, where, each inner matrix defines a closed non self-intersecting loop
364- MultiPolygon (const std::vector<Eigen:: Matrix<double , Dynamic, Dynamic>>& rings) : n_polygons_(0 ) {
364+ MultiPolygon (const std::vector<Matrix<double , Dynamic, Dynamic>>& rings) : n_polygons_(0 ) {
365365 // ESRI shapefile format specification: loops in clockwise order define the outer border of a polygon,
366366 // loops in counterclockwise order defines a hole inside the last found outer polygonal ring
367367 int n_rings = rings.size ();
368368 if (n_rings == 1 ) {
369369 triangulation_ = Polygon<local_dim, embed_dim>(rings[0 ]).triangulation ();
370370 } else {
371371 // detect polygons with holes
372- using iterator = typename std::vector<Eigen:: Matrix<double , Dynamic, Dynamic>>::const_iterator;
372+ using iterator = typename std::vector<Matrix<double , Dynamic, Dynamic>>::const_iterator;
373373 constexpr int n_nodes_per_cell = 3 ;
374374 std::vector<std::pair<iterator, iterator>> polygons;
375375 iterator begin = rings.begin ();
@@ -385,10 +385,10 @@ template <int LocalDim, int EmbedDim> class MultiPolygon {
385385 polygons.emplace_back (begin, end);
386386 begin = end;
387387 }
388- Eigen:: Matrix<double , Dynamic, Dynamic> nodes (n_nodes, embed_dim);
388+ Matrix<double , Dynamic, Dynamic> nodes (n_nodes, embed_dim);
389389 std::vector<int > cells;
390390 int nodes_off_ = 0 ;
391- for (const Eigen:: Matrix<double , Dynamic, Dynamic>& coords : rings) {
391+ for (const Matrix<double , Dynamic, Dynamic>& coords : rings) {
392392 // triangulate polygon
393393 Triangulation<local_dim, embed_dim> poly_tri = Polygon<local_dim, embed_dim>(coords).triangulation ();
394394 nodes.middleRows (nodes_off_, poly_tri.n_nodes ()) = poly_tri.nodes ();
@@ -402,22 +402,18 @@ template <int LocalDim, int EmbedDim> class MultiPolygon {
402402 }
403403 // set up face-based storage
404404 triangulation_ = Triangulation<local_dim, embed_dim>(
405- nodes,
406- Eigen::Map<Eigen::Matrix<int , Dynamic, Dynamic, Eigen::RowMajor>>(
407- cells.data (), cells.size () / n_nodes_per_cell, n_nodes_per_cell),
408- Eigen::Matrix<int , Dynamic, 1 >::Ones (nodes.rows ()));
405+ nodes, MatrixView<int , Dynamic, Dynamic>(cells.data (), cells.size () / n_nodes_per_cell, n_nodes_per_cell),
406+ Matrix<int , Dynamic, 1 >::Ones (nodes.rows ()));
409407 }
410408 }
411409 // observers
412- const Eigen::Matrix<double , Dynamic, Dynamic>& nodes () const { return triangulation_.nodes (); }
413- const Eigen::Matrix<int , Dynamic, Dynamic, Eigen::RowMajor>& cells () const { return triangulation_.cells (); }
414- Eigen::Map<const Eigen::Matrix<int , Dynamic, Dynamic, Eigen::RowMajor>> edges () const {
415- return triangulation_.edges ();
416- }
410+ const Matrix<double , Dynamic, Dynamic>& nodes () const { return triangulation_.nodes (); }
411+ const Matrix<int , Dynamic, Dynamic>& cells () const { return triangulation_.cells (); }
412+ MatrixView<const int , Dynamic, Dynamic> edges () const { return triangulation_.edges (); }
417413 // computes only the boundary edges of the multipoligon (discards triangulation's edges)
418- Eigen:: Matrix<int , Dynamic, Dynamic, Eigen::RowMajor > boundary_edges () const {
414+ Matrix<int , Dynamic, Dynamic> boundary_edges () const {
419415 int n_edges = triangulation_.n_boundary_edges ();
420- Eigen:: Matrix<int , Dynamic, Dynamic, Eigen::RowMajor > m (n_edges, 2 );
416+ Matrix<int , Dynamic, Dynamic> m (n_edges, 2 );
421417 int j = 0 ;
422418 for (int i = 0 , k = triangulation_.n_edges (); i < k; ++i) {
423419 if (triangulation_.is_edge_on_boundary (i)) { m.row (j++) = triangulation_.edges ().row (i); }
@@ -434,11 +430,11 @@ template <int LocalDim, int EmbedDim> class MultiPolygon {
434430 // test whether point p is contained in polygon
435431 template <int Rows, int Cols>
436432 requires ((Rows == embed_dim && Cols == 1 ) || (Cols == embed_dim && Rows == 1 ))
437- bool contains (const Eigen:: Matrix<double , Rows, Cols>& p) const {
433+ bool contains (const Matrix<double , Rows, Cols>& p) const {
438434 return triangulation_.locate (p) != -1 ;
439435 }
440436 // random sample points in polygon
441- Eigen:: Matrix<double , Dynamic, Dynamic> sample (int n_samples, int seed = random_seed) const {
437+ Matrix<double , Dynamic, Dynamic> sample (int n_samples, int seed = random_seed) const {
442438 return triangulation_.sample (n_samples, seed);
443439 }
444440 private:
0 commit comments