Skip to content

Commit

Permalink
Fixup: avoid inheritance (To be removed/reworked)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwestphal committed Feb 22, 2025
1 parent 9e868f5 commit 198577b
Showing 1 changed file with 79 additions and 52 deletions.
131 changes: 79 additions & 52 deletions library/public/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,52 +82,70 @@ class F3D_EXPORT ratio_t
};

/**
* A base template type for an array of double
* Describe a RGB color.
*/
template<unsigned int N>
class F3D_EXPORT double_array_t
class F3D_EXPORT color_t
{
public:
double_array_t() = default;
double_array_t(const std::vector<double>& vec)
color_t(double red, double green, double blue)
{
(*this)[0] = red;
(*this)[1] = green;
(*this)[2] = blue;
}
double r() const
{
return (*this)[0];
}
double g() const
{
if (vec.size() != N)
return (*this)[1];
}
double b() const
{
return (*this)[2];
}

color_t() = default;
color_t(const std::vector<double>& vec)
{
if (vec.size() != 3)
{
throw f3d::type_construction_exception("Provided vector does not have the right size");
}
std::copy_n(vec.begin(), N, this->Array.begin());
std::copy_n(vec.begin(), 3, this->Array.begin());
}
double_array_t(const std::initializer_list<double>& list)
color_t(const std::initializer_list<double>& list)
{
if (list.size() != N)
if (list.size() != 3)
{
throw f3d::type_construction_exception("Provided list does not have the right size");
}
std::copy_n(list.begin(), N, this->Array.begin());
std::copy_n(list.begin(), 3, this->Array.begin());
}
operator std::vector<double>() const
{
return std::vector<double>(this->Array.begin(), this->Array.end());
}
bool operator==(const double_array_t& other) const
bool operator==(const color_t& other) const
{
return this->Array == other.Array;
}
bool operator!=(const double_array_t& other) const
bool operator!=(const color_t& other) const
{
return this->Array != other.Array;
}
double operator[](size_t i) const
{
if (i >= N)
if (i >= 3)
{
throw f3d::type_access_exception("Incorrect index");
}
return this->Array[i];
}
double& operator[](size_t i)
{
if (i >= N)
if (i >= 3)
{
throw f3d::type_access_exception("Incorrect index");
}
Expand All @@ -139,77 +157,86 @@ class F3D_EXPORT double_array_t
}

private:
std::array<double, N> Array{ 0 };
std::array<double, 3> Array{ 0 };
};

/**
* Describe a RGB color.
* Describe a 3D direction.
*/
class F3D_EXPORT color_t : public double_array_t<3>
class F3D_EXPORT direction_t
{
public:
color_t() = default;
color_t(const std::vector<double>& vec)
: double_array_t(vec)
{
}
color_t(const std::initializer_list<double>& list)
: double_array_t(list)
{
}
color_t(double red, double green, double blue)
direction_t(double x, double y, double z)
{
(*this)[0] = red;
(*this)[1] = green;
(*this)[2] = blue;
(*this)[0] = x;
(*this)[1] = y;
(*this)[2] = z;
}
double r() const
double x() const
{
return (*this)[0];
}
double g() const
double y() const
{
return (*this)[1];
}
double b() const
double z() const
{
return (*this)[2];
}
};

/**
* Describe a 3D direction.
*/
class F3D_EXPORT direction_t : public double_array_t<3>
{
public:
direction_t() = default;
direction_t(const std::vector<double>& vec)
: double_array_t(vec)
{
if (vec.size() != 3)
{
throw f3d::type_construction_exception("Provided vector does not have the right size");
}
std::copy_n(vec.begin(), 3, this->Array.begin());
}
direction_t(const std::initializer_list<double>& list)
: double_array_t(list)
{
if (list.size() != 3)
{
throw f3d::type_construction_exception("Provided list does not have the right size");

Check warning on line 201 in library/public/types.h

View check run for this annotation

Codecov / codecov/patch

library/public/types.h#L201

Added line #L201 was not covered by tests
}
std::copy_n(list.begin(), 3, this->Array.begin());
}
direction_t(double x, double y, double z)
operator std::vector<double>() const
{
(*this)[0] = x;
(*this)[1] = y;
(*this)[2] = z;
return std::vector<double>(this->Array.begin(), this->Array.end());
}
double x() const
bool operator==(const direction_t& other) const
{
return (*this)[0];
return this->Array == other.Array;
}
double y() const
bool operator!=(const direction_t& other) const
{
return (*this)[1];
return this->Array != other.Array;
}
double z() const
double operator[](size_t i) const
{
return (*this)[2];
if (i >= 3)
{
throw f3d::type_access_exception("Incorrect index");

Check warning on line 221 in library/public/types.h

View check run for this annotation

Codecov / codecov/patch

library/public/types.h#L221

Added line #L221 was not covered by tests
}
return this->Array[i];
}
double& operator[](size_t i)
{
if (i >= 3)
{
throw f3d::type_access_exception("Incorrect index");

Check warning on line 229 in library/public/types.h

View check run for this annotation

Codecov / codecov/patch

library/public/types.h#L229

Added line #L229 was not covered by tests
}
return this->Array[i];
}
const double* data() const
{
return this->Array.data();
}

private:
std::array<double, 3> Array{ 0 };
};

/**
Expand Down

0 comments on commit 198577b

Please sign in to comment.