paella/src/Geometry/ProjectionTest.cpp

136 lines
3.8 KiB
C++

////////////////////////////////////////////////////////////////////////////////
//
// Paella
// Copyright (C) 2015 - Thomas FORGIONE, Emilie JALRAS, Marion LENFANT, Thierry MALON, Amandine PAILLOUX
// Authors :
// Thomas FORGIONE
// Emilie JALRAS
// Marion LENFANT
// Thierry MALON
// Amandine PAILLOUX
//
// This file is part of the project Paella
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <vector>
#include <Geometry/Vector.hpp>
#include <Geometry/MathFunctions.hpp>
std::size_t total = 0, failed = 0;
template<typename T>
std::ostream& operator<<(std::ostream& out, std::vector<T> const& vector)
{
for (auto const& v : vector)
{
out << v << std::endl;
}
return out;
}
void test(bool b)
{
if (b)
{
std::cout << "\033[32mTest succeded !\033[0m" << std::endl;
}
else
{
std::cout << "\033[31mTest failed !\033[0m" << std::endl;
failed++;
}
total++;
}
void printResult()
{
if (failed == 0)
{
std::cout << "\033[32mAll tests worked correctly !\033[0m" << std::endl;
}
else
{
std::cout << "\033[31m" << failed << " test"
<< (failed > 1 ? "s" : "")
<< " didn't work...\033[0m" << std::endl;
}
}
int main()
{
geo::Vector3<float> element1{0.0f,0.0f,0.0f};
std::vector<geo::Vector3<float>> space1 = {
{1.0f, 0.0f, 0.0f},
{0.0f, 2.0f, 0.0f},
{0.0f, 0.0f, 3.0f},
{-0.5f, 0.2f, 0.8f},
{0.6f, -0.8f, 0.0f}
};
auto const it1 = geo::project(element1, std::begin(space1), std::end(space1),
[] (geo::Vector3<float> const& v1, geo::Vector3<float> const& v2)
{
return (v1-v2).norm2();
}
);
test(std::distance(std::begin(space1), it1) == 3);
geo::Vector2f element2{0.0f,0.0f};
std::vector<geo::Vector3<float>> space2 = {
{1.0f, 2.0f, 3.0f},
{1.0f, 0.0f, -2.0f},
{0.0f, 1.0f, -1.0f},
{1.0f, 1.0f, 2.5f}
};
auto const it2 = geo::project(element2, std::begin(space2), std::end(space2),
[] (geo::Vector2f const& v, geo::Vector3<float> const& d)
{
return std::abs(d.x()*v.x()+d.y()*v.y()+d.z())/d.norm();
}
);
test(std::distance(std::begin(space2), it2) == 2);
geo::Vector2f element3{2.0f,2.0f};
std::vector<geo::Segment<float,2>> space3 = {
{geo::Vector2f{0.0f,0.0f},geo::Vector2f{1.0f,0.0f}},
{geo::Vector2f{0.0f,1.0f},geo::Vector2f{0.0f,3.0f}},
{geo::Vector2f{3.0f,0.0f},geo::Vector2f{4.0f,1.0f}},
{geo::Vector2f{3.0f,3.0f},geo::Vector2f{4.0f,3.0f}}
};
auto const it3 = geo::project(element3, std::begin(space3), std::end(space3),
[] (geo::Vector2f const& p, geo::Segment<float,2> const& s)
{
return geo::distanceToSegment(p, s.first, s.second);
}
);
test(std::distance(std::begin(space3), it3) == 3);
printResult();
return 0;
}