void Viewer::moveCursor(int& y, int& x, int c) { int amnt = 1; if (isViewMode()) amnt = 8; //If we're in view mode, just change currY/X if (isViewMode()) { if (c == KEY_LEFT) { currX -= amnt; if (currX < 0) currX = 0; } else if (c == KEY_RIGHT) { currX += amnt; if (currX + getXSize() > w.getXSize()) currX = w.getXSize() - getXSize() + 2; } else if (c == KEY_UP) { currY -= amnt; if (currY < 0) currY = 0; } else if (c == KEY_DOWN) { currY += amnt; if (currY + getYSize() > w.getYSize()) currY = w.getYSize() - getYSize() + 2; } } //Otherwise, just change x and y and then scroll else { if (c == KEY_LEFT) { if (x > amnt) x-=amnt; else x = 1; } else if (c == KEY_RIGHT) { if (x < getXSize()-1-amnt) x+=amnt; else x = getXSize()-2; } else if (c == KEY_UP) { if (y > amnt) y-=amnt; else y = 1; } else if (c == KEY_DOWN) { if (y < getYSize()-1-amnt) y+=amnt; else y = getYSize()-2; } scrollScreen(y, x); } }
bool MaskedImage::writeMaskHDF5File(const UString & fileName) const { hid_t fileID = H5Fopen(fileName, H5F_ACC_RDWR, H5P_DEFAULT); if(fileID < 0) { fprintf(stderr, "MaskedImage::writeMaskHDF5File: Could not create file %s\n", (const char *)fileName); return false; } UString dataSetName = "/mask"; hsize_t dimensions[2]; dimensions[0] = (hsize_t)getYSize(); dimensions[1] = (hsize_t)getXSize(); herr_t status = H5LTmake_dataset(fileID, dataSetName, 2, dimensions, H5T_NATIVE_UCHAR, mask.getData()); if((status < 0)) { fprintf(stderr, "MaskedImage::writeMaskHDF5File: Could not make dataset %s in file %s\n", (const char *)dataSetName, (const char *)fileName); return false; } status = H5Fclose(fileID); return true; }
bool MaskedImage::advect(const VectorField2D & velocity, double deltaT, UArray<double> & inOutX, UArray<double> & inOutY, double errorTolerance, SInt32 maximumTimeStepCount) { if(deltaT == 0.0) return true; ParticleIntegrator integrator(velocity); double nextTimeStep = 0.1*deltaT; double minimumTimeStep = fabs(0.1*deltaT/maximumTimeStepCount); SInt32 imageSize = getXSize()*getYSize(); if(inOutX.getSize() != imageSize || inOutY.getSize() != imageSize) return false; SInt32 maxCount = 1000000; // keeps us from allocating too much memory at a time UArray<double> newData(imageSize); UArray<bool> newMask(imageSize); for(SInt32 imageIndex = 0; imageIndex < imageSize; imageIndex += maxCount) { SInt32 localCount = std::min(maxCount, imageSize-imageIndex); // set up the points, one for each output pixel UArray<double> points(2*localCount); for(SInt32 index = 0; index < localCount; index++) { points[2*index] = inOutX[index + imageIndex]; points[2*index+1] = inOutY[index + imageIndex]; } // integrate the pixels backward in time to their locations in the original image if(!integrator.integrate(points, deltaT, 0, errorTolerance, nextTimeStep, minimumTimeStep, maximumTimeStepCount)) return false; // interpolate the advected image at the points in the original image UArray<double> pixelX(localCount), pixelY(localCount), advectedPixels(localCount); UArray<bool> advectedMask(localCount); for(SInt32 index = 0; index < localCount; index++) { pixelX[index] = points[2*index]; pixelY[index] = points[2*index+1]; inOutX[index + imageIndex] = points[2*index]; inOutY[index + imageIndex] = points[2*index+1]; } maskedInterpolate(pixelX, pixelY, advectedPixels, advectedMask); // copy the advected pixels and mask back into this image for(SInt32 index = 0; index < localCount; index++) { newData[index + imageIndex] = advectedPixels[index]; newMask[index + imageIndex] = advectedMask[index]; } } getData().copy(newData); mask.copy(newMask); return true; }
bool MaskedImage::readMaskHDF5File(const UString & fileName) { hid_t fileID = H5Fopen(fileName, H5F_ACC_RDONLY, H5P_DEFAULT); if(fileID < 0) { fprintf(stderr, "MaskedImage::readMaskHDF5File: Could not open file %s\n", (const char *)fileName); return false; } UString dataSetName = "/mask"; hsize_t dimensions[2]; H5T_class_t typeClass; size_t typeSize; herr_t status = H5LTget_dataset_info(fileID, dataSetName, dimensions, &typeClass, &typeSize); if(status < 0) { dataSetName.makeUpper(); status = H5LTget_dataset_info(fileID, dataSetName, dimensions, &typeClass, &typeSize); } if((status < 0) || (typeClass != H5T_INTEGER) || (typeSize != 1) || (getYSize() != (SInt32)dimensions[0]) || (getXSize() != (SInt32)dimensions[1])) { fprintf(stderr, "MaskedImage::readMaskHDF5File: Could not get dataset info for %s in file %s\n", (const char *)dataSetName, (const char *)fileName); return false; } mask.setSize(getXSize()*getYSize()); status = H5LTread_dataset(fileID, dataSetName, H5T_NATIVE_UCHAR, mask.getData()); if(status < 0) { fprintf(stderr, "MaskedImage::readMaskHDF5File: Could not read dataset %s in file %s\n", (const char *)dataSetName, (const char *)fileName); return false; } status = H5Fclose(fileID); return true; }
bool MaskedImage::advect(const VectorField2D & velocity, double deltaT, double errorTolerance, SInt32 maximumTimeStepCount) { SInt32 imageSize = getXSize()*getYSize(); UArray<double> x(imageSize), y(imageSize); for(SInt32 imageIndex = 0; imageIndex < imageSize; imageIndex++) { SInt32 yIndex = (imageIndex)/getXSize(); SInt32 xIndex = (imageIndex) - getXSize()*yIndex; x[imageIndex] = getX()[xIndex]; y[imageIndex] = getY()[yIndex]; } return advect(velocity, deltaT, x, y, errorTolerance, maximumTimeStepCount); }
/** * @brief Standard matrix multiplication. * @param matr Second matrix with same Y size as this matrix's X size. * @return Resultant matrix with X of the second matrix and Y of the first matrix. */ Matrix Matrix::operator*(const Matrix& matr) { //Note: this algorithm assumes that this matrix is the matrix on the LEFT. Matrix result; //The columns of the first MUST match the rows of the second. if (getXSize() != matr.getYSize()) return result; result.setSize(matr.getXSize(), getYSize()); //Size is equal to columns of the second by the rows of the first for (int iY = 0; iY < getYSize(); iY++) //Rows of the first { for (int iX = 0; iX < matr.getXSize(); iX++) { double sum = 0; for (int i = 0; i < getXSize(); i++) sum += getValue(i, iY) * matr.getValue(iX, i); result.setValue(sum, iX, iY); } } return result; }
Matrix Matrix::operator*(const Matrix& matr) { Matrix result; //Perform rows vs column check if (ySize != matr.getXSize()) return result; result.setSize(xSize, matr.getYSize()); for (int y = 0; y != result.getXSize(); ++y) { for (int x = 0; x != result.getYSize(); x++) { double sum = 0; for (int i = 0; i != getYSize(); ++ i) sum += getValue(i, y) * matr.getValue(x, i); result.setValue(sum, x, y); } } return result; }
void MaskedImage::maskedInterpolateAtOffsetPixels(double xLower, double yLower, SInt32 xCount, SInt32 yCount, UArray<double> & outValues, UArray<bool> & outMask) const { U_ASSERT((xCount*yCount == outValues.getSize()) && (xCount*yCount == outMask.getSize())); static double cubicSplineMatrix[4*4] = {0, 1, 0, 0, -0.5, 0, 0.5, 0, 1, -2.5, 2, -0.5, -0.5, 1.5, -1.5, 0.5}; double xPowers[4]; double yPowers[4]; xPowers[0] = 1.0; yPowers[0] = 1.0; double x = (xLower - getXLower())/getDeltaX(); double y = (yLower - getYLower())/getDeltaY(); SInt32 xLowerIndex = (SInt32)x; SInt32 yLowerIndex = (SInt32)y; x = x - (double)xLowerIndex; y = y - (double)yLowerIndex; xPowers[1] = x; xPowers[2] = x*x; xPowers[3] = x*x*x; yPowers[1] = y; yPowers[2] = y*y; yPowers[3] = y*y*y; double coefficients[4*4]; for(SInt32 i = 0; i < 16; i++) coefficients[i] = 0; for(SInt32 k = 0; k < 4; k++) { for(SInt32 j = 0; j < 4; j++) { double result = 0; for(SInt32 l = 0; l < 4; l++) for(SInt32 i = 0; i < 4; i++) result = result + xPowers[i]*cubicSplineMatrix[j+4*i]*yPowers[l]*cubicSplineMatrix[k+4*l]; coefficients[j+4*k] = result; } } SInt32 dataIndex = 0; for(SInt32 yIndex = yLowerIndex; yIndex < yLowerIndex+yCount; yIndex++) { for(SInt32 xIndex = xLowerIndex; xIndex < xLowerIndex+xCount; xIndex++) { // if the index is too close to the edge if((xIndex < 1) || (xIndex > getXSize()-3) || (yIndex < 1) || (yIndex > getYSize()-3)) { outMask[dataIndex] = false; dataIndex++; continue; } outMask[dataIndex] = true; for(SInt32 j = 0; j < 4; j++) { for(SInt32 i = 0; i < 4; i++) { outMask[dataIndex] = outMask[dataIndex] && getMask(xIndex + i - 1,yIndex + j - 1); } } if(!outMask[dataIndex]) { dataIndex++; continue; } double result = 0; for(SInt32 k = 0; k < 4; k++) for(SInt32 j = 0; j < 4; j++) result = result + coefficients[j+4*k]*(*this)(xIndex + j - 1, yIndex + k - 1); outValues[dataIndex] = result; dataIndex++; } } }
void MaskedImage::maskedInterpolate(const UArray<double> & xs, const UArray<double> & ys, UArray<double> & outValues, UArray<bool> & outMask) const { U_ASSERT((xs.getSize() == ys.getSize()) && (xs.getSize() == outValues.getSize()) && (xs.getSize() == outMask.getSize())); static double cubicSplineMatrix[4*4] = {0, 1, 0, 0, -0.5, 0, 0.5, 0, 1, -2.5, 2, -0.5, -0.5, 1.5, -1.5, 0.5}; double xPowers[4]; double yPowers[4]; xPowers[0] = 1.0; yPowers[0] = 1.0; for(SInt32 dataIndex = 0; dataIndex < xs.getSize(); dataIndex++) { double x = (xs[dataIndex]- getXLower())/getDeltaX(); double y = (ys[dataIndex]- getYLower())/getDeltaY(); SInt32 xIndex = (SInt32)x; SInt32 yIndex = (SInt32)y; // if the index is too close to the edge, extrapolate from the nearest location inbounds if((xIndex < 1) || (xIndex > getXSize()-3) || (yIndex < 1) || (yIndex > getYSize()-3)) { outMask[dataIndex] = false; continue; } outMask[dataIndex] = true; for(SInt32 j = 0; j < 4; j++) { for(SInt32 i = 0; i < 4; i++) { outMask[dataIndex] = outMask[dataIndex] && getMask(xIndex + i - 1,yIndex + j - 1); } } if(!outMask[dataIndex]) continue; x = x - (double)xIndex; y = y - (double)yIndex; xPowers[1] = x; xPowers[2] = x*x; xPowers[3] = x*x*x; yPowers[1] = y; yPowers[2] = y*y; yPowers[3] = y*y*y; double result = 0; for(SInt32 k = 0; k < 4; k++) { double localResult = 0; for(SInt32 j = 0; j < 4; j++) for(SInt32 i = 0; i < 4; i++) localResult = localResult + xPowers[i]*cubicSplineMatrix[j+4*i]*(*this)(xIndex + j - 1, yIndex + k - 1); for(SInt32 i = 0; i < 4; i++) result = result + yPowers[i]*cubicSplineMatrix[k+4*i]*localResult; } outValues[dataIndex] = result; } }
void Grid::paint(void) { int i; int j; int k; if (mpBox == NULL) return; printf("Grid::paint entry\n"); Vec3f minp = mpBox->getMin(); Vec3f maxp = mpBox->getMax(); float xBox = maxp.x() - minp.x(); float yBox = maxp.y() - minp.y(); float zBox = maxp.z() - minp.z(); int xSize = getXSize(); int ySize = getYSize(); int zSize = getZSize(); float xDelta = xBox / xSize; float yDelta = yBox / ySize; float zDelta = zBox / zSize; for (k = 0; k < zSize; k++) for (j = 0; j < ySize; j++) for (i = 0; i < xSize; i++) { printf("Obj:%d %d %d, num=%d\n", i, j, k, mObjects[offset(i, j, k)].getNumObjects()); if (mObjects[offset(i, j, k)].getNumObjects() > 0) { printf("Paint:%d, %d, %d\n", i, j, k); Vec3f curMinPoint(minp.x() + i*xDelta, minp.y() + j*yDelta, minp.z() + k*zDelta ); Vec3f curMaxPoint(curMinPoint.x() + xDelta, curMinPoint.y() + yDelta, curMinPoint.z() + zDelta ); glBegin(GL_QUADS); //The up plane Vec3f normalUp; Vec3f::Cross3( normalUp, Vec3f(0.0, curMaxPoint.y() - curMinPoint.y() , 0.0), Vec3f(curMaxPoint.x() - curMinPoint.x(), 0.0, 0.0) ); glNormal3f(normalUp.x(), normalUp.y(), normalUp.z()); glVertex3f(curMinPoint.x(), curMinPoint.y(), curMaxPoint.z()); glVertex3f(curMaxPoint.x(), curMinPoint.y(), curMaxPoint.z()); glVertex3f(curMaxPoint.x(), curMaxPoint.y(), curMaxPoint.z()); glVertex3f(curMinPoint.x(), curMaxPoint.y(), curMaxPoint.z()); //the down plane Vec3f normalDown; Vec3f::Cross3( normalDown, Vec3f(curMaxPoint.x() - curMinPoint.x(), 0.0, 0.0), Vec3f(0.0, curMaxPoint.y() - curMinPoint.y(), 0.0) ); glNormal3f(normalDown.x(), normalDown.y(), normalDown.z()); glVertex3f(curMinPoint.x(), curMinPoint.y(), curMinPoint.z()); glVertex3f(curMaxPoint.x(), curMinPoint.y(), curMinPoint.z()); glVertex3f(curMaxPoint.x(), curMaxPoint.y(), curMinPoint.z()); glVertex3f(curMinPoint.x(), curMaxPoint.y(), curMinPoint.z()); //the front plane Vec3f normalFront; Vec3f::Cross3( normalFront, Vec3f(0.0, curMaxPoint.y() - curMinPoint.y(), 0.0), Vec3f(0.0, 0.0, curMaxPoint.z() - curMinPoint.z()) ); glNormal3f(normalFront.x(), normalFront.y(), normalFront.z()); glVertex3f(curMinPoint.x(), curMinPoint.y(), curMinPoint.z()); glVertex3f(curMinPoint.x(), curMinPoint.y(), curMaxPoint.z()); glVertex3f(curMinPoint.x(), curMaxPoint.y(), curMaxPoint.z()); glVertex3f(curMinPoint.x(), curMaxPoint.y(), curMinPoint.z()); //the back plane Vec3f normalBack; Vec3f::Cross3( normalBack, Vec3f(0.0, 0.0, curMaxPoint.z() - curMinPoint.z()), Vec3f(0.0, curMaxPoint.y() - curMinPoint.y(), 0.0) ); glNormal3f(normalBack.x(), normalBack.y(), normalBack.z()); glVertex3f(curMaxPoint.x(), curMinPoint.y(), curMinPoint.z()); glVertex3f(curMaxPoint.x(), curMinPoint.y(), curMaxPoint.z()); glVertex3f(curMaxPoint.x(), curMaxPoint.y(), curMaxPoint.z()); glVertex3f(curMaxPoint.x(), curMaxPoint.y(), curMinPoint.z()); //the left plane Vec3f normalLeft; Vec3f::Cross3( normalLeft, Vec3f(curMinPoint.x() - curMaxPoint.x(), 0.0, 0.0), Vec3f(0.0, 0.0, curMaxPoint.z() - curMinPoint.z()) ); glNormal3f(normalLeft.x(), normalLeft.y(), normalLeft.z()); glVertex3f(curMaxPoint.x(), curMinPoint.y(), curMinPoint.z()); glVertex3f(curMaxPoint.x(), curMinPoint.y(), curMaxPoint.z()); glVertex3f(curMinPoint.x(), curMinPoint.y(), curMaxPoint.z()); glVertex3f(curMinPoint.x(), curMinPoint.y(), curMinPoint.z()); //the right plane Vec3f normalRight; Vec3f::Cross3( normalRight, Vec3f(0.0, 0.0, curMaxPoint.z() - curMinPoint.z()), Vec3f(curMinPoint.x() - curMaxPoint.x(), 0.0, 0.0) ); glNormal3f(normalRight.x(), normalRight.y(), normalRight.z()); glVertex3f(curMaxPoint.x(), curMaxPoint.y(), curMinPoint.z()); glVertex3f(curMaxPoint.x(), curMaxPoint.y(), curMaxPoint.z()); glVertex3f(curMinPoint.x(), curMaxPoint.y(), curMaxPoint.z()); glVertex3f(curMinPoint.x(), curMaxPoint.y(), curMinPoint.z()); glEnd(); } } printf("Grid::paint Exit\n"); }
BTSpecialOperation *BTEditor::editSpecialOperation(BTDisplay &d, BTSpecialOperation *special) { BTDisplay::selectItem cmds[BT_SPECIALCOMMANDS + BT_CONDITIONALCOMMANDS]; for (int i = 0; i < BT_CONDITIONALCOMMANDS; ++i) { cmds[i].name = std::string("if ") + conditionalCommands[i]; cmds[i].value = i; } for (int i = 0; i < BT_SPECIALCOMMANDS; ++i) { cmds[i + BT_CONDITIONALCOMMANDS].name = specialCommands[i]; cmds[i + BT_CONDITIONALCOMMANDS].value = i + BT_CONDITIONALCOMMANDS; } std::sort(cmds, cmds + BT_SPECIALCOMMANDS + BT_CONDITIONALCOMMANDS); int start(0); int current(0); { BTSpecialConditional *specialCond = dynamic_cast<BTSpecialConditional*>(special); if (NULL != specialCond) { for (int i = 0; i < BT_SPECIALCOMMANDS + BT_CONDITIONALCOMMANDS; i++) { if (cmds[i].value == specialCond->getType()) { current = i; break; } } } BTSpecialCommand *specialCom = dynamic_cast<BTSpecialCommand*>(special); if (NULL != specialCom) { for (int i = 0; i < BT_SPECIALCOMMANDS + BT_CONDITIONALCOMMANDS; i++) { if (cmds[i].value == specialCom->getType() + BT_CONDITIONALCOMMANDS) { current = i; break; } } } } int original = current; d.addSelection(cmds, BT_SPECIALCOMMANDS + BT_CONDITIONALCOMMANDS, start, current); int key = d.process(); d.clearText(); if (key == 27) return NULL; std::string text; int number[3] = {0, 0, 0}; int count = 0; const char *cmd = NULL; if (cmds[current].value < BT_CONDITIONALCOMMANDS) { cmd = conditionalCommands[cmds[current].value]; if (original == current) { BTSpecialConditional *specialCond = dynamic_cast<BTSpecialConditional*>(special); if (NULL != specialCond) { for (int i = 0; i < specialCond->getArgumentCount(); i++) { number[i] = specialCond->getNumber(i); } text = specialCond->getText(); } } } else { cmd = specialCommands[cmds[current].value - BT_CONDITIONALCOMMANDS]; if (original == current) { BTSpecialCommand *specialCom = dynamic_cast<BTSpecialCommand*>(special); if (NULL != specialCom) { for (int i = 0; i < 3; i++) { number[i] = specialCom->getNumber(i); } text = specialCom->getText(); } } } const char *dollarSign; std::string newMap; int facingDir = -1; while (dollarSign = strchr(cmd, '$')) { switch (dollarSign[1]) { case 'S': { int len = levelMap->getNumOfSpecials(); BTDisplay::selectItem list[len]; for (int i = 0; i < len; ++i) { list[i].name = levelMap->getSpecial(i)->getName(); } int specialStart(0); d.addSelection(list, len, specialStart, number[count]); int key = d.process(); d.clearText(); if (key == 27) return NULL; count++; break; } case 'I': { BTFactory<BTItem> &itemList = getItemList(); BTDisplay::selectItem items[itemList.size()]; for (size_t i = 0; i < itemList.size(); ++i) items[i].name = itemList[i].getName(); int itemStart(0); d.addSelection(items, itemList.size(), itemStart, number[count]); int key = d.process(); d.clearText(); if (key == 27) return NULL; count++; break; } case 'A': case 'M': { BTFactory<BTMonster> &monsterList = getMonsterList(); BTDisplay::selectItem monsters[monsterList.size()]; for (size_t i = 0; i < monsterList.size(); ++i) monsters[i].name = monsterList[i].getName(); int monsterStart(0); d.addSelection(monsters, monsterList.size(), monsterStart, number[count]); int key = d.process(); d.clearText(); if (key == 27) return NULL; count++; break; } case 'X': { BTFactory<BTSpell, BTSpell1> &spellList = getSpellList(); BTDisplay::selectItem spells[spellList.size()]; for (size_t i = 0; i < spellList.size(); ++i) spells[i].name = spellList[i].getName(); int spellStart(0); d.addSelection(spells, spellList.size(), spellStart, number[count]); int key = d.process(); d.clearText(); if (key == 27) return NULL; count++; break; } case 'L': { if ((newMap.empty()) || (0 != PHYSFS_exists(newMap.c_str()))) { int origX = xPos; int origY = yPos; int origFacing = facing; bool toggle = false; if ((!newMap.empty()) && (newMap != levelMap->getFilename())) { toggleMap(); loadMap(newMap.c_str()); toggle = true; } BTDisplayConfig *oldConfig = d.getConfig(); BTDisplayConfig config; XMLSerializer parser; config.serialize(&parser); parser.parse(BTDisplay::applyDisplayDir("data/mapedit.xml").c_str(), true); d.setConfig(&config); xPos = number[count] % levelMap->getXSize(); yPos = levelMap->getYSize() - 1 - (number[count + 1] % levelMap->getYSize()); facing = number[count + 2]; p3dConfig = d.setWallGraphics(levelMap->getType()); unsigned char key = ' '; while (key != '\r') { if (levelMap->getSquare(yPos, xPos).getSpecial() > -1) d.drawLabel("main", levelMap->getSpecial(levelMap->getSquare(yPos, xPos).getSpecial())->getName()); else d.drawLabel("main", ""); d.drawView(); key = d.readChar(); switch (key) { case BTKEY_UP: if (yPos > 0) yPos--; else yPos = getYSize() - 1; break; case BTKEY_LEFT: if (xPos > 0) xPos--; else xPos = getXSize() - 1; break; case BTKEY_DOWN: if (yPos < getYSize() - 1) yPos++; else yPos = 0; break; case BTKEY_RIGHT: if (xPos < getXSize() - 1) xPos++; else xPos = 0; break; case BTKEY_PGDN: if (facing < 3) facing++; else facing = 0; break; case BTKEY_END: if (facing > 0) facing--; else facing = 3; break; default: break; } } number[count++] = xPos; number[count++] = levelMap->getYSize() - 1 - yPos; facingDir = facing; d.clearText(); d.setConfig(oldConfig); if (toggle) { toggleMap(); p3dConfig = d.setWallGraphics(levelMap->getType()); } xPos = origX; yPos = origY; facing = origFacing; break; } // Fall through to $O processing if map file does not exist. } case 'O': { std::string val; if (number[count] != 0) { char convert[30]; sprintf(convert, "%d", number[count]); val = convert; } d.addReadString("X>", 100, val); key = d.process(); d.clearText(); if (27 == key) return NULL; number[count++] = atol(val.c_str()); val = ""; if (number[count] != 0) { char convert[30]; sprintf(convert, "%d", number[count]); val = convert; } d.addReadString("Y>", 100, val); key = d.process(); d.clearText(); if (27 == key) return NULL; number[count++] = atol(val.c_str()); break; } case 'T': { BTDisplay::selectItem damage[BT_MONSTEREXTRADAMAGE]; for (int i = 0; i < BT_MONSTEREXTRADAMAGE; ++i) damage[i].name = extraDamage[i]; int damageStart(0); d.addSelection(damage, BT_MONSTEREXTRADAMAGE, damageStart, number[count]); int key = d.process(); d.clearText(); if (key == 27) return NULL; count++; break; } case 'C': { BTJobList &jobList = getJobList(); BTDisplay::selectItem jobs[jobList.size()]; for (size_t i = 0; i < jobList.size(); ++i) jobs[i].name = jobList[i]->name; int jobStart(0); d.addSelection(jobs, jobList.size(), jobStart, number[count]); int key = d.process(); d.clearText(); if (key == 27) return NULL; count++; break; } case 'R': { BTRaceList &raceList = getRaceList(); BTDisplay::selectItem races[raceList.size()]; for (size_t i = 0; i < raceList.size(); ++i) races[i].name = raceList[i]->name; int raceStart(0); d.addSelection(races, raceList.size(), raceStart, number[count]); int key = d.process(); d.clearText(); if (key == 27) return NULL; count++; break; } case 'D': { if (facingDir != -1) { number[count++] = facingDir; } else { BTDisplay::selectItem dir[BT_DIRECTIONS]; for (int i = 0; i < BT_DIRECTIONS; ++i) dir[i].name = directions[i]; int dirStart(0); d.addSelection(dir, BT_DIRECTIONS, dirStart, number[count]); int key = d.process(); d.clearText(); if (key == 27) return NULL; count++; } break; } case '#': case 'G': case 'F': case '!': case 'J': { std::string val; if (number[count] != 0) { char convert[30]; sprintf(convert, "%d", number[count]); val = convert; } d.addReadString("Number>", 100, val); key = d.process(); d.clearText(); if (27 == key) return NULL; number[count++] = atol(val.c_str()); break; } case 'P': { BTDisplayConfig *oldConfig = d.getConfig(); BTDisplayConfig config; XMLSerializer parser; config.serialize(&parser); parser.parse(BTDisplay::applyDisplayDir("data/pictureselect.xml").c_str(), true); d.setConfig(&config); d.clearText(); d.addText("Select Image"); int val(number[count]); d.addSelectImage(val); key = d.process(); d.clearText(); d.clearImage(); d.setConfig(oldConfig); if (27 == key) return NULL; number[count++] = val; break; } case 'E': { BTDisplay::selectItem effects[BT_SPELLTYPES_FULL]; for (int i = 0; i < BT_SPELLTYPES_FULL; ++i) effects[i].name = spellTypes[i]; int effectStart(0); d.addSelection(effects, BT_SPELLTYPES_FULL, effectStart, number[count]); int key = d.process(); d.clearText(); if (key == 27) return NULL; count++; break; } case 'N': { char **files = PHYSFS_enumerateFiles(""); char **i; int count(1); for (i = files; *i != NULL; i++) { if (checkSkipFiles(*i)) continue; int len = strlen(*i); if ((len > 4) && (strcmp(".MAP", (*i) + (len - 4)) == 0)) { char tmp[len + 1]; strcpy(tmp, (*i)); strcpy(tmp + len - 3, "xml"); if (0 == PHYSFS_exists(tmp)) { count++; } } else if ((len > 4) && (strcmp(".xml", (*i) + (len - 4)) == 0)) { count++; } } BTDisplay::selectItem *list = new BTDisplay::selectItem[count]; int current = 0; for (i = files; *i != NULL; i++) { if (checkSkipFiles(*i)) continue; int len = strlen(*i); if ((len > 4) && (strcmp(".MAP", (*i) + (len - 4)) == 0)) { char tmp[len + 1]; strcpy(tmp, (*i)); strcpy(tmp + len - 3, "xml"); if (0 == PHYSFS_exists(tmp)) { list[current].name = *i; current++; } } else if ((len > 4) && (strcmp(".xml", (*i) + (len - 4)) == 0)) { list[current].name = *i; current++; } } list[current].name = "<New Map>"; PHYSFS_freeList(files); int start(0); int select(0); d.clearElements(); d.addSelection(list, count, start, select); unsigned int key = d.process(); d.clearText(); if (27 == key) return NULL; else if (count - 1 != select) text = list[select].name; else { d.addReadString(">", 100, text); key = d.process(); d.clearText(); if (27 == key) return NULL; } newMap = text; break; } case 'K': { BTSkillList &skillList = getSkillList(); BTDisplay::selectItem items[skillList.size()]; for (size_t i = 0; i < skillList.size(); ++i) items[i].name = skillList[i]->name; int itemStart(0); d.addSelection(items, skillList.size(), itemStart, number[count]); int key = d.process(); d.clearText(); if (key == 27) return NULL; count++; break; } case '$': default: d.addReadString(">", 100, text); key = d.process(); d.clearText(); if (27 == key) return NULL; break; } cmd = dollarSign + 2; } if (cmds[current].value < BT_CONDITIONALCOMMANDS) { BTSpecialConditional *opNew = new BTSpecialConditional(cmds[current].value, text.c_str()); for (int i = 0; i < count; ++i) opNew->addNumber(number[i]); BTSpecialConditional *opOld = dynamic_cast<BTSpecialConditional*>(special); if (opOld) { opNew->getThenClause()->moveFrom(opOld->getThenClause()); opNew->getElseClause()->moveFrom(opOld->getElseClause()); } return opNew; } else { BTSpecialCommand *opNew = new BTSpecialCommand(cmds[current].value - BT_CONDITIONALCOMMANDS); opNew->setText(text); for (int i = 0; i < count; ++i) opNew->setNumber(i, number[i]); return opNew; } }
void BTEditor::editMap(BTDisplay &d, const char *filename) { BTDisplayConfig *oldConfig = d.getConfig(); BTDisplayConfig config; XMLSerializer parser; config.serialize(&parser); parser.parse(BTDisplay::applyDisplayDir("data/mapedit.xml").c_str(), true); d.setConfig(&config); loadMap(filename); xPos = 0; yPos = 0; facing = 0; p3dConfig = d.setWallGraphics(levelMap->getType()); unsigned char key = ' '; if (currentWall < p3dConfig->mapType.size()) d.drawLabel("wall", p3dConfig->mapType[currentWall]->name.c_str()); else d.drawLabel("wall", "Clear"); while (key != 'q') { if (levelMap->getSquare(yPos, xPos).getSpecial() > -1) d.drawLabel("main", levelMap->getSpecial(levelMap->getSquare(yPos, xPos).getSpecial())->getName()); else d.drawLabel("main", ""); if (levelMap->getSquare(yPos, xPos).getStreet() > -1) d.drawLabel("street", levelMap->getStreetName(levelMap->getSquare(yPos, xPos).getStreet()).c_str()); else d.drawLabel("street", ""); d.drawView(); key = d.readChar(); switch (key) { case BTKEY_UP: if (yPos > 0) yPos--; else yPos = getYSize() - 1; break; case BTKEY_LEFT: if (xPos > 0) xPos--; else xPos = getXSize() - 1; break; case BTKEY_DOWN: if (yPos < getYSize() - 1) yPos++; else yPos = 0; break; case BTKEY_RIGHT: if (xPos < getXSize() - 1) xPos++; else xPos = 0; break; case BTKEY_PGDN: case '2': if (facing < 3) facing++; else facing = 0; break; case BTKEY_END: case '1': if (facing > 0) facing--; else facing = 3; break; case 13: { int wall = 0; if (currentWall < p3dConfig->mapType.size()) wall = p3dConfig->mapType[currentWall]->type; levelMap->getSquare(yPos, xPos).setWall(facing, wall); int xOpposite = xPos + Psuedo3D::changeXY[facing][0] + levelMap->getXSize(); xOpposite = xOpposite % levelMap->getXSize(); int yOpposite = yPos + Psuedo3D::changeXY[facing][1] + levelMap->getYSize(); yOpposite = yOpposite % levelMap->getYSize(); levelMap->getSquare(yOpposite, xOpposite).setWall((facing + 2) % 4, wall); break; } case ' ': if (currentWall < p3dConfig->mapType.size()) { currentWall++; } else { currentWall = 0; } if (currentWall < p3dConfig->mapType.size()) d.drawLabel("wall", p3dConfig->mapType[currentWall]->name.c_str()); else d.drawLabel("wall", "Clear"); break; case 'r': { if (d.getScreen(1)) d.getScreen(1)->setVisibility(true); std::string tmp = d.readString("X Size?", 3, ""); int newXSize = atol(tmp.c_str()); if (newXSize < 1) { d.clearText(); if (d.getScreen(1)) d.getScreen(1)->setVisibility(false); break; } tmp = d.readString("Y Size?", 3, ""); int newYSize = atol(tmp.c_str()); if (newYSize < 1) { d.clearText(); if (d.getScreen(1)) d.getScreen(1)->setVisibility(false); break; } levelMap->resize(newXSize, newYSize); d.clearText(); if (d.getScreen(1)) d.getScreen(1)->setVisibility(false); break; } case 'c': levelMap->getSquare(yPos, xPos).setSpecial(-1); break; case 's': { d.clearText(); if (d.getScreen(1)) d.getScreen(1)->setVisibility(true); int len = levelMap->getNumOfSpecials(); BTDisplay::selectItem list[len + 1]; for (int i = 0; i < len; ++i) { list[i].name = levelMap->getSpecial(i)->getName(); } list[len].name = "<New Special>"; d.addSelection(list, len + 1, startSpecial, currentSpecial); int key = d.process("ce"); d.clearText(); if (d.getScreen(1)) d.getScreen(1)->setVisibility(false); if ((key == 'e') || ((currentSpecial == len) && ((key == '\r') || (key == 'c')))) { editSpecial(d, levelMap->getSpecial(currentSpecial)); levelMap->getSquare(yPos, xPos).setSpecial(currentSpecial); } else if (key == 'c') { BTSpecial *s = new BTSpecial(*levelMap->getSpecial(currentSpecial)); levelMap->addSpecial(s); currentSpecial = len; editSpecial(d, s); levelMap->getSquare(yPos, xPos).setSpecial(currentSpecial); } else if (key == '\r') { levelMap->getSquare(yPos, xPos).setSpecial(currentSpecial); } if (currentWall < p3dConfig->mapType.size()) d.drawLabel("wall", p3dConfig->mapType[currentWall]->name.c_str()); else d.drawLabel("wall", "Clear"); break; } case 'l': levelMap->getSquare(yPos, xPos).setSpecial(currentSpecial); break; case 'b': levelMap->getSquare(yPos, xPos).setStreet(-1); break; case 'n': { d.clearText(); if (d.getScreen(1)) d.getScreen(1)->setVisibility(true); int len = levelMap->getNumOfStreets(); BTDisplay::selectItem list[len + 1]; for (int i = 0; i < len; ++i) { list[i].name = levelMap->getStreetName(i); } list[len].name = "<New Street>"; d.addSelection(list, len + 1, startStreet, currentStreet); int key = d.process("e"); d.clearText(); if ((key == 'e') || ((currentStreet == len) && (key == '\r'))) { std::string name; if (currentStreet != len) name = list[currentStreet].name; d.addReadString("Name: ", 25, name); key = d.process(); if (key == '\r') { if (currentStreet == len) levelMap->addStreetName(name); else levelMap->setStreetName(currentStreet, name); levelMap->getSquare(yPos, xPos).setStreet(currentStreet); } } else if (key == '\r') { levelMap->getSquare(yPos, xPos).setStreet(currentStreet); } d.clearText(); if (d.getScreen(1)) d.getScreen(1)->setVisibility(false); if (currentWall < p3dConfig->mapType.size()) d.drawLabel("wall", p3dConfig->mapType[currentWall]->name.c_str()); else d.drawLabel("wall", "Clear"); break; } case 'm': levelMap->getSquare(yPos, xPos).setStreet(currentStreet); break; case 'p': { ObjectSerializer serial; levelMap->serialize(&serial); BTMapPropertiesEditor mapPropEditor; int type = levelMap->getType(); mapPropEditor.edit(d, serial); if (levelMap->getType() != type) { p3dConfig = d.setWallGraphics(levelMap->getType()); currentWall = 0; } if (currentWall < p3dConfig->mapType.size()) d.drawLabel("wall", p3dConfig->mapType[currentWall]->name.c_str()); else d.drawLabel("wall", "Clear"); break; } default: break; } } if (d.getScreen(1)) d.getScreen(1)->setVisibility(true); d.drawText("Save?"); while ((key != 'y') && (key != 'n')) { key = d.readChar(); } if (key == 'y') { bool wrote = false; int len = strlen(levelMap->getFilename()); if ((len > 4) && (strcmp(".MAP", levelMap->getFilename() + (len - 4)) == 0)) { try { BinaryWriteFile f(levelMap->getFilename()); levelMap->write(f); wrote = true; } catch (const FileException &e) { PHYSFS_delete(levelMap->getFilename()); printf("Failed to write old map file: %s\n", e.what()); char tmp[len + 1]; strcpy(tmp, levelMap->getFilename()); strcpy(tmp + len - 3, "xml"); levelMap->setFilename(tmp); } } if (!wrote) { XMLSerializer parser; parser.add("map", levelMap); parser.write(levelMap->getFilename(), true); } } d.setConfig(oldConfig); }
/* * newPeaks() * * Create initial fit data structures for spline fitting from the peak array. The * format of the initial values is the same as what was used for 3D-DAOSTORM. * * peaks - pointer to the initial values for the parameters for each peak. * 1. height * 2. x-center * 3. x-sigma * 4. y-center * 5. y-sigma * 6. background * 7. z-center * 8. status * 9. error * .. repeat .. * n_peaks - The number of parameters (peaks). */ void newPeaks(double *peaks, int n_peaks, int fit_type) { int i,j,n_fit_params,sx,sy,sz; double temp; if(fit_type == F2D){ n_fit_params = 4; } else{ n_fit_params = 5; } /* Delete old fit data structures, if they exist. */ freePeaks(); /* Reset the fit array. */ resetFit(); /* Allocate storage for fit data structure. */ n_fit_data = n_peaks; new_fit_data = (fitData *)malloc(sizeof(fitData)*n_peaks); old_fit_data = (fitData *)malloc(sizeof(fitData)*n_peaks); sx = getXSize()/2 - 1; sy = getYSize()/2 - 1; sz = getZSize(); /* Note the assumption here that the splines are square. */ if((sx%2)==1){ xoff = (double)(sx/2) - 1.0; yoff = (double)(sy/2) - 1.0; } else{ xoff = (double)(sx/2) - 1.5; yoff = (double)(sy/2) - 1.5; } //zoff = -(double)(sz/2); zoff = 0.0; /* Initialize fit data structure. */ for(i=0;i<n_fit_data;i++){ new_fit_data[i].index = i; new_fit_data[i].n_params = n_fit_params; new_fit_data[i].size_x = sx; new_fit_data[i].size_y = sy; new_fit_data[i].size_z = sz; new_fit_data[i].status = (int)(peaks[i*NRESULTSPAR+STATUS]); new_fit_data[i].type = fit_type; new_fit_data[i].lambda = 1.0; mallocFitData(&(new_fit_data[i]), n_fit_params, sx*sy*(n_fit_params+1)); for(j=0;j<n_fit_params;j++){ new_fit_data[i].sign[j] = 0; } new_fit_data[i].clamp[CF_HEIGHT] = 100.0; new_fit_data[i].clamp[CF_XCENTER] = 1.0; new_fit_data[i].clamp[CF_YCENTER] = 1.0; new_fit_data[i].clamp[CF_BACKGROUND] = 20.0; if (fit_type == F3D){ new_fit_data[i].clamp[CF_ZCENTER] = 2.0; } if (DEBUG){ printf(" peaks: %.2f %.2f %.2f\n", peaks[i*NRESULTSPAR+XCENTER], peaks[i*NRESULTSPAR+YCENTER], peaks[i*NRESULTSPAR+ZCENTER]); } new_fit_data[i].params[CF_HEIGHT] = peaks[i*NRESULTSPAR+HEIGHT]; new_fit_data[i].params[CF_XCENTER] = peaks[i*NRESULTSPAR+XCENTER] - xoff; new_fit_data[i].params[CF_YCENTER] = peaks[i*NRESULTSPAR+YCENTER] - yoff; new_fit_data[i].params[CF_BACKGROUND] = peaks[i*NRESULTSPAR+BACKGROUND]; if (fit_type == F3D){ new_fit_data[i].params[CF_ZCENTER] = peaks[i*NRESULTSPAR+ZCENTER] - zoff; } new_fit_data[i].xi = (int)(new_fit_data[i].params[CF_XCENTER]); new_fit_data[i].yi = (int)(new_fit_data[i].params[CF_YCENTER]); if (fit_type == F3D){ new_fit_data[i].zi = (int)(new_fit_data[i].params[CF_ZCENTER]); } if (fit_type == F2D){ updateFitValues2D(&(new_fit_data[i])); } else{ updateFitValues3D(&(new_fit_data[i])); } /* * The derivative with respect to background term is always 1.0 */ for(j=0;j<(sx*sy);j++){ new_fit_data[i].values[(CF_BACKGROUND+1)*sx*sy+j] = 1.0; } addPeak(&(new_fit_data[i])); mallocFitData(&(old_fit_data[i]), n_fit_params, sx*sy*(n_fit_params+1)); copyFitData(&(new_fit_data[i]), &(old_fit_data[i])); if(new_fit_data[i].status == RUNNING){ if (TESTING){ temp = calcError(&(new_fit_data[i]), new_fit_data[i].params[CF_BACKGROUND]); new_fit_data[i].error = temp; } else{ new_fit_data[i].error = 1.0e+12; } old_fit_data[i].error = 1.0e+13; } else{ new_fit_data[i].error = peaks[i*NRESULTSPAR+IERROR]; old_fit_data[i].error = new_fit_data[i].error; } } }