青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Maths - Angle between vectors

LINK: http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm

How do we calculate the angle between two vectors?

For 2D Vectors

This is relatively simple because there is only one degree of freedom for 2D rotations. If v1 and v2 are normalised so that |v1|=|v2|=1, then,

angle = acos(v1•v2)

where:

  • • = 'dot' product (see box on right of page).
  • acos = arc cos = inverse of cosine function see trigonometry page.
  • |v1|= magnitude of v1.

The only problem is, this won't give all possible values between 0° and 360°, or -180° and +180°. In other words, it won't tell us if v1 is ahead or behind v2, to go from v1 to v2 is the opposite direction from v2 to v1.

In most math libraries acos will usually return a value between 0 and PI (in radians) which is 0° and 180°.

If we want a + or - value to indicate which vector is ahead, then we probably need to use the atan2 function (as explained on this page). using:

angle of 2 relative to 1= atan2(v2.y,v2.x) - atan2(v1.y,v1.x)

For 3D Vectors

Axis Angle Result

This is easiest to calculate using axis-angle representation because:

  • the angle is given by acos of the dot product of the two (normalised) vectors: v1•v2 = |v1||v2| cos(angle)
  • the axis is given by the cross product of the two vectors, the length of this axis is given by |v1 x v2| = |v1||v2| sin(angle).

as explained here

this is taken from this discussion.

So, if v1 and v2 are normalised so that |v1|=|v2|=1, then,

angle = acos(v1•v2)

axis = norm(v1 x v2)

If the vectors are parallel (angle = 0 or 180 degrees) then the length of v1 x v2 will be zero because sin(0)=sin(180)=0. In the zero case the axis does not matter and can be anything because there is no rotation round it. In the 180 degree case the axis can be anything at 90 degrees to the vectors so there is a whole range of possible axies.

angle (degrees) sin(angle) cos(angle) v1•v2 v1 x v2
0 0 1 1 0,0,0
90 1 0 0 unit len
180 0 -1 -1 0,0,0
270 -1 0 0 unit len

Quaternion Result

One approach might be to define a quaternion which, when multiplied by a vector, rotates it:

p2=q * p1

This almost works as explained on this page.

However, to rotate a vector, we must use this formula:

p2=q * p1 * conj(q)

where:

  • p2 = is a vector representing a point after being rotated
  • q = is a quaternion representing a rotation.
  • p1= is a vector representing a point before being rotated

This is a bit messy to solve for q, I am therefore grateful to minorlogic for the following approach which converts the axis angle result to a quaternion:

The axis angle can be converted to a quaternion as follows, let x,y,z,w be elements of quaternion, these can be expressed in terms of axis angle as explained here.

angle = arcos(v1•v2/ |v1||v2|)
axis = norm(v1 x v2)
s = sin(angle/2)
x = axis.x *s
y = axis.y *s
z = axis.z *s
w = cos(angle/2)

We can use this half angle trig formula on this page: sin(angle/2) = 0.5 sin(angle) / cos(angle/2)

so substituting in quaternion formula gives:
s = 0.5 sin(angle) / cos(angle/2)
x = norm(v1 x v2).x *s
y = norm(v1 x v2).y *s
z = norm(v1 x v2).z *s
w = cos(angle/2)

multiply x,y,z and w by 2* cos(angle/2) (this will de normalise the quaternion but we can always normalise later)

x = norm(v1 x v2).x * sin(angle)
y = norm(v1 x v2).y * sin(angle)
z = norm(v1 x v2).z * sin(angle)
w = 2 * cos(angle/2) * cos(angle/2)

now substitute half angle trig formula on this page: cos(angle/2) = sqrt(0.5*(1 + cos (angle)))

x = norm(v1 x v2).x * sin(angle)
y = norm(v1 x v2).y * sin(angle)
z = norm(v1 x v2).z * sin(angle)
w = 1 + cos (angle)

because |v1 x v2| = |v1||v2| sin(angle) we can normalise (v1 x v2) by dividing it with sin(angle),

also apply v1•v2 = |v1||v2| cos(angle)so,

x = (v1 x v2).x / |v1||v2|
y = (v1 x v2).y/ |v1||v2|
z = (v1 x v2).z/ |v1||v2|
w = 1 + v1•v2 / |v1||v2|

If v1 and v2 are already normalised then |v1||v2|=1 so,

