1 | /**********************************************************************
|
---|
2 | *<
|
---|
3 | FILE: point3.h
|
---|
4 |
|
---|
5 | DESCRIPTION: Class definitions for Point3
|
---|
6 |
|
---|
7 | CREATED BY: Dan Silva
|
---|
8 |
|
---|
9 | HISTORY:
|
---|
10 |
|
---|
11 | *> Copyright (c) 1994, All Rights Reserved.
|
---|
12 | **********************************************************************/
|
---|
13 |
|
---|
14 | #ifndef _POINT3_H
|
---|
15 |
|
---|
16 | #define _POINT3_H
|
---|
17 |
|
---|
18 | class Point3 {
|
---|
19 | public:
|
---|
20 | float x,y,z;
|
---|
21 |
|
---|
22 | // Constructors
|
---|
23 | Point3(){}
|
---|
24 | Point3(float X, float Y, float Z) { x = X; y = Y; z = Z; }
|
---|
25 | Point3(double X, double Y, double Z) { x = (float)X; y = (float)Y; z = (float)Z; }
|
---|
26 | Point3(int X, int Y, int Z) { x = (float)X; y = (float)Y; z = (float)Z; }
|
---|
27 | Point3(const Point3& a) { x = a.x; y = a.y; z = a.z; }
|
---|
28 | Point3(float af[3]) { x = af[0]; y = af[1]; z = af[2]; }
|
---|
29 |
|
---|
30 | // Access operators
|
---|
31 | float& operator[](int i) { return (&x)[i]; }
|
---|
32 | const float& operator[](int i) const { return (&x)[i]; }
|
---|
33 |
|
---|
34 | // Conversion function
|
---|
35 | operator float*() { return(&x); }
|
---|
36 |
|
---|
37 | // Unary operators
|
---|
38 | Point3 operator-() const { return(Point3(-x,-y,-z)); }
|
---|
39 | Point3 operator+() const { return *this; }
|
---|
40 |
|
---|
41 | // Assignment operators
|
---|
42 | inline Point3& operator-=(const Point3&);
|
---|
43 | inline Point3& operator+=(const Point3&);
|
---|
44 | inline Point3& operator*=(float);
|
---|
45 | inline Point3& operator/=(float);
|
---|
46 | inline Point3& operator*=(const Point3&); // element-by-element multiply.
|
---|
47 |
|
---|
48 | // Test for equality
|
---|
49 | int operator==(const Point3& p) const { return ((p.x==x)&&(p.y==y)&&(p.z==z)); }
|
---|
50 |
|
---|
51 | // Binary operators
|
---|
52 | inline Point3 operator-(const Point3&) const;
|
---|
53 | inline Point3 operator+(const Point3&) const;
|
---|
54 | inline Point3 operator/(const Point3&) const;
|
---|
55 | inline Point3 operator*(const Point3&) const;
|
---|
56 |
|
---|
57 | DllExport Point3 operator^(const Point3&) const; // CROSS PRODUCT
|
---|
58 | };
|
---|
59 |
|
---|
60 |
|
---|
61 | float DllExport Length(const Point3&);
|
---|
62 | float DllExport LengthSquared(const Point3&);
|
---|
63 | int DllExport MaxComponent(const Point3&); // the component with the maximum abs value
|
---|
64 | int DllExport MinComponent(const Point3&); // the component with the minimum abs value
|
---|
65 | Point3 DllExport Normalize(const Point3&); // Return a unit vector.
|
---|
66 |
|
---|
67 |
|
---|
68 | // RB: moved this here from object.h
|
---|
69 | class Ray {
|
---|
70 | public:
|
---|
71 | Point3 p; // point of origin
|
---|
72 | Point3 dir; // unit vector
|
---|
73 | };
|
---|
74 |
|
---|
75 |
|
---|
76 | // Inlines:
|
---|
77 |
|
---|
78 | inline float Length(const Point3& v) {
|
---|
79 | return (float)sqrt(v.x*v.x+v.y*v.y+v.z*v.z);
|
---|
80 | }
|
---|
81 |
|
---|
82 | inline float LengthSquared(const Point3& v) {
|
---|
83 | return (v.x*v.x+v.y*v.y+v.z*v.z);
|
---|
84 | }
|
---|
85 |
|
---|
86 | inline Point3& Point3::operator-=(const Point3& a) {
|
---|
87 | x -= a.x; y -= a.y; z -= a.z;
|
---|
88 | return *this;
|
---|
89 | }
|
---|
90 |
|
---|
91 | inline Point3& Point3::operator+=(const Point3& a) {
|
---|
92 | x += a.x; y += a.y; z += a.z;
|
---|
93 | return *this;
|
---|
94 | }
|
---|
95 |
|
---|
96 | inline Point3& Point3::operator*=(float f) {
|
---|
97 | x *= f; y *= f; z *= f;
|
---|
98 | return *this;
|
---|
99 | }
|
---|
100 |
|
---|
101 | inline Point3& Point3::operator/=(float f) {
|
---|
102 | x /= f; y /= f; z /= f;
|
---|
103 | return *this;
|
---|
104 | }
|
---|
105 |
|
---|
106 | inline Point3& Point3::operator*=(const Point3& a) {
|
---|
107 | x *= a.x; y *= a.y; z *= a.z;
|
---|
108 | return *this;
|
---|
109 | }
|
---|
110 |
|
---|
111 | inline Point3 Point3::operator-(const Point3& b) const {
|
---|
112 | return(Point3(x-b.x,y-b.y,z-b.z));
|
---|
113 | }
|
---|
114 |
|
---|
115 | inline Point3 Point3::operator+(const Point3& b) const {
|
---|
116 | return(Point3(x+b.x,y+b.y,z+b.z));
|
---|
117 | }
|
---|
118 |
|
---|
119 | inline Point3 Point3::operator/(const Point3& b) const {
|
---|
120 | return Point3(x/b.x,y/b.y,z/b.z);
|
---|
121 | }
|
---|
122 |
|
---|
123 | inline Point3 Point3::operator*(const Point3& b) const {
|
---|
124 | return Point3(x*b.x, y*b.y, z*b.z);
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | inline Point3 operator*(float f, const Point3& a) {
|
---|
129 | return(Point3(a.x*f, a.y*f, a.z*f));
|
---|
130 | }
|
---|
131 |
|
---|
132 | inline Point3 operator*(const Point3& a, float f) {
|
---|
133 | return(Point3(a.x*f, a.y*f, a.z*f));
|
---|
134 | }
|
---|
135 |
|
---|
136 | inline Point3 operator/(const Point3& a, float f) {
|
---|
137 | return(Point3(a.x/f, a.y/f, a.z/f));
|
---|
138 | }
|
---|
139 |
|
---|
140 | inline Point3 operator+(const Point3& a, float f) {
|
---|
141 | return(Point3(a.x+f, a.y+f, a.z+f));
|
---|
142 | }
|
---|
143 |
|
---|
144 | inline float DotProd(const Point3& a, const Point3& b) {
|
---|
145 | return(a.x*b.x+a.y*b.y+a.z*b.z);
|
---|
146 | }
|
---|
147 |
|
---|
148 | Point3 DllExport CrossProd(const Point3& a, const Point3& b); // CROSS PRODUCT
|
---|
149 |
|
---|
150 |
|
---|
151 | // Compress a normal vector from 12 to 4 bytes.
|
---|
152 | // the vector has to be <= 1.0 in length.
|
---|
153 | // The decompressed vector has absolute error <.001 in each
|
---|
154 | // component.
|
---|
155 | ULONG DllExport CompressNormal(Point3 p);
|
---|
156 | Point3 DllExport DeCompressNormal(ULONG n);
|
---|
157 |
|
---|
158 | #endif
|
---|
159 |
|
---|