-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVec3f_2.hh
More file actions
129 lines (91 loc) · 2.71 KB
/
Vec3f_2.hh
File metadata and controls
129 lines (91 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#ifndef VEC3F_HXX
#define VEC3F_HXX
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <assert.h>
#include <sys/times.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <strings.h>
#include <stdarg.h>
#include <errno.h>
#include <iostream>
#include <fstream>
#define Epsilon 1E-5
#define Infinity HUGE_VAL
using namespace std;
class Vec3f
{
public:
float x,y,z;
Vec3f()
{};
Vec3f(float x,float y, float z)
: x(x),y(y),z(z)
{};
inline const float &operator[](const int i) const
{ return *(&x+i); };
inline float &operator[](const int i)
{ return *(&x+i); }; //((float *)(this))[i]; };
inline Vec3f &operator=(const Vec3f &b)
{ x = b.x; y = b.y; z = b.z; return *this;};
float dot(const Vec3f &b)
{ return x*b.x+y*b.y+z*b.z; };
float norm()
{ return sqrt(x*x+y*y+z*z); };
void normalize()
{
float len=this->norm();
x/=len;
y/=len;
z/=len;
};
};
/*! dot product */
inline float Dot(const Vec3f &a, const Vec3f &b)
{ return a.x*b.x+a.y*b.y+a.z*b.z; };
/*! component-wise product */
inline Vec3f Product(const Vec3f &a, const Vec3f &b)
{ return Vec3f(a.x*b.x,a.y*b.y,a.z*b.z); };
/*! vector product */
inline Vec3f Cross(const Vec3f &a, const Vec3f &b)
{ return Vec3f(a.y*b.z-a.z*b.y,
a.z*b.x-a.x*b.z,
a.x*b.y-a.y*b.x); };
inline Vec3f operator-(const Vec3f &v)
{ return Vec3f(-v.x,-v.y,-v.z); };
inline float Length(const Vec3f &v)
{ return sqrt(Dot(v,v)); };
inline Vec3f operator*(const float f, const Vec3f &v)
{ return Vec3f(f*v.x, f*v.y, f*v.z); };
inline Vec3f operator*(const Vec3f &v, const float f)
{ return Vec3f(f*v.x, f*v.y, f*v.z); };
inline Vec3f operator*(const Vec3f &v, const Vec3f &f)
{ return Vec3f(v.x*f.x, v.y*f.y, v.z*f.z); };
inline void operator*=(Vec3f &v, const float f)
{ v.x *= f; v.y*=f; v.z*=f; };
inline void operator*=(Vec3f &v, const Vec3f &f)
{ v.x *= f.x; v.y*=f.y; v.z*=f.z; };
inline void operator+=(Vec3f &v, const float f)
{ v.x += f; v.y+=f; v.z+=f; };
inline void operator+=(Vec3f &v, const Vec3f &f)
{ v.x += f.x; v.y+=f.y; v.z+=f.z; };
inline void operator<<(ostream& os,Vec3f &v)
{ os << "{" << v.x << "," << v.y << "," << v.z << "}"; };
inline Vec3f operator/(const Vec3f &v, const float f)
{ return (1/f)*v; };
inline void operator/=(Vec3f &v, const float f)
{ v *= (1/f); };
inline bool operator!=(Vec3f &v, const Vec3f &f)
{ return v.x!=f.x || v.y!=f.y || v.z!=f.z ;};
inline Vec3f operator+(const Vec3f &a, const Vec3f &b)
{ return Vec3f(a.x+b.x, a.y+b.y, a.z+b.z); };
inline Vec3f operator-(const Vec3f &a, const Vec3f &b)
{ return Vec3f(a.x-b.x, a.y-b.y, a.z-b.z); };
inline void Normalize(Vec3f &v)
{ v *= (1./Length(v)); };
#endif