x = (v1 x v2).x
y = (v1 x v2).y
z = (v1 x v2).z
w = 1 + v1•v2

If v1 and v2 are not already normalised then multiply by |v1||v2| gives:

x = (v1 x v2).x
y = (v1 x v2).y
z = (v1 x v2).z
w = |v1||v2| + v1•v2

Matrix Result

Using the quaternion to matrix conversion here we get:

1 - 2*qy2 - 2*qz2 2*qx*qy - 2*qz*qw 2*qx*qz + 2*qy*qw
2*qx*qy + 2*qz*qw 1 - 2*qx2 - 2*qz2 2*qy*qz - 2*qx*qw
2*qx*qz - 2*qy*qw 2*qy*qz + 2*qx*qw 1 - 2*qx2 - 2*qy2

so substituting the quaternion results above into the matrix we get:

1 - 2*(v1 x v2).y2 - 2*(v1 x v2).z2 2*(v1 x v2).x*(v1 x v2).y - 2*(v1 x v2).z*(1 + v1•v2) 2*(v1 x v2).x*(v1 x v2).z + 2*(v1 x v2).y*(1 + v1•v2)
2*(v1 x v2).x*(v1 x v2).y + 2*(v1 x v2).z*(1 + v1•v2) 1 - 2*(v1 x v2).x2 - 2*(v1 x v2).z2 2*(v1 x v2).y*(v1 x v2).z - 2*(v1 x v2).x*(1 + v1•v2)
2*(v1 x v2).x*(v1 x v2).z - 2*(v1 x v2).y*(1 + v1•v2) 2*(v1 x v2).y*(v1 x v2).z + 2*(v1 x v2).x*(1 + v1•v2) 1 - 2*(v1 x v2).x2 - 2*(v1 x v2).y2

Substituting the following expansions:

(v1 x v2).x = v1.y * v2.z - v2.y * v1.z
(v1 x v2).y = v1.z * v2.x - v2.z * v1.x
(v1 x v2).z = v1.x * v2.y - v2.x * v1.y
(v1 x v2).x2 = v1.y * v2.z * v1.y * v2.z + v2.y * v1.z * v2.y * v1.z - 2 * v2.y * v1.z * v1.y * v2.z
(v1 x v2).y2 = v1.z * v2.x * v1.z * v2.x + v2.z * v1.x * v2.z * v1.x - 2* v2.z * v1.x * v1.z * v2.x
(v1 x v2).z2 = v1.x * v2.y * v1.x * v2.y +v2.x * v1.y * v2.x * v1.y - 2 * v2.x * v1.y * v1.x * v2.y
v1•v2 = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z

This is getting far too complicated ! can anyone help me simplify this?

Thank you again to minorlogic who gave me the following solution:

Hi !
and i think can help in matrix version.

you can use :
http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToMatrix/index.htm

And will get some thing :

matrix33 RotAngonst vector3& from, const vector3& to )
{
from.norm();
to.norm();

vector3 vs = cross(from, to); // axis multiplied by sin

vector3 v(vs);
v.norm(); // axis of rotation
float ca = dot(from, to) ; // cos angle

vector3 vt(v*(1.0f - ca));

matrix33 rotM;
rotM.M11 = vt.x * v.x + ca;
rotM.M22 = vt.y * v.y + ca;
rotM.M33 = vt.z * v.z + ca;

vt.x *= v.y;
vt.z *= v.x;
vt.y *= v.z;

rotM.M12 = vt.x - vs.z;
rotM.M13 = vt.z + vs.y;
rotM.M21 = vt.x + vs.z;
rotM.M23 = vt.y - vs.x;
rotM.M31 = vt.z - vs.y;
rotM.M32 = vt.y + vs.x;
return rotM;
}

Code

