Difference between revisions of "User:Nexii Malthus/GeometricLib"

From Second Life Wiki
Jump to navigation Jump to search
(Removing content. How do I ask a page to be deleted btw?)
 
(21 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{RightToc|clear:right;}}


Sandbox playground for Nexii Malthus for Geometric Library ideas and possible improvements.
Ideas..
What does a function do? Whats the point of it?
What can it do for me? What can I use for?
*Restrictions?
**Hmm, might require too much info
***On a technical topic, is it better to give too much information or too little?
****True true, it is quite a technical concept. But I'd rather have it up to the reader if they want it. Know how I can do collapsible points of information? I believe there was some sort of trick that I have seen been done before so that a button could be pressed to show/hide paragraphs of information.
*****That's on my list of things to poke LL about. We really need collapsible sections and the citation extension.
*Abilities?
*Examples?
**Example usage would be good
*Explanations?
How could I condense it precisely and clearly what each function does?
Template structure?:
*Function Name
**Explanation
**LSL Code
**Input
**Output
**Example?
<!-- {|class="prettytable" style="background-color:#eeeeff;" cellpadding="3" cellspacing="0" !-->
<!-- {|{{Prettytable}} !-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #f9f9f9; border-collapse: collapse table-layout: fixed min-width: 100%"
! Heading A
! Heading B
! Heading C
|-
| Row A Value A
| Row A Value B
| Row A Value C
|-
| Row B Value A
| Row B Value B
| Row B Value C
|-
| Row C Value A
| Row C Value B
| Row C Value C
|-
| Row D Value A
| Row D Value B
| Row D Value C
|-
| Row E Value A
| Row E Value B
| Row E Value C
|}
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #f9f9f9; border-collapse: collapse" table-layout: fixed
|Test 1
Test 2
Test 3
Test 4
|}
== Line Functions ==
{|{{Prettytable}}|
=== Line Nearest Point ===
Calculates the vector from a point to the closest point on a line
<lsl>
vector gLXdV(vector O,vector D,vector A){
    return (O-A)-((O-A)*D)*D;}
</lsl>
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #f9f9f9; border-collapse: collapse"
! Input
! Description
|-
| vector O
| Origin of Line
|-
| vector D
| Direction of Line
|-
| vector A
| Origin of Point
|-
!Output
!Description
|-
| return vector gLXdV
| Returns origin of closest point on Line to Point
|}
|}
{|{{Prettytable}}|
=== Line Nearest Point, Distance ===
Calculates distance of this vector, but faster on it's own
<lsl>
float gLXdZ(vector O,vector D,vector A){
    return llSqrt(CP((A-O),D)*CP((A-O),D));}
</lsl>
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #f9f9f9; border-collapse: collapse"
! Input
! Description
|-
| vector O
| Origin of Line
|-
| vector D
| Direction of Line
|-
| vector A
| Origin of Point
|-
!Output
!Description
|-
| return float gLXdZ
| Returns numerical distance from Line to Point
|}
|}
{|{{Prettytable}}|
=== Line and Line, Vector ===
Shortest vector of two lines
<lsl>
vector gLLdV(vector O1,vector D1,vector O2,vector D2){
    return Project3D( (O2-O1), CP(D1,D2) );}
</lsl>
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #f9f9f9; border-collapse: collapse"
! Input
! Description
|-
| vector O1
| Origin of Line 1
|-
| vector D1
| Direction of Line 1
|-
| vector O2
| Origin of Line 2
|-
| vector D2
| Direction of Line 2
|-
!Output
!Description
|-
| return vector gLLdV
| Returns shortest vector between the two lines
|}
|}
{|{{Prettytable}}|
=== Line and Line, Distance ===
Returns the distance between two lines
<lsl>
float gLLdZ(vector O1,vector D1,vector O2,vector D2){
    vector A = CP(D1,D2);float B = llVecMag(A);A = <A.x/B,A.y/B,A.z/B>;
    return (O2-O1) * A;}
</lsl>
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #f9f9f9; border-collapse: collapse"
! Input
! Description
|-
| vector O1
| Origin of Line 1
|-
| vector D1
| Direction of Line 1
|-
| vector O2
| Origin of Line 2
|-
| vector D2
| Direction of Line 2
|-
!Output
!Description
|-
| return float gLLdZ
| Returns numerical distance between the two lines
|}
|}
{|{{Prettytable}}|
=== Line and Line, Nearest point ===
Closest point of two lines
<lsl>
vector gLLnX(vector O1,vector D1,vector O2,vector D2){
    vector nO1 = < O1*D1, O1*D2, 0>;
    vector nO2 = < O2*D1, O2*D2, 0>;
    vector nD1 = < D1*D1, O1*D2, 0>;
    vector nD2 = < O2*D1, O2*D2, 0>;
   
    float t = ( nD2.x*nD1.y - nD1.x*nD2.y );
   
    t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t;
   
    return O1 + D1*t;}
</lsl>
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #f9f9f9; border-collapse: collapse"
! Input
! Description
|-
| vector O1
| Origin of Line 1
|-
| vector D1
| Direction of Line 1
|-
| vector O2
| Origin of Line 2
|-
| vector D2
| Direction of Line 2
|-
!Output
!Description
|-
| return vector gLLnX
| Returns closest point between the two lines
|}
|}
{|{{Prettytable}}|
=== Line and Line, two nearest points along both lines ===
Two closest points of two lines
<lsl>
vector X1;vector X2;
gLLnnXX(vector O1,vector D1,vector O2,vector D2){
    vector nO1 = < O1*D1, O1*D2, 0>;
    vector nO2 = < O2*D1, O2*D2, 0>;
    vector nD1 = < D1*D1, O1*D2, 0>;
    vector nD2 = < O2*D1, O2*D2, 0>;
   
    float t = ( nD2.x*nD1.y - nD1.x*nD2.y );
   
    t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t;
   
    X1 = O1 + D1*t;
    X2 = X1 + CP(nD1,nD2);}
</lsl>
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #f9f9f9; border-collapse: collapse"
! Input
! Description
|-
| vector O1
| Origin of Line 1
|-
| vector D1
| Direction of Line 1
|-
| vector O2
| Origin of Line 2
|-
| vector D2
| Direction of Line 2
|-
!Output
!Description
|-
| vector X1
| Closest point on line 1 to line 2
|-
| vector X2
| Closest point on line 2 to line 1
|}
|}
{|{{Prettytable}}|
=== Line and Line, two nearest points with vector and distance ===
Computes two closest points of two lines, vector and distance
<lsl>
vector X1;vector X2;vector V1;float Z1;
gLLnnXXVZ(vector O1,vector D1,vector O2,vector D2){
    vector nO1 = < O1*D1, O1*D2, 0>;
    vector nO2 = < O2*D1, O2*D2, 0>;
    vector nD1 = < D1*D1, O1*D2, 0>;
    vector nD2 = < O2*D1, O2*D2, 0>;
   
    float t = ( nD2.x*nD1.y - nD1.x*nD2.y );
   
    t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t;
   
    X1 = O1 + D1*t;
    X2 = X1 + CP(nD1,nD2);
    V1 = CP(nD1,nD2);
    Z1 = llVecMag(V1);}
</lsl>
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #f9f9f9; border-collapse: collapse"
! Input
! Description
|-
| vector O1
| Origin of Line 1
|-
| vector D1
| Direction of Line 1
|-
| vector O2
| Origin of Line 2
|-
| vector D2
| Direction of Line 2
|-
!Output
!Description
|-
| vector X1
| Closest point on line 1 to line 2
|-
| vector X2
| Closest point on line 2 to line 1
|-
| vector V1
| Direction vector of line 1 to line 2
|-
| float Z1
| Numerical distance of line 1 to line 2
|}
|}
== Geometric Library Code ==
This is the script in its entirety.
<lsl>//===================================================//
//              Geometric Library 1.0              //
//              "May 4 2008", "2:24:30"            //
//  Copyright (C) 2008, Nexii Malthus (cc-by)        //
//    http://creativecommons.org/licenses/by/3.0/    //
//===================================================//
vector CP(vector A,vector B){
    return A % B;}
