Thursday, 19 September 2013

c++ read in csv file and manipulate data then print error

c++ read in csv file and manipulate data then print error

This is a continuation for my problem here: c++ reading in text file into
vector<vector> then writing to vector or array depending on first word in
internal vector .Im reading in a file and the using the values of node
coordinates to calc cell centres and want to pring the cell centres file
with Headers: ID,X,Y,Z, with Z all 0s.
Code so far:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>
std::vector<double> GetValues(const std::vector<std::string>& src, int
start, int end)
{
std::vector<double> ret;
for(int i = start; i <= end; ++i)
{
ret.push_back(std::strtod(src[i].c_str(), nullptr));
}
return ret;
}
std::vector<double> polycentre(const std::vector<double>& x,const
std::vector<double>& y,int ID)
{
std::vector<double> C(3, 0);
std::vector<double> x1(x.size(),0);
std::vector<double> y1(y.size(),0);
int sizx = x.size();
int sizy = y.size();
if(sizy != sizx)
{
std::cerr << "polycentre inputs not equal length";
}
double x0 = x[0];
double y0 = y[0];
for(int aa = 1; aa < sizx; ++aa)
{
if(x[aa] < x0){x0 = x[aa];}
if(y[aa] < y0){y0 = y[aa];}
}
double A = 0.0;
double B = 0.0;
for(int aa = 0; aa < sizx; ++aa)
{
x1[aa] = x[aa] - x0;
y1[aa] = y[aa] - x0;
if(aa != sizx-1)
{
A = A + (x1[aa]*y1[aa+1] - x1[aa+1]*y1[aa]);
B = B + ((x1[aa]+x1[aa+1])*(x1[aa]*y1[aa-1]-x1[aa-1]*y1[aa]));
}
else if(aa == sizx-1)
{
A = A + (x1[aa] - y1[aa]);
B = B + ((x1[aa]+1)*(x1[aa]*1-1*y1[aa]));
}
}
A = A*0.5;
C[0] = ID;
C[1] = ((1/6/A)*B)+x0;
C[2] = ((1/6/A)*B)+y0;
return C;
}
void PrintValues(const std::string& title,
std::vector<std::vector<double>>& v)
{
std::cout << title << std::endl;
for(size_t line = 0; line < v.size(); ++line)
{
for(size_t val = 0; val < v[line].size(); ++val)
{
std::cout << v[line][val] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
int main()
{
std::vector<std::vector<std::string>> values;
std::ifstream fin("example.2dm");
for (std::string line; std::getline(fin, line); )
{
std::istringstream in(line);
values.push_back(
std::vector<std::string>(std::istream_iterator<std::string>(in),
std::istream_iterator<std::string>()));
}
std::vector<std::vector<double>> cells;
std::vector<std::vector<double>> nodes;
for (size_t i = 0; i < values.size(); ++i)
{
if(values[i][0] == "E3T")
{
cells.push_back(GetValues(values[i], 1, 5));
}
else if(values[i][0] == "E4Q")
{
cells.push_back(GetValues(values[i], 1, 6));
}
else if(values[i][0] == "ND")
{
nodes.push_back(GetValues(values[i], 1, 4));
}
}
std::vector<std::vector<double>> cell_centres;
for (size_t aa = 0; aa < cells.size(); ++aa)
{
if(cells[aa].size() == 4)
{
std::vector<double> xs = {&nodes[cells[aa][1]][1],
&nodes[cells[aa][2]][1], &nodes[cells[aa][3]][1]};
std::vector<double> ys = {&nodes[cells[aa][1]][2],
&nodes[cells[aa][2]][2], &nodes[cells[aa][3]][2]};
cell_centres.push_back(polycentre(xs,ys,aa));
}
}
PrintValues("Cell Centres", cell_centres);
PrintValues("Cells", cells);
PrintValues("Nodes", nodes);
return 0;
}
Im getting the following compilation error:
$ g++ cell_centres.cpp -std=c++11
cell_centres.cpp: In function 'int main()':
cell_centres.cpp:110:105: error: could not convert '{(&(&
nodes.std::vector<_Tp, _Alloc>::operator[]<std::vector<double>,
std::allocator<std::vector<double> > >((std::vector<std::vector<double,
std::allocator<double> >, std::allocator<std::vector<double,
std::allocator<double> > > >::size_type)*((std::value_type<double,
std::allocator<double> >*)cells.operator[]()(aa))->std::vector<double,
std::allocator<double> >::operator[]()(1u)))->std::vector<_Tp,
_Alloc>::operator[]<double, std::allocator<double> >(1u)), (&(&
nodes.std::vector<_Tp, _Alloc>::operator[]<std::vector<double>,
std::allocator<std::vector<double> > >((std::vector<std::vector<double,
std::allocator<double> >, std::allocator<std::vector<double,
std::allocator<double> > > >::size_type)*((std::value_type<double,
std::allocator<double> >*)cells.operator[]()(aa))->std::vector<double,
std::allocator<double> >::operator[]()(2u)))->std::vector<_Tp,
_Alloc>::operator[]<double, std::allocator<double> >(1u)), (&(&
nodes.std::vector<_Tp, _Alloc>::operator[]<std::vector<double>,
std::allocator<std::vector<double> > >((std::vector<std::vector<double,
std::allocator<double> >, std::allocator<std::vector<double,
std::allocator<double> > > >::size_type)*((std::value_type<double,
std::allocator<double> >*)cells.operator[]()(aa))->std::vector<double,
std::allocator<double> >::operator[]()(3u)))->std::vector<_Tp,
_Alloc>::operator[]<double, std::allocator<double> >(1u))}' from
'<brace-enclosed initializer list>' to 'std::vector<double>'
cell_centres.cpp:111:105: error: could not convert '{(&(&
nodes.std::vector<_Tp, _Alloc>::operator[]<std::vector<double>,
std::allocator<std::vector<double> > >((std::vector<std::vector<double,
std::allocator<double> >, std::allocator<std::vector<double,
std::allocator<double> > > >::size_type)*((std::value_type<double,
std::allocator<double> >*)cells.operator[]()(aa))->std::vector<double,
std::allocator<double> >::operator[]()(1u)))->std::vector<_Tp,
_Alloc>::operator[]<double, std::allocator<double> >(2u)), (&(&
nodes.std::vector<_Tp, _Alloc>::operator[]<std::vector<double>,
std::allocator<std::vector<double> > >((std::vector<std::vector<double,
std::allocator<double> >, std::allocator<std::vector<double,
std::allocator<double> > > >::size_type)*((std::value_type<double,
std::allocator<double> >*)cells.operator[]()(aa))->std::vector<double,
std::allocator<double> >::operator[]()(2u)))->std::vector<_Tp,
_Alloc>::operator[]<double, std::allocator<double> >(2u)), (&(&
nodes.std::vector<_Tp, _Alloc>::operator[]<std::vector<double>,
std::allocator<std::vector<double> > >((std::vector<std::vector<double,
std::allocator<double> >, std::allocator<std::vector<double,
std::allocator<double> > > >::size_type)*((std::value_type<double,
std::allocator<double> >*)cells.operator[]()(aa))->std::vector<double,
std::allocator<double> >::operator[]()(3u)))->std::vector<_Tp,
_Alloc>::operator[]<double, std::allocator<double> >(2u))}' from
'<brace-enclosed initializer list>' to 'std::vector<double>'
could someone tell me where I went wrong??
BTW the MATLAB code is:
function cell_centres(infil,outfil)
% read 2DM file
MESH = RD2DM(infil);
% get cell centres
if (isfield(MESH,'E3T'))
ne3 = length(MESH.E3T);
else
ne3 = 0;
end
if (isfield(MESH,'E4Q'))
ne4 = length(MESH.E4Q);
else
ne4 = 0;
end
ne = ne3 + ne4;
ctrd = zeros(ne,2);
id = zeros(ne,1);
z = zeros(ne,1);
k = 1;
if (isfield(MESH,'E3T'))
for i = 1:length(MESH.E3T)
pts = MESH.E3T(i,2:4);
x = MESH.ND(pts,2);
y = MESH.ND(pts,3);
z(k) = mean(MESH.ND(pts,4));
ctrd(k,:) = polycentre(x,y);
id(k) = MESH.E3T(i,1);
k = k+1;
end
end
if (isfield(MESH,'E4Q'))
for i = 1:length(MESH.E4Q)
pts = MESH.E4Q(i,2:5);
x = MESH.ND(pts,2);
y = MESH.ND(pts,3);
z(k) = mean(MESH.ND(pts,4));
ctrd(k,:) = polycentre(x,y);
id(k) = MESH.E4Q(i,1);
k = k+1;
end
end
% order cell ids
[id i] = sort(id,'ascend');
ctrd = ctrd(i,:);
z = z(i);
% write .csv file
fid = fopen(outfil,'w');
fprintf(fid,'%s\n','ID,X,Y,Z');
for aa = 1:ne
fprintf(fid,'%i,%.7f,%.7f,%.7f\n',id(aa),ctrd(aa,1),ctrd(aa,2),z(aa));
end
fclose(fid);
display('done & done :-)')
Cheers

No comments:

Post a Comment