NLMech  0.1.0
point.h
1 // Copyright (c) 2019 Prashant K. Jha
3 // Copyright (c) 2019 Patrick Diehl
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 
9 #ifndef UTIL_POINT_H
10 #define UTIL_POINT_H
11 
12 #include "matrixBlaze.h"
13 #include <cmath>
14 #include <iomanip>
15 #include <iostream>
16 #include <vector>
17 #include <stdlib.h>
18 
26 namespace util {
27 
29 struct Point3 {
30 
32  double d_x;
33 
35  double d_y;
36 
38  double d_z;
39 
43  Point3() : d_x(0.), d_y(0.), d_z(0.){};
44 
51  template <class T>
52  Point3(T x, T y, T z) : d_x(x), d_y(y), d_z(z){};
53 
58  template <class T>
59  explicit Point3(T x[3]) : d_x(x[0]), d_y(x[1]), d_z(x[2]){};
60 
65  explicit Point3(const std::vector<double> &p) {
66 
67  if (p.empty())
68  return;
69  else if (p.size() == 1)
70  d_x = p[0];
71  else if (p.size() == 2) {
72  d_x = p[0];
73  d_y = p[1];
74  } else if (p.size() == 3) {
75  d_x = p[0];
76  d_y = p[1];
77  d_z = p[2];
78  }
79  }
80 
85  Point3(const Point3 &p) : d_x(p.d_x), d_y(p.d_y), d_z(p.d_z) {};
86 
94  std::string printStr(int nt = 0, int lvl = 0) const {
95 
96  std::string tabS = "";
97  for (int i = 0; i < nt; i++)
98  tabS += "\t";
99 
100  std::ostringstream oss;
101  oss << tabS << "(" << d_x << ", " << d_y << ", " << d_z << ")";
102 
103  return oss.str();
104  }
105 
106 
113  void print(int nt = 0, int lvl = 0) const { std::cout << printStr(nt, lvl); }
114 
119  double length() const { return std::sqrt(d_x * d_x + d_y * d_y + d_z * d_z); }
120 
125  double lengthSq() const { return d_x * d_x + d_y * d_y + d_z *
126  d_z; }
127 
133  auto l = this->length();
134  if (l < 1.0E-10)
135  return {};
136  else
137  return {d_x/l, d_y/l, d_z/l};
138  }
139 
144  Point3 unit() const {
145  auto l = this->length();
146  if (l < 1.0E-10)
147  return {};
148  else
149  return {d_x/l, d_y/l, d_z/l};
150  }
151 
157  double dot(const Point3 &b) const { return d_x * b.d_x + d_y * b.d_y + d_z * b
158  .d_z; }
159 
165  double dist(const Point3 &b) const {
166  return std::sqrt((d_x - b.d_x) * (d_x - b.d_x) +
167  (d_y - b.d_y) * (d_y - b.d_y) +
168  (d_z - b.d_z) * (d_z - b.d_z));
169  }
170 
176  Point3 cross(const Point3 &b) const {
177  return {-d_z * b.d_y + d_y * b.d_z,
178  d_z * b.d_x - d_x * b.d_z,
179  -d_y * b.d_x + d_x * b.d_y};
180  }
181 
188  Point3 project(const Point3 &b, bool is_unit = false) const {
189  auto l_sq = (is_unit ? 1. : this->length() * this->length());
190  auto dot = this->dot(b);
191  return {dot * d_x / l_sq, dot * d_y / l_sq, dot * d_z / l_sq};
192  }
193 
200  Point3 projectNormal(const Point3 &b, bool is_unit = false) const {
201  auto l_sq = (is_unit ? 1. : this->length() * this->length());
202  auto dot = this->dot(b);
203  return b - Point3(dot * d_x / l_sq, dot * d_y / l_sq, dot * d_z / l_sq);
204  }
205 
206 
212  double angle(Point3 b) const {
213 
214  auto ahat = this->unit();
215  auto bhat = b.unit();
216  return std::acos(ahat.dot(bhat));
217  }
218 
224 
226 
227  tmp(0, 0) = this->d_x * this->d_x;
228  tmp(0, 1) = this->d_x * this->d_y;
229  tmp(0, 2) = this->d_x * this->d_z;
230 
231  tmp(1, 0) = this->d_y * this->d_x;
232  tmp(1, 1) = this->d_y * this->d_y;
233  tmp(1, 2) = this->d_y * this->d_z;
234 
235  tmp(2, 0) = this->d_z * this->d_x;
236  tmp(2, 1) = this->d_z * this->d_y;
237  tmp(2, 2) = this->d_z * this->d_z;
238 
239  return tmp;
240  }
241 
248 
250 
251  tmp(0, 0) = this->d_x * b.d_x;
252  tmp(0, 1) = this->d_x * b.d_y;
253  tmp(0, 2) = this->d_x * b.d_z;
254 
255  tmp(1, 0) = this->d_y * b.d_x;
256  tmp(1, 1) = this->d_y * b.d_y;
257  tmp(1, 2) = this->d_y * b.d_z;
258 
259  tmp(2, 0) = this->d_z * b.d_x;
260  tmp(2, 1) = this->d_z * b.d_y;
261  tmp(2, 2) = this->d_z * b.d_z;
262 
263  return tmp;
264  }
265 
271 
272  return util::Vector3{this->d_x, this->d_y, this->d_z};
273  }
274 
287  friend Point3 operator+(Point3 lhs, const Point3 &rhs) {
288  lhs += rhs;
289  return lhs;
290  }
291 
298  friend Point3 operator-(Point3 lhs, const Point3 &rhs) {
299  lhs -= rhs;
300  return lhs;
301  }
302 
309  friend double operator*(Point3 lhs, const Point3 rhs) {
310  return lhs.d_x * rhs.d_x + lhs.d_y * rhs.d_y + lhs.d_z * rhs.d_z;
311  }
312 
319  friend Point3 operator*(Point3 lhs, const double rhs) {
320  lhs *= rhs;
321  return lhs;
322  }
323 
330  friend Point3 operator+(Point3 lhs, const double rhs) {
331  return {lhs.d_x + rhs, lhs.d_y + rhs, lhs.d_z + rhs};
332  }
333 
340  friend Point3 operator+(const double lhs, Point3 rhs) {
341  return {lhs + rhs.d_x, lhs + rhs.d_y, lhs + rhs.d_z};
342  }
343 
350  friend Point3 operator-(Point3 lhs, const double rhs) {
351  return {lhs.d_x - rhs, lhs.d_y - rhs, lhs.d_z - rhs};
352  }
353 
360  friend Point3 operator-(const double lhs, Point3 rhs) {
361  return {lhs - rhs.d_x, lhs - rhs.d_y, lhs - rhs.d_z};
362  }
363 
370  friend Point3 operator*(const double lhs, Point3 rhs) {
371  rhs *= lhs;
372  return rhs;
373  }
374 
381  friend Point3 operator/(Point3 lhs, const double rhs) {
382  lhs /= rhs;
383  return lhs;
384  }
385 
391  Point3 &operator+=(const double b) {
392 
393  d_x += b;
394  d_y += b;
395  d_z += b;
396  return *this;
397  }
398 
404  Point3 &operator-=(const double b) {
405 
406  d_x -= b;
407  d_y -= b;
408  d_z -= b;
409  return *this;
410  }
411 
417  Point3 &operator*=(const double b) {
418 
419  d_x *= b;
420  d_y *= b;
421  d_z *= b;
422  return *this;
423  }
424 
430  Point3 &operator+=(const Point3 &b) {
431 
432  d_x += b.d_x;
433  d_y += b.d_y;
434  d_z += b.d_z;
435  return *this;
436  }
437 
443  Point3 &operator-=(const Point3 &b) {
444 
445  d_x -= b.d_x;
446  d_y -= b.d_y;
447  d_z -= b.d_z;
448  return *this;
449  }
450 
456  Point3 &operator*=(const Point3 &b) {
457 
458  d_x *= b.d_x;
459  d_y *= b.d_y;
460  d_z *= b.d_z;
461  return *this;
462  }
463 
469  Point3 &operator/=(const double b) {
470 
471  d_x /= b;
472  d_y /= b;
473  d_z /= b;
474  return *this;
475  }
476 
482  double &operator[](size_t i) {
483 
484  if (i == 0)
485  return d_x;
486  else if (i == 1)
487  return d_y;
488  else
489  return d_z;
490  }
491 
497  const double &operator[](size_t i) const {
498 
499  if (i == 0)
500  return d_x;
501  else if (i == 1)
502  return d_y;
503  else
504  return d_z;
505  }
506 
513  friend std::ostream &operator<<(std::ostream &os, const Point3 p);
514 
516 };
517 
518 inline std::ostream &operator<<(std::ostream &os, const Point3 p) {
519  os << p.d_x << " " << p.d_y << " " << p.d_z << std::endl;
520  return os;
521 }
522 
523 } // namespace util
524 
525 #endif
Collection of methods useful in simulation.
Definition: DataManager.h:50
std::ostream & operator<<(std::ostream &os, const Point3 p)
Definition: point.h:518
blaze::StaticMatrix< double, 3UL, 3UL > Matrix33
Blaze: Definition of 3 x 3 matrix.
Definition: matrixBlaze.h:23
blaze::StaticVector< double, 3UL > Vector3
Blaze: Definition of 3D vector.
Definition: matrixBlaze.h:28
A structure to represent 3d vectors.
Definition: point.h:29
double length() const
Computes the Euclidean length of the vector.
Definition: point.h:119
double angle(Point3 b) const
Computes the angle between vector given by this and the vector b.
Definition: point.h:212
friend Point3 operator*(const double lhs, Point3 rhs)
Subtracts a scalar to the point.
Definition: point.h:370
friend Point3 operator+(Point3 lhs, const double rhs)
Adds a scalar to the point.
Definition: point.h:330
Point3 & operator/=(const double b)
Divides a point by a scalar.
Definition: point.h:469
Point3 unit() const
Returns the unit vector.
Definition: point.h:144
friend Point3 operator/(Point3 lhs, const double rhs)
Divides a scalar to the point.
Definition: point.h:381
void print(int nt=0, int lvl=0) const
Prints the information.
Definition: point.h:113
Point3 & operator-=(const Point3 &b)
Subtracts a point.
Definition: point.h:443
Point3 & operator-=(const double b)
Subtracts a scalar to the point.
Definition: point.h:404
util::Matrix33 toMatrix()
Computes the dot product of the vector and its transpose.
Definition: point.h:223
Point3()
Constructor.
Definition: point.h:43
friend Point3 operator+(const double lhs, Point3 rhs)
Adds two points.
Definition: point.h:340
Point3 projectNormal(const Point3 &b, bool is_unit=false) const
Computes projection of vector on plane with normal as this vector.
Definition: point.h:200
Point3(T x, T y, T z)
Constructor.
Definition: point.h:52
Point3 & operator+=(const double b)
Add a scalar to the point.
Definition: point.h:391
Point3 & operator*=(const double b)
Multiplies a scalar to the point.
Definition: point.h:417
friend double operator*(Point3 lhs, const Point3 rhs)
Multiplies a constant to the point.
Definition: point.h:309
Point3 unit()
Returns the unit vector.
Definition: point.h:132
util::Matrix33 toMatrix(const util::Point3 b)
Computes the dot product of the vector x and the transpose of y.
Definition: point.h:247
util::Vector3 toVector()
Converts the point to an blaze vector.
Definition: point.h:270
double lengthSq() const
Computes the Euclidean length of the vector.
Definition: point.h:125
const double & operator[](size_t i) const
Access the 0th component of a point.
Definition: point.h:497
Point3 & operator*=(const Point3 &b)
Multiplies a point.
Definition: point.h:456
friend Point3 operator-(const double lhs, Point3 rhs)
Subtracts a scalar to the point.
Definition: point.h:360
friend std::ostream & operator<<(std::ostream &os, const Point3 p)
Print the point's value to the standard output stream.
Definition: point.h:518
friend Point3 operator-(Point3 lhs, const double rhs)
Subtracts a scalar to the point.
Definition: point.h:350
double d_x
the x coordinate
Definition: point.h:32
double d_y
the y coordinate
Definition: point.h:35
Point3 project(const Point3 &b, bool is_unit=false) const
Computes projection of vector on this vector.
Definition: point.h:188
std::string printStr(int nt=0, int lvl=0) const
Prints the information.
Definition: point.h:94
double dist(const Point3 &b) const
Computes the distance between a given point from this point.
Definition: point.h:165
friend Point3 operator-(Point3 lhs, const Point3 &rhs)
Substracts two points.
Definition: point.h:298
Point3(const std::vector< double > &p)
Constructor.
Definition: point.h:65
friend Point3 operator*(Point3 lhs, const double rhs)
Adds two points.
Definition: point.h:319
double d_z
the z coordinate
Definition: point.h:38
Point3(const Point3 &p)
Copy constructor.
Definition: point.h:85
friend Point3 operator+(Point3 lhs, const Point3 &rhs)
Adds two points.
Definition: point.h:287
Point3(T x[3])
Constructor.
Definition: point.h:59
double & operator[](size_t i)
Access the 0th component of a point.
Definition: point.h:482
Point3 & operator+=(const Point3 &b)
Adds a point.
Definition: point.h:430
double dot(const Point3 &b) const
Computes the dot product of this vector with another point.
Definition: point.h:157
Point3 cross(const Point3 &b) const
Computes the cross product between this vector and given vector.
Definition: point.h:176