<commit_before>// ======================================================================== //
// Copyright 2016 SURVICE Engineering Company                               //
// Copyright 2016-2018 Intel Corporation                                    //
//                                                                          //
// Licensed under the Apache License, Version 2.0 (the "License");          //
// you may not use this file except in compliance with the License.         //
// You may obtain a copy of the License at                                  //
//                                                                          //
//     http://www.apache.org/licenses/LICENSE-2.0                           //
//                                                                          //
// Unless required by applicable law or agreed to in writing, software      //
// distributed under the License is distributed on an "AS IS" BASIS,        //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and      //
// limitations under the License.                                           //
// ======================================================================== //

// ospcommon
#include "ospcommon/utility/getEnvVar.h"
#include "ospcommon/utility/SaveImage.h"
#include "ospcommon/utility/StringManip.h"

#include "imguiViewer.h"

#include "common/sg/common/FrameBuffer.h"
#include "common/sg/visitor/GatherNodesByName.h"
#include "common/sg/visitor/GatherNodesByPosition.h"
#include "transferFunction.h"

#include <imgui.h>
#include <imguifilesystem/imguifilesystem.h>

#include <unordered_map>

using std::string;
using namespace ospcommon;

namespace ospray {

  // Functions to make ImGui widgets for SG nodes /////////////////////////////

  static void sgWidget_box3f(const std::string &text,
                             std::shared_ptr<sg::Node> node)
  {
    box3f val = node->valueAs<box3f>();
    ImGui::NewLine();
    ImGui::Text("lower: (%f, %f, %f)", val.lower.x, val.lower.y, val.lower.z);
    ImGui::Text("upper: (%f, %f, %f)", val.upper.x, val.upper.y, val.upper.z);
  }

  static void sgWidget_vec3f(const std::string &text,
                             std::shared_ptr<sg::Node> node)
  {
    vec3f val = node->valueAs<vec3f>();
    if ((node->flags() & sg::NodeFlags::gui_color)) {
      if (ImGui::ColorEdit3(text.c_str(), (float*)&val.x))
        node->setValue(val);
    }
    else if ((node->flags() & sg::NodeFlags::gui_slider)) {
      if (ImGui::SliderFloat3(text.c_str(), &val.x,
                              node->min().get<vec3f>().x,
                              node->max().get<vec3f>().x))
        node->setValue(val);
    }
    else if (ImGui::DragFloat3(text.c_str(), (float*)&val.x, .01f)) {
      node->setValue(val);
    }
  }

  static void sgWidget_vec2f(const std::string &text,
                             std::shared_ptr<sg::Node> node)
  {
    vec2f val = node->valueAs<vec2f>();
    if (ImGui::DragFloat2(text.c_str(), (float*)&val.x, .01f)) {
      node->setValue(val);
    }
  }

  static void sgWidget_vec2i(const std::string &text,
                             std::shared_ptr<sg::Node> node)
  {
    vec2i val = node->valueAs<vec2i>();
    if (ImGui::DragInt2(text.c_str(), (int*)&val.x)) {
      node->setValue(val);
    }
  }

  static void sgWidget_float(const std::string &text,
                             std::shared_ptr<sg::Node> node)
  {
    float val = node->valueAs<float>();
    if ((node->flags() & sg::NodeFlags::gui_slider)) {
      if (ImGui::SliderFloat(text.c_str(), &val,
                             node->min().get<float>(),
                             node->max().get<float>()))
        node->setValue(val);
    } else if (ImGui::DragFloat(text.c_str(), &val, .01f)) {
      node->setValue(val);
    }
  }

  static void sgWidget_bool(const std::string &text,
                            std::shared_ptr<sg::Node> node)
  {
    bool val = node->valueAs<bool>();
    if (ImGui::Checkbox(text.c_str(), &val)) {
      node->setValue(val);
    }
  }

  static void sgWidget_int(const std::string &text,
                           std::shared_ptr<sg::Node> node)
  {
    int val = node->valueAs<int>();
    if ((node->flags() & sg::NodeFlags::gui_slider)) {
      if (ImGui::SliderInt(text.c_str(), &val,
                           node->min().get<int>(),
                           node->max().get<int>()))
        node->setValue(val);
    }
    else if (ImGui::DragInt(text.c_str(), &val)) {
      node->setValue(val);
    }
  }

  static void sgWidget_string(const std::string &text,
                              std::shared_ptr<sg::Node> node)
  {
    std::string value = node->valueAs<std::string>().c_str();
    std::vector<char> buf(value.size() + 1 + 256);
    strcpy(buf.data(), value.c_str());
    buf[value.size()] = '\0';
    if (ImGui::InputText(text.c_str(), buf.data(),
                         value.size()+256,
                         ImGuiInputTextFlags_EnterReturnsTrue)) {
      node->setValue(std::string(buf.data()));
    }
  }

