vlMath.hh 1.83 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*=========================================================================

  Program:   Visualization Library
  Module:    vlMath.hh
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

This file is part of the Visualization Library. No part of this file or its 
contents may be copied, reproduced or altered in any way without the express
written consent of the authors.

Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 1993, 1994 

=========================================================================*/
Will Schroeder's avatar
Will Schroeder committed
16
//
Will Schroeder's avatar
Will Schroeder committed
17
// Class for performing common math operations (e.g., dot, cross products)
Will Schroeder's avatar
Will Schroeder committed
18
19
20
21
//
#ifndef __vlMath_hh
#define __vlMath_hh

Will Schroeder's avatar
Will Schroeder committed
22
23
24
25
26
#include <math.h>

class vlMath
{
public:
Will Schroeder's avatar
Will Schroeder committed
27
28
  vlMath() {};

Will Schroeder's avatar
Will Schroeder committed
29
  float Pi() {return 3.14159265358979;};
Will Schroeder's avatar
Will Schroeder committed
30

Will Schroeder's avatar
Will Schroeder committed
31
  float DegreesToRadians() {return 0.017453292;};
Will Schroeder's avatar
Will Schroeder committed
32

Will Schroeder's avatar
Will Schroeder committed
33
34
  float Dot(float x[3], float y[3]) 
    {return x[0]*y[0] + x[1]*y[1] + x[2]*y[2];};
Will Schroeder's avatar
Will Schroeder committed
35

Will Schroeder's avatar
Will Schroeder committed
36
37
38
39
  void Cross(float x[3], float y[3], float z[3])
    {z[0] = x[1]*y[2] - x[2]*y[1]; 
     z[1] = x[2]*y[0] - x[0]*y[2];
     z[2] = x[0]*y[1] - x[1]*y[0];};
Will Schroeder's avatar
Will Schroeder committed
40

Will Schroeder's avatar
Will Schroeder committed
41
42
  float Norm(float x[3])
    {return sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]);};
Will Schroeder's avatar
Will Schroeder committed
43

Will Schroeder's avatar
Will Schroeder committed
44
45
46
47
48
49
  void Normalize(float x[3])
    {float den; int i;
     if ( (den = this->Norm(x)) != 0.0 )
        for (i=0; i < 3; i++) x[i] /= den;
    }

Will Schroeder's avatar
Will Schroeder committed
50
51
52
53
54
55
56
57
58
59
60
  float Determinate3x3(float *c1, float *c2, float *c3)
    {return c1[0]*c2[1]*c3[2] + c2[0]*c3[1]*c1[2] + c3[0]*c1[1]*c2[2] -
     c1[0]*c3[1]*c2[2] - c2[0]*c1[1]*c3[2] - c3[0]*c2[1]*c1[2];};

  float Determinate2x2(float *c1, float *c2)
    {return c1[0]*c2[1] - c2[0]*c1[1];};

  float Distance2BetweenPoints(float *x, float *y)
    {return (x[0]-y[0])*(x[0]-y[0]) + (x[1]-y[1])*(x[1]-y[1]) +
            (x[2]-y[2])*(x[2]-y[2]);};

Will Schroeder's avatar
Will Schroeder committed
61
62
  void RandomSeed(long s);  
  float Random();  
Will Schroeder's avatar
Will Schroeder committed
63
64
65
  float Random(float min, float max) {return min+Random()*(max-min);};

protected:
Will Schroeder's avatar
Will Schroeder committed
66
  static long Seed;
Will Schroeder's avatar
Will Schroeder committed
67
};
Will Schroeder's avatar
Will Schroeder committed
68
69

#endif