Эх сурвалжийг харах

Add a small attribute class to extend the viewer data.

Former-commit-id: d7f3cf3be3f3d5650e1bd436015dda670fa947a3
Jérémie Dumas 7 жил өмнө
parent
commit
2499938d82

+ 83 - 0
include/igl/Attributes.h

@@ -0,0 +1,83 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+//
+// Copyright (C) 2018 Jérémie Dumas <jeremie.dumas@ens-lyon.org>
+//
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
+// obtain one at http://mozilla.org/MPL/2.0/.
+#include <cassert>
+#include <map>
+#include <memory>
+#include <typeindex>
+#include <vector>
+
+namespace igl
+{
+
+////////////////////////////////////////////////////////////////////////////////
+
+struct AttributeBase {
+private:
+	std::type_index m_DerivedType;
+public:
+	AttributeBase(std::type_index t) : m_DerivedType(t) { }
+	virtual ~AttributeBase() = default;
+	std::type_index type() const { return m_DerivedType; }
+};
+
+// -----------------------------------------------------------------------------
+
+template<typename T>
+struct Attribute : public AttributeBase {
+	// Constructor
+	Attribute() : AttributeBase(typeid(T)) { }
+
+	// Data
+	T content_;
+};
+
+////////////////////////////////////////////////////////////////////////////////
+
+// A generic class to manage attributes
+class AttributeManager {
+
+private:
+	std::shared_ptr<AttributeBase> attr_;
+
+public:
+	// Retrieve custom attribute
+	template<typename T> T & get();
+	template<typename T> const T & get() const;
+
+	// Retrieve attribute type
+	std::type_index type() const { return attr_->type(); }
+};
+
+// -----------------------------------------------------------------------------
+
+// Retrieve custom attribute
+template<typename T>
+inline T & AttributeManager::get()
+{
+	if (!attr_)
+	{
+		attr_ = std::make_shared<Attribute<T>>();
+	}
+	auto * derived = dynamic_cast<Attribute<T> *>(attr_.get());
+	assert(derived && "Incompatible type requested for attribute");
+	return derived->content_;
+}
+
+
+// Retrieve custom attribute
+template<typename T>
+inline const T & AttributeManager::get() const
+{
+	assert(attr_);
+	const auto * derived = dynamic_cast<const Attribute<T> *>(attr_.get());
+	assert(derived && "Incompatible type requested for attribute");
+	return derived->content_;
+}
+
+} // namespace igl
+

+ 7 - 3
include/igl/opengl/ViewerData.h

@@ -12,6 +12,7 @@
 #include <vector>
 #include <Eigen/Core>
 #include "../igl_inline.h"
+#include "../Attributes.h"
 #include "MeshGL.h"
 
 // Alec: This is a mesh class containing a variety of data types (normals,
@@ -32,7 +33,7 @@ public:
 
   // Empty all fields
   IGL_INLINE void clear();
-  
+
   // Change the visualization mode, invalidating the cache if necessary
   IGL_INLINE void set_face_based(bool newvalue);
 
@@ -192,9 +193,12 @@ public:
   // OpenGL representation of the mesh
   igl::opengl::MeshGL meshgl;
 
+  // User-defined attribute
+  AttributeManager attr;
+
   // Update contents from a 'Data' instance
   IGL_INLINE void updateGL(
-    const igl::opengl::ViewerData& data, 
+    const igl::opengl::ViewerData& data,
     const bool invert_normals,
     igl::opengl::MeshGL& meshgl);
 };
@@ -204,7 +208,7 @@ public:
 
 #ifdef ENABLE_SERIALIZATION
 #include <igl/serialize.h>
