Skip to content

Commit

Permalink
Makes it easier to call cross.
Browse files Browse the repository at this point in the history
  • Loading branch information
pazmivaniye committed Jul 12, 2024
1 parent 7a3aacc commit 067b51f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion PAZ_Math
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ namespace paz
Mat operator/(double rhs) const;
Mat operator-() const;
double dot(const MatRef& rhs) const;
Vec cross(const Vec& rhs) const;
MatRef block(std::size_t startRow, std::size_t startCol, std::size_t
numRows, std::size_t numCols) const;
void setBlock(std::size_t startRow, std::size_t startCol, std::size_t
Expand Down Expand Up @@ -237,7 +238,6 @@ namespace paz
MatRef tail(std::size_t n) const;
void setTail(std::size_t n, const Vec& rhs);
void resize(std::size_t newRows);
Vec cross(const Vec& rhs) const;
};

class MatRef
Expand Down
11 changes: 11 additions & 0 deletions matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,17 @@ double paz::Mat::dot(const MatRef& rhs) const
return res;
}

paz::Vec paz::Mat::cross(const Vec& rhs) const
{
if(rows() != 3 || cols() != 1 || rhs.rows() != 3 || rhs.cols() != 1)
{
throw std::runtime_error("Not a 3-vector.");
}
return {{operator()(1)*rhs(2) - operator()(2)*rhs(1),
operator()(2)*rhs(0) - operator()(0)*rhs(2),
operator()(0)*rhs(1) - operator()(1)*rhs(0)}};
}

paz::Mat paz::Mat::operator-(const MatRef& rhs) const
{
auto res = *this;
Expand Down
2 changes: 1 addition & 1 deletion test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ int main()
z.setHead(2, z.tail(2));
PRINT(z)

PRINT(static_cast<paz::Vec>(m.row(0).trans()).cross(m.row(1).trans()))
PRINT(m.row(0).trans().cross(m.row(1).trans()))
PRINT(paz::Mat::Cross(m.row(0).trans())*m.row(1).trans())

const paz::Mat x = paz::Mat::Cross(m.row(0).trans())*m.row(1).trans();
Expand Down
11 changes: 0 additions & 11 deletions vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,3 @@ void paz::Vec::resize(std::size_t newRows)
{
resizeRows(newRows);
}

paz::Vec paz::Vec::cross(const Vec& rhs) const
{
if(size() != 3 || rhs.size() != 3)
{
throw std::runtime_error("Not a 3-vector.");
}
return {{(*this)(1)*rhs(2) - (*this)(2)*rhs(1),
(*this)(2)*rhs(0) - (*this)(0)*rhs(2),
(*this)(0)*rhs(1) - (*this)(1)*rhs(0)}};
}

0 comments on commit 067b51f

Please sign in to comment.