diff --git a/Trajectory_Hotspots/Trajectory_Hotspots/aabb.cpp b/Trajectory_Hotspots/Trajectory_Hotspots/aabb.cpp index 7bc9b4b..6a3d237 100644 --- a/Trajectory_Hotspots/Trajectory_Hotspots/aabb.cpp +++ b/Trajectory_Hotspots/Trajectory_Hotspots/aabb.cpp @@ -1,2 +1,38 @@ #include "pch.h" -#include "aabb.h" \ No newline at end of file +#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; +} diff --git a/Trajectory_Hotspots/Trajectory_Hotspots/aabb.h b/Trajectory_Hotspots/Trajectory_Hotspots/aabb.h index 75e754c..50e590d 100644 --- a/Trajectory_Hotspots/Trajectory_Hotspots/aabb.h +++ b/Trajectory_Hotspots/Trajectory_Hotspots/aabb.h @@ -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)); } @@ -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; diff --git a/Trajectory_Hotspots/Trajectory_Hotspots/float.cpp b/Trajectory_Hotspots/Trajectory_Hotspots/float.cpp index 7a4d5c2..8d89953 100644 --- a/Trajectory_Hotspots/Trajectory_Hotspots/float.cpp +++ b/Trajectory_Hotspots/Trajectory_Hotspots/float.cpp @@ -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; @@ -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); } @@ -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); } @@ -112,10 +112,9 @@ 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 @@ -123,7 +122,7 @@ 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); } @@ -133,10 +132,9 @@ 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 @@ -144,7 +142,7 @@ 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); } @@ -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); } @@ -164,7 +162,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); } @@ -172,25 +170,21 @@ Float Float::operator-(float value) const 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) @@ -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; } @@ -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; } @@ -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; } @@ -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; } diff --git a/Trajectory_Hotspots/Trajectory_Hotspots/float.h b/Trajectory_Hotspots/Trajectory_Hotspots/float.h index b8face8..cb4c2f4 100644 --- a/Trajectory_Hotspots/Trajectory_Hotspots/float.h +++ b/Trajectory_Hotspots/Trajectory_Hotspots/float.h @@ -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); diff --git a/Trajectory_Hotspots/Trajectory_Hotspots/segment.cpp b/Trajectory_Hotspots/Trajectory_Hotspots/segment.cpp index e28492a..f107d4c 100644 --- a/Trajectory_Hotspots/Trajectory_Hotspots/segment.cpp +++ b/Trajectory_Hotspots/Trajectory_Hotspots/segment.cpp @@ -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(); } @@ -38,7 +38,6 @@ const Vec2* Segment::get_top_point() const { return &end; } - } const Vec2* Segment::get_left_point() const @@ -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; @@ -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) @@ -173,7 +172,7 @@ 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; @@ -181,9 +180,9 @@ Float Segment::x_intersect(Float x) const } //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) @@ -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; @@ -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); } diff --git a/Trajectory_Hotspots/Trajectory_Hotspots/segment.h b/Trajectory_Hotspots/Trajectory_Hotspots/segment.h index 894f99d..849c750 100644 --- a/Trajectory_Hotspots/Trajectory_Hotspots/segment.h +++ b/Trajectory_Hotspots/Trajectory_Hotspots/segment.h @@ -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) { } diff --git a/Trajectory_Hotspots/Trajectory_Hotspots/segment_search_tree.cpp b/Trajectory_Hotspots/Trajectory_Hotspots/segment_search_tree.cpp index 7394c7e..200d92b 100644 --- a/Trajectory_Hotspots/Trajectory_Hotspots/segment_search_tree.cpp +++ b/Trajectory_Hotspots/Trajectory_Hotspots/segment_search_tree.cpp @@ -5,13 +5,13 @@ //{ //} -//Build the tree bottom-up from a lsit of ordered segments +//Build the tree bottom-up from a list of ordered segments Segment_Search_Tree_Node::Segment_Search_Tree_Node(const std::vector& ordered_segments, const size_t start_index, const size_t end_index) : segment_list(ordered_segments) { if (end_index == start_index) { //Leaf node - segment_index = (int)start_index; + segment_index = static_cast(start_index); const Segment& segment = segment_list.at(segment_index); @@ -40,7 +40,7 @@ Segment_Search_Tree_Node::Segment_Search_Tree_Node(const std::vector& o } } -//Query tree, reutrns bounding box from start_t to end_t +//Query tree, returns bounding box from start_t to end_t AABB Segment_Search_Tree_Node::query(const Float start_t, const Float end_t) const { //TODO: Pass bounding box as ref to avoid construction? @@ -202,5 +202,4 @@ int Segment_Search_Tree_Node::query(const Float t) const Segment_Search_Tree::Segment_Search_Tree(const std::vector& ordered_segments) : root(ordered_segments, 0, ordered_segments.size() - 1) { - } \ No newline at end of file diff --git a/Trajectory_Hotspots/Trajectory_Hotspots/segment_search_tree.h b/Trajectory_Hotspots/Trajectory_Hotspots/segment_search_tree.h index a4deaee..8993b45 100644 --- a/Trajectory_Hotspots/Trajectory_Hotspots/segment_search_tree.h +++ b/Trajectory_Hotspots/Trajectory_Hotspots/segment_search_tree.h @@ -40,7 +40,7 @@ class Segment_Search_Tree //Build the tree bottom-up from a list of ordered segments Segment_Search_Tree(const std::vector& ordered_segments); - //Query tree, reutrns bounding box from start_t to end_t + //Query tree, returns bounding box from start_t to end_t AABB query(const Float start_t, const Float end_t) const { return root.query(start_t, end_t); diff --git a/Trajectory_Hotspots/Trajectory_Hotspots/vec2.cpp b/Trajectory_Hotspots/Trajectory_Hotspots/vec2.cpp index 2cc110f..c20f0a3 100644 --- a/Trajectory_Hotspots/Trajectory_Hotspots/vec2.cpp +++ b/Trajectory_Hotspots/Trajectory_Hotspots/vec2.cpp @@ -32,7 +32,7 @@ void Vec2::normalize() Vec2 Vec2::normalized() const { //Multiply by reciprocal to prevent two divides - Float rl = 1.f / length(); + const Float rl = 1.f / length(); return Vec2(x * rl, y * rl); } @@ -43,11 +43,37 @@ Vec2 Vec2::operator/(const Float& scalar) const { return Vec2(x / scalar, y / sc Vec2 Vec2::operator-() const { return Vec2(-x, -y); } -Vec2 Vec2::operator+=(const Vec2& other) const { return Vec2(x + other.x, y + other.y); } -Vec2 Vec2::operator-=(const Vec2& other) const { return Vec2(x - other.x, y - other.y); } -Vec2 Vec2::operator*=(const Float& scalar) const { return Vec2(x * scalar, y * scalar); } -Vec2 Vec2::operator/=(const Float& scalar) const { return Vec2(x / scalar, y / scalar); } +Vec2& Vec2::operator+=(const Vec2& other) +{ + x += other.x; + y += other.y; + return *this; +} + +Vec2& Vec2::operator-=(const Vec2& other) +{ + x -= other.x; + y -= other.y; + return *this; +} +Vec2& Vec2::operator*=(const Float& scalar) +{ + x *= scalar; + y *= scalar; + return *this; +} + +Vec2& Vec2::operator/=(const Float& scalar) +{ + if (scalar == 0.f) + { + throw std::runtime_error("Can't divide by zero"); + } + x /= scalar; + y /= scalar; + return *this; +} bool Vec2::operator==(const Vec2& operand) const { return x == operand.x && y == operand.y; } bool Vec2::operator!=(const Vec2& operand) const { return !(*this == operand); } diff --git a/Trajectory_Hotspots/Trajectory_Hotspots/vec2.h b/Trajectory_Hotspots/Trajectory_Hotspots/vec2.h index df2ceb0..1240358 100644 --- a/Trajectory_Hotspots/Trajectory_Hotspots/vec2.h +++ b/Trajectory_Hotspots/Trajectory_Hotspots/vec2.h @@ -30,10 +30,10 @@ class Vec2 Vec2 operator-() const; - Vec2 operator+=(const Vec2& other) const; - Vec2 operator-=(const Vec2& other) const; - Vec2 operator*=(const Float& scalar) const; - Vec2 operator/=(const Float& scalar) const; + Vec2& operator+=(const Vec2& other); + Vec2& operator-=(const Vec2& other); + Vec2& operator*=(const Float& scalar); + Vec2& operator/=(const Float& scalar); bool operator==(const Vec2& operand) const; bool operator!=(const Vec2& operand) const;