Skip to content

Commit

Permalink
Fix QuadTree bug relating issure #32
Browse files Browse the repository at this point in the history
- The QuadTree extent was not properly set for several edge containers
when using domains larger than the defined constant `ContainerQuadTreeScale`
- Bug has been fixed
- New singleton class `TQMeshSetup` has been introduced, which handles
a proper setting of QuadTree-properties behind the scenes and which can
be used for the handling of further constant values
  • Loading branch information
FloSewn committed Feb 15, 2025
1 parent d187335 commit a4f1c8e
Show file tree
Hide file tree
Showing 18 changed files with 296 additions and 109 deletions.
19 changes: 5 additions & 14 deletions src/algorithm/Domain.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "STLHeaders.h"
#include "CppUtils.h"

#include "TQMeshSetup.h"
#include "Boundary.h"

namespace TQMesh {
Expand Down Expand Up @@ -201,13 +202,10 @@ class Domain
/*------------------------------------------------------------------
| Constructor
------------------------------------------------------------------*/
Domain( UserSizeFunction f = [](const Vec2d& p){return 1.0;},
double qtree_scale = ContainerQuadTreeScale,
size_t qtree_items = ContainerQuadTreeItems,
size_t qtree_depth = ContainerQuadTreeDepth )
Domain( UserSizeFunction f = [](const Vec2d& p){return 1.0;} )
: size_fun_ { f }
, verts_ { qtree_scale, qtree_items, qtree_depth }
{ }
, verts_ { ContainerFactory<Vertex>::build_container() }
{}

/*------------------------------------------------------------------
| Get the current number of all boundaries in the domain
Expand Down Expand Up @@ -268,14 +266,6 @@ class Domain
const EdgeList& fixed_edges() const { return fixed_edges_; }
EdgeList& fixed_edges() { return fixed_edges_; }

/*------------------------------------------------------------------
| Setter
------------------------------------------------------------------*/
void quad_tree_scale(double v) { verts_.quad_tree().scale(v); }
void quad_tree_max_item(size_t v) { verts_.quad_tree().max_item(v); }
void quad_tree_max_depth(size_t v) { verts_.quad_tree().max_depth(v); }
void quad_tree_center(const Vec2d& v) { verts_.quad_tree().center(v); }

/*------------------------------------------------------------------
| Evaluate the domain's size function at a given point
------------------------------------------------------------------*/
Expand Down Expand Up @@ -479,6 +469,7 @@ class Domain


private:

Vector boundaries_;

SizeFunction size_fun_;
Expand Down
11 changes: 7 additions & 4 deletions src/algorithm/EdgeList.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "STLHeaders.h"
#include "CppUtils.h"

#include "TQMeshSetup.h"
#include "Edge.h"

namespace TQMesh {
Expand All @@ -26,8 +27,8 @@ class EdgeList

using EdgeVector = std::vector<Edge*>;

using iterator = Container<Edge>::iterator;
using const_iterator = Container<Edge>::const_iterator;
using iterator = Edges::iterator;
using const_iterator = Edges::const_iterator;

iterator begin() { return edges_.begin(); }
iterator end() { return edges_.end(); }
Expand All @@ -38,7 +39,9 @@ class EdgeList
/*------------------------------------------------------------------
| Constructor
------------------------------------------------------------------*/
EdgeList( Orientation orient ) : orient_ { orient }
EdgeList( Orientation orient )
: orient_ { orient }
, edges_ { ContainerFactory<Edge>::build_container() }
{
ASSERT( ( orient != Orientation::CL ),
"Invalid edge list orientation.");
Expand Down Expand Up @@ -581,7 +584,7 @@ class EdgeList
| EdgeList attributes
------------------------------------------------------------------*/
Orientation orient_;
Container<Edge> edges_ {};
Edges edges_;
double area_ {0.0};


Expand Down
11 changes: 4 additions & 7 deletions src/algorithm/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,12 @@ class Mesh
| Constructor
------------------------------------------------------------------*/
Mesh(int mesh_id=DEFAULT_MESH_ID,
int element_color=DEFAULT_ELEMENT_COLOR,
double qtree_scale=ContainerQuadTreeScale,
size_t qtree_items=ContainerQuadTreeItems,
size_t qtree_depth=ContainerQuadTreeDepth)
int element_color=DEFAULT_ELEMENT_COLOR)
: mesh_id_ { ABS(mesh_id) }
, elem_color_ { ABS(element_color) }
, verts_ { qtree_scale, qtree_items, qtree_depth }
, quads_ { qtree_scale, qtree_items, qtree_depth }
, tris_ { qtree_scale, qtree_items, qtree_depth }
, verts_ { ContainerFactory<Vertex>::build_container() }
, quads_ { ContainerFactory<Quad>::build_container() }
, tris_ { ContainerFactory<Triangle>::build_container() }
{ }

/*------------------------------------------------------------------
Expand Down
9 changes: 2 additions & 7 deletions src/algorithm/MeshBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ class MeshBuilder
domain_extent *= domain_enlargement_;
}

return { mesh_id, element_color, domain_extent,
domain.vertices().quad_tree().max_items(),
domain.vertices().quad_tree().max_depth() };
return { mesh_id, element_color };

} // create_empty_mesh()

Expand All @@ -109,10 +107,7 @@ class MeshBuilder
int element_color=DEFAULT_ELEMENT_COLOR)
{
return std::move(
std::make_unique<Mesh>(mesh_id, element_color,
domain.vertices().quad_tree().scale(),
domain.vertices().quad_tree().max_items(),
domain.vertices().quad_tree().max_depth() )
std::make_unique<Mesh>( mesh_id, element_color )
);

} // create_empty_mesh_ptr()
Expand Down
1 change: 1 addition & 0 deletions src/algorithm/TQMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@

#include "MeshGenerator.h"

#include "TQMeshSetup.h"
111 changes: 111 additions & 0 deletions src/algorithm/TQMeshSetup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* This source file is part of the tqmesh library.
* This code was written by Florian Setzwein in 2022,
* and is covered under the MIT License
* Refer to the accompanying documentation for details
* on usage and license.
*/
#pragma once

#include <TQMeshConfig.h>
#include "STLHeaders.h"
#include "CppUtils.h"

namespace TQMesh {

/*********************************************************************
*
*********************************************************************/
class TQMeshSetup {
public:

/*------------------------------------------------------------------
| Enforce singleton
------------------------------------------------------------------*/
TQMeshSetup(const TQMeshSetup&) = delete;
TQMeshSetup& operator=(const TQMeshSetup&) = delete;

/*------------------------------------------------------------------
| Get singleton instance
------------------------------------------------------------------*/
static TQMeshSetup& get_instance()
{
static TQMeshSetup instance;
return instance;
}

/*------------------------------------------------------------------
| Setters
------------------------------------------------------------------*/
TQMeshSetup& set_quadtree_scale(double s)
{ quadtree_scale_ = s; return *this; }
TQMeshSetup& set_quadtree_max_items(size_t n)
{ quadtree_max_items_ = n; return *this; }
TQMeshSetup& set_quadtree_max_depth(size_t n)
{ quadtree_max_depth_ = n; return *this; }

/*------------------------------------------------------------------
| Getters
------------------------------------------------------------------*/
double get_quadtree_scale() const { return quadtree_scale_; }
size_t get_quadtree_max_items() const { return quadtree_max_items_; }
size_t get_quadtree_max_depth() const { return quadtree_max_depth_; }

private:

/*------------------------------------------------------------------
| Enforce singleton
------------------------------------------------------------------*/
TQMeshSetup() {}

/*------------------------------------------------------------------
|
------------------------------------------------------------------*/
double quadtree_scale_ { 10000.0 };
size_t quadtree_max_items_ { 100 };
size_t quadtree_max_depth_ { 25 };

}; // TQMeshSetup


/*********************************************************************
*
*********************************************************************/
template <typename T>
class ContainerFactory {
public:

/*------------------------------------------------------------------
| Enforce singleton
------------------------------------------------------------------*/
ContainerFactory(const ContainerFactory&) = delete;
ContainerFactory& operator=(const ContainerFactory&) = delete;

/*------------------------------------------------------------------
| Build a new container, after initializing QuadTree properties
------------------------------------------------------------------*/
static Container<T> build_container()
{
TQMeshSetup& setup = TQMeshSetup::get_instance();

QuadTreeBuilder<T, double>::get_instance()
.set_scale( setup.get_quadtree_scale() )
.set_max_items( setup.get_quadtree_max_items() )
.set_max_depth( setup.get_quadtree_max_depth() );

Container<T> container {};

return container;
}

private:

/*------------------------------------------------------------------
| Enforce singleton
------------------------------------------------------------------*/
ContainerFactory() {}

}; // ContainerFactory


} // namespace TQMesh
3 changes: 2 additions & 1 deletion src/app/TQMeshApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -785,13 +785,14 @@ class MeshConstruction
------------------------------------------------------------------*/
void init_mesh_domain(ParaReader& mesh_reader)
{
TQMeshSetup::get_instance().set_quadtree_scale( domain_extent_ );
// Initialize element size function
UserSizeFunction size_fun = init_size_function(
mesh_reader.get_value<std::string>("size_function")
);

// Initialize domain
domain_ = std::make_unique<Domain>(size_fun, domain_extent_ );
domain_ = std::make_unique<Domain>(size_fun );

print_parameter<std::string>(mesh_reader, "size_function");

Expand Down
9 changes: 7 additions & 2 deletions src/examples/01_simple_triangular_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ using namespace TQMesh;
bool simple_triangular_mesh()
{
/*------------------------------------------------------------------
| First, we define the size function. This function describes
|
------------------------------------------------------------------*/
TQMeshSetup::get_instance().set_quadtree_scale( 20.0 );

/*------------------------------------------------------------------
| Here, we define the size function. This function describes
| the size of the mesh elements with respect to their location
| in the domain. In this case, we use a constant size of 0.35
------------------------------------------------------------------*/
Expand All @@ -30,7 +35,7 @@ bool simple_triangular_mesh()
| Next, we need to define the domain. It requires the size function
| as argument.
------------------------------------------------------------------*/
Domain domain { f, 20.0 };
Domain domain { f };

/*------------------------------------------------------------------
| Now we define the exteriour boundary of the domain. To do this,
Expand Down
Loading

0 comments on commit a4f1c8e

Please sign in to comment.