فهرست منبع

Get rid of class AttributeManager + update variable styles.

Former-commit-id: 4a33c9136b42f3fb259b4e894db04d3b8b0b2353
Jérémie Dumas 7 سال پیش
والد
کامیت
2a64c2eda4

+ 40 - 0
include/igl/Attribute.h

@@ -0,0 +1,40 @@
+// 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/.
+#ifndef IGL_ATTRIBUTE_H
+#define IGL_ATTRIBUTE_H
+
+#include <typeindex>
+
+namespace igl
+{
+
+struct AttributeBase
+{
+private:
+	std::type_index derived_type_;
+public:
+	AttributeBase(std::type_index t) : derived_type_(t) { }
+	virtual ~AttributeBase() = default;
+	std::type_index type() const { return derived_type_; }
+};
+
+// -----------------------------------------------------------------------------
+
+template<typename T>
+struct Attribute : public AttributeBase
+{
+	// Constructor
+	Attribute() : AttributeBase(typeid(T)) { }
+
+	// Data
+	T content_;
+};
+
+} // namespace igl
+
+#endif // IGL_ATTRIBUTE_H

+ 0 - 83
include/igl/Attributes.h

@@ -1,83 +0,0 @@
-// 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
-

+ 41 - 6
include/igl/opengl/ViewerData.h

@@ -8,12 +8,14 @@
 #ifndef IGL_VIEWERDATA_H
 #define IGL_VIEWERDATA_H
 
-#include <cstdint>
-#include <vector>
-#include <Eigen/Core>
 #include "../igl_inline.h"
-#include "../Attributes.h"
+#include "../Attribute.h"
 #include "MeshGL.h"
+#include <cassert>
+#include <cstdint>
+#include <Eigen/Core>
+#include <memory>
+#include <vector>
 
 // Alec: This is a mesh class containing a variety of data types (normals,
 // overlays, material colors, etc.)
@@ -25,7 +27,6 @@ namespace igl
 namespace opengl
 {
 
-
 class ViewerData
 {
 public:
@@ -183,10 +184,12 @@ public:
   bool show_vertid;
   bool show_faceid;
   bool invert_normals;
+
   // Point size / line width
   float point_size;
   float line_width;
   Eigen::Vector4f line_color;
+
   // Shape material
   float shininess;
 
@@ -194,7 +197,11 @@ public:
   igl::opengl::MeshGL meshgl;
 
   // User-defined attribute
-  AttributeManager attr;
+  std::shared_ptr<AttributeBase> attr_ptr;
+
+  // Retrieve custom attribute
+  template<typename T> T & attr();
+  template<typename T> const T & attr() const;
 
   // Update contents from a 'Data' instance
   IGL_INLINE void updateGL(
@@ -203,9 +210,37 @@ public:
     igl::opengl::MeshGL& meshgl);
 };
 
+// -----------------------------------------------------------------------------
+
+// Retrieve custom attribute
+template<typename T>
+inline T & ViewerData::attr()
+{
+  if (!attr_ptr)
+  {
+    attr_ptr = std::make_shared<Attribute<T>>();
+  }
+  auto * derived = dynamic_cast<Attribute<T> *>(attr_ptr.get());
+  assert(derived && "Incompatible type requested for attribute");
+  return derived->content_;
 }
+
+
+// Retrieve custom attribute
+template<typename T>
+inline const T & ViewerData::attr() const
+{
+  assert(attr_ptr);
+  const auto * derived = dynamic_cast<const Attribute<T> *>(attr_ptr.get());
+  assert(derived && "Incompatible type requested for attribute");
+  return derived->content_;
 }
 
+} // namespace opengl
+} // namespace igl
+
+////////////////////////////////////////////////////////////////////////////////
+
 #ifdef ENABLE_SERIALIZATION
 #include <igl/serialize.h>
 namespace igl

+ 11 - 11
include/igl/opengl/glfw/imgui/ImGuiMenu.cpp

@@ -30,9 +30,9 @@ IGL_INLINE void ImGuiMenu::init(igl::opengl::glfw::Viewer *_viewer)
   // Setup ImGui binding
   if (_viewer)
   {
-    if (m_Context == nullptr)
+    if (context_ == nullptr)
     {
-      m_Context = ImGui::CreateContext();
+      context_ = ImGui::CreateContext();
     }
     ImGui_ImplGlfwGL3_Init(viewer->window, false);
     ImGui::GetIO().IniFilename = nullptr;
@@ -45,21 +45,21 @@ IGL_INLINE void ImGuiMenu::init(igl::opengl::glfw::Viewer *_viewer)
 
 IGL_INLINE void ImGuiMenu::reload_font(int font_size)
 {
-  m_HidpiScaling = hidpi_scaling();
-  m_PixelRatio = pixel_ratio();
+  hidpi_scaling_ = hidpi_scaling();
+  pixel_ratio_ = pixel_ratio();
   ImGuiIO& io = ImGui::GetIO();
   io.Fonts->Clear();
   io.Fonts->AddFontFromMemoryCompressedTTF(droid_sans_compressed_data,
-    droid_sans_compressed_size, font_size * m_HidpiScaling);
-  io.FontGlobalScale = 1.0 / m_PixelRatio;
+    droid_sans_compressed_size, font_size * hidpi_scaling_);
+  io.FontGlobalScale = 1.0 / pixel_ratio_;
 }
 
 IGL_INLINE void ImGuiMenu::shutdown()
 {
   // Cleanup
   ImGui_ImplGlfwGL3_Shutdown();
-  ImGui::DestroyContext(m_Context);
-  m_Context = nullptr;
+  ImGui::DestroyContext(context_);
+  context_ = nullptr;
 }
 
 IGL_INLINE bool ImGuiMenu::pre_draw()