axis-angle version
sfrotation angleBetween(sfvec3f v1,sfvec3f v2) {
float angle;
// turn vectors into unit vectors
n1 = v1.norm();
n2 = v2.norm();
angle = Math.acos( sfvec3f.dot(n1,n2) );
// if no noticable rotation is available return zero rotation
// this way we avoid Cross product artifacts
if( Math.abs(angle) < 0.0001 ) return new sfrotation( 0, 0, 1, 0 );
// in this case there are 2 lines on the same axis
if(Math.abs(angle)-Math.pi) < 0.001){
n1 = n1.Rotx( 0.5f );
// there are an infinite number of normals
// in this case. Anyone of these normals will be
// a valid rotation (180 degrees). so I rotate the curr axis by 0.5 radians this way we get one of these normals
}
sfvec3f axis = n1;
axis.cross(n2);
return new sfrotation(axis.x,axis.y,axis.z,angle);
}
quaternion version
/** note v1 and v2 dont have to be nomalised, thanks to minorlogic for telling me about this:
* http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/minorlogic.htm
*/
sfquat angleBetween(sfvec3f v1,sfvec3f v2) {
float d = sfvec3f.dot(v1,v2);
sfvec3f axis = v1;
axis.cross(v2);
float qw = (float)Math.sqrt(v1.len_squared()*v2.len_squared()) + d;
if (qw < 0.0001) { // vectors are 180 degrees apart
return (new sfquat(0,-v1.z,v1.y,v1.x)).norm;
}
sfquat q= new sfquat(qw,axis.x,axis.y,axis.z);
return q.norm();
}

matrix version

sfmatrix angleBetween(sfvec3f v1,sfvec3f v2) {
// turn vectors into unit vectors
n1 = v1.norm();
n2 = v2.norm(); 	sfvec3f vs = new sfvec3f(n1);
vs.cross(n2); // axis multiplied by sin	sfvec3f v = new sfvec3f(vs);
v = v.norm(); // axis of rotation
float ca = dot(n1, n2) ; // cos angle	sfvec3f vt = new sfvec3f(v);	vt.scale((1.0f - ca);	sfmatrix rotM = new sfmatrix();
rotM.m11 = vt.x * v.x + ca;
rotM.m22 = vt.y * v.y + ca;
rotM.m33 = vt.z * v.z + ca;	vt.x *= v.y;
vt.z *= v.x;
vt.y *= v.z;	rotM.m12 = vt.x - vs.z;
rotM.m13 = vt.z + vs.y;
rotM.m21 = vt.x + vs.z;
rotM.m23 = vt.y - vs.x;
rotM.m31 = vt.z - vs.y;
rotM.m32 = vt.y + vs.x;
return rotM;
}

see also code from minorlogic

