Exemple #1
0
		bool init(std::vector<TPoint> &p) {
			bool check = false;
			for (int i = 1; i < (int)p.size(); i++) {
				if (dcmp(sqrdist(p[0], p[i]))) {
					std::swap(p[1], p[i]);
					check = true;
					break;
				}
			}
			if (!check) return false;
			check = false;
			for (int i = 2; i < (int)p.size(); i++) {
				if (dcmp(sqrdist(det(p[i] - p[0], p[1] - p[0])))) {
					std::swap(p[2], p[i]);
					check = true;
					break;
				}
			}
			if (!check) return false;
			check = false;
			for (int i = 3; i < (int)p.size(); i++) {
				if (dcmp(detdot(p[0], p[1], p[2], p[i]))) {
					std::swap(p[3], p[i]);
					check = true;
					break;
				}
			}
			if (!check) return false;
			for (int i = 0; i < (int)p.size(); i++)
				for (int j = 0; j < (int)p.size(); j++)
					whe[i][j] = -1;
			return true;
		}
Exemple #2
0
bool		poly2::PointInPoly(const point2& p0) const
{
	//	Poly bounding box test
	if (p0[0] <= vxMin || p0[0] >= vxMax) return false;
	if (p0[1] <= vyMin || p0[1] >= vyMax) return false;

	//	else Ray-PolyEdge intresection tests
	ray2	rHor(p0,vector2(1,0));
	bool	inFlag = false;
	bool	vFlag = false;
	point2 vInt;
	for (int jj=0; jj<size(); jj++) {
//		cout << "\n" << jj << " " << (jj+1)%4 << " " << vPoly[jj] << " " << vPoly[(jj+1)%4] << " ";
		if (vFlag && (sqrdist(vInt,vPoly[jj]) < MAXPointTol || sqrdist(vInt,vPoly[(jj+1)%4]) < MAXPointTol )) {	//	cached vertex intersection point test
			vFlag = false;
			continue;	//	skip test for this edge
		}
		Double tParam = NaN_QUIET;
		int	result = rHor.intersect(lsEdge(jj),tParam);
//		cout << result << " " << tParam << " " ;
		if (result == 0) {
			continue;
		}
		if (result == -1) {	//	vertex intersection
			vFlag = true;
			vInt = p0 + tParam*rHor.dir();
// 			cout << vInt << " ";
			if (vInt[1] == yMax() || vInt[1] == yMin()) continue;	//	NECESSARY! - but only works for convex polygons!
		}
		inFlag = !inFlag;
// 		cout << inFlag << " ";
	}
// 		cout << "\n";
	return	inFlag;
}
void landmarked_neighbors<Tdata,Tint>::advance_heap()
{
  int thisGroup,pointIndex,thisPointIndex;
  const Tdata *thisPoint;
  Tdata tmpR2;

  if (!use_landmarks) {
    return;
  }

  // Add points to heap (if necessary).  Here's the scheme &
  // considerations:
  // (1) add all groups that have a lower-bound distance
  //     within the currentR2
  // (2) if the closest point yet has a square distance larger than
  //     currentR2, it might not be the closest point: there might be
  //     another landmark group containing a closer point.  So we increase
  //     currentR2 and add all the groups up to that distance.
  //
  // This leads to a double-while construction.  We could do it with a
  // single-while, but it could result in unnecessary points being
  // added to the heap, if a later group already scheduled for
  // addition has the closest point.
  //mexPrintf("1: currentR2 %g, lmIterator->R2 %g\n",currentR2,lmIterator->R2);
  while (lmIterator < landmarks_sdi.end() && lmIterator->R2 <= currentR2) {
    //mexPrintf("2: currentR2 %g, lmIterator->R2 %g\n",currentR2,lmIterator->R2);
    while (lmIterator < landmarks_sdi.end() && lmIterator->R2 <= currentR2) {
      // Add all the points in the current group
      thisGroup = lmIterator->index;
      //mexPrintf("Adding group %d\n",thisGroup);
      for (pointIndex = 0; pointIndex < lminfo.n_landmarkList[thisGroup]; pointIndex++) {
	thisPointIndex = (int) lminfo.landmarkList[thisGroup][pointIndex] - lminfo.index_offset;
	thisPoint = x + lminfo.d*thisPointIndex;
	tmpR2 = sqrdist(&(*y.begin()),&(*y.end()),thisPoint);
	*point_heap_end = SqrdistIndex<Tdata>(tmpR2,thisPointIndex);
	// make it so the top of the heap is the closest point
	push_heap(point_heap.begin(),++point_heap_end,is_farther<Tdata>());
      }
      //mexPrintf("Heap status:\n");
      //typename vector< SqrdistIndex<Tdata> >::iterator phI;
      //for (phI = point_heap.begin(); phI < point_heap_end; phI++)
      //  mexPrintf("x%d (%g) ",phI->index,phI->R2);
      //mexPrintf("\n");
      lmIterator++;
    } // end of inside while loop
    // We want to make sure we keep including groups until we get some
    // points on the heap that are closer than any points not on the
    // heap.  The way to do that is to adjust currentR2 to reflect the
    // distance to the closest point.
    if (!is_empty())
      currentR2 = point_heap.begin()->R2;
    else if (lmIterator < landmarks_sdi.end()) {
      // If the heap is still empty, but we haven't exhausted all
      // landmarks, then we must have had only empty landmark
      // groups. In that case, increment to the next landmarkR2.
      currentR2 = lmIterator->R2;
    }
  }
}
Exemple #4
0
    DBL_MATRIX Alignment::estimateAlpha_(DBL_MATRIX& points, DBL_MATRIX& rt_vec)
    {
        unsigned int n_points = points.rowCount();
        unsigned int rt_size = rt_vec.rowCount();

        if(n_points < 1){
            MSPP_LOG(logERROR) << "Alignment::estimateAlpha_(): number of points must be greater than 0!" << std::endl;
        }
        mspp_precondition(n_points >= 1 , "Alignment::estimateAlpha_(): number of points must be greater than 0!");
 
        if(points.columnCount() != rt_vec.columnCount()){
            MSPP_LOG(logERROR) << "Alignment::estimateAlpha_(): Wrong dimensionality of input - points and rt_vec must have equal dimensions!" << std::endl;
        }
        mspp_precondition(points.columnCount() == rt_vec.columnCount() , "Alignment::estimateAlpha_(): Wrong dimensionality of input - points and rt_vec must have equal dimensions!");

        //vector containing new alpha values
        DBL_MATRIX returnAlpha (n_points,1,0.);

        for(unsigned int i = 0; i < n_points; i++){
            DBL_MATRIX pointMat = rt_vec;
            pointMat.init(0.);

            //fill each line in pointMat with points(i,*)
            for(unsigned int k = 0; k < rt_size; k++){
                for(int l = 0; l < rt_vec.columnCount(); l++){
                    pointMat(k,l) = points(i,l);
                }
            }

            //calculate distances from rt_vec to points(i,*)
            vigra::linalg::Matrix<double> distances = rt_vec - pointMat;
            std::vector<double> sqrdist (rt_size); //needs to be sorted --> vector
            for(unsigned int k = 0; k < rt_size; k++){
                double sum = 0;
                for(int l = 0; l < rt_vec.columnCount(); l++){
                    sum += pow(distances(k,l) , 2);
                }
                sqrdist[k] = sum;
            }

            //sort sqrdist
            std::sort(sqrdist.begin(),sqrdist.end());

            double mean = 0;
            int range = ((20<rt_size)?20:rt_size);
            for(int k = 0; k < range; k++){
                mean += sqrdist[k];
            }
            mean /= range;

            returnAlpha(i,0) =( 1./sqrt(mean) * 10 + 0.1 );
           
        }

        return returnAlpha;
    }
