Skip to content

Commit

Permalink
rework the Groups API
Browse files Browse the repository at this point in the history
  • Loading branch information
nmwsharp committed Nov 26, 2023
1 parent 783a333 commit c60e7d1
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 197 deletions.
21 changes: 19 additions & 2 deletions include/polyscope/group.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <memory>
#include <string>
#include <unordered_set>

#include "polyscope/structure.h"
#include "polyscope/weak_handle.h"
Expand All @@ -19,6 +20,7 @@ namespace polyscope {
class Group : public virtual WeakReferrable {

public:
// End-users should not call this constructure, use polyscope::createGroup()
Group(std::string name);
~Group();

Expand All @@ -27,8 +29,8 @@ class Group : public virtual WeakReferrable {
// buildUI() for all children.

// Is the group being displayed (0 no, 1 some children, 2 all children)
int isEnabled();
Group* setEnabled(bool newEnabled);
int isEnabled(); // checks ALL descendents
Group* setEnabled(bool newEnabled); // updates setting for ALL descendents

void addChildGroup(Group& newChild);
void addChildStructure(Structure& newChild);
Expand All @@ -38,8 +40,17 @@ class Group : public virtual WeakReferrable {

bool isRootGroup();
Group* getTopLevelGrandparent();
void appendStructuresToSkip(std::unordered_set<Structure*>& skipSet);
void appendAllDescendents(std::unordered_set<Structure*>& skipSet);

std::string niceName();
std::string uniqueName();

Group* setShowChildDetails(bool newVal);
bool getShowChildDetails();

Group* setHideDescendentsFromStructureLists(bool newVal);
bool getHideDescendentsFromStructureLists();

// === Member variables ===
WeakHandle<Group> parentGroup; // the parent group of this group (if null, this is a root group)
Expand All @@ -48,6 +59,12 @@ class Group : public virtual WeakReferrable {
std::vector<WeakHandle<Structure>> childrenStructures;

protected:
// = State

PersistentValue<bool> showChildDetails;
PersistentValue<bool> hideDescendentsFromStructureLists;

// helpers
void cullExpiredChildren(); // remove any child
};

Expand Down
17 changes: 7 additions & 10 deletions include/polyscope/polyscope.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,6 @@ Structure* getStructure(std::string type, std::string name = "");
// True if such a structure exists
bool hasStructure(std::string type, std::string name = "");

// Group management
bool registerGroup(std::string name);
bool setParentGroupOfGroup(std::string child, std::string parent);
bool setParentGroupOfStructure(std::string typeName, std::string child, std::string parent);
bool setParentGroupOfStructure(Structure* child, std::string parent);
// De-register a group
void setGroupEnabled(std::string name, bool enabled);
void removeGroup(std::string name, bool errorIfAbsent = true);
void removeAllGroups();

// De-register a structure, of any type. Also removes any quantities associated with the structure
void removeStructure(Structure* structure, bool errorIfAbsent = false);
void removeStructure(std::string type, std::string name, bool errorIfAbsent = false);
Expand All @@ -130,6 +120,13 @@ void removeAllStructures();
// Recompute the global state::lengthScale, boundingBox, and center by looping over registered structures
void updateStructureExtents();

// Group management
Group* createGroup(std::string name);
Group* getGroup(std::string name);
void removeGroup(Group* group, bool errorIfAbsent = true);
void removeGroup(std::string name, bool errorIfAbsent = true);
void removeAllGroups();

// Essentially regenerates all state and programs within Polyscope, calling refresh() recurisvely on all structures and
// quantities
void refresh();
Expand Down
5 changes: 5 additions & 0 deletions include/polyscope/structure.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

namespace polyscope {

// forward declarations
class Group;


// A 'structure' in Polyscope terms, is an object with which we can associate data in the UI, such as a point cloud,
// or a mesh. This in contrast to 'quantities', which we associate with the structures. For instance, a surface mesh
Expand Down Expand Up @@ -93,6 +96,8 @@ class Structure : public render::ManagedBufferRegistry, public virtual WeakRefer
bool isEnabled();
void enableIsolate(); // enable this structure, disable all of same type
void setEnabledAllOfType(bool newEnabled); // enable/disable all structures of this type
void addToGroup(std::string groupName);
void addToGroup(Group& group);


// Options
Expand Down
77 changes: 67 additions & 10 deletions src/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ bool CheckboxTristate(const char* label, int* v_tristate) {

namespace polyscope {

Group::Group(std::string name_) : name(name_) {}
Group::Group(std::string name_)
: name(name_), showChildDetails(uniqueName() + "showChildDetails", true),
hideDescendentsFromStructureLists(uniqueName() + "hideDescendentsFromStructureLists", false) {}

Group::~Group() {
// unparent all children
Expand All @@ -51,6 +53,7 @@ void Group::buildUI() {
ImGui::SetNextTreeNodeOpen(true, ImGuiCond_Once);
}


if (ImGui::TreeNode(niceName().c_str())) {

// Enabled checkbox
Expand All @@ -62,20 +65,38 @@ void Group::buildUI() {
if (CheckboxTristate("Enabled", &enabledLocal)) {
setEnabled(enabledLocal);
}

// Options popup
ImGui::SameLine();
if (ImGui::Button("Options")) {
ImGui::OpenPopup("OptionsPopup");
}
if (ImGui::BeginPopup("OptionsPopup")) {

if (ImGui::MenuItem("Show child details", NULL, getShowChildDetails())) {
setShowChildDetails(!getShowChildDetails());
}
if (ImGui::MenuItem("Hide descendents from structure lists", NULL, getHideDescendentsFromStructureLists())) {
setHideDescendentsFromStructureLists(!getHideDescendentsFromStructureLists());
}
ImGui::EndPopup();
}
}

// Call children buildUI
for (WeakHandle<Group>& childWeak : childrenGroups) {
if (childWeak.isValid()) {
Group& child = childWeak.get();
child.buildUI();
if (getShowChildDetails()) {
for (WeakHandle<Group>& childWeak : childrenGroups) {
if (childWeak.isValid()) {
Group& child = childWeak.get();
child.buildUI();
}
}
}

for (WeakHandle<Structure>& childWeak : childrenStructures) {
if (childWeak.isValid()) {
Structure& child = childWeak.get();
child.buildUI();
for (WeakHandle<Structure>& childWeak : childrenStructures) {
if (childWeak.isValid()) {
Structure& child = childWeak.get();
child.buildUI();
}
}
}

Expand Down Expand Up @@ -238,6 +259,28 @@ void Group::cullExpiredChildren() {
childrenStructures.end());
}

void Group::appendStructuresToSkip(std::unordered_set<Structure*>& skipSet) {
if (getHideDescendentsFromStructureLists()) {
appendAllDescendents(skipSet);
}
}

void Group::appendAllDescendents(std::unordered_set<Structure*>& skipSet) {
for (WeakHandle<Group>& childWeak : childrenGroups) {
if (childWeak.isValid()) {
Group& child = childWeak.get();
child.appendAllDescendents(skipSet);
}
}

for (WeakHandle<Structure>& childWeak : childrenStructures) {
if (childWeak.isValid()) {
Structure& child = childWeak.get();
skipSet.insert(&child);
}
}
}

Group* Group::setEnabled(bool newEnabled) {
for (WeakHandle<Group>& childWeak : childrenGroups) {
if (childWeak.isValid()) {
Expand All @@ -255,8 +298,22 @@ Group* Group::setEnabled(bool newEnabled) {
return this;
}

Group* Group::setShowChildDetails(bool newVal) {
showChildDetails = newVal;
return this;
}
bool Group::getShowChildDetails() { return showChildDetails.get(); }

Group* Group::setHideDescendentsFromStructureLists(bool newVal) {
hideDescendentsFromStructureLists = newVal;
return this;
}
bool Group::getHideDescendentsFromStructureLists() { return hideDescendentsFromStructureLists.get(); }

bool Group::isRootGroup() { return !parentGroup.isValid(); }

std::string Group::niceName() { return name; }

std::string Group::uniqueName() { return "Group#" + name + "#"; }

} // namespace polyscope
Loading

0 comments on commit c60e7d1

Please sign in to comment.