Notifications
Clear all
[Closed] average planar align point helpers
Feb 16, 2015 6:16 pm
So I have a bunch of point helper randomly placed in 3d space. What I’m looking to do is find the average planar of the points based on their positions and then align them all. Hopefully that makes sense…
delete objects
points = #()
for i = 1 to 10 do
(
pos = random [-20,-20,0] [20,20,20]
p = point pos:pos size:5 wirecolor:yellow
append points p
)
1 Reply
Feb 16, 2015 6:16 pm
How big will be your pointset more or less?
I’ve made a naive algorithm for this task, it can works pretty fast on hundreds or evens thousands of points, but it’s still naive and the running time grows exponentially as the pointset grows:
delete objects
points = #()
avg_pos = [0.0, 0.0, 0.0]
numPt = 50
for i = 1 to numPt do
(
pos = random [-20,-20,0] [20,20,20]
p = point pos:pos size:5 wirecolor:yellow
append points p
avg_pos += pos
)
--average pos
avg_pos /= numPt
--plane to show the result
pl = Plane width:100.0 length:100.0 pos:avg_pos wirecolor:white
--sum of all cross vectors
cross_sum = [0.0, 0.0, 0.0]
--calculating our cross vectors
for i = 1 to numPt do
(
for j = i+1 to numPt do
(
v1 = points[i].pos - avg_pos
v2 = points[j].pos - avg_pos
a_cross = cross (normalize v1) (normalize v2)
dot_p = dot (normalize a_cross) (normalize cross_sum)
if dot_p < 0.0 then a_cross *= -1.0
cross_sum += a_cross
pl.dir = normalize cross_sum --comment this line, it's just for the preview
)
CompleteRedraw() --comment this line, it's just for the preview
)
cross_sum = normalize cross_sum
pl.dir = cross_sum
To align those points just use this function to project them on the plane:
fn pointPlaneProj pA pB pC pD =
(
local nABC=normalize (cross (pB-pA) (pC-pA))
pD+((dot (pA-pD) nABC)*nABC)
)