Exemple #5
0
double getSectorArea(const Point &a, const Point &b, const double &r) {
	double c = (2.0 * r * r - sqrdist(a, b)) / (2.0 * r * r);
	double alpha = acos(c);
	return r * r * alpha / 2.0;
}
void landmarked_neighbors<Tdata,Tint>::initialize(const Tdata *yi,const Tdata *xi,landmarkStruct<Tdata,Tint> &lminfoi)
{
  x = xi;
  lminfo = lminfoi;

  //const Tdata *yEnd = y+lminfoi.d;
  Tdata *lmData;
  Tdata closestLandmarkR2,tmpR2;  // variables for square-distance
  Tdata tmpR;  // variable for distance
  int landmarkIndex;
  int i;

  // Copy the "vantage point," to prevent bugs that arise from the
  // user changing the value of y (this is practical because y is
  // small; x and lminfo are large enough that we don't want to make a
  // copy)
  y.clear();
  y.insert(y.end(),yi,yi+lminfo.d);

  landmarks_sdi.clear();
  point_heap.clear();

  // Step #1: determine the distance of each landmark from the current
  // probe point.  While doing this, keep track of the closest one, so
  // we can return its index
  closestLandmarkR2 = -1;
  //mexPrintf("lminfo.landmarks[0] %g, n_landmarks %d, d %d\n",*(lminfo.landmarks),lminfo.n_landmarks,lminfo.d);
  //mexPrintf("y %x, yEnd %x, diff %d\n",y,y.end(),y.end()-y);

  //mexPrintf("Landmark groups & R2s:\n");
  for (landmarkIndex = 0, lmData = lminfo.landmarks; landmarkIndex < lminfo.n_landmarks; landmarkIndex++, lmData+=lminfo.d) {
    tmpR2 = sqrdist(&(*y.begin()),&(*y.end()),lmData);
    //mexPrintf("%d %g\n",landmarkIndex,tmpR2);
    landmarks_sdi.push_back(SqrdistIndex<Tdata>(tmpR2,landmarkIndex));
    if (closestLandmarkR2 < 0 || tmpR2 < closestLandmarkR2) {
      closestLandmarkR2 = tmpR2;
      closestLandmarkIndex = landmarkIndex;
    }
  }

  if (use_landmarks) {
    // Step #2: For each landmark group, determine (using the triangle
    // inequality) the lower bound on distance between points in the
    // grouping and the probe point.  This is the distance we'll store
    // and sort by.
    for (landmarkIndex = 0; landmarkIndex < lminfo.n_landmarks; landmarkIndex++) {
      tmpR = lminfo.landmarkR[landmarkIndex];  // the radius of the group
      if (landmarks_sdi[landmarkIndex].R2 < tmpR*tmpR) {
	// The current probe point could be inside this landmark
	// group, so the lower bound on distance is 0
	landmarks_sdi[landmarkIndex].R2 = 0;
      } else {
	// The landmark group is farther away, use the triangle
	// inequality to determine the lower bound on the (square)
	// distance
	tmpR = sqrt(landmarks_sdi[landmarkIndex].R2) - tmpR;
	landmarks_sdi[landmarkIndex].R2 = tmpR*tmpR;
      }
    }

    // Step #3: Sort groups by this lower-bound distance (in
    // increasing order).
    //mexPrintf("Unsorted landmark order:\n");
    //for (landmarkIndex = 0; landmarkIndex < lminfo.n_landmarks; landmarkIndex++)
    //  mexPrintf("%d (%g) ",landmarks_sdi[landmarkIndex].index,landmarks_sdi[landmarkIndex].R2);
    //mexPrintf("\n");
    std::sort(landmarks_sdi.begin(),landmarks_sdi.end());
    //mexPrintf("Sorted landmark order:\n");
    //for (landmarkIndex = 0; landmarkIndex < lminfo.n_landmarks; landmarkIndex++)
    //  mexPrintf("%d (%g) ",landmarks_sdi[landmarkIndex].index,landmarks_sdi[landmarkIndex].R2);
    //mexPrintf("\n");

    // Step #4: Clear the heap and initialize the progress variables
    point_heap.clear();
    point_heap_end = point_heap.begin();
    lmIterator = landmarks_sdi.begin();
    currentR2 = lmIterator->R2;

    // Step #5: Get the point_heap set up for the first requests
    advance_heap();
  }


  else {
    // We're not going to be using the landmarks to triage the data
    // points. So instead, compute the distance to each data point and
    // then sort.
    const Tdata *dataEnd = x + lminfo.d * N;
    const Tdata *y_begin = &(*y.begin());
    const Tdata *y_end = &(*y.end());
    const Tdata *dataIterator;
    int dataIndex;
    for (dataIndex = 0, dataIterator = x; dataIterator < dataEnd; dataIterator += lminfo.d, dataIndex++) {
      tmpR2 = sqrdist(y_begin,y_end,dataIterator);
      point_heap.push_back(SqrdistIndex<Tdata>(tmpR2,dataIndex));
    }
    
    std::sort(point_heap.begin(),point_heap.end());

    cur_position = point_heap.begin();
  }
}
Exemple #7
0
/******************************************************************************
 * SUBROUTINE cffunction calculates CF for a residue.
 * a residue may be either a protein amino acid or a hetero group.
 ******************************************************************************/
