Ajout de Makefiles et de Skeleton

This commit is contained in:
Thomas FORGIONE 2015-02-05 11:38:30 +01:00
parent 79e842f60d
commit 9512e708c8
17 changed files with 754 additions and 0 deletions

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,21 @@
SOURCES = Calibration.cpp
OBJECTS = $(SOURCES:.cpp=.o)
TARGET = main
LIBS = $(shell pkg-config opencv --libs)
CXX = g++
%.o: %.cpp
$(CXX) -std=c++14 -o $@ -c $< -Wall `pkg-config opencv --cflags`
all: $(OBJECTS)
$(CXX) -std=c++14 -o $(TARGET) $(OBJECTS) $(LIBS) -Wall
x: all
./$(TARGET)
clean:
rm -rf $(OBJECTS)
superclean: clean
rm -rf $(TARGET)

6
Code/src/Calibration/launch.sh Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/bash
if [ ! -f main ]; then
make
fi
./main -w 5 -h 5 -V ../../data/video/calib.mov -o ../../data/xml/calib.xml

View File

@ -0,0 +1,21 @@
SOURCES = DetectionAndMatching.cpp
OBJECTS = $(SOURCES:.cpp=.o)
TARGET = main
LIBS = $(shell pkg-config opencv --libs)
CXX = g++
%.o: %.cpp
$(CXX) -std=c++14 -o $@ -c $< -Wall `pkg-config opencv --cflags`
all: $(OBJECTS)
$(CXX) -std=c++14 -o $(TARGET) $(OBJECTS) $(LIBS) -Wall
x: all
./$(TARGET)
clean:
rm -rf $(OBJECTS)
superclean: clean
rm -rf $(TARGET)

View File

@ -0,0 +1,21 @@
SOURCES = DetectionAndMatchingSift.cpp
OBJECTS = $(SOURCES:.cpp=.o)
TARGET = main
LIBS = $(shell pkg-config opencv --libs)
CXX = g++
%.o: %.cpp
$(CXX) -std=c++14 -o $@ -c $< -Wall `pkg-config opencv --cflags`
all: $(OBJECTS)
$(CXX) -std=c++14 -o $(TARGET) $(OBJECTS) $(LIBS) -Wall
x: all
./$(TARGET)
clean:
rm -rf $(OBJECTS)
superclean: clean
rm -rf $(TARGET)

Binary file not shown.

21
Code/src/HelloCV/Makefile Normal file
View File

@ -0,0 +1,21 @@
SOURCES = HelloCV.cpp
OBJECTS = $(SOURCES:.cpp=.o)
TARGET = main
LIBS = $(shell pkg-config opencv --libs)
CXX = g++
%.o: %.cpp
$(CXX) -std=c++14 -o $@ -c $< -Wall `pkg-config opencv --cflags`
all: $(OBJECTS)
$(CXX) -std=c++14 -o $(TARGET) $(OBJECTS) $(LIBS) -Wall
x: all
./$(TARGET)
clean:
rm -rf $(OBJECTS)
superclean: clean
rm -rf $(TARGET)

View File

@ -0,0 +1,8 @@
#include <iostream>
#include <fstream>
#include "Box.hpp"
Box::Box() : m_x_min{}, m_x_max{}, m_y_min{}, m_y_max{}
{
}

15
Code/src/Skeleton/Box.hpp Normal file
View File

@ -0,0 +1,15 @@
#ifndef BOX_HPP
#define BOX_HPP
class Box
{
public:
Box();
float m_x_min;
float m_x_max;
float m_y_min;
float m_y_max;
};
#endif //BOX_HPP

View File

