float noise(float x, float y, float z) { int ix = ifloor(x); int iy = ifloor(y); int iz = ifloor(z); int X = ix & (NOISE_PERM_SIZE-1), // FIND UNIT CUBE THAT Y = iy & (NOISE_PERM_SIZE-1), // CONTAINS POINT. Z = iz & (NOISE_PERM_SIZE-1); x -= ix; // FIND RELATIVE X,Y,Z y -= iy; // OF POINT IN CUBE. z -= iz; float u = fade(x), // COMPUTE FADE CURVES v = fade(y), // FOR EACH OF X,Y,Z. w = fade(z); int A = p[X ]+Y; int AA = p[A]+Z; int AB = p[A+1]+Z; int B = p[X+1]+Y; int BA = p[B]+Z; int BB = p[B+1]+Z; // THE 8 CUBE CORNERS, return lerp(w, lerp(v, lerp(u, grad(p[AA ], x , y , z ), // AND ADD grad(p[BA ], x-1, y , z )), // BLENDED lerp(u, grad(p[AB ], x , y-1, z ), // RESULTS grad(p[BB ], x-1, y-1, z ))),// FROM 8 lerp(v, lerp(u, grad(p[AA+1], x , y , z-1 ), // CORNERS grad(p[BA+1], x-1, y , z-1 )), // OF CUBE lerp(u, grad(p[AB+1], x , y-1, z-1 ), grad(p[BB+1], x-1, y-1, z-1 )))); } // end noise()
BlockPosition Me::pointedBlock() { BlockPosition blockPosition; Vector direction = this->direction(); Vector position = this->eyePosition(); for(float d = 1; d < F_MAX_BLOCK_SEAK; d += F_BLOCK_SEAK_STEP) { blockPosition.x = ifloor(position.x + direction.x * d); blockPosition.y = ifloor(position.y + direction.y * d); blockPosition.z = ifloor(position.z + direction.z * d); if(!world()->block(blockPosition)->isVoid()) { return blockPosition; } } return BlockPosition(); // return 0,0,0 }
BlockPosition Me::pointedFreeBlock() { BlockPosition blockPosition, lastBlockPosition; Vector direction = this->direction(); Vector position = this->eyePosition(); for(float d = 1; d < F_MAX_BLOCK_SEAK; d += F_BLOCK_SEAK_STEP) { lastBlockPosition = blockPosition; blockPosition.x = ifloor(position.x + direction.x * d); blockPosition.y = ifloor(position.y + direction.y * d); blockPosition.z = ifloor(position.z + direction.z * d); // If we met a non void block... if(!world()->block(blockPosition)->isVoid()) { return lastBlockPosition; // We return the last void block } } return BlockPosition(); // return 0,0,0 }
/** Interpolate simu->gain. */ static void interp_gain(double *out, const dcell *gain, const dmat *gainx, double sigma2){ const long nx=gainx->nx; const double xsep=(log(gainx->p[nx-1])-log(gainx->p[0]))/(nx-1); const double xx=(log(sigma2)-log(gainx->p[0]))/xsep; int ig; if(xx<0){/*too small. */ ig=0; }else if(xx>=nx-1){/*too big */ ig=nx-1; }else{/*within the range */ ig=ifloor(xx); /*2013-12-06: use one of the set, not interpolate*/ } memcpy(out, gain->p[ig]->p, sizeof(double)*gain->p[ig]->nx); }
/* This wrapper for the assembly subroutine handles alignment. */ static inline int cntbits(uint16_t *a, size_t n) { int b = 0; unsigned char *p = (unsigned char *) a; unsigned char *e; const int blksiz = 8; /* call countbits with 64-bit "blocks" */ for (; n > 0 && ((unsigned long) p) % blksiz; ++p, --n) /* until aligned */ b += cbchar(*p); e = &p[n]; n = ifloor(e - p, blksiz); b += countbits((uint16_t *) p, n); /* this is the fast part */ for (p += n; p < e; ++p) b += cbchar(*p); /* the remaining odd bytes */ return b; }
inline int Noise::RandInt(const int &low, const int &high) { // 7/21/00 // Correction added: while (r > limit -1) // // Why? As apl9f explains it, suppose RAND_MAX is 8 and the interval // is 4. Then, the first interval is going to be 0..3 and the second // interval is 4..7 (each of these intervals contains 4.) You don't // want to hit 8 -- that's out of the limit. // ----------------------------------------------------- // 7/18/00 -- New, more correct implementation of RandInt. // This new randint makes use of the Linux GCC random number generator. // We have checked it via generation of an 8000 neuron cMatrix. This // corrects the bug in which neurons closer together in cMatrix[x] would // be more likely to connect to the same neuron. // Blame dws3t and pal4s. // ----------------------------------------------------- // ----------------------------------------------------- // 7/8/04 -- New, even more correct implementation of RandInt. // This new randint uses TRandDbl() by default. MRandDbl() can also be used // but is STRONGLY discouraged. An 8000 neuron cMatrix was analyzed by the // following technique: I compared how many connections neuron i had with // neuron i-1 for all neurons, and then did the same thing for i-2, etc., up // to i-50. I then plotted the mean values for neurons 1001-2000, 2001-3000, // etc. For TRandDbl() and the commented out rand() implementation, the // graphs for one group of neurons to the next shared little similarity. The // same is not true for MRandDbl(). // I guess you can blame abh2n, if you must. // ----------------------------------------------------- // MRandDbl was the Numerical Recipes in C random number generator // How this works: // Because of integer division, myRange will be the largest // number of complete Intervals in 0...RAND_MAX. // limit is < RAND_MAX. It is the highest multiple of interval // less than RAND_MAX. So if r > limit, we do NOT want to mod on // it -- rather, we generate another random number. return ifloor(Uniform(low, high + 1 - EPSILON)); }
bool sample(bilinear_sampler, const SrcView& src, const point2<F>& p, DstP& result) { typedef typename SrcView::value_type SrcP; ///modif point2<std::ptrdiff_t> p0(ifloor(p)); // the closest integer coordinate top left from p point2<F> frac(p.x-p0.x, p.y-p0.y); if (p0.x < 0 || p0.y < 0 || p0.x>=src.width() || p0.y>=src.height()) return false; pixel<F,devicen_layout_t<num_channels<SrcView>::value> > mp(0); // suboptimal typename SrcView::xy_locator loc=src.xy_at(p0.x,p0.y); if (p0.x+1<src.width()) { if (p0.y+1<src.height()) { // most common case - inside the image, not on the last row or column detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, (1-frac.x)*(1-frac.y),mp); detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1], frac.x *(1-frac.y),mp); ++loc.y(); detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, (1-frac.x)* frac.y ,mp); detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1], frac.x * frac.y ,mp); } else { // on the last row, but not the bottom-right corner pixel detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, (1-frac.x),mp); detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1], frac.x ,mp); } } else { if (p0.y+1<src.height()) { // on the last column, but not the bottom-right corner pixel detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, (1-frac.y),mp); ++loc.y(); detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, frac.y ,mp); } else { // the bottom-right corner pixel detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc,1,mp); } } // Convert from floating point average value to the source type SrcP src_result; cast_pixel(mp,src_result); color_convert(src_result, result); return true; }
Mesh item3DImage(TextureDescriptor td, float thickness) { assert(td); assert(td.maxU <= 1 && td.minU >= 0 && td.maxV <= 1 && td.minV >= 0); Image image = td.image; unsigned iw = image.width(); unsigned ih = image.height(); float fMinX = iw * td.minU; float fMaxX = iw * td.maxU; float fMinY = ih * (1 - td.maxV); float fMaxY = ih * (1 - td.minV); int minX = ifloor(1e-5f + fMinX); int maxX = ifloor(-1e-5f + fMaxX); int minY = ifloor(1e-5f + fMinY); int maxY = ifloor(-1e-5f + fMaxY); assert(minX <= maxX && minY <= maxY); float scale = std::min<float>(1.0f / (1 + maxX - minX), 1.0f / (1 + maxY - minY)); if(thickness <= 0) thickness = scale; TextureDescriptor backTD = td; backTD.maxU = td.minU; backTD.minU = td.maxU; float faceMinX = scale * (fMinX - minX); float faceMinY = scale * (maxY - fMaxY + 1); float faceMaxX = faceMinX + scale * (fMaxX - fMinX); float faceMaxY = faceMinY + scale * (fMaxY - fMinY); Mesh retval = transform(Matrix::translate(faceMinX, faceMinY, -0.5f).concat(Matrix::scale(faceMaxX - faceMinX, faceMaxY - faceMinY, thickness)), unitBox(TextureDescriptor(), TextureDescriptor(), TextureDescriptor(), TextureDescriptor(), backTD, td)); for(int iy = minY; iy <= maxY; iy++) { for(int ix = minX; ix <= maxX; ix++) { bool transparent = isPixelTransparent(image.getPixel(ix, iy)); if(transparent) continue; bool transparentNIX = true, transparentPIX = true, transparentNIY = true, transparentPIY = true; // image transparent for negative and positive image coordinates if(ix > minX) transparentNIX = isPixelTransparent(image.getPixel(ix - 1, iy)); if(ix < maxX) transparentPIX = isPixelTransparent(image.getPixel(ix + 1, iy)); if(iy > minY) transparentNIY = isPixelTransparent(image.getPixel(ix, iy - 1)); if(iy < maxY) transparentPIY = isPixelTransparent(image.getPixel(ix, iy + 1)); float pixelU = (ix + 0.5f) / iw; float pixelV = 1.0f - (iy + 0.5f) / ih; TextureDescriptor pixelTD = TextureDescriptor(image, pixelU, pixelU, pixelV, pixelV); TextureDescriptor nx, px, ny, py; if(transparentNIX) nx = pixelTD; if(transparentPIX) px = pixelTD; if(transparentNIY) py = pixelTD; if(transparentPIY) ny = pixelTD; Matrix tform = Matrix::translate(static_cast<float>(ix - minX), static_cast<float>(maxY - iy), -0.5f).concat(Matrix::scale(scale, scale, thickness)); retval.append(transform(tform, unitBox(nx, px, ny, py, TextureDescriptor(), TextureDescriptor()))); } } return std::move(retval); }
/** Returns the transpose of a ztilt gradient operator that converts the OPDs defined on xloc to subapertures defines on saloc. */ dsp * mkzt(loc_t* xloc, double *amp, loc_t *saloc, int saorc, double scale, double dispx, double dispy) { /*compute ztilt influence function from xloc to saloc saorc: SALOC is subaperture origin or center. 1: origin (lower left corner), 0: center. */ long nsa=saloc->nloc; double dsa=saloc->dx; double dx1=1./xloc->dx; double dx2=scale*dx1; double dy1=1./xloc->dy; double dy2=scale*dy1; loc_create_map(xloc); map_t *map=xloc->map; dispx=(dispx-map->ox+saorc*dsa*0.5*scale)*dx1; dispy=(dispy-map->oy+saorc*dsa*0.5*scale)*dy1; double dsa2=dsa*0.5*dx2; long nmax=(dsa2*2+2)*(dsa2*2+2); long *ind=mycalloc(nmax,long); loc_t *sloc=mycalloc(1,loc_t); sloc->dx=xloc->dx; sloc->dy=xloc->dy; sloc->locx=mycalloc(nmax,double); sloc->locy=mycalloc(nmax,double); double *amploc=NULL; if(amp) amploc=mycalloc(nmax,double); dsp*zax=dspnew(xloc->nloc,nsa,xloc->nloc); dsp*zay=dspnew(xloc->nloc,nsa,xloc->nloc); long xcount=0,ycount=0; spint *xpp=zax->p; spint *xpi=zax->i; double *xpx=zax->x; spint *ypp=zay->p; spint *ypi=zay->i; double *ypx=zay->x; const double *locx=xloc->locx; const double *locy=xloc->locy; double *slocx=sloc->locx; double *slocy=sloc->locy; for(int isa=0; isa<nsa; isa++){ /*center of subaperture when mapped onto XLOC*/ double scx=saloc->locx[isa]*dx2+dispx; double scy=saloc->locy[isa]*dy2+dispy; int count=0; /*find points that belongs to this subaperture. */ for(int iy=iceil(scy-dsa2); iy<ifloor(scy+dsa2);iy++){ for(int ix=iceil(scx-dsa2); ix<ifloor(scx+dsa2);ix++){ int ii=loc_map_get(map, ix, iy); if(ii>0){ ii--; ind[count]=ii; slocx[count]=locx[ii]; slocy[count]=locy[ii]; if(amp) amploc[count]=amp[ii]; count++; } } } /*locwrite(sloc,"sloc_isa%d",isa); */ /*writedbl(amploc,count,1,"amploc_isa%d",isa); */ sloc->nloc=count; dmat *mcc=loc_mcc_ptt(sloc,amploc); /*writebin(mcc,"mcc_isa%d",isa); */ dinvspd_inplace(mcc); /*writebin(mcc,"imcc_isa%d",isa); */ xpp[isa]=xcount; ypp[isa]=ycount; for(int ic=0; ic<count; ic++){ double xx=IND(mcc,0,1)+IND(mcc,1,1)*slocx[ic]+IND(mcc,2,1)*slocy[ic]; double yy=IND(mcc,0,2)+IND(mcc,1,2)*slocx[ic]+IND(mcc,2,2)*slocy[ic]; if(amp){ xx*=amploc[ic]; yy*=amploc[ic]; } xpi[xcount]=ind[ic]; xpx[xcount]=xx; xcount++; ypi[ycount]=ind[ic]; ypx[ycount]=yy; ycount++; } dfree(mcc); } xpp[nsa]=xcount; ypp[nsa]=ycount; locfree(sloc); free(ind); dspsetnzmax(zax,xcount); dspsetnzmax(zay,ycount); dsp*ZAT=dspcat(zax,zay,1); dspfree(zax); dspfree(zay); if(amp) free(amploc); return ZAT; }