PeriDEM 0.2.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
point.h
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2024 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#ifndef UTIL_POINT_H
12#define UTIL_POINT_H
13
14#include <cmath>
15#include <iomanip>
16#include <sstream>
17#include <iostream>
18#include <vector>
19
27namespace util {
28
30struct Point {
31
33 double d_x;
34
36 double d_y;
37
39 double d_z;
40
44 Point() : d_x(0.), d_y(0.), d_z(0.){};
45
52 template <class T> Point(T x, T y, T z) : d_x(x), d_y(y), d_z(z){};
53
58 template <class T>
59 explicit Point(T x[3]) : d_x(x[0]), d_y(x[1]), d_z(x[2]){};
60
65 explicit Point(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 Point(const Point &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
112 void print(int nt = 0, int lvl = 0) const { std::cout << printStr(nt, lvl); }
113
118 double length() const { return std::sqrt(d_x * d_x + d_y * d_y + d_z * d_z); }
119
124 double lengthSq() const { return d_x * d_x + d_y * d_y + d_z *
125 d_z; }
126
132 double dot(const Point &b) const { return d_x * b.d_x + d_y * b.d_y + d_z * b
133 .d_z; }
134
140 double dist(const Point &b) const {
141 return std::sqrt((d_x - b.d_x) * (d_x - b.d_x) +
142 (d_y - b.d_y) * (d_y - b.d_y) +
143 (d_z - b.d_z) * (d_z - b.d_z));
144 }
145
151 Point cross(const Point &b) const {
152 return {-d_z * b.d_y + d_y * b.d_z,
153 d_z * b.d_x - d_x * b.d_z,
154 -d_y * b.d_x + d_x * b.d_y};
155 }
156
163 Point project(const Point &b, bool is_unit = false) const {
164 auto l_sq = (is_unit ? 1. : this->length() * this->length());
165 auto dot = this->dot(b);
166 return {dot * d_x / l_sq, dot * d_y / l_sq, dot * d_z / l_sq};
167 }
168
175 Point projectNormal(const Point &b, bool is_unit = false) const {
176 auto l_sq = (is_unit ? 1. : this->length() * this->length());
177 auto dot = this->dot(b);
178 return b - Point(dot * d_x / l_sq, dot * d_y / l_sq, dot * d_z / l_sq);
179 }
180
192 friend Point operator+(Point lhs, const Point &rhs) {
193 lhs += rhs;
194 return lhs;
195 }
196
203 friend Point operator-(Point lhs, const Point &rhs) {
204 lhs -= rhs;
205 return lhs;
206 }
207
214 friend double operator*(Point lhs, const Point rhs) {
215 return lhs.d_x * rhs.d_x + lhs.d_y * rhs.d_y + lhs.d_z * rhs.d_z;
216 }
217
224 friend Point operator*(Point lhs, const double rhs) {
225 lhs *= rhs;
226 return lhs;
227 }
228
235 friend Point operator+(Point lhs, const double rhs) {
236 return {lhs.d_x + rhs, lhs.d_y + rhs, lhs.d_z + rhs};
237 }
238
245 friend Point operator+(const double lhs, Point rhs) {
246 return {lhs + rhs.d_x, lhs + rhs.d_y, lhs + rhs.d_z};
247 }
248
255 friend Point operator-(Point lhs, const double rhs) {
256 return {lhs.d_x - rhs, lhs.d_y - rhs, lhs.d_z - rhs};
257 }
258
265 friend Point operator-(const double lhs, Point rhs) {
266 return {lhs - rhs.d_x, lhs - rhs.d_y, lhs - rhs.d_z};
267 }
268
275 friend Point operator*(const double lhs, Point rhs) {
276 rhs *= lhs;
277 return rhs;
278 }
279
286 friend Point operator/(Point lhs, const double rhs) {
287 lhs /= rhs;
288 return lhs;
289 }
290
296 Point &operator+=(const double b) {
297
298 d_x += b;
299 d_y += b;
300 d_z += b;
301 return *this;
302 }
303
309 Point &operator-=(const double b) {
310
311 d_x -= b;
312 d_y -= b;
313 d_z -= b;
314 return *this;
315 }
316
322 Point &operator*=(const double b) {
323
324 d_x *= b;
325 d_y *= b;
326 d_z *= b;
327 return *this;
328 }
329
335 Point &operator+=(const Point &b) {
336
337 d_x += b.d_x;
338 d_y += b.d_y;
339 d_z += b.d_z;
340 return *this;
341 }
342
348 Point &operator-=(const Point &b) {
349
350 d_x -= b.d_x;
351 d_y -= b.d_y;
352 d_z -= b.d_z;
353 return *this;
354 }
355
361 Point &operator*=(const Point &b) {
362
363 d_x *= b.d_x;
364 d_y *= b.d_y;
365 d_z *= b.d_z;
366 return *this;
367 }
368
374 Point &operator/=(const double b) {
375
376 d_x /= b;
377 d_y /= b;
378 d_z /= b;
379 return *this;
380 }
381
387 double &operator[](size_t i) {
388
389 if (i == 0)
390 return d_x;
391 else if (i == 1)
392 return d_y;
393 else
394 return d_z;
395 }
396
398 const double &operator[](size_t i) const {
399
400 if (i == 0)
401 return d_x;
402 else if (i == 1)
403 return d_y;
404 else
405 return d_z;
406 }
407
409};
410
411} // namespace util
412
413#endif
Collection of methods useful in simulation.
A structure to represent 3d vectors.
Definition point.h:30
double d_y
the y coordinate
Definition point.h:36
std::string printStr(int nt=0, int lvl=0) const
Returns the string containing printable information about the object.
Definition point.h:94
friend Point operator/(Point lhs, const double rhs)
Division by scalar operator.
Definition point.h:286
double dist(const Point &b) const
Computes the distance between a given point from this point.
Definition point.h:140
friend Point operator*(Point lhs, const double rhs)
Scalar product operator.
Definition point.h:224
Point(const std::vector< double > &p)
Constructor.
Definition point.h:65
double d_z
the z coordinate
Definition point.h:39
Point & operator*=(const Point &b)
Multiplication (pointwise/elementwise) operator.
Definition point.h:361
Point & operator/=(const double b)
Division by scalar operator.
Definition point.h:374
Point & operator*=(const double b)
Multiplication by scalar operator.
Definition point.h:322
double & operator[](size_t i)
Access operator.
Definition point.h:387
friend Point operator+(Point lhs, const double rhs)
Addition by scalar operator.
Definition point.h:235
double dot(const Point &b) const
Computes the dot product of this vector with another point.
Definition point.h:132
friend Point operator-(const double lhs, Point rhs)
Subtraction by scalar operator.
Definition point.h:265
Point & operator+=(const double b)
Addition by scalar operator.
Definition point.h:296
friend Point operator*(const double lhs, Point rhs)
Multiplication by scalar operator.
Definition point.h:275
double length() const
Computes the Euclidean length of the vector.
Definition point.h:118
Point project(const Point &b, bool is_unit=false) const
Computes projection of vector on this vector.
Definition point.h:163
Point cross(const Point &b) const
Computes the cross product between this vector and given vector.
Definition point.h:151
Point & operator+=(const Point &b)
Addition operator.
Definition point.h:335
friend Point operator-(Point lhs, const double rhs)
Subtraction by scalar operator.
Definition point.h:255
Point(T x, T y, T z)
Constructor.
Definition point.h:52
const double & operator[](size_t i) const
Access operator.
Definition point.h:398
friend Point operator+(Point lhs, const Point &rhs)
Addition operator.
Definition point.h:192
Point()
Constructor.
Definition point.h:44
friend Point operator+(const double lhs, Point rhs)
Addition by scalar operator.
Definition point.h:245
friend double operator*(Point lhs, const Point rhs)
Multiplication (dot) operator.
Definition point.h:214
double lengthSq() const
Computes the Euclidean length of the vector.
Definition point.h:124
Point projectNormal(const Point &b, bool is_unit=false) const
Computes projection of vector on plane with normal as this vector.
Definition point.h:175
Point & operator-=(const double b)
Subtraction by scalar operator.
Definition point.h:309
Point & operator-=(const Point &b)
Subtraction operator.
Definition point.h:348
double d_x
the x coordinate
Definition point.h:33
Point(T x[3])
Constructor.
Definition point.h:59
Point(const Point &p)
Copy constructor.
Definition point.h:85
void print(int nt=0, int lvl=0) const
Prints the information about the object.
Definition point.h:112
friend Point operator-(Point lhs, const Point &rhs)
Subtraction operator.
Definition point.h:203