void Bvh::constructTree(Node* n)
{
	if (n->endPrim < n->startPrim) {
		std::cout << "ERROR: endPrim is smaller than startPrim in constructTree" << std::endl;
		return;
	}

	// Compute the node's bounding box and assign the start and endPrim to the node
	std::pair<Vec3f, Vec3f> bb = computeBB(n->startPrim, n->endPrim);
	n->bbMin = bb.first;
	n->bbMax = bb.second;

	if (n->endPrim - n->startPrim + 1 > MAX_TRIANGLES_PER_LEAF)
	{
		// Decide how to split the primitives and
		// perform the actual split: shuffle the indices in the global list
		// so that they're split into two intervals; return the index that
		// separates the two intervals
		int splitPrim;
		switch (Bvh::mode) {
			case BvhMode_Spatial:
				splitPrim = partitionPrimitives(n->startPrim, n->endPrim);
				break;
			case BvhMode_SAH:
				splitPrim = partitionPrimitivesSAH(n->startPrim, n->endPrim);
				break;
		}

		// Create the left child and recursively call this
		// function with the left triangle interval
		n->leftChild = new Node(n->startPrim, splitPrim-1);
		constructTree(n->leftChild);

		// Create the right child and recursively call this
		// function with the right triangle interval
		n->rightChild = new Node(splitPrim, n->endPrim);
		constructTree(n->rightChild);
	}
}
Beispiel #2
0
void filter(
  unsigned char **pyramid, 
  int L, // pyramid level
  unsigned char *reason,
  unsigned short *labels, 
  int minArea,
  int width, int height,
  int owidth,
  int startX, int endX,
  int startY, int endY,
  int maxLabels){

  unsigned char *in = pyramid[L];

  unsigned short area[maxLabels];
  computeArea(labels,area,width,height,startX,endX,startY,endY,maxLabels);

  unsigned short l[maxLabels];
  unsigned short r[maxLabels];
  unsigned short t[maxLabels];
  unsigned short b[maxLabels];
  computeBB(labels,l,r,t,b,width,height,startX,endX,startY,endY,maxLabels);

  bool kill[maxLabels];
  unsigned char kreason[maxLabels];

  for(int i=0; i<maxLabels; i++){
    if(area[i]>0 && i>0){ // is this region still there?

      // ratio
      int w = (r[i]-l[i])+1;
      int h = (t[i]-b[i])+1;
      float ratio = float(w)/float(h);
      
      //solidity
      float solidity = float(area[i])/float(w*h);
      
      //printf("%d %f %d %f\n",i,ratio,area[i],solidity);
      
      float areaPercent = float(area[i])/float(width*height);
      
      if(ratio < 0.1f || ratio > 2.f){
	kill[i] = true;
	kreason[i]=50;
      }else if(area[i]<minArea){
	kill[i] = true;
	kreason[i]=100;
      }else if(areaPercent>0.2f){
	kill[i] = true;
	kreason[i]=150;
      }else if(solidity < 0.20f){
	kill[i] = true;
	kreason[i]=200;
      }else{
	kill[i]=false;
	kreason[i]=0;
      }
      
    }else{
      kill[i] = false;
      kreason[i]=0;
    }
  }


  for(int x=startX; x<endX; x++){
    for(int y=startY; y<endY; y++){
      if(kill[labels[x+y*width]]){
	in[x+y*width] = 0;
	unsigned char r = kreason[labels[x+y*width]];
	reason[x+y*width]=r;
	labels[x+y*width]=0;

	if(r==150){
	  int sX = x*2;
	  int eX = x*2+2;
	  int sY = y*2;
	  int eY = y*2+2;

	  for(int l=L-1; l>=0; l--){
	    int lw=owidth/pow(2,l);

	    for(int i=sX; i<eX; i++){
	      for(int j=sY; j<eY; j++){
		pyramid[l][i+j*lw]=0;
	      }
	    }
	    sX*=2;
	    sY*=2;
	    eX*=2;
	    eY*=2;
	  }
	}

      }
    }
  }

  // keep area accurate
  for(int i=0; i<maxLabels;i++){
    if(kill[i]){area[i]=0;}
  }
}