Skip to content

Commit 5431416

Browse files
author
Karli Gillette
committed
Added carp mesh importer for tet meshes
1 parent 3576199 commit 5431416

File tree

2 files changed

+58
-235
lines changed

2 files changed

+58
-235
lines changed

src/Core/IEPlugin/CARPMesh_Plugin.cc

Lines changed: 57 additions & 234 deletions
Original file line numberDiff line numberDiff line change
@@ -180,304 +180,127 @@ FieldHandle SCIRun::CARPMesh_reader(LoggerHandle pr, const char *filename)
180180
}
181181
}
182182

183-
int ncols = 0;
184-
int nrows = 0;
185-
int line_ncols = 0;
186-
int num_nodes = 0, num_elems = 0;
187-
188183
std::string line;
189184

190185
// STAGE 1 - SCAN THE FILE TO DETERMINE THE NUMBER OF NODES
191186
// AND CHECK THE FILE'S INTEGRITY.
192187

193-
bool has_header_pts = false;
194-
bool first_line_pts = true;
195-
196-
bool has_header = false;
197-
bool first_line = true;
198-
199-
bool has_data = false;
200-
188+
int num_nodes = 0; int num_elems =0;
201189
std::vector<double> values;
190+
std::vector<double> fvalues;
191+
std::vector<VMesh::index_type> ivalues;
192+
std::string elem_type;
193+
194+
// if (nrows != num_nodes)
195+
// {
196+
// if (pr) pr->warning("Number of nodes listed in header (" + boost::lexical_cast<std::string>(num_nodes) +
197+
// ") does not match number of non-header rows in file (" + boost::lexical_cast<std::string>(nrows) + ")");
198+
// }
199+
//
200+
//
201+
// if ( nrows != num_elems)
202+
// {
203+
// if (pr) pr->warning("Number of elements listed in header (" + boost::lexical_cast<std::string>(num_elems) +
204+
// ") does not match number of non-header rows in file (" + boost::lexical_cast<std::string>(nrows) + ")");
205+
// }
202206

203-
{
204-
std::ifstream inputfile;
205-
inputfile.exceptions( std::ifstream::badbit );
206-
try
207-
{
208-
inputfile.open(pts_fn.c_str());
209-
210-
while (getline(inputfile,line,'\n'))
211-
{
212-
if (line.size() > 0)
213-
{
214-
// block out comments
215-
if ((line[0] == '#')||(line[0] == '%')) continue;
216-
}
217-
218-
// replace comma's and tabs with white spaces
219-
for (size_t p = 0;p<line.size();p++)
220-
{
221-
if ((line[p] == '\t')||(line[p] == ',')||(line[p]=='"')) line[p] = ' ';
222-
}
223-
224-
multiple_from_string(line,values);
225-
line_ncols = values.size();
207+
// add data to elems (constant basis)
208+
FieldInformation fi("TetVolMesh",-1,"double");
209+
fi.make_constantdata();
210+
result = CreateField(fi);
226211

227-
if (first_line_pts)
228-
{
229-
if (line_ncols > 0)
230-
{
231-
if (line_ncols == 1)
232-
{
233-
has_header_pts = true;
234-
num_nodes = static_cast<int>(values[0]) + 1;
235-
}
236-
else if ((line_ncols > 1))
237-
{
238-
has_header_pts = false;
239-
first_line_pts = false;
240-
nrows++;
241-
ncols = line_ncols;
242-
}
243-
}
244-
}
245-
else
246-
{
247-
if (line_ncols > 0)
248-
{
249-
nrows++;
250-
if (ncols > 0)
251-
{
252-
if (ncols != line_ncols)
253-
{
254-
if (pr) pr->error("Improper format of text file, not every line contains the same amount of coordinates");
255-
return (result);
256-
}
257-
}
258-
else
259-
{
260-
ncols = line_ncols;
261-
}
262-
}
263-
}
264-
}
265-
}
266-
catch (...)
267-
{
268-
if (pr) pr->error("Could not open and read file: " + pts_fn);
269-
return (result);
270-
}
271-
inputfile.close();
272-
}
212+
VMesh *mesh = result->vmesh();
213+
VField *field = result->vfield();
273214

274-
if (0 == num_nodes)
275-
{
276-
num_nodes = nrows;
277-
}
278-
else if ( has_header_pts && (nrows != num_nodes) )
279-
{
280-
if (pr) pr->warning("Number of nodes listed in header (" + boost::lexical_cast<std::string>(num_nodes) +
281-
") does not match number of non-header rows in file (" + boost::lexical_cast<std::string>(nrows) + ")");
282-
}
283215

