<commit_before>/*
   For more information, please see: http://software.sci.utah.edu

   The MIT License

   Copyright (c) 2012 Scientific Computing and Imaging Institute,
   University of Utah.

   License for the specific language governing rights and limitations under
   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   DEALINGS IN THE SOFTWARE.
*/

#include <Modules/Visualization/ShowField.h>
#include <Core/Datatypes/Geometry.h>
#include <Core/Datatypes/Legacy/Field/VMesh.h>
#include <Core/Datatypes/Legacy/Field/Field.h>
#include <Core/Datatypes/Legacy/Field/VField.h>
#include <Core/Datatypes/Mesh/MeshFacade.h>
#include <Core/Datatypes/Color.h>

#include <boost/foreach.hpp>

using namespace SCIRun::Modules::Visualization;
using namespace SCIRun::Core::Datatypes;
using namespace SCIRun::Dataflow::Networks;
using namespace SCIRun::Core::Algorithms;


ShowFieldModule::ShowFieldModule() : 
    Module(ModuleLookupInfo("ShowField", "Visualization", "SCIRun"))
{
}


void ShowFieldModule::execute()
{
  boost::shared_ptr<SCIRun::Field> field = getRequiredInput(Field);
  GeometryHandle geom = buildGeometryObject(field, get_state(), get_id());
  sendOutput(SceneGraph, geom);
}


