/* Assuming the file exists and is an acyclic graph, print the vertices in a topological order * Use the algorithm that finds no-predecessor vertices and deletes their successor edges. * ALERT: modifies the graph by deleting edges. * * Solves topocycleExercise.c */ void toposort2(GraphInfo gi) { Graph g = gi->graph; int numV = numVerts(g); short done[numV]; // 1 if vertex already printed, otherwise 0 for (int i = 0; i < numV; i++) done[i] = 0; for (int numPrinted = 0; numPrinted < numV; numPrinted++) { /* set noPred to a vertex with no predecessors and not already done */ int noPred; int* ps; // predecessor list for (int v = 0; v < numV; v++) { if (!done[v]) { ps = predecessors(g, v); if (ps[0] == -1 ) { // list is empty? noPred = v; break; } } } /* print noPred and mark as done */ printf("%s ", gi->vertnames[noPred]); done[noPred] = 1; /* get successors of noPred and delete them */ int* snoPred = successors(gi->graph, noPred); for (int i = 0; snoPred[i] != -1; i++) delEdge(gi->graph, noPred, snoPred[i]); } /* cleanup */ printf("\n"); }
Point Simplex::support(const Vector& v) const { int c = 0; Scalar h = dot((*this)[0], v), d; for (int i = 1; i < numVerts(); ++i) { if ((d = dot((*this)[i], v)) > h) { c = i; h = d; } } return (*this)[c]; }
/* Index of a given vertex name, or -1 if not found. */ int indexOf(GraphInfo gi, char* name) { int i = 0; int n = numVerts(gi->graph); while ( i < n && strcmp(gi->vertnames[i], name) ) i++; if (i < n) return i; else return -1; }
void TriMesh::buildHeatKernel(SparseMatrix& A, double timestep) const { VectorXd diag; computeVertArea(diag); std::map< std::pair<unsigned,unsigned>, double > offdiag; for (unsigned face = 0; face < numFaces(); ++face) { std::vector<unsigned> fVert; getFaceVerts(face, fVert); assert(fVert.size() == 3); for (unsigned i = 0; i < 3; ++i) { unsigned vert0 = fVert[(i+1)%3]; unsigned vert1 = fVert[(i+2)%3]; if (vert0 > vert1) std::swap(vert0, vert1); std::pair<unsigned,unsigned> edge(vert0,vert1); double value = timestep * 0.5 * computeFaceCotan(face, i); diag(vert0) += value; diag(vert1) += value; if (offdiag.find(edge) == offdiag.end()) offdiag[edge] = -value; else offdiag[edge] -= value; } } std::vector<Triplet> triplet; for(unsigned i = 0; i < diag.size(); ++i) { triplet.push_back(Triplet(i,i,diag(i))); } for(std::map< std::pair<unsigned,unsigned>, double >::const_iterator it = offdiag.begin(); it != offdiag.end(); ++it) { triplet.push_back(Triplet(it->first.first,it->first.second,it->second)); triplet.push_back(Triplet(it->first.second,it->first.first,it->second)); } A.resize(numVerts(), numVerts()); A.setFromTriplets(triplet.begin(),triplet.end()); }
void toposort2(char * filepath) { GraphInfo gi = readGraph(filepath, MATRIX); // DN's implementation supports precessors only for MATRIX Graph g = gi->graph; int numV = numVerts(g); /* YOUR CODE GOES HERE */ }
void TriMesh::computeVertNormals(std::vector<Vec3>& vertNormal) const { vertNormal = std::vector<Vec3>(numVerts(), Vec3::Zero()); for (unsigned face = 0; face < numFaces(); ++face) { std::vector<unsigned> fVert; getFaceVerts(face, fVert); assert(fVert.size() == 3); double faceArea = computeFaceArea(face); Vec3 faceNormal = computeFaceNormal(face); for (unsigned i = 0; i < 3; ++i) vertNormal[fVert[i]] += faceArea * faceNormal; } for (unsigned vert = 0; vert < numVerts(); ++vert) { vertNormal[vert].normalize(); } }
void TriMesh::computeVertArea(VectorXd& vertArea) const { vertArea = VectorXd::Zero(numVerts()); for (unsigned face = 0; face < numFaces(); ++face) { std::vector<unsigned> fVert; getFaceVerts(face, fVert); assert(fVert.size() == 3); double faceArea = computeFaceArea(face); for (unsigned i = 0; i < 3; ++i) vertArea(fVert[i]) += faceArea; } vertArea /= 3.0; }
void TriMesh::computeDivergence(const std::vector<Vec3>& faceVector, VectorXd& vertForm) const { vertForm = VectorXd::Zero(numVerts()); for (unsigned face = 0; face < numFaces(); ++face) { std::vector<unsigned> fVert; getFaceVerts(face, fVert); assert(fVert.size() == 3); Vec3 n = computeFaceNormal(face); for (unsigned i = 0; i < 3; ++i) { Vec3 p1 = getVertPos(fVert[(i+1)%3]); Vec3 p2 = getVertPos(fVert[(i+2)%3]); vertForm[fVert[i]] += 0.5*faceVector[face].dot(n.cross(p2-p1)); } } }
void FltExportVisitor::writeLocalVertexPool( const osg::Geometry& geom ) { // Attribute Mask static const unsigned int HAS_POSITION = 0x80000000u >> 0; // static const unsigned int HAS_COLOR_INDEX = 0x80000000u >> 1; static const unsigned int HAS_RGBA_COLOR = 0x80000000u >> 2; static const unsigned int HAS_NORMAL = 0x80000000u >> 3; static const unsigned int HAS_BASE_UV = 0x80000000u >> 4; static const unsigned int HAS_UV_LAYER1 = 0x80000000u >> 5; static const unsigned int HAS_UV_LAYER2 = 0x80000000u >> 6; static const unsigned int HAS_UV_LAYER3 = 0x80000000u >> 7; static const unsigned int HAS_UV_LAYER4 = 0x80000000u >> 8; static const unsigned int HAS_UV_LAYER5 = 0x80000000u >> 9; static const unsigned int HAS_UV_LAYER6 = 0x80000000u >> 10; static const unsigned int HAS_UV_LAYER7 = 0x80000000u >> 11; const osg::Array* v = geom.getVertexArray(); uint32 numVerts( v->getNumElements() ); osg::ref_ptr< const osg::Vec3dArray > v3 = VertexPaletteManager::asVec3dArray( v, numVerts ); if (!v3) { std::string warning( "fltexp: writeLocalVertexPool: VertexArray is not Vec3Array." ); osg::notify( osg::WARN ) << warning << std::endl; _fltOpt->getWriteResult().warn( warning ); return; } // Compute attribute bits and vertex size. const osg::Array* c = geom.getColorArray(); const osg::Array* n = geom.getNormalArray(); const osg::Array* t = geom.getTexCoordArray( 0 ); osg::ref_ptr< const osg::Vec4Array > c4 = VertexPaletteManager::asVec4Array( c, numVerts ); osg::ref_ptr< const osg::Vec3Array > n3 = VertexPaletteManager::asVec3Array( n, numVerts ); osg::ref_ptr< const osg::Vec2Array > t2 = VertexPaletteManager::asVec2Array( t, numVerts ); if (c && !c4) return; if (n && !n3) return; if (t && !t2) return; std::vector< osg::ref_ptr< const osg::Vec2Array > > mtc; mtc.resize( 8 ); int unit=1; for( ;unit<8; unit++) mtc[ unit ] = VertexPaletteManager::asVec2Array( geom.getTexCoordArray( unit ), numVerts ); uint32 attr( HAS_POSITION ); unsigned int vertSize( sizeof( float64 ) * 3 ); if ( ( c4 != NULL ) && ( geom.getColorBinding() == osg::Geometry::BIND_PER_VERTEX) ) { attr |= HAS_RGBA_COLOR; vertSize += sizeof( unsigned int ); } if ( ( n3 != NULL ) && ( geom.getNormalBinding() == osg::Geometry::BIND_PER_VERTEX) ) { attr |= HAS_NORMAL; vertSize += ( sizeof( float32 ) * 3 ); } if ( t2 != NULL ) { attr |= HAS_BASE_UV; vertSize += ( sizeof( float32 ) * 2 ); } // Add multitex if (isTextured( 1, geom )) { attr |= HAS_UV_LAYER1; vertSize += ( sizeof( float32 ) * 2 ); } if (isTextured( 2, geom )) { attr |= HAS_UV_LAYER2; vertSize += ( sizeof( float32 ) * 2 ); } if (isTextured( 3, geom )) { attr |= HAS_UV_LAYER3; vertSize += ( sizeof( float32 ) * 2 ); } if (isTextured( 4, geom )) { attr |= HAS_UV_LAYER4; vertSize += ( sizeof( float32 ) * 2 ); } if (isTextured( 5, geom )) { attr |= HAS_UV_LAYER5; vertSize += ( sizeof( float32 ) * 2 ); } if (isTextured( 6, geom )) { attr |= HAS_UV_LAYER6; vertSize += ( sizeof( float32 ) * 2 ); } if (isTextured( 7, geom )) { attr |= HAS_UV_LAYER7; vertSize += ( sizeof( float32 ) * 2 ); } unsigned int maxVerts = (0xffff - 12) / vertSize; unsigned int thisVertCount = (maxVerts > numVerts) ? numVerts : maxVerts; unsigned int currentIndexLimit = maxVerts; uint16 length( 12 + (vertSize * thisVertCount) ); _records->writeInt16( (int16) LOCAL_VERTEX_POOL_OP ); _records->writeUInt16( length ); _records->writeUInt32( numVerts ); // number of vertices _records->writeUInt32( attr ); // attribute bits unsigned int idx; for( idx=0; idx<numVerts; idx++ ) { _records->writeVec3d( (*v3)[ idx ] ); if (attr & HAS_RGBA_COLOR) { osg::Vec4 color = (*c4)[ idx ]; unsigned int packedColor = (int)(color[3]*255) << 24 | (int)(color[2]*255) << 16 | (int)(color[1]*255) << 8 | (int)(color[0]*255); _records->writeUInt32( packedColor ); } if (attr & HAS_NORMAL) _records->writeVec3f( (*n3)[ idx ] ); if (attr & HAS_BASE_UV) _records->writeVec2f( (*t2)[ idx ] ); if (attr & HAS_UV_LAYER1) _records->writeVec2f( (*mtc[1])[ idx ] ); if (attr & HAS_UV_LAYER2) _records->writeVec2f( (*mtc[2])[ idx ] ); if (attr & HAS_UV_LAYER3) _records->writeVec2f( (*mtc[3])[ idx ] ); if (attr & HAS_UV_LAYER4) _records->writeVec2f( (*mtc[4])[ idx ] ); if (attr & HAS_UV_LAYER5) _records->writeVec2f( (*mtc[5])[ idx ] ); if (attr & HAS_UV_LAYER6) _records->writeVec2f( (*mtc[6])[ idx ] ); if (attr & HAS_UV_LAYER7) _records->writeVec2f( (*mtc[7])[ idx ] ); // Handle continuation record if necessary. if ( (idx+1 == currentIndexLimit) && (idx+1 < numVerts) ) { currentIndexLimit += maxVerts; unsigned int remaining( numVerts - (idx+1) ); thisVertCount = (maxVerts > remaining) ? remaining : maxVerts; writeContinuationRecord( (vertSize * thisVertCount) ); } } }