@@ -68,7 +68,7 @@ IGL_INLINE bool ImGuiMenu::pre_draw()
 
   // Check whether window dpi has changed
   float scaling = hidpi_scaling();
-  if (std::abs(scaling - m_HidpiScaling) > 1e-5)
+  if (std::abs(scaling - hidpi_scaling_) > 1e-5)
   {
     reload_font();
     ImGui_ImplGlfwGL3_InvalidateDeviceObjects();
@@ -87,7 +87,7 @@ IGL_INLINE bool ImGuiMenu::post_draw()
 
 IGL_INLINE void ImGuiMenu::post_resize(int width, int height)
 {
-  if (m_Context)
+  if (context_)
   {
     ImGui::GetIO().DisplaySize.x = float(width);
     ImGui::GetIO().DisplaySize.y = float(height);
@@ -345,7 +345,7 @@ IGL_INLINE void ImGuiMenu::draw_text(Eigen::Vector3d pos, Eigen::Vector3d normal
   // Draw text labels slightly bigger than normal text
   ImDrawList* drawList = ImGui::GetWindowDrawList();
   drawList->AddText(ImGui::GetFont(), ImGui::GetFontSize() * 1.2,
-      ImVec2(coord[0]/m_PixelRatio, (viewer->core.viewport[3] - coord[1])/m_PixelRatio),
+      ImVec2(coord[0]/pixel_ratio_, (viewer->core.viewport[3] - coord[1])/pixel_ratio_),
       ImGui::GetColorU32(ImVec4(0, 0, 10, 255)),
       &text[0], &text[0] + text.size());
 }

+ 4 - 4
include/igl/opengl/glfw/imgui/ImGuiMenu.h

@@ -30,14 +30,14 @@ class ImGuiMenu : public igl::opengl::glfw::ViewerPlugin
 {
 protected:
   // Hidpi scaling to be used for text rendering.
-  float m_HidpiScaling;
+  float hidpi_scaling_;
 
   // Ratio between the framebuffer size and the window size.
   // May be different from the hipdi scaling!
-  float m_PixelRatio;
+  float pixel_ratio_;
 
   // ImGui Context
-  ImGuiContext * m_Context = nullptr;
+  ImGuiContext * context_ = nullptr;
 
 public:
   IGL_INLINE virtual void init(igl::opengl::glfw::Viewer *_viewer) override;
@@ -85,7 +85,7 @@ public:
 
   IGL_INLINE float hidpi_scaling();
 
-  float menu_scaling() { return m_HidpiScaling / m_PixelRatio; }
+  float menu_scaling() { return hidpi_scaling_ / pixel_ratio_; }
 };
 
 } // end namespace

+ 4 - 4
tutorial/107_MultipleMeshes/main.cpp

@@ -19,7 +19,7 @@ void update_colors(igl::opengl::glfw::Viewer &viewer)
 {
   for (auto &data : viewer.data_list)
   {
-    data.set_colors(data.attr.get<MeshData>().color);
+    data.set_colors(data.attr<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;
@@ -31,7 +31,7 @@ class MyMenu : public igl::opengl::glfw::imgui::ImGuiMenu
   virtual void draw_viewer_menu() override
   {
     if (ImGui::Combo("Selected Mesh", (int *) &viewer->selected_data_index,
-          [&](int i) { return viewer->data_list[i].attr.get<MeshData>().name.c_str(); },
+          [&](int i) { return viewer->data_list[i].attr<MeshData>().name.c_str(); },
           viewer->data_list.size())
       || last_colored_index != viewer->selected_data_index)
     {
@@ -47,8 +47,8 @@ int main(int argc, char * argv[])
   for(const auto & name : names)
   {
     viewer.load_mesh_from_file(std::string(TUTORIAL_SHARED_PATH) + "/" + name);
-    viewer.data().attr.get<MeshData>().name = name;
-    viewer.data().attr.get<MeshData>().color = Eigen::RowVector3d::Random();
+    viewer.data().attr<MeshData>().name = name;
+    viewer.data().attr<MeshData>().color = Eigen::RowVector3d::Random();
   }
 
   // Attach a custom menu