CGTalk > Software > Autodesk 3ds max > 3dsMax SDK and MaxScript
Login register
reply share thread « Previous Thread | Next Thread »
 
Thread Tools Search this Thread Display Modes
Old 11-19-2005, 01:36 AM   #1
prettyPixel
Know-it-All
cg artist
 
Join Date: Feb 2005
Posts: 302
Geometrical calculations : points, lines, planes : intersections, distances, angles

I am trying to collect the most usual functions for the geometrical calculations :
Intersection point, projection, distance, angle, ...
Of course there are many Web sites about this, but I do not seek the mathematical formulas but functions 'ready-to-use' in maxscript.

I start by giving the formulas which I know already.
If you know other formulas do not hesitate to add them.



Let's say that you have 4 points:
pA=[ax,ay,az]
pB=[bx,by,bz]
pC=[cx,cy,cz]
pD=[dx,dy,dz]
With these points you can define a vector, a line, a plane.




Vector:

vAB=pB-pA
vAC=pC-pA
vCD=pD-pC


Vector Normalization : length vector = 1.0
Code:
normalize vAB

see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource.
http://mathworld.wolfram.com/NormalizedVector.html


Cross Product :
Code:
cross vAB vAC

see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource.
http://mathworld.wolfram.com/CrossProduct.html


Dot Product :
Code:
dot vAB vAC dot (normalize vAB) (normalize vAC)

see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource.
http://mathworld.wolfram.com/DotProduct.html




line:

A line can be defined :
- by 2 points
- or by 1 point and 1 vector


Middle Point M of AB:
Code:
fn middlePoint pA pB = ((pA+pB)/2.0)



Point along AB : the point at 25% between A and B:
Code:
fn alongPoint pA pB prop = (pA+((pB-pA)*prop))





plane:

A plane can be defined :
- by 3 points
- or by 1 point and 2 vectors
- or by 1 point and 1 vector (the normal vector)


Normal Vector : the vector perpendicular to the plane:
Code:
normalVector=normalize (cross vAB vAC)

see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource.
http://mathworld.wolfram.com/NormalVector.html




line and point:

Point-Line Projection : find the point on the line AB which is the projection of the point C:
Code:
fn pointLineProj pA pB pC = ( local vAB=pB-pA local vAC=pC-pA local d=dot (normalize vAB) (normalize vAC) (pA+(vAB*(d*(length vAC/length vAB)))) )



Point-Line Distance : the distance between the line AB and the point C:
Code:
fn pointLineDist2 pA pB pC = ( local vAB=pB-pA local vAC=pC-pA (length (cross vAB vAC))/(length vAB) )

or
Code:
d=distance pC (pointLineProj pA pB pC)

see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource.
http://mathworld.wolfram.com/Point-...imensional.html


Point-Line Inclusion : is this point on the line ?
Code:
fn isPointLine pA pB pC tol = ( local vAB=pB-pA local vAC=pC-pA local d=1.0-abs(dot (normalize vAB) (normalize vAC)) if d<=tol then true else false )

or Point-Line Distance <= tolerance distance




(to be continued)
 
Old 11-19-2005, 03:20 AM   #2
Gibbz
Game Artist
 
Gibbz's Avatar
portfolio
Bronson Mathews
Game Artist
Australia
 
Join Date: Dec 2002
Posts: 1,090
Send a message via ICQ to Gibbz
nice stuff

takes me back to highschool math days....
__________________

 
Old 03-29-2006, 06:43 AM   #3
prettyPixel
Know-it-All
cg artist
 
Join Date: Feb 2005
Posts: 302
line and line

line and line:

Line-Line Intersection : intersection of two lines AB and CD:
Code:
fn lineLineIntersect pA pB pC pD = ( local a=pB-pA local b=pD-pC local c=pC-pA local cross1 = cross a b local cross2 = cross c b pA + ( a*( (dot cross2 cross1)/((length cross1)^2) ) ) )

