Code Monkey home page Code Monkey logo

Comments (2)

rana1991 avatar rana1991 commented on July 21, 2024

In my case, that was because the code my points required a higher accuracy. The code was rounding them off, and thus many points now had the same coordinates. In this case, the bounding box becomes just one point. To solve this, you can increase the accuracy to long double for all involved variables. Or, depending on your application, you can have this fix:

	for (int i = 0; i < dim; i++) {
		if ((parent == NULL) || (parent->cut_dim == i)) {
			spread_in_coordinate(i, l, u, node->box[i]);
			if (node->box[i].upper == node->box[i].lower)
				node->box[i].upper = node->box[i].upper;
		}
		else {
			node->box[i] = parent->box[i];
		}
		float spread = node->box[i].upper - node->box[i].lower;//also known as depth
		if (spread > maxspread) {
			maxspread = spread;
			c = i;
		}
	}

	if (c != -1) {

		//
		// now, c is the identity of which coordinate has the greatest spread
		//

		if (false) {
			m = (l + u) / 2;
			select_on_coordinate(c, m, l, u);
		}
		else {
			float sum;
			float average;

			if (true) {
				sum = 0.0;
				for (int k = l; k <= u; k++) {
					if (c < 0 || c>2)
						c = c;
					sum += the_data[ind[k]][c];
				}
				average = sum / static_cast<float> (u - l + 1); //median?
			}
			else {
				// average of top and bottom nodes.
				average = (node->box[c].upper + node->box[c].lower)*0.5F;
			}
			m = select_on_coordinate_value(c, average, l, u); //rerranges indices inside
		}


		// move the indices around to cut on dim 'c'.
		node->cut_dim = c;
		node->l = l;
		node->u = u;

		node->left = build_tree_for_range(l, m, node);

		node->right = build_tree_for_range(m + 1, u, node);

		if (node->right == NULL) {
			for (int i = 0; i < dim; i++)
				node->box[i] = node->left->box[i];
			node->cut_val = node->left->box[c].upper;
			node->cut_val_left = node->cut_val_right = node->cut_val;
		}
		else if (node->left == NULL) {
			for (int i = 0; i < dim; i++)
				node->box[i] = node->right->box[i];
			node->cut_val = node->right->box[c].upper;
			node->cut_val_left = node->cut_val_right = node->cut_val;
		}
		else {
			node->cut_val_right = node->right->box[c].lower;
			node->cut_val_left = node->left->box[c].upper;
			node->cut_val = (node->cut_val_left + node->cut_val_right) / 2.0F;
			//
			// now recompute true bounding box as union of subtree boxes.
			// This is now faster having built the tree, being logarithmic in
			// N, not linear as would be from naive method.
			//
			for (int i = 0; i < dim; i++) {
				node->box[i].upper = std::max(node->left->box[i].upper,
					node->right->box[i].upper);

				node->box[i].lower = std::min(node->left->box[i].lower,
					node->right->box[i].lower);
			}
		}
	}
	else {
		//create terminal node
		//possibly repeated points: bounding box --> one point
		//maxspread = 0
		node->cut_dim = 0;
		node->cut_val = 0.0;
		node->l = l;
		node->u = u;
		node->left = node->right = NULL;
	}
}
return(node);

}

from libicp.

zeraphil avatar zeraphil commented on July 21, 2024

Thanks, I found this was exactly the reason for the error. I had rounding causing many points to have the same coordinates (origin) and the stack overflow was due to the ties. I didn't implement this fix, instead ensured that my data was not extremely flat (that was a fault on my end), but thanks for providing this, I'll give it a go.

from libicp.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.