Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions include/geode/basic/cached_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@

#pragma once

#include <atomic>
#include <type_traits>

#include <bitsery/brief_syntax.h>

#include <absl/synchronization/mutex.h>

#include <geode/basic/growable.hpp>

namespace geode
Expand All @@ -39,14 +42,44 @@ namespace geode
using CachedFunction =
typename std::add_pointer< ReturnType( Args... ) >::type;

CachedValue() = default;
CachedValue( const CachedValue& other )
{
value_ = other.value_;
computed_ = other.computed_.load();
}
CachedValue( CachedValue&& other ) noexcept
{
value_ = std::move( other.value_ );
computed_ = other.computed_.load();
}

CachedValue& operator=( const CachedValue& other )
{
value_ = other.value_;
computed_ = other.computed_.load();
return *this;
}

CachedValue& operator=( CachedValue&& other ) noexcept
{
value_ = std::move( other.value_ );
computed_ = other.computed_.load();
return *this;
}

template < typename... Args >
const ReturnType& operator()(
CachedFunction< Args... > computer, Args&&... args ) const
{
if( !computed_ )
{
value_ = computer( std::forward< Args >( args )... );
computed_ = true;
absl::MutexLock lock{ &mutex_ };
if( !computed_ )
{
value_ = computer( std::forward< Args >( args )... );
computed_ = true;
}
}
return value_;
}
Expand Down Expand Up @@ -83,7 +116,9 @@ namespace geode
archive.ext(
*this, Growable< Archive, CachedValue >{
{ []( Archive& a, CachedValue& value ) {
a.value1b( value.computed_ );
bool computed;
a.value1b( computed );
value.computed_ = computed;
a( value.value_ );
},
[]( Archive& /*a*/, CachedValue& /*value*/ ) {
Expand All @@ -92,7 +127,8 @@ namespace geode
}

private:
mutable bool computed_{ false };
mutable std::atomic< bool > computed_{ false };
mutable ReturnType value_;
mutable absl::Mutex mutex_;
};
} // namespace geode
18 changes: 18 additions & 0 deletions include/geode/mesh/helpers/detail/split_along_solid_facets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ namespace geode
{
namespace detail
{
struct SolidInfo
{
SolidInfo( index_t nb_vertices )
: polyhedron_vertices( nb_vertices ),
vertices_to_check( nb_vertices, false )
{
}
absl::FixedArray< PolyhedraAroundVertex > polyhedron_vertices;
std::vector< bool > vertices_to_check;
};

class opengeode_mesh_api SplitAlongSolidFacets
{
public:
Expand All @@ -55,6 +66,13 @@ namespace geode
MeshesElementsMapping split_solid_along_facets(
absl::Span< const PolyhedronFacet > facets_list );

SolidInfo remove_adjacencies_along_facets(
absl::Span< const PolyhedronFacet > facets_list );

MeshesElementsMapping
duplicate_points_and_process_solid_facets_and_edges(
const SolidInfo& solid_info );

private:
IMPLEMENTATION_MEMBER( impl_ );
};
Expand Down
24 changes: 6 additions & 18 deletions src/geode/mesh/core/solid_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,13 +571,11 @@ namespace geode
void enable_edges( const SolidMesh< dimension >& solid ) const
{
{
absl::ReaderMutexLock lock{ &mutex_ };
if( are_edges_enabled() )
{
return;
}
}
absl::MutexLock lock{ &mutex_ };
edges_.reset( new SolidEdges< dimension >{ solid } );
}

Expand Down Expand Up @@ -620,13 +618,11 @@ namespace geode
void enable_facets( const SolidMesh< dimension >& solid ) const
{
{
absl::ReaderMutexLock lock{ &mutex_ };
if( are_facets_enabled() )
{
return;
}
}
absl::MutexLock lock{ &mutex_ };
facets_.reset( new SolidFacets< dimension >{ solid } );
}

Expand Down Expand Up @@ -677,21 +673,14 @@ namespace geode
const std::optional< PolyhedronVertex >& first_polyhedron )
const
{
const auto& cached = polyhedra_around_vertex_->value( vertex_id );
if( cached.computed()
&& ( first_polyhedron
&& absl::c_contains( cached.value().polyhedra,
first_polyhedron.value() ) ) )
{
absl::ReaderMutexLock lock{ &mutex_ };
const auto& cached =
polyhedra_around_vertex_->value( vertex_id );
const auto& polyhedra = cached.value().polyhedra;
if( cached.computed()
&& ( first_polyhedron
&& absl::c_contains(
polyhedra, first_polyhedron.value() ) ) )
{
return cached.value();
}
return cached.value();
}
absl::MutexLock lock{ &mutex_ };
const auto& cached = polyhedra_around_vertex_->value( vertex_id );
cached( compute_polyhedra_around_vertex, mesh, vertex_id,
first_polyhedron );
return cached.value();
Expand Down Expand Up @@ -804,7 +793,6 @@ namespace geode
mutable std::unique_ptr< SolidEdges< dimension > > edges_;
mutable std::unique_ptr< SolidFacets< dimension > > facets_;
mutable TextureStorage3D texture_storage_;
mutable absl::Mutex mutex_;
};

template < index_t dimension >
Expand Down
22 changes: 6 additions & 16 deletions src/geode/mesh/core/surface_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,11 @@ namespace geode
void enable_edges( const SurfaceMesh< dimension >& surface ) const
{
{
absl::ReaderMutexLock lock{ &mutex_ };
if( are_edges_enabled() )
{
return;
}
}
absl::MutexLock lock{ &mutex_ };
edges_.reset( new SurfaceEdges< dimension >{ surface } );
}

Expand Down Expand Up @@ -503,21 +501,14 @@ namespace geode
const index_t vertex_id,
const std::optional< PolygonVertex >& first_polygon ) const
{
const auto& cached = polygons_around_vertex_->value( vertex_id );
if( cached.computed()
&& ( first_polygon
&& absl::c_contains(
cached.value().polygons, first_polygon.value() ) ) )
{
absl::ReaderMutexLock lock{ &mutex_ };
const auto& cached =
polygons_around_vertex_->value( vertex_id );
const auto& polygons = cached.value().polygons;
if( cached.computed()
&& ( first_polygon
&& absl::c_contains(
polygons, first_polygon.value() ) ) )
{
return cached.value();
}
return cached.value();
}
absl::MutexLock lock{ &mutex_ };
const auto& cached = polygons_around_vertex_->value( vertex_id );
cached( compute_polygons_around_vertex, mesh, vertex_id,
first_polygon );
return cached.value();
Expand All @@ -531,7 +522,6 @@ namespace geode
polygons_around_vertex_;
mutable std::unique_ptr< SurfaceEdges< dimension > > edges_;
mutable TextureStorage2D texture_storage_;
mutable absl::Mutex mutex_;
};

template < index_t dimension >
Expand Down
106 changes: 67 additions & 39 deletions src/geode/mesh/helpers/detail/split_along_solid_facets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,50 +41,12 @@ namespace geode
{
class SplitAlongSolidFacets::Impl
{
struct SolidInfo
{
SolidInfo( index_t nb_vertices )
: polyhedron_vertices( nb_vertices ),
vertices_to_check( nb_vertices, false )
{
}
absl::FixedArray< PolyhedraAroundVertex > polyhedron_vertices;
std::vector< bool > vertices_to_check;
};

public:
Impl( const SolidMesh3D& mesh, SolidMeshBuilder3D& builder )
: solid_( mesh ), builder_( builder )
{
}

MeshesElementsMapping split_solid_along_facets(
absl::Span< const PolyhedronFacet > facets_list )
{
bool facets_enabled = solid_.are_facets_enabled();
bool edges_enabled = solid_.are_edges_enabled();
const auto nb_initial_facets =
facets_enabled ? solid_.facets().nb_facets() : 0;
const auto nb_initial_edges =
edges_enabled ? solid_.edges().nb_edges() : 0;
const auto solid_info =
remove_adjacencies_along_facets( facets_list );
MeshesElementsMapping mapping;
mapping.vertices = duplicate_points( solid_info );
if( facets_enabled )
{
mapping.polygons = process_solid_facets(
nb_initial_facets, mapping.vertices );
}
if( edges_enabled )
{
mapping.edges = process_solid_edges(
nb_initial_edges, mapping.vertices );
}
return mapping;
}

private:
SolidInfo remove_adjacencies_along_facets(
absl::Span< const PolyhedronFacet > facets_list )
{
Expand Down Expand Up @@ -122,6 +84,58 @@ namespace geode
return info;
}

MeshesElementsMapping
duplicate_points_and_process_solid_facets_and_edges(
const SolidInfo& solid_info )
{
bool facets_enabled = solid_.are_facets_enabled();
bool edges_enabled = solid_.are_edges_enabled();
const auto nb_initial_facets =
facets_enabled ? solid_.facets().nb_facets() : 0;
const auto nb_initial_edges =
edges_enabled ? solid_.edges().nb_edges() : 0;
MeshesElementsMapping mapping;
mapping.vertices = duplicate_points( solid_info );
if( facets_enabled )
{
mapping.polygons = process_solid_facets(
nb_initial_facets, mapping.vertices );
}
if( edges_enabled )
{
mapping.edges = process_solid_edges(
nb_initial_edges, mapping.vertices );
}
return mapping;
}

MeshesElementsMapping split_solid_along_facets(
absl::Span< const PolyhedronFacet > facets_list )
{
const auto solid_info =
remove_adjacencies_along_facets( facets_list );
bool facets_enabled = solid_.are_facets_enabled();
bool edges_enabled = solid_.are_edges_enabled();
const auto nb_initial_facets =
facets_enabled ? solid_.facets().nb_facets() : 0;
const auto nb_initial_edges =
edges_enabled ? solid_.edges().nb_edges() : 0;
MeshesElementsMapping mapping;
mapping.vertices = duplicate_points( solid_info );
if( facets_enabled )
{
mapping.polygons = process_solid_facets(
nb_initial_facets, mapping.vertices );
}
if( edges_enabled )
{
mapping.edges = process_solid_edges(
nb_initial_edges, mapping.vertices );
}
return mapping;
}

private:
ElementsMapping duplicate_points( const SolidInfo& solid_info )
{
ElementsMapping vertices_mapping;
Expand Down Expand Up @@ -316,7 +330,21 @@ namespace geode
{
}

SplitAlongSolidFacets::~SplitAlongSolidFacets() {}
SplitAlongSolidFacets::~SplitAlongSolidFacets() = default;

SolidInfo SplitAlongSolidFacets::remove_adjacencies_along_facets(
absl::Span< const PolyhedronFacet > facets_list )
{
return impl_->remove_adjacencies_along_facets( facets_list );
}

MeshesElementsMapping SplitAlongSolidFacets::
duplicate_points_and_process_solid_facets_and_edges(
const SolidInfo& solid_info )
{
return impl_->duplicate_points_and_process_solid_facets_and_edges(
solid_info );
}

MeshesElementsMapping SplitAlongSolidFacets::split_solid_along_facets(
absl::Span< const PolyhedronFacet > facets_list )
Expand Down
Loading