[Closed] Matching Axis from Position points
So what I’m trying to do is take an array containing X number of point3 values (no less then 3 points) and create teapot at the center location of those points. Then from there align the teapot so it’s Y axis is pointing at the first item in that point3 array and the Z axis is point outward from those points (like a local axis)
The image on the left is what I have so far. The item on the right in the image is what I’m after. Hope someone can help.
Thank you
Starter code is below for testing.
(
delete objects
local columns = 6
local columnAngle = 360.0 / columns
local radius = (units.decodevalue "20.0cm")
local master = point pos:[0,0,0]
local ptsArr = #()
local posArr = #()
for c = 1 to columns do
(
calcX = radius * cos((c-1) * columnAngle)
calcY = radius * sin((c-1) * columnAngle)
pos = [calcX,calcY,40]
pt = text text:(c as string) pos:pos size:5 wirecolor:green parent:master
append ptsArr pt
)
rotate master (AngleAxis (random -360 360) [1,1,1])
delete master
posArr = for p in ptsArr collect p.pos
--place point at the center point of the position points
centerPos = [0,0,0]
for o in posArr do centerPos += o
centerPos/=posArr.count
--the Y axis should point at the first item in the array
-- the Z axis should point along the normal
newPoint = point pos:centerPos size:radius wirecolor:yellow cross:false box:false axisTripod:true
)
so we need at least two vectors to make a matrix.
one vector is from center to the first point
second could be from center to second point…
in this case the matrix is:
fn multipointTM points = if points.count > 1 do
(
center = [0,0,0]
for p in points do center += p.pos
center /= points.count
front = normalize (points[1].pos - center)
side = normalize (points[2].pos - center)
up = normalize (cross front side) -- orthogonalized up
side = normalize (cross up front) -- orthogonalized side
tm = matrix3 front side up center
)
delete objects
pp = for k=1 to 10 collect (point size:5 axistripod:off pos:[10*cos(a = random 0 360), 10*sin(a),0] wirecolor:yellow)
in coordsys world about [0,0,0] rotate pp (eulerangles (random 0 90) (random 0 90) 0)
p = point size:10 axistripod:on transform:(multipointTM pp) wirecolor:green
I just tried yore method and seems that center position in not on the right plase.
Look at the sphere position
Maybe I’m wrong but just try this
fn multipointTM points = if points.count > 1 do
(
center = [0,0,0]
for p in points do center += p.pos
center /= points.count
front = normalize (points[1].pos - center)
side = normalize (points[2].pos - center)
up = normalize (cross front side) -- orthogonalized up
side = normalize (cross up front) -- orthogonalized side
tm = matrix3 front side up center
)
delete objects
rot = (eulerangles (random 0 90) (random 0 90) 0)
guide = splineShape name:"TestShape" vertexTicks:on
addNewSpline guide
pp = for k=1 to 10 collect
(
rndPos = [10*cos(a = random 0 360), 10*sin(a),0]
addKnot guide 1 #corner #line rndPos
point size:5 axistripod:off pos:rndPos wirecolor:yellow
)
in coordsys world about [0,0,0] (rotate pp rot ; rotate guide rot)
guide.pivot = guide.center ; updateshape guide ; sphere radius:1 pos:guide.center
p = point size:10 axistripod:on transform:(multipointTM pp) wirecolor:green
what i calculate calls physical center (or center of mass). you are talking about geometrical center (center of bounding box)
Ok, thanks Denis
I did not mean to interrupt I was just curious. :hmm:
What’s the advantage of the use of physical center?
I thought, that the centroid was the geometric center, and,
it coincides with the center of mass if the density is homogeneous ?
http://en.wikipedia.org/wiki/Centroid
.
to be absolutely correct we have to check matrix rotation (angle between vector1 and vector2) to make right matrix every time.
Is this an edit that needs to be handled in the function?
Seemingly works just as expected.
it’s about Z vector… if way from point1 to point2 is CW Z(up) vector will look Up, if CCW it will look Down… or inversely…
to know that you have to define a system coordinates… if it’s WORLD the dot product of world-up ([0,0,1]) and your Up vector has to be positive or negative depending on CW or CCW.
I see what your saying.
So then users can control if the objects Z axis is pointing in the right direction. So if need be they can flip it.
Seems like something worth putting as an option on the function.
What’s the advantage of the use of physical center?
In this case, the physical center will always be on the plane, even if points are randomly dispatched on the plane.
The geometrical center might not be on the plane.
I think…
there is no any specific advantage… different tasks – different methods. usually physical center is used in algorithms where you need ‘weighted’ positions. Skin, or Soft Selection for example. I’ve used it because it’s easier to calculate
I see that you love to “confuse” people with your super formulas.
Thanks for that.
In order to find the center of the bounding box isn’t hard to do, just a pain to do.
If the Z is to be flipped direction wise do I just Invert the Up?
Thank you Denis for your help on this. I appreciate it.
generally we are talking about positions, not about nodes. that’s the one…
the two is the selection center is always in the world coordinate system. which is not always desired.
it’s not hard if you know coordinate system for the bounding box. but in most cases you don’t know. and have to find the best (where the bounding box is optimal)… this is hard.
If the Z is to be flipped direction wise do I just Invert the Up?
invert UP or do cross product for (v2 v1) instead of (v1 v2). both things do the same.
See the ‘2d’ section here, it might help: https://en.wikipedia.org/wiki/Minimum_bounding_box_algorithms
“It is based on the observation that a side of a minimum-area enclosing box must be collinear with a side of the convex polygon”
Judging by your image you only have 6 options to check… pick the smallest one.
sounds like a nice mini challange