void FluidSimulationSaveState::_writeSolidCellIndices(FluidSimulation *sim, 
                                                      std::ofstream *state) {
    std::vector<GridIndex> indices;
    for (int k = 0; k < _depth; k++) {
        for (int j = 0; j < _height; j++) {
            for (int i = 0; i < _width; i++) {
                if (sim->getMaterial(i, j, k) == M_SOLID) {
                    indices.push_back(GridIndex(i, j, k));
                }
            }
        }
    }

    int *data = new int[3*indices.size()];
    GridIndex g;
    for (unsigned int i = 0; i < indices.size(); i++) {
        g = indices[i];
        data[3*i] = g.i;
        data[3*i + 1] = g.j;
        data[3*i + 2] = g.k;
    }

    int binsize = 3*indices.size()*sizeof(int);
    char *storage = new char[binsize];
    memcpy(storage, data, binsize);
    delete[] data;
    state->write(storage, binsize);

    delete[] storage;
}
bool FluidSimulationSaveState::_readSolidCellIndices(std::vector<GridIndex> &indices, 
                                                     int numIndices,
                                                     std::ifstream *state) {

    int binsize = 3*numIndices*sizeof(int);
    char *bin = new char[binsize];

    state->read(bin, binsize);

    if (!state->good()) {
        delete[] bin;
        return false;
    }

    int *data = new int[3*numIndices];
    memcpy(data, bin, binsize);
    delete[] bin;
    for (int idx = 0; idx < 3*numIndices; idx += 3) {
        int i = data[idx];
        int j = data[idx + 1];
        int k = data[idx + 2];

        indices.push_back(GridIndex(i, j, k));
    }
    delete[] data;

    return true;
}
Пример #3
0
void LevelSet::_getCellLayers(std::vector<std::vector<GridIndex>> &layers) {
    Array3d<int> layerGrid(_isize, _jsize, _ksize, -1);

    std::vector<GridIndex> layer;
    for (int k = 0; k < _ksize; k++) {
        for (int j = 0; j < _jsize; j++) {
            for (int i = 0; i < _isize; i++) {
                if (_isDistanceSet(i, j, k)) {
                    layer.push_back(GridIndex(i, j, k));
                    layerGrid.set(i, j, k, 0);
                }
            }
        }
    }

    std::vector<GridIndex> q;
    layers.push_back(q);
    _getLayerCells(1, layer, layers[0], layerGrid);

    for (int i = 2; layers[i-2].size() > 0 && i < _numLayers; i++) {
        std::vector<GridIndex> q;
        layers.push_back(q);
        _getLayerCells(i, layers[i - 2], layers[i - 1], layerGrid);
    }
}
Пример #4
0
/**
 * 行走到目的地
 * <move targetx="" targety=""/>
 */
int MonsterAILib::move(MutiAIStub* stub,script::tixmlCodeNode * node)
{
	if (stub && stub->npc)
	{
		stub->npc->tryMoveUseAstr(GridIndex(node->getInt("x"),node->getInt("y")));
	}
	return 1;
}
Пример #5
0
/**
 * 检查当前的位置
 */
