Ejemplo n.º 1
0
void ShortestPathVertex::compute (MessageIterator<int>* msgs) {
    int mindist =  (superstep() == 0 && vertex_id() == 0) ? 0 : getValue();

    for (; !msgs->done(); msgs->next())
        mindist = min (mindist, msgs->getValue());

    if (mindist < getValue()) {
        *mutableValue() = mindist;
        EdgeIterator<int> iter = getOutEdgeIterator();
        for (; !iter.done(); iter.next())
            sendMessageTo(iter.dest()->getDest(), mindist + iter.dest()->getValue());
    }

    voteToHalt();
}
Ejemplo n.º 2
0
void CLevelGraph::draw_nodes	()
{
	CGameObject*	O	= smart_cast<CGameObject*> (Level().CurrentEntity());
	Fvector	POSITION	= O->Position();
	POSITION.y += 0.5f;

	// display
	Fvector P			= POSITION;

//	CPosition			Local;
//	vertex_position		(Local,P);

	u32 ID				= O->ai_location().level_vertex_id();

	CGameFont* F		= UI().Font().pFontDI;
	F->SetHeightI		(.02f);
	F->OutI				(0.f,0.5f,"%f,%f,%f",VPUSH(P));
//	float				x,z;
//	unpack_xz			(Local,x,z);
//	F->Out				(0.f,0.55f,"%3d,%4d,%3d -> %d",	iFloor(x),iFloor(Local.y()),iFloor(z),u32(ID));

	svector<u32,128>	linked;
	{
		const_iterator	i,e;
		begin			(ID,i,e);
		for(; i != e; ++i)
			linked.push_back(value(ID,i));
	}

	// render
	float	sc		= header().cell_size()/16;
	float	st		= 0.98f*header().cell_size()/2;
	float	tt		= 0.01f;

	Fvector	DUP;		DUP.set(0,1,0);

	//RCache.set_Shader	(sh_debug);
	DRender->SetShader(sh_debug);

	F->SetColor			(color_rgba(255,255,255,255));

	//////////////////////////////////////////////////////////////////////////
	Fvector min_position,max_position;
	max_position = min_position = Device.vCameraPosition;
	min_position.sub(30.f);
	max_position.add(30.f);
	
	CLevelGraph::const_vertex_iterator	 I, E;
	if (valid_vertex_position(min_position))
		I = std::lower_bound(begin(),end(),vertex_position(min_position).xz(),&vertex::predicate2);
	else
		I = begin();

	if (valid_vertex_position(max_position)) {
		E = std::upper_bound(begin(),end(),vertex_position(max_position).xz(),&vertex::predicate);
		if (E != end()) ++E;
	}
	else
		E = end();

	//////////////////////////////////////////////////////////////////////////

	for ( ; I != E; ++I)
	{
		const CLevelGraph::CVertex&	N	= *I;
		Fvector			PC;
		PC				= vertex_position(N);

		u32 Nid			= vertex_id(I);

		if (Device.vCameraPosition.distance_to(PC)>30) continue;

		float			sr	= header().cell_size();
		if (::Render->ViewBase.testSphere_dirty(PC,sr)) {
			
			u32	LL = 255;
			
			u32	CC		= D3DCOLOR_XRGB(0,0,255);
			u32	CT		= D3DCOLOR_XRGB(LL,LL,LL);
			u32	CH		= D3DCOLOR_XRGB(0,128,0);

			BOOL	bHL		= FALSE;
			if (Nid==u32(ID))	{ bHL = TRUE; CT = D3DCOLOR_XRGB(0,255,0); }
			else {
				for (u32 t=0; t<linked.size(); ++t) {
					if (linked[t]==Nid) { bHL = TRUE; CT = CH; break; }
				}
			}

			// unpack plane
			Fplane PL; Fvector vNorm;
			pvDecompress(vNorm,N.plane());
			PL.build	(PC,vNorm);

			// create vertices
			Fvector		v,v1,v2,v3,v4;
			v.set(PC.x-st,PC.y,PC.z-st);	PL.intersectRayPoint(v,DUP,v1);	v1.mad(v1,PL.n,tt);	// minX,minZ
			v.set(PC.x+st,PC.y,PC.z-st);	PL.intersectRayPoint(v,DUP,v2);	v2.mad(v2,PL.n,tt);	// maxX,minZ
			v.set(PC.x+st,PC.y,PC.z+st);	PL.intersectRayPoint(v,DUP,v3);	v3.mad(v3,PL.n,tt);	// maxX,maxZ
			v.set(PC.x-st,PC.y,PC.z+st);	PL.intersectRayPoint(v,DUP,v4);	v4.mad(v4,PL.n,tt);	// minX,maxZ

			// render quad
			DRender->dbg_DrawTRI(Fidentity,v3,v2,v1,CT);
			DRender->dbg_DrawTRI(Fidentity,v1,v4,v3,CT);
			//RCache.dbg_DrawTRI	(Fidentity,v3,v2,v1,CT);
			//RCache.dbg_DrawTRI	(Fidentity,v1,v4,v3,CT);

			// render center
			Level().debug_renderer().draw_aabb	(PC,sc,sc,sc,CC);

			// render id
			if (bHL) {
				Fvector		T;
				Fvector4	S;
				T.set		(PC); T.y+=0.3f;
				Device.mFullTransform.transform	(S,T);
				if (S.z < 0 || S.z < 0)												continue;
				if (S.x < -1.f || S.x > 1.f || S.y<-1.f || S.x>1.f)					continue;
				F->SetHeightI	(0.05f/_sqrt(_abs(S.w)));
				F->SetColor	(0xffffffff);
				F->OutI		(S.x,-S.y,"~%d",Nid);
			}
		}
	}
}
	bool save(const std::string& filename) {
		if(map_ == nil) {
			Logger::err("PlyMeshSave") << "mesh is null" << std::endl ;
			return false ;
		}

		p_ply ply = ply_create(filename.c_str(), PLY_LITTLE_ENDIAN, nil, 0, nil) ;

		if(ply == nil) {
			Logger::err("PlyMeshSave") << filename << ": could not open" << std::endl ;
			return false ;
		}

		//////////////////////////////////////////////////////////////////////////

		if (!ply_add_comment(ply, "saved by [email protected]")) {
			Logger::err("PlyMeshSave") << "unable to add comment" << std::endl ;
			ply_close(ply) ;
			return false ;
		}

		int num_v = map_->size_of_vertices();
		if (!ply_add_element(ply, "vertex", num_v)) {
			Logger::err("PlyMeshSave") << "unable to add element \'vertex\'" << std::endl ;
			ply_close(ply) ;
			return false ;
		}
		
		e_ply_type length_type, value_type;
		length_type = value_type = static_cast<e_ply_type>(-1);
		std::string pos[3] = { "x", "y", "z" };
		for (unsigned int i=0; i<3; ++i) {
			if (!ply_add_property(ply, pos[i].c_str(), PLY_FLOAT, length_type, value_type)) {
				Logger::err("PlyMeshSave") << "unable to add property \'" << pos[i] << "\'" << std::endl ;
				ply_close(ply) ;
				return false ;
			}
		}

		MapVertexAttribute<Color> vertex_color;
		vertex_color.bind_if_defined(const_cast<Map*>(map_), "color") ;
		if (vertex_color.is_bound()) {
			std::string color[4] = { "red", "green", "blue", "alpha" };
			for (unsigned int i=0; i<4; ++i) {
				if (!ply_add_property(ply, color[i].c_str(), PLY_UCHAR, length_type, value_type)) {
					Logger::err("PlyMeshSave") << "unable to add property \'" << color[i] << "\'" << std::endl ;
					ply_close(ply) ;
					return false ;
				}
			}
		}
		
		int num_f = map_->size_of_facets();
		if (!ply_add_element(ply, "face", num_f)) {
			Logger::err("PlyMeshSave") << "unable to add element \'face\'" << std::endl ;
			ply_close(ply) ;
			return false ;
		}
		if (!ply_add_property(ply, "vertex_indices", PLY_LIST, PLY_UCHAR, PLY_INT)) {
			Logger::err("PlyMeshSave") << "unable to add property \'vertex_indices\'" << std::endl ;
			ply_close(ply) ;
			return false ;
		}

		if(!ply_write_header(ply)) {
			Logger::err("PlyMeshSave") << filename << ": invalid PLY file" << std::endl ;
			ply_close(ply) ;
			return false ;
		}

		//////////////////////////////////////////////////////////////////////////
		
		FOR_EACH_VERTEX_CONST(Map, map_, it) {
			const vec3& p = it->point();
			ply_write(ply, p.x);
			ply_write(ply, p.y);
			ply_write(ply, p.z);
			if (vertex_color.is_bound()) {
				const Color& c = vertex_color[it];
				double r = c.r() * color_mult_;	ogf_clamp(r, 0.0, 255.0);
				double g = c.g() * color_mult_;	ogf_clamp(g, 0.0, 255.0);
				double b = c.b() * color_mult_;	ogf_clamp(b, 0.0, 255.0);
				double a = c.a() * color_mult_; ogf_clamp(a, 0.0, 255.0);
				ply_write(ply, r);
				ply_write(ply, g);
				ply_write(ply, b);
				ply_write(ply, a);
			}
		}

		// ply files numbering starts with 0
		Attribute<Map::Vertex, int> vertex_id(map_->vertex_attribute_manager());
		MapEnumerator::enumerate_vertices(const_cast<Map*>(map_), vertex_id, 0);
		FOR_EACH_FACET_CONST(Map, map_, it) {
			ply_write(ply, it->nb_vertices());
			Map::Halfedge* h = it->halfedge();
			do 
			{
				int id = vertex_id[h->vertex()];
				ply_write(ply, id);
				h = h->next();
			} while (h != it->halfedge());
		}