Line data Source code
1 : /*
2 : * Copyright (c) 2015, Lakshman Anumolu, Pradeep Garigipati
3 : * All rights reserved.
4 : *
5 : * This file is part of MeshIO whose distribution is governed by
6 : * the BSD 2-Clause License contained in the accompanying LICENSE.txt
7 : * file.
8 : */
9 :
10 : #ifndef __VECTORS_HPP__
11 : #define __VECTORS_HPP__
12 :
13 : namespace meshio {
14 :
15 : template<class T>
16 : class Vec2 {
17 : public:
18 : T x, y;
19 :
20 : Vec2(T pX, T pY)
21 : : x(pX), y(pY) {
22 : }
23 :
24 : Vec2()
25 : : x(0), y(0) {
26 : }
27 :
28 : Vec2& operator+=(const Vec2& other) {
29 : x += other.x;
30 : y += other.y;
31 : return *this;
32 : }
33 :
34 : Vec2& operator-=(const Vec2& other) {
35 : x -= other.x;
36 : y -= other.y;
37 : return *this;
38 : }
39 :
40 : Vec2& operator*=(const T pFactor) {
41 : x *= pFactor;
42 : y *= pFactor;
43 : return *this;
44 : }
45 :
46 : Vec2& operator/=(const T pFactor) {
47 : T oneByPDiv = (T)1/pFactor;
48 : return operator*=(oneByPDiv);
49 : }
50 :
51 : bool operator==(const Vec2& other) {
52 : return ((x == other.x) && (y == other.y));
53 : }
54 : };
55 :
56 : template<class T>
57 : class Vec3 {
58 : public:
59 : T x,y,z;
60 :
61 : Vec3(T pX, T pY, T pZ)
62 48 : : x(pX), y(pY), z(pZ) {
63 48 : }
64 :
65 : Vec3()
66 452 : : x(0), y(0), z(0) {
67 452 : }
68 :
69 : Vec3& operator+=(const Vec3& other) {
70 : x += other.x;
71 : y += other.y;
72 : z += other.z;
73 : return *this;
74 : }
75 :
76 : Vec3& operator-=(const Vec3& other) {
77 : x -= other.x;
78 : y -= other.y;
79 : z -= other.z;
80 : return *this;
81 : }
82 :
83 : Vec3& operator*=(const T pFactor) {
84 : x *= pFactor;
85 : y *= pFactor;
86 : z *= pFactor;
87 : return *this;
88 : }
89 :
90 : Vec3& operator/=(const T pFactor) {
91 : T oneByPDiv = (T)1/pFactor;
92 : return operator*=(oneByPDiv);
93 : }
94 :
95 : bool operator==(const Vec3& other) {
96 148 : return ((x == other.x) && (y == other.y) && (z == other.z));
97 : }
98 : };
99 :
100 : template<class T>
101 : class Vec4 {
102 : public:
103 : T x,y,z,w;
104 :
105 : Vec4(T pX, T pY, T pZ, T pW)
106 144 : : x(pX), y(pY), z(pZ), w(pW) {
107 144 : }
108 :
109 : Vec4()
110 1316 : : x(0), y(0), z(0), w(0) {
111 1316 : }
112 :
113 : Vec4& operator+=(const Vec4& other) {
114 : x += other.x;
115 : y += other.y;
116 : z += other.z;
117 : w += other.w;
118 : return *this;
119 : }
120 :
121 : Vec4& operator-=(const Vec4& other) {
122 : x -= other.x;
123 : y -= other.y;
124 : z -= other.z;
125 : w -= other.w;
126 : return *this;
127 : }
128 :
129 : Vec4& operator*=(const T pFactor) {
130 : x *= pFactor;
131 : y *= pFactor;
132 : z *= pFactor;
133 : w *= pFactor;
134 : return *this;
135 : }
136 :
137 : Vec4& operator/=(const T pFactor) {
138 : T oneByPDiv = (T)1/pFactor;
139 : return operator*=(oneByPDiv);
140 : }
141 :
142 : bool operator==(const Vec4& other) {
143 725 : return ((x==other.x) && (y==other.y) && (z==other.z) && (w==other.w));
144 : }
145 : };
146 :
147 : /* Dot product of two vectors */
148 : template<typename T>
149 : T dot(const Vec2<T>& lhs, const Vec2<T>& rhs) {
150 : return lhs.x*rhs.x + lhs.y*rhs.y;
151 : }
152 :
153 : /* Cross product of two vectors */
154 : template<typename T>
155 : Vec3<T> cross(const Vec2<T>& lhs, const Vec2<T>& rhs) {
156 : return Vec3<T>(0, 0, lhs.x*rhs.y - lhs.y*rhs.x);
157 : }
158 :
159 : /* Dot product of two vectors */
160 : template<typename T>
161 : T dot(const Vec3<T> &lhs, const Vec3<T>& rhs) {
162 : return lhs.x*rhs.x + lhs.y*rhs.y, lhs.z*rhs.z;
163 : }
164 :
165 : /* Cross product of three dimensional vectors */
166 : template<typename T>
167 : Vec3<T> cross(const Vec3<T> &lhs, const Vec3<T>& rhs) {
168 : return Vec3<T>(lhs.y*rhs.z - lhs.z*rhs.y,
169 : lhs.z*rhs.x - lhs.x*rhs.z,
170 : lhs.x*rhs.y - lhs.y*rhs.x);
171 : }
172 :
173 : }
174 : #endif // __VECTORS_HPP__
|