void LD3dsExporter::writeTriangle( VertexVector &vecVertices, FaceVector &vecFaces, const TCVector *points, int i0, int i1, int i2, int colorNumber, const TCFloat *matrix) { int ix[3]; int voffset = (int)vecVertices.size(); int foffset = (int)vecFaces.size(); ix[0] = i0; ix[1] = i1; ix[2] = i2; vecVertices.resize(vecVertices.size() + 3); vecFaces.resize(vecFaces.size() + 1); for (int i = 0; i < 3; i++) { TCVector vector = points[ix[i]]; vector = vector.transformPoint(matrix); vecVertices[voffset + i].v[0] = vector[0]; vecVertices[voffset + i].v[1] = vector[1]; vecVertices[voffset + i].v[2] = vector[2]; vecFaces[foffset].index[i] = (unsigned short)(voffset + i); vecFaces[foffset].material = getMaterial(colorNumber); } }
double TravelingSalesmanProblemAlgorithm::getLength(const VertexVector& workingVector) { double totalDistance = 0; // distance between verices in vector for (size_t i = 0; i < workingVector.size() ; ++i) { totalDistance += getLength(workingVector[i], workingVector[i + 1]); } // + distance between the first one and the last one verices. totalDistance += getLength(workingVector[0], workingVector[workingVector.size()-1]); return totalDistance; }
void Delaunay::addBounding( const VertexVector &verSet, TriangleVector &triSet ) { double max_x = -100000, max_y = -100000; double min_x = 100000, min_y = 100000; for (int i = 0; i < verSet.size(); i++) { Vec3d v = verSet[i].m_xyz; max_x = max_x < v[0] ? v[0] : max_x; min_x = min_x > v[0] ? v[0] : min_x; max_y = max_y < v[1] ? v[1] : max_y; min_y = min_y > v[1] ? v[1] : min_y; } // 将矩形区域的外接三角形作为边界 double dx = max_x - min_x; double dy = max_y - min_y; double mid_x = (min_x + max_x) / 2; double mid_y = (min_y + max_y) / 2; // 为了去除边界方便讲边界点的索引置为负数 Vertex v0(Vec3d(mid_x, max_y + dy, 0.0f), -1); Vertex v1(Vec3d(mid_x - dx, min_y, 0.0f), -2); Vertex v2(Vec3d(mid_x + dx, min_y, 0.0f), -3); triSet.push_back(Triangle(v0, v1, v2)); }
double FramedFace::do_calculateMaxWidth(const VertexVector& vertices) const { double maxHeight=1000000.0; int vertexNum=(int) vertices.size(); for (int i=0;i<vertexNum;i++) { int a1=i; //verticies for 1st angle int a2=i+1; int a3=i+2; a1%=vertexNum; a2%=vertexNum; a3%=vertexNum; int b1=i+1; //verticies for 2nd angle int b2=i+2; int b3=i+3; b1%=vertexNum; b2%=vertexNum; b3%=vertexNum; double angle1=Point::angleMeasure(vertices.at(a1), vertices.at(a2), vertices.at(a3))/2.0; //1st angle measure double angle2=Point::angleMeasure(vertices.at(b1), vertices.at(b2), vertices.at(b3))/2.0; //2nd angle measure double edgeLength=Point::distance(vertices.at(a2), vertices.at(b2)); double heightForEdge=edgeLength/(1.0/tan(angle1)+1.0/tan(angle2)); /*if (heightForEdge<maxHeight) { maxHeight=heightForEdge; }*/ maxHeight=fmin(heightForEdge,maxHeight); } return maxHeight; }
void FramedFace::generateInnerPoints(double frameWidth,const VertexVector& vertices) { int faceSize=(int) vertices.size(); inners.resize(faceSize); Point bisector; double angle,factor; for (int i=0;i<faceSize;i++) { int index1=(i-1)%faceSize; int index2=i%faceSize; int index3=(i+1)%faceSize; if (index1==-1) { index1=faceSize-1; } Point v1=vertices.at(index1); Point v2=vertices.at(index2); Point v3=vertices.at(index3); bisector=Point::angleBisector(v1, v2, v3); angle=Point::angleMeasure(v1, v2, v3)/2.0; factor=frameWidth/sin(angle)/Point::distance(v2, bisector); inners.at(i)=Point::extendLine(v2, bisector, factor); } }
void Delaunay::computeDelaunay( const VertexVector &verSet, TriangleVector &triSet ) { TriangleVector tmpTriSet; addBounding(verSet, tmpTriSet); for (int i = 0; i < verSet.size(); i++) { /* cout << verSet[i].m_xyz << endl;*/ insertVertex(tmpTriSet, verSet[i]); /* drawTrianglesOnPlane(triSet);*/ } removeBounding(tmpTriSet, triSet, verSet[0].m_index); }
void OpenGL_Engine::genTangentSpaceLight(const TexCoords3f& sTangent, const TexCoords3f &tTangent, const VertexVector& vertex, const Normals& normal, const matrix34 matrix, const v3 light, pvector<v3>& tangentSpaceLight) { matrix34 inverseModelMatrix; inverseModelMatrix = matrix.getInverse(); v3 objectLightPosition = inverseModelMatrix*light; pvector<v3>& tl = tangentSpaceLight; // vi4isljaem vektor napravlennij na isto4nik sveta v tangensnom prostranstve kazhdoj ver6ini for (unsigned int i=0; i<vertex.size(); i++) { v3 lightVector = objectLightPosition - vertex[i]; tl[i].x = sTangent[i].dotProduct(lightVector); // scalar product tl[i].y = tTangent[i].dotProduct(lightVector); tl[i].z = normal[i].dotProduct(lightVector); } }
void SFUtils::DoTopologicalSort( SLSF::Subsystem subsystem ) { int vertexIndex = 0; VertexIndexBlockMap vertexIndexBlockMap; BlockVertexIndexMap blockVertexIndexMap; BlockVector blockVector = subsystem.Block_kind_children(); for( BlockVector::iterator blvItr = blockVector.begin(); blvItr != blockVector.end(); ++blvItr, ++vertexIndex ) { SLSF::Block block = *blvItr; vertexIndexBlockMap[ vertexIndex ] = block; blockVertexIndexMap[ block ] = vertexIndex; std::string blockType = block.BlockType(); if ( blockType == "UnitDelay" ) { // check on other delay blocks as well ... ++vertexIndex; // UnitDelay is vertexed twice - one for outputs (timestep n-1), and one for inputs: vertexIndex is as destination, vertexIndex + 1 is as source } } Graph graph( vertexIndex ); LineSet lineSet = subsystem.Line_kind_children(); for( LineSet::iterator lnsItr = lineSet.begin(); lnsItr != lineSet.end() ; ++lnsItr ) { SLSF::Line line = *lnsItr; SLSF::Port sourcePort = line.srcLine_end(); SLSF::Port destinationPort = line.dstLine_end(); SLSF::Block sourceBlock = sourcePort.Block_parent(); SLSF::Block destinationBlock = destinationPort.Block_parent(); if ( sourceBlock == subsystem || destinationBlock == subsystem ) continue; int sourceBlockVertexIndex = blockVertexIndexMap[ sourceBlock ]; if ( static_cast< std::string >( sourceBlock.BlockType() ) == "UnitDelay" ) { ++sourceBlockVertexIndex; } int destinationBlockVertexIndex = blockVertexIndexMap[ destinationBlock ]; boost::add_edge( sourceBlockVertexIndex, destinationBlockVertexIndex, graph ); } LoopDetector loopDetector( graph ); if ( loopDetector.check() ) { // TODO: add support for loops involving integrator and other stateful blocks // Determine what Blocks caused the loop typedef std::map< Vertex, int > VertexStrongComponentIndexMap; VertexStrongComponentIndexMap vertexStrongComponentIndexMap; boost::associative_property_map< VertexStrongComponentIndexMap > apmVertexStrongComponentIndexMap( vertexStrongComponentIndexMap ); strong_components( graph, apmVertexStrongComponentIndexMap ); typedef std::vector< Vertex > VertexVector; typedef std::map< int, VertexVector > StrongComponentIndexVertexGroupMap; StrongComponentIndexVertexGroupMap strongComponentIndexVertexGroupMap; for( VertexStrongComponentIndexMap::iterator vsmItr = vertexStrongComponentIndexMap.begin(); vsmItr != vertexStrongComponentIndexMap.end(); ++vsmItr ) { strongComponentIndexVertexGroupMap[ vsmItr->second ].push_back( vsmItr->first ); } std::string error( "Dataflow Graph '" + static_cast< std::string >( subsystem.Name() ) + "' has unhandled loops: " ); for( StrongComponentIndexVertexGroupMap::iterator svmItr = strongComponentIndexVertexGroupMap.begin(); svmItr != strongComponentIndexVertexGroupMap.end(); ++svmItr ) { VertexVector vertexVector = svmItr->second; if ( vertexVector.size() <= 1 ) continue; error.append( "\n" ); for( VertexVector::iterator vtvItr = vertexVector.begin(); vtvItr != vertexVector.end(); ++vtvItr ) { error.append( blockVector[ *vtvItr ].getPath("/") ); error.append( ", " ); } error.erase( error.size() - 2 ); } throw udm_exception(error); } typedef std::set< Vertex > VertexSet; typedef std::map< int, VertexSet > PriorityVertexSetMap; PriorityVertexSetMap priorityVertexSetMap; for( BlockVector::iterator blvItr = blockVector.begin() ; blvItr != blockVector.end() ; ++blvItr ) { SLSF::Block block = *blvItr; int priority = block.Priority(); if ( priority == 0 ) continue; Vertex vertex = blockVertexIndexMap[ block ]; priorityVertexSetMap[ priority ].insert( vertex ); } if ( priorityVertexSetMap.size() > 1 ) { PriorityVertexSetMap::iterator lstPvmItr = priorityVertexSetMap.end(); --lstPvmItr; for( PriorityVertexSetMap::iterator pvmItr = priorityVertexSetMap.begin() ; pvmItr != lstPvmItr ; ) { PriorityVertexSetMap::iterator nxtPvmItr = pvmItr; ++nxtPvmItr; VertexSet &higherPriorityVertexSet = pvmItr->second; VertexSet &lowerPriorityVertexSet = nxtPvmItr->second; for( VertexSet::iterator hvsItr = higherPriorityVertexSet.begin() ; hvsItr != higherPriorityVertexSet.end() ; ++hvsItr ) { for( VertexSet::iterator lvsItr = lowerPriorityVertexSet.begin() ; lvsItr != lowerPriorityVertexSet.end() ; ++lvsItr ) { boost::add_edge( *hvsItr, *lvsItr, graph ); LoopDetector loopDetector( graph ); if ( loopDetector.check( *hvsItr ) ) { SLSF::Block higherPriorityBlock = vertexIndexBlockMap[ *hvsItr ]; SLSF::Block lowerPriorityBlock = vertexIndexBlockMap[ *lvsItr ]; std::cerr << "WARNING: Cannot implement priority difference between block \"" << higherPriorityBlock.getPath( "/" ) << "\" (Priority = " << *hvsItr << ") and " << std::endl; std::cerr << " block \"" << lowerPriorityBlock.getPath( "/" ) << "\" (Priority = " << *lvsItr << "): contradicts topology of subsystem or other implemented block priority order." << std::endl; boost::remove_edge( *hvsItr, *lvsItr, graph ); } } } pvmItr = nxtPvmItr; } } VertexList vertexList; boost::topological_sort( graph, std::back_inserter( vertexList ) ); /* PUT ALL "DataStoreMemory" BLOCKS AT END OF "C" SO THEY HAVE HIGHEST PRIORITY */ VertexList::reverse_iterator vtlRit = vertexList.rbegin(); while( vtlRit != vertexList.rend() ) { int index = *vtlRit; SLSF::Block block = vertexIndexBlockMap[ index ]; (void)++vtlRit; if ( block != Udm::null && static_cast< std::string >( block.BlockType() ) == "DataStoreMemory" ) { VertexList::reverse_iterator vtlRit2 = vtlRit; vertexList.splice( vertexList.end(), vertexList, vtlRit2.base() ); } } int priority = 0; for( VertexList::reverse_iterator vtlRit = vertexList.rbegin() ; vtlRit != vertexList.rend() ; ++vtlRit ) { int index = *vtlRit; SLSF::Block block = vertexIndexBlockMap[ index ]; if ( block == Udm::null ) { // unit delay as source is not registered - we will invoke it initially, and invoke it as destination in the priority order // const std::string& bt = blk.BlockType(); // assert(bt.compare("UnitDelay") == 0); /* Unit Delay Block as destination */ continue; } block.Priority() = priority++; } }
VertexVector GeneticAlgorithm::crossover(const VertexVector& Parent1,const VertexVector& Parent2,const size_t dot) { QString chr; for (size_t j = 0; j < Parent1.size(); ++j) { chr += QString::number(Parent1[j]) + " => "; } //qDebug() << "PARENT1: " << chr; chr.clear(); for (size_t j = 0; j < Parent2.size(); ++j) { chr += QString::number(Parent2[j]) + " => "; } //qDebug() << "PARENT2: " << chr; VertexVector::const_iterator it = Parent1.begin(); //it += dot; VertexVector Child(it, it + dot); size_t i = dot; //qDebug() << "dot = " << i << ", parent size = " << Parent1.size() ; VertexVector::iterator result; for(; (i<Parent1.size()) && (Child.size() <= Parent1.size() - 1); ++i) { result = std::find( Child.begin(), Child.end(), Parent2[i] ); if( result == Child.end()) { chr.clear(); for (size_t j = 0; j < Child.size(); ++j) { chr += QString::number(Child[j]) + " => "; } //qDebug() << "Child before adding parent2: " << chr; //qDebug() << "parent2[" << i << "] = " << Parent2[i]; Child.push_back(Parent2[i]); QString chr; for (size_t j = 0; j < Child.size(); ++j) { chr += QString::number(Child[j]) + " => "; } //qDebug() << "Parent2 added: " << chr; } else { result = std::find( Child.begin(), Child.end(), Parent1[i] ); if( result == Child.end()) { Child.push_back(Parent1[i] ); QString chr; for (size_t j = 0; j < Child.size(); ++j) { chr += QString::number(Child[j]) + " => "; } //qDebug() << "Parent1 added: " << chr; } else { VertexVector sortedChild = Child; VertexVector sortedParent = Parent1; std::sort(sortedChild.begin(), sortedChild.end()); std::sort(sortedParent.begin(), sortedParent.end()); size_t iter=0; while((sortedChild[iter]==sortedParent[iter]) && (iter < sortedParent.size())) { iter++; } //qDebug() << "iter = " << iter; if (iter<sortedParent.size()) Child.push_back(sortedParent[iter]); QString chr; for (size_t j = 0; j < Child.size(); ++j) { chr += QString::number(Child[j]) + " => "; } //qDebug() << "something added: " << chr; } } } //qDebug() << "child size = " << Child.size(); chr.clear(); for (size_t j = 0; j < Child.size(); ++j) { chr += QString::number(Child[j]) + " => "; } //qDebug() << "Child before returning: " << chr; return Child; }
void LD3dsExporter::doExport( LDLModel *pModel, Lib3dsNode *pParentNode, const TCFloat *matrix, int colorNumber, bool inPart, bool bfc, bool invert) { LDLFileLineArray *pFileLines = pModel->getFileLines(); if (pFileLines != NULL) { BFCState newBfcState = pModel->getBFCState(); int count = pModel->getActiveLineCount(); std::string meshName; Lib3dsMesh *pMesh = NULL; Lib3dsNode *pChildNode = NULL; // Lib3dsMeshInstanceNode *pInst; bool linesInvert = invert; bool isNew = false; if (TCVector::determinant(matrix) < 0.0f) { linesInvert = !linesInvert; } bfc = (bfc && newBfcState == BFCOnState) || newBfcState == BFCForcedOnState; meshName.resize(128); sprintf(&meshName[0], "m_%06d", ++m_meshCount); // meshName = getMeshName(pModel, pMesh); if (pMesh == NULL) { pMesh = lib3ds_mesh_new(meshName.c_str()); // memcpy(pMesh->matrix, matrix, sizeof(pMesh->matrix)); m_meshes[meshName] = pMesh; lib3ds_file_insert_mesh(m_file, pMesh, -1); isNew = true; } // pInst = lib3ds_node_new_mesh_instance(pMesh, // NULL/*(meshName + "n").c_str()*/, NULL, NULL, NULL); // pChildNode = (Lib3dsNode *)pInst; // memcpy(pChildNode->matrix, matrix, sizeof(float) * 16); // lib3ds_file_append_node(m_file, pChildNode, pParentNode); VertexVector vecVertices; FaceVector vecFaces; for (int i = 0; i < count; i++) { LDLFileLine *pFileLine = (*pFileLines)[i]; if (!pFileLine->isValid()) { continue; } switch (pFileLine->getLineType()) { case LDLLineTypeTriangle: case LDLLineTypeQuad: writeShapeLine(vecVertices, vecFaces, (LDLShapeLine *)pFileLine, matrix, colorNumber, bfc, linesInvert); break; case LDLLineTypeModel: { LDLModelLine *pModelLine = (LDLModelLine *)pFileLine; LDLModel *pOtherModel = pModelLine->getModel(true); if (pOtherModel != NULL) { TCFloat newMatrix[16]; int otherColorNumber = pModelLine->getColorNumber(); bool otherInPart = inPart; bool otherInvert = invert; if (pModelLine->getBFCInvert()) { otherInvert = !otherInvert; } if (otherColorNumber == 16) { otherColorNumber = colorNumber; } TCVector::multMatrix(matrix, pModelLine->getMatrix(), newMatrix); if (!inPart && pOtherModel->isPart() && m_seams) { TCVector min, max; TCFloat seamMatrix[16]; TCFloat tempMatrix[16]; pOtherModel->getBoundingBox(min, max); TCVector::calcScaleMatrix(m_seamWidth, seamMatrix, min, max); TCVector::multMatrix(newMatrix, seamMatrix, tempMatrix); memcpy(newMatrix, tempMatrix, sizeof(newMatrix)); otherInPart = true; } doExport(pOtherModel, pChildNode, newMatrix, otherColorNumber, otherInPart, bfc, otherInvert); } } break; default: // Get rid of warning break; } } if (isNew && vecVertices.size() > 0) { lib3ds_mesh_resize_vertices(pMesh, (int)vecVertices.size(), 0, 0); memcpy(pMesh->vertices, &vecVertices[0], sizeof(vecVertices[0]) * vecVertices.size()); lib3ds_mesh_resize_faces(pMesh, (int)vecFaces.size()); memcpy(pMesh->faces, &vecFaces[0], sizeof(vecFaces[0]) * vecFaces.size()); } else { --m_meshCount; } } }