int MonsterAILib::checknowposition(MutiAIStub* stub,script::tixmlCodeNode * node)
{
	if (stub && stub->npc)
	{
		GridIndex nowIndex = stub->npc->getNowIndex();
		
		if (nowIndex.equal(GridIndex(node->getInt("x"),node->getInt("y"))))
		{
			return 1;
		}
	}
	return 0;
}
Пример #6
0
void LevelSet::_getNeighbourGridIndices6(GridIndex g, GridIndex n[6]) {
    n[0] = GridIndex(g.i-1, g.j, g.k);
    n[1] = GridIndex(g.i+1, g.j, g.k);
    n[2] = GridIndex(g.i, g.j-1, g.k);
    n[3] = GridIndex(g.i, g.j+1, g.k);
    n[4] = GridIndex(g.i, g.j, g.k-1);
    n[5] = GridIndex(g.i, g.j, g.k+1);
}
Пример #7
0
vector<Node3D*> SpaceGrid3D::find_neighbors(Node3D* n)
{
  vector<Node3D*> res;
  int i, j, k;
  GridIndex gi, gj;

  Vector3D pos = n->position();
  gi = grid_index(pos);

  // add nodes from own and neighboring cube
  for(i = gi.a - 1; i <= gi.a + 1; i++)
    for(j = gi.b - 1; j <= gi.b + 1; j++)
      for(k = gi.c - 1; k <= gi.c + 1; k++) {
	gj = GridIndex(i, j, k);
	if(cubes.find(gj) != cubes.end())
	  copy(cubes[gj]->begin(), cubes[gj]->end(), inserter(res, res.begin()));
      }
  
  return res;
} 
Пример #8
0
void LevelSet::_calculateDistanceFieldSigns() {
    std::vector<glm::vec3> triangleFaceDirections;
    std::vector<glm::vec3> triangleFaceCenters;
    triangleFaceDirections.reserve(_surfaceMesh.triangles.size());
    triangleFaceCenters.reserve(_surfaceMesh.triangles.size());

    for (unsigned int i = 0; i < _surfaceMesh.triangles.size(); i++) {
        triangleFaceDirections.push_back(_surfaceMesh.getTriangleFaceDirection(i));
        triangleFaceCenters.push_back(_surfaceMesh.getTriangleCenter(i));
    }

    for (int k = 0; k < _ksize; k++) {
        for (int j = 0; j < _jsize; j++) {
            for (int i = 0; i < _isize; i++) {
                if (_isDistanceSet(i, j, k)) {
                    _updateCellSign(GridIndex(i, j, k), triangleFaceCenters,
                                                        triangleFaceDirections);
                }
            }
        }
    }
}
Пример #9
0
GridIndexVector Polygonizer3d::_findSurfaceCells() {
    bool isEmpty = true;
    for (int k = 0; k < _ksize + 1; k++) {
        for (int j = 0; j < _jsize + 1; j++) {
            for (int i = 0; i < _isize + 1; i++) {
                if (_scalarField->getScalarFieldValue(i, j, k) > _surfaceThreshold) {
                    isEmpty = false;
                    goto endLoop;
                }
            }
        }
    }
    endLoop:

    if (isEmpty) {
        return GridIndexVector(_isize, _jsize, _ksize);
    }

    GridIndexVector surfaceCells(_isize, _jsize, _ksize);
    for (int k = 0; k < _ksize; k++) {
        for (int j = 0; j < _jsize; j++) {
            for (int i = 0; i < _isize; i++) {
                GridIndex cell = GridIndex(i, j, k);

                if (_isSurfaceCellMaskSet && !_surfaceCellMask->get(cell)) {
                    continue;
                }

                if (_isCellOnSurface(cell)) {
                    surfaceCells.push_back(cell);
                }
            }
        }
    }

    return surfaceCells;
}
Пример #10
0
GridIndex GridIndex::operator+(const GridIndex & rhs) const
{
    return GridIndex(rowIndex + rhs.rowIndex, colIndex + rhs.colIndex);
}
Пример #11
0
#include "cocos2d.h"
#include "../cocos2d/external/tinyxml2/tinyxml2.h"

#include "GridIndex.h"
#include "AdjacentDirection.h"

const std::vector<std::pair<AdjacentDirection, GridIndex>> s_Adjacents{
    std::make_pair(AdjacentDirection::BELOW, GridIndex(-1, 0)),
    std::make_pair(AdjacentDirection::UPPER, GridIndex(1, 0)),
    std::make_pair(AdjacentDirection::LEFT, GridIndex(0, -1)),
    std::make_pair(AdjacentDirection::RIGHT, GridIndex(0, 1))
};

