TriangleSelector: first partially working implementation

This commit is contained in:
Lukas Matena 2020-06-16 16:10:26 +02:00
parent 953d1417a0
commit d2b2446b07
2 changed files with 422 additions and 143 deletions

View file

@ -24,15 +24,17 @@ class ClippingPlane;
// to recursively subdivide the triangles and make the selection finer.
class TriangleSelector {
public:
void test();
explicit TriangleSelector(const TriangleMesh& mesh) {}
void render() const;
// Create new object on a TriangleMesh. The referenced mesh must
// stay valid, a ptr to it is saved and used.
explicit TriangleSelector(const TriangleMesh& mesh);
// Select all triangles inside the circle, subdivide where needed.
void select_patch(const Vec3f& hit, // point where to start
int facet_idx, // facet that point belongs to
int facet_start, // facet that point belongs to
const Vec3f& dir, // direction of the ray
float radius_sqr, // squared radius of the cursor
bool enforcer); // enforcer or blocker?
FacetSupportType new_state); // enforcer or blocker?
void unselect_all();
@ -43,11 +45,15 @@ public:
private:
// A struct to hold information about how a triangle was divided.
struct DivisionNode {
DivisionNode();
// Index of triangle this describes.
int triangle_idx;
bool valid{true};
// Index of parent triangle (-1: original)
int parent{-1};
// Bitmask encoding which sides are split.
unsigned char division_type;
int8_t division_type;
// bits 0 and 1 : 00 - no division
// 01 - one-edge split
// 10 - two-edge split
@ -55,29 +61,37 @@ private:
// bits 2 and 3 : decimal 0, 1 or 2 identifying the special edge (one that
// splits in one-edge split or one that stays in two-edge split).
// Pointers to children nodes (not all are always used).
std::array<DivisionNode*, 4> children;
// Children triangles (0 = no child)
std::array<int, 4> children;
// Set the division type.
void set_division(int sides_to_split, int special_side_idx = -1);
void set_state(FacetSupportType state);
// Helpers that decode the division_type bitmask.
int number_of_split_sides() const { return division_type & 0b11; }
int side_to_keep() const;
int side_to_split() const;
FacetSupportType get_state() const;
};
// Triangle and pointer to how it's divided (nullptr = not divided).
// The ptr is nullptr for all new triangles, it is only valid for
// the original (undivided) triangles.
struct Triangle {
Triangle(int a, int b, int c)
: verts_idxs{stl_triangle_vertex_indices(a, b, c)},
div_info{std::make_unique<DivisionNode>()}
{}
stl_triangle_vertex_indices verts_idxs;
DivisionNode* div_info;
std::unique_ptr<DivisionNode> div_info;
};
// Lists of vertices and triangles, both original and new
std::vector<stl_vertex> m_vertices;
std::vector<Triangle> m_triangles;
const TriangleMesh* m_mesh;
// Number of original vertices and triangles.
int m_orig_size_vertices;
@ -86,6 +100,32 @@ private:
// Limits for stopping the recursion.
float m_max_edge_length;
int m_max_recursion_depth;
// Caches for cursor position, radius and direction.
struct Cursor {
Vec3f center;
Vec3f dir;
float radius_sqr;
};
Cursor m_cursor;
// Private functions:
void select_triangle(int facet_idx, FacetSupportType type,
int num_of_inside_vertices = -1,
bool cursor_inside = false);
bool is_point_inside_cursor(const Vec3f& point) const;
int vertices_inside(int facet_idx) const;
bool faces_camera(int facet) const;
void undivide_triangle(int facet_idx);
bool split_triangle(int facet_idx);
void remove_needless(int child_facet);
};
@ -107,10 +147,7 @@ private:
// individual facets (one of the enum values above).
std::vector<std::vector<FacetSupportType>> m_selected_facets;
// Vertex buffer arrays for each model-part volume. There is a vector of
// arrays so that adding triangles can be done without regenerating all
// other triangles. Enforcers and blockers are of course separate.
std::vector<std::array<std::vector<GLIndexedVertexArray>, 2>> m_ivas;
GLIndexedVertexArray m_iva;
void update_vertex_buffers(const TriangleMesh* mesh,
int mesh_id,
@ -148,6 +185,8 @@ private:
bool m_setting_angle = false;
bool m_internal_stack_active = false;
bool m_schedule_update = false;
std::unique_ptr<TriangleSelector> m_triangle_selector;
// This map holds all translated description texts, so they can be easily referenced during layout calculations
// etc. When language changes, GUI is recreated and this class constructed again, so the change takes effect.