@ -0,0 +1,67 @@
#include <iostream>
#include <cmath>
#include <GL/glu.h>
#include <boost/algorithm/clamp.hpp>
#include "VectorFunctions.hpp"
#include "FreeFlyCamera.hpp"
FreeFlyCamera::FreeFlyCamera() : speed(0.1), sensitivity(0.001), theta(0), phi(0)
{
vectorsFromAngles();
}
void FreeFlyCamera::nextStep(int width, int height)
{
// Gestion de la souris
sf::Vector2i mouse_position = sf::Mouse::getPosition();
theta += sensitivity * (width/2-mouse_position.x);
phi += sensitivity * (height/2-mouse_position.y);
vectorsFromAngles();
// Gestion du clavier
sf::Vector3<double> move;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
{
move += forward * speed;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
move -= forward * speed;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
{
move += left * speed;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
move -= left * speed;
}
position += move;
target += move;
}
void FreeFlyCamera::vectorsFromAngles()
{
phi = boost::algorithm::clamp(phi,-PI_2_MINUS_EPSILON,PI_2_MINUS_EPSILON);
// Passage du sphérique au cartésien
double r_temp = std::cos(phi);
forward.z = std::sin(phi);
forward.x = r_temp * std::cos(theta);
forward.y = r_temp * std::sin(theta);
left = crossProduct(sf::Vector3<double>(0,0,1), forward);
normalize(left);
target = position + forward;
}
void FreeFlyCamera::look() const
{
gluLookAt(position.x, position.y, position.z,
target.x, target.y, target.z,
0,0,1);
}

View File

@ -0,0 +1,33 @@
#ifndef FREE_FLY_CAMERA_HPP
#define FREE_FLY_CAMERA_HPP
#include <SFML/System/Vector3.hpp>
#include <SFML/Window/Event.hpp>
constexpr auto PI_2_MINUS_EPSILON = M_PI_2 - 0.1;
class FreeFlyCamera
{
public:
FreeFlyCamera();
void look() const;
void nextStep(int width, int height);
private:
double speed;
double sensitivity;
sf::Vector3<double> position;
sf::Vector3<double> target;
sf::Vector3<double> forward;
sf::Vector3<double> left;
double theta;
double phi;
void vectorsFromAngles();
};
#endif // FREE_FLY_CAMERA_HPP

View File

@ -0,0 +1,21 @@
SOURCES = Box.cpp Skeleton.cpp FreeFlyCamera.cpp main.cpp
OBJECTS = $(SOURCES:.cpp=.o)
TARGET = main
LIBS = -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio -lGL -lGLU -lglut
CXX = g++
%.o: %.cpp
$(CXX) -std=c++14 -o $@ -c $< -Wall
all: $(OBJECTS)
$(CXX) -std=c++14 -o $(TARGET) $(OBJECTS) $(LIBS) -Wall
x: all
./$(TARGET)
clean:
rm -rf $(OBJECTS)
superclean: clean
rm -rf $(TARGET)

View File

@ -0,0 +1,253 @@
#include <iostream>
#include <fstream>
#include <algorithm>
#include <GL/gl.h>
#include "Skeleton.hpp"
#include "Box.hpp"
std::stringstream Skeleton::stream{};
Skeleton::Skeleton() : m_vertices{}, m_edges{}, m_neighbourgs_counters{}, m_junctions{}, m_bounding_box{}
{
}
void Skeleton::loadFromFile(std::string const& path)
{
std::ifstream file{path};
std::string line;
while (std::getline(file, line))
{
if (line.size() == 0)
continue;
char first = line[0];
line[0]=' ';
stream.clear();
stream.str(line);
switch (first)
{
case 'v':
float x;
float y;
float r;
stream >> x >> y >> r;
m_vertices.push_back(Vertex{x,y,r});
break;
case 'e':
unsigned int v1;
unsigned int v2;
stream >> v1 >> v2;
m_edges.push_back(Edge{v1-1,v2-1});
break;
default:
break;
}
}
}
void Skeleton::loadFromVectors(std::vector<Vertex> const& vertices, std::vector<unsigned int> const& path)
{
m_vertices = vertices;
for (unsigned int i = 0; i < path.size() - 1; i++)
{
m_edges.push_back(Edge{path[i], path[i+1]});
}
}
void Skeleton::countNeighbourgs()
{
m_neighbourgs_counters.reserve(m_vertices.size());
for (auto i = 0u; i < m_vertices.size(); i++)
{
m_neighbourgs_counters.push_back(0);
}
for (auto const& edge : m_edges)
{
m_neighbourgs_counters[edge.v1]++;
m_neighbourgs_counters[edge.v2]++;
}
}
void Skeleton::draw() const
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2,GL_FLOAT,sizeof(Vertex),&m_vertices[0]);
glDrawElements(GL_LINES,m_edges.size()*2,GL_UNSIGNED_INT,&m_edges[0]);
glDisableClientState(GL_VERTEX_ARRAY);
}
void Skeleton::computeJunctions()
{
countNeighbourgs();
for (unsigned int i = 0; i < m_vertices.size(); i++)
{
if (m_neighbourgs_counters[i]>2 || m_neighbourgs_counters[i]==1)
{
m_junctions.push_back(Junction{i});
}
}
for (auto& junction : m_junctions)
{
for (auto const& edge : m_edges)
{
if (edge.v1 == junction.center)
{
junction.neighbourgs.push_back(edge.v2);
}
if (edge.v2 == junction.center)
{
junction.neighbourgs.push_back(edge.v1);
}
}
}
}
std::vector<Skeleton> Skeleton::split()
{
std::vector<Skeleton> branches;
std::vector<std::vector<bool>> connections;
if (m_junctions.size() == 0)
computeJunctions();
computeBoundingBox();
for (unsigned int i = 0; i < m_vertices.size(); i++)
{
connections.push_back(std::vector<bool>{});
for (unsigned int j = 0; j <= i; j++)
{
connections[i].push_back(false);
}
}
for(auto const& edge : m_edges)
{
auto v1 = edge.v1;
auto v2 = edge.v2;
connections[std::max(v1,v2)][std::min(v1,v2)]= true;
}
for (unsigned int i = 0; i < m_vertices.size(); i++)
{
if (m_neighbourgs_counters[i]>2 || m_neighbourgs_counters[i]==1)
{
unsigned int numberOfBranchesFound = 0;
std::vector <unsigned int> onesPosition = findOnes(connections,i);
for (auto const& pathNumber : onesPosition)
{
std::vector<unsigned int> path;
path.push_back(i);
path.push_back(pathNumber);
auto j = 0u;
unsigned int k;
// auto counter = 0u;
do
{
addNextPoint(connections, path);
j++;
k = path[path.size()-1];
} while (m_neighbourgs_counters[k] == 2);
branches.push_back(Skeleton{});
branches[branches.size()-1].loadFromVectors(m_vertices, path);
branches[branches.size()-1].m_bounding_box = m_bounding_box;
numberOfBranchesFound++;
}
}
}
return branches;
}
void Skeleton::addNextPoint(std::vector<std::vector<bool>> const& connections, std::vector<unsigned int>& path) const
{
auto lastPoint = path[path.size()-1];
auto nextPointFound = false;
auto i = 0u;
while (!nextPointFound)
{
if (i >= m_vertices.size())
{
break;
}
if (lastPoint != i && connections[std::max(i,lastPoint)][std::min(i,lastPoint)]
&& std::find(std::begin(path), std::end(path), i) == std::end(path))
{
path.push_back(i);
nextPointFound = true;
}
i++;
}
}
std::vector<unsigned int> Skeleton::findOnes(std::vector<std::vector<bool>> const& connections, unsigned int lineNumber) const
{
std::vector<unsigned int> onesPosition;
unsigned int numberOfOnesToFind = m_neighbourgs_counters[lineNumber];
unsigned int numberOfOnesFound = 0;
unsigned int counter = 0;
while (numberOfOnesFound< numberOfOnesToFind && counter < m_vertices.size())
{
if (connections[std::max(counter,lineNumber)][std::min(counter,lineNumber)])
{
onesPosition.push_back(counter);
numberOfOnesFound++;
}
counter++;
}
return onesPosition;
}
void Skeleton::computeBoundingBox()
{
m_bounding_box.m_x_min = m_vertices[0].x;
m_bounding_box.m_x_max = m_vertices[0].x;
m_bounding_box.m_y_min = m_vertices[0].y;
m_bounding_box.m_y_max = m_vertices[0].y;
for (auto const& vertex : m_vertices)
{
m_bounding_box.m_x_min = std::min(m_bounding_box.m_x_min,vertex.x);
m_bounding_box.m_x_max = std::max(m_bounding_box.m_x_max,vertex.x);
m_bounding_box.m_y_min = std::min(m_bounding_box.m_y_min,vertex.y);
m_bounding_box.m_y_max = std::max(m_bounding_box.m_y_max,vertex.y);
}
std::cout << "[" << m_bounding_box.m_x_min << "," << m_bounding_box.m_x_max << "] "
<< "[" << m_bounding_box.m_y_min << "," << m_bounding_box.m_y_max << "]"
<< std::endl;
}
std::ostream& operator<<(std::ostream& out, Skeleton const& s)
{
for (auto const& vertex : s.m_vertices)
{
out << "v " << vertex.x << " " << vertex.y << " " << vertex.r << "\n";
}
out << "\n";
for (auto const& edge : s.m_edges)
{
out << "e " << edge.v1 << " " << edge.v2 << "\n";
}
return out;
}

