diff --git a/Common/GmshRemote.cpp b/Common/GmshRemote.cpp index 508d93e3fe95879388c6d11cf3331d7e67043f06..a836eb060fafb0e8b7978aa298e846d924deddf9 100644 --- a/Common/GmshRemote.cpp +++ b/Common/GmshRemote.cpp @@ -100,42 +100,22 @@ 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 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); - 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; - - PView *p = PView::list[num-1]; + PView *p = PView::list[num - 1]; PViewData *data = p->getData(); PViewOptions *opt = p->getOptions(); VertexArray *varrays[4] = {p->va_points, p->va_lines, p->va_triangles, p->va_vectors}; - 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; + VertexArray *va = varrays[type - 1]; if (data->getMin() > min) data->setMin(min); if (data->getMax() < max) data->setMax(max); @@ -147,8 +127,8 @@ void addToVertexArrays(const char* bytes, int len) data->setBoundingBox(bb); if (type == 4) type = 2; - VertexArray* toAdd = new VertexArray(type,100); - toAdd->fromChar(bytes,0); + VertexArray* toAdd = new VertexArray(type, 100); + 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); diff --git a/Common/VertexArray.cpp b/Common/VertexArray.cpp index bbe8837253ec46e9ca030ee5ba233fc91a7fc198..2256e5f3e48afc903f1268f8c99e5e3c47d58a4f 100644 --- a/Common/VertexArray.cpp +++ b/Common/VertexArray.cpp @@ -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), index = 0; - - int num; memcpy(&num, &bytes[index], is); index += is; - + 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 0; + } + + 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; + 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; +} - 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; - +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()); } } diff --git a/Common/VertexArray.h b/Common/VertexArray.h index d84549f3fe84b310849b0009ec88539d9a9c06af..9e244a00a58609c3b221dc27519ecd8141922227 100644 --- a/Common/VertexArray.h +++ b/Common/VertexArray.h @@ -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 diff --git a/Post/PViewVertexArrays.cpp b/Post/PViewVertexArrays.cpp index b477e77000aa5322baf8440000900d1917ac9506..6b50e7cb119f118e585ba0bc5bb5a37943a62c42 100644 --- a/Post/PViewVertexArrays.cpp +++ b/Post/PViewVertexArrays.cpp @@ -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);