<commit_before>/*===================================================================

The Medical Imaging Interaction Toolkit (MITK)

Copyright (c) German Cancer Research Center,
Division of Medical and Biological Informatics.
All rights reserved.

This software is distributed WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.

See LICENSE.txt or http://www.mitk.org for details.

===================================================================*/

// MITK
#include "mitkPointSetReaderService.h"
#include "mitkGeometry3DToXML.h"
#include "mitkProportionalTimeGeometry.h"
#include "mitkIOMimeTypes.h"

// STL
#include <iostream>
#include <fstream>
#include <mitkLocaleSwitch.h>

#include <tinyxml.h>

mitk::PointSetReaderService::PointSetReaderService()
  : AbstractFileReader(CustomMimeType(IOMimeTypes::POINTSET_MIMETYPE()), "MITK Point Set Reader")
{
  RegisterService();
}

mitk::PointSetReaderService::~PointSetReaderService()
{}

std::vector< itk::SmartPointer<mitk::BaseData> > mitk::PointSetReaderService::Read()
{
  // Switch the current locale to "C"
  LocaleSwitch localeSwitch("C");

  std::vector< itk::SmartPointer<mitk::BaseData> > result;

  InputStream stream(this);

  TiXmlDocument doc;
  stream >> doc;
  if (!doc.Error())
  {
    TiXmlHandle docHandle( &doc );
    //unsigned int pointSetCounter(0);
    for( TiXmlElement* currentPointSetElement = docHandle.FirstChildElement("point_set_file").FirstChildElement("point_set").ToElement();
         currentPointSetElement != NULL; currentPointSetElement = currentPointSetElement->NextSiblingElement())
    {
      mitk::PointSet::Pointer newPointSet = mitk::PointSet::New();

      // time geometry assembled for addition after all points
      // else the SetPoint method would already transform the points that we provide it
      mitk::ProportionalTimeGeometry::Pointer timeGeometry = mitk::ProportionalTimeGeometry::New();

      if(currentPointSetElement->FirstChildElement("time_series") != NULL)
      {
        for( TiXmlElement* currentTimeSeries = currentPointSetElement->FirstChildElement("time_series")->ToElement();
             currentTimeSeries != NULL; currentTimeSeries = currentTimeSeries->NextSiblingElement())
        {
          unsigned int currentTimeStep(0);
          TiXmlElement* currentTimeSeriesID = currentTimeSeries->FirstChildElement("time_series_id");

          currentTimeStep = atoi(currentTimeSeriesID->GetText());

          TiXmlElement* geometryElem = currentTimeSeries->FirstChildElement("Geometry3D");
          if ( geometryElem )
          {
              Geometry3D::Pointer geometry = Geometry3DToXML::FromXML(geometryElem);
              if (geometry.IsNotNull())
              {
                timeGeometry->Expand( currentTimeStep + 1 );
                timeGeometry->SetTimeStepGeometry(geometry,currentTimeStep);
              }
              else
              {
                MITK_ERROR << "Could not deserialize Geometry3D element.";
              }
          }
          else
          {
              MITK_WARN << "Fallback to legacy behavior: defining PointSet geometry as identity";
          }

          newPointSet = this->ReadPoints(newPointSet, currentTimeSeries, currentTimeStep);
        }
      }
      else
      {
        newPointSet = this->ReadPoints(newPointSet, currentPointSetElement, 0);
      }

      newPointSet->SetTimeGeometry(timeGeometry);<commit_msg>COMP: Merge branch 'bug-16115-store-geometry-in-pointset-file'<commit_after>/*===================================================================

The Medical Imaging Interaction Toolkit (MITK)

Copyright (c) German Cancer Research Center,
Division of Medical and Biological Informatics.
All rights reserved.

This software is distributed WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.

See LICENSE.txt or http://www.mitk.org for details.

===================================================================*/

// MITK
#include "mitkPointSetReaderService.h"
#include "mitkGeometry3DToXML.h"
#include "mitkProportionalTimeGeometry.h"
#include "mitkIOMimeTypes.h"

// STL
#include <iostream>
#include <fstream>
#include <mitkLocaleSwitch.h>

#include <tinyxml.h>

mitk::PointSetReaderService::PointSetReaderService()
  : AbstractFileReader(CustomMimeType(IOMimeTypes::POINTSET_MIMETYPE()), "MITK Point Set Reader")
{
  RegisterService();
}

mitk::PointSetReaderService::~PointSetReaderService()
{}

std::vector< itk::SmartPointer<mitk::BaseData> > mitk::PointSetReaderService::Read()
{
  // Switch the current locale to "C"
  LocaleSwitch localeSwitch("C");

  std::vector< itk::SmartPointer<mitk::BaseData> > result;

  InputStream stream(this);

  TiXmlDocument doc;
  stream >> doc;
  if (!doc.Error())
  {
    TiXmlHandle docHandle( &doc );
    //unsigned int pointSetCounter(0);
    for( TiXmlElement* currentPointSetElement = docHandle.FirstChildElement("point_set_file").FirstChildElement("point_set").ToElement();
         currentPointSetElement != NULL; currentPointSetElement = currentPointSetElement->NextSiblingElement())
    {
      mitk::PointSet::Pointer newPointSet = mitk::PointSet::New();

      // time geometry assembled for addition after all points
      // else the SetPoint method would already transform the points that we provide it
      mitk::ProportionalTimeGeometry::Pointer timeGeometry = mitk::ProportionalTimeGeometry::New();

      if(currentPointSetElement->FirstChildElement("time_series") != NULL)
      {
        for( TiXmlElement* currentTimeSeries = currentPointSetElement->FirstChildElement("time_series")->ToElement();
             currentTimeSeries != NULL; currentTimeSeries = currentTimeSeries->NextSiblingElement())
        {
          unsigned int currentTimeStep(0);
          TiXmlElement* currentTimeSeriesID = currentTimeSeries->FirstChildElement("time_series_id");

          currentTimeStep = atoi(currentTimeSeriesID->GetText());

          timeGeometry->Expand( currentTimeStep + 1 ); // expand (default to identity) in any case
          TiXmlElement* geometryElem = currentTimeSeries->FirstChildElement("Geometry3D");
          if ( geometryElem )
          {
              Geometry3D::Pointer geometry = Geometry3DToXML::FromXML(geometryElem);
              if (geometry.IsNotNull())
              {
                timeGeometry->SetTimeStepGeometry(geometry,currentTimeStep);
              }
              else
              {
                MITK_ERROR << "Could not deserialize Geometry3D element.";
              }
          }
          else
          {
              MITK_WARN << "Fallback to legacy behavior: defining PointSet geometry as identity";
          }

          newPointSet = this->ReadPoints(newPointSet, currentTimeSeries, currentTimeStep);
        }
      }
      else
      {
        newPointSet = this->ReadPoints(newPointSet, currentPointSetElement, 0);
      }

      newPointSet->SetTimeGeometry(timeGeometry);
{'fraction_non_alphanumeric': 0.13679715302491102, 'fraction_numerical': 0.003274021352313167, 'mean_word_length': 3.6902536715620826, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '3307249', 'n_tokens_mistral': 1878, 'n_tokens_neox': 1821, 'n_words': 403}