Skip to content

Commit 394efee

Browse files
author
kgillette
committed
Updates to the IGB importer: code included but does not properly handle data
1 parent 12c0706 commit 394efee

File tree

2 files changed

+118
-197
lines changed

2 files changed

+118
-197
lines changed

src/Core/IEPlugin/IEPluginInit.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void IEPluginManager::Initialize()
6666
static FieldIEPluginLegacyAdapter CurveField_plugin("CurveField", "*.pts *.pos *.edge", "", TextToCurveField_reader, CurveFieldToTextBaseIndexZero_writer);
6767

6868
static MatrixIEPluginLegacyAdapter EcgsimFileMatrix_plugin("ECGSimFile", "", "", EcgsimFileMatrix_reader, EcgsimFileMatrix_writer);
69-
static MatrixIEPluginLegacyAdapter IgbFileMatrix_plugin("IGBFile", "*.igb", "*.igb", IgbFileMatrix_reader, IgbFileMatrix_writer);
69+
static MatrixIEPluginLegacyAdapter IgbFileMatrix_plugin("IGBFile", "*.igb", "*.igb", IgbFileMatrix_reader, nullptr);
7070

7171
static FieldIEPluginLegacyAdapter TriSurfField_plugin("TriSurfField", "*.fac *.tri *.pts *.pos", "", TextToTriSurfField_reader, TriSurfFieldToTextBaseIndexZero_writer);
7272
static FieldIEPluginLegacyAdapter TriSurfFieldBaseIndexOne_plugin("TriSurfField[BaseIndex 1]", "*.fac *.pts", "", nullptr, TriSurfFieldToTextBaseIndexOne_writer);

src/Core/IEPlugin/IgbFileToMatrix_Plugin.cc

Lines changed: 117 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
*/
2828

2929
/*
30-
* EcgsimFileToMatrix_Plugin.cc
30+
* IgbFiletoMatrix_Plugin.cc
3131
*
3232
* Written by:
33-
* Jeroen Stinstra
34-
* Department of Computer Science
33+
* Karli Gillette
34+
* Department of Bioengineering
3535
* University of Utah
3636
*
3737
*/
@@ -46,222 +46,143 @@
4646
#include <Core/ImportExport/Matrix/MatrixIEPlugin.h>
4747
#include <Core/Datatypes/DenseMatrix.h>
4848

49+
#include <string.h>
50+
#include <stdlib.h>
51+
#include <string>
52+
#include <stdio.h>
53+
#include <vector>
54+
#include <boost/tokenizer.hpp>
55+
#include <boost/foreach.hpp>
56+
#include <boost/algorithm/string.hpp>
57+
#include <boost/algorithm/string/iter_find.hpp>
58+
#include <cmath>
59+
4960
#include <iostream>
5061
#include <fstream>
5162
#include <sstream>
5263

64+
using namespace std;
5365
using namespace SCIRun;
5466
using namespace SCIRun::Core;
5567
using namespace SCIRun::Core::Logging;
5668
using namespace SCIRun::Core::Datatypes;
5769

5870
MatrixHandle SCIRun::IgbFileMatrix_reader(LoggerHandle pr, const char *filename)
5971
{
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
72+
73+
string line;
74+
vector<string> strs;
75+
int x_size=0;
76+
int t_size=0;
77+
int count=0;
78+
79+
80+
ifstream myfile (filename);
81+
if (myfile.is_open())
7982
{
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)
83+
//while ( myfile.good() )
84+
for(int i=0; i<5; i++)
12285
{
123-
nrows++;
124-
if (ncols > 0)
125-
{
126-
if (ncols != line_ncols)
86+
getline (myfile,line);
87+
cout << line << endl;
88+
89+
boost::split(strs,line,boost::is_any_of(":, "));
90+
size_t sz;
91+
sz=line.size();
92+
93+
std::cout << sz << std::endl;
94+
95+
96+
for(int p=0;p<sz;p++)
12797
{
128-
if (pr) pr->error("Improper format of text file, not every line contains the same amount of numbers");
129-
return (result);
98+
99+
std::cout << p << std::endl;
100+
101+
if (boost::iequals(strs[p], "x"))
102+
{
103+
x_size=atoi(strs[p+1].c_str());
104+
std::cout << "found it: " << strs[p] << "= " << strs[p+1] << '\n';
105+
106+
count += 1;
107+
108+
}
109+
if (boost::iequals(strs[p], "t"))
110+
{
111+
t_size=atoi(strs[p+1].c_str());
112+
std::cout << "found it: " << strs[p] << "= " << strs[p+1] << '\n';
113+
114+
count +=1;
115+
116+
}
117+
118+
if (count ==2 ) break;
119+
120+
130121
}
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-
}
122+
123+
124+
}
125+
myfile.close();
144126
}
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
155127

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;
128+
129+
streamoff length=0;
130+
streamsize numbyts=0;
131+
//char * buffer;
132+
133+
134+
135+
136+
ifstream is;
137+
is.open (filename, ios::in | ios::binary );
138+
if (is.is_open())
139+
{
140+
// get length of file:
141+
is.seekg (0, ios::end);
142+
length = is.tellg();
143+
is.seekg (1024, ios::beg);
144+
145+
vector<float> vec;
146+
vec.resize(x_size*t_size);
147+
148+
149+
150+
is.read ((char*)&vec[0],x_size * t_size * sizeof(float));
151+
if (!is)
152+
{
153+
numbyts=is.gcount();
154+
cout << "Error reading binary data. Number of bytes read: " << numbyts << endl;
169155

170-
try
171-
{
172-
inputfile.open(filename);
156+
}
157+
is.close();
158+
159+
160+
// auto result(boost::make_shared<DenseMatrix>(x_size,t_size));
161+
162+
DenseMatrixHandle result;
163+
164+
result.reset(new DenseMatrix(x_size,t_size));
165+
166+
for(size_t p=0;p<t_size;p++ )
167+
{
168+
169+
for(size_t pp=0;pp<x_size;pp++ )
170+
{
171+
172+
173+
(*result) << vec[(p*x_size)+pp];
174+
std::cout << vec[(p*x_size)+pp] << std::endl;
175+
}
176+
}
177+
173178

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-
{
179+
return(result);
202180
}
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);
214181
}
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);
224182

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-
}
262183

263-
return (true);
264-
}
184+
185+
265186

266187

267188

0 commit comments

Comments
 (0)