void SVGStyle::parseNode(xmlNodePtr node, map<string, SVGGradient*> &gradients) { AttributeParser parser; parser.parseNode(node); const map<string, vector<string> > &attributes = parser.getAttributes(); for(map<string, vector<string> >::const_iterator iter = attributes.begin(); iter != attributes.end(); iter++) { const string &attribute = (*iter).first; const vector<string> &values = (*iter).second; for (vector<string>::const_iterator iterValue = values.begin (); iterValue != values.end (); iterValue ++) { const string &valueStr = (*iterValue); const char *value = valueStr.c_str (); bool fParsed = true; if(attribute == "stroke") { SVGGradient *gradient = getGradient(valueStr, gradients); if(gradient) { strokeGradient = gradient; _hasStrokeGradient = true; _hasStroke = true; fParsed = true; } else { fParsed = setStrokeColor(value); } } else if(attribute == "stroke-width") { setStrokeWidth(atof(value)); } else if(attribute == "stroke-opacity") { setStrokeAlpha(atof(value)); } else if(attribute == "stroke-linecap") { fParsed = setLineCap(value); } else if(attribute == "stroke-linejoin") { fParsed = setLineJoin(value); } else if(attribute == "stroke-miterlimit") { setMiterLimit(atof(value)); } else if(attribute == "fill") { SVGGradient *gradient = getGradient(valueStr, gradients); if(gradient) { fillGradient = gradient; _hasFillGradient = true; _hasFill = true; fParsed = true; } else { fParsed = setFillColor(value); } } else if(attribute == "fill-opacity") { setFillAlpha(atof(value)); } else if(attribute == "opacity") { setOpacity(atof(value)); } if (fParsed) { break; } } } }
QBrush* getBrush(const CLGraphicalPrimitive2D *item, const CLGroup *group, const CLRenderResolver* resolver, const CLBoundingBox *pBB) { QColor color; if (item != NULL && item->isSetFill()) { const CLGradientBase* base = resolver->getGradientBase(item->getFillColor()); if (base != NULL) { return new QBrush(*getGradient(base, pBB, resolver)); } return new QBrush(getColor(item->getFillColor(), resolver)); } else if (group != NULL && group->isSetFill()) { const CLGradientBase* base = resolver->getGradientBase(group->getFillColor()); if (base != NULL) { return new QBrush(*getGradient(base, pBB, resolver)); } return new QBrush(getColor(group->getFillColor(), resolver)); } return new QBrush(); }
void calculateSum(std::vector<Mat>& x_contrast, std::vector<Mat>& y_contrast, Mat& sum) { sum = Mat::zeros(x_contrast[x_contrast.size() - 1].size(), CV_32F); for(int i = x_contrast.size() - 1; i >= 0; i--) { Mat grad_x, grad_y; getGradient(x_contrast[i], grad_x, 1); getGradient(y_contrast[i], grad_y, 1); resize(sum, sum, x_contrast[i].size()); sum += grad_x + grad_y.t(); } }
void getContrast(Mat src, std::vector<Mat>& x_contrast, std::vector<Mat>& y_contrast) { int levels = static_cast<int>(logf(static_cast<float>(min(src.rows, src.cols))) / logf(2.0f)); x_contrast.resize(levels); y_contrast.resize(levels); Mat layer; src.copyTo(layer); for(int i = 0; i < levels; i++) { getGradient(layer, x_contrast[i], 0); getGradient(layer.t(), y_contrast[i], 0); resize(layer, layer, Size(layer.cols / 2, layer.rows / 2)); } }
Vector4 GridSource::getValueAndGradient(const Vector3 &position) const { Vector3 scaledPosition(position.x * mPosXScale, position.y * mPosYScale, position.z * mPosZScale); Vector3 gradient; if (mTrilinearGradient) { size_t x0 = (size_t)scaledPosition.x; size_t x1 = (size_t)ceil(scaledPosition.x); size_t y0 = (size_t)scaledPosition.y; size_t y1 = (size_t)ceil(scaledPosition.y); size_t z0 = (size_t)scaledPosition.z; size_t z1 = (size_t)ceil(scaledPosition.z); Real dX = scaledPosition.x - (Real)x0; Real dY = scaledPosition.y - (Real)y0; Real dZ = scaledPosition.z - (Real)z0; Vector3 f000 = getGradient(x0, y0, z0); Vector3 f100 = getGradient(x1, y0, z0); Vector3 f010 = getGradient(x0, y1, z0); Vector3 f001 = getGradient(x0, y0, z1); Vector3 f101 = getGradient(x1, y0, z1); Vector3 f011 = getGradient(x0, y1, z1); Vector3 f110 = getGradient(x1, y1, z0); Vector3 f111 = getGradient(x1, y1, z1); Real oneMinX = (Real)1.0 - dX; Real oneMinY = (Real)1.0 - dY; Real oneMinZ = (Real)1.0 - dZ; Real oneMinXoneMinY = oneMinX * oneMinY; Real dXOneMinY = dX * oneMinY; gradient = oneMinZ * (f000 * oneMinXoneMinY + f100 * dXOneMinY + f010 * oneMinX * dY) + dZ * (f001 * oneMinXoneMinY + f101 * dXOneMinY + f011 * oneMinX * dY) + dX * dY * (f110 * oneMinZ + f111 * dZ); gradient *= (Real)-1.0; } else { gradient = getGradient((size_t)(scaledPosition.x + (Real)0.5), (size_t)(scaledPosition.y + (Real)0.5), (size_t)(scaledPosition.z + (Real)0.5)); gradient *= (Real)-1.0; } return Vector4(gradient.x, gradient.y, gradient.z, getValue(position)); }
pair<uint, uint> match_polish_rot(Image& pattern, Image& scene, double (*f)(Image&, uint, uint, Image&, uint)){ polish(pattern, 0, 0, pattern.getWidth(), pattern); map<uint, vector< pix > > comps; for(uint i=0;i<360;i+=90){ vector< pix > vec = getComparator( pattern.getHeight(), i ); sort( vec.begin(), vec.end(), comp ); comps.insert(make_pair(i,vec)); } const uint dimension = pattern.getHeight(); const uint G = (uint)getGradient( pattern, 0, 0, pattern.getHeight() ); vector< pix > patorder = comps[G]; pair<uint, uint> minPoint(-1,-1); double min = INF; double ang = 0; vector< pix > sceorder; for(uint i = 0; i < scene.getHeight(); i++){ for(uint j = 0; j < scene.getWidth(); j++){ if( scene.valid( i + dimension - 1, j + dimension - 1 ) ){ uint sceg = (uint)getGradient( scene, i, j, dimension ); sceorder = comps[sceg]; double m = f(scene, i, j, pattern, dimension); if( m <= min ){ min = m; minPoint = make_pair( i, j ); ang = sceg; } } } } cout<<"G = "<<G<<endl; cout<<"distance = "<<min<<endl; cout<<"angle = "<<ang<<endl; return minPoint; }
float Warp::noise(float x, float y) const { float x0 = floorf(x); float x1 = ceilf(x); float y0 = floorf(y); float y1 = ceilf(y); Vec2 g_rand_x0_y1 = getGradient(x0, y1); Vec2 g_rand_x1_y1 = getGradient(x1, y1); Vec2 g_rand_x1_y0 = getGradient(x1, y0); Vec2 g_rand_x0_y0 = getGradient(x0, y0); float s = (g_rand_x0_y0.x() * (x-x0)) + (g_rand_x0_y0.y() * (y-y0)); float t = (g_rand_x1_y0.x() * (x-x1)) + (g_rand_x1_y0.y() * (y-y0)); float u = (g_rand_x0_y1.x() * (x-x0)) + (g_rand_x0_y1.y() * (y-y1)); float v = (g_rand_x1_y1.x() * (x-x1)) + (g_rand_x1_y1.y() * (y-y1)); float weight_x = ease(x-x0); float a = s + (weight_x * (t - s)); float b = u + (weight_x * (v - u)); float weight_y = ease(y-y0); return a + weight_y * (b - a); };
/* Gradient Descent Method, assuming gamma is given?*/ void gradientDescent(struct Matrix* a, struct Vector* b, struct Vector* initialVal, struct Vector** x, double gamma, int size, int steps){ copy_Vector(initialVal,x[0]); int i=0; struct Vector* gradient=new_Vector(size); for(i=1;i<steps;i++){ getGradient(a,b,x[i-1],gradient,size); scale_Vector(-gamma,gradient,gradient); add_Vectors(x[i-1],gradient,x[i]); } delete_Vector(gradient); }
void CContinuousActionGradientPolicy::getGradientPre(ColumnVector *input, ColumnVector *outputErrors, CFeatureList *gradientFeatures) { CFeatureList *featureList = new CFeatureList(); CState *state = new CState(modelState); for (int i = 0; i < getNumInputs(); i ++) { state->setContinuousState(i, input->element(i)); } for (int i = 0; i < getNumOutputs(); i++) { getGradient(state, i, featureList); gradientFeatures->add(featureList, outputErrors->element(i)); } delete featureList; delete state; }
// inverse x-axis Line *makeLine(vector<Point> &segment) { int i; Line *line = (Line *) malloc (sizeof(Line)); memset(line, 0, sizeof(Line)); line->length = segment.size(); line->pixels = (GPixel *) malloc (sizeof(GPixel) * line->length); memset(line->pixels, 0, sizeof(GPixel) * line->length); for (i=0; i<line->length; i++) { line->pixels[i].r = segment[i].y; line->pixels[i].c = segment[i].x; } for (i=0; i<line->length; i++) { line->pixels[i].gradient = getGradient(*line, i); } return line; }
/*! This method calls getGradient(ok, QLinearGradient(), parent, caption). */ QGradient QtGradientDialog::getGradient(bool *ok, QWidget *parent, const QString &caption) { return getGradient(ok, QLinearGradient(), parent, caption); }
vector<double> FeatureExtractor::computeFeaturesPerWindow(int winNb) { unsigned char *input = (unsigned char*)(im_bw.data); int imgStep = im_bw.step; int j = winNb; //the column for which the features are computed int rows = im_bw.rows; double f1 = 0.0, f2 = 0.0, f3 = 0.0; vector<double> featuresVec; //compute the first 3 features: // f1: number of black pixels (= mean gray value) // f2: center of gravity // f3: second order moment of the window for(int i = 0; i < rows; ++i) { int pixelVal = (int)input[i*imgStep + j]/255; int blackPixel = 1 - pixelVal; //revert the black pixels -- to be 1 f1 += blackPixel; //count black pixels f2 += i*blackPixel; f3 += i*i*blackPixel; } f1 /= rows; f2 /= rows; f3 = f3/(rows*rows); //DEBUG featuresVec.push_back(f1); featuresVec.push_back(f2); // return featuresVec; //to return just 2 features featuresVec.push_back(f3); int uppermost = 0, lowermost = rows - 1; //f4: compute position of the uppermost black pixel for(int i = 0; i < rows; ++i) if((int)input[i*imgStep + j] == 0) { uppermost = i; break; } //f5: compute position of the lowermost black pixel for(int i = rows-1; i >= 0; i--) if((int)input[i*imgStep + j] == 0) { lowermost = i; break; } //f6: the rate of change of the uppermost position //f7: the rate of change of the lowermost position double f6 = getGradient(uppermost, j); double f7 = getGradient(lowermost, j); featuresVec.push_back(uppermost); featuresVec.push_back(lowermost); featuresVec.push_back(f6); featuresVec.push_back(f7); //f8: number of black-white transitions between the uppermost and the lowermost //f9: proportion of black pixels in this region int f8 = 0, prevPixelVal = 0; //the current pixel is black double f9 = 1.0; // the pixel corresponding to the uppermost int interval = lowermost - uppermost + 1; for(int i = uppermost + 1; i <= lowermost; ++i) { int pixelVal = (int)input[i*imgStep + j]/255; if(pixelVal != prevPixelVal) f8++; prevPixelVal = pixelVal; if(pixelVal == 0) f9++; } if(interval != 0) f9 /= interval; featuresVec.push_back(f8); featuresVec.push_back(f9); return featuresVec; }
Vector getGradient(VectorToRFunction &f, const Vector &x) { return getGradient(f,x,f(x)); }
void operator()(const Value& val, Expr&& expr) { getGradient(val).noalias() += expr; }
MapGenerator::TerrainBlock MapGenerator::getBlock(std::int64_t row, std::int64_t col) const { MapGenerator::TerrainBlock terrainBlock; // We need size^2 gradient vectors. constexpr std::size_t size = blockSize / gridSize + 1; // Assign a random gradient vector of unit length to each grid node. TODO: we really // only need to store (size + 2) vectors at a time. std::array<std::array<std::array<float, 2>, size>, size> gradients; { // Use the gradient vectors of adjacent blocks for two edges. Otherwise we would // get visible transitions at block edges, since adjacent tiles would use different // gradient vectors. std::size_t i = size - 1; std::size_t j = 0; // Seed of the adjacent block to the bottom. seedRNG(row + 1, col); // Get the random gradients used for the top row of the block to the bottom. Use // them for this bottom row. for (; j < size - 1; ++j) { gradients[i][j] = getGradient(); } // Seed of the adjacent block to the bottom-right. seedRNG(row + 1, col + 1); assert(j == size - 1); gradients[i][j] = getGradient(); // Seed of the adjacent block to the right. seedRNG(row, col + 1); i = 0; assert(j == size - 1); gradients[i][j] = getGradient(); // Throw the remaining (size - 2) pairs of random numbers for the top row away. rNGen.discard(2 * size - 4); // The next random numbers are those the block to the right uses for its leftmost // column. Use them for the rightmost column. for (++i; i < size - 1; ++i) { gradients[i][j] = getGradient(); } } // Finally, use the seed of this block. seedRNG(row, col); // The gradient vectors for the top row and leftmost column are the ones adjacent // blocks also use. They are computed first. Otherwise we would have to waste more // time generating and throwing away random numbers in the code generating gradients of // other blocks above. for (std::size_t j = 0; j < size - 1; ++j) { gradients[0][j] = getGradient(); } for (std::size_t j = 0; j < size - 1; ++j) { for (std::size_t i = 1; i < size - 1; ++i) { gradients[i][j] = getGradient(); } } #ifdef DEBUG // Assert all the gradients are unit vectors. {{{1 { constexpr float epsilon = 0.01f; for (std::size_t i = 0; i < size; ++i) { for (std::size_t j = 0; j < size; ++j) { auto& gradient = gradients[i][j]; assert(dotProduct(gradient, gradient) <= 1.f + epsilon); } } } #endif // }}}1 for (std::size_t i = 0; i < blockSize; ++i) { for (std::size_t j = 0; j < blockSize; ++j) { // Convert the indices to floats in the gradient grid. std::array<float, 2> point{static_cast<float>(j) / gridSize, static_cast<float>(i) / gridSize}; // Determine into which grid cell (i, j) falls; store the cell's top-left corner // which is also the index of that point's gradient vector. TODO: we could // iterate over grid cells and avoid repeating this computation. std::array<std::size_t, 2> topLeft{j / gridSize, i / gridSize}; #ifdef DEBUG // Assert use of topLeft to index gradients is correct. {{{1 assert(topLeft[0] + 1 < gradients.size()); assert(topLeft[1] + 1 < gradients[0].size()); #endif // }}}1 // For each corner of that cell, determine the distance vector from the corner to // the point. std::array<std::array<float, 2>, 4> distance; distance[0] = {point[0] - topLeft[0], point[1] - topLeft[1]}; distance[1] = {distance[0][0] - 1.f, distance[0][1]}; distance[2] = {distance[0][0], distance[0][1] - 1.f}; distance[3] = {distance[1][0], distance[2][1]}; #ifdef DEBUG // Validate value of distance[0]. {{{1 assert(0.f <= distance[0][0] && distance[0][0] <= 1.f); assert(0.f <= distance[0][1] && distance[0][1] <= 1.f); #endif // }}}1 // For each of the 4 distance vectors, compute the dot product between it and the // corner's gradient vector. std::array<float, 4> dots{ dotProduct(distance[0], gradients[topLeft[1]][topLeft[0]]), dotProduct(distance[1], gradients[topLeft[1]][topLeft[0] + 1]), dotProduct(distance[2], gradients[topLeft[1] + 1][topLeft[0]]), dotProduct(distance[3], gradients[topLeft[1] + 1][topLeft[0] + 1])}; #ifdef DEBUG // ... {{{1 { constexpr float epsilon = 0.01f; constexpr float maxDist = 1.414213562373 + epsilon; for (std::size_t n = 0; n < 4; ++n) { assert(dots[n] <= maxDist); } } #endif // }}}1 // Interpolate between the 4 dot products. float xWeight = point[0] - topLeft[0]; float yWeight = point[1] - topLeft[1]; float topXAverage = lerp(dots[0], dots[1], xWeight); float bottomXAverage = lerp(dots[2], dots[3], xWeight); float value = lerp(topXAverage, bottomXAverage, yWeight); // constexpr float maxVal = std::sqrt(2) / 2; // assert(-maxVal <= value && value <= maxVal); // value += maxVal; // Transform value into the range [0, 2 * maxValue]. // value /= 2 * maxVal; // Transform it into the range [0, 1]. // I think my implementation of Perlin noise can result in values in the range // [-maxVal, maxVal]. However, the vast majority of values are much closer to // zero, so the commented out code above doesn't work very well. // This should kind of move most values into the range [-2.5, 2.5]. value *= 5.f; // Now most should be in the range [0, 5]. value += 2.5f; // Treat everything negative as 0 and everything bigger than or equal to 6 as 5. if (value < 0.f) value = 0.f; if (value >= 6.f) value = 5.f; // Now we only have values in [0, 6). They can be converted to TileType values // by truncating. terrainBlock[i][j] = static_cast<TileType>(value); } } return terrainBlock; }
void StereoVar::VariationalSolver(Mat &I1, Mat &I2, Mat &I2x, Mat &u, int level) { register int n, x, y; float gl = 1, gr = 1, gu = 1, gd = 1, gc = 4; Mat g_c, g_p; Mat U; u.copyTo(U); int N = nIt; float l = lambda; float Fi = fi; if (flags & USE_SMART_ID) { double scale = pow(pyrScale, (double) level) * (1 + pyrScale); N = (int) (N / scale); } double scale = pow(pyrScale, (double) level); Fi /= (float) scale; l *= (float) scale; int width = u.cols - 1; int height = u.rows - 1; for (n = 0; n < N; n++) { if (penalization != PENALIZATION_TICHONOV) { Mat gradient = getGradient(U); switch (penalization) { case PENALIZATION_CHARBONNIER: g_c = getG_c(gradient, l); break; case PENALIZATION_PERONA_MALIK: g_p = getG_p(gradient, l); break; } gradient.release(); } for (y = 1 ; y < height; y++) { float *pU = U.ptr<float>(y); float *pUu = U.ptr<float>(y + 1); float *pUd = U.ptr<float>(y - 1); float *pu = u.ptr<float>(y); float *pI1 = I1.ptr<float>(y); float *pI2 = I2.ptr<float>(y); float *pI2x = I2x.ptr<float>(y); float *pG_c = NULL, *pG_cu = NULL, *pG_cd = NULL; float *pG_p = NULL, *pG_pu = NULL, *pG_pd = NULL; switch (penalization) { case PENALIZATION_CHARBONNIER: pG_c = g_c.ptr<float>(y); pG_cu = g_c.ptr<float>(y + 1); pG_cd = g_c.ptr<float>(y - 1); break; case PENALIZATION_PERONA_MALIK: pG_p = g_p.ptr<float>(y); pG_pu = g_p.ptr<float>(y + 1); pG_pd = g_p.ptr<float>(y - 1); break; } for (x = 1; x < width; x++) { switch (penalization) { case PENALIZATION_CHARBONNIER: gc = pG_c[x]; gl = gc + pG_c[x - 1]; gr = gc + pG_c[x + 1]; gu = gc + pG_cu[x]; gd = gc + pG_cd[x]; gc = gl + gr + gu + gd; break; case PENALIZATION_PERONA_MALIK: gc = pG_p[x]; gl = gc + pG_p[x - 1]; gr = gc + pG_p[x + 1]; gu = gc + pG_pu[x]; gd = gc + pG_pd[x]; gc = gl + gr + gu + gd; break; } float _fi = Fi; if (maxDisp > minDisp) { if (pU[x] > maxDisp * scale) {_fi *= 1000; pU[x] = static_cast<float>(maxDisp * scale);} if (pU[x] < minDisp * scale) {_fi *= 1000; pU[x] = static_cast<float>(minDisp * scale);} } int A = static_cast<int>(pU[x]); int neg = 0; if (pU[x] <= 0) neg = -1; if (x + A > width) pu[x] = pU[width - A]; else if (x + A + neg < 0) pu[x] = pU[- A + 2]; else { pu[x] = A + (pI2x[x + A + neg] * (pI1[x] - pI2[x + A]) + _fi * (gr * pU[x + 1] + gl * pU[x - 1] + gu * pUu[x] + gd * pUd[x] - gc * A)) / (pI2x[x + A + neg] * pI2x[x + A + neg] + gc * _fi) ; } }// x pu[0] = pu[1]; pu[width] = pu[width - 1]; }// y for (x = 0; x <= width; x++) { u.at<float>(0, x) = u.at<float>(1, x); u.at<float>(height, x) = u.at<float>(height - 1, x); } u.copyTo(U); if (!g_c.empty()) g_c.release(); if (!g_p.empty()) g_p.release(); }//n }