psim  1.0
Generalized 2D phonon transport using a Monte Carlo method
geometry.h
Go to the documentation of this file.
1 #ifndef PSIM_GEOMETRY_H
2 #define PSIM_GEOMETRY_H
3 
4 #include <array>// for array
5 #include <exception>// for exception
6 #include <optional>// for optional
7 #include <ostream>// for ostream
8 #include <string>// for string
9 #include <string_view>// for string_view
10 #include <utility>// for pair
11 
12 
13 namespace Geometry {
14 struct Vector2D {
15  constexpr Vector2D(double x_coord, double y_coord) noexcept
16  : x{ x_coord }
17  , y{ y_coord } {
18  }
19  double x;
20  double y;
21 };
22 
23 struct Point {
24  constexpr Point(double x_coord, double y_coord) noexcept
25  : x{ x_coord }
26  , y{ y_coord } {
27  }
28  double x;
29  double y;
30 
31  bool operator==(const Point& rhs) const;
32  bool operator!=(const Point& rhs) const;
33 };
34 std::ostream& operator<<(std::ostream& os, const Point& point);// NOLINT
35 Point operator-(const Point& lhs, const Point& rhs);
36 
37 using PointPair = std::pair<Point, Point>;
38 
39 struct Line {
40  Line(Point pt1, Point pt2);
43 
44  // maybe only store the length and bounding box
45  double slope;
46  double intercept;
48  double length;
49 
50  [[nodiscard]] PointPair getPoints() const noexcept {
51  return { p1, p2 };
52  }
53 
54  [[nodiscard]] bool overlaps(const Line& other) const noexcept;
55  [[nodiscard]] bool contains(const Line& other) const noexcept;
56  [[nodiscard]] bool contains(const Point& point) const noexcept;
57  [[nodiscard]] bool intersects(const Line& other) const noexcept;
58  // norm_sign controls the direction of the normal vector. It can be used
59  // to ensure the normal vector always points into a surface.
60  // Should be 1 if the polygon is constructed in a clockwise fashion and -1 otherwise
61  [[nodiscard]] Vector2D normal(int norm_sign = 1) const noexcept;
62  // Returns the point of intersection if this line intersects with the input line.
63  // Return std::nullopt if the lines do not intersect
64  [[nodiscard]] std::optional<Point> getIntersection(const Line& other) const noexcept;
65  [[nodiscard]] Point getRandPoint(double r1) const noexcept;// NOLINT
66 
67  bool operator==(const Line& rhs) const {
68  return p1 == rhs.p1 && p2 == rhs.p2;
69  }
70  bool operator>(const Line& rhs) const {
71  return length > rhs.length;
72  }
73 };
74 std::ostream& operator<<(std::ostream& os, const Line& line);// NOLINT
75 
76 struct Triangle {
77  Triangle(Point pt1, Point pt2, Point pt3);
81 
82  [[nodiscard]] std::array<Line, 3> lines() const noexcept;
83 
84  [[nodiscard]] double area() const noexcept;
85  [[nodiscard]] bool intersects(const Triangle& other) const noexcept;
86  // Returns true if any point of the incoming triangle is contained in this triangle
87  [[nodiscard]] bool contains(const Triangle& other) const noexcept;
88  // Return false if the point is on an edge of the triangle
89  [[nodiscard]] bool contains(const Point& p) const noexcept;// NOLINT
90  [[nodiscard]] bool isClockwise() const noexcept;
91  [[nodiscard]] Point getRandPoint(double r1, double r2) const noexcept;// NOLINT
92 
93  bool operator==(const Triangle& rhs) const;
94 };
95 std::ostream& operator<<(std::ostream& os, const Triangle& triangle);// NOLINT
96 }// namespace Geometry
97 
98 
99 class ShapeError : public std::exception {
100 public:
101  [[nodiscard]] const char* what() const noexcept override {
102  return message_.c_str();
103  }
104 
105 protected:
106  void setMessage(std::string_view message) {
107  message_ = message;
108  }
109 
110 private:
111  std::string message_;
112 };
113 
114 class LineError : public ShapeError {
115 public:
116  explicit LineError(Geometry::Line line);
117 
118 private:
119  Geometry::Line line_;
120 };
121 
122 class TriangleError : public ShapeError {
123 public:
124  explicit TriangleError(Geometry::Triangle triangle);
125 
126 private:
127  Geometry::Triangle triangle_;
128 };
129 
130 #endif// PSIM_GEOMETRY_H
Definition: geometry.h:114
Definition: geometry.h:99
void setMessage(std::string_view message)
Definition: geometry.h:106
const char * what() const noexcept override
Definition: geometry.h:101
Definition: geometry.h:122
Definition: geometry.h:13
std::ostream & operator<<(std::ostream &os, const Point &point)
Definition: geometry.cpp:30
std::pair< Point, Point > PointPair
Definition: geometry.h:37
Point operator-(const Point &lhs, const Point &rhs)
Definition: geometry.cpp:35
Definition: geometry.h:39
bool overlaps(const Line &other) const noexcept
Definition: geometry.cpp:58
PointPair getPoints() const noexcept
Definition: geometry.h:50
Point getRandPoint(double r1) const noexcept
Definition: geometry.cpp:142
bool contains(const Line &other) const noexcept
Definition: geometry.cpp:77
double intercept
Definition: geometry.h:46
Point p2
Definition: geometry.h:42
Point p1
Definition: geometry.h:41
PointPair boundingBox
Definition: geometry.h:47
double slope
Definition: geometry.h:45
bool operator>(const Line &rhs) const
Definition: geometry.h:70
bool intersects(const Line &other) const noexcept
Definition: geometry.cpp:94
Vector2D normal(int norm_sign=1) const noexcept
Definition: geometry.cpp:99
Line(Point pt1, Point pt2)
Definition: geometry.cpp:47
double length
Definition: geometry.h:48
std::optional< Point > getIntersection(const Line &other) const noexcept
Definition: geometry.cpp:106
Definition: geometry.h:23
double y
Definition: geometry.h:29
bool operator==(const Point &rhs) const
Definition: geometry.cpp:39
bool operator!=(const Point &rhs) const
Definition: geometry.cpp:43
constexpr Point(double x_coord, double y_coord) noexcept
Definition: geometry.h:24
double x
Definition: geometry.h:28
Definition: geometry.h:76
Point p3
Definition: geometry.h:80
bool intersects(const Triangle &other) const noexcept
Definition: geometry.cpp:170
std::array< Line, 3 > lines() const noexcept
Definition: geometry.cpp:163
Triangle(Point pt1, Point pt2, Point pt3)
Definition: geometry.cpp:153
Point getRandPoint(double r1, double r2) const noexcept
Definition: geometry.cpp:237
double area() const noexcept
Definition: geometry.cpp:253
bool contains(const Triangle &other) const noexcept
Definition: geometry.cpp:265
bool isClockwise() const noexcept
Definition: geometry.cpp:220
Point p1
Definition: geometry.h:78
Point p2
Definition: geometry.h:79
Definition: geometry.h:14
double y
Definition: geometry.h:20
constexpr Vector2D(double x_coord, double y_coord) noexcept
Definition: geometry.h:15
double x
Definition: geometry.h:19