vector Project3D(vector A,vector B){
    vector proj;
    proj.x = ( (A*B) / (B.x*B.x + B.y*B.y + B.z*B.z) ) * B.x;
    proj.y = ( (A*B) / (B.x*B.x + B.y*B.y + B.z*B.z) ) * B.y;
    proj.z = ( (A*B) / (B.x*B.x + B.y*B.y + B.z*B.z) ) * B.z;
    return proj;}
// POINT
float gXXdZ(vector A,vector B){
    // Distance P2P.
    return llVecDist(A,B);}
vector gXXdV(vector A,vector B){
    // Vector to move from P2P.
    return A-B;}
// LINE
vector gLXdV(vector O,vector D,vector A){
    // Calculates the vector from a point to the closest point on a line
    return (O-A)-((O-A)*D)*D;}
float gLXdZ(vector O,vector D,vector A){
    // Calculates distance of this vector, but faster on it's own
    return llSqrt(CP((A-O),D)*CP((A-O),D));}
vector gLLdV(vector O1,vector D1,vector O2,vector D2){
    // Shortest vector of two lines
    return Project3D( (O2-O1), CP(D1,D2) );}
float gLLdZ(vector O1,vector D1,vector O2,vector D2){
    // Returns the distance between two lines
    vector A = CP(D1,D2);float B = llVecMag(A);A = <A.x/B,A.y/B,A.z/B>;
    return (O2-O1) * A;}
