ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
rtkGeneralPurposeFunctions.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright RTK Consortium
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18
19#ifndef rtkGeneralPurposeFunctions_h
20#define rtkGeneralPurposeFunctions_h
21
22#include <vector>
23
24
25#include <cmath>
26
27#include <itkImageFileWriter.h>
28#include <itkImageIOFactory.h>
29#include <itkMacro.h>
30#include <itkMath.h>
31
32namespace rtk
33{
34
41
42static inline std::vector<double>
43ReadSignalFile(const std::string & filename)
44{
45 std::vector<double> signalVector;
46 std::ifstream is(filename.c_str());
47 if (!is.is_open())
48 {
49 itkGenericExceptionMacro(<< "Could not open signal file " << filename);
50 }
51
52 double value = NAN;
53 std::string s;
54 while (getline(is, s))
55 {
56 if (!s.empty())
57 {
58 std::istringstream tmp(s);
59 tmp >> value;
60 if (itk::Math::Round<double>(value * 100) / 100 == 1)
61 signalVector.push_back(0);
62 else
63 signalVector.push_back(itk::Math::Round<double>(value * 100) / 100);
64 }
65 }
66
67 return signalVector;
68}
69
71static inline itk::ImageIOBase::Pointer
72GetFileHeader(const std::string & filename)
73{
75 itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::ImageIOFactory::IOFileModeEnum::ReadMode);
76 if (!reader)
77 {
78 itkGenericExceptionMacro(<< "Could not read " << filename);
79 }
80 reader->SetFileName(filename);
81 reader->ReadImageInformation();
82 return reader;
83}
84
85template <typename ImageType>
86void
87WriteImage(typename ImageType::ConstPointer input, std::string name)
88{
89 // Create an itk::ImageFileWriter
90 using WriterType = itk::ImageFileWriter<ImageType>;
91 auto writer = WriterType::New();
92 writer->SetInput(input);
93 writer->SetFileName(name);
94 writer->Update();
95}
96
97} // namespace rtk
98
99#endif // rtkGeneralPurposeFunctions_h
Writes image data to a single file.
SmartPointer< Self > Pointer
static ImageIOBasePointer CreateImageIO(const char *path, IOFileModeEnum mode)
static std::vector< double > ReadSignalFile(const std::string &filename)
A few functions that are used either in the applications or for debugging purposes.
TInput TInput constexpr auto Round(TInput x)
Definition itkMath.h:202
ITK_TEMPLATE_EXPORT void WriteImage(TImagePointer &&image, const std::string &filename, bool compress=false)
static itk::ImageIOBase::Pointer GetFileHeader(const std::string &filename)