View File

@ -0,0 +1,58 @@
#ifndef SKELETON_HPP
#define SKELETON_HPP
#include <string>
#include <vector>
#include <sstream>
#include "Box.hpp"
class Vertex
{
public:
float x;
float y;
float r;
};
class Edge
{
public:
unsigned int v1;
unsigned int v2;
};
class Junction
{
public:
Junction(unsigned int i) : center{i}, neighbourgs{} {};
unsigned int center;
std::vector<unsigned int> neighbourgs;
};
class Skeleton
{
public:
Skeleton();
void loadFromFile(std::string const& path);
void draw() const;
void loadFromVectors(std::vector<Vertex> const& vertices, std::vector<unsigned int> const& path);
void countNeighbourgs();
void computeJunctions();
std::vector<Skeleton> split();
friend std::ostream& operator<<(std::ostream& out, Skeleton const& s);
void computeBoundingBox();
private:
void addNextPoint(std::vector<std::vector<bool>> const& connections, std::vector<unsigned int>& path) const;
std::vector<unsigned int> findOnes(std::vector<std::vector<bool>> const& connections, unsigned int lineNumber) const;
std::vector<Vertex> m_vertices;
std::vector<Edge> m_edges;
std::vector<unsigned int> m_neighbourgs_counters;
std::vector<Junction> m_junctions;
Box m_bounding_box;
static std::stringstream stream;
};
#endif // SKELETON_HPP