  static void sgWidget_TransferFunction(
    const std::string &text,
    std::shared_ptr<sg::Node> node)
  {
    if (!node->hasChild("transferFunctionWidget")) {
      std::shared_ptr<sg::TransferFunction> tfn =
        std::dynamic_pointer_cast<sg::TransferFunction>(node);

      node->createChildWithValue("transferFunctionWidget","Node",
                                 TransferFunction(tfn));
    }

    auto &tfnWidget =
      node->child("transferFunctionWidget").valueAs<TransferFunction>();

    static bool show_editor = true;
    ImGui::Checkbox("show_editor", &show_editor);

    if (show_editor) {
      tfnWidget.render();
      tfnWidget.drawUi();
    }
  }

  using ParameterWidgetBuilder =
      void(*)(const std::string &, std::shared_ptr<sg::Node>);

  static std::unordered_map<std::string, ParameterWidgetBuilder>
      widgetBuilders =
      {
        {"float", sgWidget_float},
        {"int", sgWidget_int},
        {"vec2i", sgWidget_vec2i},
        {"vec2f", sgWidget_vec2i},
        {"vec3f", sgWidget_vec3f},
        {"box3f", sgWidget_box3f},
        {"string", sgWidget_string},
        {"bool", sgWidget_bool},
        {"TransferFunction", sgWidget_TransferFunction}
      };


// ImGuiViewer definitions ////////////////////////////////////////////////////

  ImGuiViewer::ImGuiViewer(const std::shared_ptr<sg::Node> &scenegraph)
    : ImGuiViewer(scenegraph->nodeAs<sg::Renderer>(), nullptr)
  {}

  ImGuiViewer::ImGuiViewer(const std::shared_ptr<sg::Renderer> &scenegraph,
                           const std::shared_ptr<sg::Renderer> &scenegraphDW)
    : ImGui3DWidget(ImGui3DWidget::FRAMEBUFFER_NONE),
      scenegraph(scenegraph),
      scenegraphDW(scenegraphDW),<commit_msg>fix typo in sg node value ImGui widgets<commit_after>// ======================================================================== //
// Copyright 2016 SURVICE Engineering Company                               //
// Copyright 2016-2018 Intel Corporation                                    //
//                                                                          //
// Licensed under the Apache License, Version 2.0 (the "License");          //
// you may not use this file except in compliance with the License.         //
// You may obtain a copy of the License at                                  //
//                                                                          //
//     http://www.apache.org/licenses/LICENSE-2.0                           //
//                                                                          //
// Unless required by applicable law or agreed to in writing, software      //
// distributed under the License is distributed on an "AS IS" BASIS,        //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and      //
// limitations under the License.                                           //
// ======================================================================== //

// ospcommon
#include "ospcommon/utility/getEnvVar.h"
#include "ospcommon/utility/SaveImage.h"
#include "ospcommon/utility/StringManip.h"

#include "imguiViewer.h"

#include "common/sg/common/FrameBuffer.h"
#include "common/sg/visitor/GatherNodesByName.h"
#include "common/sg/visitor/GatherNodesByPosition.h"
#include "transferFunction.h"

#include <imgui.h>
#include <imguifilesystem/imguifilesystem.h>

#include <unordered_map>

using std::string;
using namespace ospcommon;

namespace ospray {

  // Functions to make ImGui widgets for SG nodes /////////////////////////////

  static void sgWidget_box3f(const std::string &text,
                             std::shared_ptr<sg::Node> node)
  {
    box3f val = node->valueAs<box3f>();
    ImGui::NewLine();
    ImGui::Text("lower: (%f, %f, %f)", val.lower.x, val.lower.y, val.lower.z);
    ImGui::Text("upper: (%f, %f, %f)", val.upper.x, val.upper.y, val.upper.z);
  }

  static void sgWidget_vec3f(const std::string &text,
                             std::shared_ptr<sg::Node> node)
  {
    vec3f val = node->valueAs<vec3f>();
    if ((node->flags() & sg::NodeFlags::gui_color)) {
      if (ImGui::ColorEdit3(text.c_str(), (float*)&val.x))
        node->setValue(val);
    }
    else if ((node->flags() & sg::NodeFlags::gui_slider)) {
      if (ImGui::SliderFloat3(text.c_str(), &val.x,
                              node->min().get<vec3f>().x,
                              node->max().get<vec3f>().x))
        node->setValue(val);
    }
    else if (ImGui::DragFloat3(text.c_str(), (float*)&val.x, .01f)) {
      node->setValue(val);
    }
  }

