// Print the impedances of nodes to a text file void PrintImpedances(void) { FILE *OutputFile; double Impedance; char *FilenameBuffer; FilenameBuffer = (char*)malloc(100*sizeof(char)); sprintf_s(FilenameBuffer, 100*sizeof(char), "%s/%s_%s", FolderName, ProjectName, OutputFilename); if (fopen_s(&OutputFile, FilenameBuffer, "w") != 0) { printf("Could not open file '%s'\n", OutputFilename); } else { printf("Printing Impedances to output file '%s'\n", OutputFilename); PrintFileHeader(OutputFile); for (int y = ySize-1; y>=0; y--) { for (int x = 0; x < xSize; x++) { Impedance = Grid[x][y][zSize/2].Z; if (x == ImpulseSource.X && y == ImpulseSource.Y) { fputc('i',OutputFile); } else if (x==PlaceWithinGridX(RoundToNearest(PathLossParameters.X1/GridSpacing)) && y == PlaceWithinGridY(RoundToNearest(PathLossParameters.Y1/GridSpacing))) { fputc('1',OutputFile); } else if (PathLossParameters.Type != POINT && x==PlaceWithinGridX(RoundToNearest(PathLossParameters.X2/GridSpacing)) && y == PlaceWithinGridY(RoundToNearest(PathLossParameters.Y2/GridSpacing))) { fputc('2',OutputFile); } // E = 1 else if (Impedance == IMPEDANCE_OF_FREE_SPACE) { fputc('.',OutputFile); } // E <= 2 else if (Impedance >= IMPEDANCE_OF_FREE_SPACE/sqrt(2.0)) { fputc('W', OutputFile); } // E <= 5 else if (Impedance >= IMPEDANCE_OF_FREE_SPACE/sqrt(5.0)) { fputc('#', OutputFile); } // E >= 5 else { fputc(' ', OutputFile); } } fprintf(OutputFile, "\n"); } if (fclose(OutputFile)) { printf("Output file close unsuccessful\n"); } } free(FilenameBuffer); }
/** * <p>Computes the dimension (number of modules on a size) of the QR Code based on the position * of the finder patterns and estimated module size.</p> * Return -1 in case of error. */ static int ComputeDimension(const ResultPoint& topLeft, const ResultPoint& topRight, const ResultPoint& bottomLeft, float moduleSize) { int tltrCentersDimension = RoundToNearest(ResultPoint::Distance(topLeft, topRight) / moduleSize); int tlblCentersDimension = RoundToNearest(ResultPoint::Distance(topLeft, bottomLeft) / moduleSize); int dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7; switch (dimension & 0x03) { // mod 4 case 0: return ++dimension; case 1: return dimension; case 2: return --dimension; } return -1; // to signal error; }
static bool GetBlackPointOnSegment(const BitMatrix& image, int aX, int aY, int bX, int bY, ResultPoint& result) { int dist = RoundToNearest(ResultPoint::Distance(aX, aY, bX, bY)); float xStep = static_cast<float>(bX - aX) / dist; float yStep = static_cast<float>(bY - aY) / dist; for (int i = 0; i < dist; i++) { int x = RoundToNearest(aX + i * xStep); int y = RoundToNearest(aY + i * yStep); if (image.get(x, y)) { result.set(static_cast<float>(x), static_cast<float>(y)); return true; } } return false; }
// Print information required to estimate the path loss error constant kappa to a text file void PrintKappaData(void) { FILE *OutputFile; char *FilenameBuffer; FilenameBuffer = (char*)malloc(100*sizeof(char)); sprintf_s(FilenameBuffer, 100*sizeof(char), "%s/%s_%s", FolderName, ProjectName, "KappaCalcs.txt"); int x, y, z; double d; if (fopen_s(&OutputFile, FilenameBuffer, "w") != 0) { printf("Could not open file '%s'\n", "KappaCalcs.txt"); } else { printf("Printing time variation to output file %s\n", "KappaCalcs.txt"); PrintFileHeader(OutputFile); fprintf(OutputFile,"V\tDistance\tTheta\tPhi\n"); for (int xx = 0; xx < 10; xx++) { for (int yy = 0; yy < 10; yy++) { for (int zz = 0; zz < 10; zz++) { x = RoundToNearest(ImpulseSource.X + 9*xx*GridSpacing); y = RoundToNearest(ImpulseSource.Y + 9*yy*GridSpacing); z = RoundToNearest(ImpulseSource.Z + 9*zz*GridSpacing); d = 9*sqrt((double)(SQUARE(xx)+SQUARE(yy)+SQUARE(zz))); fprintf(OutputFile,"%f\t%f\t%f\t%f\n", Grid[x][y][z].Vmax, d, asin((9.0*zz)/d), atan((double)(yy)/xx)); } } } if (fclose(OutputFile)) { printf("Output file close unsuccessful\n"); } } free(FilenameBuffer); }
void CalculateInitialBoundaries(void) { for (int i=0; i<MaxThreadIndex.X; i++) { for (int j=0; j<MaxThreadIndex.Y; j++) { for (int k=0; k<MaxThreadIndex.Z; k++) { ThreadData[i][j][k].xMin = RoundToNearest(i*xSize/MaxThreadIndex.X); ThreadData[i][j][k].xMax = RoundToNearest((i+1)*xSize/MaxThreadIndex.X-1); ThreadData[i][j][k].yMin = RoundToNearest(j*ySize/MaxThreadIndex.Y); ThreadData[i][j][k].yMax = RoundToNearest((j+1)*ySize/MaxThreadIndex.Y-1); ThreadData[i][j][k].zMin = RoundToNearest(k*zSize/MaxThreadIndex.Z); ThreadData[i][j][k].zMax = RoundToNearest((k+1)*zSize/MaxThreadIndex.Z-1); if (ImpulseSource.X >= ThreadData[i][j][k].xMin && ImpulseSource.X <= ThreadData[i][j][k].xMax && ImpulseSource.Y >= ThreadData[i][j][k].yMin && ImpulseSource.Y <= ThreadData[i][j][k].yMax && ImpulseSource.Z >= ThreadData[i][j][k].zMin && ImpulseSource.Z <= ThreadData[i][j][k].zMax) { ActiveJunctions[i][j][k] = 1; ActiveSet[i][j][k] = AddJunctionToSet(ImpulseSource.X, ImpulseSource.Y, ImpulseSource.Z, true); } } } } }
// Print the estimated path loss to a text file. Either print a point, route (line) or grid of estimates void PrintPathLossToFile(void) { FILE *PathLossFile; char *FilenameBuffer; FilenameBuffer = (char*)malloc(100*sizeof(char)); sprintf_s(FilenameBuffer, 100*sizeof(char), "%s/%s_%s", FolderName, ProjectName, PathLossFilename); if (fopen_s(&PathLossFile, FilenameBuffer, "w") != 0) { printf("Could not open file '%s'\n", PathLossFilename); } else { printf("Printing path loss values to '%s'\n",PathLossFilename); PrintFileHeader(PathLossFile); int x, y, z; double PathLoss; // Z coordinate is constant z = PlaceWithinGridZ(RoundToNearest(PathLossParameters.Z1/GridSpacing)); switch (PathLossParameters.Type) { // Print the path loss at a single point case POINT: { // Details of the path loss estimates required and column titles fprintf(PathLossFile, "Point Analysis\nHeight = %f\n\nX\t\tY\t\tPL(dB)\n", z*GridSpacing); x = PlaceWithinGridX(RoundToNearest(PathLossParameters.X1/GridSpacing)); y = PlaceWithinGridY(RoundToNearest(PathLossParameters.Y1/GridSpacing)); PathLoss = VoltageToDB(SPEED_OF_LIGHT/Frequency * Grid[x][y][z].Vmax * KAPPA/4/M_PI/GridSpacing); fprintf(PathLossFile, "%f\t%f\t%f\n", x*GridSpacing, y*GridSpacing, PathLoss); break; } // Print the path loss along a route case ROUTE: { double length = sqrt(SQUARE(PathLossParameters.X2 - PathLossParameters.X1)+SQUARE(PathLossParameters.Y2 - PathLossParameters.Y1)); double nSamples = length/PathLossParameters.Spacing; double dx = (PathLossParameters.X2 - PathLossParameters.X1)/nSamples; double dy = (PathLossParameters.Y2 - PathLossParameters.Y1)/nSamples; // Details of the path loss estimates required and column titles fprintf(PathLossFile, "Route Analysis - %d samples\nHeight = %f\n\nX\t\tY\t\tPL(dB)\n", (int)nSamples == nSamples ? (int)nSamples+1 : (int)nSamples+2, z*GridSpacing); for (int i = 0; i < nSamples; i++) { x = PlaceWithinGridX(RoundToNearest((PathLossParameters.X1+dx*i)/GridSpacing)); y = PlaceWithinGridY(RoundToNearest((PathLossParameters.Y1+dy*i)/GridSpacing)); PathLoss = VoltageToDB(SPEED_OF_LIGHT/Frequency * Grid[x][y][z].Vmax * KAPPA/4/M_PI/GridSpacing); fprintf(PathLossFile, "%f\t%f\t%f\n", x*GridSpacing, y*GridSpacing, PathLoss); } x = PlaceWithinGridX(RoundToNearest(PathLossParameters.X2/GridSpacing)); y = PlaceWithinGridY(RoundToNearest(PathLossParameters.Y2/GridSpacing)); PathLoss = VoltageToDB(SPEED_OF_LIGHT/Frequency * Grid[x][y][z].Vmax * KAPPA/4/M_PI/GridSpacing); fprintf(PathLossFile, "%f\t%f\t%f\n", x*GridSpacing, y*GridSpacing, PathLoss); break; } // Print the path loss in a 2d grid case GRID: { double nSamplesX = (PathLossParameters.X2-PathLossParameters.X1)/PathLossParameters.Spacing; double nSamplesY = (PathLossParameters.Y2-PathLossParameters.Y1)/PathLossParameters.SpacingY; double dx = (PathLossParameters.X2 - PathLossParameters.X1)/nSamplesX; double dy = (PathLossParameters.Y2 - PathLossParameters.Y1)/nSamplesY; // Details of the path loss estimates required and column titles fprintf(PathLossFile, "Grid Analysis - %d x %d samples\nHeight = %f\n\nX\t\tY\t\tPL(dB)\n", (int)nSamplesX == nSamplesX ? (int)nSamplesX+1 : (int)nSamplesX+2, (int)nSamplesY == nSamplesY ? (int)nSamplesY+1 : (int)nSamplesY+2, z*GridSpacing); for (int j=0; j < nSamplesY; j++) { for (int i=0; i < nSamplesX; i++) { x = PlaceWithinGridX(RoundToNearest((PathLossParameters.X1+dx*i)/GridSpacing)); y = PlaceWithinGridY(RoundToNearest((PathLossParameters.Y1+dy*j)/GridSpacing)); PathLoss = VoltageToDB(SPEED_OF_LIGHT/Frequency * Grid[x][y][z].Vmax * KAPPA/4/M_PI/GridSpacing); fprintf(PathLossFile, "%f\t%f\t%f\n", x*GridSpacing, y*GridSpacing, PathLoss); } x = PlaceWithinGridX(RoundToNearest(PathLossParameters.X2/GridSpacing)); y = PlaceWithinGridY(RoundToNearest((PathLossParameters.Y1+dy*j)/GridSpacing)); PathLoss = VoltageToDB(SPEED_OF_LIGHT/Frequency * Grid[x][y][z].Vmax * KAPPA/4/M_PI/GridSpacing); fprintf(PathLossFile, "%f\t%f\t%f\n", x*GridSpacing, y*GridSpacing, PathLoss); } // Print a path loss estimate of the outer X row nearest X2,Y2 on the grid (may not be a whole sample space apart) for (int i=0; i < nSamplesX; i++) { x = PlaceWithinGridX(RoundToNearest((PathLossParameters.X1+dx*i)/GridSpacing)); y = PlaceWithinGridY(RoundToNearest(PathLossParameters.Y2/GridSpacing)); PathLoss = VoltageToDB(SPEED_OF_LIGHT/Frequency * Grid[x][y][z].Vmax * KAPPA/4/M_PI/GridSpacing); fprintf(PathLossFile, "%f\t%f\t%f\n", x*GridSpacing, y*GridSpacing, PathLoss); } x = PlaceWithinGridX(RoundToNearest(PathLossParameters.X2/GridSpacing)); y = PlaceWithinGridY(RoundToNearest(PathLossParameters.Y2/GridSpacing)); PathLoss = VoltageToDB(SPEED_OF_LIGHT/Frequency * Grid[x][y][z].Vmax * KAPPA/4/M_PI/GridSpacing); fprintf(PathLossFile, "%f\t%f\t%f\n", x*GridSpacing, y*GridSpacing, PathLoss); break; } // Print the path loss in a set of 2d grids case CUBE: { double nSamplesX = (PathLossParameters.X2-PathLossParameters.X1)/PathLossParameters.Spacing; double nSamplesY = (PathLossParameters.Y2-PathLossParameters.Y1)/PathLossParameters.SpacingY; double nSamplesZ = (PathLossParameters.Z2-PathLossParameters.Z1)/PathLossParameters.SpacingZ; double dx = (PathLossParameters.X2 - PathLossParameters.X1)/nSamplesX; double dy = (PathLossParameters.Y2 - PathLossParameters.Y1)/nSamplesY; double dz = (PathLossParameters.Z2 - PathLossParameters.Z1)/nSamplesZ; // Details of the path loss estimates required and column titles fprintf(PathLossFile, "Grid Analysis - %d x %d x %d samples\n", (int)nSamplesX == nSamplesX ? (int)nSamplesX+1 : (int)nSamplesX+2, (int)nSamplesY == nSamplesY ? (int)nSamplesY+1 : (int)nSamplesY+2, (int)nSamplesZ == nSamplesZ ? (int)nSamplesZ+1 : (int)nSamplesZ+2); for (int k=0; k < nSamplesZ; k++) { z = PlaceWithinGridZ(RoundToNearest((PathLossParameters.Z1+dz*k)/GridSpacing)); fprintf(PathLossFile, "\nHeight = %f\n\nX\t\tY\t\tPL(dB)\n", z*GridSpacing); for (int j=0; j < nSamplesY; j++) { for (int i=0; i < nSamplesX; i++) { x = PlaceWithinGridX(RoundToNearest((PathLossParameters.X1+dx*i)/GridSpacing)); y = PlaceWithinGridY(RoundToNearest((PathLossParameters.Y1+dy*j)/GridSpacing)); PathLoss = VoltageToDB(SPEED_OF_LIGHT/Frequency * Grid[x][y][z].Vmax * KAPPA/4/M_PI/GridSpacing); fprintf(PathLossFile, "%f\t%f\t%f\n", x*GridSpacing, y*GridSpacing, PathLoss); } x = PlaceWithinGridX(RoundToNearest(PathLossParameters.X2/GridSpacing)); y = PlaceWithinGridY(RoundToNearest((PathLossParameters.Y1+dy*j)/GridSpacing)); PathLoss = VoltageToDB(SPEED_OF_LIGHT/Frequency * Grid[x][y][z].Vmax * KAPPA/4/M_PI/GridSpacing); fprintf(PathLossFile, "%f\t%f\t%f\n", x*GridSpacing, y*GridSpacing, PathLoss); } // Print a path loss estimate of the outer X row nearest X2,Y2 on the grid (may not be a whole sample space apart) for (int i=0; i < nSamplesX; i++) { x = PlaceWithinGridX(RoundToNearest((PathLossParameters.X1+dx*i)/GridSpacing)); y = PlaceWithinGridY(RoundToNearest(PathLossParameters.Y2/GridSpacing)); PathLoss = VoltageToDB(SPEED_OF_LIGHT/Frequency * Grid[x][y][z].Vmax * KAPPA/4/M_PI/GridSpacing); fprintf(PathLossFile, "%f\t%f\t%f\n", x*GridSpacing, y*GridSpacing, PathLoss); } x = PlaceWithinGridX(RoundToNearest(PathLossParameters.X2/GridSpacing)); y = PlaceWithinGridY(RoundToNearest(PathLossParameters.Y2/GridSpacing)); PathLoss = VoltageToDB(SPEED_OF_LIGHT/Frequency * Grid[x][y][z].Vmax * KAPPA/4/M_PI/GridSpacing); fprintf(PathLossFile, "%f\t%f\t%f\n", x*GridSpacing, y*GridSpacing, PathLoss); } z = PlaceWithinGridZ(RoundToNearest((PathLossParameters.Z2)/GridSpacing)); fprintf(PathLossFile, "\nHeight = %f\n\nX\t\tY\t\tPL(dB)\n", z*GridSpacing); for (int j=0; j < nSamplesY; j++) { for (int i=0; i < nSamplesX; i++) { x = PlaceWithinGridX(RoundToNearest((PathLossParameters.X1+dx*i)/GridSpacing)); y = PlaceWithinGridY(RoundToNearest((PathLossParameters.Y1+dy*j)/GridSpacing)); PathLoss = VoltageToDB(SPEED_OF_LIGHT/Frequency * Grid[x][y][z].Vmax * KAPPA/4/M_PI/GridSpacing); fprintf(PathLossFile, "%f\t%f\t%f\n", x*GridSpacing, y*GridSpacing, PathLoss); } x = PlaceWithinGridX(RoundToNearest(PathLossParameters.X2/GridSpacing)); y = PlaceWithinGridY(RoundToNearest((PathLossParameters.Y1+dy*j)/GridSpacing)); PathLoss = VoltageToDB(SPEED_OF_LIGHT/Frequency * Grid[x][y][z].Vmax * KAPPA/4/M_PI/GridSpacing); fprintf(PathLossFile, "%f\t%f\t%f\n", x*GridSpacing, y*GridSpacing, PathLoss); } // Print a path loss estimate of the outer X row nearest X2,Y2 on the grid (may not be a whole sample space apart) for (int i=0; i < nSamplesX; i++) { x = PlaceWithinGridX(RoundToNearest((PathLossParameters.X1+dx*i)/GridSpacing)); y = PlaceWithinGridY(RoundToNearest(PathLossParameters.Y2/GridSpacing)); PathLoss = VoltageToDB(SPEED_OF_LIGHT/Frequency * Grid[x][y][z].Vmax * KAPPA/4/M_PI/GridSpacing); fprintf(PathLossFile, "%f\t%f\t%f\n", x*GridSpacing, y*GridSpacing, PathLoss); } x = PlaceWithinGridX(RoundToNearest(PathLossParameters.X2/GridSpacing)); y = PlaceWithinGridY(RoundToNearest(PathLossParameters.Y2/GridSpacing)); PathLoss = VoltageToDB(SPEED_OF_LIGHT/Frequency * Grid[x][y][z].Vmax * KAPPA/4/M_PI/GridSpacing); fprintf(PathLossFile, "%f\t%f\t%f\n", x*GridSpacing, y*GridSpacing, PathLoss); break; } } if (fclose(PathLossFile)) { printf("Path loss file close unsuccessful\n"); } } free(FilenameBuffer); }