vector gLLnX(vector O1,vector D1,vector O2,vector D2){
    // Closest point of two lines
    vector nO1 = < O1*D1, O1*D2, 0>;
    vector nO2 = < O2*D1, O2*D2, 0>;
    vector nD1 = < D1*D1, O1*D2, 0>;
    vector nD2 = < O2*D1, O2*D2, 0>;
   
    float t = ( nD2.x*nD1.y - nD1.x*nD2.y );
   
    t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t;
   
    return O1 + D1*t;}
vector X1;vector X2;vector V1;float Z1;
gLLnnXX(vector O1,vector D1,vector O2,vector D2){
    // Two closest points of two lines
    vector nO1 = < O1*D1, O1*D2, 0>;
    vector nO2 = < O2*D1, O2*D2, 0>;
    vector nD1 = < D1*D1, O1*D2, 0>;
    vector nD2 = < O2*D1, O2*D2, 0>;
   
    float t = ( nD2.x*nD1.y - nD1.x*nD2.y );
   
    t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t;
   
    X1 = O1 + D1*t;
    X2 = X1 + CP(nD1,nD2);}
gLLnnXXVZ(vector O1,vector D1,vector O2,vector D2){
    // Computes two closest points of two lines, vector and distance
    vector nO1 = < O1*D1, O1*D2, 0>;
    vector nO2 = < O2*D1, O2*D2, 0>;
    vector nD1 = < D1*D1, O1*D2, 0>;
    vector nD2 = < O2*D1, O2*D2, 0>;
   
    float t = ( nD2.x*nD1.y - nD1.x*nD2.y );
   
    t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t;
   
    X1 = O1 + D1*t;
    X2 = X1 + CP(nD1,nD2);
    V1 = CP(nD1,nD2);
    Z1 = llVecMag(V1);}
// PLANE
float gPXdZ(vector Pn,float Pd,vector A){
    // Finds distance of a point from a plane
    return A * Pn + Pd;}
vector gPXdV(vector Pn,float Pd,vector A){
    // Finds vector that points from point to nearest on plane
    return -(Pn * A + Pd)*Pn;}
vector gPXnX(vector Pn,float Pd,vector A){
    // Finds closest point on plane given point
    return A - (Pn * A + Pd) * Pn;}
float gPRxZ(vector Pn,float Pd,vector O,vector D){
    // Finds distance to intersection of plane along ray
    return -( ( (Pn*D)+(Pn*O) ) / (Pn*D) );}
    //return -( (Pn*D)/(Pn*O+Pd) );}
vector gPRdV(vector Pn,float Pd,vector O,vector D){
    // Finds distance vector along a ray to a plane
    return D * gPRxZ(Pn,Pd,O,D);}
    //return -( (Pn*D)/(Pn*O+Pd) )*D;}
vector gPRxX(vector Pn,float Pd,vector O,vector D){
    // Finds intersection point along a ray to a plane
    return O + gPRdV(Pn,Pd,O,D);}