  static void sgWidget_vec2f(const std::string &text,
                             std::shared_ptr<sg::Node> node)
  {
    vec2f val = node->valueAs<vec2f>();
    if (ImGui::DragFloat2(text.c_str(), (float*)&val.x, .01f)) {
      node->setValue(val);
    }
  }

  static void sgWidget_vec2i(const std::string &text,
                             std::shared_ptr<sg::Node> node)
  {
    vec2i val = node->valueAs<vec2i>();
    if (ImGui::DragInt2(text.c_str(), (int*)&val.x)) {
      node->setValue(val);
    }
  }

  static void sgWidget_float(const std::string &text,
                             std::shared_ptr<sg::Node> node)
  {
    float val = node->valueAs<float>();
    if ((node->flags() & sg::NodeFlags::gui_slider)) {
      if (ImGui::SliderFloat(text.c_str(), &val,
                             node->min().get<float>(),
                             node->max().get<float>()))
        node->setValue(val);
    } else if (ImGui::DragFloat(text.c_str(), &val, .01f)) {
      node->setValue(val);
    }
  }

  static void sgWidget_bool(const std::string &text,
                            std::shared_ptr<sg::Node> node)
  {
    bool val = node->valueAs<bool>();
    if (ImGui::Checkbox(text.c_str(), &val)) {
      node->setValue(val);
    }
  }

  static void sgWidget_int(const std::string &text,
                           std::shared_ptr<sg::Node> node)
  {
    int val = node->valueAs<int>();
    if ((node->flags() & sg::NodeFlags::gui_slider)) {
      if (ImGui::SliderInt(text.c_str(), &val,
                           node->min().get<int>(),
                           node->max().get<int>()))
        node->setValue(val);
    }
    else if (ImGui::DragInt(text.c_str(), &val)) {
      node->setValue(val);
    }
  }

  static void sgWidget_string(const std::string &text,
                              std::shared_ptr<sg::Node> node)
  {
    std::string value = node->valueAs<std::string>().c_str();
    std::vector<char> buf(value.size() + 1 + 256);
    strcpy(buf.data(), value.c_str());
    buf[value.size()] = '\0';
    if (ImGui::InputText(text.c_str(), buf.data(),
                         value.size()+256,
                         ImGuiInputTextFlags_EnterReturnsTrue)) {
      node->setValue(std::string(buf.data()));
    }
  }

  static void sgWidget_TransferFunction(
    const std::string &text,
    std::shared_ptr<sg::Node> node)
  {
    if (!node->hasChild("transferFunctionWidget")) {
      std::shared_ptr<sg::TransferFunction> tfn =
        std::dynamic_pointer_cast<sg::TransferFunction>(node);

      node->createChildWithValue("transferFunctionWidget","Node",
                                 TransferFunction(tfn));
    }

    auto &tfnWidget =
      node->child("transferFunctionWidget").valueAs<TransferFunction>();

    static bool show_editor = true;
    ImGui::Checkbox("show_editor", &show_editor);

    if (show_editor) {
      tfnWidget.render();
      tfnWidget.drawUi();
    }
  }

  using ParameterWidgetBuilder =
      void(*)(const std::string &, std::shared_ptr<sg::Node>);

  static std::unordered_map<std::string, ParameterWidgetBuilder>
      widgetBuilders =
      {
        {"float", sgWidget_float},
        {"int", sgWidget_int},
        {"vec2i", sgWidget_vec2i},
        {"vec2f", sgWidget_vec2f},
        {"vec3f", sgWidget_vec3f},
        {"box3f", sgWidget_box3f},
        {"string", sgWidget_string},
        {"bool", sgWidget_bool},
        {"TransferFunction", sgWidget_TransferFunction}
      };


// ImGuiViewer definitions ////////////////////////////////////////////////////

  ImGuiViewer::ImGuiViewer(const std::shared_ptr<sg::Node> &scenegraph)
    : ImGuiViewer(scenegraph->nodeAs<sg::Renderer>(), nullptr)
  {}

  ImGuiViewer::ImGuiViewer(const std::shared_ptr<sg::Renderer> &scenegraph,
                           const std::shared_ptr<sg::Renderer> &scenegraphDW)
    : ImGui3DWidget(ImGui3DWidget::FRAMEBUFFER_NONE),
      scenegraph(scenegraph),
      scenegraphDW(scenegraphDW),
{'fraction_non_alphanumeric': 0.17950211093317806, 'fraction_numerical': 0.008589314310671132, 'mean_word_length': 2.104157252598283, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 141, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2754571', 'n_tokens_mistral': 4199, 'n_tokens_neox': 4082, 'n_words': 748}