-namespace igl 
+namespace igl
 {
   namespace serialization
   {

+ 12 - 0
include/igl/opengl/glfw/imgui/ImGuiHelpers.h

@@ -13,6 +13,7 @@
 #include <vector>
 #include <string>
 #include <algorithm>
+#include <functional>
 ////////////////////////////////////////////////////////////////////////////////
 
 // Extend ImGui by populating its namespace directly
@@ -37,6 +38,17 @@ inline bool Combo(const char* label, int* idx, std::vector<std::string>& values)
 		static_cast<void*>(&values), values.size());
 }
 
+inline bool Combo(const char* label, int* idx, std::function<const char *(int)> getter, int items_count)
+{
+	auto func = [](void* data, int i, const char** out_text) {
+		auto &getter = *reinterpret_cast<std::function<const char *(int)> *>(data);
+		const char *s = getter(i);
+		if (s) { *out_text = s; return true; }
+		else { return false; }
+	};
+	return Combo(label, idx, func, reinterpret_cast<void *>(&getter), items_count);
+}
+
 inline bool ListBox(const char* label, int* idx, std::vector<std::string>& values)
 {
 	if (values.empty()) { return false; }

+ 17 - 16
tutorial/107_MultipleMeshes/main.cpp

@@ -6,16 +6,20 @@
 #include <map>
 #include <iostream>
 
-std::vector<std::string> names = {"cube.obj","sphere.obj","xcylinder.obj","ycylinder.obj","zcylinder.obj"};
-std::map<std::string, Eigen::RowVector3d> colors;
+// Add custom data to a viewer mesh
+struct MeshData
+{
+  std::string name;
+  Eigen::RowVector3d color;
+};
 int last_colored_index = -1;
 
 // Set colors of each mesh
 void update_colors(igl::opengl::glfw::Viewer &viewer)
 {
-  for (int i = 0; i < (int) viewer.data_list.size(); ++i)
+  for (auto &data : viewer.data_list)
   {
-    viewer.data_list[i].set_colors(colors[names[i]]);
+    data.set_colors(data.attr.get<MeshData>().color);
   }
   viewer.data_list[viewer.selected_data_index].set_colors(Eigen::RowVector3d(0.9,0.1,0.1));
   last_colored_index = viewer.selected_data_index;
@@ -26,11 +30,10 @@ class MyMenu : public igl::opengl::glfw::imgui::ImGuiMenu
 {
   virtual void draw_viewer_menu() override
   {
-    if (ImGui::Combo("Selected Mesh", (int *) &viewer->selected_data_index, names))
-    {
-      update_colors(*viewer);
-    }
-    if (last_colored_index != viewer->selected_data_index)
+    if (ImGui::Combo("Selected Mesh", (int *) &viewer->selected_data_index,
+          [&](int i) { return viewer->data_list[i].attr.get<MeshData>().name.c_str(); },
+          viewer->data_list.size())
+      || last_colored_index != viewer->selected_data_index)
     {
       update_colors(*viewer);
     }
@@ -40,10 +43,12 @@ class MyMenu : public igl::opengl::glfw::imgui::ImGuiMenu
 int main(int argc, char * argv[])
 {
   igl::opengl::glfw::Viewer viewer;
+  auto names = {"cube.obj","sphere.obj","xcylinder.obj","ycylinder.obj","zcylinder.obj"};
   for(const auto & name : names)
   {
     viewer.load_mesh_from_file(std::string(TUTORIAL_SHARED_PATH) + "/" + name);
-    colors[name] = Eigen::RowVector3d::Random();
+    viewer.data().attr.get<MeshData>().name = name;
+    viewer.data().attr.get<MeshData>().color = Eigen::RowVector3d::Random();
   }
 
   // Attach a custom menu
@@ -59,12 +64,8 @@ int main(int argc, char * argv[])
     // Delete
     if(key == '3')
     {
-      int old_index = viewer.selected_data_index;
-      if (viewer.erase_mesh(viewer.selected_data_index))
-      {
-        names.erase(names.begin() + old_index);
-        update_colors(viewer);
-      }
+      viewer.erase_mesh(viewer.selected_data_index);
+      update_colors(viewer);
       return true;
     }
     return false;