or by using vectors:
Code:
fn lineLineIntersect pA a pB b = ( local c=pB-pA local cross1 = cross a b local cross2 = cross c b pA + ( a*( (dot cross2 cross1)/((length cross1)^2) ) ) )

see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource.
http://mathworld.wolfram.com/Line-LineIntersection.html




Line-Line Distance : distance between two skew lines
Code:
fn lineLineDist pA a pB b = ( local c=pB-pA local crossAB=cross a b abs (dot c crossAB) / (length crossAB) )


see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource.
http://mathworld.wolfram.com/Line-LineDistance.html




Line-Line // Distance : distance between two parallel lines
use "Point-Line Distance" : pA-pB is the first line and pC is a point of the second line




Line-Line Angle : angle between two lines:
Code:
fn getVectorsAngle vAB vCD = ( acos (dot (normalize vAB) (normalize vCD)) )

or
Code:
fn lineLineAngle pA pB pC pD = ( local vAB=pB-pA local vCD=pD-pC local angle=acos (dot (normalize vAB) (normalize vCD)) if angle<90.0 then angle else (180.0-angle) )

see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource.
http://mathworld.wolfram.com/Line-LineAngle.html
 
Old 03-30-2006, 07:00 AM   #4
prettyPixel
Know-it-All
cg artist
 
Join Date: Feb 2005
Posts: 302
plane and point

plane and point:

Point-Plane Projection : find the point on the plane ABC which is the projection of the point D
Code:
fn pointPlaneProj pA pB pC pD = ( local nABC=normalize (cross (pB-pA) (pC-pA)) pD+((dot (pA-pD) nABC)*nABC) )




Point-Plane Distance : find the distance between a plane and a point
Code:
fn pointPlaneDist pA pB pC pD = ( local nABC=normalize (cross (pB-pA) (pC-pA)) length ((dot (pA-pD) nABC)*nABC) )

see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource.
http://mathworld.wolfram.com/Point-PlaneDistance.html

Last edited by prettyPixel : 03-30-2006 at 07:05 AM.
 
Old 03-30-2006, 07:03 AM   #5
prettyPixel
Know-it-All
cg artist
 
Join Date: Feb 2005
Posts: 302
plane and line

plane and line:

Line-Plane Intersection : intersection between a line and a plane
Code:
fn planeLineIntersect planePoint planeNormal linePoint lineVector = ( local lineVector=normalize lineVector local d1=dot (planePoint-linePoint) planeNormal local d2=dot lineVector planeNormal if abs(d2)<.0000000754 then ( if abs(d1)>.0000000754 then 0 else -1 ) else ( linePoint + ( (d1/d2)*lineVector ) ) )

This script is based on an original script of Joshua Newman.
return 0 for parrallel (no intersections) results
and -1 for co-incident (infinate) results
http://forums.cgsociety.org/showthread.php?t=290255
see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource.
http://mathworld.wolfram.com/Line-P...tersection.html



Line-Plane Distance : distance between a plane and a line //
use "Point-Plane Distance" : where 'point' is a point of the line



Vector perpendicular to a line into a Plane : the perpendicular of AB into the plane ABC
Code:
vectorPerp=cross vectorAB (cross vectorAB vectorAC) -- or vectorPerp=cross (cross vectorAB vectorAC) vectorAB

see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource.
http://mathworld.wolfram.com/Perpendicular.html
 
Old 03-30-2006, 10:30 AM   #6
j-man
in 3D!
 
j-man's Avatar
portfolio
Joshua Newman
Director
MESHROOM
London, United Kingdom
 
Join Date: Apr 2005
Posts: 1,345
Send a message via MSN to j-man
Quote:
Originally Posted by prettyPixel
plane and line:

Line-Plane Intersection : intersection between a line and a plane
Code:
fn planeLineIntersect planePoint planeNormal linePoint lineVector = ( local lineVector=normalize lineVector local d1=dot (planePoint-linePoint) planeNormal local d2=dot lineVector planeNormal if abs(d2)<.0000000754 then ( if abs(d1)>.0000000754 then 0 else -1 ) else ( linePoint + ( (d1/d2)*lineVector ) ) )

