Skip to content

Commit

Permalink
Merge pull request #18 from NHLStenden-ISAL/refactor
Browse files Browse the repository at this point in the history
added const correctness, references where possible, removed some bugs…
  • Loading branch information
GTMeijer authored Nov 2, 2023
2 parents cc1ca8a + 6edf5d3 commit 3b24ff9
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 66 deletions.
38 changes: 37 additions & 1 deletion Trajectory_Hotspots/Trajectory_Hotspots/aabb.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,38 @@
#include "pch.h"
#include "aabb.h"
#include "aabb.h"

AABB AABB::operator+(const AABB& other) const { return {min + other.min, max + other.max}; }
AABB AABB::operator-(const AABB& other) const { return {min - other.min, max - other.max}; }
AABB AABB::operator*(const Float scalar) const { return {min * scalar, max * scalar}; }
AABB AABB::operator/(const Float scalar) const { return {min / scalar, max / scalar}; }

AABB AABB::operator-() const { return {-min, -max}; }

AABB& AABB::operator+=(const AABB& other)
{
min += other.min;
max += other.max;
return *this;
}

AABB& AABB::operator-=(const AABB& other)
{
min -= other.min;
max -= other.max;
return *this;
}

AABB& AABB::operator*=(const Float scalar)
{
min *= scalar;
max *= scalar;
return *this;
}

AABB& AABB::operator/=(const Float scalar)
{
//TODO: perhaps check for division by zero?
min /= scalar;
max /= scalar;
return *this;
}
21 changes: 17 additions & 4 deletions Trajectory_Hotspots/Trajectory_Hotspots/aabb.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ class AABB
//Combine two AABBs with each other by keeping the extremes in all four directions
static AABB combine(const AABB& a, const AABB& b)
{
Float min_x = std::min(a.min.x, b.min.x);
Float min_y = std::min(a.min.y, b.min.y);
const Float min_x = std::min(a.min.x, b.min.x);
const Float min_y = std::min(a.min.y, b.min.y);

Float max_x = std::max(a.max.x, b.max.x);
Float max_y = std::max(a.max.y, b.max.y);
const Float max_x = std::max(a.max.x, b.max.x);
const Float max_y = std::max(a.max.y, b.max.y);

return AABB(Vec2(min_x, min_y), Vec2(max_x, max_y));
}
Expand Down Expand Up @@ -101,6 +101,19 @@ class AABB
return std::max(width(), height());
}

AABB operator+(const AABB& other) const;
AABB operator-(const AABB& other) const;
AABB operator*(const Float scalar) const;
AABB operator/(const Float scalar) const;

AABB& operator+=(const AABB& other);
AABB& operator-=(const AABB& other);
AABB& operator*=(const Float scalar);
AABB& operator/=(const Float scalar);

AABB operator-() const;


Vec2 min;
Vec2 max;

Expand Down
44 changes: 19 additions & 25 deletions Trajectory_Hotspots/Trajectory_Hotspots/float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Float& Float::operator=(const Float& other)
return *this;
}

Float& Float::operator=(float new_value)
Float& Float::operator=(const float new_value)
{
this->value = new_value;
return *this;
Expand All @@ -92,7 +92,7 @@ bool Float::operator==(const Float& other) const
return nearly_equal(value, other.value);
}

bool Float::operator==(float value) const
bool Float::operator==(const float value) const
{
return nearly_equal(this->value, value);
}
Expand All @@ -102,7 +102,7 @@ bool Float::operator!=(const Float& other) const
return !nearly_equal(this->value, other.value);
}

bool Float::operator!=(float value) const
bool Float::operator!=(const float value) const
{
return !nearly_equal(this->value, value);
}
Expand All @@ -112,18 +112,17 @@ bool Float::operator<(const Float& other) const
return !nearly_greater_or_equal(value, other.value);
}

bool Float::operator<(float value) const
bool Float::operator<(const float value) const
{
return !nearly_greater_or_equal(this->value, value);

}

bool Float::operator>(const Float& other) const
{
return nearly_greater(this->value, other.value);
}

