Пример #1
0
  void mitk::XMLSerializable::FromXMLFile(const std::string& file
                                          , const std::string& elemName)
  {
    endodebug( "Trying to read from " << file )

    TiXmlDocument doc( file.c_str() );
    bool loadOkay = doc.LoadFile();
    if(!loadOkay)
    {
      std::ostringstream s; s << "File " << file
          << " could not be loaded!";
      throw std::logic_error(s.str().c_str());
    }

    m_XMLFileName = file;

    TiXmlElement* elem = doc.FirstChildElement();
    endoAssertMsg( elem, "No root element found" );

    // determine element to read from
    std::string elementName = elemName;
    if(elementName.empty())
      elementName = this->GetNameOfClass();
    // try again with the first element
    if(strcmp(elem->Value(), elementName.c_str()) != 0)
      elem = elem->FirstChildElement(elementName.c_str());

    endoAssertMsg( elem, "No child element \"" << elementName <<
                 "\" found in " << file );

    // if theres an attribute as file reference try to load the class
    // from that file
    std::string filename;
    if(elem->QueryStringAttribute(FILE_REFERENCE_ATTRIBUTE_NAME.c_str(), &filename)
      == TIXML_SUCCESS)
    {
      if( !itksys::SystemTools::FileIsFullPath(filename.c_str()) )
        filename = itksys::SystemTools::GetFilenamePath(file) + "/" + filename;
      this->FromXMLFile(filename);
      return; // exit!
    }

    this->FromXML( elem );
  }