This script is based on an original script of Joshua Newman.
return 0 for parrallel (no intersections) results
and -1 for co-incident (infinate) results
http://forums.cgsociety.org/showthread.php?t=290255
see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource.
http://mathworld.wolfram.com/Line-P...tersection.html



Line-Plane Distance : distance between a plane and a line //
use "Point-Plane Distance" : where 'point' is a point of the line



Vector perpendicular to a line into a Plane : the perpendicular of AB into the plane ABC
Code:
vectorPerp=cross vectorAB (cross vectorAB vectorAC) -- or vectorPerp=cross (cross vectorAB vectorAC) vectorAB

see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource.
http://mathworld.wolfram.com/Perpendicular.html



Thanks for the aknowelgement PP.

Still can't get around using the tolerence then eh?

Great resource BTW, keep up the good work!

J
__________________
www.joshuanewman.net
 
Old 03-31-2006, 01:58 AM   #7
JHaywood
Know-it-All
 
JHaywood's Avatar
portfolio
James Haywood
Senior Technical Artist
Bungie Studios
Seattle, USA
 
Join Date: Jun 2005
Posts: 395
This is great, thanks.
__________________
Website
Blog
 
Old 03-31-2006, 02:13 AM   #8
erilaz
Stealthy Tea Monkey
 
erilaz's Avatar
portfolio
Martin Brennand
VFX Artist
Melbourne, Australia
 
Join Date: Jun 2002
Posts: 13,756
This should be a sticky!
__________________
"There Really is No Secret"
Martin Brennand - mocha Product Manager - Imagineer Systems
 
Old 03-31-2006, 07:00 AM   #9
prettyPixel
Know-it-All
cg artist
 
Join Date: Feb 2005
Posts: 302
Thanks guys

jman: The tolerance seems to work for me, but I use rather the function in cases where the line is not parallel to the plan. Can you give an example where the tolerance is an issue?

Well, the continuation:
 
Old 03-31-2006, 07:08 AM   #10
prettyPixel
Know-it-All
cg artist
 
Join Date: Feb 2005
Posts: 302
plane and plane

plane and plane:

Plane-Plane Intersection : find the line which is the intersection of 2 planes
p1 : a point of the plane 1
n1 : the normal of the plane 1
Code:
fn planePlaneIntersect p1 n1 p2 n2 = ( -- n1, n2 are normalized local lineVector = cross n1 n2 local proj1=(dot n1 p1)*n1 local proj2=(dot n2 p2)*n2 local perp1=cross n1 (normalize lineVector) local perp2=cross n2 (normalize lineVector) local cr = cross (proj2-proj1) perp2 local intersectionPoint = proj1 + (perp1*( (dot cr lineVector) / ((length lineVector)^2)) ) ray intersectionPoint lineVector )

I think that it is not the easiest solution. If you know a better means...
see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource.
http://mathworld.wolfram.com/Plane-...tersection.html

Plane perpendicular to a Plane and a Line : find the plane which is perpendicular to the plane n and to the line AB
It's the plane ABC where C=A+n
see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource.
http://mathworld.wolfram.com/ParallelPlanes.html

Plane-Plane Angle : find the angle between two planes n1 and n2 (normals)
Code:
fn getVectorsAngle n1 n2 = ( acos (dot (normalize n1) (normalize n2)) )
 
Old 04-03-2006, 10:17 AM   #11
j-man
in 3D!
 
j-man's Avatar
portfolio
Joshua Newman
Director
MESHROOM
London, United Kingdom
 
Join Date: Apr 2005
Posts: 1,345
Send a message via MSN to j-man
Quote:
Originally Posted by prettyPixel
Can you give an example where the tolerance is an issue?


Yes, if the results are parrallel or co-incident the rounding error tells the function from telling which!