int spfunction(FA_Global* FA,atom* atoms,resid* residue){
  int    i,j,l,k,m,n,a,b,c;                    // dumb counters REMOVED s,p,k 

  int    nbonded;
  float  dis;

  cfstr* cfs;
  OptRes* optres;
  constraint* cons;

  float ang;
  int   npoints;
  double constant;

  float radA,radB,radC;
  float rAB;
  int   contnum;

  int    type;
  int    fatm;
  int    covalent;

  //int   blist[20];
  //int   nblist=0;
  // int   bloops;

  float  spoints[MAX_SPHERE_POINTS][3];
  float  spoints_flag[MAX_SPHERE_POINTS]; // flag indicating if sphere point was chosen

  float  Kwall;
  float  Kdist;
  float  Kangle;

  Kwall=1.0e6;
  Kdist=1.0e3;
  Kangle=1.0e2;
  
  for(i=0;i<FA->num_optres;i++){
    FA->optres[i].cf.rclash = 0;
    FA->optres[i].cf.com = 0.0;
    FA->optres[i].cf.con = 0.0;
    FA->optres[i].cf.sas = 0.0;
    FA->optres[i].cf.wal = 0.0;    
  }

  for(i=1;i<=FA->res_cnt;i++){
    
    optres = NULL;
    cfs = NULL;
    
    for(j=residue[i].fatm[residue[i].rot];j<=residue[i].latm[residue[i].rot];j++){
      
      if(atoms[j].optres != NULL){
	
	optres = atoms[j].optres;
	type = optres->type;
	cfs = &optres->cf;
	
      }else{

	// non-optimizable residue
	continue;
      }
      
      if(type == 0 && atoms[j].isbb){continue;}

      for(c=0;c<MAX_SPHERE_POINTS;c++){
	spoints_flag[c]=0;
      }

      radA = atoms[j].radius;

      // approximation of area of one point of sphere
      constant=4.0*PI*pow((double)(radA+Rw),2.0)/(double)FA->tspoints;
      
      contnum = 0;
      nbonded = 0;

      
      if(atoms[j].ncons > 0){
	for(m=0;m<atoms[j].ncons;m++){

	  radC = atoms[j].number==atoms[j].cons[m]->anum1?
	    atoms[FA->num_atm[atoms[j].cons[m]->anum2]].radius:
	    atoms[FA->num_atm[atoms[j].cons[m]->anum1]].radius;

	  // maximum penalty value (starting penalty)
	  // default value if atoms are not interacting
	  cfs->con += Kangle*(radA+radC+2.0f*Rw);
	  cfs->con += Kdist*(radA+radC+2.0f*Rw);
	  
	}
      }

      // find contacts
      for(k=1;k<=FA->res_cnt;k++){
	
	for(l=residue[k].fatm[residue[k].rot];l<=residue[k].latm[residue[k].rot];l++){
	  
	  // intramolecular atoms
	  if(k==i){
	    fatm = residue[i].fatm[residue[i].rot];
	    if(residue[i].bonded[j-fatm][l-fatm] >= 0){
	      // atom is bonded
	      nbonded++;
	      continue;
	    }
	  }
	  	  
	  radB=atoms[l].radius;
	  rAB=radA+radB;
	  
	  dis=sqrdist(atoms[j].coor,atoms[l].coor);
	  
	  // is atom in contact range.
	  // if NO, discard atom l
	  if(dis > (rAB+2.0*Rw)*(rAB+2.0*Rw)){continue;}
	  
	  covalent = 0;
	  cons=NULL;

	  if(atoms[j].ncons > 0 && atoms[l].ncons > 0){
	    for(m=0;m<atoms[j].ncons;m++){
	      for(n=0;n<atoms[l].ncons;n++){
		if(atoms[j].cons[m]->id == atoms[l].cons[n]->id){
		  cons = atoms[j].cons[m];
		  break;
		}
	      }
	      if(cons != NULL){break;}
	    }	 
	    
	    if(cons != NULL){
	      
	      // covalent constraint
	      if(cons->type == 1){
		covalent = 1;
				
		ang = angle(atoms[j].coor,atoms[l].coor,atoms[atoms[l].bond[1]].coor);
		cfs->con -= Kangle*(rAB+2.0f*Rw)*(1.0f-(fabs(ang-120.0f)/120.0f));

		cfs->con -= Kdist*(rAB+2.0f*Rw)*(1.0f-(fabs(dis-1.5f)/1.5f));
	
	      }

	      // interaction constraint
	      else{
		

	      }
	      
	    } // end of constraint found
	    
	  } // end of constraint
       	
	  // WALL term
	  if(dis <= (FA->permeability*rAB)*(FA->permeability*rAB)  &&
	     !covalent || dis <= 1.5*1.5){
	    
	    cfs->wal += Kwall*(pow(dis,-6.0f)-pow(FA->permeability*rAB,-12.0f));

	    if(dis <= (FA->dee_clash*rAB)){cfs->rclash=1;}

	  }

	  npoints=0;
	  // calculate complementarity using spheres of points
	  for(a=0;a<FA->tspoints;a++){
	    for(b=0;b<3;b++){
	      spoints[a][b] = FA->sphere[a][b]*(radA+Rw)+atoms[j].coor[b];
	    }

	    if(sqrdist(spoints[a],atoms[l].coor) <= (atoms[l].radius+Rw)*(atoms[l].radius+Rw)){
	      spoints_flag[a]=1;
	      npoints++;
	    }

	  }

	  cfs->com += FA->energy[atoms[j].type][atoms[l].type]*constant*npoints;


	  // number of contacts for atom j
	  contnum++;

	} // end of atom l
	
      } // end of residue k
      
      npoints=0;

      for(a=0;a<FA->tspoints;a++){
	if(spoints_flag[a]){npoints++;}
      }
      
      if(nbonded){ npoints += FA->tspoints/4*nbonded; }

      if(npoints > FA->tspoints){ npoints = FA->tspoints; }

      printf("number of points in contact for atom[%d] = %d\n",j,npoints);

      cfs->sas += (FA->tspoints-npoints)*constant;

    } // end of atom j
    
  } // end of residue i
  
  return(0);

} 
Exemple #8
0
	long double dist(const TPoint &a) {
		return sqrt(sqrdist(a));
	}