bool Float::operator>(float value) const
bool Float::operator>(const float value) const
{
return nearly_greater(this->value, value);
}
Expand All @@ -133,18 +132,17 @@ bool Float::operator<=(const Float& other) const
return nearly_less_or_equal(this->value, other.value);
}

bool Float::operator<=(float value) const
bool Float::operator<=(const float value) const
{
return nearly_less_or_equal(this->value, value);

}

bool Float::operator>=(const Float& other) const
{
return nearly_greater_or_equal(this->value, other.value);
}

bool Float::operator>=(float value) const
bool Float::operator>=(const float value) const
{
return nearly_greater_or_equal(this->value, value);
}
Expand All @@ -154,7 +152,7 @@ Float Float::operator+(const Float& other) const
return Float(this->value + other.value);
}

Float Float::operator+(float value) const
Float Float::operator+(const float value) const
{
return Float(this->value + value);
}
Expand All @@ -164,33 +162,29 @@ Float Float::operator-(const Float& other) const
return Float(this->value - other.value);
}

Float Float::operator-(float value) const
Float Float::operator-(const float value) const
{
return Float(this->value - value);
}

Float Float::operator*(const Float& other) const
{
return Float(this->value * other.value);

}

Float Float::operator*(float value) const
Float Float::operator*(const float value) const
{
return Float(this->value * value);

}

Float Float::operator/(const Float& other) const
{
return Float(this->value / other.value);

}

Float Float::operator/(float value) const
Float Float::operator/(const float value) const
{
return Float(this->value / value);

}

Float& Float::operator+=(const Float& other)
Expand All @@ -199,9 +193,9 @@ Float& Float::operator+=(const Float& other)
return *this;
}

Float& Float::operator+=(float value)
Float& Float::operator+=(const float value)
{
value += value;
this->value += value;
return *this;
}

Expand All @@ -211,9 +205,9 @@ Float& Float::operator-=(const Float& other)
return *this;
}

Float& Float::operator-=(float value)
Float& Float::operator-=(const float value)
{
value -= value;
this->value -= value;
return *this;
}

Expand All @@ -223,9 +217,9 @@ Float& Float::operator*=(const Float& other)
return *this;
}

Float& Float::operator*=(float value)
Float& Float::operator*=(const float value)
{
value *= value;
this->value *= value;
return *this;
}

Expand All @@ -235,9 +229,9 @@ Float& Float::operator/=(const Float& other)
return *this;
}

Float& Float::operator/=(float value)
Float& Float::operator/=(const float value)
{
value /= value;
this->value /= value;
return *this;
}

Expand Down
4 changes: 2 additions & 2 deletions Trajectory_Hotspots/Trajectory_Hotspots/float.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ class Float
{
public:
Float() {};
Float(float value) : value(value) {};
Float(const float value) : value(value) {};
Float(const Float& other) : value(other.value) {};
~Float() {};

float get_value() const { return value; }
float set_value(float value) { this->value = value; }
float set_value(const float value) { this->value = value; }

Float& operator=(const Float& other);
Float& operator=(float value);
Expand Down
33 changes: 16 additions & 17 deletions Trajectory_Hotspots/Trajectory_Hotspots/segment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

Float Segment::length() const
{
Vec2 distance_vector = start - end;
const Vec2 distance_vector = start - end;
return distance_vector.length();
}

Float Segment::squared_length() const
{
Vec2 distance_vector = start - end;
const Vec2 distance_vector = start - end;
return distance_vector.squared_length();
}

Expand All @@ -38,7 +38,6 @@ const Vec2* Segment::get_top_point() const
{
return &end;
}

}

