Пример #1
0
 inline EdgeDescriptor
 dereference() const
 {
     return EdgeDescriptor(get_edge_exists(*this->base(), 0),
                           m_src, m_targ,
                           &get_edge_property(*this->base()));
 }
bool TriangleFill_SweepLine(Grid& grid, TIterator edgesBegin,
							TIterator edgesEnd, APosition& aPosVRT,
							AInt& aIntVRT,
							SubsetHandler* pSH,
							int newSubsetIndex)
{
	if(!grid.has_vertex_attachment(aPosVRT)){
		UG_LOG("position attachment missing in TriangleFill_SweepLine");
		return false;
	}

	if(!grid.has_vertex_attachment(aIntVRT)){
		UG_LOG("int attachment missing in TriangleFill_SweepLine");
		return false;
	}

	Grid::VertexAttachmentAccessor<APosition> aaPos(grid, aPosVRT);
	Grid::VertexAttachmentAccessor<AInt> aaInt(grid, aIntVRT);

//	set up the vertex and edge arrays and build some additional
//	information that is required when it comes to building the triangles.
	std::vector<Vertex*> vrtPtrs;
	std::vector<vector3> vrts;
	std::vector<int>	edges;

	grid.begin_marking();
	for(TIterator iter = edgesBegin; iter != edgesEnd; ++iter)
	{
		Edge* e = *iter;

		for(size_t i = 0; i < 2; ++i){
			Vertex* vrt = e->vertex(i);
			if(!grid.is_marked(vrt)){
				aaInt[vrt] = (int)vrts.size();
				vrts.push_back(aaPos[vrt]);
				//vrts.push_back(vector2(aaPos[vrt].x(), aaPos[vrt].y()));
				vrtPtrs.push_back(vrt);
				grid.mark(vrt);
			}

			edges.push_back(aaInt[vrt]);
		}
	}
	grid.end_marking();

//	now call the original implementation
	size_t numInitialEdges = edges.size();
	std::vector<int> faces;
	if(TriangleFill_SweepLine(faces, vrts, edges)){

	//	now create the triangles
		for(size_t i = 0; i < faces.size(); i+=3){
			Triangle* tri = *grid.create<Triangle>(TriangleDescriptor(vrtPtrs[faces[i]],
																	  vrtPtrs[faces[i + 1]],
																	  vrtPtrs[faces[i + 2]]));
			if(pSH){
				pSH->assign_subset(tri, newSubsetIndex);
			}
		}

		return true;
	}

	else{
	//DEBUG ONLY
	//	create edges
		for(size_t i = numInitialEdges; i < edges.size(); i+=2){
			Edge* e = *grid.create<RegularEdge>(EdgeDescriptor(vrtPtrs[edges[i]], vrtPtrs[edges[i+1]]));
			if(pSH){
				pSH->assign_subset(e, newSubsetIndex);
			}
		}
	}

	return false;
}
Пример #3
0
////////////////////////////////////////////////////////////////////////
//	ImportGridFromLGM
bool ImportGridFromLGM(Grid& grid,
                       const char* filename,
                       AVector3& aPos,
                       ISubsetHandler* pSurfaceHandler)
{
//	we'll read the lgm in two steps:
//	first we'll try to load a 3d lgm. If that fails we'll try to
//	load a 2d on. If that fails too, we'll give up.

	// create lgm object
	lgm* l = lgm_new();
	// read lgm file
	lgm_info* linfo = lgm_info_new();
	
//	load 3d
	if(lgm_read(filename, l, linfo))
	{/*
		LOG("WARNING in ImportGridFromLGM: " << linfo->err_msg << endl);
		lgm_delete(l);
		lgm_info_delete(linfo);
		return false;*/

	//	3d could not be loaded. Try 2d.
	//TODO: add full 2d support to lgm_parser. There are problems
	//		with line left and line right (They are not yet parsed).
	//		This leads to an error in the current implementation.
		lgm_info_delete(linfo);
		lgm_delete(l);
		l = lgm_new();
		l->dim = 2;
		linfo = lgm_info_new();

		if(lgm_read(filename, l, linfo)){
			LOG("WARNING in ImportGridFromLGM: " << linfo->err_msg << endl);
			lgm_info_delete(linfo);
			lgm_delete(l);
			return false;
		}
	}
	lgm_info_delete(linfo);

	// make sure lgm has dimension of 2 or 3
	if(l->dim != 2 && l->dim != 3)
	{
		LOG("WARNING in ImportGridFromLGM: "
		    << "LGM is does not have dimension of 2 or 3!"
		    << endl);
		lgm_delete(l);
		return false;
	}

	//	set up vertex attachment
	if(!grid.has_vertex_attachment(aPos))
		grid.attach_to_vertices(aPos);

	//	set up vertex attachment accessor
	Grid::VertexAttachmentAccessor<AVector3> aaPosition(grid, aPos);

	//	read points and store them in an array for index access
	vector<RegularVertex*> vVertices;
	vVertices.reserve(l->num_points);

	// read points
	for(int i = 0; i < l->num_points; ++i)
	{
		// get point
		double* point = l->points[i];

		// create and store vertex
		RegularVertex* vert = *grid.create<RegularVertex>();
		vVertices.push_back(vert);

		// set vertex coordinates
		aaPosition[vert] = vector3(
			(number)point[0],
			(number)point[1],
			(l->dim == 3) ? (number)point[2] : (number)0.
		);
	}

	//	read lines and store them in an array for index access
	vector<RegularEdge*> vEdges;
	vEdges.reserve(l->num_lines);

	//	read lines
	for(int i = 0; i < l->num_lines; ++i)
	{
		// get line
		lgm_line* li = &l->lines[i];

		// scan through line nodes
		for(int j = 1; j < li->num_points; ++j)
		{
			// vertex indices
			int v1 = li->points[j-1];
			int v2 = li->points[j];

			// create edge
			RegularEdge* e = *grid.create<RegularEdge>(EdgeDescriptor(
				vVertices[v1],
				vVertices[v2]
			));

			// add line to line subset
			if(pSurfaceHandler)
				pSurfaceHandler->assign_subset(e, i);

			// store edge
			vEdges.push_back(e);
		}
	}

	// read surfaces
	for(int i = 0; i < l->num_surfaces; ++i)
	{
		// get surface
		lgm_surface* s = &l->surfaces[i];
		/*
		// read points
		for(int j = 0; j < s->num_points; ++j)
		{
			// vertex index
			int v = s->points[j];

			// add vertex to surface subset
			if(pSurfaceHandler)
				pSurfaceHandler->assign_subset(vVertices[v], i);
		}

		// read lines
		for(int j = 0; j < s->num_lines; ++j)
		{
			// edge index
			int e = s->lines[j];

			// add edge to surface subset
			if(pSurfaceHandler)
				pSurfaceHandler->assign_subset(vEdges[e], i);
		}*/

		// read triangles
		for(int j = 0; j < s->num_triangles; ++j)
		{
			// vertex indices
			int v1 = s->triangles[j][0];
			int v2 = s->triangles[j][1];
			int v3 = s->triangles[j][2];

			// create triangle
			Triangle* t = *grid.create<Triangle>(TriangleDescriptor(
				vVertices[v1],
				vVertices[v2],
				vVertices[v3]
			));

			// add triangle to surface subset
			if(pSurfaceHandler)
				pSurfaceHandler->assign_subset(t, i);
		}
	}

	// done importing!

	// delete lgm object
	lgm_delete(l);

	return true;
}