vector gPLxX(vector Pn,float Pd,vector O,vector D){
    // Finds interesection point of a line and a plane
    return O -( (Pn*D)/(Pn*O+Pd) )*D;}
vector oO;vector oD;
gPPxL(vector Pn,float Pd,vector Qn,float Qd){
    // Finds line of intersection of two planes
    oD = CP(Pn,Qn)/llVecMag(CP(Pn,Qn));
    vector Cross = CP(CP(Pn,Qn),Pn);
    vector Bleh = (-Pd*Pn);
    oO = Bleh - (Qn*Cross)/(Qn*Bleh+Qd)*Cross/llVecMag(Cross);}
float gRXpZ(vector O,vector D,vector A){
    // Finds projected distance of a point along a ray
    return (A-O)*D;}
gPRpR(vector Pn,float Pd,vector O,vector D){
    // Projects a ray onto a plane
    oO = O - (Pn * O + Pd) * Pn;
    vector t = llVecNorm( D - Project3D(D,Pn) );t = <1.0/t.x,1.0/t.y,1.0/t.z>;
    oD = CP(Pn,t);}
// SPHERE
vector gSRxX(vector Sp, float Sr, vector Ro, vector Rd){
    float t;Ro = Ro - Sp;
    //vector RayOrg = llDetectedPos(x) - llGetPos();
    if(Rd == ZERO_VECTOR) return ZERO_VECTOR;
   
    float a = Rd * Rd;
    float b = 2 * Rd * Ro;
    float c = (Ro * Ro)  - (Sr * Sr);
   
    float disc = b * b - 4 * a * c;
   
    if(disc < 0) return ZERO_VECTOR;
   
    float distSqrt = llSqrt(disc);
    float q;
   
    if(b < 0)
        q = (-b - distSqrt)/2.0;
    else
        q = (-b + distSqrt)/2.0;
   
    float t0 = q / a;
    float t1 = c / q;
   
    if(t0 > t1){
        float temp = t0;
        t0 = t1;
        t1 = temp;
    }
   
    if(t1 < 0) return ZERO_VECTOR;
   
    if(t0 < 0)
        t = t1;
    else
        t = t0;
   
    return Ro + (t * Rd);
}
integer gSRx(vector Sp, float Sr, vector Ro, vector Rd){
    float t;Ro = Ro - Sp;
    //vector RayOrg = llDetectedPos(x) - llGetPos();
    if(Rd == ZERO_VECTOR) return FALSE;
   
    float a = Rd * Rd;
    float b = 2 * Rd * Ro;
    float c = (Ro * Ro)  - (Sr * Sr);
   
    float disc = b * b - 4 * a * c;
   
    if(disc < 0) return FALSE;
    return TRUE;
}
// Other
vector pN;float pD;
gTiP(vector p1,vector p2,vector p3){
    // Turns three vector points in space into a plane
    pN = llVecNorm( CP((p2-p1),(p3-p1)) );
    pD = -p1*pN;}
integer gTXcC(vector p1,vector p2,vector p3,vector x){
    // Can be used to check weather a point is inside a triangle
    gTiP(p1,p2,p3);
    vector Vn;vector En;
        Vn = p1 - x;
        En = CP((p2-p1),pN);
    if( ((p1-x)*CP(p2-p1,pN) >= 0)&&((p2-x)*CP(p3-p2,pN) >= 0)&&((p3-x)*CP(p1-p3,pN) >= 0) ) return TRUE;
    return FALSE;}
integer gTVXcC(vector p1,vector p2,vector p3,vector v,vector x){
    if( ((p1-x)*CP(p2-p1,v) >= 0)&&((p2-x)*CP(p3-p2,v) >= 0)&&((p3-x)*CP(p1-p3,v) >= 0) ) return TRUE;
    return FALSE;}
integer gTRcC(vector p1,vector p2,vector p3,vector O,vector D){
    return gTVXcC(p1,p2,p3,O,D);}
vector gRZiX(vector O,vector D,float z){
    return O+z*D;}
default{state_entry(){}}
</lsl>

Latest revision as of 13:48, 27 May 2008