// Triangulate a polygon void glc::triangulate(QList<GLC_Point2d>& polygon, QList<int>& index, QList<int>& tList) { const int size= polygon.size(); if (size == 3) { tList << index[0] << index[1] << index[2]; return; } int i0= 0; int i1= 1; int i2= 2; while(i0 < size) { if (isDiagonal(polygon, i0, i2)) { // Add the triangle before removing the ear. tList << index[i0] << index[i1] << index[i2]; // Remove the ear from polygon and index lists polygon.removeAt(i1); index.removeAt(i1); // recurse to the new polygon triangulate(polygon, index, tList); return; } ++i0; i1= (i1 + 1) % size; i2= (i2 + 1) % size; } }
int MapNode::countGCost(MapNode* to) { if(isDiagonal(to)) return this->getCostG() + DIAGONAL_MOVE_COST; else return this->getCostG() + STRAIGHT_MOVE_COST; }
/*! This method returns true if and only if the matrix is * (approximately) equal to the identity matrix. The precision used * by this function is 1e-6. */ bool matrix3x3::isUnitMatrix(void) const { return ( isDiagonal() && IsApprox( ele[0][0], 1.0, 1e-6 ) && IsApprox( ele[1][1], 1.0, 1e-6 ) && IsApprox( ele[2][2], 1.0, 1e-6 ) ); }
// For all ops, an "Identity" is an op that only does bit-depth conversion // and is therefore a candidate for the optimizer to remove. bool MatrixOpData::isIdentity() const { if (hasOffsets() || hasAlpha() || !isDiagonal()) { return false; } return isMatrixIdentity(); }
std::string Gaussian<ScalarType>::toJSONString(bool styledWriter) const { DEBUG_OUT("saving gaussian as JSON", 40); //uses Jsoncpp as library. Jsoncpp is licensed as MIT, so we may use it without restriction. Json::Value root; Json::Writer* writer=NULL; if (styledWriter) writer = new Json::StyledWriter(); else writer = new Json::FastWriter(); //output the weight root["weight"] = getWeight(); //output the mean as array of doubles Json::Value mean(Json::arrayValue); Eigen::Matrix<ScalarType, Eigen::Dynamic, 1> gMean = getMean(); for (int i=0; i<gMean.size(); i++) mean.append(Json::Value(gMean[i])); root["mean"] = mean; //output the variance as array of double. only save the lower //triangular, as the other values are mirrored. Json::Value variance(Json::arrayValue); Eigen::Matrix<ScalarType, Eigen::Dynamic, Eigen::Dynamic> gVariance = getCovarianceMatrix(); if (isDiagonal(gVariance)) { DEBUG_OUT("save as diagonal covariance matrix", 45); for (int i=0; i<gVariance.rows(); i++) { variance.append(Json::Value(gVariance(i, i))); } } else { DEBUG_OUT("save as full covariance matrix", 45); for (int i=0; i<gVariance.rows(); i++) { for (int j=i; j<gVariance.cols(); j++) { variance.append(Json::Value(gVariance(i, j))); } } } root["covariance"] = variance; std::string str = writer->write(root); delete writer; return str; }
PackingMode detectOptimalPackingMode() const { if (areAllElementsNan()) { return PackingModeEmpty; } if (isScalar()) { return PackingModeScalar; } if (isDiagonal()) { return PackingModeDiagonal; } if (isSymmetric()) { return PackingModeSymmetric; } return PackingModeFull; }
/* * findTilt() : Angle of tilt calculated with distance * from corners (max/min_x/y) * * findDirection() : Direction of tilt calculated with * distance from side mid points (max/min_x/y, center_x/y) * * min_x, min_y center_x,min_y max_x, min_y * +---#-----+---------+ * | | | * | x2,y2 | x1,y1 # * | | | * min_x,center_y +---------+---------+ max_x,center_y * | | | * # x3,y3 | x4,y4 | * | | | * +---------+-----#---+ * min_x, max_y center_x,max_y max_x, max_y * * * # - tilted anchor corner points ( x1/2/3/4, y1/2/3/4 ) * */ int Shape::findTilt() { int x1 = center_x, y1 = center_y, p1 = grid_w * grid_h; int x2 = center_x, y2 = center_y, p2 = grid_w * grid_h; int x3 = center_x, y3 = center_y, p3 = grid_w * grid_h; int x4 = center_x, y4 = center_y, p4 = grid_w * grid_h; int angle = 0, a1=0, a2=0, a3=0, a4=0; int x=0, y=0, p=0; // p : roximity to corner int bbx = (max_x - min_x)/3, bby = (max_y - min_y)/3; //inner bounding box int direction = 1; if(debug){ cout << endl << "[W " ; int c = max_y-min_y; for(int i = 1; i < c/2; i++){ cout << widths_at_y[i] << "~" << widths_at_y[c-i] << " "; } cout << "W] " << endl; cout << endl << "[H " ; c = max_x-min_x; for(int i = 1; i < c/2; i++){ cout << heights_at_x[i] << "~" << heights_at_x[c-i] << " "; } cout << "H] " << endl; } /* * loop through all border pixels * only select pixels close to bounding box edge * (selected by outside inner bounding box) * for each quandrant measure disance from coner * closeset to corner for each quandrant is picked as * the anchor corner point */ int i = 0; while( i < mapcount ){ x = xmap[i]; y = ymap[i]; if( abs(x-center_x) > bbx || abs(y-center_y) > bby){//close to edge if( x > center_x && y <= center_y ){ //top right Q1 p = (max_x - x) + (y - min_y); if(p < p1){ p1 = p; x1 = x; y1 = y; } }else if( x <= center_x && y <= center_y ){ //top left Q2 p = (x - min_x) + (y - min_y); if(p < p2){ p2 = p; x2 = x; y2 = y; } }else if( x <= center_x && y > center_y ){ //bot left Q3 p = (x - min_x) + (max_y - y); if(p < p3){ p3 = p; x3 = x; y3 = y; } }else if( x > center_x && y > center_y ){ //bot right Q4 p = (max_x - x) + (max_y - y); if(p < p4){ p4 = p; x4 = x; y4 = y; } } } i++; } a1 = findAngle(x2, y2, x1, y1); a2 = findAngle(x1, y1, x4, y4); a3 = findAngle(x4, y4, x3, y3); a4 = findAngle(x3, y3, x2, y2); angle = isDiagonal(a1, a2, a3, a4) ? maxAngle(a1, a2, a3, a4) : (a1+a2+a3+a4)/4; if(angle != 45 ) direction = findDirection(x1, y1, x2, y2, x3, y3, x4, y4); angle*=direction; if(debug) cout << "[ " << angle << " | " << a1 << " " << a2 << " " << a3 << " " << a4 << " ]" << endl ; if( pixdebug ){ d_pixmap->clearPixmap(); d_markAnchor(); d_pixmap->setPen(0, 255, 0); d_pixmap->markPoint( x1, y1, 6); d_pixmap->setPen(0, 0, 255); d_pixmap->markPoint( x4, y4, 6); d_pixmap->setPen(0, 255, 255); d_pixmap->markPoint( x3, y3, 6); d_pixmap->setPen(0, 0, 0); d_pixmap->markPoint( x2, y2, 6); d_pixmap->writeImage("corners"); } return angle; }
//------------ // Constructor //------------ XEMModel::XEMModel(XEMModelType * modelType, int64_t nbCluster, XEMData *& data, XEMPartition *& knownPartition) { int64_t k; int64_t i; _deleteData = false; _nbCluster = nbCluster; _data = data; _nbSample = _data->_nbSample; _algoName = UNKNOWN_ALGO_NAME; _tabFik = new double*[_nbSample]; _tabCik = new double*[_nbSample]; _tabSumF = new double[_nbSample]; _tabTik = new double*[_nbSample]; _tabZikKnown = new int64_t *[_nbSample]; _tabZiKnown = new bool[_nbSample]; _tabNk = new double[_nbCluster]; for (i=0; i<_nbSample; i++) { _tabFik[i] = new double[_nbCluster]; _tabTik[i] = new double[_nbCluster]; _tabZikKnown[i] = new int64_t [_nbCluster]; _tabCik[i] = new double[_nbCluster]; for (k=0; k<_nbCluster; k++) { _tabFik[i][k] = 0.0; _tabTik[i][k] = 0.0; _tabZikKnown[i][k] = 0; _tabCik[i][k] = 0.0; } _tabZiKnown[i] = false; _tabSumF[i] = 0.0; } // _tabNk[k] = 0 even if knownPartition because this partition could be partial for (k=0; k<_nbCluster; k++) { _tabNk[k] = 0.0; } FixKnownPartition(knownPartition); XEMModelName modelName = modelType->_nameModel; // create Param if (isSpherical(modelName)) { _parameter = new XEMGaussianSphericalParameter(this, modelType); } if (isDiagonal(modelName)) { _parameter = new XEMGaussianDiagParameter(this, modelType); } if (isGeneral(modelName)) { _parameter = new XEMGaussianGeneralParameter(this, modelType); } //HDDA models if (isHD(modelName)) { _parameter = new XEMGaussianHDDAParameter(this, modelType); } switch(modelName) { // Binary models // case (Binary_p_E): _parameter = new XEMBinaryEParameter(this, modelType, ((XEMBinaryData*)data)->getTabNbModality()); break; case (Binary_p_Ek): _parameter = new XEMBinaryEkParameter(this, modelType, ((XEMBinaryData*)data)->getTabNbModality()); break; case (Binary_p_Ej): _parameter = new XEMBinaryEjParameter(this, modelType, ((XEMBinaryData*)data)->getTabNbModality()); break; case (Binary_p_Ekj): _parameter = new XEMBinaryEkjParameter(this, modelType, ((XEMBinaryData*)data)->getTabNbModality()); break; case (Binary_p_Ekjh): _parameter = new XEMBinaryEkjhParameter(this, modelType, ((XEMBinaryData*)data)->getTabNbModality()); break; case (Binary_pk_E): _parameter = new XEMBinaryEParameter(this, modelType, ((XEMBinaryData*)data)->getTabNbModality()); break; case (Binary_pk_Ek): _parameter = new XEMBinaryEkParameter(this, modelType, ((XEMBinaryData*)data)->getTabNbModality()); break; case (Binary_pk_Ej): _parameter = new XEMBinaryEjParameter(this, modelType, ((XEMBinaryData*)data)->getTabNbModality()); break; case (Binary_pk_Ekj): _parameter = new XEMBinaryEkjParameter(this, modelType, ((XEMBinaryData*)data)->getTabNbModality()); break; case (Binary_pk_Ekjh): _parameter = new XEMBinaryEkjhParameter(this, modelType, ((XEMBinaryData*)data)->getTabNbModality()); break; } }
adjacency_list_t make_graph(Landscape landscape) { int cnt = 0; adjacency_list_t adjacency_list(landscape.width*landscape.height); for(int i = 0; i < landscape.height; i++) { for(int j = 0; j < landscape.width; j++) { if (landscape.getTile(j, i).walkSpeed() == 0) { cnt++; continue; } if(i == 0) { ///////////////////////////////// for(int i1 = 0; i1 < 2; i1++) { for(int j1 = -1; j1 < 2; j1++) { if( (j == 0 && j1 == -1) || (j == landscape.width-1 && j1 == 1) ) { continue; } double weight = 1; if (isDiagonal(i1, j1)) { weight = 1./sqrt(2.); } if( !( (i1 == 0) && (j1 == 0) ) ) { if (landscape.getTile(j+j1, i+i1).walkSpeed() != 0) { adjacency_list[cnt].push_back(neighbor(cnt + j1 + landscape.width*i1, weight)); } } } } ////////////////////////////////// } else if(i == landscape.height - 1) { ///////////////////////////////// for(int i1 = -1; i1 < 1; i1++) { for(int j1 = -1; j1 < 2; j1++) { double weight = 1; if (isDiagonal(i1, j1)) { weight = 1./sqrt(2.); } if( (j == 0 && j1 == -1) || (j == landscape.width-1 && j1 == 1) ) { continue; } if( !( (i1 == 0) && (j1 == 0) ) ) { if ( landscape.getTile(j+j1, i+i1).walkSpeed() != 0 ) { adjacency_list[cnt].push_back(neighbor(cnt + j1 + landscape.width*i1, weight)); } } } } ////////////////////////////////// } else { ///////////////////////////////// for(int i1 = -1; i1 < 2; i1++) { for(int j1 = -1; j1 < 2; j1++) { double weight = 1; if (isDiagonal(i1, j1)) { weight = 1./sqrt(2.); } if( (j == 0 && j1 == -1) || (j == landscape.width-1 && j1 == 1) ) { continue; } if( !( (i1 == 0) && (j1 == 0) ) ) { if ( landscape.getTile(j+j1, i+i1).walkSpeed() != 0) { adjacency_list[cnt].push_back(neighbor(cnt + j1 + landscape.width*i1, weight)); } } } } ////////////////////////////////// } cnt++; } } //print_neighbour(adjacency_list, cnt); return adjacency_list; }