J
__________________
www.joshuanewman.net

Last edited by j-man : 04-03-2006 at 12:41 PM.
 
Old 04-05-2006, 04:48 PM   #12
JHaywood
Know-it-All
 
JHaywood's Avatar
portfolio
James Haywood
Senior Technical Artist
Bungie Studios
Seattle, USA
 
Join Date: Jun 2005
Posts: 395
Hey PP, just wondering if you're done adding to this thread. I want to put together a word doc containing all these functions, but wanted to wait until you were fiinished.
__________________
Website
Blog
 
Old 04-07-2006, 01:03 AM   #13
JHaywood
Know-it-All
 
JHaywood's Avatar
portfolio
James Haywood
Senior Technical Artist
Bungie Studios
Seattle, USA
 
Join Date: Jun 2005
Posts: 395
I've got a math question, and this seems like a good place to ask. I tried digging through Mathworld, but it's way over my head.

I'd like to be able to find a point on an arc defined by the end point of an object rotated around it's pivot, along a specific axis. For example, let's say there's a bone object with a end bone as it's child. If I rotate the parent bone around it's Y axis 10 degrees, the end bone is now at a new position. What's the formula for finding that position?

This page at Mathworld describes what I'm talking about, except in 2D: http://mathworld.wolfram.com/Arc.html
__________________
Website
Blog
 
Old 04-07-2006, 02:52 AM   #14
erilaz
Stealthy Tea Monkey
 
erilaz's Avatar
portfolio
Martin Brennand
VFX Artist
Melbourne, Australia
 
Join Date: Jun 2002
Posts: 13,756
Quote:
Originally Posted by JHaywood
I've got a math question, and this seems like a good place to ask. I tried digging through Mathworld, but it's way over my head.

I'd like to be able to find a point on an arc defined by the end point of an object rotated around it's pivot, along a specific axis. For example, let's say there's a bone object with a end bone as it's child. If I rotate the parent bone around it's Y axis 10 degrees, the end bone is now at a new position. What's the formula for finding that position?

This page at Mathworld describes what I'm talking about, except in 2D: http://mathworld.wolfram.com/Arc.html


That should be a straight trig calculation where (skeleton code):
Code:
r = $bone01.length theta = (rotation of your bone) phi = 90 - theta y = sin(theta) * r x = cos(theta) * r z = sin(theta) * r * cos(phi)


I'm a little hazy on the Z phi stuff, so somebody correct me if i'm wrong!
__________________
"There Really is No Secret"
Martin Brennand - mocha Product Manager - Imagineer Systems

Last edited by erilaz : 04-07-2006 at 03:58 AM.
 
Old 04-07-2006, 06:54 AM   #15
prettyPixel
Know-it-All
cg artist
 
Join Date: Feb 2005
Posts: 302
Quote:
Originally Posted by JHaywood
Hey PP, just wondering if you're done adding to this thread. I want to put together a word doc containing all these functions, but wanted to wait until you were fiinished.


I think that the essential formulae are there but anyone can add some more. The possibilities are illimited.
Here is an example: this script discovers if a line intersects a sphere.
It returns true or false.

Code:
fn lineSphereIntersection sphereCenter sphereRadius linePoint lineVector = ( local nLineVector= normalize lineVector local projPoint=linePoint+((dot (sphereCenter-linePoint) nLineVector)*nLineVector) local dist=distance sphereCenter projPoint if dist>sphereRadius then false else true )

I use the projection of a point on a plane which passes by the sphere center. If the distance between this point and the center is lower or equal to the sphere radius then the line intersects the sphere. It's based on the function 'Point-Plane Projection' .

Another way of going farther is to write the same functions but on objects like triangular faces or segments. For example find the position of a line that intersects a mesh face (not a plane). The case is more restricted.
 
reply share thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 02:24 AM.


Powered by vBulletin
Copyright ©2000 - 2016, Jelsoft Enterprises Ltd.