posted on 2009-05-31 13:50 zmj 閱讀(1599) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美日韩国产大片| 国产精品日韩在线| 亚洲激情视频| 欧美国产视频在线观看| 免费成人av在线看| 亚洲精品一级| 在线亚洲国产精品网站| 国产精品入口夜色视频大尺度| 一本色道久久精品| 亚洲少妇诱惑| 国内在线观看一区二区三区| 蜜桃av综合| 欧美激情四色| 欧美在线免费| 老鸭窝亚洲一区二区三区| 亚洲免费激情| 亚洲欧美日韩直播| 亚洲精品乱码久久久久久| 日韩一级精品视频在线观看| 国产亚洲成av人在线观看导航| 蜜臀久久99精品久久久画质超高清| 欧美精品久久99| 欧美一区二区三区喷汁尤物| 麻豆久久婷婷| 亚洲欧美国产高清| 久久在线免费观看视频| 亚洲五月六月| 免费不卡在线观看av| 亚洲欧美日韩国产一区二区三区| 欧美在线综合| 中文国产成人精品| 久久婷婷久久| 欧美亚洲综合在线| 欧美屁股在线| 免费日韩视频| 国产亚洲亚洲| 亚洲视频一区| 99国内精品久久| 久久美女性网| 久久精品五月婷婷| 国产精品国产三级国产| 亚洲福利视频网站| 国产一区二区三区在线观看网站 | 国产精品视频免费| 亚洲第一二三四五区| 国产欧美日韩视频一区二区三区| 欧美国产日韩视频| 国内一区二区在线视频观看| 在线中文字幕日韩| 在线中文字幕一区| 欧美大色视频| 欧美sm极限捆绑bd| 永久555www成人免费| 午夜一区不卡| 欧美中文字幕视频| 国产精品普通话对白| 在线综合亚洲欧美在线视频| 亚洲靠逼com| 欧美国产精品专区| 亚洲国产成人av| 亚洲欧洲在线观看| 美女图片一区二区| 欧美福利网址| 亚洲人成毛片在线播放| 老司机成人在线视频| 嫩草国产精品入口| 亚洲精品一区二区三区福利| 老司机午夜精品视频在线观看| 老司机凹凸av亚洲导航| 国产一区二区在线观看免费| 小黄鸭精品aⅴ导航网站入口| 欧美在线免费| 国内精品模特av私拍在线观看| 欧美在线播放| 欧美成人小视频| 亚洲精品永久免费| 欧美日韩精品免费观看| 9国产精品视频| 午夜在线成人av| 国内精品模特av私拍在线观看| 久久精品91| 欧美激情麻豆| 亚洲图片欧洲图片av| 国产精品午夜春色av| 久久国产精品99精品国产| 你懂的国产精品| 亚洲伦理在线免费看| 欧美三级网页| 久久国产精品久久久| 欧美二区乱c少妇| 亚洲小视频在线| 国产性天天综合网| 欧美va日韩va| 亚洲在线观看| 欧美高清成人| 亚洲欧美日韩国产综合精品二区| 国产精品人人爽人人做我的可爱 | 久久激情中文| 亚洲国产综合91精品麻豆| 欧美精品在线观看一区二区| 一区二区三区高清在线观看| 久久精品在线视频| 亚洲三级网站| 国产精品一区一区三区| 久久这里只有| 亚洲一区二区三区精品在线观看 | 美女视频黄a大片欧美| 99精品欧美| 国模精品娜娜一二三区| 欧美精品国产一区二区| 欧美伊久线香蕉线新在线| 亚洲激情电影在线| 久久久精品国产免大香伊| 一本一本久久a久久精品综合妖精| 国产欧美日韩中文字幕在线| 欧美大香线蕉线伊人久久国产精品| 亚洲综合精品四区| 亚洲欧洲日本专区| 免费一级欧美在线大片| 亚洲欧美制服另类日韩| 日韩亚洲欧美精品| 亚洲第一主播视频| 国产亚洲福利一区| 国产精品久久久| 欧美精品日韩一区| 蜜桃视频一区| 久久精品国产免费看久久精品| 一本大道久久精品懂色aⅴ | 西西裸体人体做爰大胆久久久| 最新国产成人在线观看| 狠狠综合久久| 国产一区91| 国产欧美在线观看| 国产精品免费观看在线| 欧美日韩国产大片| 欧美精品一区二区高清在线观看| 久久婷婷人人澡人人喊人人爽| 性伦欧美刺激片在线观看| 亚洲视频每日更新| 一区二区三区视频在线| 一本到12不卡视频在线dvd| 亚洲精品一区二区三区婷婷月 | 亚洲欧美精品| 一区二区三区四区在线| 日韩网站在线观看| 一本色道精品久久一区二区三区| 亚洲国产欧美一区二区三区久久| 狠狠综合久久| 亚洲电影自拍| 亚洲福利视频二区| 亚洲精品在线免费观看视频| 亚洲黄色精品| 日韩一级免费观看| 亚洲视频观看| 亚洲欧美国产高清va在线播| 性久久久久久久久久久久| 欧美一区二区高清| 久久男人av资源网站| 女人天堂亚洲aⅴ在线观看| 欧美电影免费观看| 亚洲精品小视频| 在线一区二区日韩| 欧美一级视频免费在线观看| 久久精品123| 欧美精品成人一区二区在线观看 | 免费看成人av| 欧美日韩一级片在线观看| 欧美无乱码久久久免费午夜一区| 国产精品久久久久77777| 国产日韩欧美高清免费| 亚洲国产精品va在线看黑人| 一个人看的www久久| 欧美一区二区三区免费看| 久久综合给合久久狠狠色| 亚洲黄色一区二区三区| 亚洲一区二区免费看| 久久久之久亚州精品露出| 欧美激情一区二区三区成人| 国产精品美女久久久| 禁久久精品乱码| 中文亚洲字幕| 久久综合五月| 在线亚洲一区二区| 久久网站热最新地址| 国产精品sm| 亚洲电影免费| 欧美伊人影院| 亚洲精品裸体| 久久久99爱| 国产精品夜夜夜| 亚洲美女黄色| 久久在线免费观看| 亚洲视频网在线直播| 欧美成人中文| 精品电影在线观看| 欧美在线视频一区二区| 亚洲精品美女在线| 乱中年女人伦av一区二区| 国产日韩欧美麻豆| 亚洲一区视频|