home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vectoper.zip / RTVecOps.cpp < prev    next >
C/C++ Source or Header  |  1996-09-15  |  13KB  |  648 lines

  1.  
  2.  
  3. #include <math.h>
  4.  
  5. #include "RTTypes.h"
  6. #include "RTMatMac.h"
  7. #include "RTVecOps.h"
  8.  
  9.  
  10.  
  11.  
  12.  
  13. /*
  14.  *******************************************************************************
  15.  *
  16.  *  Name:  Cross_Product
  17.  *
  18.  *  Purpose:  this routine calculates the cross product between two input
  19.  *            vectors, A and B, and places the resultant vector in C.
  20.  *
  21.  *
  22.  *
  23.  *  Input Parameters
  24.  *
  25.  *     A - first vector in cross product
  26.  *     B - second vector in cross product
  27.  *
  28.  *
  29.  *  Output Parameters
  30.  *
  31.  *     C - vector result of cross product
  32.  *
  33.  *******************************************************************************
  34.  */
  35. void
  36.    Cross_Product( Vector *A, Vector *B, Vector *C)
  37.       {
  38.  
  39.        C->x = (A->y * B->z) - (A->z * B->y);
  40.  
  41.        C->y = (A->z * B->x) - (A->x * B->z);
  42.  
  43.        C->z = (A->x * B->y) - (A->y * B->x);
  44.       }
  45.  
  46.  
  47.  
  48. /*
  49.  *******************************************************************************
  50.  *
  51.  *  Name:  NCross_Product
  52.  *
  53.  *  Purpose:  this routine calculates the sum of the cross product between
  54.  *            adjacent points in a polygon.  Theresultant vector that is
  55.  *            returned will be normal to the face of the polygon.
  56.  *
  57.  *
  58.  *
  59.  *  Input Parameters
  60.  *
  61.  *     n - number of points in input array
  62.  *     pts - points array representing polygon
  63.  *
  64.  *
  65.  *  Output Parameters
  66.  *
  67.  *     none
  68.  *
  69.  *******************************************************************************
  70.  */
  71. Vector
  72.    NCross_Product(int n, Vector pts[])
  73.       {
  74.  //       Vector Pa, Pb;
  75.        Vector Pcross;
  76.        Vector V_sum;
  77.        int i;
  78.  
  79. /*
  80.  *    Pa = Vector_Difference(&pts[1], &pts[0]);
  81.  */
  82.      vset(&Pcross, (float) 0.0,  (float) 0.0,  (float) 0.0);
  83.      vset(&V_sum,  (float) 0.0,  (float) 0.0,  (float) 0.0);
  84.  
  85.        for (i=0; i<=n-1; i++)  {
  86.            Cross_Product(&pts[(i+1)%n], &pts[i], &Pcross);
  87.            V_sum = Vector_Sum(&V_sum, &Pcross);
  88. /*
  89.  *         if (i != n)
  90.  *            Pb = Vector_Difference(&pts[i], &pts[i+1]);
  91.  *         else
  92.  *            Pb = Vector_Difference(&pts[1], &pts[0]);
  93.  *         Cross_Product(&Pb, &Pa, &Pcross);
  94.  *         V_sum = Vector_Sum(&V_sum, &Pcross);
  95.  *         Pa = Pb;
  96.  */
  97.           }
  98. /*
  99.  *     Cross_Product(&pts[0], &pts[n-1], &Pcross);
  100.  *     V_sum = Vector_Sum(&V_sum, &Pcross);
  101.  */
  102.        return(V_sum);
  103.       }
  104.  
  105.  
  106.  
  107. /*
  108.  *******************************************************************************
  109.  *
  110.  *  Name: Dot_Product
  111.  *
  112.  *  Purpose:  this routine calculates the dot product between two input
  113.  *            vectors, A and B, and returns the resulting scaler value
  114.  *
  115.  *
  116.  *
  117.  *  Input Parameters
  118.  *
  119.  *     A - first vector in dot product
  120.  *     B - second vector in dot product
  121.  *
  122.  *
  123.  *  Output Parameters
  124.  *
  125.  *     none
  126.  *
  127.  *******************************************************************************
  128.  */
  129. float
  130.    Dot_Product(Vector *A, Vector *B)
  131.       {
  132.        float dp;
  133.  
  134.        dp = (A->x * B->x) + (A->y * B->y) + (A->z * B->z);
  135.  
  136.        return(dp);
  137.       }
  138.  
  139.  
  140.  
  141. /*
  142.  *******************************************************************************
  143.  *
  144.  *  Name: VectorScaler_Division
  145.  *
  146.  *  Purpose:  this routine divides the specified scaler quantity into the
  147.  *            specified vector and returns the resultant vector.
  148.  *
  149.  *
  150.  *
  151.  *  Input Parameters
  152.  *
  153.  *     vec - specified vector
  154.  *     scaler - specified scaler
  155.  *
  156.  *
  157.  *  Output Parameters
  158.  *
  159.  *     none
  160.  *
  161.  *******************************************************************************
  162.  */
  163. Vector
  164.    VectorScaler_Division(Vector *vec, float scaler)
  165.       {
  166.        Vector v;
  167.  
  168.        v.x = vec->x / scaler;
  169.        v.y = vec->y / scaler;
  170.        v.z = vec->z / scaler;
  171.  
  172.        return(v);
  173.       }
  174.  
  175.  
  176.  
  177. /*
  178.  *******************************************************************************
  179.  *
  180.  *  Name: VectorScaler_Product
  181.  *
  182.  *  Purpose:  this routine multiplys the specified scaler quantity into the
  183.  *            specified vector and returns the resultant vector.
  184.  *
  185.  *
  186.  *
  187.  *  Input Parameters
  188.  *
  189.  *     vec - specified vector
  190.  *     scaler - specified scaler
  191.  *
  192.  *
  193.  *  Output Parameters
  194.  *
  195.  *     none
  196.  *
  197.  *******************************************************************************
  198.  */
  199. Vector
  200.    VectorScaler_Product(Vector *vec, float scaler)
  201.       {
  202.        Vector v;
  203.  
  204.        v.x = vec->x * scaler;
  205.        v.y = vec->y * scaler;
  206.        v.z = vec->z * scaler;
  207.  
  208.        return(v);
  209.       }
  210.  
  211.  
  212.  
  213.  
  214. /*
  215.  *******************************************************************************
  216.  *
  217.  *  Name: Vector_Difference
  218.  *
  219.  *  Purpose:  this routine calculates the vector difference between two input
  220.  *            vectors, v1 and v2, and returns the resulting vector
  221.  *
  222.  *
  223.  *
  224.  *  Input Parameters
  225.  *
  226.  *     v1 - first vector in vector difference
  227.  *     v2 - second vector in vector difference
  228.  *
  229.  *
  230.  *  Output Parameters
  231.  *
  232.  *     none
  233.  *
  234.  *******************************************************************************
  235.  */
  236. Vector
  237.    Vector_Difference(Vector *v1, Vector *v2)
  238.       {
  239.        Vector vdiff;
  240.  
  241.        vdiff.x = v1->x - v2->x;
  242.        vdiff.y = v1->y - v2->y;
  243.        vdiff.z = v1->z - v2->z;
  244.  
  245.        return(vdiff);
  246.       }
  247.  
  248.  
  249.  
  250. /*
  251.  *******************************************************************************
  252.  *
  253.  *  Name:  Vector_Sum
  254.  *
  255.  *  Purpose:  this routine calculates the vector sum between two input
  256.  *            vectors, v1 and v2, and returns the resulting vector
  257.  *
  258.  *
  259.  *
  260.  *  Input Parameters
  261.  *
  262.  *     v1 - first vector in vector sum
  263.  *     v2 - second vector in vector sum
  264.  *
  265.  *
  266.  *  Output Parameters
  267.  *
  268.  *     none
  269.  *
  270.  *******************************************************************************
  271.  */
  272. Vector
  273.    Vector_Sum(Vector *v1, Vector *v2)
  274.       {
  275.        Vector vsum;
  276.  
  277.        vsum.x = v1->x + v2->x;
  278.        vsum.y = v1->y + v2->y;
  279.        vsum.z = v1->z + v2->z;
  280.  
  281.        return(vsum);
  282.       }
  283.  
  284.  
  285.  
  286. /*
  287.  *******************************************************************************
  288.  *
  289.  *  Name:  Vector_Average
  290.  *
  291.  *  Purpose:
  292.  *
  293.  *
  294.  *
  295.  *  Input Parameters
  296.  *
  297.  *     v1 - first vector in vector average
  298.  *     v2 - second vector in vector average
  299.  *
  300.  *
  301.  *  Output Parameters
  302.  *
  303.  *     none
  304.  *
  305.  *******************************************************************************
  306.  */
  307. Vector
  308.    Vector_Average(Vector *v1, Vector *v2)
  309.       {
  310.        Vector vaverage;
  311.  
  312.        vaverage.x = (v1->x + v2->x) / ((float)2.0);
  313.        vaverage.y = (v1->y + v2->y) / ((float)2.0);
  314.        vaverage.z = (v1->z + v2->z) / ((float)2.0);
  315.  
  316.        return(vaverage);
  317.       }
  318.  
  319.  
  320.  
  321. /*
  322.  *******************************************************************************
  323.  *
  324.  *  Name:  Vector_eq
  325.  *
  326.  *  Purpose:  this routine determines if the two specified vectors are
  327.  *            equal in all three components.
  328.  *
  329.  *
  330.  *
  331.  *  Input Parameters
  332.  *
  333.  *     v1 - first vector
  334.  *     v2 - second vector
  335.  *
  336.  *
  337.  *  Output Parameters
  338.  *
  339.  *     none
  340.  *
  341.  *******************************************************************************
  342.  */
  343. Boolean
  344.    Vector_eq(Vector *v1, Vector *v2)
  345.       {
  346.        Boolean vequal = FALSE;
  347.  
  348.        if ( (fabs(v1->x - v2->x) < 0.2)  &&
  349.             (fabs(v1->y - v2->y) < 0.2)  &&
  350.             (fabs(v1->z - v2->z) < 0.2) )
  351.           vequal = TRUE;
  352.  
  353.        return(vequal);
  354.       }
  355.  
  356.  
  357. /*
  358.  *******************************************************************************
  359.  *
  360.  *  Name:  vgt_lim
  361.  *
  362.  *  Purpose:  this routine determines if any element of the first vector
  363.  *            exceeds the value of the corresponding element of the second
  364.  *            vector.
  365.  *
  366.  *
  367.  *
  368.  *  Input Parameters
  369.  *
  370.  *     v1 - first vector
  371.  *     v2 - second vector
  372.  *
  373.  *
  374.  *  Output Parameters
  375.  *
  376.  *     none
  377.  *
  378.  *******************************************************************************
  379.  */
  380. Boolean
  381.    vgt_lim(Vector *v1, Vector *v2)
  382.       {
  383.        Boolean vgt = FALSE;
  384.  
  385.        if ( (v1->x > v2->x) ||
  386.             (v1->y > v2->y) ||
  387.             (v1->z > v2->z)    )
  388.           vgt = TRUE;
  389.  
  390.        return(vgt);
  391.       }
  392.  
  393.  
  394. /*
  395.  *******************************************************************************
  396.  *
  397.  *  Name:  Vector_Put_Col
  398.  *
  399.  *  Purpose:  this routine places a vector in a matrix column.  It is used
  400.  *            to create the the G matrix.
  401.  *
  402.  *
  403.  *
  404.  *  Input Parameters
  405.  *
  406.  *     vec - specified input vector
  407.  *     col - matrix column to place vector in
  408.  *
  409.  *
  410.  *  Output Parameters
  411.  *
  412.  *     matrix - matrix to which input vector was added
  413.  *
  414.  *******************************************************************************
  415.  * 
  416. void
  417.    Vector_Put_Col(Vector *vec, float matrix[][4], int col)
  418.       {
  419.        matrix[0][col] = vec->x;
  420.        matrix[1][col] = vec->y;
  421.        matrix[2][col] = vec->z;
  422.       }
  423.  */
  424.  
  425.  
  426.  
  427. /*
  428.  *******************************************************************************
  429.  *
  430.  *  Name:  vector_len
  431.  *
  432.  *  Purpose:  this routine calculates and returns the length of a vector.
  433.  *
  434.  *
  435.  *
  436.  *  Input Parameters
  437.  *
  438.  *     vec - specified vector
  439.  *
  440.  *
  441.  *  Output Parameters
  442.  *
  443.  *     none
  444.  *
  445.  *******************************************************************************
  446.  */
  447. float
  448.    vector_len(Vector *vec)
  449.       {
  450.        float vector_length = (float) 0.0;
  451.  
  452.        vector_length = (vec->x * vec->x) + 
  453.                        (vec->y * vec->y) + 
  454.                        (vec->z * vec->z);
  455.  
  456.        vector_length = sqrt(vector_length);
  457.  
  458.        return(vector_length);
  459.        }
  460.  
  461.  
  462.  
  463. /*
  464.  *******************************************************************************
  465.  *
  466.  *  Name: vproduct
  467.  *
  468.  *  Purpose:  this routine multiplies two vectors together as three scaler
  469.  *            components.
  470.  *
  471.  *
  472.  *
  473.  *  Input Parameters
  474.  *
  475.  *     v1 - first vector
  476.  *     v2 - second vector
  477.  *
  478.  *
  479.  *  Output Parameters
  480.  *
  481.  *     none
  482.  *
  483.  *******************************************************************************
  484.  */
  485. Vector
  486.    vproduct(Vector *vec1, Vector *vec2)
  487.       {
  488.        Vector vec;
  489.  
  490.        vec.x = vec1->x * vec2->x;
  491.        vec.y = vec1->y * vec2->y;
  492.        vec.z = vec1->z * vec2->z;
  493.  
  494.        return (vec);
  495.       }
  496.  
  497.  
  498.  
  499. /*
  500.  *******************************************************************************
  501.  *
  502.  *  Name: vmax
  503.  *
  504.  *  Purpose:  this routine compares the components of two vectors and returns
  505.  *            a vector that contains the maximum components of the two.
  506.  *
  507.  *
  508.  *
  509.  *  Input Parameters
  510.  *
  511.  *     vec1 - first vector to be compared
  512.  *     vec2 - second vector to be compared
  513.  *
  514.  *
  515.  *  Output Parameters
  516.  *
  517.  *     none
  518.  *
  519.  *******************************************************************************
  520.  */
  521. Vector
  522.    vmax(Vector *vec1, Vector *vec2)
  523.       {
  524.        Vector max_vector;
  525.  
  526.        max_vector.x = MAX(vec1->x, vec2->x);
  527.        max_vector.y = MAX(vec1->y, vec2->y);
  528.        max_vector.z = MAX(vec1->z, vec2->z);
  529.  
  530.        return (max_vector);
  531.       }
  532.  
  533.  
  534.  
  535. /*
  536.  *******************************************************************************
  537.  *
  538.  *  Name: vmin
  539.  *
  540.  *  Purpose:  this routine compares the components of two vectors and returns
  541.  *            a vector that contains the minimum components of the two.
  542.  *
  543.  *
  544.  *
  545.  *  Input Parameters
  546.  *
  547.  *     vec1 - first vector to be compared
  548.  *     vec2 - second vector to be compared
  549.  *
  550.  *
  551.  *  Output Parameters
  552.  *
  553.  *     none
  554.  *
  555.  *******************************************************************************
  556.  */
  557. Vector
  558.    vmin(Vector *vec1, Vector *vec2)
  559.       {
  560.        Vector min_vector;
  561.  
  562.        min_vector.x = MIN(vec1->x, vec2->x);
  563.        min_vector.y = MIN(vec1->y, vec2->y);
  564.        min_vector.z = MIN(vec1->z, vec2->z);
  565.  
  566.        return (min_vector);
  567.       }
  568.  
  569.  
  570.  
  571. /*
  572.  *******************************************************************************
  573.  *
  574.  *  Name:  vset
  575.  *
  576.  *  Purpose:  this routine initializes the specified vector to the specified
  577.  *            component values.
  578.  *
  579.  *
  580.  *
  581.  *  Input Parameters
  582.  *
  583.  *     vec - vector to be initialized
  584.  *     x - input x component
  585.  *     y - input y component
  586.  *     z - input z component
  587.  *
  588.  *
  589.  *  Output Parameters
  590.  *
  591.  *     none
  592.  *
  593.  *******************************************************************************
  594.  */
  595. void
  596.    vset(Vector *vec, float x, float y, float z)
  597.       {
  598.        vec->x = x;
  599.        vec->y = y;
  600.        vec->z = z;
  601.       }
  602.  
  603.  
  604.  
  605. /*
  606.  *******************************************************************************
  607.  *
  608.  *  Name:  vclamp
  609.  *
  610.  *  Purpose:  this routine clamps a vector at the maximum value specified.
  611.  *
  612.  *
  613.  *
  614.  *  Input Parameters
  615.  *
  616.  *     vec - vector to be clamped
  617.  *     max - clamp value
  618.  *
  619.  *
  620.  *  Output Parameters
  621.  *
  622.  *     none
  623.  *
  624.  *******************************************************************************
  625.  */
  626. void
  627.    vclamp(Vector *vec, float max)
  628.       {
  629.        vec->x = (vec->x > max)? max : vec->x;
  630.        vec->y = (vec->y > max)? max : vec->y;
  631.        vec->z = (vec->z > max)? max : vec->z;
  632.       }
  633.  
  634.  
  635.  
  636. float
  637.    vmax_element(Vector *vec)
  638.       {
  639.        float max_element;
  640.  
  641.        max_element = MAX(vec->x, vec->y);
  642.        max_element = MAX(max_element, vec->z);
  643.  
  644.        return(max_element);
  645.       }
  646.  
  647.  
  648.