mat Scheme::implicitScheme(int nSteps, double tSteps, double(*u_s)(double)) { //input: nSteps (# of interior points), time, tSteps, u_s(x) double deltaX = 1.0 / (nSteps + 1); double deltaT = 1.0 / tSteps; double alpha = deltaT / pow(deltaX,2); vec v(nSteps), vNext(nSteps), u(nSteps); int printIndex = tSteps / 10; mat M(printIndex, nSteps); int index = 0; for( int i = 0; i < nSteps; i++) { double x = (i + 1) * deltaX; vNext[i] = v[i] = -u_s(x); } // Boundary conditions v[0] = vNext[0] = v[nSteps] = vNext[nSteps] = 0; double a = -alpha; // diagonal element double b = 1 + 2*alpha; // off-diagonal element Solver solv = Solver(); for(int t = 1; t <= tSteps; t++) { solv.tridiagonalSolver(a, b, v, vNext); // Initial conditions vNext[0] = vNext[nSteps] = 0; v = vNext; for (int i = 0; i < nSteps; i++) { double x = (i + 1) * deltaX; u[i] = v[i] + u_s(x); } // Print to file ! if (t % 10 == 0) { for (int i = 0; i < nSteps; i++) M(index, i) = u[i]; index++; } } for (int i = 0; i < printIndex; i++) { for (int j = 0; j < nSteps; j++) printf("%f \t", M(i, j)); } return M; }
mat Scheme::explicitScheme(int nSteps, double tSteps, double(*u_s)(double)) { //input: nSteps , time, u_s(x) double deltaX = 1.0 / (float)(nSteps + 1); double alpha = 0.4; double deltaT = alpha * pow(deltaX, 2); //double tSteps = 1.0 / deltaT; vec v(nSteps), vNext(nSteps), u(nSteps); int printIndex = tSteps / 10; mat M(printIndex, nSteps); int index = 0; //Initialization for (int i = 1; i < nSteps; i++) { double x = (i + 1) * deltaX; v(i) = -u_s(x); } // Boundary conditions v[0] = vNext[0] = 0; v[nSteps] = vNext[nSteps] = 0; // for(int t = 1; t <= tSteps; t ++) { if ((t - 1) % 204 == 0) { for (int i = 0; i < nSteps; i++) printf("%f \t", v(i)); printf("\n"); } for(int i = 1; i < nSteps; i++ ) { vNext[i] = (1 - 2 * alpha) * v[i]; double truc = vNext[i]; // XX if( i > 0 ) vNext[i] += alpha * v[i-1]; // else += 0 if( i < nSteps ) vNext[i] += alpha * v[i+1]; // else += 0 } v = vNext; /*if ((t - 1) % 204 == 0) { for (int i = 0; i < nSteps; i++) printf("%f \t", v(i)); printf("\n"); }*/ for (int i = 0; i < nSteps; i++) { double x = (i + 1) * deltaX; u[i] = v[i] + u_s(x); } // Print to file ! if (t % 10 == 0) { for (int i = 0; i < nSteps; i++) M(index, i) = u[i]; index++; } } for (int i = 0; i < printIndex; i++) { for (int j = 0; j < nSteps; j++) printf("%f \t", M(i, j)); } return M; }
mat Scheme::crankNicolsonScheme(int nSteps, double tSteps, double(*u_s)(double)) { //input: nSteps (# of interior points), time, tSteps, u_s(x) double deltaX = 1.0 / (nSteps + 1); double deltaT = 1.0 / tSteps; double alpha = deltaT / pow(deltaX,2); vec v(nSteps), w(nSteps), u(nSteps); int printIndex = tSteps / 10; mat M(printIndex, nSteps); int index = 0; for(int i = 0; i < nSteps; i++) { double x = (i + 1) * deltaX; v[i] = -u_s(x); } // Boundary conditions v[0] = w[0] = v[nSteps] = w[nSteps] = 0; double a = 2 * (1 + alpha); // diagonal element double b = -alpha; // off-diagonal element Solver solv = Solver(); for(int t = 1; t <= tSteps; t++) { for( int i = 0; i < nSteps; i++ ) { w[i] = 2 * (1 - alpha) * v[i]; if( i > 0 ) w[i] += alpha * v[i - 1]; // else += 0 if( i < nSteps - 1 ) w[i] += alpha * v[i + 1]; // else += 0 } solv.tridiagonalSolver(a, b, w, v); // Boundary conditions: v[0] = v[nSteps] = 0; for (int i = 0; i < nSteps; i++) { double x = (i + 1) * deltaX; u[i] = v[i] + u_s(x); } // Print to file ! if (t % 10 == 0) { for (int i = 0; i < nSteps; i++) M(index, i) = u[i]; index++; } } for (int i = 0; i < printIndex; i++) { for (int j = 0; j < nSteps; j++) printf("%f \t", M(i, j)); } return M; }
void exportTerrain::printPolygonData(const MFnMesh &theMesh,const bool phy) { MStatus status; MPointArray vertices; MIntArray perPolyVertices, numVerticesPerPoly; MVector tNorm; MVectorArray perVertexNormals, normals; MItMeshPolygon itPoly( theMesh.object() ); long numPolygons = theMesh.numPolygons(); long numVertices = theMesh.numVertices(); theMesh.getPoints(vertices); fout << "Vertices: " << endl; fout << "Number of Vertices: " << numVertices << endl; fout << vertices << endl; fout << "Number of polygons: " << numPolygons << endl; fout << "Polygon Connection List:" << endl; fout << "["; for(int i = 0; i < numPolygons - 1; ++i){ fout << i << ": "; status = theMesh.getPolygonVertices(i, perPolyVertices); fout << perPolyVertices; fout << ", " << endl; } //last is a special case fout << numPolygons -1 << ": "; status = theMesh.getPolygonVertices(numPolygons -1, perPolyVertices); fout << perPolyVertices; fout << "]" << endl; //Per Vertex Normals for(i=0; i < numVertices; ++i){ theMesh.getVertexNormal( i, tNorm); perVertexNormals.append(tNorm); } fout << "Per Vertex Normals: \n"; fout << perVertexNormals << endl; /* //per vertex per polygon Normals //Not supported by the Reaper graphic engine fout << "Normals" << endl; i = 0; fout << "[ "; while (!itPoly.isDone() ){ itPoly.getNormals(normals); fout << i << ": " << normals << endl; ++i; itPoly.next(); } fout << " ]" << endl; */ if( !phy ){ //Texture coordinate information MIntArray ids; int index; MFloatArray Us, Vs; theMesh.getUVs(Us,Vs); MFloatArray u_s(numVertices),v_s(numVertices); MIntArray numuvs(numVertices,0); //Now the tactic is: //Iterate over all polygons, and for each vertex read out the UV out of the list //Insert that in two float arrays, and keep track of how many uvs //that have been inserted. At last divd the us and vs by the number you //already go there //Shared uvs will look strange, but it is the average uvs that is shown //Make sure to model using no shared uvs for(i = 0;i<numPolygons;++i){ MIntArray polyVertices; theMesh.getPolygonVertices(i,polyVertices); for(int j = 0;j<polyVertices.length();++j){ int uv_id; theMesh.getPolygonUVid(i,j,uv_id); u_s[polyVertices[j]] += Us[uv_id]; v_s[polyVertices[j]] += Vs[uv_id]; numuvs[polyVertices[j]]+=1; } } for(int k = 0;k<numVertices;++k){ u_s[k] /= numuvs[k]; v_s[k] /= numuvs[k]; } fout << "Texture Coordinates: \n"; fout << "[" ; for(index = 0;index < (u_s.length() - 1); ++index){ fout << index << ": [" << u_s[index] << ", " << v_s[index] << "]," << endl; } //last is a special case fout << index << ": [" << u_s[index] << ", " << v_s[index] << "]"; fout << "]" << endl; //Color data, for coloring. Make sure MItMeshVertex vertexIt(theMesh.object()); MColor color; std::vector<MColor> colorArray; while (!vertexIt.isDone() ){ vertexIt.getColor(color); colorArray.push_back(color); vertexIt.next(); } fout << "PerVertexColors: \n"; fout << "[" ; for(index = 0;index < colorArray.size()-1; ++index){ fout << index << ": " << colorArray[index] << "," << endl; } //last is a special case fout << index << ": " << colorArray[index]; fout << "]" << endl; } }