Skip to content
Snippets Groups Projects
Commit 3ada8d22 authored by Christophe Geuzaine's avatar Christophe Geuzaine
Browse files

encapsulate serialized varray header parsing in VertexArraay::decodeHeader

parent 21acb6c8
Branches
Tags
No related merge requests found
......@@ -100,22 +100,13 @@ static void computeAndSendVertexArrays()
}
// Merge the vertex arrays
void addToVertexArrays(const char* bytes, int len)
static void addToVertexArrays(int length, const char* bytes, int swap)
{
int is = sizeof(int), ds = sizeof(double);
std::string name;
int index = 0;
int num; memcpy(&num, &bytes[index], is); index += is;
int ss; memcpy(&ss, &bytes[index], is); index += is;
if(ss){
std::vector<char> n(ss);
memcpy(&n[0], &bytes[index], ss); index += ss;
for(unsigned int i = 0; i < n.size(); i++) name += n[i];
}
int type; memcpy(&type, &bytes[index], is); index += is;
int num, type, numSteps;
double min, max, time, xmin, ymin, zmin, xmax, ymax, zmax;
int index = VertexArray::decodeHeader(length, bytes, swap, name, num, type, min, max,
numSteps, time, xmin, ymin, zmin, xmax, ymax, zmax);
PView *p = PView::list[num - 1];
PViewData *data = p->getData();
......@@ -126,17 +117,6 @@ void addToVertexArrays(const char* bytes, int len)
VertexArray *va = varrays[type - 1];
double min; memcpy(&min, &bytes[index], ds); index += ds;
double max; memcpy(&max, &bytes[index], ds); index += ds;
int numsteps; memcpy(&numsteps, &bytes[index], is); index += is;
double time; memcpy(&time, &bytes[index], ds); index += ds;
double xmin; memcpy(&xmin, &bytes[index], ds); index += ds;
double ymin; memcpy(&ymin, &bytes[index], ds); index += ds;
double zmin; memcpy(&zmin, &bytes[index], ds); index += ds;
double xmax; memcpy(&xmax, &bytes[index], ds); index += ds;
double ymax; memcpy(&ymax, &bytes[index], ds); index += ds;
double zmax; memcpy(&zmax, &bytes[index], ds); index += ds;
if (data->getMin() > min) data->setMin(min);
if (data->getMax() < max) data->setMax(max);
......@@ -148,7 +128,7 @@ void addToVertexArrays(const char* bytes, int len)
if (type == 4) type = 2;
VertexArray* toAdd = new VertexArray(type, 100);
toAdd->fromChar(bytes,0);
toAdd->fromChar(length, bytes, swap);
va->merge(toAdd);
delete toAdd;
}
......@@ -226,7 +206,7 @@ int GmshRemote()
char str[len];
MPI_Recv(str, len, MPI_CHAR, status.MPI_SOURCE,
MPI_GMSH_VARRAY, MPI_COMM_WORLD, &status2);
addToVertexArrays(str,len);
addToVertexArrays(len, str, swap);
}
}
computeAndSendVertexArrays(client, false);
......
......@@ -252,32 +252,56 @@ char *VertexArray::toChar(int num, std::string name, int type, double min, doubl
return bytes;
}
void VertexArray::fromChar(const char *bytes, int swap)
int VertexArray::decodeHeader(int length, const char *bytes, int swap,
std::string &name, int &num, int &type,
double &min, double &max, int &numSteps, double &time,
double &xmin, double &ymin, double &zmin,
double &xmax, double &ymax, double &zmax)
{
// FIXME deal with swap
int is = sizeof(int), ds = sizeof(double);
int is = sizeof(int), ds = sizeof(double), index = 0;
if(length < 4 * is + 9 * ds){
Msg::Error("Too few bytes to create vertex array: %d", length);
return 0;
}
int num; memcpy(&num, &bytes[index], is); index += is;
if(swap){
Msg::Error("Should swap bytes in vertex array--not implemented yet");
return 0;
}
int index = 0;
memcpy(&num, &bytes[index], is); index += is;
int ss; memcpy(&ss, &bytes[index], is); index += is;
if(ss){
std::vector<char> name(ss);
memcpy(&name[0], &bytes[index], ss); index += ss;
}
int type; memcpy(&type, &bytes[index], is); index += is;
double min; memcpy(&min, &bytes[index], ds); index += ds;
double max; memcpy(&max, &bytes[index], ds); index += ds;
int numsteps; memcpy(&numsteps, &bytes[index], is); index += is;
double time; memcpy(&time, &bytes[index], ds); index += ds;
double xmin; memcpy(&xmin, &bytes[index], ds); index += ds;
double ymin; memcpy(&ymin, &bytes[index], ds); index += ds;
double zmin; memcpy(&zmin, &bytes[index], ds); index += ds;
double xmax; memcpy(&xmax, &bytes[index], ds); index += ds;
double ymax; memcpy(&ymax, &bytes[index], ds); index += ds;
double zmax; memcpy(&zmax, &bytes[index], ds); index += ds;
std::vector<char> n(ss);
memcpy(&n[0], &bytes[index], ss); index += ss;
for(unsigned int i = 0; i < n.size(); i++) name += n[i];
}
memcpy(&type, &bytes[index], is); index += is;
memcpy(&min, &bytes[index], ds); index += ds;
memcpy(&max, &bytes[index], ds); index += ds;
memcpy(&numSteps, &bytes[index], is); index += is;
memcpy(&time, &bytes[index], ds); index += ds;
memcpy(&xmin, &bytes[index], ds); index += ds;
memcpy(&ymin, &bytes[index], ds); index += ds;
memcpy(&zmin, &bytes[index], ds); index += ds;
memcpy(&xmax, &bytes[index], ds); index += ds;
memcpy(&ymax, &bytes[index], ds); index += ds;
memcpy(&zmax, &bytes[index], ds); index += ds;
return index;
}
void VertexArray::fromChar(int length, const char *bytes, int swap)
{
std::string name;
int num, type, numSteps;
double min, max, time, xmin, ymin, zmin, xmax, ymax, zmax;
int index = decodeHeader(length, bytes, swap, name, num, type, min, max,
numSteps, time, xmin, ymin, zmin, xmax, ymax, zmax);
if(!index) return;
int is = sizeof(int);
int vn; memcpy(&vn, &bytes[index], is); index += is;
if(vn){
_vertices.resize(vn); int vs = vn * sizeof(float);
......@@ -297,15 +321,13 @@ void VertexArray::fromChar(const char *bytes, int swap)
}
}
void VertexArray::merge(VertexArray* arr) {
if(arr->getNumVertices() != 0) {
_vertices.insert(_vertices.end(),arr->firstVertex(),
arr->lastVertex());
_normals.insert(_normals.end(),arr->firstNormal(),
arr->lastNormal());
_colors.insert(_colors.end(),arr->firstColor(),
arr->lastColor());
_elements.insert(_elements.end(),arr->firstElementPointer(),
arr->lastElementPointer());
void VertexArray::merge(VertexArray* va)
{
if(va->getNumVertices() != 0) {
_vertices.insert(_vertices.end(), va->firstVertex(), va->lastVertex());
_normals.insert(_normals.end(), va->firstNormal(), va->lastNormal());
_colors.insert(_colors.end(), va->firstColor(), va->lastColor());
_elements.insert(_elements.end(), va->firstElementPointer(),
va->lastElementPointer());
}
}
......@@ -178,9 +178,14 @@ class VertexArray{
// network)
char *toChar(int num, std::string name, int type, double min, double max,
int numsteps, double time, SBoundingBox3d bbox, int &len);
void fromChar(const char *bytes, int swap);
void fromChar(int length, const char *bytes, int swap);
static int decodeHeader(int length, const char *bytes, int swap,
std::string &name, int &num, int &type,
double &min, double &max, int &numSteps, double &time,
double &xmin, double &ymin, double &zmin,
double &xmax, double &ymax, double &zmax);
// merge another vertex array into this one
void merge(VertexArray* arr);
void merge(VertexArray *va);
};
#endif
......@@ -1145,39 +1145,12 @@ void PView::fillVertexArrays()
void PView::fillVertexArray(ConnectionManager *remote, int length,
const char *bytes, int swap)
{
int is = sizeof(int), ds = sizeof(double);
if(length < 4 * is + 9 * ds){
Msg::Error("Too few bytes to create vertex array: %d", length);
return;
}
if(swap){
Msg::Error("Should swap bytes in vertex array--not implemented yet");
return;
}
std::string name;
int index = 0;
int num; memcpy(&num, &bytes[index], is); index += is;
int ss; memcpy(&ss, &bytes[index], is); index += is;
if(ss){
std::vector<char> n(ss);
memcpy(&n[0], &bytes[index], ss); index += ss;
for(unsigned int i = 0; i < n.size(); i++) name += n[i];
}
int type; memcpy(&type, &bytes[index], is); index += is;
double min; memcpy(&min, &bytes[index], ds); index += ds;
double max; memcpy(&max, &bytes[index], ds); index += ds;
int numsteps; memcpy(&numsteps, &bytes[index], is); index += is;
double time; memcpy(&time, &bytes[index], ds); index += ds;
double xmin; memcpy(&xmin, &bytes[index], ds); index += ds;
double ymin; memcpy(&ymin, &bytes[index], ds); index += ds;
double zmin; memcpy(&zmin, &bytes[index], ds); index += ds;
double xmax; memcpy(&xmax, &bytes[index], ds); index += ds;
double ymax; memcpy(&ymax, &bytes[index], ds); index += ds;
double zmax; memcpy(&zmax, &bytes[index], ds); index += ds;
int num, type, numSteps;
double min, max, time, xmin, ymin, zmin, xmax, ymax, zmax;
if(!VertexArray::decodeHeader(length, bytes, swap, name, num, type, min, max,
numSteps, time, xmin, ymin, zmin, xmax, ymax, zmax))
return;
Msg::Debug("Filling vertex array (type %d) in view num %d", type, num);
......@@ -1186,7 +1159,7 @@ void PView::fillVertexArray(ConnectionManager *remote, int length,
PView *p = PView::getViewByNum(num);
if(!p){
Msg::Info("View num %d does not exist: creating new view", num);
PViewData *data = new PViewDataRemote(remote, min, max, numsteps, time, bbox);
PViewData *data = new PViewDataRemote(remote, min, max, numSteps, time, bbox);
data->setName(name + " (remote)");
p = new PView(data, num);
SetBoundingBox();
......@@ -1207,22 +1180,22 @@ void PView::fillVertexArray(ConnectionManager *remote, int length,
case 1:
if(p->va_points) delete p->va_points;
p->va_points = new VertexArray(1, 100);
p->va_points->fromChar(bytes, swap);
p->va_points->fromChar(length, bytes, swap);
break;
case 2:
if(p->va_lines) delete p->va_lines;
p->va_lines = new VertexArray(2, 100);
p->va_lines->fromChar(bytes, swap);
p->va_lines->fromChar(length, bytes, swap);
break;
case 3:
if(p->va_triangles) delete p->va_triangles;
p->va_triangles = new VertexArray(3, 100);
p->va_triangles->fromChar(bytes, swap);
p->va_triangles->fromChar(length, bytes, swap);
break;
case 4:
if(p->va_vectors) delete p->va_vectors;
p->va_vectors = new VertexArray(2, 100);
p->va_vectors->fromChar(bytes, swap);
p->va_vectors->fromChar(length, bytes, swap);
break;
default:
Msg::Error("Cannot fill vertex array of type %d", type);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment