vec3 TerrainNoise::getNormal(float x, float y, float eps) const { float ha = getHauteur2(x,y); float g = getHauteur2(x-eps,y), d = getHauteur2(x+eps,y), b = getHauteur2(x,y+eps), h = getHauteur2(x,y-eps); vec3 vg(-eps, 0, g-ha), vd(eps, 0, d-ha), vb(0, eps, b-ha), vh(0, -eps, h-ha); float distg = length(vg), distd = length(vd), distb = length(vb), disth = length(vh); vec3 v1 = cross(vg,vh), v2 = cross(vh,vd), v3 = cross(vd,vb), v4 = cross(vb,vg); v1 = normalize(v1); v2 = normalize(v2); v3 = normalize(v3); v4 = normalize(v4); vec3 normale = v1*distg*disth + v2*disth*distd + v3*distd*distb + v4*distb*distg; return normalize(normale); }
T uniformRandomInteger() { // setup generator and distribution typedef boost::mt19937 GeneratorType; typedef boost::uniform_int<T> DistributionType; GeneratorType generator(std::time(NULL)); DistributionType distribution(std::numeric_limits<T>::min(), std::numeric_limits<T>::max()); // create variate generator boost::variate_generator<GeneratorType, DistributionType> vg(generator, distribution); // return random number return vg(); }
double Attributes::getDoubleAttr(const string& name) const { this->checkAttributeName(name); GetDoubleAttrVisitor vg(name); this->accept(vg); return vg.getValue(); }
void PropertiesPalette_Group::handleGradientChanged() { if (m_haveDoc) { VGradient vg(transPalWidget->gradEdit->gradient()); m_doc->itemSelection_SetMaskGradient(vg); } }
int main() { MaRC::ViewingGeometry vg(body); return test_initialization(vg) && test_visibility(vg) && test_conversion(vg) ? 0 : -1; }
chromosome( const std::size_t n ) : gene_matrix(n, n) { //is there a c++11 singleton lib that accepts initializer arguments? vg_type& vg = feng::singleton<vg>::instance(); for ( std::size_t i = 1; i != n; ++i ) { //random gene feng::for_each( gene_matrix.upper_diag_begin(i), gene_matrix.upper_diag_end(i), [&](gene_value_type& g) { g = static_cast<gene_value_type>( vg() * std::numeric_limits<gene_value_type>::max() ); } ); //symmetric matrix std::copy( gene_matrix.upper_diag_begin(i), gene_matrix.upper_diag_end(i), gene_matrix.lower_diag_begin(i) ); } }
static lbfgsfloatval_t evaluate( void *instance, const lbfgsfloatval_t *x, lbfgsfloatval_t *g, const int n, const lbfgsfloatval_t step ) { EnergyFunction * efun = static_cast<EnergyFunction*>( instance ); Eigen::VectorXf vx( n ), vg( n ); std::copy( x, x+n, vx.data() ); lbfgsfloatval_t r = efun->gradient( vx, vg ); std::copy( vg.data(), vg.data()+n, g ); return r; }
//should current gene execute mutation? bool should_mutate_current_gene() const { auto& vg = feng::singleton<vg::variate_generator<value_type>>::instance(); if ( vg() >= current_probability ) return false; return true; }
void add_noise( T const level = 0.01 ) { variate_generator<T> vg( T{-1.0}, T{1.0} ); for ( auto& mat : intensity ) std::for_each( mat.begin(), mat.end(), [&vg,level]( value_type& x ) { x += vg() * level; }); }
int EEBearing3d::update() { int rValue = 0; // get current time Domain *theDomain = this->getDomain(); (*t)(0) = theDomain->getCurrentTime(); // get global trial response int ndim = 0, i; Vector dg(12), vg(12), ag(12), dgDelta(12); for (i=0; i<2; i++) { Vector disp = theNodes[i]->getTrialDisp(); Vector vel = theNodes[i]->getTrialVel(); Vector accel = theNodes[i]->getTrialAccel(); Vector dispIncr = theNodes[i]->getIncrDeltaDisp(); dg.Assemble(disp, ndim); vg.Assemble(vel, ndim); ag.Assemble(accel, ndim); dgDelta.Assemble(dispIncr, ndim); ndim += 6; } // transform response from the global to the local system Vector vl(12), al(12), dlDelta(12); dl.addMatrixVector(0.0, Tgl, dg, 1.0); vl.addMatrixVector(0.0, Tgl, vg, 1.0); al.addMatrixVector(0.0, Tgl, ag, 1.0); dlDelta.addMatrixVector(0.0, Tgl, dgDelta, 1.0); // transform response from the local to the basic system Vector dbDelta(6); db->addMatrixVector(0.0, Tlb, dl, 1.0); vb->addMatrixVector(0.0, Tlb, vl, 1.0); ab->addMatrixVector(0.0, Tlb, al, 1.0); dbDelta.addMatrixVector(0.0, Tlb, dlDelta, 1.0); // 1) set axial deformations in basic x-direction theMaterials[0]->setTrialStrain((*db)(0), (*vb)(0)); if (pFrcCtrl == 1) (*qb)(0) = theMaterials[0]->getStress(); // 2) set shear deformations in basic y- and z-direction // do not check time for right now because of transformation constraint // handler calling update at beginning of new step when applying load // if (dbDelta.pNorm(0) > DBL_EPSILON || (*t)(0) > tLast) { if (dbDelta.pNorm(0) > DBL_EPSILON) { // set the trial response at the site if (theSite != 0) { theSite->setTrialResponse(db, vb, ab, qb, t); } else { sData[0] = OF_RemoteTest_setTrialResponse; rValue += theChannel->sendVector(0, 0, *sendData, 0); } } // 3) set rotations about basic x-direction theMaterials[1]->setTrialStrain((*db)(3), (*vb)(3)); // 4) set rotations about basic y-direction theMaterials[2]->setTrialStrain((*db)(4), (*vb)(4)); // 5) set rotations about basic z-direction theMaterials[3]->setTrialStrain((*db)(5), (*vb)(5)); // save the last time tLast = (*t)(0); return rValue; }
TEST(voxel_grid, basicMarkingAndClearing){ int size_x = 50, size_y = 10, size_z = 16; voxel_grid::VoxelGrid vg(size_x, size_y, size_z); //Put a "tabletop" into the scene. A flat rectangle of set voxels at z = 12. int table_z = 12; int table_x_min = 5, table_x_max = 15; int table_y_min = 0, table_y_max = 3; for(int x = table_x_min; x <= table_x_max; x++){ vg.markVoxelLine(x, table_y_min, table_z, x, table_y_max, table_z); } for(int i = table_x_min; i <= table_x_max; ++i){ for(int j = table_y_min; j <= table_y_max; ++j){ //check that each cell of the table is marked ASSERT_EQ(voxel_grid::MARKED, vg.getVoxel(i, j, table_z)); } } int mark_count = 0; unsigned int unknown_count = 0; //go through each cell in the voxel grid and make sure that only 44 are filled in for(unsigned int i = 0; i < vg.sizeX(); ++i){ for(unsigned int j = 0; j < vg.sizeY(); ++j){ for(unsigned int k = 0; k < vg.sizeZ(); ++k){ if(vg.getVoxel(i, j, k) == voxel_grid::MARKED){ mark_count++; } else if(vg.getVoxel(i, j, k) == voxel_grid::UNKNOWN){ unknown_count++; } } } } ASSERT_EQ(mark_count, 44); //the rest of the cells should be unknown ASSERT_EQ(unknown_count, vg.sizeX() * vg.sizeY() * vg.sizeZ() - 44); //now, let's clear one of the rows of the table vg.clearVoxelLine(table_x_min, table_y_min, table_z, table_x_max, table_y_min, table_z); mark_count = 0; unknown_count = 0; int free_count = 0; //go through each cell in the voxel grid and make sure that only 33 are now filled in for(unsigned int i = 0; i < vg.sizeX(); ++i){ for(unsigned int j = 0; j < vg.sizeY(); ++j){ for(unsigned int k = 0; k < vg.sizeZ(); ++k){ if(vg.getVoxel(i, j, k) == voxel_grid::MARKED){ mark_count++; } else if(vg.getVoxel(i, j, k) == voxel_grid::FREE){ free_count++; } else if(vg.getVoxel(i, j, k) == voxel_grid::UNKNOWN){ unknown_count++; } } } } //we've switched 11 cells from marked to free ASSERT_EQ(mark_count, 33); //we've just explicitly seen through 11 cells ASSERT_EQ(free_count, 11); //the rest of the cells should still be unknown ASSERT_EQ(unknown_count, vg.sizeX() * vg.sizeY() * vg.sizeZ() - 44); //now let's put in a vertical column manually to test markVoxel for(unsigned int i = 0; i < vg.sizeZ(); ++i){ vg.markVoxel(0, 0, i); ASSERT_EQ(vg.getVoxel(0, 0, i), voxel_grid::MARKED); } vg.printColumnGrid(); vg.printVoxelGrid(); //now, let's clear that line of voxels and make sure that they clear out OK vg.clearVoxelLine(0, 0, 0, 0, 0, vg.sizeZ() - 1); for(unsigned int i = 0; i < vg.sizeZ(); ++i){ ASSERT_EQ(vg.getVoxel(0, 0, i), voxel_grid::FREE); } mark_count = 0; //Visualize the output /* v->printVoxelGrid(); v->printColumnGrid(); printf("CostMap:\n===========\n"); for(int y = 0; y < size_y; y++){ for(int x = 0; x < size_x; x++){ printf((costMap[y * size_x + x] > 0 ? "#" : " ")); }printf("|\n"); } */ }
void ComputeMeshBoundingSpheresInternal( VertexIndexer indexer, const std::uint32_t* indices, size_t triangle_count, double radius, Callable proc) { for (int tidx = 0; tidx < triangle_count; ++tidx) { std::uint32_t iv1 = indices[3 * tidx]; std::uint32_t iv2 = indices[3 * tidx + 1]; std::uint32_t iv3 = indices[3 * tidx + 2]; Vector3 a(indexer.x(iv1), indexer.y(iv1), indexer.z(iv1)); Vector3 b(indexer.x(iv2), indexer.y(iv2), indexer.z(iv2)); Vector3 c(indexer.x(iv3), indexer.y(iv3), indexer.z(iv3)); // compute the pose of the triangle const double a2 = (b - c).squaredNorm(); const double b2 = (a - c).squaredNorm(); const double c2 = (a - b).squaredNorm(); double bc1 = a2 * (b2 + c2 - a2); double bc2 = b2 * (c2 + a2 - b2); double bc3 = c2 * (a2 + b2 - c2); const double s = bc1 + bc2 + bc3; bc1 /= s; bc2 /= s; bc3 /= s; Vector3 p = bc1 * a + bc2 * b + bc3 * c; if ((p - a).squaredNorm() <= radius * radius && (p - b).squaredNorm() <= radius * radius && (p - c).squaredNorm() <= radius * radius) { proc(p, tidx); continue; } Vector3 z = (c - b).cross(b - a); // normalize or skip z auto len = z.norm(); if (len < 1e-6) { continue; } z /= len; Vector3 x; if (a2 > b2 && a2 > c2) { x = b - c; } else if (b2 > c2) { x = a - c; } else { x = a - b; } // normalize or skip len = x.norm(); if (len < 1e-6) { continue; } x /= len; Vector3 y = z.cross(x); Affine3 T_mesh_triangle; T_mesh_triangle(0, 0) = x[0]; T_mesh_triangle(1, 0) = x[1]; T_mesh_triangle(2, 0) = x[2]; T_mesh_triangle(3, 0) = 0.0; T_mesh_triangle(0, 1) = y[0]; T_mesh_triangle(1, 1) = y[1]; T_mesh_triangle(2, 1) = y[2]; T_mesh_triangle(3, 1) = 0.0; T_mesh_triangle(0, 2) = z[0]; T_mesh_triangle(1, 2) = z[1]; T_mesh_triangle(2, 2) = z[2]; T_mesh_triangle(3, 2) = 0.0; T_mesh_triangle(0, 3) = p[0]; T_mesh_triangle(1, 3) = p[1]; T_mesh_triangle(2, 3) = p[2]; T_mesh_triangle(3, 3) = 1.0; Affine3 T_triangle_mesh = T_mesh_triangle.inverse(); // transform the triangle vertices into the triangle frame Vector3 at = T_triangle_mesh * a; Vector3 bt = T_triangle_mesh * b; Vector3 ct = T_triangle_mesh * c; double minx = std::min(at.x(), std::min(bt.x(), ct.x())); double miny = std::min(at.y(), std::min(bt.y(), ct.y())); double maxx = std::max(at.x(), std::max(bt.x(), ct.x())); double maxy = std::max(at.y(), std::max(bt.y(), ct.y())); // voxelize the triangle PivotVoxelGrid vg( Vector3(minx, miny, 0.0), Vector3(maxx - minx, maxy - miny, 0.0), Vector3(radius, radius, radius), Vector3::Zero()); VoxelizeTriangle(at, bt, ct, vg); // extract filled voxels and append as sphere centers for (int x = 0; x < vg.sizeX(); ++x) { for (int y = 0; y < vg.sizeY(); ++y) { MemoryCoord mc(x, y, 0); if (vg[mc]) { WorldCoord wc = vg.memoryToWorld(mc); proc(T_mesh_triangle * Vector3(wc.x, wc.y, wc.z), tidx); } } } } }
ASMmxBase::VolumeVec ASMmxBase::establishBases(Go::SplineVolume* svol, MixedType type) { VolumeVec result(2); // With mixed methods we need two separate spline spaces if (type == FULL_CONT_RAISE_BASIS1 || type == FULL_CONT_RAISE_BASIS2) { // basis1 should be one degree higher than basis2 and C^p-1 continuous int ndim = svol->dimension(); Go::BsplineBasis b1 = svol->basis(0).extendedBasis(svol->order(0)+1); Go::BsplineBasis b2 = svol->basis(1).extendedBasis(svol->order(1)+1); Go::BsplineBasis b3 = svol->basis(2).extendedBasis(svol->order(2)+1); /* To lower order and regularity this can be used instead std::vector<double>::const_iterator first = ++surf->basis(0).begin(); std::vector<double>::const_iterator last = --surf->basis(0).end(); Go::BsplineBasis b1 = Go::BsplineBasis(surf->order_u()-1,first,last); first = ++surf->basis(1).begin(); last = --surf->basis(1).end(); Go::BsplineBasis b2 = Go::BsplineBasis(surf->order_v()-1,first,last); */ // Compute parameter values of the Greville points size_t i; RealArray ug(b1.numCoefs()), vg(b2.numCoefs()), wg(b3.numCoefs()); for (i = 0; i < ug.size(); i++) ug[i] = b1.grevilleParameter(i); for (i = 0; i < vg.size(); i++) vg[i] = b2.grevilleParameter(i); for (i = 0; i < wg.size(); i++) wg[i] = b3.grevilleParameter(i); if (svol->rational()) { std::vector<double> rCoefs(svol->rcoefs_begin(), svol->rcoefs_end()); // we normally would set coefs as (x*w, y*w, w) // however, gotools use this representation internally already. // instance a Bspline surface in ndim+1 Go::SplineVolume vol2(svol->basis(0), svol->basis(1), svol->basis(2), rCoefs.begin(), ndim+1, false); // interpolate the Bspline surface onto new basis RealArray XYZ((ndim+1)*ug.size()*vg.size()*wg.size()); vol2.gridEvaluator(XYZ,ug,vg,wg); std::unique_ptr<Go::SplineVolume> svol3(Go::VolumeInterpolator::regularInterpolation(b1,b2,b3,ug,vg,wg,XYZ,ndim+1,false,XYZ)); // new rational coefs are (x/w', y/w', w') // apparently gotools will rescale coeffs on surface creation. result[0].reset(new Go::SplineVolume(svol3->basis(0), svol3->basis(1), svol3->basis(2), svol3->coefs_begin(), ndim, true)); } else { RealArray XYZ(ndim*ug.size()*vg.size()*wg.size()); // Evaluate the spline surface at all points svol->gridEvaluator(ug,vg,wg,XYZ); // Project the coordinates onto the new basis (the 2nd XYZ is dummy here) result[0].reset(Go::VolumeInterpolator::regularInterpolation(b1,b2,b3, ug,vg,wg,XYZ,ndim, false,XYZ)); } result[1].reset(new Go::SplineVolume(*svol)); } else if (type == REDUCED_CONT_RAISE_BASIS1 || type == REDUCED_CONT_RAISE_BASIS2) { // Order-elevate basis1 such that it is of one degree higher than basis2 // but only C^p-2 continuous result[0].reset(new Go::SplineVolume(*svol)); result[0]->raiseOrder(1,1,1); result[1].reset(new Go::SplineVolume(*svol)); } else if (ASMmxBase::Type == ASMmxBase::DIV_COMPATIBLE) { result.resize(4); // basis1 should be one degree higher than basis2 and C^p-1 continuous int ndim = svol->dimension(); Go::BsplineBasis a1 = svol->basis(0); Go::BsplineBasis a2 = svol->basis(1); Go::BsplineBasis a3 = svol->basis(2); Go::BsplineBasis b1 = svol->basis(0).extendedBasis(svol->order(0)+1); Go::BsplineBasis b2 = svol->basis(1).extendedBasis(svol->order(1)+1); Go::BsplineBasis b3 = svol->basis(2).extendedBasis(svol->order(2)+1); // Compute parameter values of the Greville points size_t i; RealArray u0(a1.numCoefs()), v0(a2.numCoefs()), w0(a3.numCoefs()); for (i = 0; i < u0.size(); i++) u0[i] = a1.grevilleParameter(i); for (i = 0; i < v0.size(); i++) v0[i] = a2.grevilleParameter(i); for (i = 0; i < w0.size(); i++) w0[i] = a3.grevilleParameter(i); RealArray ug(b1.numCoefs()), vg(b2.numCoefs()), wg(b3.numCoefs()); for (i = 0; i < ug.size(); i++) ug[i] = b1.grevilleParameter(i); for (i = 0; i < vg.size(); i++) vg[i] = b2.grevilleParameter(i); for (i = 0; i < wg.size(); i++) wg[i] = b3.grevilleParameter(i); // Evaluate the spline surface at all points // Project the coordinates onto the new basis (the 2nd XYZ is dummy here) RealArray XYZ0(ndim*ug.size()*v0.size()*w0.size()), XYZ1(ndim*u0.size()*vg.size()*w0.size()), XYZ2(ndim*u0.size()*v0.size()*wg.size()); svol->gridEvaluator(ug,v0,w0,XYZ0); svol->gridEvaluator(u0,vg,w0,XYZ1); svol->gridEvaluator(u0,v0,wg,XYZ2); result[0].reset(Go::VolumeInterpolator::regularInterpolation(b1,a2,a3, ug,v0,w0,XYZ0,ndim, false,XYZ0)); result[1].reset(Go::VolumeInterpolator::regularInterpolation(a1,b2,a3, u0,vg,w0,XYZ1,ndim, false,XYZ1)); result[2].reset(Go::VolumeInterpolator::regularInterpolation(a1,a2,b3, u0,v0,wg,XYZ2,ndim, false,XYZ2)); result[3].reset(new Go::SplineVolume(*svol)); geoBasis = 4; } if (type == FULL_CONT_RAISE_BASIS2 || type == REDUCED_CONT_RAISE_BASIS2) std::swap(result[0], result[1]); return result; }