PeriDEM 0.3.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 - 2026 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 std::vector<double> toVec() const { return std::vector<double>({d_x, d_y, d_z}); }
119
124 double length() const { return std::sqrt(d_x * d_x + d_y * d_y + d_z * d_z); }
125
130 double lengthSq() const { return d_x * d_x + d_y * d_y + d_z *
131 d_z; }
132
138 double dot(const Point &b) const { return d_x * b.d_x + d_y * b.d_y + d_z * b
139 .d_z; }
140
146 double dist(const Point &b) const {
147 return std::sqrt((d_x - b.d_x) * (d_x - b.d_x) +
148 (d_y - b.d_y) * (d_y - b.d_y) +
149 (d_z - b.d_z) * (d_z - b.d_z));
150 }
151
157 Point cross(const Point &b) const {
158 return {-d_z * b.d_y + d_y * b.d_z,
159 d_z * b.d_x - d_x * b.d_z,
160 -d_y * b.d_x + d_x * b.d_y};
161 }
162
169 Point project(const Point &b, bool is_unit = false) const {
170 auto l_sq = (is_unit ? 1. : this->length() * this->length());
171 auto dot = this->dot(b);
172 return {dot * d_x / l_sq, dot * d_y / l_sq, dot * d_z / l_sq};
173 }
174
181 Point projectNormal(const Point &b, bool is_unit = false) const {
182 auto l_sq = (is_unit ? 1. : this->length() * this->length());
183 auto dot = this->dot(b);
184 return b - Point(dot * d_x / l_sq, dot * d_y / l_sq, dot * d_z / l_sq);
185 }
186
198 friend Point operator+(Point lhs, const Point &rhs) {
199 lhs += rhs;
200 return lhs;
201 }
202
209 friend Point operator-(Point lhs, const Point &rhs) {
210 lhs -= rhs;
211 return lhs;
212 }
213
220 friend double operator*(Point lhs, const Point rhs) {
221 return lhs.d_x * rhs.d_x + lhs.d_y * rhs.d_y + lhs.d_z * rhs.d_z;
222 }
223
230 friend Point operator*(Point lhs, const double rhs) {
231 lhs *= rhs;
232 return lhs;
233 }
234
241 friend Point operator+(Point lhs, const double rhs) {
242 return {lhs.d_x + rhs, lhs.d_y + rhs, lhs.d_z + rhs};
243 }
244
251 friend Point operator+(const double lhs, Point rhs) {
252 return {lhs + rhs.d_x, lhs + rhs.d_y, lhs + rhs.d_z};
253 }
254
261 friend Point operator-(Point lhs, const double rhs) {
262 return {lhs.d_x - rhs, lhs.d_y - rhs, lhs.d_z - rhs};
263 }
264
271 friend Point operator-(const double lhs, Point rhs) {
272 return {lhs - rhs.d_x, lhs - rhs.d_y, lhs - rhs.d_z};
273 }
274
281 friend Point operator*(const double lhs, Point rhs) {
282 rhs *= lhs;
283 return rhs;
284 }
285
292 friend Point operator/(Point lhs, const double rhs) {
293 lhs /= rhs;
294 return lhs;
295 }
296
302 Point &operator+=(const double b) {
303
304 d_x += b;
305 d_y += b;
306 d_z += b;
307 return *this;
308 }
309
315 Point &operator-=(const double b) {
316
317 d_x -= b;
318 d_y -= b;
319 d_z -= b;
320 return *this;
321 }
322
328 Point &operator*=(const double b) {
329
330 d_x *= b;
331 d_y *= b;
332 d_z *= b;
333 return *this;
334 }
335
341 Point &operator+=(const Point &b) {
342
343 d_x += b.d_x;
344 d_y += b.d_y;
345 d_z += b.d_z;
346 return *this;
347 }
348
354 Point &operator-=(const Point &b) {
355
356 d_x -= b.d_x;
357 d_y -= b.d_y;
358 d_z -= b.d_z;
359 return *this;
360 }
361
367 Point &operator*=(const Point &b) {
368
369 d_x *= b.d_x;
370 d_y *= b.d_y;
371 d_z *= b.d_z;
372 return *this;
373 }
374
380 Point &operator/=(const double b) {
381
382 d_x /= b;
383 d_y /= b;
384 d_z /= b;
385 return *this;
386 }
387
393 double &operator[](size_t i) {
394
395 if (i == 0)
396 return d_x;
397 else if (i == 1)
398 return d_y;
399 else
400 return d_z;
401 }
402
404 const double &operator[](size_t i) const {
405
406 if (i == 0)
407 return d_x;
408 else if (i == 1)
409 return d_y;
410 else
411 return d_z;
412 }
413
415};
416
417} // namespace util
418
419#endif
Collection of methods useful in simulation.
Definition constants.h:14
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:292
double dist(const Point &b) const
Computes the distance between a given point from this point.
Definition point.h:146
friend Point operator*(Point lhs, const double rhs)
Scalar product operator.
Definition point.h:230
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:367
Point & operator/=(const double b)
Division by scalar operator.
Definition point.h:380
Point & operator*=(const double b)
Multiplication by scalar operator.
Definition point.h:328
double & operator[](size_t i)
Access operator.
Definition point.h:393
friend Point operator+(Point lhs, const double rhs)
Addition by scalar operator.
Definition point.h:241
double dot(const Point &b) const
Computes the dot product of this vector with another point.
Definition point.h:138
friend Point operator-(const double lhs, Point rhs)
Subtraction by scalar operator.
Definition point.h:271
Point & operator+=(const double b)
Addition by scalar operator.
Definition point.h:302
friend Point operator*(const double lhs, Point rhs)
Multiplication by scalar operator.
Definition point.h:281
double length() const
Computes the Euclidean length of the vector.
Definition point.h:124
Point project(const Point &b, bool is_unit=false) const
Computes projection of vector on this vector.
Definition point.h:169
Point cross(const Point &b) const
Computes the cross product between this vector and given vector.
Definition point.h:157
Point & operator+=(const Point &b)
Addition operator.
Definition point.h:341
friend Point operator-(Point lhs, const double rhs)
Subtraction by scalar operator.
Definition point.h:261
Point(T x, T y, T z)
Constructor.
Definition point.h:52
const double & operator[](size_t i) const
Access operator.
Definition point.h:404
friend Point operator+(Point lhs, const Point &rhs)
Addition operator.
Definition point.h:198
Point()
Constructor.
Definition point.h:44
friend Point operator+(const double lhs, Point rhs)
Addition by scalar operator.
Definition point.h:251
friend double operator*(Point lhs, const Point rhs)
Multiplication (dot) operator.
Definition point.h:220
double lengthSq() const
Computes the Euclidean length of the vector.
Definition point.h:130
Point projectNormal(const Point &b, bool is_unit=false) const
Computes projection of vector on plane with normal as this vector.
Definition point.h:181
Point & operator-=(const double b)
Subtraction by scalar operator.
Definition point.h:315
std::vector< double > toVec() const
Returns coordinate in stl form.
Definition point.h:118
Point & operator-=(const Point &b)
Subtraction operator.
Definition point.h:354
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:209