vOOlkan
An object oriented approach to Vulkan
Segment.h
Go to the documentation of this file.
1#ifndef VULKAN_SEGMENT
2#define VULKAN_SEGMENT
3
4#include "Foundations.h"
5
6
7namespace Vulkan::Physics {
8
9
10 class Segment {
11 public:
12 Segment(Position p1, Position p2) : origin{ p1 }, direction{ p2 - p1 } {}
13
14 float distance(Position p) const {
15 auto distFromOrigin = p - origin; //distance of p from the origin of the segment
16
17 auto projectionFromOrigin = (distFromOrigin * direction) / length();
18
19 //if the projection of the segment o-p is greater than the lenght of the segment, it means that the point is "outside" the segment, on the ending side, so the closest point of the segment to our point p is the end
20 if (projectionFromOrigin > length()) {
21 return glm::length(glm::vec3(p - (origin + direction)));
22 }
23 //if the projection of the segment o-p is smaller than 0, it means that the point is "outside" the segment, on the starting side, so the closest point of the segment to our point p is the origin
24 else if (projectionFromOrigin < 0.0f) {
25 return glm::length(glm::vec3(p - origin));
26 }
27 //else the closest point is one of the points internal to the segment
28 else {
29 auto closestPoint = origin + glm::normalize(glm::vec3(direction)) * projectionFromOrigin;
30 return glm::length(glm::vec3(p - closestPoint));
31 }
32 }
33
34
35 float length() const {
36 return glm::length(glm::vec3(direction));
37 }
38
39 //TODO not efficient
44 auto distFromOrigin = p - origin; //distance of p from the origin of the segment
45 auto projectionFromOrigin = (distFromOrigin * direction) / length();
46 auto closestPoint = origin + glm::normalize(glm::vec3(direction)) * projectionFromOrigin; //calculate the closest point of the segment (or its prolongment) to the point
47 return glm::normalize(glm::vec3(closestPoint - p)); //the normal pointing to the segment
48 }
49
50
55 return glm::vec3{ direction.y(), -direction.x(), direction.z()};
56 }
57
58 const Position& getOrigin() const {
59 return origin;
60 }
61
62 const Position& getEnd() const {
63 return origin + direction;
64 }
65
66 const DeltaSpace& getDirection() const {
67 return direction;
68 }
69
70 private:
71 Position origin;
72 DeltaSpace direction;
73 };
74
75
76}
77
78
79#endif
Definition: Foundations.h:230
Definition: Foundations.h:187
Definition: Segment.h:10
DeltaSpace normal(Position p) const
Returns the normal always pointing to the vector.
Definition: Segment.h:43
const Position & getEnd() const
Definition: Segment.h:62
float length() const
Definition: Segment.h:35
DeltaSpace normal() const
Returns the clockwise normal.
Definition: Segment.h:54
const DeltaSpace & getDirection() const
Definition: Segment.h:66
Segment(Position p1, Position p2)
Definition: Segment.h:12
const Position & getOrigin() const
Definition: Segment.h:58
float distance(Position p) const
Definition: Segment.h:14
float x() const
Definition: Foundations.h:20
float z() const
Definition: Foundations.h:28
float y() const
Definition: Foundations.h:24
Definition: Cinematicable.h:9