the function below is published by FP system and used in maxscirpt
here is the code
bool MeshFns::meshFromPts(Mesh* pMs,Tab<Point3*>*pts, int rows, Point2* uv) { int nmvts = pts->Count(); int colums = nmvts / rows; pMs->setNumVerts(nmvts); for (int i = 0; i < nmvts; i++){ pMs->setVert(i, *(*pts)[i]); } ////TV verts/// pMs->setNumTVerts(nmvts); float* udisls = new float[colums]; float* vdisls = new float[rows]; udisls[0] = 0.0f; vdisls[0] = 0.0f; float disU = 0.0f; float disV = 0.0f; float uwid = uv->x, vwid = uv->y; for (int i = 1; i < rows; i++) { disV += (*(*pts)[i*colums] - *(*pts)[(i - 1)*colums]).Length(); vdisls[i] = disV; } for (int j = 1; j < colums; j++){ disU += (*(*pts)[j] - *(*pts)[j - 1]).Length(); udisls[j] = disU; } for (int i = 0; i < rows; i++){ for (int j = 0; j < colums; j++){ pMs->setTVert(i*colums + j, udisls[j] / uwid, vdisls[i] / vwid, 0.0f); } } ////end tv verts// int numfcs = 2 * (rows - 1)*(colums - 1); int n = 0; pMs->setNumFaces(numfcs); pMs->setNumTVFaces(numfcs); for (int i = 0; i < rows - 1; i++){ for (int j = 0; j < colums - 1; j++){ pMs->faces[n].setVerts(i*colums + j, i*colums + j + 1,(i + 1)*colums + j ); pMs->faces[n].setEdgeVisFlags(1, 0, 1); pMs->tvFace[n].setTVerts(i*colums + j, i*colums + j + 1, (i + 1)*colums + j); pMs->faces[n].setSmGroup(1); n += 1; pMs->faces[n].setVerts((i + 1)*colums + j + 1, (i + 1)*colums + j,i*colums + j + 1); pMs->faces[n].setEdgeVisFlags(1, 0, 1); pMs->tvFace[n].setTVerts((i + 1)*colums + j + 1, (i + 1)*colums + j, i*colums + j + 1); pMs->faces[n].setSmGroup(1); n += 1; } } delete[] udisls; delete[] vdisls; return true; }
this function can work ,but after run several times ,the speed obviously become slow ,I have to call gc() to clear memory.
I also write another function with MAXScript in same way,but the ms function run faster than the function before writed with C++.
I think there is someting wrong.
Could you tell me how to improve the function?
thank you
Your c++ function looks like a grid-mesh to me ... I would make a grid-mesh using built-in methods and move the vertices to the specified positions, which is easy to do with memcpy.
About MXS performance ... Looks like the published function is doing something wrong and causing a memory leak. As it was correctly noted above ... before we suggest anything useful, we need to first look at how your MXS function is used in code.
I agree with the others - w/o seeing the other code impossible to say..
But one thing is already noticeable - ihmo: the parameter: "Tab<Point3 *> * pts".
How you're filling out this array? Why you cannot use "Tab<Point3> * pts" instead?