Ejemplo n.º 1
0
Py::Object 
RendererAgg::draw_image(const Py::Tuple& args) {
  _VERBOSE("RendererAgg::draw_image");
  theRasterizer->reset_clipping();
  args.verify_length(5);
  
  float x = Py::Float(args[0]);
  float y = Py::Float(args[1]);
  Image *image = static_cast<Image*>(args[2].ptr());
  std::string origin = Py::String(args[3]);
  
  if (origin!="lower" && origin!="upper")
    throw Py::ValueError("origin must be upper|lower");
  
  bool isUpper = origin=="upper";
  
  size_t ind = 0;
  size_t thisx, thisy;
  float oy = isUpper ? y : height-y;

  float minx(0), maxx(width), miny(0), maxy(height);

  if (args[4].ptr() != Py_None) {
    Bbox* bbox = static_cast<Bbox*>(args[4].ptr());
    minx = bbox->ll_api()->x_api()->val();
    maxy = height-bbox->ll_api()->y_api()->val();
    maxx = bbox->ur_api()->x_api()->val();
    miny = height-bbox->ur_api()->y_api()->val();
  }

  //if (isUpper) oy -= image->rowsOut;  //start at top
  //std::cout << minx << " " << maxx << " " << miny << " " << maxy << std::endl;
  for (size_t j=0; j<image->rowsOut; j++) {
    thisy =  (size_t)(isUpper ?  oy+j : oy-j-0.5);
    if (thisy<miny || thisy>=maxy) {
     ind += 4*image->colsOut;
      continue;
    }
    for (size_t i=0; i<image->colsOut; i++) {
      thisx = (size_t)(i+x); 
      if (thisx<minx || thisx>=maxx) {
      ind += 4;
      	continue;
      }
	
      pixfmt::color_type p;

      p.r = *(image->bufferOut+ind++);
      p.g = *(image->bufferOut+ind++);
      p.b = *(image->bufferOut+ind++);
      p.a = *(image->bufferOut+ind++);

      pixFmt->blend_pixel(thisx, thisy, p, p.a);
    }
  }

  return Py::Object();
  
}
Ejemplo n.º 2
0
static void computexform()
{
	Bbox bb;
	bb.clear();
	NEST { for (int i=0;i<pt.n;i++) bb.takeunion(pt.co[i]); }
	ForMeshVertex(mesh,v) {
		bb.takeunion(mesh.point(v));
	} EndFor;
Ejemplo n.º 3
0
///-----------------------------------------------------------------------------
///! @brief   TODO enter a description
///! @remark
///-----------------------------------------------------------------------------
bool Frustum::IsInside(const Bbox& boundingBox)
{
    const Vector3 center = boundingBox.center();
    const float radius = boundingBox.radius();

    for (size_t counter = 0; counter < 6; ++counter)
    {
        if (m_planes[counter].ClassifyPoint(center) < -radius)
        {
            return false;
        }
    }

    return true;
}
Ejemplo n.º 4
0
void
RendererAgg::set_clip_from_bbox(const Py::Object& o) {
  if (o.ptr() != Py_None) {  //using clip
    // Bbox::check(args[0]) failing; something about cross module?
    // set the clip rectangle
    // flipy
    Bbox* clipbox = static_cast<Bbox*>(o.ptr());
    double l = clipbox->ll_api()->x_api()->val() ; 
    double b = clipbox->ll_api()->y_api()->val();
    double r = clipbox->ur_api()->x_api()->val() ; 
    double t = clipbox->ur_api()->y_api()->val() ; ;       
    theRasterizer->clip_box(l, height-t, r, height-b);      
  }

  
}
Ejemplo n.º 5
0
OGRErr OGRLIBKMLLayer::GetExtent( OGREnvelope * psExtent, int bForce )
{
    Bbox oKmlBbox;

    if( m_poKmlLayer != NULL &&
        kmlengine::GetFeatureBounds( AsFeature( m_poKmlLayer ), &oKmlBbox ) )
    {
        psExtent->MinX = oKmlBbox.get_west();
        psExtent->MinY = oKmlBbox.get_south();
        psExtent->MaxX = oKmlBbox.get_east();
        psExtent->MaxY = oKmlBbox.get_north();

        return OGRERR_NONE;
    }

    return OGRLayer::GetExtent(psExtent, bForce);
}
Ejemplo n.º 6
0
static int
PyAggImagePhoto(ClientData clientdata, Tcl_Interp* interp,
               int argc, char **argv)
{
    Tk_PhotoHandle photo;
    Tk_PhotoImageBlock block;
    PyObject* aggo;

    // vars for blitting
    PyObject* bboxo;
    Bbox* bbox;
    agg::int8u *destbuffer;
    double l,b,r,t;
    int destx, desty, destwidth, destheight, deststride;

    long mode;
    long nval;
    if (argc != 5) {
        Tcl_AppendResult(interp, "usage: ", argv[0],
                         " destPhoto srcImage", (char *) NULL);
        return TCL_ERROR;
    }

    /* get Tcl PhotoImage handle */
    photo = Tk_FindPhoto(interp, argv[1]);
    if (photo == NULL) {
        Tcl_AppendResult(interp, "destination photo must exist", (char *) NULL);
        return TCL_ERROR;
    }
    /* get array (or object that can be converted to array) pointer */
    aggo = (PyObject*)atol(argv[2]);
    RendererAgg *aggRenderer = (RendererAgg *)aggo;
    int srcheight = (int)aggRenderer->get_height();

    /* XXX insert aggRenderer type check */

    /* get array mode (0=mono, 1=rgb, 2=rgba) */
    mode = atol(argv[3]);
    if ((mode != 0) && (mode != 1) && (mode != 2)) {
        Tcl_AppendResult(interp, "illegal image mode", (char *) NULL);
        return TCL_ERROR;
    }

    /* check for bbox/blitting */
    bboxo = (PyObject*)atol(argv[4]);
    if (bboxo != Py_None) {
      bbox = (Bbox*)bboxo;
      l = bbox->ll_api()->x_api()->val();
      b = bbox->ll_api()->y_api()->val();
      r = bbox->ur_api()->x_api()->val();
      t = bbox->ur_api()->y_api()->val();

      destx = (int)l;
      desty = srcheight-(int)t;
      destwidth = (int)(r-l);
      destheight = (int)(t-b);
      deststride = 4*destwidth;

      destbuffer = new agg::int8u[deststride*destheight];
      if (destbuffer == NULL) {
	throw Py::MemoryError("_tkagg could not allocate memory for destbuffer");
      }

      agg::rendering_buffer destrbuf;
      destrbuf.attach(destbuffer, destwidth, destheight, deststride);
      pixfmt destpf(destrbuf);
      renderer_base destrb(destpf);

      agg::rect_base<int> region(destx, desty, (int)r, srcheight-(int)b);
      destrb.copy_from(*aggRenderer->renderingBuffer, &region,
		       -destx, -desty);
    } else {
      bbox = NULL;
      destbuffer = NULL;
      destx = desty = destwidth = destheight = deststride = 0;
    }

    /* setup tkblock */
    block.pixelSize = 1;
    if (mode == 0) {
        block.offset[0]= block.offset[1] = block.offset[2] =0;
        nval = 1;
    } else {
        block.offset[0] = 0;
        block.offset[1] = 1;
        block.offset[2] = 2;
        if (mode == 1) {
            block.offset[3] = 0;
            block.pixelSize = 3;
            nval = 3;
        } else {
            block.offset[3] = 3;
            block.pixelSize = 4;
            nval = 4;
        }
    }

    if (bbox) {

      block.width  = destwidth;
      block.height = destheight;
      block.pitch = deststride;
      block.pixelPtr = destbuffer;

      Tk_PhotoPutBlock(photo, &block, destx, desty, destwidth, destheight);
      delete [] destbuffer;

    } else {
      block.width  = aggRenderer->get_width();
      block.height = aggRenderer->get_height();
      block.pitch = block.width * nval;
      block.pixelPtr =  aggRenderer->pixBuffer;

      /* Clear current contents */
      Tk_PhotoBlank(photo);
      /* Copy opaque block to photo image, and leave the rest to TK */
      Tk_PhotoPutBlock(photo, &block, 0, 0, block.width, block.height);
    }

    return TCL_OK;
}
Ejemplo n.º 7
0
void Implicit::SetBoundingBox(const Bbox &b)
{
  mBox = b.Transform(mWorld2Obj);
}
Ejemplo n.º 8
0
void
BVH::build(std::vector<Bbox*>& bboxes)
{
	num_nodes++;
	int num = bboxes.size();

	if (num == 0) {
		std::cerr << "(BVH::build) bboxes has size 0" << std::endl;
		return;
	}

	// Make leaf node if only one object
	if (num <= NUM_TRI_IN_LEAF) {
		num_leaves++;
		//m_bbox = bboxes[0];
		m_bbox = new Bbox();

		for (int i = 0; i < num; i++) {
			m_bbox->addObject(bboxes[i]->object[0]);
		}

		for (int i = 0; i < num; i++) {
			delete bboxes[i];
		}
		return;
	}

	// Get split axis based on objects' center
	BVHHelper splits[NUM_SPLITS];
	/*
	int best_plane;
	float minVal, maxVal;
	int axis = getSplitPlane(bboxes, minVal, maxVal);
	float splitLength = (maxVal - minVal) / ((float)NUM_SPLITS);
	*/

	
	Vector3 best_minVal, best_maxVal;
	int best_axis = -1;
	float best_min_cost = MIRO_TMAX;
	int best_plane = -1;
	float best_splitLength;
	
	for (int axis = 0; axis < 3; axis++) {
		Vector3 minVal, maxVal;
		splitPlaneByAxis(bboxes, axis, minVal, maxVal);
		float splitLength = (maxVal[axis] - minVal[axis]) / ((float)NUM_SPLITS);
		int tmp_best_plane = -1;
	
		// initialize
		for (int i = 0; i < NUM_SPLITS; i++) {
			splits[i] = BVHHelper();
		}

		// Put bbox (objects) in the right box split
		for (int i = 0; i < num; i++) {
			if (bboxes[i]->object.size() > 1) {
				std::cerr << "(BVH::build) bboxes[i]->object has size > 1" << std::endl;
				return;
			}
			float boxSplitVal = bboxes[i]->object[0]->center()[axis];
			int index = getSplitIndex(boxSplitVal, minVal[axis], splitLength, axis);

			if (index >= NUM_SPLITS) {
				//if (tmp == NUM_SPLITS) {
				//	index--;				// Include edges to last box split
				//}
				//else {
				std::cerr << "(BVH::build) index > NUM_SPLITS (1)" << std::endl;
				//}
			}
			splits[index].update(bboxes[i]);
		}


		float l = (maxVal[axis] - minVal[axis]) / ((float)NUM_SPLITS);
		float w = maxVal[(axis + 1) % 3] - minVal[(axis + 1) % 3];
		float h = maxVal[(axis + 2) % 3] - minVal[(axis + 2) % 3];

		//// Calculate cost for each box split
		//for (int i = 0; i < NUM_SPLITS; i++) {
		//	// (Not sure if sa is left to 0.0f)
		//	if (splits[i].num_objs == 0) continue;

		//	/*
		//	// Not sure on cost equation
		//	// 2LW + 2(L+W)H
		//	float l = splits[i].max.x - splits[i].min.x;
		//	float w = splits[i].max.y - splits[i].min.y;
		//	float h = splits[i].max.z - splits[i].min.z;
		//	float sa = 2.0f * l * w + (2 * (l + w) * h);
		//	*/
		//	splits[i].sa = sa;
		//}


		// Get the plane with minimum cost
		float min_cost = MIRO_TMAX;

		for (int i = 1; i < NUM_SPLITS; i++) {
			float l_sa = 0.0f, r_sa = 0.0f;
			int l_num = 0, r_num = 0;
			float left_side = 0.0f, right_side = 0.0f;

			// left side
			for (int j = 0; j < i; j++) {
				//l_sa += splits[j].sa;
				l_num += splits[j].num_objs;
			}

			// right side
			for (int j = i; j < NUM_SPLITS; j++) {
				//r_sa += splits[j].sa;
				r_num += splits[j].num_objs;
			}

			//left_side = l_sa * l_num;
			//right_side = r_sa * r_num;

			l_sa = l * i;
			r_sa = l * (NUM_SPLITS - i);
			left_side = 2.0f * l_sa * w + (2 * (l_sa + w) * h);
			right_side = 2.0f * r_sa * w + (2 * (r_sa + w) * h);

			float cost = left_side * l_num + right_side * r_num;

			if (cost < min_cost) {
				min_cost = cost;
				tmp_best_plane = i;
			}
		}
	
		if (min_cost < best_min_cost) {
			best_min_cost = min_cost;
			best_plane = tmp_best_plane;
			best_axis = axis;
			best_minVal = minVal;
			best_maxVal = maxVal;
			best_splitLength = splitLength;
		}
	}

	// Put bbox (objects) in the left or right split
	std::vector<Bbox*> leftBoxes; //= new Bbox[best_l_num];
	std::vector<Bbox*> rightBoxes; //= new Bbox[best_r_num];
	Bbox * lBox = new Bbox();
	Bbox * rBox = new Bbox();

	int l_index = 0, r_index = 0;

	for (int i = 0; i < num; i++) {
		float boxSplitVal = bboxes[i]->object[0]->center()[best_axis];
		int index = getSplitIndex(boxSplitVal, best_minVal[best_axis], best_splitLength, best_axis);
		float boundary = m_bbox->min[best_axis] + (best_plane * best_splitLength);

		//float boxSplitVal = bboxes[i]->object[0]->center()[axis];
		//int index = getSplitIndex(boxSplitVal, minVal, splitLength, axis);
		//float boundary = m_bbox->min[axis] + (best_plane * splitLength);

		if (index < best_plane) {
			//leftBoxes[l_index++] = *bboxes[i];
			leftBoxes.push_back(bboxes[i]);
			lBox->addObject(bboxes[i]->object[0]);
		}
		else {
			//rightBoxes[r_index++] = *bboxes[i];
			rightBoxes.push_back(bboxes[i]);
			rBox->addObject(bboxes[i]->object[0]);
		}
	}

	// delete ptrs
	//delete splits;

	BVH *leftChild = new BVH();
	BVH *rightChild = new BVH();

	leftChild->m_bbox = lBox;
	rightChild->m_bbox = rBox;

	leftChild->build(leftBoxes);
	rightChild->build(rightBoxes);

	m_children.push_back(leftChild);
	m_children.push_back(rightChild);
}
/**
 * CGAL constructor
 * @param bbox The CGAL bounding box to copy
 */
SpatialPartitionNode::SpatialPartitionNode( const Bbox & bbox ) : circumcenters(), tetrahedrons(), left( NULL ), middle( NULL ), right( NULL ), box( bbox.xmin(), bbox.ymin(), bbox.zmin(), bbox.xmax(), bbox.ymax(), bbox.zmax() ) {

	return;
}
/**
 * Determines if two bounding boxes overlap
 * @param b0 The first box to check
 * @param b1 The second box to check
 * @return TRUE if the boxes overlap, FALSE otherwise
 */
bool boxesOverlap( const Bbox & b0, const Bbox & b1 ) {

	return !( ( b0.xmin() > b1.xmax() ) || ( b0.xmax() < b1.xmin() ) || ( b0.ymin() > b1.ymax() ) || ( b0.ymax() < b1.ymin() ) || ( b0.zmin() > b1.zmax() ) || ( b0.zmax() < b1.zmin() ) );
}