GridIndex::GridIndex(int rowIndex, int colIndex) : rowIndex{ rowIndex }, colIndex{ colIndex }
{
}

GridIndex::GridIndex(const cocos2d::Vec2 & position, const cocos2d::Size & gridSize)
{
    assert(gridSize.height > 0. && gridSize.width >= 0. && "GridIndex::GridIndex() gridSize is non-positive.");

    if (position.x < 0. || position.y < 0.) {
        rowIndex = -1;
        colIndex = -1;

        return;
    }

    rowIndex = static_cast<int>(position.y / gridSize.height);
    colIndex = static_cast<int>(position.x / gridSize.width);
}
Пример #12
0
GridIndex MovingPathScript::MovingPathScriptImpl::toGridIndex(const cocos2d::Vec2 & positionInWindow) const
{
	auto positionInMap = m_TransformComponent.lock()->convertToLocalSpace(positionInWindow);
	auto gridSize = SingletonContainer::getInstance()->get<ResourceLoader>()->getDesignGridSize();
	return GridIndex(positionInMap, gridSize);
}
Пример #13
0
int ANBSample::DartThrowing(){

	//Dart-Throwing法

	int trail = 0;
	int neighbor_range = 3; //局部邻域范围
	int rand_pt_ind;        //随机点索引
	if(USING_ONION_ORDER){
		grid.SetUpOnionOrder(neighbor_range);  //开始采样
	}

#if ORDER_TYPE == 0
	random_shuffle(dense_pt_list.begin(),dense_pt_list.end());
	int id_counter = 0;
	vector<sample *>::iterator it;      //设置点迭代器
	for (it=dense_pt_list.begin();it!=dense_pt_list.end();++it){

		(*it)->_ID = id_counter;    //获取采样点的id

	}

	for(int i = 0 ; i < dense_pt_list.size(); i++){

		rand_pt_ind = i;
#elif ORDER_TYPE == 1
	for(int i = 0 ; i < dense_pt_list.size(); i++){
		rand_pt_ind = i;
#else
#endif
	sample *s = dense_pt_list[rand_pt_ind];  //采样点集
	if(pt_list.empty()){

		s->acp_ID = pt_list.size();
		pt_list.push_back(s);

		//将采样点插入到网格中
		vector<int> g_ind = GridIndex(s);
		grid.SetCellValue(g_ind[0],g_ind[1],g_ind[2],s->acp_ID);

		trail = 0;
		continue;
	}
	else{
		vector<int>g_ind =GridIndex(s); 
		vector<int>neighbor;
		if(USING_ONION_ORDER){

			neighbor = grid.GetNeighbor_Onion(g_ind[0],g_ind[1],g_ind[2]);

		}
		else{
			neighbor = grid.GetNeighbor(g_ind[0],g_ind[1],g_ind[2],neighbor_range, neighbor_range,neighbor_range);
		}
		int neighbor_ind;
		bool conflict = false;
		for (unsigned int nsi=0; nsi<neighbor.size(); nsi++){
			neighbor_ind=neighbor[nsi];
			while(true)
			{ 
				if(conflict_check(pt_list[neighbor_ind], s))
				{

					conflict=true;
					trail++;


					nsi=neighbor.size();
					break;
				}



				if(pt_list[neighbor_ind]->next==NULL)// arrive the end of current grid cell
				{
					break;
				}
				else
				{
					neighbor_ind=pt_list[neighbor_ind]->next->acp_ID;
				}
			}// end while(true)
		}//end for (unsigned nsi=0; nsi<neigbor.size(); nsi++)

		if (!conflict){
			s->acp_ID=pt_list.size();
			pt_list.push_back(s);

			// 采样点插入网格
			std::vector<int> g_ind=GridIndex(s);
			if(!grid._grid[g_ind[0]][g_ind[1]][g_ind[2]].empty())
			{
				s->next=pt_list[ grid._grid[g_ind[0]][g_ind[1]][g_ind[2]][0] ];
				pt_list[ grid._grid[g_ind[0]][g_ind[1]][g_ind[2]][0] ]->pre=s;
			}

			grid.SetCellValue(g_ind[0], g_ind[1], g_ind[2], s->acp_ID);

			trail=0;				
			}
		}
	}
	return 1;
}
int ANBSample::process(){
	
	//采样过程
	int grid_size[3];
	grid_size[0]=grid_size[1]=grid_size[2]=1.0f/r_global;
	
	

	real model_width[3]={
		boundingbox[1]-boundingbox[0],
		boundingbox[3]-boundingbox[2],
		boundingbox[5]-boundingbox[4]};

	real cell_wid=(model_width[0])/( grid_size[0]*1.0f );
	grid_size[1]=max((int)floor(model_width[1]/cell_wid),1);
	grid_size[2]=max((int)floor(model_width[2]/cell_wid),1);


	grid.CreateGrid(grid_size[0],grid_size[1],grid_size[2]);
	for (int i=0; i<3; i++)
	{
		grid._bin_width[i]=model_width[i]/(grid._size[i]*1.0f);
	}


	DartThrowing();
	grid.CleanGrid();
	grid.DeleteGrid();
	return 1;
}
Пример #14
0
bool StartScene::init()
{
	theAILib.initWithFile("monsterai.xml");
	theUILib.initWithFile("uilib.xml");
	CCSprite3D *d3 = CCSprite3D::create("terran.md2", "terran.png");
	if (d3)
	{
//		this->addChild(d3);
	}
	//Layer3D *layer3D = Layer3D::create();
	//if (layer3D)
	{
	//	this->addChild(layer3D);
	}
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
	window = UIWindow::create();
	if (!window) return false;
	
	this->addChild(window);
	if ( !CCLayer::init() )
    {
        return false;  
    }
	
    this->setTouchEnabled(true);
	
	/**
	 * 操作界面 几个Button 
	 */
	MainDialog *mainDialog = MainDialog::create(window,"mainui.xml");
	
	
	MapManager::getMe().window = window;

	this->schedule(schedule_selector(StartScene::step), 0.75f);   
	
	MutiBigTerrain *terrain = MutiBigTerrain::create("grass.png",2,2);
	if (terrain)
	{
		//this->addChild(terrain);
		terrain->setPosition(ccp(10,10));
	}
	MutiMap *map = MutiMap::create("defaultmap.xml");
	if (map)
	{
		map->showGrids();
		MapManager::getMe().replaceMap(map);
	}
	Cartoon2DEdit *edit = Cartoon2DEdit::create(window,"cartoonedit/createcartoon.xml");
	if (edit)
	{
		window->pushModel(edit);
	}
#ifdef MONSTER_TEST
	for (int i = 0; i < 1;i++)
	{
		MutiMonster *monster = MutiMonster::create();
		if (monster)
		{
			monster->monsterAIID = 1;
			monster->start("monster1.xml");
			map->addMonster(monster);
		//	monster->tryMoveUseAstr(GridIndex(5+i,5));
			monster->setPosition(GridIndex(0,i));
			
		}
	}
	MutiMonster *monster = MutiMonster::create();
	if (monster)
	{
		monster->monsterAIID = 0;
		monster->start("monster1.xml");
		map->addMonster(monster);
		monster->setPosition(GridIndex(1,0));

		//monster->jumpTo();
	//	monster->moveLeft();
	}
#endif
	CCSprite *sprite = CCSprite::create("cell.png");
	if (sprite)
	{
		map->addSprite(sprite);
	}
	return true;
}
Пример #15
0
/**
* 将像素点转化为网格
*/
GridIndex MapScene::transPixelPointToGird(const Point &point)
{
	return GridIndex(point.x / 64, point.y / 32);
}