void Mesh::computeGeodesics(const int vIdx) { int v = (int)vertices.size(); // set random point on mesh to 1 Eigen::VectorXd u(v); u.setZero(); u(vIdx) = 1.0; u = heatSolver.solve(u); // 2. evaluate face gradients Eigen::MatrixXd gradients((int)faces.size(), 3); computeFaceGradients(gradients, u); // 3. solve poisson equation Eigen::VectorXd integratedDivs(v); computeIntegratedDivergence(integratedDivs, gradients); Eigen::VectorXd phis = poissonSolver.solve(integratedDivs); // compute max and min phis double minPhi = INFINITY; double maxPhi = -INFINITY; for (VertexIter v = vertices.begin(); v != vertices.end(); v++) { if (minPhi > phis(v->index)) minPhi = phis(v->index); if (maxPhi < phis(v->index)) maxPhi = phis(v->index); } double range = maxPhi - minPhi; // set phis for (VertexIter v = vertices.begin(); v != vertices.end(); v++) { v->phi = 1 - ((phis(v->index) - minPhi) / range); } }
void lbann_callback_summary::save_histograms(model *m) { for (const auto& layer : m->get_layers()) { const std::string prefix = layer->get_name() + "/"; for (int i = 0; i < layer->get_num_children(); ++i) { AbsDistMatReadProxy<El::Device::CPU> acts(layer->get_activations(i)); m_summarizer->reduce_histogram(prefix + "activations" + std::to_string(i), acts.GetLocked(), m->get_cur_step()); } } for (const auto& w : m->get_weights()) { const std::string prefix = w->get_name() + "/"; AbsDistMatReadProxy<El::Device::CPU> weights(w->get_values()); m_summarizer->reduce_histogram(prefix + "weights", weights.GetLocked(), m->get_cur_step()); optimizer *opt = w->get_optimizer(); if (opt != nullptr) { AbsDistMatReadProxy<El::Device::CPU> gradients(opt->get_gradient()); m_summarizer->reduce_histogram(prefix + "weights_gradient", gradients.GetLocked(), m->get_cur_step()); } } }
void simple_upd(struct CSRMat *Mat, struct RHS *Rhs, double sol[ncell*nvar]) { int icell, ivar, idir; double *phi, *dep, *pgrd, *CelldblPnt, *dd_i; // Solution update ------------------- // (all variables except pressure) for(icell=0;icell<ncell;icell++) { phi = setvar( icell); memcpy( &phi[Uvar], &sol[icell*nvar+Uvar], (nvar-1)*sizeof(double)); } if ( eos == IFROZENP) return; // Not sure if these two instructions are relevant ... boundary(); gradients( nvar, setvar( 0), PGrd, ndep, setdep( 0)); // Velocity coefficients ------------- calcdd( Mat); // Pressure equation resolution ------ simple_peqn(); // SIMPLE BC ------------------------- simple_bc(); // PresVel corrections --------------- for(icell=0;icell<ncell;icell++) { phi = setvar(icell); CelldblPnt = setdblcell(icell); //Pressure update P = P + p' phi[Pvar] += 0.5 * p_sol[icell]; //Velocity update U = u* + u' dd_i = setsol( icell, ndir, dd); pgrd = setsol( icell, ndir, p_grd); for (idir=0;idir<ndir;idir++) phi[Uvar+idir] -= CelldblPnt[CellVol] * pgrd[idir] / dd_i[idir]; #if _DBG == 10 // Adding P' and U' to CEI plot dep = setdep(icell); dep[Ppdep] = p_sol[icell]; for (idir=0;idir<ndir;idir++) dep[Updep+idir] = -CelldblPnt[CellVol] * pgrd[idir] / dd_i[idir]; #endif } // Memory release -------------------- free(p_sol); free(p_grd); free(dd); }
void simple_bc( void) { int ighst, *GhsintPnt, icell; for (ighst=0;ighst<nghst;ighst++) { GhsintPnt = setintghst(ighst); icell = GhsintPnt[GhstFidx]; p_sol[ghst2gbl(ighst)] = p_sol[icell] * ghst2fld( ighst, Pvar); } // p' gradients p_grd = (double *) calloc( ntot*ndir, sizeof(double)); gradients( 1, p_sol, 0, ndir, p_grd); }
QList<Dataset*> Bingham::bingham2Tensor( DatasetBingham* ds ) { qDebug() << "start bingham to dwi"; std::vector< std::vector<float> >* data = ds->getData(); std::vector<ColumnVector> signals1; std::vector<ColumnVector> signals2; std::vector<ColumnVector> signals3; signals1.resize( data->size() ); signals2.resize( data->size() ); signals3.resize( data->size() ); float kernel[4] = { 258.747, -82.5396, 18.716, -2.35225 }; int gradientsSize = __GRADIENTS_60_SIZE; int tessGradientsSize = tess::__TESS_THREE_V_SIZE; ColumnVector v( gradientsSize ); v = 0.0; for ( unsigned int i = 0; i < data->size(); ++i ) { signals1[i] = v; signals2[i] = v; signals3[i] = v; } std::vector< std::vector<ColumnVector>* > sigs; sigs.push_back( &signals1 ); sigs.push_back( &signals2 ); sigs.push_back( &signals3 ); ColumnVector m1( 3 ); ColumnVector m2( 3 ); float k1; float k2; float f0; //**************************************************************************** std::vector<QVector3D>bvecs; std::vector<float>bvals; const int max_ord = 10; //((-3+static_cast<int>(sqrt(8*((*sh.get(0,0,0)).size())+1)))/2); Matrix gradients( gradientsSize, 3 ); Matrix tessGradients( tessGradientsSize, 3 ); for ( int i = 0; i < gradientsSize; ++i ) { gradients( i + 1, 1 ) = __GRADIENTS_60[ i * 3 ]; gradients( i + 1, 2 ) = __GRADIENTS_60[ i * 3 + 1 ]; gradients( i + 1, 3 ) = __GRADIENTS_60[ i * 3 + 2 ]; QVector3D v( __GRADIENTS_60[ i * 3 ], __GRADIENTS_60[ i * 3+1 ], __GRADIENTS_60[ i * 3 + 2] ); bvecs.push_back( v ); bvals.push_back( 1000 ); } for ( int i = 0; i < tessGradientsSize; ++i ) { tessGradients( i + 1, 1 ) = tess::__TESS_THREE_V[ i * 3 ]; tessGradients( i + 1, 2 ) = tess::__TESS_THREE_V[ i * 3 + 1 ]; tessGradients( i + 1, 3 ) = tess::__TESS_THREE_V[ i * 3 + 2 ]; } Matrix base( FMath::sh_base( gradients, max_ord ) ); Matrix inv_base( FMath::pseudoInverse( FMath::sh_base( tessGradients, max_ord ) ) ); for ( unsigned int i = 0; i < data->size(); ++i ) // for all voxels { for ( int k = 0; k < 3; ++k ) // for all 3 bingham peaks { ColumnVector sig( gradientsSize ); sig = 0.0; f0 = data->at( i )[ k * 9 + 8 ]; if ( f0 > 0.0 ) { m1( 1 ) = data->at( i )[ k * 9 ]; m1( 2 ) = data->at( i )[ k * 9 + 1 ]; m1( 3 ) = data->at( i )[ k * 9 + 2 ]; m2( 1 ) = data->at( i )[ k * 9 + 3 ]; m2( 2 ) = data->at( i )[ k * 9 + 4 ]; m2( 3 ) = data->at( i )[ k * 9 + 5 ]; k1 = data->at( i )[ k * 9 + 6 ]; k2 = data->at( i )[ k * 9 + 7 ]; ColumnVector tmp( tessGradientsSize ); tmp = 0.0; for ( int l = 0; l < tessGradientsSize; ++l ) { ColumnVector cur_dir(3); cur_dir(1) = tess::__TESS_THREE_V[l*3]; cur_dir(2) = tess::__TESS_THREE_V[l*3+1]; cur_dir(3) = tess::__TESS_THREE_V[l*3+2]; tmp( l + 1 ) = f0 * exp( - ( k1 * FMath::iprod(cur_dir,m1) * FMath::iprod(cur_dir,m1) + k2 * FMath::iprod(cur_dir,m2) * FMath::iprod(cur_dir,m2) ) ); } // transform to spherical harmonic space ColumnVector sh_signal = inv_base * tmp; //do the convolution for( int order(0), j(0), m(0); order <= max_ord; order+=2, ++m ) { for( int degree( -order ); degree <= order; ++degree, ++j ) { sh_signal(j+1) = sh_signal( j+1 ) * ( kernel[m] / boost::math::spherical_harmonic_r( order, 0, 0, 0 ) ); } } // back transform to signal space ColumnVector tmp1 = base * sh_signal; for ( int ii = 0; ii < tmp.Nrows(); ++ii ) { if ( tmp( ii + 1 ) > 1.0 ) { tmp( ii + 1 ) = 1.0; } } sigs[k]->at( i ) = tmp1; } } } std::vector<float> b0Data( data->size(), 220 ); QList<Dataset*> dsout; for ( int i = 0; i < 3; ++i ) { // Writer writer( ds, QFileInfo() ); // DatasetDWI* out = new DatasetDWI( QDir( "dwifrombingham" ), *(sigs[i]), b0Data, bvals, bvecs, writer.createHeader( gradientsSize + 1 ) ); // // dsout.push_back( out ); } qDebug() << "finished bingham to dwi"; return dsout; }
void OcclusionBuffer::DrawTriangle2D(const Vector3* vertices) { int top, middle, bottom; bool middleIsRight; // Sort vertices in Y-direction if (vertices[0].y_ < vertices[1].y_) { if (vertices[2].y_ < vertices[0].y_) { top = 2; middle = 0; bottom = 1; middleIsRight = true; } else { top = 0; if (vertices[1].y_ < vertices[2].y_) { middle = 1; bottom = 2; middleIsRight = true; } else { middle = 2; bottom = 1; middleIsRight = false; } } } else { if (vertices[2].y_ < vertices[1].y_) { top = 2; middle = 1; bottom = 0; middleIsRight = false; } else { top = 1; if (vertices[0].y_ < vertices[2].y_) { middle = 0; bottom = 2; middleIsRight = false; } else { middle = 2; bottom = 0; middleIsRight = true; } } } int topY = (int)vertices[top].y_; int middleY = (int)vertices[middle].y_; int bottomY = (int)vertices[bottom].y_; // Check for degenerate triangle if (topY == bottomY) return; Gradients gradients(vertices); Edge topToMiddle(gradients, vertices[top], vertices[middle], topY); Edge topToBottom(gradients, vertices[top], vertices[bottom], topY); Edge middleToBottom(gradients, vertices[middle], vertices[bottom], middleY); // The triangle is clockwise, so if bottom > middle then middle is right if (middleIsRight) { // Top half int* row = buffer_ + topY * width_; int* endRow = buffer_ + middleY * width_; while (row < endRow) { int invZ = topToBottom.invZ_; int* dest = row + (topToBottom.x_ >> 16); int* end = row + (topToMiddle.x_ >> 16); while (dest < end) { if (invZ < *dest) *dest = invZ; invZ += gradients.dInvZdXInt_; ++dest; } topToBottom.x_ += topToBottom.xStep_; topToBottom.invZ_ += topToBottom.invZStep_; topToMiddle.x_ += topToMiddle.xStep_; row += width_; } // Bottom half row = buffer_ + middleY * width_; endRow = buffer_ + bottomY * width_; while (row < endRow) { int invZ = topToBottom.invZ_; int* dest = row + (topToBottom.x_ >> 16); int* end = row + (middleToBottom.x_ >> 16); while (dest < end) { if (invZ < *dest) *dest = invZ; invZ += gradients.dInvZdXInt_; ++dest; } topToBottom.x_ += topToBottom.xStep_; topToBottom.invZ_ += topToBottom.invZStep_; middleToBottom.x_ += middleToBottom.xStep_; row += width_; } } else {
FillStyles(map< string, string>& options) { FSMovie movie; int width = 3600; int height = 2700; string imageFile = stringOption(options, "image"); /* * A row of five rectangles will be displayed - one for each fill style. * The additional space is ued to provide padding between the rectangles * and to the edge of the screen. */ movie.setFrameSize(FSBounds(0, 0, (width+800)*5 , height+800)); movie.setFrameRate(1.0f); movie.add(new FSSetBackgroundColor(FSColorTable::lightblue())); /* * Define the rectangle that is filled with the different fill styles. */ FSBounds bounds(-width/2, -height/2, width/2, height/2); FSShape rectangle; rectangle.add(new FSShapeStyle(1, 1, 0, -width/2, -height/2)); rectangle.add(new FSLine(width, 0)); rectangle.add(new FSLine(0, height)); rectangle.add(new FSLine(-width, 0)); rectangle.add(new FSLine(0, -height)); /* * The following variables are reused for each fill style to simplify the code. * In Transform an object assumes ownership for any added to it, so the shape * and style arrays can be safely reused in the FSDefineShape constructor. A * shallow copy of the object is made. The FSDefineShape object assumes * ownership of the objects they contain and no explicit memory management * is required - the objects will be deleted when the shape is deleted. */ int identifier = 0; FSShape shape; FSVector<FSLineStyle*> lineStyles(1); FSVector<FSFillStyle*> fillStyles(1); /************************************************* * Create a rectangle filled with a solid colour. *************************************************/ shape = rectangle; identifier = movie.newIdentifier(); lineStyles[0] = new FSSolidLine(20, FSColorTable::black()); fillStyles[0] = new FSSolidFill(FSColorTable::red()); movie.add(new FSDefineShape(identifier, bounds, fillStyles, lineStyles, shape)); movie.add(new FSPlaceObject(identifier, 1, 2000, 1600)); /****************************************** * Create a rectangle tiled with an image. ******************************************/ shape = rectangle; identifier = movie.newIdentifier(); lineStyles[0] = new FSSolidLine(20, FSColorTable::black()); try { size_t size = 0; byte* bytes = dataFromFile(imageFile.c_str(), size); FSDefineJPEGImage* image = new FSDefineJPEGImage(movie.newIdentifier(), bytes, size); /* * Scale the loaded image to half its original size so it will tile four times inside the * rectangle. When an image is loaded its width and height default to twips rather than * pixels. An image 300 x 200 pixels will be displayed as 300 x 200 twips (15 x 10 pixels). * Scaling the image by 20 (20 twips = 1 pixel) would restore it to its original size. Here * we only scale the image to half its original size so variable sized images passed on the * command line when the example is run are more easily seen. * * The coordinate transform relocates the top left corner of the image to the top left * corner of the rectangle in which it is displayed. */ FSCoordTransform transform = FSCoordTransform(-width/2, -height/2, 10.0, 10.0); fillStyles[0] = new FSBitmapFill(FSFillStyle::TiledBitmap, image->getIdentifier(), transform); movie.add(image); movie.add(new FSJPEGEncodingTable()); // The image will still be displayed with an empty table movie.add(new FSDefineShape(identifier, bounds, fillStyles, lineStyles, shape)); movie.add(new FSPlaceObject(identifier, 2, 6000, 1600)); } catch (FSFileOpenException e) { cerr << e.what(); } catch (FSAccessException e) { cerr << e.what(); } /************************************************** * Create a rectangle filled with a clipped image. **************************************************/ shape = rectangle; identifier = movie.newIdentifier(); lineStyles[0] = new FSSolidLine(20, FSColorTable::black()); try { size_t size = 0; byte* bytes = dataFromFile(imageFile.c_str(), size); FSDefineJPEGImage* image = new FSDefineJPEGImage(movie.newIdentifier(), bytes, size); /* * Scale the loaded image to half its original size so variable sized images are * more easily seen. The coordinate transformation, taking into account the scaling * factor will display the image centered in the middle of the rectangle. */ FSCoordTransform transform = FSCoordTransform(-(image->getWidth()*10)/2, -(image->getHeight()*10)/2, 10.0, 10.0); fillStyles[0] = new FSBitmapFill(FSFillStyle::ClippedBitmap, image->getIdentifier(), transform); movie.add(image); movie.add(new FSJPEGEncodingTable()); // The image will still be displayed with an empty table movie.add(new FSDefineShape(identifier, bounds, fillStyles, lineStyles, shape)); movie.add(new FSPlaceObject(identifier, 3, 10000, 1600)); } catch (FSFileOpenException e) { cerr << e.what(); } catch (FSAccessException e) { cerr << e.what(); } /********************************************************* * Create a rectangle filled with linear gradient colour. *********************************************************/ shape = rectangle; identifier = movie.newIdentifier(); /* * The gradient square must be mapped to the rectangle being filled. The square * measures 32768 x 32768 twips. The coordinates range from -16384, -16384 at the * bottom left corner to (16384, 16384) at the top right corner. Mapping takes place * in three steps: * * 1. The coordinate system of the gradient square is translated to match the coordinate * system of the shape in which the gradient is displayed. In this example, both the * gradient square and the rectangle in which it will be displayed are both centred at * (0,0) - see the FSBounds object created above. For this example the full range of the * gradient will be displayed so no translation is required. The second example that * displays a radial gradient illustrates how the translation can be calculated to change * the portion of the gradient being displayed. * * 2. The gradient square is scaled so that the full range of the gradient will be * displayed inside the rectangle. If a smaller scaling factor is set then the * portion of the gradient being displayed will change. The second example shows * how the scaling factor affects the gradient. * * 3. The gradient can be rotated to change the direction of the colour change. If a * rotation is not applied then the gradient changes in the positive x axis - front left * to right on the Flash Player's screen. */ // To display the full gradient, calculate the ratio of the shape's width to the width // of the gradient square. The gradient will be rotated so the width is used rather than // calculating the length of the diagonal. float scale = bounds.getWidth() / 32768.0f; // Since the centre of the rectangle and the gradient square are both at (0,0) no // additional translation is required. int translateX = 0; int translateY = 0; // The order of composition is important. If the scale transform was the first argument // then the translation coordinates would also be scaled. // The scaling is performed in the x and y direction as the gradient is rotated in the // the following step. FSCoordTransform transform = FSCoordTransform(translateX, translateY, scale, scale); // Apply a rotation so the gradient changes across the diagonal of the rectangle. transform.rotate(45); /* * The array of FSGradient objects defines how the colour changes across the gradient * square. The ratio defines the proportion across the square: 0 is the left side and * 255 is the right side. */ FSVector<FSGradient> gradients(2); gradients[0] = FSGradient(0, FSColorTable::white()); gradients[1] = FSGradient(255, FSColorTable::black()); lineStyles[0] = new FSSolidLine(20, FSColorTable::black()); fillStyles[0] = new FSGradientFill(FSFillStyle::LinearGradient, transform, gradients); movie.add(new FSDefineShape(identifier, bounds, fillStyles, lineStyles, shape)); movie.add(new FSPlaceObject(identifier, 4, 14000, 1600)); /********************************************************* * Create a rectangle filled with radial gradient colour. *********************************************************/ shape = rectangle; identifier = movie.newIdentifier(); // Resize the gradients array so the gradient will contain 4 colours. gradients.resize(4); /* The gradient will be centred in the bottom left corner of the rectangle * since the full spectrum of the radial gradient will be displayed the * gradient square need only be scaled by half the amount compared to the * linear gradient above. */ float scaleX = (bounds.getWidth() / 32768.0f) * 2.0f; float scaleY = (bounds.getHeight() / 32768.0f) * 2.0f; /* Here the translation is calculated so the centre is expressed as a percentage * of the width of the rectangle. The general form of the calculation is: * * translateX = bounds.getMinX() + bounds.getWidth() * 0.0; * translateY = bounds.getMinY() + bounds.getHeight() * 0.25; */ translateX = bounds.getMinX(); translateY = bounds.getMinY(); transform = FSCoordTransform(translateX, translateY, scaleX, scaleY); gradients[0] = FSGradient(15, FSColorTable::red()); gradients[1] = FSGradient(63, FSColorTable::orange()); gradients[2] = FSGradient(127, FSColorTable::yellow()); gradients[3] = FSGradient(255, FSColorTable::green()); lineStyles[0] = new FSSolidLine(1, FSColorTable::black()); fillStyles[0] = new FSGradientFill(FSFillStyle::RadialGradient, transform, gradients); movie.add(new FSDefineShape(identifier, bounds, fillStyles, lineStyles, shape)); movie.add(new FSPlaceObject(identifier, 5, 18000, 1600)); movie.add(new FSShowFrame()); saveMovie(movie, stringOption(options, "resultDir"), "FillStyles.swf"); }