284-
nrows = 0;
285-
ncols = 0;
286-
line_ncols = 0;
216+
// Elements file
287217

288-
bool zero_based = false;
289218
{
290219
std::ifstream inputfile;
291220
inputfile.exceptions( std::ifstream::badbit );
221+
292222
try
293223
{
294224
inputfile.open(elems_fn.c_str());
295225

296-
while (getline(inputfile,line,'\n'))
297-
{
298-
if (line.size() > 0)
299-
{
300-
// block out comments
301-
if ((line[0] == '#')||(line[0] == '%')) continue;
302-
}
226+
VMesh::Node::array_type vdata;
227+
vdata.resize(4);
303228

304-
// replace comma's and tabs with white spaces
305-
for (size_t p = 0;p<line.size();p++)
306-
{
307-
if ((line[p] == '\t')||(line[p] == ',')||(line[p]=='"')) line[p] = ' ';
308-
}
229+
getline(inputfile,line,'\n');
230+
multiple_from_string(line,values);
231+
num_elems=static_cast<int>(values[0]);
232+
mesh->elem_reserve(num_elems);
309233

310-
multiple_from_string(line,values);
311-
line_ncols = values.size();
234+
for (int i = 0; i < num_elems && getline(inputfile,line,'\n'); ++i)
235+
{
312236

313-
for (size_t j=0; j<values.size(); j++) if (values[j] == 0.0) zero_based = true;
314237

315-
if (first_line)
316-
{
317-
if (line_ncols > 0)
318-
{
319-
if (line_ncols == 1)
320-
{
321-
has_header = true;
322-
num_elems = static_cast<int>(values[0]) + 1;
323-
}
324-
else if (line_ncols > 3)
325-
{
326-
has_header = false;
327-
first_line = false;
328-
nrows++;
329-
ncols = line_ncols;
330-
if (ncols == 5) has_data = true;
331-
}
332-
else
333-
{
334-
if (pr) pr->error("Improper format of text file, some lines do not contain 4 entries");
335-
return (result);
336-
}
337-
}
338-
}
339-
else
340-
{
341-
if (line_ncols > 0)
342-
{
343-
nrows++;
344-
if (ncols > 0)
238+
if (i == 0) {
239+
for (size_t k=0; k<line.size(); k++)
345240
{
346-
if (ncols != line_ncols)
347-
{
348-
if (pr) pr->error("Improper format of text file, not every line contains the same amount of node references");
349-
return (result);
350-
}
351-
}
352-
else
353-
{
354-
ncols = line_ncols;
355-
if (ncols == 5) has_data = true;
241+
if (line[k]=' '){ break;}
242+
else {
243+
elem_type += line[k];
244+
}
356245
}
357246
}
358-
}
359-
}
360-
}
361-
catch (...)
362-
{
363-
if (pr) pr->error("Could not open and read file: " + elems_fn);
364-
return (result);
365-
}
366-
inputfile.close();
367-
}
368-
369-
if (0 == num_elems)
370-
{
371-
num_elems = nrows;
372-
}
373-
else if ( has_header && (nrows != num_elems) )
374-
{
375-
if (pr) pr->warning("Number of elements listed in header (" + boost::lexical_cast<std::string>(num_elems) +
376-
") does not match number of non-header rows in file (" + boost::lexical_cast<std::string>(nrows) + ")");
377-
}
378247

379-
// add data to elems (constant basis)
380-
FieldInformation fi("TetVolMesh",-1,"double");
381-
if (has_data) fi.make_constantdata();
382-
result = CreateField(fi);
383-
384-
VMesh *mesh = result->vmesh();
385-
VField *field = result->vfield();
386-
387-
mesh->node_reserve(num_nodes);
388-
mesh->elem_reserve(num_elems);
389-
390-
{
391-
std::ifstream inputfile;
392-
inputfile.exceptions( std::ifstream::badbit );
393-
394-
try
395-
{
396-
inputfile.open(pts_fn.c_str());
397-
398-
std::vector<double> vdata(3);
248+
multiple_from_string(line,ivalues);
399249

400-
for (int i = 0; i < num_nodes && getline(inputfile,line,'\n'); ++i)
401-
{
402-
if (line.size() > 0)
250+
for (size_t j=0; j<ivalues.size() && j<4; j++)
403251
{
404-
// block out comments
405-
if ((line[0] == '#')||(line[0] == '%')) continue;
252+
vdata[j] = ivalues[j];
406253
}
407254

408-
// replace comma's and tabs with white spaces
409-
for (size_t p = 0;p<line.size();p++)
410-
{
411-
if ((line[p] == '\t')||(line[p] == ',')||(line[p]=='"')) line[p] = ' ';
412-
}
255+
fvalues.push_back(ivalues[ivalues.size()-1]);
413256

414-
multiple_from_string(line,values);
257+
mesh->add_elem(vdata);
415258

416-
if (values.size() == 3) mesh->add_point(Point(values[0],values[1],values[2]));
417-
if (values.size() == 2) mesh->add_point(Point(values[0],values[1],0.0));
418259
}
419260
}
420261
catch (...)
421262
{
422-
if (pr) pr->error("Could not open and process file: " + pts_fn);
263+
if (pr) pr->error("Could not open and process file: " + elems_fn);
423264
return (result);
424265
}
425266
inputfile.close();
426267
}
427268

428-
std::vector<double> fvalues;
269+
270+
// Points file
429271

430272
{
431273
std::ifstream inputfile;
432274
inputfile.exceptions( std::ifstream::badbit );
433275

434276
try
435277
{
436-
inputfile.open(elems_fn.c_str());
437-
438-
VMesh::Node::array_type vdata;
439-
vdata.resize(4);
278+
inputfile.open(pts_fn.c_str());
440279

441-
std::vector<VMesh::index_type> ivalues;
280+
std::vector<double> vdata(3);
281+
getline(inputfile,line,'\n');
282+
multiple_from_string(line,values);
283+
num_nodes=static_cast<int>(values[0]);
442284

443-
for (int i = 0; i < num_elems && getline(inputfile,line,'\n'); ++i)
444-
{
445-
if (line.size() > 0)
446-
{
447-
// block out comments
448-
if ((line[0] == '#')||(line[0] == '%')) continue;
449-
}
285+
for (int i = 0; i < num_nodes && getline(inputfile,line,'\n'); ++i) {
450286

451-
// replace comma's and tabs with white spaces
452-
for (size_t p = 0;p<line.size();p++)
453-
{
454-
if ((line[p] == '\t')||(line[p] == ',')||(line[p]=='"')) line[p] = ' ';
455-
}
287+
multiple_from_string(line, values);
456288

457-
multiple_from_string(line,ivalues);
458-
for (size_t j=0; j<ivalues.size() && j<4; j++)
459-
{
460-
if (zero_based) vdata[j] = ivalues[j];
461-
else vdata[j] = ivalues[j]-1;
462-
}
463-
if (ivalues.size() > 4) fvalues.push_back(ivalues[4]);
289+
if (values.size() == 3) mesh->add_point(Point(values[0], values[1], values[2]));
464290

465-
if (ivalues.size() > 3) mesh->add_elem(vdata);
466291
}
467292
}
468293
catch (...)
469294
{
470-
if (pr) pr->error("Could not open and process file: " + elems_fn);
295+
if (pr) pr->error("Could not open and process file: " + pts_fn);
471296
return (result);
472297
}
473298
inputfile.close();
474299
}
475300

476-
if (has_data)
477-
{
478301
field->resize_values();
479302
field->set_values(fvalues);
480-
}
303+
481304
return (result);
482305
}
483306

src/Core/IEPlugin/IEPluginInit.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void IEPluginManager::Initialize()
8080
static FieldIEPluginLegacyAdapter TriSurfFieldToExotxtBaseIndexOne_plugin("TriSurfFieldToExotxt[BaseIndex 1]", "*.ex2", "", nullptr, TriSurfFieldToExotxtBaseIndexOne_writer);
8181

8282
static FieldIEPluginLegacyAdapter TetVolField_plugin("TetVolField","*.elem *.tet *.pts *.pos", "", TextToTetVolField_reader, TetVolFieldToTextBaseIndexZero_writer);
83-
static FieldIEPluginLegacyAdapter CARPMesh_plugin("CARPMesh","*.elem *.pts *.lon", "", nullptr, CARPMesh_writer);
83+
static FieldIEPluginLegacyAdapter CARPMesh_plugin("CARPMesh","*.elem *.pts *.lon", "", CARPMesh_reader, CARPMesh_writer);
8484
static FieldIEPluginLegacyAdapter CARPFiber_plugin("CARPFiber","*.lon", "", nullptr, CARPFiber_writer);
8585
static FieldIEPluginLegacyAdapter TetVolFieldBaseIndexOne_plugin("TetVolField[BaseIndex 1]", "*.tet *.pts", "", nullptr, TetVolFieldToTextBaseIndexOne_writer);
8686
static FieldIEPluginLegacyAdapter JHU_elemsPtsFileToTetVol_plugin("JHUFileToTetVol","*.elem *.tet *.pts *.pos", "", TextToTetVolField_reader, nullptr);

0 commit comments

Comments
 (0)