const Vec2* Segment::get_left_point() const
Expand Down Expand Up @@ -128,8 +127,8 @@ bool Segment::get_points_on_same_axis_with_distance_l(const Segment& start_segme
const Float start_axis_difference = axis ? start_segment.end.x - start_segment.start.x : start_segment.end.y - start_segment.start.y;
const Float end_axis_difference = axis ? end_segment.end.x - end_segment.start.x : end_segment.end.y - end_segment.start.y;

Float start_length = start_segment.length();
Float end_length = end_segment.length();
const Float start_length = start_segment.length();
const Float end_length = end_segment.length();

const Float determinant = start_axis_difference * end_length - end_axis_difference * start_length;

Expand Down Expand Up @@ -162,9 +161,9 @@ bool Segment::get_points_on_same_axis_with_distance_l(const Segment& start_segme
}

//Returns the y-coordinate of the intersection with the vertical line at x, or infinity if it lies on the segment
Float Segment::x_intersect(Float x) const
Float Segment::x_intersect(const Float x) const
{
Float x_diff = end.x - start.x;
const Float x_diff = end.x - start.x;

//Vertical segment, either no or infinite intersections
if (x_diff == 0.f)
Expand All @@ -173,17 +172,17 @@ Float Segment::x_intersect(Float x) const
}
else
{
Float segment_to_x = x - start.x;
const Float segment_to_x = x - start.x;
Float intersection_y = start.y + ((segment_to_x / x_diff) * (end.y - start.y));

return intersection_y;
}
}

//Returns the x-coordinate of the intersection with the horizontal line at y, or infinity if it lies on the segment
Float Segment::y_intersect(Float y) const
Float Segment::y_intersect(const Float y) const
{
Float y_diff = end.y - start.y;
const Float y_diff = end.y - start.y;

//Horizontal segment, either no or infinite intersections
if (y_diff == 0.f)
Expand All @@ -192,7 +191,7 @@ Float Segment::y_intersect(Float y) const
}
else
{
Float segment_to_y = y - start.y;
const Float segment_to_y = y - start.y;
Float intersection_x = start.x + ((segment_to_y / y_diff) * (end.x - start.x));

return intersection_x;
Expand Down Expand Up @@ -239,29 +238,29 @@ bool Segment::y_intersects(const Float y, Float& intersection_point_x) const

Float Segment::get_time_at_x(const Float x) const
{
Float x_fraction = (x - start.x) / (end.x - start.x);
const Float x_fraction = (x - start.x) / (end.x - start.x);
return start_t + (x_fraction * (end_t - start_t));
}

Float Segment::get_time_at_y(const Float y) const
{
Float y_fraction = (y - start.y) / (end.y - start.y);
const Float y_fraction = (y - start.y) / (end.y - start.y);
return start_t + (y_fraction * (end_t - start_t));
}

Float Segment::get_time_at_point(const Vec2& point) const
{
Vec2 start_to_point = point - start;
Float time_fraction = start_to_point.length() / length();
const Vec2 start_to_point = point - start;
const Float time_fraction = start_to_point.length() / length();
return start_t + (time_fraction * (end_t - start_t));
}

//Returns the point on the segment at a given time
Vec2 Segment::get_point_at_time(const Float time) const
{
Float fraction = (time - start_t) / (end_t - start_t);
const Float fraction = (time - start_t) / (end_t - start_t);

Vec2 vector_to_point = (end - start) * fraction;
const Vec2 vector_to_point = (end - start) * fraction;

return (start + vector_to_point);
}
Expand Down
6 changes: 3 additions & 3 deletions Trajectory_Hotspots/Trajectory_Hotspots/segment.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ class Segment

}

Segment(const Vec2 start, const Vec2 end, const Float start_t, const Float end_t) :
Segment(const Vec2& start, const Vec2& end, const Float start_t, const Float end_t) :
start(start), end(end), start_t(start_t), end_t(end_t)
{

}

Segment(const Vec2 start, const Vec2 end, const Float start_t) :
Segment(const Vec2& start, const Vec2& end, const Float start_t) :
start(start), end(end), start_t(start_t)
{
end_t = start_t + this->length();
}

Segment(const Vec2 start, const Vec2 end) :
Segment(const Vec2& start, const Vec2& end) :
start(start), end(end), start_t(0.f), end_t(0.f)
{
}
Expand Down
Loading

0 comments on commit 3b24ff9

Please sign in to comment.