void BestCandidate2D(float table[][2], int totalSamples, RNG &rng, SampleGrid *grid) { SampleGrid localGrid; if (!grid) grid = &localGrid; ProgressReporter progress(totalSamples-1, "Throwing Darts"); // Generate first 2D sample arbitrarily table[0][0] = rng.RandomFloat(); table[0][1] = rng.RandomFloat(); addSampleToGrid(table, 0, grid); for (int currentSample = 1; currentSample < totalSamples; ++currentSample) { // Generate next best 2D image sample float maxDist2 = 0.; int numCandidates = 500 * currentSample; for (int currentCandidate = 0; currentCandidate < numCandidates; ++currentCandidate) { // Generate a random candidate sample float candidate[2]; candidate[0] = rng.RandomFloat(); candidate[1] = rng.RandomFloat(); // Loop over neighboring grid cells and check distances float sampleDist2 = INFINITY; int gu = GRID(candidate[0]), gv = GRID(candidate[1]); for (int du = -1; du <= 1; ++du) { for (int dv = -1; dv <= 1; ++dv) { // Compute (u,v) grid cell to check int u = gu + du, v = gv + dv; if (u < 0) u += BC_GRID_SIZE; if (u >= BC_GRID_SIZE) u -= BC_GRID_SIZE; if (v < 0) v += BC_GRID_SIZE; if (v >= BC_GRID_SIZE) v -= BC_GRID_SIZE; // Update minimum squared distance from cell's samples for (uint32_t g = 0; g < (*grid)[u][v].size(); ++g) { int s = (*grid)[u][v][g]; float xdist = Wrapped1DDist(candidate[0], table[s][0]); float ydist = Wrapped1DDist(candidate[1], table[s][1]); float d2 = xdist*xdist + ydist*ydist; sampleDist2 = min(sampleDist2, d2); } } } // Keep this sample if it is the best one so far if (sampleDist2 > maxDist2) { maxDist2 = sampleDist2; table[currentSample][0] = candidate[0]; table[currentSample][1] = candidate[1]; } } addSampleToGrid(table, currentSample, grid); progress.Update(); } progress.Done(); }
/* * calculateConnections * * @param pointer to user defined connection matrix * @param number of elements in a matrix line * @param 1 if matrix contains drop-propabilities */ void calculateConnections(float *connMatrix, int socketcount, int drop) { /* calculating unicast/broadcast matrix */ connUni = malloc(sizeof(u_int32_t) * socketcount * socketcount); memset(connUni, 0, sizeof(u_int32_t) * socketcount * socketcount); connBC = malloc(sizeof(u_int32_t) * socketcount * socketcount); memset(connBC, 0, sizeof(u_int32_t) * socketcount * socketcount); float broadcast, unicast; int i, j; for (j = 0; j < socketcount; j++) { for (i = 0; i < socketcount; i++) { float prop = connMatrix[GRID(i, j, socketcount)]; if (drop) { prop = 1.0 - prop; } broadcast = prop; /* IEEE 802.11 do (normally) up to 4 retransmissions for unicast. * A unicast package is only lost if all of this 5 transmissions * are lost. */ prop = 1 - prop; unicast = (1 - prop) * (1 + prop + prop * prop + prop * prop * prop + prop * prop * prop * prop); connBC[GRID(i, j, socketcount)] = (1 << 24) * broadcast; connUni[GRID(i, j, socketcount)] = (1 << 24) * unicast; } } if (debugMode) { printf("Connection matrix for unicast:\n"); for (j = 0; j < socketcount; j++) { for (i = 0; i < socketcount; i++) { if (i > 0) { printf(" "); } printf("%1.2f", (float)connBC[GRID(i, j, socketcount)] / (float)(1 << 24)); } printf("\n"); } printf("\nConnectionmatrix for broadcast:\n"); for (j = 0; j < socketcount; j++) { for (i = 0; i < socketcount; i++) { if (i > 0) { printf(" "); } printf("%1.2f", (float)connUni[GRID(i, j, socketcount)] / (float)(1 << 24)); } printf("\n"); } } }
static void _DBGBinary(int x,int y,int n,int cOn,int onColour,int cOff,int offColour) { for (int i = 0;i < 8;i++) { if (n & 0x80) { GFXCharacter(GRID(x,y),cOn,GRIDSIZE,onColour,-1); } else { GFXCharacter(GRID(x,y),cOff,GRIDSIZE,offColour,-1); } n = n << 1; x++; if (i == 3) x++; } };
void CityLayer::generateStars() { int numberOfStars = 10; Size minSize = GRID(0, 10); Size maxSize = GRID(35, 35); for (int i = 0; i < numberOfStars; i++) { auto star = Star::create(); float x = MathHelper::RandomFloat(minSize.width, maxSize.width); float y = MathHelper::RandomFloat(minSize.height, maxSize.height); star->setPosition(Point(x, y)); _starLayer->addChild(star); } }
static void Redistribute2D(float samples[][2], SampleGrid &pixelGrid) { ProgressReporter progress(SAMPLE_TABLE_SIZE, "Redistribution"); //NOBOOK for (int currentSample = 1; currentSample < SAMPLE_TABLE_SIZE; ++currentSample) { // Select best lens sample for current image sample int best = -1; // Find best 2D sample relative to neighbors float maxMinDist2 = 0.f; for (int samp = currentSample; samp < SAMPLE_TABLE_SIZE; ++samp) { // Check distance to lens positions at nearby samples int gu = GRID(imageSamples[currentSample][0]); int gv = GRID(imageSamples[currentSample][1]); float minDist2 = INFINITY; for (int du = -1; du <= 1; ++du) { for (int dv = -1; dv <= 1; ++dv) { // Check 2D samples in current grid cell // Compute (u,v) grid cell to check int u = gu + du, v = gv + dv; if (u < 0) u += BC_GRID_SIZE; if (u >= BC_GRID_SIZE) u -= BC_GRID_SIZE; if (v < 0) v += BC_GRID_SIZE; if (v >= BC_GRID_SIZE) v -= BC_GRID_SIZE; for (u_int g = 0; g < pixelGrid[u][v].size(); ++g) { int s2 = pixelGrid[u][v][g]; if (s2 < currentSample) { float dx = Wrapped1DDist(samples[s2][0], samples[samp][0]); float dy = Wrapped1DDist(samples[s2][1], samples[samp][1]); float d2 = dx*dx + dy*dy; minDist2 = min(d2, minDist2); } } } } // Update _best_ for 2D lens sample if it is best so far if (minDist2 > maxMinDist2) { maxMinDist2 = minDist2; best = samp; } } Assert(best != -1); // NOBOOK swap(samples[best][0], samples[currentSample][0]); swap(samples[best][1], samples[currentSample][1]); progress.Update(); //NOBOOK } fprintf(stderr, "\n"); // NOBOOK progress.Done(); //NOBOOK }
void TTInstViewReceiver::setLayout() { createNameLabel(getName()); createChangeButton(); createReconnectButton(); QGroupBox* status_box = getStatusWidget(); layout = new QGridLayout; GRID(layout)->addWidget(getNameLabel(), 0, 0); GRID(layout)->addWidget(status_box, 1, 0); GRID(layout)->addWidget(getChangeButton(), 2, 0); QWidget::setLayout(layout); }
void Grid_vmevent(void *vthis, Screen *screen) { Widget *widget; u16 x, y; for (y = 0; y < GRID(vthis)->c_row; y++) { for (x = 0; x < GRID(vthis)->c_col; x++) { widget = GRID(vthis)->items[y][x].widget; if (widget) { Widget_mevent(widget, screen); if (screen->event_handled) return; } } } Widget_vmevent(vthis, screen); }
void FightingLayer::gameDidEnd() { _readyToPlayAgain = false; this->stopAllActions(); for (std::vector<Enemy *>::size_type i = 0; i != _enemies->size(); i++) { Enemy *enemy = _enemies->at(i); enemy->kill(); _enemies->erase(_enemies->begin() + i); i--; } if (_fadeBackground == NULL) { _fadeBackground = LayerColor::create(Color4B(0, 0, 0, 0), GRID(35, 35).width, GRID(35, 35).height); } _fadeBackground->removeAllChildren(); auto callback = CallFuncN::create(CC_CALLBACK_1(FightingLayer::gameReadyToStart, this)); auto sequence = Sequence::create(EaseQuadraticActionInOut::create(FadeTo::create(0.4, 100)), callback, NULL); _fadeBackground->runAction(sequence); char buff[100]; sprintf(buff, "%li points", _score); auto scoreLabel = Label::createWithTTF(buff, "BebasNeue.ttf", 50); scoreLabel->setPosition(Point(_fadeBackground->getContentSize().width / 2, _fadeBackground->getContentSize().height - 150)); _fadeBackground->addChild(scoreLabel); auto nextLabel = Label::createWithTTF("Press any key to play again", "BebasNeue.ttf", 32); nextLabel->setPosition(Point(_fadeBackground->getContentSize().width / 2, _fadeBackground->getContentSize().height - 200)); _fadeBackground->addChild(nextLabel); this->addChild(_fadeBackground); }
void MainScene::start() { Size size = GRID(35, 35); _fightingLayer = FightingLayer::create(); this->addChild(_fightingLayer); _fightingLayer->setPosition(0, -size.height); // wer { auto transition = EaseSineOut::create(MoveTo::create(1.0, Point(0, 0))); auto sequence = Sequence::create(transition, NULL); _fightingLayer->runAction(sequence); } { auto transition = EaseSineOut::create(MoveTo::create(1.0, GRID_POSITION(17.5, 52.5))); auto sequence = Sequence::create(transition, NULL); _introLabel->runAction(sequence); } { auto transition = EaseSineOut::create(MoveTo::create(1.0, Point(0, size.height))); auto callbackAction = CallFuncN::create(CC_CALLBACK_1(MainScene::gameShouldStart, this)); auto sequence = Sequence::create(transition, callbackAction, NULL); sequence->setTag(1337); _cityLayer->runAction(sequence); } }
void BefungeRunner0::Set(int_grid x, int_grid y, int_grid chr) { if (x < 0 || y < 0 || x >= width || y >= height) return; GRID(x, y) = chr; }
static game_state *new_game(midend *me, game_params *params, char *desc) { game_state *state = snew(game_state); int dlen = strlen(desc), i; unsigned char *bmp; state->minballs = params->minballs; state->maxballs = params->maxballs; state->nballs = ((dlen/2)-2)/2; bmp = hex2bin(desc, state->nballs*2 + 2); obfuscate_bitmap(bmp, (state->nballs*2 + 2) * 8, TRUE); state->w = bmp[0]; state->h = bmp[1]; state->nlasers = 2 * (state->w + state->h); state->grid = snewn((state->w+2)*(state->h+2), unsigned int); memset(state->grid, 0, (state->w+2)*(state->h+2) * sizeof(unsigned int)); state->exits = snewn(state->nlasers, unsigned int); memset(state->exits, LASER_EMPTY, state->nlasers * sizeof(unsigned int)); for (i = 0; i < state->nballs; i++) { GRID(state, bmp[(i+1)*2 + 0]+1, bmp[(i+1)*2 + 1]+1) = BALL_CORRECT; } sfree(bmp); state->done = state->nguesses = state->reveal = state->justwrong = state->nright = state->nwrong = state->nmissed = 0; state->laserno = 1; return state; }
int_grid BefungeRunner0::Get(int_grid x, int_grid y) { if (x < 0 || y < 0 || x >= width || y >= height) return 0; return GRID(x, y); }
void BefungeRunner0::RunSingle() { ExecuteCommand(GRID(pcX, pcY)); Move(); stepCounter++; }
void Grid_vdraw(void *vthis, Screen *screen, b8 flip) { Widget *widget; u16 x, y; Widget_vdraw(vthis, screen, false); for (y = 0; y < GRID(vthis)->c_row; y++) { for (x = 0; x < GRID(vthis)->c_col; x++) { widget = GRID(vthis)->items[y][x].widget; if (widget) { Widget_draw(widget, screen, false); } } } if (flip) Screen_flip(screen); }
void BefungeRunner0::Init(std::vector<std::string> lines) { for (size_t y = 0; y < lines.size(); y++) for (size_t x = 0; x < lines[y].length(); x++) { GRID(x, y) = lines[y][x]; } }
void Grid_vdestroy(void *vthis) { #ifdef VERBOSE_CREATE Static_printObj(vthis); #endif /*! Delete self stuff !*/ if (GRID(vthis)->items) { u16 i = 0; for (; i < GRID(vthis)->c_row; i++) free(GRID(vthis)->items[i]); free(GRID(vthis)->items); GRID(vthis)->items = NULL; GRID(vthis)->c_col = GRID(vthis)->c_row = 0; } free(GRID(vthis)->maxw); GRID(vthis)->maxw = NULL; /*! Delete parent object !*/ Container_vdestroy(CONTAINER(vthis)); }
bool FightingLayer::init() { if (!Layer::init()) { return false; } _bullets = new std::vector<Bullet *>(); _smokes = new std::vector<Smoke *>(); _enemies = new std::vector<Enemy *>(); auto background = TrianglePlane::create(); background->setContentSize(GRID(35, 35)); background->setColor(Color3B(117, 78, 58)); this->addChild(background); _ground = TrianglePlane::create(); _ground->setContentSize(GRID(35, 25)); _ground->setColor(Color3B(84, 56, 42)); _ground->setPosition(GRID_POSITION(0, 2)); this->addChild(_ground); _player = Player::create(); _ground->addChild(_player); this->generateSmoke(); _scoreLabel = Label::createWithTTF("0", "BebasNeue.ttf", 32); _scoreLabel->setAlignment(TextHAlignment::CENTER); _scoreLabel->setPosition(Point(_ground->getContentSize().width / 2, _ground->getContentSize().height + 100 )); this->addChild(_scoreLabel); _discoLayer = DiscoLayer::create(); _ground->addChild(_discoLayer); this->start(); return true; }
static void fire_laser(game_state *state, int entryno) { int tmp, exitno, x, y, direction; tmp = range2grid(state, entryno, &x, &y, &direction); assert(tmp); exitno = fire_laser_internal(state, x, y, direction); if (exitno == LASER_HIT || exitno == LASER_REFLECT) { GRID(state, x, y) = state->exits[entryno] = exitno; } else { int newno = state->laserno++; int xend, yend, unused; tmp = range2grid(state, exitno, &xend, ¥d, &unused); assert(tmp); GRID(state, x, y) = GRID(state, xend, yend) = newno; state->exits[entryno] = exitno; state->exits[exitno] = entryno; } }
bool MainScene::init() { Size size = Director::getInstance()->getWinSize(); if (!LayerColor::initWithColor(Color4B(27, 27, 27, 255), size.width, size.height )) { return false; } Size visibleSize = Director::getInstance()->getWinSize(); // City _cityLayer = CityLayer::create(); _cityLayer->setPositionY(-GRID(0, 35).height); this->addChild(_cityLayer); //this->setPosition(Point(8.0, 8.0)); _label = Label::createWithTTF("Press any key to start", "BebasNeue.ttf", 32); _label->enableShadow(); //_label->enableStroke(Color3B(253, 23, 66), 1.0); _cityLayer->addChild(_label); Point origin = Director::getInstance()->getVisibleOrigin(); _label->setPosition(Point(origin.x + (visibleSize.width / 2), 40)); CocosDenshion::SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(0.2); CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("background_loop.wav", true); _introLabel = Label::createWithTTF("The year is 2016 and the human race has fallen. The earth is now controlled by dead pixels. In an underground disco, far beneath the surface of Berlin – a kickass disco dancer is still alive. Not knowing what has happened outside he still rocks his disco moves.\n\nHelp him battle the intruders by using the arrow keys. To make his fingers flick, use the spacebar. ", "BebasNeue.ttf", 22); this->addChild(_introLabel); _introLabel->setMaxLineWidth(GRID(25,0).width); _introLabel->setPosition(GRID_POSITION(17.5, 17.5)); _introLabel->setAlignment(TextHAlignment::CENTER); auto callback = CallFuncN::create(CC_CALLBACK_1(MainScene::introComplete, this)); auto sequence = Sequence::create(EaseSineOut::create(MoveTo::create(20.0, Point(0, 0))), callback, NULL); _cityLayer->runAction(sequence); return true; }
/* * capture_callback * * This function is called for every package received through libpcap on * one of the connected devices. It retransmit the package to the other * devices (loss propability is defined in connection matrix) and always * to the tap device (if active) * * See libpcap documentation for parameters */ void capture_callback(u_char * args, const struct pcap_pkthdr *hdr, const u_char * packet) { int *index = (int *)args; int unicast = memcmp(packet, mac_bc, 6) != 0; int i, len; len = hdr->len; memcpy(buffer, packet, len); if (tapFD != -1) { int send = 0; while (send < len) { int t = write(tapFD, &buffer[send], len - send); if (t == -1) { printf("Error while sending to tap device!\n"); break; } send += t; } } for (i = 0; i < deviceCount; i++) { u_int32_t prop; if (unicast) { prop = connUni[GRID(*index, i, deviceCount)]; } else { prop = connBC[GRID(*index, i, deviceCount)]; } if (prop == 0 || prop < (rand() % (1 << 24))) { continue; } pcap_inject(devices[i], buffer, len); } }
/* * readConnectionMatrix * * This function reads the network settings and place them in the * connection matrix (float array with size "len" x "len". * * @param pointer to begin of connection matrix * @param pointer to filename * @param number of elements in each connection matrix line * @return 0 if function was successfull, 1 if an error happened */ int readConnectionMatrix(float *connectionMatrix, char *filename, int len) { FILE *file = fopen(filename, "r"); if (!file) { return 1; } char buffer[1024]; while (fgets(buffer, 1024, file)) { int from, to; float propability; char *line = stripString(buffer); DPRINT("%s\n", line); if (line[0] == '#' || line[0] == 0) { continue; } if (sscanf(line, "%d %d %f", &from, &to, &propability) != 3) { continue; } if (from < 0 || from >= len || to < 0 || to >= len || from == to) { continue; } connectionMatrix[GRID(from, to, len)] = propability; if (connectionMatrix[GRID(to, from, len)] == 0) { connectionMatrix[GRID(to, from, len)] = propability; } } fclose(file); return 0; }
bool CityLayer::init() { if (!Layer::init()) { return false; } _skyTrianglePlane = TrianglePlane::create(); _skyTrianglePlane->setPosition(0, 0); _skyTrianglePlane->setContentSize(GRID(35, 35*2)); _skyTrianglePlane->setColor(Color3B(27, 27, 27)); this->addChild(_skyTrianglePlane); _starLayer = Layer::create(); _starLayer->setContentSize(GRID(35, 35)); this->generateStars(); this->addChild(_starLayer); auto building1 = TrianglePlane::create(); building1->setContentSize(GRID(5, 22)); building1->setColor(Color3B(91, 91, 91)); building1->setPosition(GRID_POSITION(4, 4)); this->addChild(building1); auto building2 = TrianglePlane::create(); building2->setContentSize(GRID(5, 25)); building2->setColor(Color3B(55, 55, 55)); building2->setPosition(GRID_POSITION(16, 4)); this->addChild(building2); auto building3 = TrianglePlane::create(); building3->setContentSize(GRID(5, 21)); building3->setColor(Color3B(91, 91, 91)); building3->setPosition(GRID_POSITION(20, 4)); this->addChild(building3); auto grass = TrianglePlane::create(); grass->setContentSize(GRID(35, 1)); grass->setColor(Color3B(59, 75, 27)); grass->setPosition(GRID_POSITION(0, 4)); this->addChild(grass); auto ground = TrianglePlane::create(); ground->setContentSize(GRID(35, 4)); ground->setColor(Color3B(117, 78, 58)); ground->setPosition(GRID_POSITION(0, 0)); this->addChild(ground); return true; }
bool DiscoLayer::init() { if (!ClippingNode::init()) { return false; } auto stencil = LayerColor::create(Color4B(255, 255, 255, 255), GRID(35, 25).width, GRID(35, 25).height); this->setStencil(stencil); _lights = new std::vector<Sprite *>(); auto globe = Globe::create(); globe->setPosition(GRID_POSITION(17.5, 23)); this->addChild(globe, 999); return true; }
BefungeRunner0::BefungeRunner0(int w, int h) { stack.reserve(INITIAL_STACK_SIZE); width = w; height = h; raster = new int_grid[width * height]; for (int x = 0; x < width; x++) for (int y = 0; y < height; y++) { GRID(x, y) = ' '; } pcX = 0; pcY = 0; deltaX = 1; deltaY = 0; stepCounter = 0; }
/* Given a position and a direction, check whether we can see a ball in front * of us, or to our front-left or front-right. */ static int isball(game_state *state, int gx, int gy, int direction, int lookwhere) { debug(("isball, (%d, %d), dir %s, lookwhere %s\n", gx, gy, dirstrs[direction], lookwhere == LOOK_LEFT ? "LEFT" : lookwhere == LOOK_FORWARD ? "FORWARD" : "RIGHT")); OFFSET(gx,gy,direction); if (lookwhere == LOOK_LEFT) OFFSET(gx,gy,direction-1); else if (lookwhere == LOOK_RIGHT) OFFSET(gx,gy,direction+1); else if (lookwhere != LOOK_FORWARD) assert(!"unknown lookwhere"); debug(("isball, new (%d, %d)\n", gx, gy)); /* if we're off the grid (into the firing range) there's never a ball. */ if (gx < 1 || gy < 1 || gx > state->w || gy > state->h) return 0; if (GRID(state, gx,gy) & BALL_CORRECT) return 1; return 0; }
/* * Read the dungeon * * The monsters/objects must be loaded in the same order * that they were stored, since the actual indexes matter. * * Note that the size of the dungeon is now hard-coded to * DUNGEON_HGT by DUNGEON_WID, and any dungeon with another * size will be silently discarded by this routine. * * Note that dungeon objects, including objects held by monsters, are * placed directly into the dungeon, using "object_copy()", which will * copy "iy", "ix", and "held_m_idx", leaving "next_o_idx" blank for * objects held by monsters, since it is not saved in the savefile. * * After loading the monsters, the objects being held by monsters are * linked directly into those monsters. */ static errr rd_dungeon(void) { int i, y, x; int by, bx; s16b town; s16b dungeon; s16b depth; s16b py, px; s16b ymax, xmax; byte count; byte tmp8u; u16b tmp16u; u16b limit; /*** Basic info ***/ /* Header info */ rd_s16b(&depth); rd_s16b(&dungeon); rd_s16b(&py); rd_s16b(&px); rd_s16b(&ymax); rd_s16b(&xmax); rd_s16b(&town); rd_u16b(&tmp16u); /* Ignore illegal dungeons */ if (town>=z_info->t_max) { note(format("Ignoring illegal dungeon (%d)", dungeon)); return (0); } /* Ignore illegal dungeons */ if ((depth < 0) || (depth > max_depth(dungeon))) { note(format("Ignoring illegal dungeon depth (%d)", depth)); return (0); } /* Ignore illegal dungeons */ if ((ymax != DUNGEON_HGT) || (xmax != DUNGEON_WID)) { /* XXX XXX XXX */ note(format("Ignoring illegal dungeon size (%d,%d).", ymax, xmax)); return (0); } /* Ignore illegal dungeons */ if ((px < 0) || (px >= DUNGEON_WID) || (py < 0) || (py >= DUNGEON_HGT)) { note(format("Ignoring illegal player location (%d,%d).", py, px)); return (1); } /*** Run length decoding ***/ /* Load the dungeon data */ for (x = y = 0; y < DUNGEON_HGT; ) { /* Grab RLE info */ rd_byte(&count); rd_byte(&tmp8u); /* Apply the RLE info */ for (i = count; i > 0; i--) { /* Extract "info" */ cave_info[y][x] = tmp8u; /* Advance/Wrap */ if (++x >= DUNGEON_WID) { /* Wrap */ x = 0; /* Advance/Wrap */ if (++y >= DUNGEON_HGT) break; } } } /* Hack -- not fully dynamic */ dyna_full = FALSE; /*** Run length decoding ***/ /* Load the dungeon data */ for (x = y = 0; y < DUNGEON_HGT; ) { /* Grab RLE info */ rd_byte(&count); if (variant_save_feats) rd_u16b(&tmp16u); else rd_byte(&tmp8u); /* Apply the RLE info */ for (i = count; i > 0; i--) { /* Save the feat */ if (variant_save_feats) cave_feat[y][x] = tmp16u; else cave_feat[y][x] = tmp8u; /* Check for los flag set*/ if (f_info[cave_feat[y][x]].flags1 & (FF1_LOS)) { cave_info[y][x] &= ~(CAVE_WALL); } /* Handle wall grids */ else { cave_info[y][x] |= (CAVE_WALL); } /* Handle dynamic grids */ if (f_info[cave_feat[y][x]].flags3 & (FF3_DYNAMIC_MASK)) { if (dyna_n < (DYNA_MAX-1)) { dyna_g[dyna_n++] = GRID(y,x); } else { dyna_full = TRUE; dyna_cent_y = 255; dyna_cent_x = 255; } } /* Advance/Wrap */ if (++x >= DUNGEON_WID) { /* Wrap */ x = 0; /* Advance/Wrap */ if (++y >= DUNGEON_HGT) break; } } } /*** Player ***/ /* Fix depth */ if (depth < 1) depth = min_depth(dungeon); /* Fix depth */ if (depth > max_depth(dungeon)) depth = max_depth(dungeon); /* Load depth */ p_ptr->depth = depth; p_ptr->dungeon = dungeon; p_ptr->town = town; /* Place player in dungeon */ if (!player_place(py, px)) { note(format("Cannot place player (%d,%d)!", py, px)); return (-1); } /*** Room descriptions ***/ if (!variant_room_info) { /* Initialize the room table */ for (by = 0; by < MAX_ROOMS_ROW; by++) { for (bx = 0; bx < MAX_ROOMS_COL; bx++) { dun_room[by][bx] = 0; } } /* Initialise 'zeroeth' room description */ room_info[0].flags = 0; } else { for (bx = 0; bx < MAX_ROOMS_ROW; bx++) { for (by = 0; by < MAX_ROOMS_COL; by++) { rd_byte(&dun_room[bx][by]); } } for (i = 1; i < DUN_ROOMS; i++) { int j; rd_byte(&room_info[i].type); rd_byte(&room_info[i].flags); if (room_info[i].type == ROOM_NORMAL) { for (j = 0; j < ROOM_DESC_SECTIONS; j++) { rd_s16b(&room_info[i].section[j]); if (room_info[i].section[j] == -1) break; } } } } /*** Objects ***/ /* Read the item count */ rd_u16b(&limit); /* Verify maximum */ if (limit > z_info->o_max) { note(format("Too many (%d) object entries!", limit)); return (-1); } /* Read the dungeon items */ for (i = 1; i < limit; i++) { object_type *i_ptr; object_type object_type_body; s16b o_idx; object_type *o_ptr; /* Get the object */ i_ptr = &object_type_body; /* Wipe the object */ object_wipe(i_ptr); /* Read the item */ if (rd_item(i_ptr)) { note("Error reading item"); return (-1); } /* Make an object */ o_idx = o_pop(); /* Paranoia */ if (o_idx != i) { note(format("Cannot place object %d!", i)); return (-1); } /* Get the object */ o_ptr = &o_list[o_idx]; /* Structure Copy */ object_copy(o_ptr, i_ptr); /* Dungeon floor */ if (!i_ptr->held_m_idx) { int x = i_ptr->ix; int y = i_ptr->iy; /* ToDo: Verify coordinates */ /* Link the object to the pile */ o_ptr->next_o_idx = cave_o_idx[y][x]; /* Link the floor to the object */ cave_o_idx[y][x] = o_idx; } } /*** Monsters ***/ /* Read the monster count */ rd_u16b(&limit); /* Hack -- verify */ if (limit > z_info->m_max) { note(format("Too many (%d) monster entries!", limit)); return (-1); } /* Read the monsters */ for (i = 1; i < limit; i++) { monster_type *n_ptr; monster_type monster_type_body; /* Get local monster */ n_ptr = &monster_type_body; /* Clear the monster */ (void)WIPE(n_ptr, monster_type); /* Read the monster */ rd_monster(n_ptr); /* Place monster in dungeon */ if (monster_place(n_ptr->fy, n_ptr->fx, n_ptr) != i) { note(format("Cannot place monster %d", i)); return (-1); } } /*** Holding ***/ /* Reacquire objects */ for (i = 1; i < o_max; ++i) { object_type *o_ptr; monster_type *m_ptr; /* Get the object */ o_ptr = &o_list[i]; /* Ignore dungeon objects */ if (!o_ptr->held_m_idx) continue; /* Verify monster index */ if (o_ptr->held_m_idx >= z_info->m_max) { note("Invalid monster index"); return (-1); } /* Get the monster */ m_ptr = &m_list[o_ptr->held_m_idx]; /* Link the object to the pile */ o_ptr->next_o_idx = m_ptr->hold_o_idx; /* Link the monster to the object */ m_ptr->hold_o_idx = i; } /*** Success ***/ /* The dungeon is ready */ character_dungeon = TRUE; /* Regenerate town in post 2.9.3 versions */ if (older_than(2, 9, 4) && (p_ptr->depth == 0)) character_dungeon = FALSE; /* Success */ return (0); }
int main(int argc, char* argv[]) { // command line error check if (argc < 2) { std::cout << "Usage: " << argv[0] << " [--help] infile [outfile]\n\n"; exit(-1); } // help diagnostic if (std::string(argv[1]) == "--help") { std::cout << argv[0] << ": convert MMSP grid data to VTK image data format.\n"; std::cout << "Usage: " << argv[0] << " [--help] infile [outfile]\n\n"; std::cout << "Questions/comments to [email protected] (Jason Gruber).\n\n"; exit(0); } // file open error check std::ifstream input(argv[1]); if (!input) { std::cerr << "File input error: could not open " << argv[1] << ".\n\n"; exit(-1); } // generate output file name std::stringstream filename; if (argc < 3) filename << std::string(argv[1]).substr(0, std::string(argv[1]).find_last_of(".")) << ".vti"; else filename << argv[2]; std::ofstream output(filename.str().c_str()); if (!output) { std::cerr << "File output error: could not open "; std::cerr << filename.str() << "." << std::endl; exit(-1); } // read data type std::string type; getline(input, type, '\n'); // grid type error check if (type.substr(0, 4) != "grid") { std::cerr << "File input error: file does not contain grid data." << std::endl; exit(-1); } // parse data type bool bool_type = (type.find("bool") != std::string::npos); bool char_type = (type.find("char") != std::string::npos); bool unsigned_char_type = (type.find("unsigned char") != std::string::npos); bool int_type = (type.find("int") != std::string::npos); bool unsigned_int_type = (type.find("unsigned int") != std::string::npos); bool long_type = (type.find("long") != std::string::npos); bool unsigned_long_type = (type.find("unsigned long") != std::string::npos); bool short_type = (type.find("short") != std::string::npos); bool unsigned_short_type = (type.find("unsigned short") != std::string::npos); bool float_type = (type.find("float") != std::string::npos); bool double_type = (type.find("double") != std::string::npos); bool long_double_type = (type.find("long double") != std::string::npos); bool scalar_type = (type.find("scalar") != std::string::npos); bool vector_type = (type.find("vector") != std::string::npos); bool sparse_type = (type.find("sparse") != std::string::npos); if (not bool_type and not char_type and not unsigned_char_type and not int_type and not unsigned_int_type and not long_type and not unsigned_long_type and not short_type and not unsigned_short_type and not float_type and not double_type and not long_double_type) { std::cerr << "File input error: unknown grid data type." << std::endl; exit(-1); } if (not sparse_type) { std::cerr << "File input error: sparse data expected." << std::endl; } // read grid dimension int dim; input >> dim; // read number of fields int fields; input >> fields; // read grid sizes int x0[3] = {0, 0, 0}; int x1[3] = {0, 0, 0}; for (int i = 0; i < dim; i++) input >> x0[i] >> x1[i]; // read cell spacing float dx[3] = {1.0, 1.0, 1.0}; for (int i = 0; i < dim; i++) input >> dx[i]; // ignore trailing endlines input.ignore(10, '\n'); // determine byte order std::string byte_order; if (0x01 & static_cast<int>(1)) byte_order = "LittleEndian"; else byte_order = "BigEndian"; // output header markup output << "<?xml version=\"1.0\"?>\n"; output << "<VTKFile type=\"ImageData\" version=\"0.1\" byte_order=\"" << byte_order << "\">\n"; if (dim == 1) { output << " <ImageData WholeExtent=\"" << x0[0] << " " << x1[0] << " 0 0 0 0\""; output << " Origin=\"0 0 0\" Spacing=\"" << dx[0] << " 1 1\">\n"; } if (dim == 2) { output << " <ImageData WholeExtent=\"" << x0[1] << " " << x1[1] << " " << x0[0] << " " << x1[0] << " 0 0\""; output << " Origin=\"0 0 0\" Spacing=\"" << dx[1] << " " << dx[0] << " 1\">\n"; } if (dim == 3) { output << " <ImageData WholeExtent=\"" << x0[2] << " " << x1[2] << " " << x0[1] << " " << x1[1] << " " << x0[0] << " " << x1[0] << "\""; output << " Origin=\"0 0 0\" Spacing=\"" << dx[2] << " " << dx[1] << " " << dx[0] << "\">\n"; } // read number of blocks int blocks; input.read(reinterpret_cast<char*>(&blocks), sizeof(blocks)); for (int i = 0; i < blocks; i++) { // read block limits int lmin[3] = {0, 0, 0}; int lmax[3] = {0, 0, 0}; for (int j = 0; j < dim; j++) { input.read(reinterpret_cast<char*>(&lmin[j]), sizeof(lmin[j])); input.read(reinterpret_cast<char*>(&lmax[j]), sizeof(lmax[j])); } int blo[dim]; int bhi[dim]; // read boundary conditions for (int j = 0; j < dim; j++) { input.read(reinterpret_cast<char*>(&blo[j]), sizeof(blo[j])); input.read(reinterpret_cast<char*>(&bhi[j]), sizeof(bhi[j])); } // write header markup if (dim == 1) output << " <Piece Extent=\"" << lmin[0] << " " << lmax[0] << " 0 0 0 0\">\n"; if (dim == 2) output << " <Piece Extent=\"" << lmin[1] << " " << lmax[1] << " " << lmin[0] << " " << lmax[0] << " 0 0\">\n"; if (dim == 3) output << " <Piece Extent=\"" << lmin[2] << " " << lmax[2] << " " << lmin[1] << " " << lmax[1] << " " << lmin[0] << " " << lmax[0] << "\">\n"; // write cell data markup if (scalar_type) { output << " <CellData>\n"; output << " <DataArray Name=\"scalar_data\""; } else if (vector_type) { output << " <CellData>\n"; output << " <DataArray Name=\"vector_data\" NumberOfComponents=\"" << fields << "\""; } else if (sparse_type) { output << " <CellData>\n"; output << " <DataArray Name=\"grain_id\""; } else { /* built-in data types */ output << " <CellData>\n"; output << " <DataArray Name=\"scalar_data\""; } if (bool_type) output << " type=\"UInt8\" format=\"ascii\">\n"; else if (char_type) output << " type=\"Int8\" format=\"ascii\">\n"; else if (unsigned_char_type) output << " type=\"UInt8\" format=\"ascii\">\n"; else if (int_type) output << " type=\"Int32\" format=\"ascii\">\n"; else if (unsigned_int_type) output << " type=\"UInt32\" format=\"ascii\">\n"; else if (long_type) output << " type=\"Int32\" format=\"ascii\">\n"; else if (unsigned_long_type) output << " type=\"UInt32\" format=\"ascii\">\n"; else if (short_type) output << " type=\"Int16\" format=\"ascii\">\n"; else if (unsigned_short_type) output << " type=\"UInt16\" format=\"ascii\">\n"; else if (float_type) output << " type=\"Float32\" format=\"ascii\">\n"; else if (double_type && !sparse_type) output << " type=\"Float64\" format=\"ascii\">\n"; else if (double_type && sparse_type) output << " type=\"UInt16\" format=\"ascii\">\n"; else if (long_double_type) output << " type=\"Float128\" format=\"ascii\">\n"; // read grid data unsigned long size, rawSize; input.read(reinterpret_cast<char*>(&rawSize), sizeof(rawSize)); // read raw size input.read(reinterpret_cast<char*>(&size), sizeof(size)); // read compressed size char* compressed_buffer = new char[size]; input.read(compressed_buffer, size); char* buffer; if (size!=rawSize) { // Decompress data buffer = new char[rawSize]; int status; status = uncompress(reinterpret_cast<unsigned char*>(buffer), &rawSize, reinterpret_cast<unsigned char*>(compressed_buffer), size); switch( status ) { case Z_OK: break; case Z_MEM_ERROR: std::cerr << "Uncompress: out of memory." << std::endl; exit(1); break; case Z_BUF_ERROR: std::cerr << "Uncompress: output buffer wasn't large enough." << std::endl; exit(1); break; } delete [] compressed_buffer; } else { buffer=compressed_buffer; compressed_buffer=NULL; } // write grid data if (sparse_type) { if (bool_type) { if (dim == 1) { MMSP::grid<1, MMSP::sparse<bool> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { bool sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<bool>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 2) { MMSP::grid<2, MMSP::sparse<bool> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { bool sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<bool>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 3) { MMSP::grid<3, MMSP::sparse<bool> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { bool sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<bool>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } } if (char_type) { if (dim == 1) { MMSP::grid<1, MMSP::sparse<char> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { char sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<char>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 2) { MMSP::grid<2, MMSP::sparse<char> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { char sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<char>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 3) { MMSP::grid<3, MMSP::sparse<char> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { char sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<char>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } } if (unsigned_char_type) { if (dim == 1) { MMSP::grid<1, MMSP::sparse<unsigned char> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { unsigned char sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<unsigned char>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 2) { MMSP::grid<2, MMSP::sparse<unsigned char> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { unsigned char sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<unsigned char>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 3) { MMSP::grid<3, MMSP::sparse<unsigned char> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { unsigned char sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<unsigned char>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } } if (int_type) { if (dim == 1) { MMSP::grid<1, MMSP::sparse<int> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { int sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<int>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 2) { MMSP::grid<2, MMSP::sparse<int> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { int sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<int>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 3) { MMSP::grid<3, MMSP::sparse<int> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { int sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<int>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } } if (unsigned_int_type) { if (dim == 1) { MMSP::grid<1, MMSP::sparse<unsigned int> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { unsigned int sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<unsigned int>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 2) { MMSP::grid<2, MMSP::sparse<unsigned int> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { unsigned int sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<unsigned int>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 3) { MMSP::grid<3, MMSP::sparse<unsigned int> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { unsigned int sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<unsigned int>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } } if (long_type) { if (dim == 1) { MMSP::grid<1, MMSP::sparse<long> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { long sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<long>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 2) { MMSP::grid<2, MMSP::sparse<long> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { long sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<long>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 3) { MMSP::grid<3, MMSP::sparse<long> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { long sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<long>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } } if (unsigned_long_type) { if (dim == 1) { MMSP::grid<1, MMSP::sparse<unsigned long> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { unsigned long sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<unsigned long>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 2) { MMSP::grid<2, MMSP::sparse<unsigned long> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { unsigned long sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<unsigned long>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 3) { MMSP::grid<3, MMSP::sparse<unsigned long> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { unsigned long sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<unsigned long>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } } if (short_type) { if (dim == 1) { MMSP::grid<1, MMSP::sparse<short> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { short sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<short>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 2) { MMSP::grid<2, MMSP::sparse<short> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { short sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<short>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 3) { MMSP::grid<3, MMSP::sparse<short> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { short sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<short>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } } if (unsigned_short_type) { if (dim == 1) { MMSP::grid<1, MMSP::sparse<unsigned short> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { unsigned short sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<unsigned short>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 2) { MMSP::grid<2, MMSP::sparse<unsigned short> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { unsigned short sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<unsigned short>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 3) { MMSP::grid<3, MMSP::sparse<unsigned short> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { unsigned short sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<unsigned short>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } } // === FLOAT === if (float_type) { if (dim == 1) { MMSP::grid<1, MMSP::sparse<float> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) output << GRID(k).grain_id() << " "; } else if (dim == 2) { MMSP::grid<2, MMSP::sparse<float> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) output << GRID(k).grain_id() << " "; } else if (dim == 3) { MMSP::grid<3, MMSP::sparse<float> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) output << GRID(k).grain_id() << " "; } } // === DOUBLE === if (double_type) { if (dim == 1) { MMSP::grid<1, MMSP::sparse<double> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) output << GRID(k).grain_id() << " "; } else if (dim == 2) { MMSP::grid<2, MMSP::sparse<double> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) output << GRID(k).grain_id() << " "; } else if (dim == 3) { MMSP::grid<3, MMSP::sparse<double> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) output << GRID(k).grain_id() << " "; } } if (long_double_type) { if (dim == 1) { MMSP::grid<1, MMSP::sparse<long double> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { long double sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<long double>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 2) { MMSP::grid<2, MMSP::sparse<long double> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { long double sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<long double>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } else if (dim == 3) { MMSP::grid<3, MMSP::sparse<long double> > GRID(fields, lmin, lmax); GRID.from_buffer(buffer); for (int k = 0; k < MMSP::nodes(GRID); k++) { long double sum = 0; int nonzero = MMSP::length(GRID(k)); for (int h = 0; h < nonzero; h++) sum += static_cast<long double>(GRID(k).value(h) * GRID(k).value(h)); output << sum << " "; } } } } // clean up delete [] buffer; // write closing markup output << "\n"; output << " </DataArray>\n"; output << " </CellData>\n"; output << " </Piece>\n"; } // output closing markup output << " </ImageData>\n"; output << "</VTKFile>\n"; }
int main(int argc, char **argv) { int i; int dropPropability = 0; char *connectionFile = NULL; int deviceIndex = -1; int hubMode = 0; MacAddress mac; char *tapname = NULL; if (argc == 1 || (argc == 2 && strcmp(argv[1], "--help") == 0)) { printf("%s: [-con <connectionfile>] [-hub] [-debug]" " [-tap <devname> <mac>] [-drop] -dev <dev1> <dev2> <dev3>...\n", argv[0]); return 0; } deviceCount = 0; tapFD = -1; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-con") == 0 && i < argc - 1) { connectionFile = argv[i + 1]; i++; } if (strcmp(argv[i], "-drop") == 0) { dropPropability = 1; } if (strcmp(argv[i], "-dev") == 0) { deviceIndex = ++i; while (i < argc && argv[i][0] != '-') { i++; deviceCount++; } i--; /* to cancel the i++ in the for loop */ } if (strcmp(argv[i], "-hub") == 0) { hubMode = 1; } if (strcmp(argv[i], "-tap") == 0 && i < argc - 2) { tapname = argv[++i]; readMac(argv[++i], &mac); } if (strcmp(argv[i], "-debug") == 0) { debugMode = 1; } } if (hubMode == 1 && connectionFile != NULL) { printf("Error, you cannot set matrix file in hub mode.\n"); return 1; } if (connectionFile == NULL && hubMode == 0) { printf("Error, netsim needs a matrix file for connections if not running in hub mode.\n"); return 1; } if (deviceIndex < 0) { printf("Error, you must specify the devices the programm connects to.\n"); return 1; } if (deviceCount < 2) { printf("Error, you need to bind at least two devices to the bridge.\n"); return 1; } if (tapname) { tapFD = createTap(tapname, &mac); if (tapFD == -1) { printf("Error, cannot open tap device '%s'\n", tapname); return 1; } } running = 1; float *connMatrix = malloc(sizeof(float) * deviceCount * deviceCount); if (!connMatrix) { printf("Error, not enough memory for mac buffer!"); if (tapFD != -1) closeTap(tapFD); return 1; } if (hubMode) { int x, y; /* * In hub mode the any-to-any loss factor is 1.0, which * means we can skip reading a matrix file. */ for (y = 0; y < deviceCount; y++) { for (x = 0; x < deviceCount; x++) { if (x != y) { connMatrix[GRID(x, y, deviceCount)] = 1.0; } } } } else { if (readConnectionMatrix(connMatrix, connectionFile, deviceCount)) { printf("Error while reading matrix file\n"); free(connMatrix); if (tapFD != -1) closeTap(tapFD); return 1; } } calculateConnections(connMatrix, deviceCount, dropPropability); free(connMatrix); char errbuf[PCAP_ERRBUF_SIZE]; int maxDeviceFD = 0; if (tapFD != -1) { maxDeviceFD = tapFD; } for (i = 0; i < deviceCount; i++) { devices[i] = pcap_open_live(argv[i + deviceIndex], BUFSIZ, 0, -1, errbuf); deviceFD[i] = -1; if (devices[i] == NULL) { printf("Error, cannot open pcap for device '%s'.\n", argv[i + deviceIndex]); running = 0; } else { deviceFD[i] = pcap_fileno(devices[i]); if (deviceFD[i] > maxDeviceFD) { maxDeviceFD = deviceFD[i]; } } } /* activate signal handling */ signal(SIGABRT, &signalHandler); signal(SIGTERM, &signalHandler); signal(SIGQUIT, &signalHandler); signal(SIGINT, &signalHandler); while (running) { fd_set socketSet; int socketsReady; struct timeval timeout; timeout.tv_sec = 1; timeout.tv_usec = 0; FD_ZERO(&socketSet); for (i = 0; i < deviceCount; i++) { FD_SET(deviceFD[i], &socketSet); } if (tapFD != -1) { FD_SET(tapFD, &socketSet); } socketsReady = select(maxDeviceFD + 1, &socketSet, (fd_set *) 0, (fd_set *) 0, &timeout); if (socketsReady <= 0) { break; } for (i = 0; i < deviceCount; i++) { if (FD_ISSET(deviceFD[i], &socketSet)) { int error = pcap_dispatch(devices[i], -1, capture_callback, (u_char *) & i); if (error == -1) { printf("Error during pcap_dispatch for device %s\n", argv[i + deviceIndex]); running = 0; break; } } } if (tapFD != -1 && FD_ISSET(tapFD, &socketSet)) { tap_callback(); } } for (i = 0; i < deviceCount; i++) { if (devices[i] != NULL) { pcap_close(devices[i]); } } free(connUni); free(connBC); if (tapFD != -1) closeTap(tapFD); return 0; }
static void bilateral_filter (GeglBuffer *src, const GeglRectangle *src_rect, GeglBuffer *dst, const GeglRectangle *dst_rect, gint s_sigma, gfloat r_sigma) { gint c, x, y, z, k, i; const gint padding_xy = 2; const gint padding_z = 2; const gint width = src_rect->width; const gint height = src_rect->height; const gint channels = 4; const gint sw = (width -1) / s_sigma + 1 + (2 * padding_xy); const gint sh = (height-1) / s_sigma + 1 + (2 * padding_xy); const gint depth = (int)(1.0f / r_sigma) + 1 + (2 * padding_z); /* down-sampling */ gfloat *grid = g_new0 (gfloat, sw * sh * depth * channels * 2); gfloat *blurx = g_new0 (gfloat, sw * sh * depth * channels * 2); gfloat *blury = g_new0 (gfloat, sw * sh * depth * channels * 2); gfloat *blurz = g_new0 (gfloat, sw * sh * depth * channels * 2); gfloat *input = g_new0 (gfloat, width * height * channels); gfloat *smoothed = g_new0 (gfloat, width * height * channels); #define INPUT(x,y,c) input[c+channels*(x + width * y)] #define GRID(x,y,z,c,i) grid [i+2*(c+channels*(x+sw*(y+z*sh)))] #define BLURX(x,y,z,c,i) blurx[i+2*(c+channels*(x+sw*(y+z*sh)))] #define BLURY(x,y,z,c,i) blury[i+2*(c+channels*(x+sw*(y+z*sh)))] #define BLURZ(x,y,z,c,i) blurz[i+2*(c+channels*(x+sw*(y+z*sh)))] gegl_buffer_get (src, src_rect, 1.0, babl_format ("RGBA float"), input, GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE); for (k=0; k < (sw * sh * depth * channels * 2); k++) { grid [k] = 0.0f; blurx[k] = 0.0f; blury[k] = 0.0f; blurz[k] = 0.0f; } #if 0 /* in case we want to normalize the color space */ gfloat input_min[4] = { FLT_MAX, FLT_MAX, FLT_MAX}; gfloat input_max[4] = {-FLT_MAX, -FLT_MAX, -FLT_MAX}; for(y = 0; y < height; y++) for(x = 0; x < width; x++) for(c = 0; c < channels; c++) { input_min[c] = MIN(input_min[c], INPUT(x,y,c)); input_max[c] = MAX(input_max[c], INPUT(x,y,c)); } #endif /* downsampling */ for(y = 0; y < height; y++) for(x = 0; x < width; x++) for(c = 0; c < channels; c++) { const float z = INPUT(x,y,c); // - input_min[c]; const int small_x = (int)((float)(x) / s_sigma + 0.5f) + padding_xy; const int small_y = (int)((float)(y) / s_sigma + 0.5f) + padding_xy; const int small_z = (int)((float)(z) / r_sigma + 0.5f) + padding_z; g_assert(small_x >= 0 && small_x < sw); g_assert(small_y >= 0 && small_y < sh); g_assert(small_z >= 0 && small_z < depth); GRID(small_x, small_y, small_z, c, 0) += INPUT(x, y, c); GRID(small_x, small_y, small_z, c, 1) += 1.0f; } /* blur in x, y and z */ /* XXX: we could use less memory, but at expense of code readability */ for (z = 1; z < depth-1; z++) for (y = 1; y < sh-1; y++) for (x = 1; x < sw-1; x++) for(c = 0; c < channels; c++) for (i=0; i<2; i++) BLURX(x, y, z, c, i) = (GRID (x-1, y, z, c, i) + 2.0f * GRID (x, y, z, c, i) + GRID (x+1, y, z, c, i)) / 4.0f; for (z = 1; z < depth-1; z++) for (y = 1; y < sh-1; y++) for (x = 1; x < sw-1; x++) for(c = 0; c < channels; c++) for (i=0; i<2; i++) BLURY(x, y, z, c, i) = (BLURX (x, y-1, z, c, i) + 2.0f * BLURX (x, y, z, c, i) + BLURX (x, y+1, z, c, i)) / 4.0f; for (z = 1; z < depth-1; z++) for (y = 1; y < sh-1; y++) for (x = 1; x < sw-1; x++) for(c = 0; c < channels; c++) for (i=0; i<2; i++) BLURZ(x, y, z, c, i) = (BLURY (x, y-1, z, c, i) + 2.0f * BLURY (x, y, z, c, i) + BLURY (x, y+1, z, c, i)) / 4.0f; /* trilinear filtering */ for (y=0; y < height; y++) for (x=0; x < width; x++) for(c = 0; c < channels; c++) { float xf = (float)(x) / s_sigma + padding_xy; float yf = (float)(y) / s_sigma + padding_xy; float zf = INPUT(x,y,c) / r_sigma + padding_z; int x1 = CLAMP((int)xf, 0, sw-1); int y1 = CLAMP((int)yf, 0, sh-1); int z1 = CLAMP((int)zf, 0, depth-1); int x2 = CLAMP(x1+1, 0, sw-1); int y2 = CLAMP(y1+1, 0, sh-1); int z2 = CLAMP(z1+1, 0, depth-1); float x_alpha = xf - x1; float y_alpha = yf - y1; float z_alpha = zf - z1; float interpolated[2]; g_assert(xf >= 0 && xf < sw); g_assert(yf >= 0 && yf < sh); g_assert(zf >= 0 && zf < depth); for (i=0; i<2; i++) interpolated[i] = lerp(lerp(lerp(BLURZ(x1, y1, z1, c, i), BLURZ(x2, y1, z1, c, i), x_alpha), lerp(BLURZ(x1, y2, z1, c, i), BLURZ(x2, y2, z1, c, i), x_alpha), y_alpha), lerp(lerp(BLURZ(x1, y1, z2, c, i), BLURZ(x2, y1, z2, c, i), x_alpha), lerp(BLURZ(x1, y2, z2, c, i), BLURZ(x2, y2, z2, c, i), x_alpha), y_alpha), z_alpha); smoothed[channels*(y*width+x)+c] = interpolated[0] / interpolated[1]; } gegl_buffer_set (dst, dst_rect, 0, babl_format ("RGBA float"), smoothed, GEGL_AUTO_ROWSTRIDE); g_free (grid); g_free (blurx); g_free (blury); g_free (blurz); g_free (input); g_free (smoothed); }
void testInvalid() { GRID(bogus); }