GeometryHandle ShowFieldModule::buildGeometryObject(
    boost::shared_ptr<SCIRun::Field> field,
    ModuleStateHandle state, 
    const std::string& id)
{
  // Function for reporting progress.
  SCIRun::Core::Algorithms::AlgorithmStatusReporter::UpdaterFunc progressFunc =
      getUpdaterFunc();

  // VMesh facade. A simpler interface to the vmesh type.
  SCIRun::Core::Datatypes::MeshTraits<VMesh>::MeshFacadeHandle facade =
      field->mesh()->getFacade();
  ENSURE_NOT_NULL(facade, "Mesh facade");

  // VField required only for extracting field data from the field datatype.
  VField* vfield = field->vfield();

  // Grab the vmesh object so that we can synchronize and extract normals.
  VMesh* vmesh = field->vmesh();

  // Ensure any changes made to the mesh are reflected in the normals by
  // by synchronizing the mesh.
  if (vmesh->has_normals())
    vmesh->synchronize(Mesh::NORMALS_E);
  
  /// \todo Determine a better way of handling all of the various object state.
  bool showNodes = state->getValue(ShowFieldModule::ShowNodes).getBool();
  bool showEdges = state->getValue(ShowFieldModule::ShowEdges).getBool();
  bool showFaces = state->getValue(ShowFieldModule::ShowFaces).getBool();
  bool nodeTransparency = state->getValue(ShowFieldModule::NodeTransparency).getBool();

  // Resultant geometry type (representing a spire object and a number of passes).
  GeometryHandle geom(new GeometryObject(field));
  geom->objectName = id;

  /// \todo Split the mesh into chunks of about ~32,000 vertices. May be able to
  ///       eek out better coherency and use a 16 bit index buffer instead of
  ///       a 32 bit index buffer.

  // Crude method of counting the attributes we are placing in the VBO.
  int numFloats = 3 + 1;  // Position + field data.
  if (vmesh->has_normals())
    numFloats += 3;       // Position + field data + normals;

  // Allocate memory for vertex buffer.
  // Edges and faces should use the same vbo!
  std::shared_ptr<std::vector<uint8_t>> rawVBO(new std::vector<uint8_t>());
  size_t vboSize = sizeof(float) * numFloats * facade->numNodes();
  rawVBO->resize(vboSize); // linear complexity.
  // The C++ standard guarantees that vectors are contiguous in memory, so we
  // grab a pointer to the first element and use that as the starting point
  // for building our VBO.
  float* vbo = reinterpret_cast<float*>(&(*rawVBO)[0]);

  // Add shared VBO to the geometry object.
  /// \note This 'primaryVBO' is dependent on the types present in the data.
  ///       If normals are specified, then this will NOT be the primary VBO.
  ///       Another VBO will be constructed containing normal information.
  ///       All of this is NOT necessary if we are on OpenGL 3.2+ where we
  ///       can compute all normals in the geometry shader (smooth and face).
  std::string primVBOName = id + "primaryVBO";
  std::vector<std::string> attribs;   ///< \todo Switch to initializer lists when msvc supports it.
  attribs.push_back("aPos");          ///< \todo Find a good place to pull these names from.
  attribs.push_back("aFieldData");
  if (vmesh->has_normals())
    attribs.push_back("aNormal");
  geom->mVBOs.emplace_back(GeometryObject::SpireVBO(primVBOName, attribs, rawVBO));

  if (progressFunc) progressFunc(0.1);

  // Build vertex buffer.
  size_t i = 0;
  BOOST_FOREACH(const NodeInfo<VMesh>& node, facade->nodes())
  {
    // Add position (aPos)
    size_t nodeOffset = 0;
    vbo[i+nodeOffset+0] = node.point().x();
    vbo[i+nodeOffset+1] = node.point().y();
    vbo[i+nodeOffset+2] = node.point().z();
    nodeOffset += 3;

    // Add field data (aFieldData)
    if (node.index() < vfield->num_values())
    {
      double val = 0.0;
      vfield->get_value(val, node.index());
      vbo[i+nodeOffset] = static_cast<float>(val);
    }
    else
    {
      vbo[i+nodeOffset] = 0.0f;
    }
    nodeOffset += 1;

    // Add optional normals (aNormal)
    if (vmesh->has_normals())
    {
      Core::Geometry::Vector normal;
      vmesh->get_normal(normal, node.index());

      vbo[i+nodeOffset+0] = normal.x();
      vbo[i+nodeOffset+1] = normal.y();
      vbo[i+nodeOffset+2] = normal.z();
      nodeOffset += 3;
    }

    i += nodeOffset;
  }

  if (progressFunc) progressFunc(0.25);

  // Build the edges
  if (showEdges)
  {
    std::string iboName = id + "edgesIBO";<commit_msg>Changed position of normal within structure.<commit_after>/*
   For more information, please see: http://software.sci.utah.edu

   The MIT License

   Copyright (c) 2012 Scientific Computing and Imaging Institute,
   University of Utah.

   License for the specific language governing rights and limitations under
   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   DEALINGS IN THE SOFTWARE.
*/

#include <Modules/Visualization/ShowField.h>
#include <Core/Datatypes/Geometry.h>
#include <Core/Datatypes/Legacy/Field/VMesh.h>
#include <Core/Datatypes/Legacy/Field/Field.h>
#include <Core/Datatypes/Legacy/Field/VField.h>
#include <Core/Datatypes/Mesh/MeshFacade.h>
#include <Core/Datatypes/Color.h>

#include <boost/foreach.hpp>

using namespace SCIRun::Modules::Visualization;
using namespace SCIRun::Core::Datatypes;
using namespace SCIRun::Dataflow::Networks;
using namespace SCIRun::Core::Algorithms;


ShowFieldModule::ShowFieldModule() : 
    Module(ModuleLookupInfo("ShowField", "Visualization", "SCIRun"))
{
}


void ShowFieldModule::execute()
{
  boost::shared_ptr<SCIRun::Field> field = getRequiredInput(Field);
  GeometryHandle geom = buildGeometryObject(field, get_state(), get_id());
  sendOutput(SceneGraph, geom);
}


GeometryHandle ShowFieldModule::buildGeometryObject(
    boost::shared_ptr<SCIRun::Field> field,
    ModuleStateHandle state, 
    const std::string& id)
{
  // Function for reporting progress.
  SCIRun::Core::Algorithms::AlgorithmStatusReporter::UpdaterFunc progressFunc =
      getUpdaterFunc();

  // VMesh facade. A simpler interface to the vmesh type.
  SCIRun::Core::Datatypes::MeshTraits<VMesh>::MeshFacadeHandle facade =
      field->mesh()->getFacade();
  ENSURE_NOT_NULL(facade, "Mesh facade");

  // VField required only for extracting field data from the field datatype.
  VField* vfield = field->vfield();

  // Grab the vmesh object so that we can synchronize and extract normals.
  VMesh* vmesh = field->vmesh();

  // Ensure any changes made to the mesh are reflected in the normals by
  // by synchronizing the mesh.
  if (vmesh->has_normals())
    vmesh->synchronize(Mesh::NORMALS_E);
  
  /// \todo Determine a better way of handling all of the various object state.
  bool showNodes = state->getValue(ShowFieldModule::ShowNodes).getBool();
  bool showEdges = state->getValue(ShowFieldModule::ShowEdges).getBool();
  bool showFaces = state->getValue(ShowFieldModule::ShowFaces).getBool();
  bool nodeTransparency = state->getValue(ShowFieldModule::NodeTransparency).getBool();

  // Resultant geometry type (representing a spire object and a number of passes).
  GeometryHandle geom(new GeometryObject(field));
  geom->objectName = id;

  /// \todo Split the mesh into chunks of about ~32,000 vertices. May be able to
  ///       eek out better coherency and use a 16 bit index buffer instead of
  ///       a 32 bit index buffer.

  // Crude method of counting the attributes we are placing in the VBO.
  int numFloats = 3 + 1;  // Position + field data.
  if (vmesh->has_normals())
    numFloats += 3;       // Position + field data + normals;

  // Allocate memory for vertex buffer.
  // Edges and faces should use the same vbo!
  std::shared_ptr<std::vector<uint8_t>> rawVBO(new std::vector<uint8_t>());
  size_t vboSize = sizeof(float) * numFloats * facade->numNodes();
  rawVBO->resize(vboSize); // linear complexity.
  // The C++ standard guarantees that vectors are contiguous in memory, so we
  // grab a pointer to the first element and use that as the starting point
  // for building our VBO.
  float* vbo = reinterpret_cast<float*>(&(*rawVBO)[0]);

  // Add shared VBO to the geometry object.
  /// \note This 'primaryVBO' is dependent on the types present in the data.
  ///       If normals are specified, then this will NOT be the primary VBO.
  ///       Another VBO will be constructed containing normal information.
  ///       All of this is NOT necessary if we are on OpenGL 3.2+ where we
  ///       can compute all normals in the geometry shader (smooth and face).
  std::string primVBOName = id + "primaryVBO";
  std::vector<std::string> attribs;   ///< \todo Switch to initializer lists when msvc supports it.
  attribs.push_back("aPos");          ///< \todo Find a good place to pull these names from.
  if (vmesh->has_normals())
    attribs.push_back("aNormal");
  attribs.push_back("aFieldData");
  geom->mVBOs.emplace_back(GeometryObject::SpireVBO(primVBOName, attribs, rawVBO));

  if (progressFunc) progressFunc(0.1);

  // Build vertex buffer.
  size_t i = 0;
  BOOST_FOREACH(const NodeInfo<VMesh>& node, facade->nodes())
  {
    // Add position (aPos)
    size_t nodeOffset = 0;
    vbo[i+nodeOffset+0] = node.point().x();
    vbo[i+nodeOffset+1] = node.point().y();
    vbo[i+nodeOffset+2] = node.point().z();
    nodeOffset += 3;

    // Add optional normals (aNormal)
    if (vmesh->has_normals())
    {
      Core::Geometry::Vector normal;
      vmesh->get_normal(normal, node.index());

      vbo[i+nodeOffset+0] = normal.x();
      vbo[i+nodeOffset+1] = normal.y();
      vbo[i+nodeOffset+2] = normal.z();
      nodeOffset += 3;
    }

    // Add field data (aFieldData)
    if (node.index() < vfield->num_values())
    {
      double val = 0.0;
      vfield->get_value(val, node.index());
      vbo[i+nodeOffset] = static_cast<float>(val);
    }
    else
    {
      vbo[i+nodeOffset] = 0.0f;
    }
    nodeOffset += 1;

    i += nodeOffset;
  }

  if (progressFunc) progressFunc(0.25);

  // Build the edges
  if (showEdges)
  {
    std::string iboName = id + "edgesIBO";
{'fraction_non_alphanumeric': 0.11095817083428922, 'fraction_numerical': 0.0062705513496979426, 'mean_word_length': 3.719595813785637, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4539730', 'n_tokens_mistral': 4088, 'n_tokens_neox': 3945, 'n_words': 1304}