コード例 #1
0
ファイル: init.c プロジェクト: AsherBond/MondocosmOS
void Init()
{
    struct Cell_head Region;
    int Count;
    int FD, row, col;
    double MinRes;

    G_debug(2, "Init()");

    Rs = Rast_window_rows();
    Cs = Rast_window_cols();
    G_get_set_window(&Region);
    EW = Region.ew_res;
    NS = Region.ns_res;
    if (EW < NS)
	MinRes = EW;
    else
	MinRes = NS;
    CellBuffer = Rast_allocate_c_buf();

    /* Out = FlagCreate( Rs, Cs); */
    Out = (CELL **) G_malloc(sizeof(CELL *) * Rs);
    for (row = 0; row < Rs; row++) {
	Out[row] = Rast_allocate_c_buf();
	Rast_zero_buf(Out[row], CELL_TYPE);
    }

    Cells = FlagCreate(Rs, Cs);
    CellCount = 0;
    if (G_find_raster2("MASK", G_mapset())) {
	FD = Rast_open_old("MASK", G_mapset());
	{
	    for (row = 0; row < Rs; row++) {
		Rast_get_c_row_nomask(FD, CellBuffer, row);
		for (col = 0; col < Cs; col++) {
		    if (CellBuffer[col]) {
			FLAG_SET(Cells, row, col);
			CellCount++;
		    }
		}
	    }
	    Rast_close(FD);
	}
    }
    else {
	for (row = 0; row < Rs; row++) {
	    for (col = 0; col < Cs; col++) {
		FLAG_SET(Cells, row, col);
	    }
	}
	CellCount = Rs * Cs;
    }

    sscanf(Distance->answer, "%lf", &MaxDist);
    if (MaxDist < 0.0)
	G_fatal_error(_("Distance must be >= 0.0"));
    
    G_debug(3, "(MaxDist):%.12lf", MaxDist);
    MaxDistSq = MaxDist * MaxDist;
    if (!SeedStuff->answer) {
	Seed = (int)getpid();
    }
    else {
	sscanf(SeedStuff->answer, "%d", &(Seed));
    }

    if (Seed > SEED_MAX) {
	Seed = Seed % SEED_MAX;
    }
    else if (Seed < SEED_MIN) {
	while (Seed < SEED_MIN)
	    Seed += SEED_MAX - SEED_MIN;
    }

    G_message(_("Generating raster map <%s>..."),
	      Output->answer);

    DoNext = (CELLSORTER *) G_malloc(CellCount * sizeof(CELLSORTER));
    Count = 0;
    for (row = 0; row < Rs; row++) {
	G_percent(row, Rs, 2);
	for (col = 0; col < Cs; col++) {
	    if (0 != FlagGet(Cells, row, col)) {
		DoNext[Count].R = row;
		DoNext[Count].C = col;
		DoNext[Count].Value = GasDev();
		if (++Count == CellCount) {
		    row = Rs;
		    col = Cs;
		}
	    }
	}
    }
    G_percent(1, 1, 1);
    
    qsort(DoNext, CellCount, sizeof(CELLSORTER), comp_array);
}
コード例 #2
0
ファイル: ibngauss.c プロジェクト: gaoshuai/pcraster
int IBNGauss(
     MAP_REAL8 *out,
     const MAP_REAL8 *units,
     const MAP_REAL8 *range,
     const MAP_REAL8 *nrPackages)
{
  int   rSrc,nrRows= units->NrRows(units);
  int   cSrc,nrCols= units->NrCols(units);
  REAL8 unitsValSrc,rangeValSrc,nrPackValSrc;

  /* algorithm wants points->Get() and all others to
   * return FALSE if a value is a missing value
   */
  out->SetGetTest(GET_MV_TEST, out);
  units->SetGetTest(GET_MV_TEST, units);
  nrPackages->SetGetTest(GET_MV_TEST, nrPackages);
  range->SetGetTest(GET_MV_TEST, range);

  /* set all to 0 or MV
   * check range of rangeValSrc
   */
  for(rSrc = 0; rSrc < nrRows; rSrc++)
   for(cSrc = 0; cSrc < nrCols; cSrc++)
   {
    if(units->Get(&unitsValSrc, rSrc, cSrc, units) &&
       nrPackages->Get(&nrPackValSrc, rSrc, cSrc, nrPackages) &&
       range->Get(&rangeValSrc, rSrc, cSrc, range))
    { /* init with remainder */
      REAL8 remainder = unitsValSrc - floor(unitsValSrc);
      out->Put(remainder, rSrc, cSrc, out);
      if (rangeValSrc < 0)
       return  RetError(1,"ibngauss: Domain error on parameters");
    } else /* some of the units maps are MV */
      out->PutMV(rSrc, cSrc, out);
   }


  for(rSrc = 0; rSrc < nrRows; rSrc++)
   for(cSrc = 0; cSrc < nrCols; cSrc++)
    if(units->Get(&unitsValSrc, rSrc, cSrc, units) &&
       nrPackages->Get(&nrPackValSrc, rSrc, cSrc, nrPackages) &&
       range->Get(&rangeValSrc, rSrc, cSrc, range)
      )
    {
      int i,nrInputUnits = (int)floor(unitsValSrc);
      int nrPackages = (int)nrPackValSrc; /* round to integer */
      int packageSize;
      int nrPackagesWithOneMore;
      if (nrPackages <= 0 || nrPackages > nrInputUnits) {
        nrPackages=nrInputUnits;
        packageSize=1;
        nrPackagesWithOneMore=0;
      }
      else {
        packageSize=nrInputUnits/nrPackages;          /* integer division */
        nrPackagesWithOneMore= nrInputUnits%nrPackages; /* integer modulo */
      }
      rangeValSrc /= Side(); /* transform to pixel lengths */
       for(i=0; i < nrPackages; i++) {
         /* GasDev returns value from normal distribution mean=0,sd=1 */
          REAL8 length = GasDev()*rangeValSrc;
         /* Ran() return an uniform number
          * we create an angle between 0 and 180 degrees
          */
          REAL8 angle =Ran()*M_PI;
         /* now since length can be negative or positive we
          * generate a receiving cell address having an angle
          * with the src cell between 0 and 360 degrees
          */
          int xColDest = cSrc+(int)(length*cos(angle));
          int yRowDest = rSrc+(int)(length*sin(angle));
          REAL8 valDest;
          if (out->Get(&valDest, yRowDest, xColDest, out)) {
            /* destination is defined area */
            valDest+=packageSize;
           if (i < nrPackagesWithOneMore) {
             /* the first nrPackagesWithOneMore package do 1 more */
             valDest+=1;
           }
           out->Put(valDest, yRowDest, xColDest, out);
          }
       } /* for all packages */
    } /* eo for all cells */
  return 0;    /* successful terminated */
}
コード例 #3
0
ファイル: calcsurf.c プロジェクト: imincik/pkg-grass
void CalcSurface(void)
{
    int Count, OutRows, OutCols;
    int Row, Row2, Col, Col2, RanRows, RanCols;
    int owC, oeC, onR, osR, wC, eC, nR, sR;
    double **Randoms;

    G_debug(2, "CalcSurface()");

    OutRows = BigF.RowPlus;
    OutCols = BigF.ColPlus;
    RanRows = (2 * OutRows + Rs);
    RanCols = (2 * OutCols + Cs);
    owC = osR = 0;
    wC = OutCols;
    sR = OutRows;
    oeC = RanCols - 1;
    onR = RanRows - 1;

    if (OutCols > 0)
	eC = RanCols - (OutCols + 1);
    else
	eC = oeC;

    if (OutRows > 0)
	nR = RanRows - (OutRows + 1);
    else
	nR = onR;

    Randoms = (double **)G_malloc(sizeof(double *) * RanRows);
    for (Row = 0; Row < RanRows; Row++)
	Randoms[Row] = (double *)G_malloc(RanCols * sizeof(double));

    /* OLD
       for( Row = OutRows; Row < RanRows; Row++) {
       for( Col = OutCols; Col < OutCols + Cs; Col++) {
       Randoms[ Row][ Col] = GasDev();
       }
       }

       for( Row = OutRows - 1; Row >= 0; Row--) {
       for( Col = OutCols; Col < OutCols + Cs; Col++) {
       Randoms[ Row][ Col] = GasDev();
       }
       }

       for( Row = 0; Row < RanRows; Row++) {
       for( Col = 0; Col < OutCols; Col++)
       Randoms[ Row][ Col] = GasDev();
       for( Col = OutCols + Cs; Col < RanCols; Col++)
       Randoms[ Row][ Col] = GasDev();
       }
       end OLD */

    for (Row = sR; Row <= nR; Row++) {
	for (Col = wC; Col <= eC; Col++) {
	    Randoms[Row][Col] = GasDev();
	}
    }

    Col = wC - 1;
    Col2 = eC + 1;
    while (Col >= 0) {
	for (Row = sR; Row <= nR; Row++) {
	    Randoms[Row][Col] = GasDev();
	    Randoms[Row][Col2] = GasDev();
	}
	Col--;
	Col2++;
    }

    Row = sR - 1;
    Row2 = nR + 1;
    while (Row >= 0) {
	for (Col = 0; Col < RanCols; Col++) {
	    Randoms[Row][Col] = GasDev();
	    Randoms[Row2][Col] = GasDev();
	}
	Row--;
	Row2++;
    }

    Count = 0;
    if (FDM == -1) {
	for (Row = 0; Row < Rs; Row++) {
	    if (ODD(Row)) {
		for (Col = Cs - 1; Col >= 0; Col--) {
		    G_percent(Count++, MapCount, 1);
		    Surface[Row][Col] =
			MakePP(Row, Col, OutRows, OutCols, Randoms, BigF);
		}
	    }
	    else {
		for (Col = 0; Col < Cs; Col++) {
		    G_percent(Count++, MapCount, 1);
		    Surface[Row][Col] =
			MakePP(Row, Col, OutRows, OutCols, Randoms, BigF);
		}
	    }
	}
	G_percent(1, 1, 1);
    }
    else {
	for (Row = 0; Row < Rs; Row++) {
	    G_get_map_row_nomask(FDM, CellBuffer, Row);
	    if (ODD(Row)) {
		for (Col = Cs - 1; Col >= 0; Col--) {
		    if (CellBuffer[Col] == 0)
			Surface[Row][Col] = 0.0;
		    else {
			G_percent(Count++, MapCount, 1);
			Surface[Row][Col] =
			    MakePP(Row, Col, OutRows, OutCols, Randoms, BigF);
		    }
		}
	    }
	    else {
		for (Col = 0; Col < Cs; Col++) {
		    if (CellBuffer[Col] == 0)
			Surface[Row][Col] = 0.0;
		    else {
			G_percent(Count++, MapCount, 1);
			Surface[Row][Col] =
			    MakePP(Row, Col, OutRows, OutCols, Randoms, BigF);
		    }
		}
	    }
	}
	G_percent(1, 1, 1);
    }

    G_free(Randoms);
}