-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example of spectral clustering on C++, OpenCV
- Loading branch information
Saburo Okita
authored and
Saburo Okita
committed
Mar 31, 2014
1 parent
7679a5d
commit ac14546
Showing
33 changed files
with
2,221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// | ||
// ARViewer.cpp | ||
// MarkerDetection | ||
// | ||
// Created by Saburo Okita on 29/03/14. | ||
// Copyright (c) 2014 Saburo Okita. All rights reserved. | ||
// | ||
|
||
#include "ARViewer.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// ARViewer.h | ||
// MarkerDetection | ||
// | ||
// Created by Saburo Okita on 29/03/14. | ||
// Copyright (c) 2014 Saburo Okita. All rights reserved. | ||
// | ||
|
||
#ifndef __MarkerDetection__ARViewer__ | ||
#define __MarkerDetection__ARViewer__ | ||
|
||
#include <iostream> | ||
|
||
#endif /* defined(__MarkerDetection__ARViewer__) */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// | ||
// Arcball.cpp | ||
// Arcball | ||
// | ||
// Created by Saburo Okita on 12/03/14. | ||
// Copyright (c) 2014 Saburo Okita. All rights reserved. | ||
// | ||
|
||
#include "Arcball.h" | ||
|
||
namespace glv { | ||
|
||
/** | ||
* @param roll_speed the speed of rotation | ||
*/ | ||
void Arcball::init( int window_width, int window_height, GLfloat roll_speed, bool x_axis, bool y_axis ) { | ||
this->windowWidth = window_width; | ||
this->windowHeight = window_height; | ||
|
||
this->mouseEvent = 0; | ||
this->rollSpeed = roll_speed; | ||
this->angle = 0.0f; | ||
this->camAxis = glm::vec3(0.0f, 1.0f, 0.0f); | ||
|
||
this->xAxis = x_axis; | ||
this->yAxis = y_axis; | ||
} | ||
|
||
/** | ||
* Convert the mouse cursor coordinate on the window (i.e. from (0,0) to (windowWidth, windowHeight)) | ||
* into normalized screen coordinate (i.e. (-1, -1) to (1, 1) | ||
*/ | ||
glm::vec3 Arcball::toScreenCoord( double x, double y ) { | ||
glm::vec3 coord(0.0f); | ||
|
||
if( xAxis ) | ||
coord.x = (2 * x - windowWidth ) / windowWidth; | ||
|
||
if( yAxis ) | ||
coord.y = -(2 * y - windowHeight) / windowHeight; | ||
|
||
/* Clamp it to border of the windows, comment these codes to allow rotation when cursor is not over window */ | ||
coord.x = glm::clamp( coord.x, -1.0f, 1.0f ); | ||
coord.y = glm::clamp( coord.y, -1.0f, 1.0f ); | ||
|
||
float length_squared = coord.x * coord.x + coord.y * coord.y; | ||
if( length_squared <= 1.0 ) | ||
coord.z = sqrt( 1.0 - length_squared ); | ||
else | ||
coord = glm::normalize( coord ); | ||
|
||
return coord; | ||
} | ||
|
||
/** | ||
* Check whether we should start the mouse event | ||
* Event 0: when no tracking occured | ||
* Event 1: at the start of tracking, recording the first cursor pos | ||
* Event 2: tracking of subsequent cursor movement | ||
*/ | ||
void Arcball::mouseButtonCallback( GLFWwindow * window, int button, int action, int mods ){ | ||
mouseEvent = ( action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT ); | ||
} | ||
|
||
void Arcball::cursorCallback( GLFWwindow *window, double x, double y ){ | ||
if( mouseEvent == 0 ) { | ||
return; | ||
} | ||
else if( mouseEvent == 1 ) { | ||
/* Start of trackball, remember the first position */ | ||
prevPos = toScreenCoord( x, y ); | ||
mouseEvent = 2; | ||
return; | ||
} | ||
|
||
/* Tracking the subsequent */ | ||
currPos = toScreenCoord( x, y ); | ||
|
||
/* Calculate the angle in radians, and clamp it between 0 and 90 degrees */ | ||
angle = acos( std::min(1.0f, glm::dot(prevPos, currPos) )); | ||
|
||
/* Cross product to get the rotation axis, but it's still in camera coordinate */ | ||
camAxis = glm::cross( prevPos, currPos ); | ||
} | ||
|
||
/** | ||
* Create rotation matrix within the camera coordinate, | ||
* multiply this matrix with view matrix to rotate the camera | ||
*/ | ||
glm::mat4 Arcball::createViewRotationMatrix() { | ||
return glm::rotate( glm::degrees(angle) * rollSpeed, camAxis ); | ||
} | ||
|
||
/** | ||
* Create rotation matrix within the world coordinate, | ||
* multiply this matrix with model matrix to rotate the object | ||
*/ | ||
glm::mat4 Arcball::createModelRotationMatrix( glm::mat4& view_matrix ){ | ||
glm::vec3 axis = glm::inverse(glm::mat3(view_matrix)) * camAxis; | ||
return glm::rotate( glm::degrees(angle) * rollSpeed, axis ); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// Arcball.h | ||
// Arcball | ||
// | ||
// Created by Saburo Okita on 12/03/14. | ||
// Copyright (c) 2014 Saburo Okita. All rights reserved. | ||
// | ||
|
||
#ifndef __Arcball__Arcball__ | ||
#define __Arcball__Arcball__ | ||
|
||
#include <iostream> | ||
#include "GLVHeader.h" | ||
|
||
namespace glv { | ||
class Arcball { | ||
private: | ||
int windowWidth; | ||
int windowHeight; | ||
int mouseEvent; | ||
GLfloat rollSpeed; | ||
GLfloat angle ; | ||
glm::vec3 prevPos; | ||
glm::vec3 currPos; | ||
glm::vec3 camAxis; | ||
|
||
bool xAxis; | ||
bool yAxis; | ||
|
||
public: | ||
void init( int window_width, int window_height, GLfloat roll_speed = 1.0f, bool x_axis = true, bool y_axis = true ); | ||
glm::vec3 toScreenCoord( double x, double y ); | ||
|
||
void mouseButtonCallback( GLFWwindow * window, int button, int action, int mods ); | ||
void cursorCallback( GLFWwindow *window, double x, double y ); | ||
|
||
glm::mat4 createViewRotationMatrix(); | ||
glm::mat4 createModelRotationMatrix( glm::mat4& view_matrix ); | ||
|
||
}; | ||
}; | ||
|
||
#endif /* defined(__Arcball__Arcball__) */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// BasicShape.cpp | ||
// OpenGLViewer | ||
// | ||
// Created by Saburo Okita on 13/03/14. | ||
// Copyright (c) 2014 Saburo Okita. All rights reserved. | ||
// | ||
|
||
#include "BasicShape.h" | ||
|
||
|
||
namespace glv { | ||
BasicShape::BasicShape(){ | ||
} | ||
|
||
BasicShape::~BasicShape() { | ||
glDeleteBuffers( 6, bufferIds ); | ||
} | ||
|
||
bool BasicShape::init( float size ) { | ||
noOfElements = 0; | ||
return false; | ||
} | ||
|
||
void BasicShape::draw(){ | ||
/* Vertices */ | ||
glEnableVertexAttribArray( 0 ); | ||
glBindBuffer( GL_ARRAY_BUFFER, bufferIds[0] ); | ||
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0 ); | ||
|
||
/* Normals */ | ||
glEnableVertexAttribArray( 1 ); | ||
glBindBuffer( GL_ARRAY_BUFFER, bufferIds[1] ); | ||
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0 ); | ||
|
||
/* UVs */ | ||
glEnableVertexAttribArray( 2 ); | ||
glBindBuffer( GL_ARRAY_BUFFER, bufferIds[3] ); | ||
glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE, 0, (void*) 0 ); | ||
|
||
/* Indices */ | ||
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, bufferIds[2] ); | ||
glDrawElements( GL_TRIANGLES, noOfElements, GL_UNSIGNED_SHORT, (void*) 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// BasicShape.h | ||
// OpenGLViewer | ||
// | ||
// Created by Saburo Okita on 13/03/14. | ||
// Copyright (c) 2014 Saburo Okita. All rights reserved. | ||
// | ||
|
||
#ifndef __OpenGLViewer__BasicShape__ | ||
#define __OpenGLViewer__BasicShape__ | ||
|
||
#include <iostream> | ||
#include "GLVHeader.h" | ||
|
||
using namespace std; | ||
|
||
namespace glv { | ||
class BasicShape { | ||
protected: | ||
GLuint bufferIds[6]; | ||
int noOfElements; | ||
|
||
public: | ||
BasicShape(); | ||
~BasicShape(); | ||
virtual bool init( float size ); | ||
virtual void draw(); | ||
}; | ||
}; | ||
#endif /* defined(__OpenGLViewer__BasicShape__) */ |
Oops, something went wrong.