Maybe there is something in the boost geometry libraries?
Do you know how to do it mathematically?
If not, I dreamt up this, not sure if it is right:
- 4 points create a spherical trapezium.
- cut the trapezium in half to form 2 spherical triangles.
- the plane defined by the 3 points in the triangle is at right angles to a normal of the sphere.
- calc the centre point of the triangle using plane geometry, the normal vector goes through this point.
- use the centre point and 2 points of the triangle to calc a cross product, which gives the normal vector
- do the same for the other spherical triangle
- the two normal intersect at the centre of the sphere
- calc the radius
If you do have to implement this yourself, I can see there is a bit of work in it. Unless there is a much easier way that I am not aware of.
Not sure if this is of any help at all, but I had a go. :-)
You only need three points on the circumference, and there is actually a pretty easy method.
Given p1, p2, p3 which are distinct, non-linear points
Let d1, d2, d3 be the linear distances between the points.
Use the Pythagorean Theorem to calculate them.
d = √( (pm.x-pn.x)2 + (pm.y-pn.y)2 )
Let a be the area of the triangle formed by the three points
Use Heron's Forumula to calculate it.
s = (d1 + d2 + d3) ÷ 2
a = √( s(s-d1)(s-d2)(s-d3) )
Let r be the radius of the circle
Using the formula
r = (d1d2d3) ÷ (4a)
Finally you can use the Pythagorean Theorem again to locate the center point of the circle. The only trick is to make sure you put it inside the circle. This is easy enough to figure because you only need identify the two points at the end of the longest edge identified in step one above, and make sure that the new center point is not on the same side of that edge as the remaining point.
It does not work because the four points are not exactly on the surface (a little error resulting from the sensors that acquire these points). I have to acquire more points and then fit them to a sphere. Could you have any source code for fitting a cloud of 3D points to a sphere (theoretically these points must be on the sphere surface with a small error)? I have googled it but I didn't find any code using c++.