|
![](/i/fill.gif) |
Wasn't it Kevin Loney who wrote:
>I'm graphing some data I obtained in a physics experiment for school and I
>have a set of points. I'm trying to find the best fit plane for the set of
>points, I tried this algorithim from the book "3D Math Primer for Graphics
>and Game Development" but I'm getting strange results and the plane is no
>where near what it's supposed to look like.
>All the points are already semi-planar so I'm assuming that the plane
>shouldn't be orient nearly 90 degrees to the data. I'll post an example of
>what going in in p.b.i if my explanation isn't very clear.
The code you posted works perfectly for me. My guess would be that you
have a silly mistake somewhere in the code that calls the bit that you
posted. (Wild guess: perhaps it's a left/right handed co-ordinate system
problem)
Here's an example of a complete scene using the plane-fitting code that
you posted. The 20 points are displaced from the plane "y=x+0.5z-1" by
random amounts. I reckon that the plane produced by the best fit
algorithm is a pretty good fit to the data.
global_settings {assumed_gamma 1.0}
camera {location <0,0,-10> look_at <0.5,-0.3,0> angle 12}
light_source { <-30, 100, -30> color rgb 1}
#declare Points = array[20];
#declare R=seed(2);
#declare i=0;
#while (i<20)
#declare X=rand(R);
#declare Z=rand(R);
#declare Y=(X + 0.5*Z - 1 -rand(R)*0.2);
#declare Points[i] = <X,Y,Z>;
sphere {Points[i],0.04 pigment {rgb x}}
#declare i=i+1;
#end
#local n = 20;
//Calculate Best Fit Normal
#local Normal = <0, 0, 0>;
#local P = Points[n-1];
#local i = 0;
#while(i < n)
#local C = Points[i];
#local Normal = Normal + <(P.z+C.z)*(P.y-C.y), (P.x+C.x)*(P.z-C.z),
(P.y+C.y)*(P.x-C.x)>;
#local P = C;
#local i = i + 1;
#end
#local Normal = vnormalize(Normal);
//Calculate the best fit distance
#local Distance = 0;
#local i = 0;
#while(i < n)
#local Distance = Distance + vdot(Points[i], Normal);
#local i = i + 1;
#end
#local Distance = Distance / n;
plane {Normal, Distance pigment {rgbt <1,1,1,0.7>}}
Post a reply to this message
|
![](/i/fill.gif) |