From 1f10fb93d7692da50013a274c30430d6e2370e8c Mon Sep 17 00:00:00 2001 From: Julian Stahl Date: Thu, 15 Sep 2022 11:35:37 +0200 Subject: [PATCH] Change triangle creation --- .../internal/Marching_cubes_3_internal.h | 112 ++++++++++++------ .../Isosurfacing_3/internal/Tmc_internal.h | 75 +++++++++++- .../include/CGAL/Marching_cubes_3.h | 14 ++- .../include/CGAL/TC_marching_cubes_3.h | 11 ++ 4 files changed, 173 insertions(+), 39 deletions(-) diff --git a/Isosurfacing_3/include/CGAL/Isosurfacing_3/internal/Marching_cubes_3_internal.h b/Isosurfacing_3/include/CGAL/Isosurfacing_3/internal/Marching_cubes_3_internal.h index 54298659f00..50fe93bc789 100644 --- a/Isosurfacing_3/include/CGAL/Isosurfacing_3/internal/Marching_cubes_3_internal.h +++ b/Isosurfacing_3/include/CGAL/Isosurfacing_3/internal/Marching_cubes_3_internal.h @@ -1,12 +1,54 @@ +// Copyright (c) 2020 INRIA Sophia-Antipolis (France). +// All rights reserved. +// +// This file is part of CGAL (www.cgal.org). +// +// $URL$ +// $Id$ +// SPDX-License-Identifier: ( GPL-3.0-or-later OR LicenseRef-Commercial ) AND MIT +// +// Author(s) : Julian Stahl +// +// This file incorporates work covered by the following copyright and permission notice: +// +// MIT License +// +// Copyright (c) 2020 Roberto Grosso +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// +// +// The code below uses the version of +// https://github.com/rogrosso/tmc available on 15th of September 2022. +// + #ifndef CGAL_MARCHING_CUBES_3_INTERNAL_MARCHING_CUBES_3_H #define CGAL_MARCHING_CUBES_3_INTERNAL_MARCHING_CUBES_3_H #include +#include #include -#include -#include #include +#include +#include +#include namespace CGAL { namespace Isosurfacing { @@ -79,9 +121,8 @@ void mc_construct_vertices(const CellEdges& cell_edges, const FT iso_value, cons } } -template -void mc_construct_triangles(const int i_case, const Vertices_& vertices, PointRange& points, PolygonRange& polygons, - std::atomic_size_t& triangle_id) { +template +void mc_construct_triangles(const int i_case, const Vertices_& vertices, TriangleList& triangles) { // construct triangles for (int t = 0; t < 16; t += 3) { @@ -93,39 +134,45 @@ void mc_construct_triangles(const int i_case, const Vertices_& vertices, PointRa const int eg1 = Cube_table::triangle_cases[t_index + 1]; const int eg2 = Cube_table::triangle_cases[t_index + 2]; - const std::size_t t_id = triangle_id++; - - points.grow_to_at_least((t_id + 1) * 3); - points[t_id * 3 + 0] = vertices[eg0]; - points[t_id * 3 + 1] = vertices[eg1]; - points[t_id * 3 + 2] = vertices[eg2]; - // insert new triangle in list - typename PolygonRange::value_type triangle(3); - triangle[0] = t_id * 3 + 2; - triangle[1] = t_id * 3 + 1; - triangle[2] = t_id * 3 + 0; + std::array points; + points[0] = vertices[eg0]; + points[1] = vertices[eg1]; + points[2] = vertices[eg2]; - polygons.push_back(triangle); + triangles.push_back(points); } } -template +template +void to_indexed_face_set(const TriangleList& triangle_list, PointRange& points, PolygonRange& polygons) { + for (auto& triangle : triangle_list) { + const std::size_t id = points.size(); + + points.push_back(triangle[0]); + points.push_back(triangle[1]); + points.push_back(triangle[2]); + + polygons.push_back({}); + auto& triangle = polygons.back(); + triangle.push_back(id + 2); + triangle.push_back(id + 1); + triangle.push_back(id + 0); + } +} + +template class Marching_cubes_functor { private: typedef Domain_ Domain; - typedef PointRange Point_range; - typedef PolygonRange Polygon_range; - typedef typename Domain::FT FT; typedef typename Domain::Point Point; - typedef typename Domain::Vertex_handle Vertex_handle; - typedef typename Domain::Edge_handle Edge_handle; typedef typename Domain::Cell_handle Cell_handle; + typedef tbb::concurrent_vector> Triangle_list; + public: - Marching_cubes_functor(const Domain& domain, const FT iso_value, Point_range& points, Polygon_range& polygons) - : domain(domain), iso_value(iso_value), points(points), polygons(polygons) {} + Marching_cubes_functor(const Domain& domain, const FT iso_value) : domain(domain), iso_value(iso_value) {} void operator()(const Cell_handle& cell) { @@ -145,21 +192,18 @@ public: std::array vertices; mc_construct_vertices(domain.cell_edges(cell), iso_value, i_case, corners, values, vertices); - mc_construct_triangles(i_case, vertices, points, polygons, triangle_id); + mc_construct_triangles(i_case, vertices, triangle_list); + } + + const Triangle_list& get_triangles() const { + return triangle_list; } private: const Domain& domain; FT iso_value; - Point_range& points; - Polygon_range& polygons; - - // compute a unique global index for vertices - // use as key the unique edge number - std::map vertex_map; - - std::atomic_size_t triangle_id; + Triangle_list triangle_list; }; } // namespace internal diff --git a/Isosurfacing_3/include/CGAL/Isosurfacing_3/internal/Tmc_internal.h b/Isosurfacing_3/include/CGAL/Isosurfacing_3/internal/Tmc_internal.h index 742a991030c..89bac91a287 100644 --- a/Isosurfacing_3/include/CGAL/Isosurfacing_3/internal/Tmc_internal.h +++ b/Isosurfacing_3/include/CGAL/Isosurfacing_3/internal/Tmc_internal.h @@ -1,3 +1,43 @@ +// Copyright (c) 2020 INRIA Sophia-Antipolis (France). +// All rights reserved. +// +// This file is part of CGAL (www.cgal.org). +// +// $URL$ +// $Id$ +// SPDX-License-Identifier: ( GPL-3.0-or-later OR LicenseRef-Commercial ) AND MIT +// +// Author(s) : Julian Stahl +// +// This file incorporates work covered by the following copyright and permission notice: +// +// MIT License +// +// Copyright (c) 2020 Roberto Grosso +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// +// +// The code below uses the version of +// https://github.com/rogrosso/tmc available on 15th of September 2022. +// + #ifndef CGAL_TMC_INTERNAL_TMC_H #define CGAL_TMC_INTERNAL_TMC_H @@ -5,9 +45,9 @@ #include #include +#include #include #include -#include namespace CGAL { namespace Isosurfacing { @@ -23,7 +63,6 @@ private: typedef typename Domain::FT FT; typedef typename Domain::Point Point; typedef typename Domain::Vector Vector; - typedef typename Domain::Vertex_handle Vertex_handle; typedef typename Domain::Edge_handle Edge_handle; typedef typename Domain::Cell_handle Cell_handle; @@ -54,10 +93,38 @@ public: std::array vertices; mc_construct_vertices(domain.cell_edges(cell), iso_value, i_case, corners, values, vertices); - mc_construct_triangles(i_case, vertices, points, polygons, triangle_id++); + // TODO: improve triangle generation + // construct triangles + std::lock_guard lock(mutex); + for (int t = 0; t < 16; t += 3) { + + const int t_index = i_case * 16 + t; + // if (e_tris_list[t_index] == 0x7f) + if (Cube_table::triangle_cases[t_index] == -1) break; + + const int eg0 = Cube_table::triangle_cases[t_index + 0]; // TODO: move more of this stuff into the table + const int eg1 = Cube_table::triangle_cases[t_index + 1]; + const int eg2 = Cube_table::triangle_cases[t_index + 2]; + + const std::size_t p0_idx = points.size(); + + points.push_back(vertices[eg0]); + points.push_back(vertices[eg1]); + points.push_back(vertices[eg2]); + + // insert new triangle in list + polygons.push_back({}); + auto& triangle = polygons.back(); + + triangle.push_back(p0_idx + 2); + triangle.push_back(p0_idx + 1); + triangle.push_back(p0_idx + 0); + } } void add_triangle(const std::size_t p0, const std::size_t p1, const std::size_t p2) { + std::lock_guard lock(mutex); + polygons.push_back({}); auto& triangle = polygons.back(); @@ -893,7 +960,7 @@ private: // use as key the unique edge number std::map vertex_map; - std::atomic_size_t triangle_id; + std::mutex mutex; }; } // namespace internal diff --git a/Isosurfacing_3/include/CGAL/Marching_cubes_3.h b/Isosurfacing_3/include/CGAL/Marching_cubes_3.h index efbe5f0429e..36d945024ab 100644 --- a/Isosurfacing_3/include/CGAL/Marching_cubes_3.h +++ b/Isosurfacing_3/include/CGAL/Marching_cubes_3.h @@ -1,3 +1,14 @@ +// Copyright (c) 2022 INRIA Sophia-Antipolis (France). +// All rights reserved. +// +// This file is part of CGAL (www.cgal.org). +// +// $URL$ +// $Id$ +// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial +// +// Author(s) : Julian Stahl + #ifndef CGAL_MARCHING_CUBES_3_H #define CGAL_MARCHING_CUBES_3_H @@ -33,8 +44,9 @@ void make_triangle_mesh_using_marching_cubes(const Domain_& domain, const typena // static_assert(Domain_::CELL_TYPE & CUBICAL_CELL); - internal::Marching_cubes_functor functor(domain, iso_value, points, polygons); + internal::Marching_cubes_functor functor(domain, iso_value); domain.iterate_cells(functor, Concurrency_tag()); + internal::to_indexed_face_set(functor.get_triangles(), points, polygons); } } // namespace Isosurfacing diff --git a/Isosurfacing_3/include/CGAL/TC_marching_cubes_3.h b/Isosurfacing_3/include/CGAL/TC_marching_cubes_3.h index 3e3e80b14e6..940ecb0d25d 100644 --- a/Isosurfacing_3/include/CGAL/TC_marching_cubes_3.h +++ b/Isosurfacing_3/include/CGAL/TC_marching_cubes_3.h @@ -1,3 +1,14 @@ +// Copyright (c) 2022 INRIA Sophia-Antipolis (France). +// All rights reserved. +// +// This file is part of CGAL (www.cgal.org). +// +// $URL$ +// $Id$ +// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial +// +// Author(s) : Julian Stahl + #ifndef CGAL_TMC_3_H #define CGAL_TMC_3_H