int
dwarf_entry_breakpoints (Dwarf_Die *die, Dwarf_Addr **bkpts)
{
  int nbkpts = 0;
  *bkpts = NULL;

  /* Fetch the CU's line records to look for this DIE's addresses.  */
  Dwarf_Die cudie = CUDIE (die->cu);
  Dwarf_Lines *lines;
  size_t nlines;
  if (INTUSE(dwarf_getsrclines) (&cudie, &lines, &nlines) < 0)
    {
      int error = INTUSE (dwarf_errno) ();
      if (error == 0)		/* CU has no DW_AT_stmt_list.  */
	return entrypc_bkpt (die, bkpts, &nbkpts);
      __libdw_seterrno (error);
      return -1;
    }

  /* Search each contiguous address range for DWARF prologue_end markers.  */

  Dwarf_Addr base;
  Dwarf_Addr begin;
  Dwarf_Addr end;
  ptrdiff_t offset = INTUSE(dwarf_ranges) (die, 0, &base, &begin, &end);
  if (offset < 0)
    return -1;

  /* Most often there is a single contiguous PC range for the DIE.  */
  if (offset == 1)
    return search_range (begin, end, true, true, lines, nlines, bkpts, &nbkpts)
        ?: entrypc_bkpt (die, bkpts, &nbkpts);

  Dwarf_Addr lowpc = (Dwarf_Addr) -1l;
  Dwarf_Addr highpc = (Dwarf_Addr) -1l;
  while (offset > 0)
    {
      /* We have an address range entry.  */
      if (search_range (begin, end, true, false,
                        lines, nlines, bkpts, &nbkpts) < 0)
	return -1;

      if (begin < lowpc)
	{
	  lowpc = begin;
	  highpc = end;
	}

      offset = INTUSE(dwarf_ranges) (die, offset, &base, &begin, &end);
    }

  /* If we didn't find any proper DWARF markers, then look in the
     lowest-addressed range for an ad hoc marker.  Failing that,
     fall back to just using the entrypc value.  */
  return (nbkpts
	  ?: (lowpc == (Dwarf_Addr) -1l ? 0
	      : search_range (lowpc, highpc, false, true,
	                      lines, nlines, bkpts, &nbkpts))
	  ?: entrypc_bkpt (die, bkpts, &nbkpts));
}
Beispiel #2
0
int main() {
	p_srand(0);
	static int32_t x[MAXN], y[MAXN], w[MAXN];
	int N = 1000, M = 10000, R = 100;
	
	init(N, R, x, y, w);

	int32_t hash = 0;
	for (int it = 0; it < M; it++) {
		Rect rect = rand_rect(R);
		int32_t ret = search_range(rect, x, y, w, N);
		hash ^= ret;
		tick(N, R, x, y, w);
	}
	printf("%" PRIi32 "\n", hash);
	return 0;
}
Beispiel #3
0
	void test()
	{
		int data[5] = {0, 1, 2, 3, 4};
		int search_array[2] = {0, 4};
		const int const_search_array[2] = {0, 4};

		range<int> data_range(data, 5);
		range<const int> const_data_range= data_range;

		range<int> search_range(data, 2);
		range<const int> const_search_range(data, 2);
		
		find_first_of(data_range, 2);
		find_first_of(data_range, search_range);
		find_first_of(data_range, const_search_range);
		find_first_of(data_range, search_array);
		find_first_of(data_range, const_search_array);

		find_first_of(const_data_range, 2);
		find_first_of(const_data_range, search_range);
		find_first_of(const_data_range, const_search_range);
		find_first_of(const_data_range, search_array);
		find_first_of(const_data_range, const_search_array);


		find_last_of(data_range, 2);
		find_last_of(data_range, search_range);
		find_last_of(data_range, const_search_range);
		find_last_of(data_range, search_array);
		find_last_of(data_range, const_search_array);

		find_last_of(const_data_range, 2);
		find_last_of(const_data_range, search_range);
		find_last_of(const_data_range, const_search_range);
		find_last_of(const_data_range, search_array);
		find_last_of(const_data_range, const_search_array);

		
		// range algorithms
		
	}
