Skip to content

Commit 4dcb9ab

Browse files
authored
Merge pull request #1469 from kgillette/basictutorial
Closes #1155
2 parents 3b70aef + d85131a commit 4dcb9ab

35 files changed

+677
-0
lines changed

src/Core/IEPlugin/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ SET(Core_IEPlugin_SRCS
3232
CurveField_Plugin.cc
3333
#DIF_Plugin.cc
3434
EcgsimFileToMatrix_Plugin.cc
35+
#IgbFileToMatrix_Plugin.cc
3536
#EcgsimFileToTriSurf_Plugin.cc
3637
#HexVolField_Plugin.cc
3738
IEPluginInit.cc
@@ -60,6 +61,7 @@ SET(Core_IEPlugin_HEADERS
6061
CurveField_Plugin.h
6162
#DIF_Plugin.h
6263
EcgsimFileToMatrix_Plugin.h
64+
IgbFileToMatrix_Plugin.h
6365
#EcgsimFileToTriSurf_Plugin.h
6466
#HexVolField_Plugin.h
6567
#MRC_Plugin.h

src/Core/IEPlugin/IEPluginInit.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <Core/IEPlugin/SimpleTextFileToMatrix_Plugin.h>
3333
#include <Core/IEPlugin/EcgsimFileToMatrix_Plugin.h>
3434
#include <Core/IEPlugin/PointCloudField_Plugin.h>
35+
#include <Core/IEPlugin/IGBFileToMatrix_Plugin.h>
3536
#include <Core/IEPlugin/CurveField_Plugin.h>
3637
#include <Core/IEPlugin/TriSurfField_Plugin.h>
3738
#include <Core/IEPlugin/TetVolField_Plugin.h>
@@ -65,6 +66,7 @@ void IEPluginManager::Initialize()
6566
static FieldIEPluginLegacyAdapter CurveField_plugin("CurveField", "*.pts *.pos *.edge", "", TextToCurveField_reader, CurveFieldToTextBaseIndexZero_writer);
6667

6768
static MatrixIEPluginLegacyAdapter EcgsimFileMatrix_plugin("ECGSimFile", "", "", EcgsimFileMatrix_reader, EcgsimFileMatrix_writer);
69+
//static MatrixIEPluginLegacyAdapter IgbFileMatrix_plugin("IGBFile", "*.igb", "*.igb", IgbFileMatrix_reader, IgbFileMatrix_writer);
6870

6971
static FieldIEPluginLegacyAdapter TriSurfField_plugin("TriSurfField", "*.fac *.tri *.pts *.pos", "", TextToTriSurfField_reader, TriSurfFieldToTextBaseIndexZero_writer);
7072
static FieldIEPluginLegacyAdapter TriSurfFieldBaseIndexOne_plugin("TriSurfField[BaseIndex 1]", "*.fac *.pts", "", nullptr, TriSurfFieldToTextBaseIndexOne_writer);
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
/*
2+
For more information, please see: http://software.sci.utah.edu
3+
4+
The MIT License
5+
6+
Copyright (c) 2015 Scientific Computing and Imaging Institute,
7+
University of Utah.
8+
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a
11+
copy of this software and associated documentation files (the "Software"),
12+
to deal in the Software without restriction, including without limitation
13+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
14+
and/or sell copies of the Software, and to permit persons to whom the
15+
Software is furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included
18+
in all copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26+
DEALINGS IN THE SOFTWARE.
27+
*/
28+
29+
/*
30+
* EcgsimFileToMatrix_Plugin.cc
31+
*
32+
* Written by:
33+
* Jeroen Stinstra
34+
* Department of Computer Science
35+
* University of Utah
36+
*
37+
*/
38+
39+
40+
#include <Core/ImportExport/Matrix/MatrixIEPlugin.h>
41+
#include <Core/IEPlugin/IgbmFileToMatrix_Plugin.h>
42+
#include <Core/Datatypes/DenseMatrix.h>
43+
#include <Core/Datatypes/MatrixTypeConversions.h>
44+
#include <Core/Utils/Legacy/StringUtil.h>
45+
#include <Core/Logging/LoggerInterface.h>
46+
#include <Core/ImportExport/Matrix/MatrixIEPlugin.h>
47+
#include <Core/Datatypes/DenseMatrix.h>
48+
49+
#include <iostream>
50+
#include <fstream>
51+
#include <sstream>
52+
53+
using namespace SCIRun;
54+
using namespace SCIRun::Core;
55+
using namespace SCIRun::Core::Logging;
56+
using namespace SCIRun::Core::Datatypes;
57+
58+
MatrixHandle SCIRun::IgbFileMatrix_reader(LoggerHandle pr, const char *filename)
59+
{
60+
DenseMatrixHandle result;
61+
62+
int ncols = 0;
63+
int nrows = 0;
64+
int line_ncols = 0;
65+
int header_rows = 0;
66+
int header_cols = 0;
67+
68+
std::string line;
69+
double data;
70+
71+
// STAGE 1 - SCAN THE FILE TO DETERMINE THE DIMENSIONS OF THE MATRIX
72+
// AND CHECK THE FILE'S INTEGRITY.
73+
74+
{
75+
std::ifstream inputfile;
76+
inputfile.exceptions( std::ifstream::badbit );
77+
78+
try
79+
{
80+
inputfile.open(filename);
81+
82+
// get header information
83+
getline(inputfile,line,'\n');
84+
for (size_t p = 0;p<line.size();p++)
85+
{
86+
if ((line[p] == '\t')||(line[p] == ',')||(line[p]=='"')) line[p] = ' ';
87+
}
88+
std::istringstream iss(line);
89+
iss >> header_rows;
90+
iss >> header_cols;
91+
92+
while( getline(inputfile,line,'\n'))
93+
{
94+
if (line.size() > 0)
95+
{
96+
// block out comments
97+
if ((line[0] == '#')||(line[0] == '%')) continue;
98+
}
99+
100+
// replace comma's and tabs with white spaces
101+
for (size_t p = 0;p<line.size();p++)
102+
{
103+
if ((line[p] == '\t')||(line[p] == ',')||(line[p]=='"')) line[p] = ' ';
104+
}
105+
std::istringstream iss(line);
106+
iss.exceptions( std::ifstream::failbit | std::ifstream::badbit);
107+
108+
try
109+
{
110+
line_ncols = 0;
111+
while(1)
112+
{
113+
iss >> data;
114+
line_ncols++;
115+
}
116+
}
117+
catch(...)
118+
{
119+
}
120+
121+
if (line_ncols > 0)
122+
{
123+
nrows++;
124+
if (ncols > 0)
125+
{
126+
if (ncols != line_ncols)
127+
{
128+
if (pr) pr->error("Improper format of text file, not every line contains the same amount of numbers");
129+
return (result);
130+
}
131+
}
132+
else
133+
{
134+
ncols = line_ncols;
135+
}
136+
}
137+
}
138+
139+
if(ncols*nrows != header_cols*header_rows)
140+
{
141+
if (pr) pr->error("Data does not match header information.");
142+
return(result);
143+
}
144+
}
145+
catch (...)
146+
{
147+
if (pr) pr->error("Could not open file: "+std::string(filename));
148+
return (result);
149+
}
150+
151+
inputfile.close();
152+
}
153+
154+
// STAGE 2 - NOW ACTUALLY READ AND STORE THE MATRIX
155+
156+
{
157+
std::ifstream inputfile;
158+
inputfile.exceptions( std::ifstream::badbit );
159+
160+
result.reset(new DenseMatrix(header_rows,header_cols));
161+
if (!result)
162+
{
163+
if (pr) pr->error("Could not allocate matrix");
164+
return(result);
165+
}
166+
167+
double* dataptr = result->data();
168+
int k = 0;
169+
170+
try
171+
{
172+
inputfile.open(filename);
173+
174+
// get header information
175+
getline(inputfile,line,'\n');
176+
177+
while( getline(inputfile,line,'\n'))
178+
{
179+
if (line.size() > 0)
180+
{
181+
// block out comments
182+
if ((line[0] == '#')||(line[0] == '%')) continue;
183+
}
184+
185+
// replace comma's and tabs with white spaces
186+
for (size_t p = 0;p<line.size();p++)
187+
{
188+
if ((line[p] == '\t')||(line[p] == ',')||(line[p]=='"')) line[p] = ' ';
189+
}
190+
std::istringstream iss(line);
191+
iss.exceptions( std::ifstream::failbit | std::ifstream::badbit);
192+
try
193+
{
194+
while(1)
195+
{
196+
iss >> data;
197+
dataptr[k++] = data;
198+
}
199+
}
200+
catch(...)
201+
{
202+
}
203+
}
204+
}
205+
catch (...)
206+
{
207+
if (pr) pr->error("Could not open and read data in file: " + std::string(filename));
208+
return (result);
209+
}
210+
211+
inputfile.close();
212+
}
213+
return(result);
214+
}
215+
216+
217+
bool SCIRun::IgbFileMatrix_writer(LoggerHandle pr, MatrixHandle matrixInput, const char *filename)
218+
{
219+
220+
std::ofstream outputfile;
221+
outputfile.exceptions( std::ofstream::failbit | std::ofstream::badbit );
222+
223+
DenseMatrixHandle matrix = castMatrix::toDense(matrixInput);
224+
225+
if (!matrix)
226+
{
227+
if (pr) pr->error("Empty matrix detected");
228+
return(false);
229+
}
230+
231+
double* dataptr = matrix->data();
232+
if (!dataptr)
233+
{
234+
if (pr) pr->error("Empty matrix detected");
235+
return(false);
236+
}
237+
238+
try
239+
{
240+
outputfile.open(filename);
241+
}
242+
catch (...)
243+
{
244+
if (pr) pr->error("Could not open file: "+std::string(filename));
245+
return (false);
246+
}
247+
248+
// output header line
249+
//int rows = matrix->nrows();
250+
//int cols = matrix->ncols();
251+
outputfile << matrix->nrows() << " " << matrix->ncols() << std::endl;
252+
253+
size_t k = 0;
254+
for (int p=0; p<matrix->nrows(); p++)
255+
{
256+
for (int q=0; q<matrix->ncols(); q++)
257+
{
258+
outputfile << dataptr[k++] << " ";
259+
}
260+
outputfile << "\n";
261+
}
262+
263+
return (true);
264+
}
265+
266+
267+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
For more information, please see: http://software.sci.utah.edu
3+
4+
The MIT License
5+
6+
Copyright (c) 2015 Scientific Computing and Imaging Institute,
7+
University of Utah.
8+
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a
11+
copy of this software and associated documentation files (the "Software"),
12+
to deal in the Software without restriction, including without limitation
13+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
14+
and/or sell copies of the Software, and to permit persons to whom the
15+
Software is furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included
18+
in all copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26+
DEALINGS IN THE SOFTWARE.
27+
*/
28+
29+
#ifndef CORE_IEPLUGIN_IGBFILETOMATRIX_PLUGIN_H__
30+
#define CORE_IEPLUGIN_IGBFILETOMATRIX_PLUGIN_H__
31+
32+
#include <Core/Logging/LoggerFwd.h>
33+
#include <Core/Datatypes/DatatypeFwd.h>
34+
#include <Core/IEPlugin/share.h>
35+
36+
namespace SCIRun
37+
{
38+
SCISHARE Core::Datatypes::MatrixHandle IgbFileMatrix_reader(Core::Logging::LoggerHandle pr, const char *filename);
39+
40+
SCISHARE bool IgbFileMatrix_writer(Core::Logging::LoggerHandle pr, Core::Datatypes::MatrixHandle fh, const char* filename);
41+
}
42+
43+
#endif
4.12 MB
Binary file not shown.

0 commit comments

Comments
 (0)