int main() { load(config); load(config.config(), config); Module *input = init(config.input()); Module *output = init(config.output()); if(config.verbose()) { pretty_print(clog, config); v1 = viewer() .size(config.width(), config.height()) .layer(1) .name("Distorted points") .pen_width(3) .pen_color(red) ; v2 = viewer() .layer(2) .name("Undistorted points") .show(false) .pen_color(blue) ; v3 = viewer() .layer(3) .name("Proposed points") .pen_color(green) ; } for(double x = 0; x < config.width(); x += config.step()) for(double y = 0; y < config.height(); y += config.step()) { Error e; e.p2 << x, y; input->init(e); output->add(e); if(config.verbose()) { Array2d p = e.p3.head<2>().array() * config.focal() + config.center(); v1.add_point(e.p2.x(), e.p2.y()); v2.add_line(e.p2.x(), e.p2.y(), p.x(), p.y()); } } if(config.verbose()) { v1.update(); v2.update(); output->run_verbose(); v3.title("Done, you can now close this window"); wait_viewers(); } else { output->run(); } }
void ComputeMeanAndVariance(Array2d<double>& data,Array2dC<double>& avg,Array2d<double>& cov,const bool subtractMean) { avg.Create(1,data.ncol); avg.Zero(); cov.Create(data.ncol,data.ncol); cov.Zero(); for(int i=0;i<data.nrow;i++) { for(int j=0;j<data.ncol;j++) avg.buf[j] += data.p[i][j]; for(int j=0;j<data.ncol;j++) for(int k=0;k<data.ncol;k++) cov.p[j][k] += data.p[i][j]*data.p[i][k]; } const double r = 1.0/data.nrow; for(int i=0;i<data.ncol;i++) avg.buf[i] *= r; if(subtractMean) { for(int i=0;i<data.ncol;i++) for(int j=0;j<data.ncol;j++) cov.p[i][j] = cov.p[i][j]*r-avg.buf[i]*avg.buf[j]; } else { for(int i=0;i<data.ncol;i++) for(int j=0;j<data.ncol;j++) cov.p[i][j] = cov.p[i][j]*r; } }
void pop_preview(Array2d* a) { if (a) { a->resize(boost::extents[pv_.size()][pv_[0].size()]); *a = pv_; print2d(std::cerr, *a); } auto p = const_mats_[::rand() % const_mats_.size()]; int x = ::sqrt(p.second); pv_.resize(boost::extents[x][x]); pv_.assign(p.first, p.first + p.second); print2d(std::cerr, pv_); }
void SVD(Array2d<double>& a,double* w,Array2d<double>& v) { // w -- signular values; a = UWV', where U is stored in a // NOTE: a.nrow >= a.ncol is required Array2dC<double*> nr_a(1,a.nrow+1),nr_v(1,a.ncol+1); v.Create(a.ncol,a.ncol); for(int i=0;i<a.nrow;i++) nr_a.buf[i+1] = a.p[i] - 1; for(int i=0;i<a.ncol;i++) nr_v.buf[i+1] = v.p[i] - 1; svdcmp(nr_a.buf,a.nrow,a.ncol,w-1,nr_v.buf); }
void Grid:: apply_preconditioner(const Array2d &x, Array2d &y, Array2d &m) { int i, j; float d; m.zero(); // solve L*m=x for(j=1; j<x.ny-1; ++j) for(i=1; i<x.nx-1; ++i) if(marker(i,j)==FLUIDCELL){ d=x(i,j) - poisson(i-1,j,1)*preconditioner(i-1,j)*m(i-1,j) - poisson(i,j-1,2)*preconditioner(i,j-1)*m(i,j-1); m(i,j)=preconditioner(i,j)*d; } // solve L'*y=m y.zero(); for(j=x.ny-2; j>0; --j) for(i=x.nx-2; i>0; --i) if(marker(i,j)==FLUIDCELL){ d=m(i,j) - poisson(i,j,1)*preconditioner(i,j)*y(i+1,j) - poisson(i,j,2)*preconditioner(i,j)*y(i,j+1); y(i,j)=preconditioner(i,j)*d; } }
void Grid:: apply_poisson(const Array2d &x, Array2d &y) { y.zero(); for(int j=1; j<poisson.ny-1; ++j) for(int i=1; i<poisson.nx-1; ++i){ if(marker(i,j)==FLUIDCELL){ y(i,j)=poisson(i,j,0)*x(i,j) + poisson(i-1,j,1)*x(i-1,j) + poisson(i,j,1)*x(i+1,j) + poisson(i,j-1,2)*x(i,j-1) + poisson(i,j,2)*x(i,j+1); } } }
bool next_round() { pop_preview(&smat_); p_[1] = int(vmat_[0].size() - smat_[0].size()) / 2; p_[0] = -int(smat_.size()-1); while (p_[0] <= 0) { Point tmp = p_; if (is_collision(vmat_, p_, smat_)) { or_assign(vmat_, p_, smat_); over(); return 0; } if (p_[0] == 0) break; ++p_[0]; } td_ = time(0); return 1; }
RowCol searchSorted2dArray(const Array2d<T>& a, const T& target) { int rows = a.size(); int cols = a[0].size(); int top = 0; int bottom = rows - 1; int left = 0; int right = cols - 1; int row = bottom; int col = left; while (row >= top && col <= right) { if (a[row][col] < target) { ++col; } else if (a[row][col] > target) { --row; } else { return RowCol(row, col); } } return RowCol(-1, -1); }
void buildDistributions( SkeletonVisibilityDistributions& distributions, const Scene& scene, const std::vector<GraphNodeIndex>& skeletonNodes, const EmissionVertex* pEmissionVertexArray, const Array2d<BDPTPathVertex>& surfaceLightVertexArray, std::size_t lightPathCount, bool useNodeRadianceScale, bool useNodeDistanceScale, std::size_t threadCount) { auto maxDepth = surfaceLightVertexArray.size(0); auto pathCount = lightPathCount; auto evalDefaultConservativeWeight = [&](std::size_t depth, std::size_t pathIdx) { if(!depth) { if(!pEmissionVertexArray[pathIdx].m_pLight || pEmissionVertexArray[pathIdx].m_fLightPdf == 0.f) { return 0.f; } return 1.f; } if(surfaceLightVertexArray(depth - 1u, pathIdx).m_fPathPdf == 0.f) { return 0.f; } return 1.f; }; auto evalNodeVisibilityWeight = [&](std::size_t depth, std::size_t pathIdx, const Vec3f& nodePosition, float nodeMaxballRadius) { if(!depth) { if(!pEmissionVertexArray[pathIdx].m_pLight || pEmissionVertexArray[pathIdx].m_fLightPdf == 0.f) { return 0.f; } RaySample shadowRaySample; auto L = pEmissionVertexArray[pathIdx].m_pLight->sampleDirectIllumination( scene, pEmissionVertexArray[pathIdx].m_PositionSample, nodePosition, shadowRaySample); if(L == zero<Vec3f>() || shadowRaySample.pdf == 0.f) { return 0.f; } if(scene.occluded(shadowRaySample.value)) { return 0.f; } Vec3f weight(1.f); if(useNodeRadianceScale) { weight *= L; } if(useNodeDistanceScale) { weight /= shadowRaySample.pdf; } return luminance(weight); } auto& lightVertex = surfaceLightVertexArray(depth - 1, pathIdx); if(lightVertex.m_fPathPdf == 0.f) { return 0.f; } auto& I = lightVertex.m_Intersection; auto dir = nodePosition - I.P; auto l = BnZ::length(dir); dir /= l; if(dot(I.Ns, dir) <= 0.f || scene.occluded(Ray(I, dir, l))) { return 0.f; } Vec3f weight(1.f); if(useNodeRadianceScale) { weight *= lightVertex.m_Power; } if(useNodeDistanceScale) { weight /= sqr(l); } return luminance(weight); }; auto evalNodeGeometryWeight = [&](std::size_t depth, std::size_t pathIdx, const Vec3f& nodePosition, float nodeMaxballRadius) { if(!depth) { if(!pEmissionVertexArray[pathIdx].m_pLight || pEmissionVertexArray[pathIdx].m_fLightPdf == 0.f) { return 0.f; } RaySample shadowRaySample; auto L = pEmissionVertexArray[pathIdx].m_pLight->sampleDirectIllumination( scene, pEmissionVertexArray[pathIdx].m_PositionSample, nodePosition, shadowRaySample); if(L == zero<Vec3f>() || shadowRaySample.pdf == 0.f) { return 0.f; } Vec3f weight(1.f); if(useNodeRadianceScale) { weight *= L; } if(useNodeDistanceScale) { weight /= shadowRaySample.pdf; } return luminance(weight); } auto& lightVertex = surfaceLightVertexArray(depth - 1, pathIdx); if(lightVertex.m_fPathPdf == 0.f) { return 0.f; } auto& I = lightVertex.m_Intersection; auto dir = nodePosition - I.P; auto l = BnZ::length(dir); dir /= l; if(dot(I.Ns, dir) <= 0.f) { return 0.f; } Vec3f weight(1.f); if(useNodeRadianceScale) { weight *= lightVertex.m_Power; } if(useNodeDistanceScale) { weight /= sqr(l); } return luminance(weight); }; auto evalNodeConservativeWeight = [&](std::size_t depth, std::size_t pathIdx, const Vec3f& nodePosition, float nodeMaxballRadius) { if(!depth) { if(!pEmissionVertexArray[pathIdx].m_pLight || pEmissionVertexArray[pathIdx].m_fLightPdf == 0.f) { return 0.f; } return 1.f; } if(surfaceLightVertexArray(depth - 1u, pathIdx).m_fPathPdf == 0.f) { return 0.f; } return 1.f; }; distributions.buildDistributions(*scene.getCurvSkeleton(), skeletonNodes, pathCount, maxDepth, threadCount, evalDefaultConservativeWeight, evalNodeVisibilityWeight, evalNodeGeometryWeight, evalNodeConservativeWeight); }
/** Make sure the [Steady_insac_ib](@ref Steady_insac_ib) object has been [setup](@ref setup) and initialized with some [initial condition](@ref setInitialConditions). Both the momentum-magnitude residual and the mass flux are taken as convergence criteria; the tolerance for the mass flux is the square-root of the tolerance for the relative momentum-magnitude residual. [tol](@ref tol) is the latter. */ void Steady_insac_ib::solve() { setInitialConditions(); setBCs(); compute_beta(); int i,j,k, ivar,iq,jq; vector<double> ubc(nvar); vector<double> uave(nvar); double nxd, nyd, udotn, unormal, vnormal, utangent, vtangent; double resnorm, resnorm0, massflux, dtv; cout << "Steady_insac_ib: solve(): Beginning the time-march." << endl; for(int n = 0; n < maxiter; n++) { // calculate stuff needed for this iteration setBCs(); compute_timesteps(); for(k = 0; k < nvar; k++) res[k].zeros(); // compute fluxes invf->compute_fluxes(); if(!isinviscid) grad->compute_fluxes(); if(haveIB) { for(i = 1; i <= m->gimx()-1; i++) for(j = 1; j <= m->gjmx()-1; j++) { // CAUTION: checking equality of doubles if(ib.ghh(i,j) == 1.0) { // if interior cell, set call properties to zero if(ib.gdistg(i,j) < 0) { for(k = 0; k < nvar; k++) ubc[k] = 0.0; } else { for(k = 0; k < nvar; k++) uave[k] = 0.0; for(k = 0; k < ib.gncellvnbd(); k++) { iq = i + ib.gidif(k); jq = j + ib.gjdif(k); for(ivar = 0; ivar < nvar; ivar++) uave[ivar] += ib.gweights(i,j,k)*u[ivar].get(iq,jq); } nxd = ib.gsnx( ib.glpri(i,j), ib.gitag(i,j,ib.glpri(i,j)) ); nyd = ib.gsny( ib.glpri(i,j), ib.gitag(i,j,ib.glpri(i,j)) ); udotn = uave[1]*nxd + uave[2]*nyd; unormal = udotn*nxd; vnormal = udotn*nyd; utangent = uave[1] - unormal; vtangent = uave[2] - vnormal; ubc[0] = uave[0]; // "classic boundary layer" approximation - might fail for boundaries with high curvature ubc[1] = utangent*ib.gacoef(i,j) + unormal*ib.gbcoef(i,j); ubc[2] = vtangent*ib.gacoef(i,j) + vnormal*ib.gbcoef(i,j); } dtv = m->gvol(i,j)/dt.get(i,j); // we update the residuals like this so that when we update u,v,p later, we get ubc,vbc,pbc in the band cell res[0](i,j) = dtv*(u[0](i,j)-ubc[0])/(beta.get(i,j)*beta.get(i,j)); res[1](i,j) = dtv*rho*(u[1].get(i,j)-ubc[1]); res[2](i,j) = dtv*rho*(u[2].get(i,j)-ubc[2]); } } } // check convergence resnorm = 0; massflux = 0; for(i = 1; i <= m->gimx()-1; i++) for(j = 1; j <= m->gjmx()-1; j++) { resnorm += (res[1].get(i,j)*res[1].get(i,j) + res[2].get(i,j)*res[2].get(i,j))*m->gvol(i,j); massflux += res[0].get(i,j); } resnorm = sqrt(resnorm); if(n == 0) resnorm0 = resnorm; if(n == 1 || n%10 == 0) { cout << "Steady_insac_ib: solve(): Iteration " << n << ": relative L2 norm of residual = " << resnorm/resnorm0 << ", net mass flux = " << massflux << endl; cout << " L2 norm of residual = " << resnorm << endl; } if(resnorm/resnorm0 < tol && fabs(massflux) < sqrt(tol)) { cout << "Steady_insac_ib: solve(): Converged in " << n << " iterations. Norm of final residual = " << resnorm << ", final net mass flux = " << massflux << endl; break; } // update u for(i = 1; i <= m->gimx()-1; i++) for(j = 1; j <= m->gjmx()-1; j++) { dtv = dt.get(i,j)/m->gvol(i,j); u[0](i,j) = u[0].get(i,j) - res[0].get(i,j)*beta.get(i,j)*beta.get(i,j)*dtv; for(k = 1; k < nvar; k++) u[k](i,j) = u[k].get(i,j) - res[k].get(i,j)*dtv/rho; } } }
void Steady_insac_ib::setup(Structmesh2d* mesh, double dens, double visc, vector<int> _bcflags, vector<vector<double>> _bvalues, string gradscheme, string pressure_scheme, double refvel, double CFL, double tolerance, int maxiters, bool have_IB, string ibfile, bool is_inviscid) { ndim = 2; m = mesh; nvar = 3; rho = dens; mu = visc; bcflags = _bcflags; bvalues = _bvalues; pressurescheme = pressure_scheme; uref = refvel; haveIB = have_IB; if(gradscheme == "normtan") grad = new NormTanGradientIns; else if(gradscheme == "parallelcv") grad = new ParallelCVGradientIns; else grad = new ThinLayerGradientIns; invf = new InviscidFlux; u = new Array2d<double>[nvar]; res = new Array2d<double>[nvar]; visc_lhs = new Array2d<double>[5]; isalloc = true; isinviscid = is_inviscid; for(int i = 0; i < nvar; i++) { u[i].setup(m->gimx()+1, m->gjmx()+1); res[i].setup(m->gimx()+1, m->gjmx()+1); } for(int i = 0; i < 5; i++) visc_lhs[i].setup(m->gimx()+1, m->gjmx()+1); beta.setup(m->gimx()+1,m->gjmx()+1); dt.setup(m->gimx()+1,m->gjmx()+1); cfl = CFL; tol = tolerance; maxiter = maxiters; invf->setup(m, u, res, &beta, rho, pressure_scheme, _bcflags, _bvalues); grad->setup(m,u,res,visc_lhs, mu); if(haveIB) { cout << "Steady_insac_ib: setup(): Computing IB-related data." << endl; ib.setup(m, ibfile); ib.classify(); ib.compute_interpolation_data(); } cout << "Steady_insac_ib: setup():\n"; cout << "BC flags "; for(int i = 0; i < 4; i++) cout << bcflags[i] << " "; cout << "\nB values:\n"; for(int i = 0; i < 4; i++) { for(int j = 0; j < 2; j++) cout << bvalues[i][j] << " "; cout << endl; } cout << endl; }
int main() { Array2d<int> arr; Array2d<int> arr_param(10, 10); for( int y = 0; y < 10; ++y ) { for( int x = 0; x < 10; ++x ) { try { arr.Set( x, y, x + y ); arr_param.Set( x, y, x + y ); } catch( std::out_of_range except ) { std::cout << except.what(); } } } Array2d<int> arr_copy( arr ); std::cout << "New 2-d array:" << std::endl; PrintArray( arr, arr.Width(), arr.Height() ); std::cout << std::endl; std::cout << "Width: " << arr.Width() << std::endl; std::cout << "Height: " << arr.Height() << std::endl; std::cout << "Size: " << arr.Size() << std::endl; std::cout << std::endl; try { arr.Shrink( 1, 0, 10, 1 ); } catch( std::out_of_range except ) { std::cout << except.what(); } std::cout << "Shrunk to its first row from the second column to the last column:" << std::endl; PrintArray( arr, arr.Width(), arr.Height() ); std::cout << std::endl; arr.Expand(2, 3, 0); std::cout << "Expanded 2 columns and 3 rows, with the new spaces filled with 0:" << std::endl; PrintArray<int>( arr, arr.Width(), arr.Height() ); std::cout << std::endl; unsigned int count_1 = arr.Count( 1 ); std::cout << "The value 1 appears in the array " << count_1 << " time"; if( count_1 != 1 ) { std::cout << "s"; } std::cout << "." << std::endl; std::cout << std::endl; std::cout << "Original array created with the parametrized constructor:" << std::endl; PrintArray<int>( arr_param, arr_param.Width(), arr_param.Height() ); std::cout << std::endl; std::cout << "Original array made with the copy constructor:" << std::endl; PrintArray<int>( arr_copy, arr_copy.Width(), arr_param.Height() ); std::cout << std::endl; std::cout << "All tests finished." << std::endl; std::cout << "Press any key to finish: "; getchar(); std::cout << std::endl; return 0; }