bool KeypointMatcher::matchPatch(const cv::Mat& patch, const cv::Mat& target, cv::Point2d& target_pt) const {

	const int rx = irange(static_cast<int>(target_pt.x)-ncc_range,0,target.cols),
		ry = irange(static_cast<int>(target_pt.y)-ncc_range,0,target.rows),
		rw = rx+ncc_range_dbl>target.cols? target.cols-rx:ncc_range_dbl,
		rh = ry+ncc_range_dbl>target.rows? target.rows-ry:ncc_range_dbl;
	cv::Mat search_range(target, cv::Rect(rx,ry,rw,rh));
	
	if (rw < patch.cols || rh < patch.rows)
		return false;

	double peak;
	cv::Mat result;
	cv::Point peak_pt;
	/* */
	cv::matchTemplate(search_range, patch, result, CV_TM_CCORR_NORMED);
	cv::minMaxLoc(result, NULL, &peak, NULL, &peak_pt);

	if (peak < threshold) {
		return false;
	}
	/* *
	cv::matchTemplate(search_range, patch, result, CV_TM_SQDIFF_NORMED);
	cv::minMaxLoc(result, &peak, NULL, &peak_pt, NULL);
	/* */

	double peak_x = static_cast<double>(peak_pt.x);
	double peak_y = static_cast<double>(peak_pt.y);
	
	if (refine_subpx)
	{
		//Quadratic Interpolation
		const double z_00   = static_cast<double>( result.at<float>(peak_y,peak_x) );
		const double z_p10  = static_cast<double>( result.at<float>(peak_y,peak_x+1) );
		const double z_m10  = static_cast<double>( result.at<float>(peak_y,peak_x-1) );
		const double z_0m1  = static_cast<double>( result.at<float>(peak_y-1,peak_x) );
		//const double z_p1m1 = static_cast<double>( result.at<float>(peak_y-1,peak_x+1) );
		//const double z_m1m1 = static_cast<double>( result.at<float>(peak_y-1,peak_x-1) );
		const double z_0p1  = static_cast<double>( result.at<float>(peak_y+1,peak_x) );
		//const double z_p1p1 = static_cast<double>( result.at<float>(peak_y+1,peak_x+1) );
		//const double z_m1p1 = static_cast<double>( result.at<float>(peak_y+1,peak_x-1) );

		//const double a = z_00;
		const double b = (z_p10 - z_m10) / 2;
		const double c = (z_0p1 - z_0m1) / 2;
		const double d = -z_00 + (z_p10 + z_m10) / 2;
		const double e = -z_00 + (z_0p1 + z_0m1) / 2;
		
		target_pt.x = rx + peak_x + b/(2*d);
		target_pt.y = ry + peak_y + c/(2*e);
		//std::cout << "===" << std::endl;
		//std::cout << target_pt.x << std::endl;
		//std::cout << rx+peak_x << std::endl;
	}
	else
	{
		target_pt.x = rx + peak_x;
		target_pt.y = ry + peak_y;
	}
	
	return true;
}
PointedThing ClientEnvironment::getPointedThing(
	core::line3d<f32> shootline,
	bool liquids_pointable,
	bool look_for_object)
{
	PointedThing result;

	INodeDefManager *nodedef = m_map->getNodeDefManager();

	core::aabbox3d<s16> maximal_exceed = nodedef->getSelectionBoxIntUnion();
	// The code needs to search these nodes
	core::aabbox3d<s16> search_range(-maximal_exceed.MaxEdge,
		-maximal_exceed.MinEdge);
	// If a node is found, there might be a larger node behind.
	// To find it, we have to go further.
	s16 maximal_overcheck =
		std::max(abs(search_range.MinEdge.X), abs(search_range.MaxEdge.X))
			+ std::max(abs(search_range.MinEdge.Y), abs(search_range.MaxEdge.Y))
			+ std::max(abs(search_range.MinEdge.Z), abs(search_range.MaxEdge.Z));

	const v3f original_vector = shootline.getVector();
	const f32 original_length = original_vector.getLength();

	f32 min_distance = original_length;

	// First try to find an active object
	if (look_for_object) {
		ClientActiveObject *selected_object = getSelectedActiveObject(
			shootline, &result.intersection_point,
			&result.intersection_normal);

		if (selected_object != NULL) {
			min_distance =
				(result.intersection_point - shootline.start).getLength();

			result.type = POINTEDTHING_OBJECT;
			result.object_id = selected_object->getId();
		}
	}

	// Reduce shootline
	if (original_length > 0) {
		shootline.end = shootline.start
			+ shootline.getVector() / original_length * min_distance;
	}

	// Try to find a node that is closer than the selected active
	// object (if it exists).

	voxalgo::VoxelLineIterator iterator(shootline.start / BS,
		shootline.getVector() / BS);
	v3s16 oldnode = iterator.m_current_node_pos;
	// Indicates that a node was found.
	bool is_node_found = false;
	// If a node is found, it is possible that there's a node
	// behind it with a large nodebox, so continue the search.
	u16 node_foundcounter = 0;
	// If a node is found, this is the center of the
	// first nodebox the shootline meets.
	v3f found_boxcenter(0, 0, 0);
	// The untested nodes are in this range.
	core::aabbox3d<s16> new_nodes;
	while (true) {
		// Test the nodes around the current node in search_range.
		new_nodes = search_range;
		new_nodes.MinEdge += iterator.m_current_node_pos;
		new_nodes.MaxEdge += iterator.m_current_node_pos;

		// Only check new nodes
		v3s16 delta = iterator.m_current_node_pos - oldnode;
		if (delta.X > 0)
			new_nodes.MinEdge.X = new_nodes.MaxEdge.X;
		else if (delta.X < 0)
			new_nodes.MaxEdge.X = new_nodes.MinEdge.X;
		else if (delta.Y > 0)
			new_nodes.MinEdge.Y = new_nodes.MaxEdge.Y;
		else if (delta.Y < 0)
			new_nodes.MaxEdge.Y = new_nodes.MinEdge.Y;
		else if (delta.Z > 0)
			new_nodes.MinEdge.Z = new_nodes.MaxEdge.Z;
		else if (delta.Z < 0)
			new_nodes.MaxEdge.Z = new_nodes.MinEdge.Z;

		// For each untested node
		for (s16 x = new_nodes.MinEdge.X; x <= new_nodes.MaxEdge.X; x++) {
			for (s16 y = new_nodes.MinEdge.Y; y <= new_nodes.MaxEdge.Y; y++) {
				for (s16 z = new_nodes.MinEdge.Z; z <= new_nodes.MaxEdge.Z; z++) {
					MapNode n;
					v3s16 np(x, y, z);
					bool is_valid_position;

					n = m_map->getNodeNoEx(np, &is_valid_position);
					if (!(is_valid_position &&
						isPointableNode(n, nodedef, liquids_pointable))) {
						continue;
					}
					std::vector<aabb3f> boxes;
					n.getSelectionBoxes(nodedef, &boxes,
						n.getNeighbors(np, m_map));

					v3f npf = intToFloat(np, BS);
					for (std::vector<aabb3f>::const_iterator i = boxes.begin();
						i != boxes.end(); ++i) {
						aabb3f box = *i;
						box.MinEdge += npf;
						box.MaxEdge += npf;
						v3f intersection_point;
						v3s16 intersection_normal;
						if (!boxLineCollision(box, shootline.start, shootline.getVector(),
							&intersection_point, &intersection_normal)) {
							continue;
						}
						f32 distance = (intersection_point - shootline.start).getLength();
						if (distance >= min_distance) {
							continue;
						}
						result.type = POINTEDTHING_NODE;
						result.node_undersurface = np;
						result.intersection_point = intersection_point;
						result.intersection_normal = intersection_normal;
						found_boxcenter = box.getCenter();
						min_distance = distance;
						is_node_found = true;
					}
				}
			}
		}
		if (is_node_found) {
			node_foundcounter++;
			if (node_foundcounter > maximal_overcheck) {
				break;
			}
		}
		// Next node
		if (iterator.hasNext()) {
			oldnode = iterator.m_current_node_pos;
			iterator.next();
		} else {
			break;
		}
	}

	if (is_node_found) {
		// Set undersurface and abovesurface nodes
		f32 d = 0.002 * BS;
		v3f fake_intersection = result.intersection_point;
		// Move intersection towards its source block.
		if (fake_intersection.X < found_boxcenter.X)
			fake_intersection.X += d;
		else
			fake_intersection.X -= d;

		if (fake_intersection.Y < found_boxcenter.Y)
			fake_intersection.Y += d;
		else
			fake_intersection.Y -= d;

		if (fake_intersection.Z < found_boxcenter.Z)
			fake_intersection.Z += d;
		else
			fake_intersection.Z -= d;

		result.node_real_undersurface = floatToInt(fake_intersection, BS);
		result.node_abovesurface = result.node_real_undersurface
			+ result.intersection_normal;
	}
	return result;
}