예제 #1
0
파일: Dijkstra.cpp 프로젝트: ana-GT/LJM2
/**
 * @function Search
 */
void Dijkstra3D::Search()
{
   //-- Initialize your states
   for( int i = 0; i < mDimX; i++ )
   {  for( int j = 0; j < mDimY; j++ )
      {  for( int k = 0; k < mDimZ; k++ )
         {  
            mV[ GetRef(i,j,k) ].Init( i, j, k ) ;
            if( mGrid->getCell( i, j, k ) != 0 ) //-- If it is an obstacle
	      {  mV[ GetRef(i,j,k) ].color = 3; } //--Mark as Obstacle (3)     
         }
      }    
   } 

  //-- Initialize the goal state
  int s = GetRef( mGoalX, mGoalY, mGoalZ ) ;
  mV[ s ].color = 1; // gray: In Open List
  mV[ s ].dist = 0;  // start: 0 distance of itself
 
  //-- Insert to Q (unvisited nodes)
  InsertOpenSet( s );
  
  //-- Iterate
  int u; int v;
  float new_dist;

  while( mQ.size() != 0 )
  {
     u = PopOpenSet( );

     //-- All neighbors
     std::vector< int > ng = GetNeighbors( u );
 
     for( int i = 0; i < ng.size(); i++ )
     {
        v = ng[i];
        new_dist = ( GetEdgeCost( u, v ) + mV[ u ].dist );

        if( new_dist < mV[ v ].dist )
        {
           mV[ v ].dist = new_dist ;
           mV[ v ].parent = u;      
        }  
  
        if( mV[ v ].color == 2 )  //-- Nowhere
        { 
          mV[ v ].color = 1;  //-- Add it to the OpenList (queue)
          InsertOpenSet( v );
        }
        else if( mV[ v ].color == 1 )
        {
           LowerKeyOpenSet( v );
        }
        else
        { /** Pretty much nothing by now */ }
  
     }  //-- End for  
     
     mV[u].color = 0; //-- Closed List
  } //-- End while
  
}
예제 #2
0
void GenerateLOD::CollapseEdge_And_UpdateCost(int EdgeIdx, FbxVector4 *pControlPoints, int &addTriangleIndex, int &addEdgeIndex)
{
	int U, V;
	const Edge &edge = (*Edges)[EdgeIdx];
	if (edge.isUV) {
		U = edge.twoPoints[0];
		V = edge.twoPoints[1];
	}
	else {
		U = edge.twoPoints[1];
		V = edge.twoPoints[0];
	}
	// Construct triangles in U's adjacent vertices
	const std::set<int> &UTi = (*ControlP)[U].Ti;
	const std::set<int> &VTi = (*ControlP)[V].Ti;
	std::unordered_set<int> UdiffV;
	UdiffV.clear();
	std::set_difference(UTi.begin(), UTi.end(),
		VTi.begin(), VTi.end(),
		std::inserter(UdiffV, UdiffV.begin()));

	for (const auto &i : UdiffV) {
		std::vector<int> TrianglethreePoints;
		std::vector<int> TrianglethreeUVs;

		const Face &trian_i = (*Triangles)[i];

		int flag;
		for (int j = 0; j < 3; ++j) {
			if (U == trian_i.points[j]) {
				flag = j;
				break;
			}
		}

		switch (flag)
		{
		case 2:
		{
			TrianglethreePoints.push_back(trian_i.points[0]);
			TrianglethreeUVs.push_back(trian_i.uvs[0]);

			TrianglethreePoints.push_back(trian_i.points[1]);
			TrianglethreeUVs.push_back(trian_i.uvs[1]);

			TrianglethreePoints.push_back(V);
			TrianglethreeUVs.push_back(*((*ControlP)[V].uvSet.begin()));

			break;
		}
		case 1:
		{
			TrianglethreePoints.push_back(trian_i.points[0]);
			TrianglethreeUVs.push_back(trian_i.uvs[0]);

			TrianglethreePoints.push_back(V);
			TrianglethreeUVs.push_back(*((*ControlP)[V].uvSet.begin()));

			TrianglethreePoints.push_back(trian_i.points[2]);
			TrianglethreeUVs.push_back(trian_i.uvs[2]);

			break;
		}
		case 0:
		{
			TrianglethreePoints.push_back(trian_i.points[1]);
			TrianglethreeUVs.push_back(trian_i.uvs[1]);

			TrianglethreePoints.push_back(trian_i.points[2]);
			TrianglethreeUVs.push_back(trian_i.uvs[2]);

			TrianglethreePoints.push_back(V);
			TrianglethreeUVs.push_back(*((*ControlP)[V].uvSet.begin()));

			break;
		}
		default:
			break;
		}

		//(*Triangles)[addTriangleIndex] = Face();
		Face &face = (*Triangles)[addTriangleIndex];
		face.points = TrianglethreePoints;
		face.uvs = TrianglethreeUVs;
		face.normal = GetNormal(pControlPoints,
			TrianglethreePoints[0],
			TrianglethreePoints[1],
			TrianglethreePoints[2]);
		(*ControlP)[TrianglethreePoints[0]].Ti.insert(addTriangleIndex);
		(*ControlP)[TrianglethreePoints[1]].Ti.insert(addTriangleIndex);
		(*ControlP)[TrianglethreePoints[2]].Ti.insert(addTriangleIndex);

		addTriangleIndex += 1;
	}

	//MessageBox(NULL, "OK5", "This", 0);

	// Delete all U's adjacent triangles from Dict (*Triangles)
	// And delete it in Is's adjacent vertex's Ti
	for (const auto &triangleIndex : (*ControlP)[U].Ti) {
		for (const auto &idx : (*Triangles)[triangleIndex].points) {
			if (idx != U)
				(*ControlP)[idx].Ti.erase(triangleIndex);
		}
		(*Triangles).erase(triangleIndex);
	}

	for (const auto &i : (*ControlP)[U].Vi) {
		//MessageBox(NULL, "OK a1", "This", 0);
		(*ControlP)[i].Vi.erase(U);
		//MessageBox(NULL, "OK a2", "This", 0);
	}

	//# # Delete (*Edges) from U's adjacent vertex's Ei
	for (const auto &i : (*ControlP)[U].Ei) {
		const Edge &edge_i = (*Edges)[i];
		if (U == edge_i.twoPoints[0])
			(*ControlP)[edge_i.twoPoints[1]].Ei.erase(i);
		else
			(*ControlP)[edge_i.twoPoints[0]].Ei.erase(i);

		if (edge_i.ShouldCollapse) {
			const FbxDouble &cost = edge_i.Cost;
			std::vector<int>::iterator it = find((*CostToEdge)[cost].begin(), (*CostToEdge)[cost].end(), i);
			(*CostToEdge)[cost].erase(it);
		}

		(*Edges).erase(i);  //# include edge U <----> V
	}

	//MessageBox(NULL, "OK8", "This", 0);

	// Construct new edges
	const std::set<int> &UVi = (*ControlP)[U].Vi;
	const std::set<int> &VVi = (*ControlP)[V].Vi;
	UdiffV.clear();
	std::set_difference(UVi.begin(), UVi.end(),
		VVi.begin(), VVi.end(),
		std::inserter(UdiffV, UdiffV.begin()));
	UdiffV.erase(V);
	for (const auto &i : UdiffV) {
		Edge &edge_i = (*Edges)[addEdgeIndex];
		//(*Edges)[addEdgeIndex] = Edge();
		edge_i.twoPoints = { i, V };

		(*ControlP)[i].Ei.insert(addEdgeIndex);
		(*ControlP)[V].Ei.insert(addEdgeIndex);

		(*ControlP)[i].Vi.insert(V);
		(*ControlP)[V].Vi.insert(i);

		if ((*ControlP)[i].uvSet.size() > 1) {
			edge_i.ShouldCollapse = false;
			addEdgeIndex += 1;
			continue;
		}
		bool isUtoV;
		FbxDouble cost;
		GetEdgeCost(isUtoV, cost, i, V, pControlPoints);
		edge_i.Cost = cost;
		edge_i.isUV = isUtoV;

		HeapCost->push(cost);
		//if ((*CostToEdge).find(cost) != (*CostToEdge).end())
		(*CostToEdge)[cost].push_back(addEdgeIndex);
		//else
		//(*CostToEdge)[cost] = std::vector<int>({addEdgeIndex});
		addEdgeIndex += 1;
	}
	(*ControlP).erase(U);
}