Пример #2
0
void mitk::CameraIntrinsics::FromXML(TiXmlElement* elem)
{
  endoAssert ( elem );
  MITK_DEBUG << elem->Value();
  std::string filename;
  if(elem->QueryStringAttribute("file", &filename) == TIXML_SUCCESS)
  {
    this->FromXMLFile(filename);
    return;
  }
  else if(strcmp(elem->Value(), "CalibrationProject") == 0)
  {
    this->FromGMLCalibrationXML(elem->FirstChildElement("results"));
    return;
  }

  assert ( elem );
  if(strcmp(elem->Value(), this->GetNameOfClass()) != 0)
    elem = elem->FirstChildElement(this->GetNameOfClass());

  std::ostringstream err;
  // CAMERA MATRIX
  cv::Mat CameraMatrix = cv::Mat::zeros(3, 3, cv::DataType<double>::type);
  CameraMatrix.at<double>(2,2) = 1.0;
  float val = 0.0f;
  if(elem->QueryFloatAttribute("fx", &val) == TIXML_SUCCESS)
    CameraMatrix.at<double>(0,0) = val;
  else
    err << "fx, ";

  if(elem->QueryFloatAttribute("fy", &val) == TIXML_SUCCESS)
    CameraMatrix.at<double>(1,1) = val;
  else
    err << "fy, ";

  if(elem->QueryFloatAttribute("cx", &val) == TIXML_SUCCESS)
    CameraMatrix.at<double>(0,2) = val;
  else
    err << "cx, ";

  if(elem->QueryFloatAttribute("cy", &val) == TIXML_SUCCESS)
    CameraMatrix.at<double>(1,2) = val;
  else
    err << "cy, ";

  // DISTORSION COEFFS
  endodebug( "creating DistorsionCoeffs from XML file")
  cv::Mat DistorsionCoeffs = cv::Mat::zeros(1, 5, cv::DataType<double>::type);
  if(elem->QueryFloatAttribute("k1", &val) == TIXML_SUCCESS)
    DistorsionCoeffs.at<double>(0,0) = val;
  else
    err << "k1, ";

  if(elem->QueryFloatAttribute("k2", &val) == TIXML_SUCCESS)
    DistorsionCoeffs.at<double>(0,1) = val;
  else
    err << "k2, ";

  if(elem->QueryFloatAttribute("p1", &val) == TIXML_SUCCESS)
    DistorsionCoeffs.at<double>(0,2) = val;
  else
    err << "p1, ";

  if(elem->QueryFloatAttribute("p2", &val) == TIXML_SUCCESS)
    DistorsionCoeffs.at<double>(0,3) = val;
  else
    err << "p2, ";

  DistorsionCoeffs.at<double>(0,4) = 0.0;

  /*if(elem->QueryFloatAttribute("k3", &val) == TIXML_SUCCESS)
    DistorsionCoeffs.at<double>(4,0) = val;
  else
    err << "k3, ";*/

  std::string errorStr = err.str();
  int errLength = errorStr.length();
  if(errLength > 0)
  {
    errorStr = errorStr.substr(0, errLength-2);
    errorStr.append(" not found");
    throw std::invalid_argument(err.str());
  }

  int valid = 0;
  elem->QueryIntAttribute("Valid", &valid);

  {
    itk::MutexLockHolder<itk::FastMutexLock> lock(*m_Mutex);
    m_Valid = static_cast<bool>(valid);
    m_CameraMatrix = CameraMatrix;
    m_DistorsionCoeffs = DistorsionCoeffs;
  }

  this->Modified();
}
Пример #3
0
  void EndoDebugFromXmlFile::Update()
  {
    endodebug( __FUNCTION__ )

    std::string _FileName = *d->m_FileName;
    if( !itksys::SystemTools::FileExists( _FileName.c_str() ) )
    {
      endodebug(_FileName << " does not exist");
      return;
    }

    long int _FileModifiedTime
      = itksys::SystemTools::ModifiedTime( _FileName.c_str() );
    // file has changed: we know an older version...
    if( d->m_FileModifiedTime >= _FileModifiedTime )
    {
        endodebug("File not changed. No Update necessary.");
        return;
    }

    // reread
    endodebugvar( _FileName )
    TiXmlDocument doc( _FileName );
    doc.LoadFile();
    TiXmlHandle docHandle( &doc );
    TiXmlElement* elem = docHandle.FirstChildElement().FirstChildElement( "EndoDebug" ).ToElement();

    if(elem == 0)
    {
      endodebug("EndoDebug element not found");
      return;
    }

    int _DebugEnabled = d->m_EndoDebug->GetDebugEnabled();
    if( elem->QueryIntAttribute("DebugEnabled",&_DebugEnabled) != TIXML_SUCCESS )
      endodebug("DebugEnabled attribute not found");

    int _ShowImagesInDebug = d->m_EndoDebug->GetShowImagesInDebug();
    if( elem->QueryIntAttribute("ShowImagesInDebug",&_ShowImagesInDebug) != TIXML_SUCCESS )
      endodebug("ShowImagesInDebug attribute not found");

    int _ShowImagesTimeOut = d->m_EndoDebug->GetShowImagesTimeOut();
    if( elem->QueryIntAttribute("ShowImagesTimeOut",&_ShowImagesTimeOut) != TIXML_SUCCESS )
      endodebug("ShowImagesTimeOut attribute not found");

    std::string _DebugImagesOutputDirectory = d->m_EndoDebug->GetDebugImagesOutputDirectory();
    if( elem->QueryStringAttribute("DebugImagesOutputDirectory",&_DebugImagesOutputDirectory) != TIXML_SUCCESS )
      endodebug("DebugImagesOutputDirectory attribute not found");

    std::set<std::string> _FilesToDebug;
    std::string _FilesToDebugString;
    if( elem->QueryStringAttribute("FilesToDebug",&_FilesToDebugString) != TIXML_SUCCESS )
    {
      endodebug("FilesToDebug attribute not found");
    }
    else
    {
        StringExplode( _FilesToDebugString, ";", &_FilesToDebug );
    }

    std::set<std::string> _SymbolsToDebug;
    std::string _SymbolsToDebugString;
    if( elem->QueryStringAttribute("SymbolsToDebug",&_SymbolsToDebugString) != TIXML_SUCCESS )
    {
      endodebug("SymbolsToDebug attribute not found");
    }
    else
    {
        StringExplode( _SymbolsToDebugString, ";", &_SymbolsToDebug );
    }

    // save
    mitk::EndoDebug::GetInstance().SetDebugEnabled( _DebugEnabled == 1? true: false );
    mitk::EndoDebug::GetInstance().SetShowImagesInDebug( _ShowImagesInDebug == 1? true: false  );
    mitk::EndoDebug::GetInstance().SetShowImagesTimeOut( _ShowImagesTimeOut );
    mitk::EndoDebug::GetInstance().SetDebugImagesOutputDirectory( _DebugImagesOutputDirectory );
    mitk::EndoDebug::GetInstance().SetFilesToDebug(_FilesToDebug);
    mitk::EndoDebug::GetInstance().SetSymbolsToDebug(_SymbolsToDebug);

    // save that modified time
    d->m_FileModifiedTime = _FileModifiedTime;
  }