00001 #ifndef _POINT3_H_
00002 #define _POINT3_H_
00003
00004 #include "cglib/vector3.h"
00005
00007 struct Point3
00008 {
00009 public:
00011 float x;
00013 float y;
00015 float z;
00016
00018 Point3(): x(0), y(0), z(0)
00019 {
00020 }
00021
00023 Point3(float c): x(c), y(c), z(c)
00024 {
00025 }
00026
00028 Point3(float _x, float _y, float _z) : x(_x), y(_y), z(_z)
00029 {
00030 }
00031
00033 Point3 operator+(const Vector3 &v) const
00034 {
00035 return Point3(x + v.x, y + v.y, z + v.z);
00036 }
00037
00039 Point3 &operator+=(const Vector3 &v)
00040 {
00041 x += v.x; y += v.y; z += v.z;
00042 return *this;
00043 }
00044
00046 Vector3 operator-(const Point3 &p) const
00047 {
00048 return Vector3(x - p.x, y - p.y, z - p.z);
00049 }
00050
00052 Point3 operator-(const Vector3 &v) const
00053 {
00054 return Point3(x - v.x, y - v.y, z - v.z);
00055 }
00056
00058 Point3 &operator-=(const Vector3 &v)
00059 {
00060 x -= v.x; y -= v.y; z -= v.z;
00061 return *this;
00062 }
00063
00065 bool operator==(const Point3 &other) const
00066 {
00067 return x == other.x && y == other.y && z == other.z;
00068 }
00069
00071 bool operator!=(const Point3 &other) const
00072 {
00073 return x != other.x || y != other.y || z != other.z;
00074 }
00075
00077
00083 float operator[](int i) const
00084 {
00085 return (&x)[i];
00086 }
00087
00089
00103 float &operator[](int i)
00104 {
00105 return (&x)[i];
00106 }
00107
00109
00115 void fill_array_3(float *a)
00116 {
00117 a[0] = x;
00118 a[1] = y;
00119 a[2] = z;
00120 }
00121
00123
00130 void fill_array_4(float *a)
00131 {
00132 a[0] = x;
00133 a[1] = y;
00134 a[2] = z;
00135 a[3] = 1.0f;
00136 }
00137
00139
00142 bool is_near(const Point3 &other, float threshold=EPSILON) const
00143 {
00144 float absx = fabs(x-other.x);
00145 float absy = fabs(y-other.y);
00146 float absz = fabs(z-other.z);
00147 return (absx < EPSILON && absy < EPSILON && absz < EPSILON);
00148 }
00149 };
00150
00152 inline float distance(const Point3 &p1, const Point3 &p2)
00153 {
00154 return (p1 - p2).length();
00155 }
00156
00158 inline float distance_squared(const Point3 &p1, const Point3 &p2)
00159 {
00160 return (p1 - p2).length_squared();
00161 }
00162
00163 #endif