View File

@ -0,0 +1,48 @@
#ifndef VECTOR_FUNCTIONS_HPP
#define VECTOR_FUNCTIONS_HPP
#include <cmath>
#include <SFML/System/Vector3.hpp>
using sf::Vector3;
template <class T>
Vector3<T> makeVector(Vector3<T> const& from, Vector3<T> const& to)
{
return to - from;
}
template <class T>
double dotProduct(Vector3<T> const& v1, Vector3<T> const& v2)
{
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
template <class T>
double norm2(Vector3<T> const& vecteur)
{
return dotProduct(vecteur, vecteur);
}
template <class T>
double norm(Vector3<T> const& vecteur)
{
return sqrt(norm2(vecteur));
}
template <class T>
void normalize(Vector3<T>& vecteur)
{
vecteur /= norm(vecteur);
}
template <class T>
Vector3<T> crossProduct(Vector3<T> const& v1, Vector3<T> const& v2)
{
return Vector3<T>{v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x
};
}
#endif // VECTOR_FUNCTIONS_HPP

161
Code/src/Skeleton/main.cpp Normal file
View File

@ -0,0 +1,161 @@
#include <iostream>
#include <vector>
#include <memory>
#include <cmath>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <GL/gl.h>
#include <GL/glu.h>
#include "FreeFlyCamera.hpp"
#include "Skeleton.hpp"
// Full hd : 1920x1080
constexpr auto WIDTH = 1920;
constexpr auto HEIGHT = 1080;
void drawScene(FreeFlyCamera const& camera, std::vector<Skeleton> const& s, std::vector<std::array<float,3>> const& colors);
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cout << "I need a skeleton !" << std::endl;
return -1;
}
std::srand(std::time(0));
// Création de la fenêtre
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT),
"Skeleton",
sf::Style::Fullscreen,
sf::ContextSettings(32));
window.setVerticalSyncEnabled(true);
window.setMouseCursorVisible(false);
//Initialisation du mode 3D
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70,(double)WIDTH/HEIGHT,0.2,1000);
// Initialisation de Z-Buffer
glEnable(GL_DEPTH_TEST);
// Activer les textures
glEnable(GL_TEXTURE_2D);
// Autoriser la transparence
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Ces 2 lignes améliorent le rendu
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
// On efface le tampon d'affichage
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glFlush();
// Init mesh
Skeleton s;
s.loadFromFile(std::string{argv[1]});
s.computeJunctions();
std::vector<Skeleton> split = s.split();
std::vector<std::array<float,3>> colors;
for (auto i = 0u; i < split.size(); i++)
{
float r = static_cast<float>(std::rand())/RAND_MAX;
float g = static_cast<float>(std::rand())/RAND_MAX;
float b = static_cast<float>(std::rand())/RAND_MAX;
colors.push_back({r,g,b});
}
// Camera
auto camera = FreeFlyCamera{};
// la boucle principale
auto running = true;
auto captureRequired = false;
while (running)
{
// gestion des évènements
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
// on stoppe le programme
running = false;
}
else if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Escape)
{
running = false;
}
else if (event.key.code == sf::Keyboard::P)
{
captureRequired = true;
}
}
else if (event.type == sf::Event::MouseMoved)
{
// Hum...
}
else if (event.type == sf::Event::Resized)
{
// on ajuste le viewport lorsque la fenêtre est redimensionnée
glViewport(0, 0, event.size.width, event.size.height);
}
}
// Les trucs qui sont pas des évènements
camera.nextStep(WIDTH, HEIGHT);
sf::Mouse::setPosition(sf::Vector2i{WIDTH/2, HEIGHT/2});
// Dessin de la scene
drawScene(camera, split, colors);
glScalef(0.01, 0.01, 1);
if (captureRequired)
{
window.capture().saveToFile("capture.jpg");
captureRequired = false;
}
// termine la trame courante (en interne, échange les deux tampons de rendu)
window.display();
}
// libération des ressources...
return 0;
}
void drawScene(FreeFlyCamera const& camera, std::vector<Skeleton> const& split, std::vector<std::array<float,3>> const& colors)
{
// Initialisation
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Position Camera gluLookAt(positionCamera, positionCible, vecteurVertical)
camera.look();
for (auto i = 0u; i < split.size(); i++)
{
// Find random color
glColor3f(colors[i][0], colors[i][1], colors[i][2]);
split[i].draw();
}
// MAJ de l'écran
glFlush();
}