text:
/*
 *  mkSVG.cpp
 *  MonkSVG
 *
 *  Created by Micah Pearlman on 8/2/10.
 *  Copyright 2010 Zero Vision. All rights reserved.
 *
 */

#include "mkSVG.h"
#include "tinyxml.h"
#include <map>
#include <iterator>
#include <boost/foreach.hpp>
#include <boost/tokenizer.hpp>
#include <boost/regex.hpp>
using namespace boost;

namespace MonkSVG {

	bool SVG::initialize( ISVGHandler::SmartPtr handler ) {
		_handler = handler;

		return true;
	}

	bool SVG::read( const char* data ) {

		TiXmlDocument doc;
		doc.Parse( data );

        if (doc.Error()) {
            return false;
        }

		TiXmlElement* root = doc.FirstChild( "svg" )->ToElement();
		recursive_parse( root );

        // get bounds information from the svg file, ignoring non-pixel values

        string numberWithUnitString;
        regex numberWithUnitPattern( "^(-?\\d+)(px)?$" );

        _handler->_minX = 0.0f;
        if ( root->QueryStringAttribute( "x", &numberWithUnitString ) == TIXML_SUCCESS ) {
            match_results<string::const_iterator> matches;
            if ( regex_search( numberWithUnitString, matches, numberWithUnitPattern ) ) {
                _handler->_minX = ::atof( matches[1].str().c_str() );
            }
        }

        _handler->_minY = 0.0f;
        if ( root->QueryStringAttribute( "y", &numberWithUnitString ) == TIXML_SUCCESS ) {
            match_results<string::const_iterator> matches;
            if ( regex_search( numberWithUnitString, matches, numberWithUnitPattern ) ) {
                _handler->_minY = ::atof( matches[1].str().c_str() );
            }
        }

        _handler->_width = 0.0f;
        if ( root->QueryStringAttribute( "width", &numberWithUnitString ) == TIXML_SUCCESS ) {
            match_results<string::const_iterator> matches;
            if ( regex_search( numberWithUnitString, matches, numberWithUnitPattern ) ) {
                _handler->_width = ::atof( matches[1].str().c_str() );
            }
        }

        _handler->_height = 0.0f;
        if ( root->QueryStringAttribute( "height", &numberWithUnitString ) == TIXML_SUCCESS ) {
            match_results<string::const_iterator> matches;
            if ( regex_search( numberWithUnitString, matches, numberWithUnitPattern ) ) {
                _handler->_height = ::atof( matches[1].str().c_str() );
            }
        }

		return true;

	}

	bool SVG::read( string& data ) {
		return read( data.c_str() );
	}

	void SVG::recursive_parse( TiXmlElement* element ) {

		if ( element ) {
			for ( TiXmlElement* sibbling = element; sibbling != 0; sibbling = sibbling->NextSiblingElement() ) {
				handle_xml_element( sibbling );
			}
		}

		if ( element ) {
			TiXmlElement* child = element->FirstChildElement();
			// if we don't handle the element recursively go into it
			if( handle_xml_element( child ) == false ) {
				recursive_parse( child );
			}
		}

	}

	bool SVG::handle_xml_element( TiXmlElement* element ) {
		if( !element )
			return false;

		string type = element->Value();

		if ( type == "g" ) {
			handle_group( element );
			return true;
		} else if ( type == "path" ) {
			handle_path( element );
			return true;
		} else if ( type == "rect" ) {
			handle_rect( element );
			return true;
		} else if ( type == "polygon" ) {
			handle_polygon ( element );
			return true;
		} else if( type == "symbol" ) {
			string id;
			if ( element->QueryStringAttribute( "id", &id ) == TIXML_SUCCESS ) {
				_symbols[id] = (TiXmlElement*)element->Clone();
			}
			return true;
		} else if ( type == "use" ) {
			string href;
			if ( element->QueryStringAttribute( "xlink:href", &href ) == TIXML_SUCCESS ) {
				string id = href.substr( 1 );	// skip the #
				_handler->onUseBegin();
				// handle transform and other parameters
				handle_general_parameter( element );
				recursive_parse( _symbols[id] );
				_handler->onUseEnd();
			}

			return true;
		}
		return false;

	}

	void SVG::handle_group( TiXmlElement* pathElement ) {
		string id_;
//		if ( pathElement->QueryStringAttribute( "id", &id_) == TIXML_SUCCESS ) {
//			//_handler->onId( id_ );
//			cout << "group begin: " << id_ << endl;
//		}

		_handler->onGroupBegin();

		// handle transform and other parameters
		handle_general_parameter( pathElement );

		// go through all the children
		TiXmlElement* children = pathElement->FirstChildElement();
		for ( TiXmlElement* child = children; child != 0; child = child->NextSiblingElement() ) {
			string type = child->Value();
			handle_xml_element( child );
		}

		_handler->onGroupEnd();

//		if ( pathElement->QueryStringAttribute( "id", &id_) == TIXML_SUCCESS ) {
//			//_handler->onId( id_ );
//			cout << "group end: " << id_ << endl;
//		}

	}

	void SVG::handle_path( TiXmlElement* pathElement ) {

//		string id_;
//		if ( pathElement->QueryStringAttribute( "id", &id_) == TIXML_SUCCESS ) {
//			//_handler->onId( id_ );
//			cout << "path: " << id_ << endl;
//		}

		_handler->onPathBegin();
		string d;
		if ( pathElement->QueryStringAttribute( "d", &d ) == TIXML_SUCCESS ) {
			parse_path_d( d );
		}

		handle_general_parameter( pathElement );

		_handler->onPathEnd();
	}

	void SVG::handle_rect( TiXmlElement* pathElement ) {
		_handler->onPathBegin();

		float pos[2] = { 0, 0 };
		if ( pathElement->QueryFloatAttribute( "x", &pos[0] ) == TIXML_SUCCESS ) {
			//parse_path_d( d );
		}
		if ( pathElement->QueryFloatAttribute( "y", &pos[1] ) == TIXML_SUCCESS ) {
			//parse_path_d( d );
		}
		float sz[2] = { 0, 0 };
		if ( pathElement->QueryFloatAttribute( "width", &sz[0] ) == TIXML_SUCCESS ) {
			//parse_path_d( d );
		}
		if ( pathElement->QueryFloatAttribute( "height", &sz[1] ) == TIXML_SUCCESS ) {
			//parse_path_d( d );
		}
		_handler->onPathRect( pos[0], pos[1], sz[0], sz[1] );

		handle_general_parameter( pathElement );

		_handler->onPathEnd();

	}

	void SVG::handle_polygon( TiXmlElement* pathElement ) {
		_handler->onPathBegin();
		string points;
		if ( pathElement->QueryStringAttribute( "points", &points ) == TIXML_SUCCESS ) {
			parse_points( points );
		}

		handle_general_parameter( pathElement );

		_handler->onPathEnd();
	}

	void SVG::handle_general_parameter( TiXmlElement* pathElement ) {
		string fill;
		if ( pathElement->QueryStringAttribute( "fill", &fill ) == TIXML_SUCCESS ) {
			_handler->onPathFillColor( string_hex_color_to_uint( fill ) );
		}

		string stroke;
		if ( pathElement->QueryStringAttribute( "stroke", &stroke) == TIXML_SUCCESS ) {
			_handler->onPathStrokeColor( string_hex_color_to_uint( stroke ) );
		}

		string stroke_width;
		if ( pathElement->QueryStringAttribute( "stroke-width", &stroke_width) == TIXML_SUCCESS ) {
			float width = atof( stroke_width.c_str() );
			_handler->onPathStrokeWidth( width );
		}

		string style;
		if ( pathElement->QueryStringAttribute( "style", &style) == TIXML_SUCCESS ) {
			parse_path_style( style );
		}

		string transform;
		if ( pathElement->QueryStringAttribute( "transform", &transform) == TIXML_SUCCESS ) {
			parse_path_transform( transform );
		}
		string id_;
		if ( pathElement->QueryStringAttribute( "id", &id_) == TIXML_SUCCESS ) {
			_handler->onId( id_ );
			//cout << id_ << endl;
		}

		string opacity;
		if ( pathElement->QueryStringAttribute( "opacity", &opacity) == TIXML_SUCCESS ) {
			float o = atof( opacity.c_str() );
			_handler->onPathFillOpacity( o );
			// TODO: ??? stroke opacity???
		}
		if ( pathElement->QueryStringAttribute( "fill-opacity", &opacity) == TIXML_SUCCESS ) {
			float o = atof( opacity.c_str() );
			_handler->onPathFillOpacity( o );
		}

		string fillrule;
		if ( pathElement->QueryStringAttribute( "fill-rule", &fillrule) == TIXML_SUCCESS ) {
			_handler->onPathFillRule( fillrule );
		}

	}

	float SVG::d_string_to_float( char *c, char** str ) {
		while ( isspace(*c) ) {
			c++;
			(*str)++;
		}
		while ( *c == ',' ) {
			c++;
			(*str)++;
		}

		return strtof( c, str );
	}

	int SVG::d_string_to_int( char *c, char **str ) {
		while ( isspace(*c) ) {
			c++;
			(*str)++;
		}
		while ( *c == ',' ) {
			c++;
			(*str)++;
		}

		return (int)strtol( c, str, 10);

	}

	uint32_t SVG::string_hex_color_to_uint( string& hexstring ) {
		uint32_t color = (uint32_t)strtol( hexstring.c_str() + 1, 0, 16 );
		if ( hexstring.length() == 7 ) {	// fix up to rgba if the color is only rgb
			color = color << 8;
			color |= 0x000000ff;
		}

		return color;
	}

	void SVG::nextState( char** c, char* state ) {
		if ( **c == '\0') {
			*state = 'e';
			return;
		}

		while ( isspace(**c) ) {
			if ( **c == '\0') {
				*state = 'e';
				return;
			}
			(*c)++;
		}
		if ( isalpha( **c ) ) {
			*state = **c;
			(*c)++;
//			if ( **c == '\0') {
//				*state = 'e';
//				return;
//			}

			if ( islower(*state) ) {	// if lower case then relative coords (see SVG spec)
				_handler->setRelative( true );
			} else {
				_handler->setRelative( false );
			}
		}

		//cout << "state: " << *state << endl;
	}

	void SVG::parse_path_transform( string& tr )	{
		size_t p = tr.find( "translate" );
		if ( p != string::npos ) {
			size_t left = tr.find( "(" );
			size_t right = tr.find( ")" );
			string values = tr.substr( left+1, right-1 );
			char* c = const_cast<char*>( values.c_str() );
			float x = d_string_to_float( c, &c );
			float y = d_string_to_float( c, &c );
			_handler->onTransformTranslate( x, y );
		} else if ( tr.find( "rotate" ) != string::npos ) {
			size_t left = tr.find( "(" );
			size_t right = tr.find( ")" );
			string values = tr.substr( left+1, right-1 );
			char* c = const_cast<char*>( values.c_str() );
			float a = d_string_to_float( c, &c );
			_handler->onTransformRotate( a );	// ??? radians or degrees ??
		} else if ( tr.find( "matrix" ) != string::npos ) {
			size_t left = tr.find( "(" );
			size_t right = tr.find( ")" );
			string values = tr.substr( left+1, right-1 );
			char* cc = const_cast<char*>( values.c_str() );
			float a = d_string_to_float( cc, &cc );
			float b = d_string_to_float( cc, &cc );
			float c = d_string_to_float( cc, &cc );
			float d = d_string_to_float( cc, &cc );
			float e = d_string_to_float( cc, &cc );
			float f = d_string_to_float( cc, &cc );
			_handler->onTransformMatrix( a, b, c, d, e, f );
		}
	}

	void SVG::parse_path_d( string& d ) {
		char* c = const_cast<char*>( d.c_str() );
		char state = *c;
		nextState( &c, &state );
		while ( /**c &&*/ state != 'e' ) {

			switch ( state ) {
				case 'm':
				case 'M':
				{
					//c++;
					float x = d_string_to_float( c, &c );
					float y = d_string_to_float( c, &c );
					_handler->onPathMoveTo( x, y );
					nextState(&c, &state);
				}
				break;

				case 'l':
				case 'L':
				{
					//c++;
					float x = d_string_to_float( c, &c );
					float y = d_string_to_float( c, &c );
					_handler->onPathLineTo( x, y );
					nextState(&c, &state);

				}
				break;

				case 'h':
				case 'H':
				{
					float x = d_string_to_float( c, &c );
					_handler->onPathHorizontalLine( x );
					nextState(&c, &state);

				}
				break;

				case 'v':
				case 'V':
				{
					float y = d_string_to_float( c, &c );
					_handler->onPathVerticalLine( y );
					nextState(&c, &state);

				}
				break;

				case 'c':
				case 'C':
				{
					float x1 = d_string_to_float( c, &c );
					float y1 = d_string_to_float( c, &c );
					float x2 = d_string_to_float( c, &c );
					float y2 = d_string_to_float( c, &c );
					float x3 = d_string_to_float( c, &c );
					float y3 = d_string_to_float( c, &c );
					_handler->onPathCubic(x1, y1, x2, y2, x3, y3);
					nextState(&c, &state);

				}
				break;

				case 's':
				case 'S':
				{
					float x2 = d_string_to_float( c, &c );
					float y2 = d_string_to_float( c, &c );
					float x3 = d_string_to_float( c, &c );
					float y3 = d_string_to_float( c, &c );
					_handler->onPathSCubic(x2, y2, x3, y3);
					nextState(&c, &state);

				}
				break;

				case 'a':
				case 'A':
				{
					float rx = d_string_to_float( c, &c );
					float ry = d_string_to_float( c, &c );
					float x_axis_rotation = d_string_to_float( c, &c );
					int large_arc_flag = d_string_to_int( c, &c );
					int sweep_flag = d_string_to_int( c, &c );
					float x = d_string_to_float( c, &c );;
					float y = d_string_to_float( c, &c );
					_handler->onPathArc( rx, ry, x_axis_rotation, large_arc_flag, sweep_flag, x, y );
					nextState(&c, &state);

				}
				break;

				case 'z':
				case 'Z':
				{
					//c++;
					_handler->onPathClose();
					nextState(&c, &state);

				}
				break;

                case 'q':
                case 'Q':
                {
                    float x1 = d_string_to_float(c, &c);
                    float y1 = d_string_to_float(c, &c);
                    float x2 = d_string_to_float(c, &c);
                    float y2 = d_string_to_float(c, &c);
                    _handler->onPathQuad(x1, y1, x2, y2);
                    nextState(&c, &state);
                }
                break;

				default:
					// BUGBUG: can get stuck here!
					// TODO: figure out the next state if we don't handle a particular state or just dummy handle a state!
					c++;
					break;
			}
		}
	}

	// semicolon-separated property declarations of the form "name : value" within the ‘style’ attribute
	void SVG::parse_path_style( string& ps ) {
		map< string, string > style_key_values;
		char_separator<char> values_seperator(";");
		char_separator<char> key_value_seperator(":");
		tokenizer<char_separator<char> > values_tokens( ps, values_seperator );
		BOOST_FOREACH( string values, values_tokens ) {
			tokenizer<char_separator<char> > key_value_tokens( values, key_value_seperator );
			tokenizer<char_separator<char> >::iterator k = key_value_tokens.begin();
			tokenizer<char_separator<char> >::iterator v = k;
			v++;
			//cout << *k << ":" << *v << endl;
			style_key_values[*k] = *v;
		}

		map<string, string>::iterator kv = style_key_values.find( string("fill") );
		if ( kv != style_key_values.end() ) {
			if( kv->second != "none" )
				_handler->onPathFillColor( string_hex_color_to_uint( kv->second ) );
		}

		kv = style_key_values.find( "stroke" );
		if ( kv != style_key_values.end() ) {
			if( kv->second != "none" )
				_handler->onPathStrokeColor( string_hex_color_to_uint( kv->second ) );
		}

		kv = style_key_values.find( "stroke-width" );
		if ( kv != style_key_values.end() ) {
			float width = atof( kv->second.c_str() );
			_handler->onPathStrokeWidth( width );
		}

		kv = style_key_values.find( "fill-rule" );
		if ( kv != style_key_values.end() ) {
			_handler->onPathFillRule( kv->second );
		}

		kv = style_key_values.find( "fill-opacity" );
		if ( kv != style_key_values.end() ) {
			float o = atof( kv->second.c_str() );
			_handler->onPathFillOpacity( o );
		}

		kv = style_key_values.find( "opacity" );
		if ( kv != style_key_values.end() ) {
			float o = atof( kv->second.c_str() );
			_handler->onPathFillOpacity( o );
			// ?? TODO: stroke Opacity???
		}

		kv = style_key_values.find( "stroke-opacity" );
		if ( kv != style_key_values.end() ) {
			float o = atof( kv->second.c_str() );
			_handler->onPathStrokeOpacity( o );
		}

	}

	void SVG::parse_points( string& points ) {
		char_separator<char> sep(", \t");
		tokenizer<char_separator<char>> tokens(points,sep);
		float xy[2];
		int xy_offset = 0;  // 0:x, 1:y
		bool first = true;
		_handler->setRelative(false);
		BOOST_FOREACH( string p, tokens ) {
			xy[xy_offset++] = (float)atof(p.c_str());

			if (xy_offset == 2) {
				xy_offset = 0;
				if (first) {
					_handler->onPathMoveTo( xy[0], xy[1] );
					first = false;
				} else
					_handler->onPathLineTo( xy[0], xy[1] );
			}
		}
		_handler->onPathClose();
	}
};

max_stars_repo_name: micahpearlman/MonkSVG

max_issues_repo_name: micahpearlman/MonkSVG

max_forks_repo_name: micahpearlman/MonkSVG


text:
#include <unet/wire/ethernet.hpp>

#include <boost/format.hpp>

namespace unet {

std::ostream& operator<<(std::ostream& os, EthernetAddr ethAddr) {
  boost::format fmt{"%02X:%02X:%02X:%02X:%02X:%02X"};
  for (auto p = ethAddr.addr; p < ethAddr.addr + 6; p++) {
    fmt % static_cast<std::uint32_t>(*p);
  }
  return os << fmt;
}

}  // namespace unet

ext: cpp

max_stars_repo_name: andreimaximov/unet

max_issues_repo_name: andreimaximov/unet

max_forks_repo_name: andreimaximov/unet


text:
#include <map>
#include <vector>
#include <iostream>
#include <iomanip>
#include <boost/multiprecision/cpp_bin_float.hpp>
#include <boost/random/uniform_real_distribution.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_real_distribution.hpp>

#include "GaussSeq.h"
#include "ARSeq.h"
#include "utils.h"

using utils::my_float;

int main() {
    using boost::multiprecision::cpp_bin_float_50;
    using boost::random::uniform_real_distribution;

    boost::random::mt19937 gen {};
    uniform_real_distribution<my_float> u (0, 0.25);

    // set precision of output
//    std::streamsize precision = std::numeric_limits<cpp_bin_float_50>::digits10;
//    std::cout << std::setprecision(10);

    // testing
    std::map<int, my_float> coeff {
        {1, -0.9},
    };
    ARSeq seq(coeff, 100);
    std::vector<my_float> v;
    for (auto i = 0; i < 1; i++) {
        v.push_back(u(gen));
    }

    seq.seed_prev_vals(v);

    seq.print_past_vals();
    for (auto i = 0; i < 100; i++) {
        seq.next();
    }
    std::cout << "After 100 iterations\n";
    seq.print_past_vals();

    for (auto i = 0; i < 900; i++) {
        seq.next();
    }
    std::cout << "After 1000 iterations\n";
    seq.print_past_vals();

    for (auto i = 0; i < 9000; i++) {
        seq.next();
    }
    std::cout << "After 10000 iterations\n";
    seq.print_past_vals();

    return 0;
}


text:
/// \file
/// Maintainer: Felice Serena
///
///

#include "resolve_symlink.h"

#include <boost/filesystem.hpp>
#include <boost/log/trivial.hpp>

namespace MouseTrack {

namespace fs = boost::filesystem;

std::string resolve_symlink(const std::string &p, int maxSymlinksFollowed) {
  fs::path path(p);
  int evaluatedSymlinks = 0;
  while (evaluatedSymlinks < maxSymlinksFollowed && fs::is_symlink(path)) {
    path = fs::read_symlink(path);
    BOOST_LOG_TRIVIAL(info) << "Followed symbolic link to: " << path.string();
    evaluatedSymlinks += 1;
  }
  return path.string();
}

} // namespace MouseTrack

max_stars_repo_name: itko/scanbox

max_issues_repo_name: itko/scanbox

max_forks_repo_name: itko/scanbox


text:
#include "camera_control.h"
#include "vertices_camera_path.h"

#include <iostream>

#include <GL/glew.h>
#include <GL/glfw.h>
#include "opengp.h"
#include <Eigen/Geometry>

//key status
static bool KeyW = false;
static bool KeyA = false;
static bool KeyS = false;
static bool KeyD = false;
static bool KeyE = false;
static bool KeyQ = false;
static bool KeyX = false;
static bool KeySHIFT = false;
static bool KeySPACE = false;
static bool Key1 = false;
static bool Key2 = false;
static bool KeyENTER = false;
static bool flagAnimatePictorialCamera = true;

float deltaT;
const float OneRad = 0.0174f;

void CameraControl::init(VerticesCameraPath* verticesCameraPath, VerticesCameraPath* verticesCameraPathControls, GLuint heightMapTexID) {

    /// The camera control can change the camera path. It should thus have
    /// access to the path vertices object.
    _verticesCameraPath = verticesCameraPath;
    _verticesCameraPathControls = verticesCameraPathControls;

    /// Copy the heightmap texture to CPU. Used by FPS exploration mode to
    /// follow the terrain.
    _heightmapCPU = new float[1024*1024];
    glBindTexture(GL_TEXTURE_2D, heightMapTexID);
    glGetTexImage(GL_TEXTURE_2D, 0, GL_RED, GL_FLOAT, _heightmapCPU);

    /// Generate the camera path.
    //bezier_4_points(0, 0, 0, 0);
	MultipleBezier();

	//MultipleBezier_controlled(0,0, 0, 0);
   // InitdeCasteljauSubdivision();
    //InitSubdivision();

    /// Initialize the camera position.
    _explorationMode = FIX;
    trackball(mat4::Identity());

    update_camera_modelview(0.5f, -0.5f, 0.8f, 0.0f, 0.0f, 0.0f);

    /// HACK to show pictorial camera.
    /// Simple translation along z-axis for now.
    _cameraPictorialModel = mat4::Identity();
    _cameraPictorialModel(2,3) = 0.5f;

    /// Control point curently modified.
    _selectedControlPoint = 0;

	animatePictorialCamera(1);
}

/// This controls the controller view camera.
void CameraControl::trackball(const mat4& model) {

    /// View matrix (camera extrinsics) (position in world space).

    /// Camera is in the sky, looking down.

//    vec3 camPos(0.5f, -0.5f, 0.8f);
	//vec3 camPos(2.5f, -2.5f, 2.8f);

//    vec3 camPos(0.0f, -1.5f, 0.8f);

//    vec3 camLookAt(0.0f, 0.0f, 0.0f);
//    vec3 camUp(0.0f, 0.0f, 1.0f);

    /// Camera is right on top (comparison with light position).
//    vec3 camPos = vec3(0.0, 0.0, 5.0);
//    vec3 camLookAt = vec3(0.0, 0.0, 0.0);
//    vec3 camUp = vec3(1.0, 0.0, 0.0);

    /// Global view from outside.
//    vec3 camPos(0.0f, -3.0f, 4.0f);
//    vec3 camLookAt(0.0f, 0.0f, 0.0f);
//    vec3 camUp(0.0f, 0.0f, 1.0f);

    /// Frontal view to observe falling particles.

  /*  vec3 camPos(0.0f, -3.0f, 1.3f);
    vec3 camLookAt(0.0f, 0.0f, 0.2f);
    vec3 camUp(0.0f, 0.0f, 1.0f);
*/
    /// Frontal view to observe water reflection.

    vec3 camPos(0.0f, -2.8f, 1.7f);
    vec3 camLookAt(0.0f, 0.0f, 0.0f);
    vec3 camUp(0.0f, 0.0f, 1.0f);

    /// Frontal view to observe water reflection.
    //vec3 camPos(-3.0f, 0.0f, 1.0f);
//    vec3 camLookAt(0.0f, 0.0f, 0.0f);
//    vec3 camUp(0.0f, 0.0f, 1.0f);

    //vec3 camPos(-3.0f, 0.0f, 1.0f);
//    vec3 camLookAt(0.0f, 0.0f, 0.0f);
//    vec3 camUp(0.0f, 0.0f, 1.0f);

    /// FPS exploration.
//	  vec3 camPos(0.78f, 0.42f, 0.30f);
//    vec3 camLookAt(-0.24f, 0.19f, 0.13f);
//    vec3 camUp(0.0f, 0.0f, 1.0f);

    /// Internal view from center.
//    vec3 camPos(0.9f, -0.8f, 1.0f);
//    vec3 camLookAt(-0.3f, 0.1f, 0.5f);
//    vec3 camUp(0.0f, 0.0f, 1.0f);

    /// Close texture view, for screenshots.
//    vec3 camPos(0.2f, -0.1f, 0.5f);
//    vec3 camLookAt(-0.3f, 0.1f, 0.2f);
//    vec3 camUp(0.0f, 0.0f, 1.0f);

    /// Assemble the view matrix.
    mat4 view = Eigen::lookAt(camPos, camLookAt, camUp);

    /// Assemble the "Model View" matrix.
    _controllerModelview = view * model;

}

/// This controls the camera view camera.
void CameraControl::update_camera_modelview(float posX,float posY,float posZ,float lookX,float lookY,float lookZ){
	vec3 camPos(posX,posY,posZ);
	vec3 camLookAt(lookX, lookY, lookZ);
	//std::cout<<posX<<" "<<posY<<" "<<posZ<<" "<<lookX<<" "<<lookY<<" "<<lookZ<<" "<<std::endl;
	//std::cout<<posX-lookX<<" "<<posY-lookY<<" "<<posZ-lookZ<<std::endl;
	//vec3 camUp(posX-lookX,posY-lookY,posZ-lookZ);
	vec3 camUp(0.0f,0.0f,1.0f);

    _cameraModelview = Eigen::lookAt(camPos, camLookAt, camUp);

	//std::cout<<(powf(posX-lookX,2)+powf(posY-lookY,2)+powf(posZ-lookZ,2))<<endl;

	/*
	std::cout<<endl<<endl<<"update"<<endl<<endl;
	std::cout<<posX-lookX<<" "<<posY-lookY<<" "<<posZ-lookZ<<endl;
	std::cout<<powf(posX-lookX,2)<<" "<<powf(posY-lookY,2)<<" "<<powf(posZ-lookZ,2)<<endl;
	std::cout<<powf(posX-lookX,2)/(powf(posX-lookX,2)+powf(posY-lookY,2))<<" "<<powf(posY-lookY,2)/(powf(posX-lookX,2)+powf(posY-lookY,2))<<endl;
*/
}

void CameraControl::rotate2D(float pos1, float pos2, float& look1, float& look2, float angle){
	float tmp1 = (look1-pos1)*cos(angle) - (look2-pos2)*sin(angle) + pos1;
	float tmp2 = (look1-pos1)*sin(angle) + (look2-pos2)*cos(angle) + pos2;
	look1=tmp1;
	look2=tmp2;
}

void CameraControl::fpsRotateLeftRight(float& posX, float& posY, float& posZ, float& lookX, float& lookY, float& lookZ,float velocity){

		float tmpX = lookX-posX;
		float tmpY = lookY-posY;

		//3 rotate on Z axis
		float Angle = OneRad*velocity; // more or less 1 degree
		rotate2D(0,0,tmpX,tmpY,Angle);

		lookX = tmpX+posX;
		lookY = tmpY+posY;

		update_camera_modelview(posX,posY,posZ,lookX,lookY,lookZ);
}

void CameraControl::fpsRotateUpDown(float& posX, float& posY, float& posZ, float& lookX, float& lookY, float& lookZ,float recordRotZ, float velocity){

		float tmpX =lookX-posX;
		float tmpY =lookY-posY;
		float tmpZ =lookZ-posZ;

		//undo lateral (Z) rotation
		rotate2D(0,0,tmpX,tmpY,-recordRotZ);

		//proceed vertical (Y) rotation
		float Angle = OneRad*velocity; // more or less 1 degree

		rotate2D(0,0,tmpX,tmpZ, Angle);

		//redo lateral (Z) rotation
		rotate2D(0,0,tmpX,tmpY,recordRotZ);

		lookX = tmpX + posX;
		lookY = tmpY + posY;
		lookZ = tmpZ + posZ;

		update_camera_modelview(posX,posY,posZ,lookX,lookY,lookZ);
}

void CameraControl::rotateLeftRight(float& posX, float& posY, float& posZ, float& lookX, float& lookY, float& lookZ,float& recordRotY,float velocity){

		//1 move to the origine
		float tmpX = lookX - posX;
		float tmpY = lookY - posY;
		float tmpZ = lookZ - posZ;

		//2 cancel Y rotation
		rotate2D(0,0,tmpX,tmpZ, -recordRotY);

		//3 rotate on Z axis
		float Angle = OneRad*velocity; // more or less 1 degree
		rotate2D(0,0,tmpX,tmpY,Angle);

		//4 redo Y rotation
		rotate2D(0,0,tmpX,tmpZ,recordRotY);

		lookX = tmpX + posX;
		lookY = tmpY + posY;
		lookZ = tmpZ + posZ;

		update_camera_modelview(posX,posY,posZ,lookX,lookY,lookZ);
}

void CameraControl::rotateUpDown(float& posX, float& posY, float& posZ, float& lookX, float& lookY, float& lookZ,float& recordRotZ,float velocity){

		//1 move to the origine
		float tmpX = lookX - posX;
		float tmpY = lookY - posY;
		float tmpZ = lookZ - posZ;

		//2 undo lateral (Z) rotation
		rotate2D(0,0,tmpX,tmpY,-recordRotZ);

		//3 rotate on Y axis
		float Angle = OneRad*velocity; // more or less 1 degree
		rotate2D(0,0,tmpX,tmpZ, Angle);

		//4 redo Z rotation
		rotate2D(0,0,tmpX,tmpY, recordRotZ);

		lookX = tmpX + posX ;
		lookY = tmpY + posY ;
		lookZ = tmpZ + posZ ;

		update_camera_modelview(posX,posY,posZ,lookX,lookY,lookZ);
}

void CameraControl::moveAlongAxis(float& posX, float& posY, float& posZ, float& lookX, float& lookY, float& lookZ, float velocity){

		float dispX = (posX - lookX)*velocity;
		float dispY = (posY - lookY)*velocity;
		float dispZ = (posZ - lookZ)*velocity;

		posX = posX - dispX;
		posY = posY - dispY;
		posZ = posZ - dispZ;

		lookX = lookX - dispX;
		lookY = lookY - dispY;
		lookZ = lookZ - dispZ;

		update_camera_modelview(posX,posY,posZ,lookX,lookY,lookZ);
}

void CameraControl::fpsExplorationForwardBackward(float& posX, float& posY, float& posZ, float& lookX, float& lookY, float& lookZ,float dispX,float dispY){
	posX = posX - dispX;
	posY = posY - dispY;
	lookX = lookX - dispX;
	lookY = lookY - dispY;
	if(posX>1){posX=1;}
	else if(posX<-1){posX=-1;}
	if(posY>1){posY=1;}
	else if(posY<-1){posY=-1;}

	int tmpX = int((1+posX)*1024/2);
	int tmpY = int((1+posY)*1024/2);
	tmpX = tmpX -2;
	tmpY = tmpY -2;
	float tmpZ=0;
	float AmountOfPoints = 0;
	for(int i=0;i<5;i++){//take in count 24 z values arround to have a smooth move
		for(int j=0;j<5;j++){
			tmpX = tmpX + i;
			tmpY = tmpY + j;
			if ((tmpX>0) & (tmpX<1025) & (tmpY>0) & (tmpY<1025)){
                tmpZ = tmpZ + _heightmapCPU[tmpX+1024*tmpY];
				AmountOfPoints++;
			}
		}
	}
	//std::cout<<"AmountOfPoints ="<<AmountOfPoints<<endl;
	tmpZ=tmpZ/AmountOfPoints;
	posZ = float(tmpZ+0.06); //fps person height
	//lookZ = posZ+0.06;
	update_camera_modelview(posX,posY,posZ,lookX,lookY,lookZ);
}

void CameraControl::deCasteljauTest3Points(){

	static float t=0;

	if(t<=1){
		//set control points
		float b0X = 1.0f;
		float b0Y = 1.0f;
		float b0Z = 0.4f;

		float b1X = 1.0f;
		float b1Y = -1.0f;
		float b1Z = 0.4f;

		float b2X = -1.0f;
		float b2Y = -1.0f;
		float b2Z = 0.4f;

		float posX = powf((1-t),2) * b0X + 2*t*(1-t) * b1X + powf(t,2) *b2X;
		float posY = powf((1-t),2) * b0Y + 2*t*(1-t) * b1Y + powf(t,2) *b2Y;
		float posZ = powf((1-t),2) * b0Z + 2*t*(1-t) * b1Z + powf(t,2) *b2Z;

		float lookX = 0.0f;
		float lookY = 0.0f;
		float lookZ = 0.3f;
		t = float(t+0.001);
		update_camera_modelview(posX,posY,posZ,lookX,lookY,lookZ);
	}
	else{
		t=0;
	}

}

/*void CameraControl::deCasteljauTest4Points(){ // wrong naming => use to follow the curve with the camera...
	static unsigned int i = 0;

	if(deltaT>0.01){
        if(i<(_cameraPath.size()/3)-1){
			//std::cout<<i<<std::endl;
            float posX	= _cameraPath[i*3+0];
            float posY	= _cameraPath[i*3+1];
            float posZ	= _cameraPath[i*3+2];

            float lookX = _cameraPath[(i+1)*3+0];
            float lookY = _cameraPath[(i+1)*3+1];
            float lookZ = _cameraPath[(i+1)*3+2];
			i++;
			update_camera_modelview(posX,posY,posZ,lookX,lookY,lookZ);
		}
		else{
			i=0;
		}
	}
}*/

void CameraControl::animatePictorialCamera(unsigned int back){ //advance point per point
	std::cout<<"init"<<std::endl;
	static unsigned int i = 0;
	i-=back;
    if(i<(_cameraPath.size()/3)-1){
		float posX	= _cameraPath[i*3+0];
        float posY	= _cameraPath[i*3+1];
        float posZ	= _cameraPath[i*3+2];

        float lookX = _cameraPath[(i+1)*3+0];
        float lookY = _cameraPath[(i+1)*3+1];
        float lookZ = _cameraPath[(i+1)*3+2];

		//update camera position and look at
		update_camera_modelview(posX,posY,posZ,lookX,lookY,lookZ);

		//update pictorial camera poisition and look at
		_cameraPictorialModel(0,3) = posX;
		_cameraPictorialModel(1,3) = posY;
		_cameraPictorialModel(2,3) = posZ;

		float dirX= lookX-posX;
		float dirY= lookY-posY;
		float dirZ= lookZ-posZ;

		float length = sqrt(dirX*dirX+dirY*dirY+dirZ*dirZ);

		float dirXnorm = dirX/length;
		float dirYnorm = dirY/length;
		float dirZnorm = dirZ/length;

		float angleZ = atan2(dirYnorm,dirXnorm);
		float angleY = atan2(sqrt(dirYnorm*dirYnorm+dirXnorm*dirXnorm),dirZnorm);

		mat3 rot = Eigen::AngleAxisf(angleZ, Eigen::Vector3f::UnitZ()).toRotationMatrix()
			* Eigen::AngleAxisf(angleY, Eigen::Vector3f::UnitY()).toRotationMatrix();

		for (int j=0; j<3;j++){
			for(int k=0; k<3;k++){
				_cameraPictorialModel(k,j)=rot(k,j);
			}
		}
		i++;

	}
	else{
		i=0;
	}
}

void CameraControl::animatePictorialCamera2(unsigned int back){ //advance distance/temps

	const float speed = 0.005f;

	float distanceToGo = speed*deltaT;

	static int lastBPoint = 0;

	std::cout<<"init "<<lastBPoint<<std::endl;

	float distX= _cameraPath[(lastBPoint)*3+0]-_cameraPictorialModel(0,3);
	float distY= _cameraPath[(lastBPoint)*3+1]-_cameraPictorialModel(1,3);
	float distZ= _cameraPath[(lastBPoint)*3+2]-_cameraPictorialModel(2,3);
	float length = sqrt(powf(distX,2)+powf(distY,2)+powf(distZ,2));

	if(length<distanceToGo){//add next distance
		/*do{
			lastBPoint++;
			if((_cameraPath.size()/3)-1<=lastBPoint){
				lastBPoint=0;
				_cameraPictorialModel(0,3) =_cameraPath[0];
				_cameraPictorialModel(1,3) =_cameraPath[1];
				_cameraPictorialModel(2,3) =_cameraPath[2];
				std::cout<<std::endl<<std::endl<<"stop"<<std::endl;
				return;
			}
			distX= _cameraPath[(lastBPoint+1)*3+0]-_cameraPath[lastBPoint*3+0];
			distY= _cameraPath[(lastBPoint+1)*3+1]-_cameraPath[lastBPoint*3+1];
			distZ= _cameraPath[(lastBPoint+1)*3+2]-_cameraPath[lastBPoint*3+2];
			length += sqrt(powf(distX,2)+powf(distY,2)+powf(distZ,2));

		}while(length<distanceToGo);

		lastBPoint--;//get the point just befor the distance we look for

		//compute distance to go
		distX= _cameraPath[(lastBPoint+1)*3+0]-_cameraPath[lastBPoint*3+0];
		distY= _cameraPath[(lastBPoint+1)*3+1]-_cameraPath[lastBPoint*3+1];
		distZ= _cameraPath[(lastBPoint+1)*3+2]-_cameraPath[lastBPoint*3+2];
		length -= sqrt(powf(distX,2)+powf(distY,2)+powf(distZ,2));

		//compute distance from nearest Bpoint
		distanceToGo -= length;

		length = sqrt(powf(distX,2)+powf(distY,2)+powf(distZ,2));

		float coeffX = distX/length;
		float coeffY = distY/length;
		float coeffZ = distZ/length;

		float distanceToGoX = coeffX*distanceToGo;
		float distanceToGoY = coeffY*distanceToGo;
		float distanceToGoZ = coeffZ*distanceToGo;

				std::cout<<distanceToGoX<<" "<<distanceToGoY<< " "<<distanceToGoZ<<std::endl;

		_cameraPictorialModel(0,3) = _cameraPath[lastBPoint*3+0]+distanceToGoX;
		_cameraPictorialModel(1,3) = _cameraPath[lastBPoint*3+1]+distanceToGoY;
		_cameraPictorialModel(2,3) = _cameraPath[lastBPoint*3+2]+distanceToGoZ;*/
	}
	//else{
		std::cout<<std::endl<<std::endl<<"long"<<std::endl;

		float coeffX = distX/length;
		float coeffY = distY/length;
		float coeffZ = distZ/length;

		float distanceToGoX = distanceToGo/coeffX;
		float distanceToGoY = distanceToGo/coeffY;
		float distanceToGoZ = distanceToGo/coeffZ;
		std::cout<<distanceToGoX<<" "<<distanceToGoY<< " "<<distanceToGoZ<<std::endl;

		_cameraPictorialModel(0,3) = _cameraPictorialModel(0,3)+distanceToGoX;
		_cameraPictorialModel(1,3) = _cameraPictorialModel(1,3)+distanceToGoY;
		_cameraPictorialModel(2,3) = _cameraPictorialModel(2,3)+distanceToGoZ;
	//}

	/*
    if(i<(_cameraPath.size()/3)-1){
		double currentTime = glfwGetTime();
		float deltaT = float(currentTime - lastTime); //deltaT in sc
		if(deltaT>0.02){
			lastTime = currentTime;

			float posX	= _cameraPath[i*3+0];
            float posY	= _cameraPath[i*3+1];
            float posZ	= _cameraPath[i*3+2];

            float lookX = _cameraPath[(i+1)*3+0];
            float lookY = _cameraPath[(i+1)*3+1];
            float lookZ = _cameraPath[(i+1)*3+2];

			//update camera position and look at
			update_camera_modelview(posX,posY,posZ,lookX,lookY,lookZ);

			//update pictorial camera poisition and look at
			_cameraPictorialModel(0,3) = posX;
			_cameraPictorialModel(1,3) = posY;
			_cameraPictorialModel(2,3) = posZ;

			float dirX= lookX-posX;
			float dirY= lookY-posY;
			float dirZ= lookZ-posZ;

			float length = sqrt(dirX*dirX+dirY*dirY+dirZ*dirZ);

			float dirXnorm = dirX/length;
			float dirYnorm = dirY/length;
			float dirZnorm = dirZ/length;

			float angleZ = atan2(dirYnorm,dirXnorm);
			float angleY = atan2(sqrt(dirYnorm*dirYnorm+dirXnorm*dirXnorm),dirZnorm);

			mat3 rot = Eigen::AngleAxisf(angleZ, Eigen::Vector3f::UnitZ()).toRotationMatrix()
				* Eigen::AngleAxisf(angleY, Eigen::Vector3f::UnitY()).toRotationMatrix();

			for (int j=0; j<3;j++){
				for(int k=0; k<3;k++){
					_cameraPictorialModel(k,j)=rot(k,j);
				}
			}
			i++;
		}
	}
	else{
		i=0;
	}*/
}

void CameraControl::InitdeCasteljauSubdivision(){
	//here we use subdivision to create more control point in order to generate bezier curve
	//control points
	float b0X = -0.51f, b0Y =  1.09f, b0Z = 0.40f;
	float b1X =  0.57f, b1Y =  1.26f, b1Z = 0.40f;
	float b2X =  1.31f, b2Y =  0.92f, b2Z = 0.40f;
	float b3X =  1.25f, b3Y = -0.41f, b3Z = 0.40f;

	float l0X = b0X, l0Y = b0Y, l0Z = b0Z;

	double l1X = 0.5 * (b0X + b1X);
	double l1Y = 0.5 * (b0Y + b1Y);
	double l1Z = 0.5 * (b0Z + b1Z);

	double l2X = 0.25 * (b0X + 2*b1X + b2X);
	double l2Y = 0.25 * (b0Y + 2*b1Y + b2Y);
	double l2Z = 0.25 * (b0Z + 2*b1Z + b2Z);

	double l3X = 0.125 * (b0X + 3*b1X + 3*b2X + b3X);
	double l3Y = 0.125 * (b0Y + 3*b1Y + 3*b2Y + b3Y);
	double l3Z = 0.125 * (b0Z + 3*b1Z + 3*b2Z + b3Z);

	double r0X = l3X, r0Y = l3Y, r0Z = l3Z;

	double r1X = 0.25 * (b1X + 2*b2X + b3X);
	double r1Y = 0.25 * (b1Y + 2*b2Y + b3Y);
	double r1Z = 0.25 * (b1Z + 2*b2Z + b3Z);

	double r2X = 0.5 * (b2X + b3X);
	double r2Y = 0.5 * (b2Y + b3Y);
	double r2Z = 0.5 * (b2Z + b3Z);

	double r3X = b3X, r3Y = b3Y, r3Z = b3Z;

	double tmp0X = l0X, tmp0Y=l0Y, tmp0Z=l0Z;
	double tmp1X = l1X, tmp1Y=l1Y, tmp1Z=l1Z;
	double tmp2X = l2X, tmp2Y=l2Y, tmp2Z=l2Z;
	double tmp3X = l3X, tmp3Y=l3Y, tmp3Z=l3Z;

	float t=0;
	for (int i=0; i<=500; i++){

		double posX = powf((1-t),3) * tmp0X + 3*t*powf((1-t),2) * tmp1X + 3*powf(t,2)*(1-t) *tmp2X + powf(t,3)*tmp3X;
		double posY = powf((1-t),3) * tmp0Y + 3*t*powf((1-t),2) * tmp1Y + 3*powf(t,2)*(1-t) *tmp2Y + powf(t,3)*tmp3Y;
		double posZ = powf((1-t),3) * tmp0Z + 3*t*powf((1-t),2) * tmp1Z + 3*powf(t,2)*(1-t) *tmp2Z + powf(t,3)*tmp3Z;

        _cameraPath[i*3+0] = float(posX);
        _cameraPath[i*3+1] = float(posY);
        _cameraPath[i*3+2] = float(posZ);
		//std::cout<<"1 "<<t<<" "<<posX<<" "<<posY<<std::endl;

		t = float(t+0.001);
	}

	tmp0X = r0X, tmp0Y=r0Y, tmp0Z=r0Z;
	tmp1X = r1X, tmp1Y=r1Y, tmp1Z=r1Z;
	tmp2X = r2X, tmp2Y=r2Y, tmp2Z=r2Z;
	tmp3X = r3X, tmp3Y=r3Y, tmp3Z=r3Z;
	for (int i=500; i<=1000; i++){

		double posX = powf((1-t),3) * tmp0X + 3*t*powf((1-t),2) * tmp1X + 3*powf(t,2)*(1-t) *tmp2X + powf(t,3)*tmp3X;
		double posY = powf((1-t),3) * tmp0Y + 3*t*powf((1-t),2) * tmp1Y + 3*powf(t,2)*(1-t) *tmp2Y + powf(t,3)*tmp3Y;
		double posZ = powf((1-t),3) * tmp0Z + 3*t*powf((1-t),2) * tmp1Z + 3*powf(t,2)*(1-t) *tmp2Z + powf(t,3)*tmp3Z;

        _cameraPath[i*3+0] = float(posX);
        _cameraPath[i*3+1] = float(posY);
        _cameraPath[i*3+2] = float(posZ);

		//std::cout<<"2 "<<t<<" "<<posX<<" "<<posY<<std::endl;

		t = float(t+0.001);
	}
}

void CameraControl::InitSubdivision(){

	//test time to compute
	static double lastTime = glfwGetTime();

	//control points
	//b0[iteration][XYZ]
	const int iteration = 8;
	const int nPoints = 256; //2^iteration
	float res0[nPoints][3]; //[3][2] = {{1,1},{2,2},{3,3}};
	float res1[nPoints][3];
	float res2[nPoints][3];
	float res3[nPoints][3];

	res0[0][0]=-0.51f; //b0X
	res1[0][0]=-0.43f; //b1X
	res2[0][0]= 0.31f; //b2X
	res3[0][0]= 0.55f; //b3X

	res0[0][1]=-0.91f; //b0Y
	res1[0][1]= 2.66f; //b1Y
	res2[0][1]=-2.08f; //b2Y
	res3[0][1]= 0.39f; //b3Y

	res0[0][2]= 0.20f; //b0Z
	res1[0][2]= 0.00f; //b1Z
	res2[0][2]= 0.50f; //b2Z
	res3[0][2]= 0.30f; //b3Z

	float b0,b1,b2,b3;
	float l0,l1,l2,l3,r1,r2,r3;
	for(int i=0; i<iteration;i++){ // iterate to get more set of points
		for(int j=int(pow(2.0,i)-1); j>=0;j--){
			for(int k=0;k<3;k++){//iterate for X Y Z
				b0=res0[j][k];
				b1=res1[j][k];
				b2=res2[j][k];
				b3=res3[j][k];

				Subdivision(b0,b1,b2,b3,l0,l1,l2,l3,r1,r2,r3);
				res0[j*2][k] =l0;
				res1[j*2][k] =l1;
				res2[j*2][k] =l2;
				res3[j*2][k] =l3;
				res0[j*2+1][k]=l3;
				res1[j*2+1][k]=r1;
				res2[j*2+1][k]=r2;
				res3[j*2+1][k]=r3;
			}
		}
	}

    _cameraPath.clear();
    /// To avoid vector resizing on every loop.
    _cameraPath.reserve(3*nPoints*3);

    /// Generate coordinates.
    for(int k=0; k<nPoints; ++k) {
        _cameraPath.push_back(res0[k][0]);
        _cameraPath.push_back(res0[k][1]);
        _cameraPath.push_back(res0[k][2]);

        _cameraPath.push_back(res1[k][0]);
        _cameraPath.push_back(res1[k][1]);
        _cameraPath.push_back(res1[k][2]);

        _cameraPath.push_back(res2[k][0]);
        _cameraPath.push_back(res2[k][1]);
        _cameraPath.push_back(res2[k][2]);
    }

	//end test time to compute
	/*double currentTime = glfwGetTime();
    float deltaT = float(currentTime - lastTime); //deltaT in sc
    std::cout<<"subdivision computed in "<<deltaT<<" sc with N = "<< _cameraPath.size()<<" points"<<std::endl;
	lastTime = currentTime;*/

	/// Copy the vertices to GPU.
    _verticesCameraPath ->copy(_cameraPath.data(), _cameraPath.size());

}

void CameraControl::Subdivision(float b0,float b1, float b2,float b3, float& l0, float& l1, float& l2, float& l3 ,float& r1,float& r2, float& r3 ){
	l0 = b0;
	l1 = float(0.5 * (b0 + b1));
	l2 = float(0.25 * (b0 + 2*b1 + b2));
	l3 = float(0.125 * (b0 + 3*b1 + 3*b2 + b3));
	r1 = float(0.25 * (b1 + 2*b2 + b3));
	r2 = float(0.5 * (b2 + b3));
	r3 = b3;
}

void CameraControl::bezier_4_points(int PointToChange, float deltaX, float deltaY, float deltaZ) {

    /// Bézier curve control points.
    static float b0X = -0.51f, b0Y = -0.91f, b0Z = 0.20f;
    static float b1X = -0.43f, b1Y =  2.66f, b1Z = 0.001f;
    static float b2X =  0.31f, b2Y = -2.08f, b2Z = 0.50f;
    static float b3X =  0.55f, b3Y =  0.39f, b3Z = 0.30f;

	switch(PointToChange){
	case 0:
        b0X += deltaX;
        b0Y += deltaY;
        b0Z += deltaZ;
		break;
	case 1:
        b1X += deltaX;
        b1Y += deltaY;
        b1Z += deltaZ;
		break;
	case 2:
        b2X += deltaX;
        b2Y += deltaY;
        b2Z += deltaZ;
		break;
	case 3:
        b3X += deltaX;
        b3Y += deltaY;
        b3Z += deltaZ;
		break;
    }

    /// Fill the control points vector.
    _cameraPathControls.clear();
    _cameraPathControls.reserve(12);
    _cameraPathControls.push_back(b0X);
    _cameraPathControls.push_back(b0Y);
    _cameraPathControls.push_back(b0Z);
    _cameraPathControls.push_back(b1X);
    _cameraPathControls.push_back(b1Y);
    _cameraPathControls.push_back(b1Z);
    _cameraPathControls.push_back(b2X);
    _cameraPathControls.push_back(b2Y);
    _cameraPathControls.push_back(b2Z);
    _cameraPathControls.push_back(b3X);
    _cameraPathControls.push_back(b3Y);
    _cameraPathControls.push_back(b3Z);

	/// Choose the resolution.
    const unsigned int nPoints = 500;

    /// To avoid vector resizing on every loop.
    _cameraPath.clear();
    _cameraPath.reserve(3*nPoints);

    /// Generate coordinates.
    for(int k=0; k<nPoints; ++k) {
        float t = float(k) / float(nPoints);
        _cameraPath.push_back(std::pow((1-t),3)*b0X + 3*t*std::pow((1-t),2)*b1X + 3*std::pow(t,2)*(1-t)*b2X + std::pow(t,3)*b3X);
        _cameraPath.push_back(std::pow((1-t),3)*b0Y + 3*t*std::pow((1-t),2)*b1Y + 3*std::pow(t,2)*(1-t)*b2Y + std::pow(t,3)*b3Y);
        _cameraPath.push_back(std::pow((1-t),3)*b0Z + 3*t*std::pow((1-t),2)*b1Z + 3*std::pow(t,2)*(1-t)*b2Z + std::pow(t,3)*b3Z);
    }

    /// Copy the vertices to GPU.
    _verticesCameraPath->copy(_cameraPath.data(), _cameraPath.size());
    _verticesCameraPathControls->copy(_cameraPathControls.data(), _cameraPathControls.size());

}

void CameraControl::createBCurve(){
    int AmountBcurve = int(_cameraPathControls.size()/12);

	/// Choose the resolution.
	const unsigned int nPoints = 300*AmountBcurve;

    _cameraPath.clear();
	/// To avoid vector resizing on every loop.
    _cameraPath.reserve(3*nPoints);

	for(int i=1;i<=AmountBcurve;i++){

		//other setting flying through
        float b0X = _cameraPathControls.at(i*12-12);
        float b0Y = _cameraPathControls.at(i*12-11);
        float b0Z = _cameraPathControls.at(i*12-10);
        float b1X = _cameraPathControls.at(i*12-9);
        float b1Y = _cameraPathControls.at(i*12-8);
        float b1Z = _cameraPathControls.at(i*12-7);
        float b2X = _cameraPathControls.at(i*12-6);
        float b2Y = _cameraPathControls.at(i*12-5);
        float b2Z = _cameraPathControls.at(i*12-4);
        float b3X = _cameraPathControls.at(i*12-3);
        float b3Y = _cameraPathControls.at(i*12-2);
        float b3Z = _cameraPathControls.at(i*12-1);

		/// Generate coordinates.
		for(unsigned int k=0; k<nPoints; ++k) {
			float t = float(k) / float(nPoints);
            _cameraPath.push_back(std::pow((1-t),3)*b0X + 3*t*std::pow((1-t),2)*b1X + 3*std::pow(t,2)*(1-t)*b2X + std::pow(t,3)*b3X);
            _cameraPath.push_back(std::pow((1-t),3)*b0Y + 3*t*std::pow((1-t),2)*b1Y + 3*std::pow(t,2)*(1-t)*b2Y + std::pow(t,3)*b3Y);
            _cameraPath.push_back(std::pow((1-t),3)*b0Z + 3*t*std::pow((1-t),2)*b1Z + 3*std::pow(t,2)*(1-t)*b2Z + std::pow(t,3)*b3Z);
		}
	}
    /// Copy the vertices to GPU.
    _verticesCameraPath ->copy(_cameraPath.data(), _cameraPath.size());

}

void CameraControl::MultipleBezier() {//init

//    //// setting first bezier curve
//    const float bA0X = -1.00f,bA0Y =  0.00f,bA0Z =  0.40f;
//    const float bA1X = -0.50f,bA1Y = -1.00f,bA1Z =  0.40f;
//    const float bA2X = -0.50f,bA2Y =  1.00f,bA2Z =  0.40f;
//    const float bA3X =  0.00f,bA3Y =  0.00f,bA3Z =  0.40f;

//    //// setting second bezier curve
//    const float bB0X =  bA3X ,bB0Y =  bA3Y ,bB0Z =  bA3Z ; // PB0 = PA3
//    const float bB1X = 2*bB0X - bA2X;//PB1 = 2PB0 - PA2
//    const float bB1Y = 2*bB0Y - bA2Y;//PB1 is the PA2 mirrored on PB0
//    const float bB1Z = 2*bB0Z - bA2Z;
//    const float bB2X =  0.50f,bB2Y =  1.00f,bB2Z =  0.40f;
//    const float bB3X =  1.00f,bB3Y =  0.00f,bB3Z =  0.40f;

    //// setting first bezier curve
    const float bA0X = -0.50f,bA0Y = -1.00f,bA0Z =  0.20f;
    const float bA1X = -0.80f,bA1Y = -0.80f,bA1Z =  0.40f;
    const float bA2X = -0.30f,bA2Y =  0.20f,bA2Z =  0.00f;
    const float bA3X =  0.00f,bA3Y =  0.00f,bA3Z =  0.20f;

	//// setting second bezier curve
	const float bB0X =  bA3X ,bB0Y =  bA3Y ,bB0Z =  bA3Z ; // PB0 = PA3
    const float bB1X = 2*bB0X - bA2X;//PB1 = 2PB0 - PA2
	const float bB1Y = 2*bB0Y - bA2Y;//PB1 is the PA2 mirrored on PB0
    const float bB1Z = 2*bB0Z - bA2Z;
    const float bB2X =  0.30f,bB2Y =  1.00f,bB2Z =  0.20f;
    const float bB3X =  0.60f,bB3Y =  0.80f,bB3Z =  0.20f;

    //// Fill the control points vector.
    _cameraPathControls.clear();
    _cameraPathControls.reserve(21);
    _cameraPathControls.push_back(bA0X);
	_cameraPathControls.push_back(bA0Y);
    _cameraPathControls.push_back(bA0Z);
    _cameraPathControls.push_back(bA1X);
    _cameraPathControls.push_back(bA1Y);
    _cameraPathControls.push_back(bA1Z);
    _cameraPathControls.push_back(bA2X);
    _cameraPathControls.push_back(bA2Y);
    _cameraPathControls.push_back(bA2Z);
    _cameraPathControls.push_back(bA3X);
    _cameraPathControls.push_back(bA3Y);
    _cameraPathControls.push_back(bA3Z);

	_cameraPathControls.push_back(bB1X);
    _cameraPathControls.push_back(bB1Y);
    _cameraPathControls.push_back(bB1Z);
    _cameraPathControls.push_back(bB2X);
    _cameraPathControls.push_back(bB2Y);
    _cameraPathControls.push_back(bB2Z);
    _cameraPathControls.push_back(bB3X);
    _cameraPathControls.push_back(bB3Y);
    _cameraPathControls.push_back(bB3Z);

	 /// Copy the vertices to GPU.
    _verticesCameraPathControls->copy(_cameraPathControls.data(), _cameraPathControls.size());

    /// Choose the resolution.
    const unsigned int nPoints = 350;

    _cameraPath.clear();
    /// To avoid vector resizing on every loop.
    _cameraPath.reserve(2*3*nPoints);

    /// Generate coordinates of first bezier curve.
    for(int k=0; k<nPoints; ++k) {
        float t = float(k) / float(nPoints);
        _cameraPath.push_back(std::pow((1-t),3)*bA0X + 3*t*std::pow((1-t),2)*bA1X + 3*std::pow(t,2)*(1-t)*bA2X + std::pow(t,3)*bA3X);
        _cameraPath.push_back(std::pow((1-t),3)*bA0Y + 3*t*std::pow((1-t),2)*bA1Y + 3*std::pow(t,2)*(1-t)*bA2Y + std::pow(t,3)*bA3Y);
        _cameraPath.push_back(std::pow((1-t),3)*bA0Z + 3*t*std::pow((1-t),2)*bA1Z + 3*std::pow(t,2)*(1-t)*bA2Z + std::pow(t,3)*bA3Z);
    }
	/// Generate coordinates of second bezier curve.
	for(int k=1; k<nPoints; ++k) {
        float t = float(k) / float(nPoints);
        _cameraPath.push_back(std::pow((1-t),3)*bB0X + 3*t*std::pow((1-t),2)*bB1X + 3*std::pow(t,2)*(1-t)*bB2X + std::pow(t,3)*bB3X);
        _cameraPath.push_back(std::pow((1-t),3)*bB0Y + 3*t*std::pow((1-t),2)*bB1Y + 3*std::pow(t,2)*(1-t)*bB2Y + std::pow(t,3)*bB3Y);
        _cameraPath.push_back(std::pow((1-t),3)*bB0Z + 3*t*std::pow((1-t),2)*bB1Z + 3*std::pow(t,2)*(1-t)*bB2Z + std::pow(t,3)*bB3Z);
    }
	 /// Copy the vertices to GPU.
    _verticesCameraPath ->copy(_cameraPath.data(), _cameraPath.size());

}

void CameraControl::N_MultipleBezier_controlled(unsigned int PointToChange, float deltaX, float deltaY, float deltaZ) {

	if(PointToChange==0){
		_cameraPathControls.at(PointToChange+0)+= deltaX;
		_cameraPathControls.at(PointToChange+1)+= deltaY;
		_cameraPathControls.at(PointToChange+2)+= deltaZ;

	}
	else if (PointToChange==1){
		_cameraPathControls.at(PointToChange*3+0) += deltaX;
        _cameraPathControls.at(PointToChange*3+1) += deltaY;
        _cameraPathControls.at(PointToChange*3+2) += deltaZ;
	}
	else if (PointToChange==2){
		_cameraPathControls.at(PointToChange*3+0) += deltaX;
        _cameraPathControls.at(PointToChange*3+1) += deltaY;
        _cameraPathControls.at(PointToChange*3+2) += deltaZ;
		if(_cameraPathControls.size()>12){
			//refresh other control points to respect contstraint
			_cameraPathControls.at(PointToChange*3+0+6) = 2*_cameraPathControls.at(PointToChange*3+0+3) - _cameraPathControls.at(PointToChange*3+0);
			_cameraPathControls.at(PointToChange*3+1+6) = 2*_cameraPathControls.at(PointToChange*3+1+3) - _cameraPathControls.at(PointToChange*3+1);
			_cameraPathControls.at(PointToChange*3+2+6) = 2*_cameraPathControls.at(PointToChange*3+2+3) - _cameraPathControls.at(PointToChange*3+2);
		}
	}
	else if (PointToChange==3){
		_cameraPathControls.at(PointToChange*3+0) += deltaX;
        _cameraPathControls.at(PointToChange*3+1) += deltaY;
        _cameraPathControls.at(PointToChange*3+2) += deltaZ;
		if(_cameraPathControls.size()>12){
			//refresh other control points to respect contstraint
			_cameraPathControls.at(PointToChange*3+0+3) = 2*_cameraPathControls.at(PointToChange*3+0+0) - _cameraPathControls.at(PointToChange*3+0-3);
			_cameraPathControls.at(PointToChange*3+1+3) = 2*_cameraPathControls.at(PointToChange*3+1+0) - _cameraPathControls.at(PointToChange*3+1-3);
			_cameraPathControls.at(PointToChange*3+2+3) = 2*_cameraPathControls.at(PointToChange*3+2+0) - _cameraPathControls.at(PointToChange*3+2-3);
		}
	}
	else if((PointToChange>1) & (((PointToChange-1)%3) ==0)){
		_cameraPathControls.at(PointToChange*3+0) += deltaX;
        _cameraPathControls.at(PointToChange*3+1) += deltaY;
        _cameraPathControls.at(PointToChange*3+2) += deltaZ;

		//refresh other control points to respect contstraint
		_cameraPathControls.at(PointToChange*3+0-6) = 2*_cameraPathControls.at(PointToChange*3+0-3) - _cameraPathControls.at(PointToChange*3+0);
		_cameraPathControls.at(PointToChange*3+1-6) = 2*_cameraPathControls.at(PointToChange*3+1-3) - _cameraPathControls.at(PointToChange*3+1);
		_cameraPathControls.at(PointToChange*3+2-6) = 2*_cameraPathControls.at(PointToChange*3+2-3) - _cameraPathControls.at(PointToChange*3+2);

		//_cameraPathControls.at(6) = - _cameraPathControls.at(12) + 2*_cameraPathControls.at(9) ;
		//_cameraPathControls.at(7) = - _cameraPathControls.at(13) + 2*_cameraPathControls.at(10);
		//_cameraPathControls.at(8) = - _cameraPathControls.at(14) + 2*_cameraPathControls.at(11);
	}
	else if((PointToChange>1) & (((PointToChange-1)%3) ==1)){
		_cameraPathControls.at(PointToChange*3+0) += deltaX;
        _cameraPathControls.at(PointToChange*3+1) += deltaY;
        _cameraPathControls.at(PointToChange*3+2) += deltaZ;
		if((_cameraPathControls.size()/3)>PointToChange+3){//only if necessary
			//refresh other control points to respect contstraint
			_cameraPathControls.at(PointToChange*3+0+6) = 2*_cameraPathControls.at(PointToChange*3+0+3) - _cameraPathControls.at(PointToChange*3+0);
			_cameraPathControls.at(PointToChange*3+1+6) = 2*_cameraPathControls.at(PointToChange*3+1+3) - _cameraPathControls.at(PointToChange*3+1);
			_cameraPathControls.at(PointToChange*3+2+6) = 2*_cameraPathControls.at(PointToChange*3+2+3) - _cameraPathControls.at(PointToChange*3+2);
		}
	}
	else if((PointToChange>1) & (((PointToChange-1)%3) ==2)){
		_cameraPathControls.at(PointToChange*3+0) += deltaX;
        _cameraPathControls.at(PointToChange*3+1) += deltaY;
        _cameraPathControls.at(PointToChange*3+2) += deltaZ;
		if((_cameraPathControls.size()/3)>PointToChange+2){//only if necessary
			//refresh other control points to respect contstraint
			_cameraPathControls.at(PointToChange*3+0+3) = 2*_cameraPathControls.at(PointToChange*3+0+0) - _cameraPathControls.at(PointToChange*3+0-3);
			_cameraPathControls.at(PointToChange*3+1+3) = 2*_cameraPathControls.at(PointToChange*3+1+0) - _cameraPathControls.at(PointToChange*3+1-3);
			_cameraPathControls.at(PointToChange*3+2+3) = 2*_cameraPathControls.at(PointToChange*3+2+0) - _cameraPathControls.at(PointToChange*3+2-3);
		}
	}

    /// Choose the resolution.
    const unsigned int nPoints = 200;

    _cameraPath.clear();
    /// To avoid vector resizing on every loop.
    _cameraPath.reserve(2*3*nPoints);

    /// Generate coordinates of first bezier curve.
    for(int k=0; k<nPoints; ++k) {
        float t = float(k) / float(nPoints);
		for(int l=0;l<3;l++){
			_cameraPath.push_back(std::pow((1-t),3)*_cameraPathControls.at(0+l) + 3*t*std::pow((1-t),2)*_cameraPathControls.at(3+l) + 3*std::pow(t,2)*(1-t)*_cameraPathControls.at(6+l) + std::pow(t,3)*_cameraPathControls.at(9+l));
		}
	}
	/// Generate coordinates of nexts bezier curve.
	for(int m=1;m<=int((_cameraPathControls.size()-3)/9)-1;m++){
		for(int k=1; k<nPoints; ++k) {
			float t = float(k) / float(nPoints);
			for(int l=0;l<3;l++){
			_cameraPath.push_back(std::pow((1-t),3)*_cameraPathControls.at(9+l+(m-1)*9) + 3*t*std::pow((1-t),2)*_cameraPathControls.at(12+l+(m-1)*9) + 3*std::pow(t,2)*(1-t)*_cameraPathControls.at(15+l+(m-1)*9) + std::pow(t,3)*_cameraPathControls.at(18+l+(m-1)*9));
			}
			//_cameraPath.push_back(std::pow((1-t),3)*bB0Y + 3*t*std::pow((1-t),2)*bB1Y + 3*std::pow(t,2)*(1-t)*bB2Y + std::pow(t,3)*bB3Y);
			//_cameraPath.push_back(std::pow((1-t),3)*bB0Z + 3*t*std::pow((1-t),2)*bB1Z + 3*std::pow(t,2)*(1-t)*bB2Z + std::pow(t,3)*bB3Z);
		}
	}
	/// Copy the vertices to GPU.
    _verticesCameraPath->copy(_cameraPath.data(), _cameraPath.size());
    _verticesCameraPathControls->copy(_cameraPathControls.data(), _cameraPathControls.size());

	animatePictorialCamera(1);
}

void CameraControl::Add_Bcurve(){

	//add control point 1 under constraint of the two previous ctrl point to keep continuity
	_cameraPathControls.push_back( 2*_cameraPathControls.at(_cameraPathControls.size()-3) - _cameraPathControls.at(_cameraPathControls.size()-6));
	_cameraPathControls.push_back( 2*_cameraPathControls.at(_cameraPathControls.size()-3) - _cameraPathControls.at(_cameraPathControls.size()-6));
	_cameraPathControls.push_back( 2*_cameraPathControls.at(_cameraPathControls.size()-3) - _cameraPathControls.at(_cameraPathControls.size()-6));

	//add control point 2 near control point 1
	_cameraPathControls.push_back(_cameraPathControls.at(_cameraPathControls.size()-3)+float(0.1));
	_cameraPathControls.push_back(_cameraPathControls.at(_cameraPathControls.size()-3));
	_cameraPathControls.push_back(_cameraPathControls.at(_cameraPathControls.size()-3));

	//add control point 3 near control point 2
	_cameraPathControls.push_back(_cameraPathControls.at(_cameraPathControls.size()-9)+float(0.4));
	_cameraPathControls.push_back(_cameraPathControls.at(_cameraPathControls.size()-9));
	_cameraPathControls.push_back(_cameraPathControls.at(_cameraPathControls.size()-9));

	//display new ctrl point and Bcurve
	N_MultipleBezier_controlled(0,0,0,0);
	//_verticesCameraPathControls->copy(_cameraPathControls.data(), _cameraPathControls.size());
}

void CameraControl::Remove_Bcurve(){

	//add control point 1 under constraint of the two previous ctrl point to keep continuity
	_cameraPathControls.pop_back();
	_cameraPathControls.pop_back();
	_cameraPathControls.pop_back();

	_cameraPathControls.pop_back();
	_cameraPathControls.pop_back();
	_cameraPathControls.pop_back();

	_cameraPathControls.pop_back();
	_cameraPathControls.pop_back();
	_cameraPathControls.pop_back();

	//display Bcurve
	N_MultipleBezier_controlled(0,0,0,0);
}

void CameraControl::MultipleBezier_controlled(unsigned int PointToChange, float deltaX, float deltaY, float deltaZ) {

	//// setting first bezier curve
	static float bA0X = -1.00f,bA0Y =  0.00f,bA0Z =  0.40f;
    static float bA1X = -0.50f,bA1Y = -1.00f,bA1Z =  0.40f;
    static float bA2X = -0.50f,bA2Y =  1.00f,bA2Z =  0.40f;
    static float bA3X =  0.00f,bA3Y =  0.00f,bA3Z =  0.40f;

	//// setting second bezier curve
	static float bB0X =  bA3X ,bB0Y =  bA3Y ,bB0Z =  bA3Z ; // PB0 = PA3
    static float bB1X = 2*bB0X - bA2X;//PB1 = 2PB0 - PA2
	static float bB1Y = 2*bB0Y - bA2Y;//PB1 is the PA2 mirrored on PB0
	static float bB1Z = 2*bB0Z - bA2Z;
    static float bB2X =  0.50f,bB2Y =  1.00f,bB2Z =  0.40f;
    static float bB3X =  1.00f,bB3Y =  0.00f,bB3Z =  0.40f;

	switch(PointToChange){
	case 0:
        bA0X += deltaX;
        bA0Y += deltaY;
        bA0Z += deltaZ;
		break;
	case 1:
        bA1X += deltaX;
        bA1Y += deltaY;
        bA1Z += deltaZ;
		break;
	case 2:
        bA2X += deltaX;
        bA2Y += deltaY;
        bA2Z += deltaZ;

		//refresh other control points
		bB1X = 2*bB0X - bA2X;
		bB1Y = 2*bB0Y - bA2Y;
		bB1Z = 2*bB0Z - bA2Z;
		break;
	case 3:
        bA3X += deltaX;
        bA3Y += deltaY;
        bA3Z += deltaZ;

		//refresh other control points
		bB0X =  bA3X ,bB0Y =  bA3Y ,bB0Z =  bA3Z ;

		bB1X = 2*bB0X - bA2X;
		bB1Y = 2*bB0Y - bA2Y;
		bB1Z = 2*bB0Z - bA2Z;
		break;
	case 4:
		bB1X += deltaX;
		bB1Y += deltaY;
		bB1Z += deltaZ;

		//refresh other control points
		bA2X = - bB1X + 2*bB0X;
		bA2Y = - bB1Y + 2*bB0Y;
		bA2Z = - bB1Z + 2*bB0Z;
		break;
	case 5:
        bB2X += deltaX;
        bB2Y += deltaY;
        bB2Z += deltaZ;
		break;
	case 6:
        bB3X += deltaX;
        bB3Y += deltaY;
        bB3Z += deltaZ;
		break;
    }

    /// Fill the control points vector.
    _cameraPathControls.clear();
    _cameraPathControls.reserve(21);
    _cameraPathControls.push_back(bA0X);
	_cameraPathControls.push_back(bA0Y);
    _cameraPathControls.push_back(bA0Z);
    _cameraPathControls.push_back(bA1X);
    _cameraPathControls.push_back(bA1Y);
    _cameraPathControls.push_back(bA1Z);
    _cameraPathControls.push_back(bA2X);
    _cameraPathControls.push_back(bA2Y);
    _cameraPathControls.push_back(bA2Z);
    _cameraPathControls.push_back(bA3X);
    _cameraPathControls.push_back(bA3Y);
    _cameraPathControls.push_back(bA3Z);

	_cameraPathControls.push_back(bB1X);
    _cameraPathControls.push_back(bB1Y);
    _cameraPathControls.push_back(bB1Z);
    _cameraPathControls.push_back(bB2X);
    _cameraPathControls.push_back(bB2Y);
    _cameraPathControls.push_back(bB2Z);
    _cameraPathControls.push_back(bB3X);
    _cameraPathControls.push_back(bB3Y);
    _cameraPathControls.push_back(bB3Z);

	std::cout<<_cameraPathControls.size()<<" "<<int((_cameraPathControls.size()-3)/9)<<std::endl;

    /// Choose the resolution.
    const unsigned int nPoints = 200;

    _cameraPath.clear();
    /// To avoid vector resizing on every loop.
    _cameraPath.reserve(2*3*nPoints);

    /// Generate coordinates of first bezier curve.
    for(int k=0; k<nPoints; ++k) {
        float t = float(k) / float(nPoints);
        _cameraPath.push_back(std::pow((1-t),3)*bA0X + 3*t*std::pow((1-t),2)*bA1X + 3*std::pow(t,2)*(1-t)*bA2X + std::pow(t,3)*bA3X);
        _cameraPath.push_back(std::pow((1-t),3)*bA0Y + 3*t*std::pow((1-t),2)*bA1Y + 3*std::pow(t,2)*(1-t)*bA2Y + std::pow(t,3)*bA3Y);
        _cameraPath.push_back(std::pow((1-t),3)*bA0Z + 3*t*std::pow((1-t),2)*bA1Z + 3*std::pow(t,2)*(1-t)*bA2Z + std::pow(t,3)*bA3Z);
    }
	/// Generate coordinates of second bezier curve.
	for(int k=1; k<nPoints; ++k) {
        float t = float(k) / float(nPoints);
        _cameraPath.push_back(std::pow((1-t),3)*bB0X + 3*t*std::pow((1-t),2)*bB1X + 3*std::pow(t,2)*(1-t)*bB2X + std::pow(t,3)*bB3X);
        _cameraPath.push_back(std::pow((1-t),3)*bB0Y + 3*t*std::pow((1-t),2)*bB1Y + 3*std::pow(t,2)*(1-t)*bB2Y + std::pow(t,3)*bB3Y);
        _cameraPath.push_back(std::pow((1-t),3)*bB0Z + 3*t*std::pow((1-t),2)*bB1Z + 3*std::pow(t,2)*(1-t)*bB2Z + std::pow(t,3)*bB3Z);
    }
	/// Copy the vertices to GPU.
    _verticesCameraPath->copy(_cameraPath.data(), _cameraPath.size());
    _verticesCameraPathControls->copy(_cameraPathControls.data(), _cameraPathControls.size());
}

void CameraControl::flyingExploration(){
	static float straightMaxSpeed = 0.5f;
	static float rotationMaxSpeed = 40.0f;
	static float straightAcceleration = 0.05f;
	static float rotationAcceleration = 4.0f;

	//To set user defined bcurve
	static bool ModeSettingControlPoint=false;
	static int controlPointsCount = 0;
	//To tmp save the control point
	static float tmpCtrlPointX;
	static float tmpCtrlPointY;
	static float tmpCtrlPointZ;

	//starting position
	static float posX =  -0.92f;
	static float posY =  -0.72f;
	static float posZ =  0.20f;

	static float lookX = 0.05f;
	static float lookY =-0.32f;
	static float lookZ = 0.15f;
	//std::cout << std::endl  << std::endl << posX << " " << lookX << std::endl;
	//std::cout<< posY << " " << lookY << std::endl;
	//std::cout<< posZ << " " << lookZ << std::endl;

	//velocity of actions, nothing moves instanteanously
	static float velocityForward = 0;
	static float velocityBackward = 0;
	static float velocityLeft = 0;
	static float velocityRight = 0;
	static float velocityUp = 0;
	static float velocityDown = 0;

	//tmp save rotation done
	static float recordRotZ = 0;
	static float recordRotY = 0;

	//use time to set movement speeds
	/*static double lastTime = glfwGetTime();
    double currentTime = glfwGetTime();
    float deltaT = float(currentTime - lastTime); //deltaT in sc
    lastTime = currentTime;*/

    if ((KeyW) | (velocityForward > 0.0f)){//W pressed => go forward
		if((KeyW==1) & (velocityForward<straightMaxSpeed*deltaT)){
				velocityForward = velocityForward + straightAcceleration * deltaT ;
		}
		else{
			velocityForward = velocityForward - straightAcceleration*deltaT;
		}

		//Flying exploration
		moveAlongAxis(posX,posY,posZ,lookX,lookY,lookZ,velocityForward);

	}
    if  ((KeyS)| (velocityBackward > 0.0f)){//S pressed => go backward
        if((KeyS) & (velocityBackward<straightMaxSpeed*deltaT)){
				velocityBackward = velocityBackward + straightAcceleration*deltaT;
		}
		else{
			velocityBackward = velocityBackward - straightAcceleration*deltaT;
		}
		//flying exploration
		moveAlongAxis(posX,posY,posZ,lookX,lookY,lookZ,-velocityBackward);

	}
    if ((KeyA) | (velocityLeft > 0.0f)){//A pressed => turn left
        if((KeyA) & (velocityLeft<rotationMaxSpeed*deltaT)){
				velocityLeft = velocityLeft + rotationAcceleration*deltaT;
		}
		else{
			velocityLeft = velocityLeft - rotationAcceleration*deltaT;
		}
	//	rotateLeftRight(posX,posY,posZ,lookX,lookY,lookZ,recordRotY,recordRotZ,velocityLeft);
		rotateLeftRight(posX,posY,posZ,lookX,lookY,lookZ,recordRotY,velocityLeft);

		//rotateLeftRight(posX,posY,posZ,lookX,lookY,lookZ,recordRotY,velocityLeft);
		recordRotZ = fmod((recordRotZ+OneRad*velocityLeft),float(2*M_PI)); //save rotation done
	}
    if ((KeyD) | (velocityRight > 0.0f)){//D pressed =turn right
        if((KeyD) & (velocityRight<rotationMaxSpeed*deltaT)){
				velocityRight = velocityRight + rotationAcceleration*deltaT;
		}
		else{
			velocityRight = velocityRight - rotationAcceleration*deltaT;
		}
	//	rotateLeftRight(posX,posY,posZ,lookX,lookY,lookZ,recordRotY,recordRotZ,-velocityRight);
		rotateLeftRight(posX,posY,posZ,lookX,lookY,lookZ,recordRotY,-velocityRight);
		//rotateLeftRight(posX,posY,posZ,lookX,lookY,lookZ,recordRotY,-velocityRight);
		recordRotZ = fmod((recordRotZ-OneRad*velocityRight),float(2*M_PI)); //save rotation done
	}
    if  ((KeyQ)  | (velocityUp > 0.0f)){//Q pressed turn up
        if((KeyQ) & (velocityUp<rotationMaxSpeed*deltaT)){
				velocityUp = velocityUp + rotationAcceleration*deltaT;
		}
		else{
			velocityUp = velocityUp - rotationAcceleration*deltaT;
		}
		//rotateUpDown(posX,posY,posZ,lookX,lookY,lookZ,recordRotZ,velocityUp);
	//	rotateUpDown(posX,posY,posZ,lookX,lookY,lookZ,recordRotY,recordRotZ,velocityUp);
		rotateUpDown(posX,posY,posZ,lookX,lookY,lookZ,recordRotZ,velocityUp);

		recordRotY = fmod((recordRotY+OneRad*velocityUp),float(2*M_PI)); //save rotation done
	}
    if  ( (KeyE) | (velocityDown > 0.0f)){//E pressed => down
        if((KeyE) & (velocityDown<rotationMaxSpeed*deltaT)){
				velocityDown = velocityDown + rotationAcceleration*deltaT;
		}
		else{
			velocityDown = velocityDown - rotationAcceleration*deltaT;
		}
		//rotateUpDown(posX,posY,posZ,lookX,lookY,lookZ,recordRotZ,-velocityDown);
	//	rotateUpDown(posX,posY,posZ,lookX,lookY,lookZ,recordRotY,recordRotZ,-velocityDown);
		rotateUpDown(posX,posY,posZ,lookX,lookY,lookZ,recordRotZ,-velocityDown);
		recordRotY = fmod((recordRotY-OneRad*velocityDown),float(2*M_PI)); //save rotation done
	}
    if(Key1){
		update_camera_modelview(posX,posY,posZ,lookX,lookY,lookZ);
	}
    if((KeyENTER) & (ModeSettingControlPoint==false)){
		KeyENTER=false;
		ModeSettingControlPoint=true;
		/*if(controlPointsCount<=4){//restart creating path
			controlPointsCount=0;
            _cameraPathControls.clear();
		}*/
		if (controlPointsCount%4 == 0){
			if(controlPointsCount>2){// User continue to creat bcurve => push the last controlP of previous Bcurve P0 and the computed control P1
                _cameraPathControls.push_back(_cameraPathControls.at(_cameraPathControls.size()-3));
                _cameraPathControls.push_back(_cameraPathControls.at(_cameraPathControls.size()-3));
                _cameraPathControls.push_back(_cameraPathControls.at(_cameraPathControls.size()-3));
                _cameraPathControls.push_back(tmpCtrlPointX);
                _cameraPathControls.push_back(tmpCtrlPointY);
                _cameraPathControls.push_back(tmpCtrlPointZ);
				controlPointsCount=controlPointsCount+2;
			}
			else{ // User define first control point
				std::cout<<"Control point 1 of bezier curve settled"<<std::endl;
                _cameraPathControls.push_back(posX-0.02f);
                _cameraPathControls.push_back(posY-0.02f);
                _cameraPathControls.push_back(posZ-0.02f);
				controlPointsCount++;
			}
		}
	}
    if (ModeSettingControlPoint){ // set a control point if necessary
		//use look at to define the 2nd control point (P1) only for first Bcurve
        if ((controlPointsCount%4==1) & (_cameraPathControls.size()%12==3) & (controlPointsCount<3) ){
            _cameraPathControls.push_back(lookX);
            _cameraPathControls.push_back(lookY);
            _cameraPathControls.push_back(lookZ);
		}//modify and refresh the look at to define the 2nd control point (P1) only for first Bcurve
        else if((controlPointsCount%4==1) & (_cameraPathControls.size()%12==6) & (controlPointsCount<3)){
            _cameraPathControls.pop_back();
            _cameraPathControls.pop_back();
            _cameraPathControls.pop_back();
            _cameraPathControls.push_back(lookX);
            _cameraPathControls.push_back(lookY);
            _cameraPathControls.push_back(lookZ);
		}//set P2 and P3 using pos for every Bcurve created
        else if((controlPointsCount%4 ==2)  & (_cameraPathControls.size()%12==6)){
            _cameraPathControls.push_back(lookX);
            _cameraPathControls.push_back(lookY);
            _cameraPathControls.push_back(lookZ);
            _cameraPathControls.push_back(posX-0.02f);
            _cameraPathControls.push_back(posY-0.02f);
            _cameraPathControls.push_back(posZ-0.02f);
		}//refresh P2 and P3 using pos for every Bcurve created
        else if((controlPointsCount%4==2) & (_cameraPathControls.size()%12==0)){
            _cameraPathControls.pop_back();
            _cameraPathControls.pop_back();
            _cameraPathControls.pop_back();
            _cameraPathControls.pop_back();
            _cameraPathControls.pop_back();
            _cameraPathControls.pop_back();
            _cameraPathControls.push_back(lookX);
            _cameraPathControls.push_back(lookY);
            _cameraPathControls.push_back(lookZ);
            _cameraPathControls.push_back(posX-0.02f);
            _cameraPathControls.push_back(posY-0.02f);
            _cameraPathControls.push_back(posZ-0.02f);
		}
        if(_cameraPathControls.size()>6){//compute and render bezier
			createBCurve();
		}
        if(KeyENTER){//finish setting ctrl points
			if(controlPointsCount%4==1){
				controlPointsCount++;
			}
			else if(controlPointsCount%4==2){ // a bcurve is completly settled
				controlPointsCount = controlPointsCount+2;
				ModeSettingControlPoint=false;
                std::cout<<int(_cameraPathControls.size()/12)<<" Bezier curves has been created!"<<std::endl;
			}
			if((controlPointsCount%4==0) & (controlPointsCount>2)){ // compute control P1 for next Bcurve, respecting linearity
                tmpCtrlPointX = 2*_cameraPathControls.at(_cameraPathControls.size()-3) - _cameraPathControls.at(_cameraPathControls.size()-6);
                tmpCtrlPointY = 2*_cameraPathControls.at(_cameraPathControls.size()-2) - _cameraPathControls.at(_cameraPathControls.size()-5);
                tmpCtrlPointZ = 2*_cameraPathControls.at(_cameraPathControls.size()-1) - _cameraPathControls.at(_cameraPathControls.size()-4);
			}
			KeyENTER=false;
		}
	}
}

void CameraControl::fpsExploration(){

	static float straightMaxSpeed = 0.5f;
	static float rotationMaxSpeed = 60.0f;
	static float straightAcceleration = 0.05f;
	static float rotationAcceleration = 4.0f;

	static float posX = -0.73f;
	static float posY =  0.51f;
	static float posZ =  0.08f;

	static float lookX = 0.19f;
	static float lookY = 0.04f;
	static float lookZ = 0.01f;

	//std::cout << std::endl  << std::endl << posX << " " << lookX << std::endl;
	//std::cout<< posY << " " << lookY << std::endl;
	//std::cout<< posZ << " " << lookZ << std::endl;

	static float velocityForward = 0;
	static float velocityBackward = 0;
	static float velocityLeft = 0;
	static float velocityRight = 0;
	static float velocityUp = 0;
	static float velocityDown = 0;

	static float recordRotZ = 0;

	static bool jumping = false;
	static float initJumpPosZ = 0;
	static float initJumpLookZ = 0;
	static float jumpLevel =0;

	//static double lastTime = glfwGetTime();
    //double currentTime = glfwGetTime();
    //float deltaT = float(currentTime - lastTime); //deltaT in sc
    //lastTime = currentTime;

    if((KeyX) & (jumping==false)){//x => jump
			jumping = true;
			initJumpPosZ = posZ;
			initJumpLookZ = lookZ;
	}
    if (jumping){

		if((jumpLevel>80) & (jumpLevel<100)){
			jumpLevel +=80*deltaT;
		}
		else if (((jumpLevel>60) & (jumpLevel<=80))|((jumpLevel>=100) & (jumpLevel<120))){
			jumpLevel +=130*deltaT;
		}
		else{
			jumpLevel +=230*deltaT;
		}

		//estimate floor level 8only if mooving while jumping
		int tmpX = int((1+posX)*1024/2);
		int tmpY = int((1+posY)*1024/2);
		tmpX = tmpX -2;
		tmpY = tmpY -2;
		float tmpZ=0;
		float AmountOfPoints = 0;
		for(int i=0;i<5;i++){//take in count 24 z values arround to have a smooth move
			for(int j=0;j<5;j++){
				tmpX = tmpX + i;
				tmpY = tmpY + j;
				if ((tmpX>0) & (tmpX<1025) & (tmpY>0) &(tmpY<1025)){
					tmpZ = tmpZ + _heightmapCPU[tmpX+1024*tmpY];
					AmountOfPoints++;
				}
			}
		}
		//std::cout<<"AmountOfPoints ="<<AmountOfPoints<<endl;
		tmpZ=tmpZ/AmountOfPoints;
		float floorLevel = tmpZ+float(0.06); //fps person height

		if ((jumpLevel >= 90) & (posZ<=floorLevel)){ //reach floor =>jump finished
			jumping = false;

			jumpLevel = 0;
			posZ = floorLevel;// 0.06 person height
		}
		else if(jumpLevel > 180 ){// falling !
			posZ -= 0.3f*deltaT;
		}
		else{
			posZ = initJumpPosZ + float(0.1) * sin(OneRad*jumpLevel);
			lookZ = initJumpLookZ + float(0.1) * sin(OneRad*jumpLevel);
		}

		update_camera_modelview(posX,posY,posZ,lookX,lookY,lookZ);
	}
    if ((KeyW) | (velocityForward > 0.0f)){//W pressed => go forward
		if(((KeyW==1) & (velocityForward<straightMaxSpeed*deltaT)) & (jumping ==false)){
				velocityForward += straightAcceleration*deltaT;
		}
		else if (jumping ==false){
			velocityForward -= straightAcceleration*deltaT;
		}

        if(((KeySHIFT) & (KeyW) & (velocityForward<2*straightMaxSpeed*deltaT)) & (jumping ==false)){ // press shift => running
			velocityForward +=3*straightAcceleration*deltaT;
		}

		//FPS exploration
		if((posX<=1) & (posX>=-1) & (posY<=1) & (posY>=-1) & (jumping == false)){//stay on the map
			float dispX = 0.2f*velocityForward*powf(posX-lookX,2)/(powf(posX-lookX,2)+powf(posY-lookY,2))*(posX-lookX)/abs(posX-lookX);
			float dispY = 0.2f*velocityForward*powf(posY-lookY,2)/(powf(posX-lookX,2)+powf(posY-lookY,2))*(posY-lookY)/abs(posY-lookY);
			fpsExplorationForwardBackward(posX,posY,posZ,lookX,lookY,lookZ,dispX,dispY);
		}
		else if((posX<=1) & (posX>=-1) & (posY<=1) & (posY>=-1) & (jumping == true)){//stay on the map jumping forward
			float dispX = 0.2f*velocityForward*powf(posX-lookX,2)/(powf(posX-lookX,2)+powf(posY-lookY,2))*(posX-lookX)/abs(posX-lookX);
			float dispY = 0.2f*velocityForward*powf(posY-lookY,2)/(powf(posX-lookX,2)+powf(posY-lookY,2))*(posY-lookY)/abs(posY-lookY);
			posX = posX - dispX;
			posY = posY - dispY;
			lookX = lookX - dispX;
			lookY = lookY - dispY;
			if(posX>1){posX=1;}
			else if(posX<-1){posX=-1;}
			if(posY>1){posY=1;}
			else if(posY<-1){posY=-1;}
			update_camera_modelview(posX,posY,posZ,lookX,lookY,lookZ);
		}

	}
    if  ((KeyS)| (velocityBackward > 0.0f)){//S pressed => go backward
        if((KeyS) & (velocityBackward<straightMaxSpeed*deltaT) & (jumping ==false)){
				velocityBackward = velocityBackward + straightAcceleration*deltaT;
		}
		else if (jumping ==false){
			velocityBackward = velocityBackward - straightAcceleration*deltaT;
		}

		//fps exploration
		if((posX<=1) & (posX>=-1) & (posY<=1) & (posY>=-1) & (jumping == false)){//stay on the map
			float dispX = 0.2f*velocityBackward*powf(posX-lookX,2)/(powf(posX-lookX,2)+powf(posY-lookY,2))*(posX-lookX)/abs(posX-lookX);
			float dispY = 0.2f*velocityBackward*powf(posY-lookY,2)/(powf(posX-lookX,2)+powf(posY-lookY,2))*(posY-lookY)/abs(posY-lookY);
			fpsExplorationForwardBackward(posX,posY,posZ,lookX,lookY,lookZ,-dispX,-dispY);
		}
		else if((posX<=1) & (posX>=-1) & (posY<=1) & (posY>=-1) & (jumping == true)){//stay on the map jumping forward
			float dispX = 0.2f*velocityBackward*powf(posX-lookX,2)/(powf(posX-lookX,2)+powf(posY-lookY,2))*(posX-lookX)/abs(posX-lookX);
			float dispY = 0.2f*velocityBackward*powf(posY-lookY,2)/(powf(posX-lookX,2)+powf(posY-lookY,2))*(posY-lookY)/abs(posY-lookY);
			posX = posX - dispX;
			posY = posY - dispY;
			lookX = lookX - dispX;
			lookY = lookY - dispY;
			if(posX>1){posX=1;}
			else if(posX<-1){posX=-1;}
			if(posY>1){posY=1;}
			else if(posY<-1){posY=-1;}
			update_camera_modelview(posX,posY,posZ,lookX,lookY,lookZ);
		}

	}
    if ((KeyA) | (velocityLeft > 0.0f)){//A pressed => turn left
        if((KeyA) & (velocityLeft<rotationMaxSpeed*deltaT)  & (jumping ==false)){
				velocityLeft = velocityLeft + rotationAcceleration*deltaT;
		}
		else if (jumping ==false){
			velocityLeft = velocityLeft - rotationAcceleration*deltaT;
		}
		fpsRotateLeftRight(posX,posY,posZ,lookX,lookY,lookZ,velocityLeft);
		recordRotZ = fmod((recordRotZ+OneRad*velocityRight),float(2*M_PI)); //save rotation done

		//rotateLeftRight(posX,posY,posZ,lookX,lookY,lookZ,recordRotY,velocityLeft);
	}
    if ((KeyD) | (velocityRight > 0.0f)){//D pressed =turn right
        if((KeyD) & (velocityRight<rotationMaxSpeed*deltaT) & (jumping ==false)){
				velocityRight = velocityRight + rotationAcceleration*deltaT;
		}
		else if (jumping ==false){
			velocityRight = velocityRight - rotationAcceleration*deltaT;
		}
		fpsRotateLeftRight(posX,posY,posZ,lookX,lookY,lookZ,-velocityRight);
		recordRotZ = fmod((recordRotZ-OneRad*velocityRight),float(2*M_PI)); //save rotation done

		//rotateLeftRight(posX,posY,posZ,lookX,lookY,lookZ,recordRotY,-velocityRight);
	}
    if  ((KeyQ)  | (velocityUp > 0.0f)){//Q pressed turn up
        if((KeyQ) & (velocityUp<rotationMaxSpeed*deltaT) & (jumping ==false)){
				velocityUp = velocityUp + rotationAcceleration*deltaT;
		}
		else if (jumping ==false){
			velocityUp = velocityUp - rotationAcceleration*deltaT;
		}
		//rotateUpDown(posX,posY,posZ,lookX,lookY,lookZ,recordRotZ,velocityUp);
		fpsRotateUpDown(posX,posY,posZ,lookX,lookY,lookZ,recordRotZ,velocityUp);
	}
    if  ( (KeyE) | (velocityDown > 0.0f)){//E pressed => down
        if((KeyE) & (velocityDown<rotationMaxSpeed*deltaT) & (jumping ==false)){
				velocityDown = velocityDown + rotationAcceleration*deltaT;
		}
		else if (jumping ==false){
			velocityDown = velocityDown - rotationAcceleration*deltaT;
		}
		//rotateUpDown(posX,posY,posZ,lookX,lookY,lookZ,recordRotZ,-velocityDown);
		fpsRotateUpDown(posX,posY,posZ,lookX,lookY,lookZ,recordRotZ,-velocityDown);
	}
    if(Key2){
		update_camera_modelview(posX,posY,posZ,lookX,lookY,lookZ);
	}
}

void CameraControl::updateCameraPosition(mat4 views[], mat4& cameraPictorialModel, int& selectedControlPoint) {

	static double lastTime = glfwGetTime();
	double currentTime = glfwGetTime();
	deltaT = float(currentTime - lastTime); //deltaT in sc
	lastTime = currentTime;

    /// Modify camera position according to the exploration mode.
    switch(_explorationMode) {
    case FLYING:
        flyingExploration();
        break;
    case FPS:
        fpsExploration();
        break;
    //case PATH:
    //    deCasteljauTest4Points();
    //    break;
    default:
        /// Nothing to do : trackball() will update the transformation matrix.
        break;
    }

    if(flagAnimatePictorialCamera)
        animatePictorialCamera(0);

    /// Update the view transformation matrix.
    views[0] = _controllerModelview;
    views[1] = _cameraModelview;

    /// Update the camera pictorial model transformation matrix.
    cameraPictorialModel = _cameraPictorialModel;

    /// Update the selected control point.
    selectedControlPoint = _selectedControlPoint;

}

void CameraControl::handleKeyboard(int key, int action){
	//std::cout<<key<<"  "<<action<<std::endl;

	switch(key){
	case 87 :
		KeyW= !KeyW;
		break;
	case 65 :
		KeyA = !KeyA;
		break;
	case 83 :
		KeyS = !KeyS;
		break;
	case 68 :
		KeyD = !KeyD;
		break;
	case 69 :
		KeyE = !KeyE;
		break;
	case 81 :
		KeyQ = !KeyQ;
		break;
	case 88 :
		KeyX = !KeyX;
		break;
	case 287 :
		KeySHIFT = !KeySHIFT;
		break;
	case 32 :
		KeySPACE = !KeySPACE;
		break;
	case 303: //1
        std::cout << "Exploration mode : FLYING" << std::endl;
		Key1 =!Key1;
        _explorationMode = FLYING;
        flagAnimatePictorialCamera = false;
		break;
    case 304: //2
        std::cout << "Exploration mode : FPS" << std::endl;
        _explorationMode = FPS;
        flagAnimatePictorialCamera = false;
		Key2 =!Key2;
		break;
    case 306: //4
        std::cout << "Exploration mode : FIX" << std::endl;
        _explorationMode = FIX;
        flagAnimatePictorialCamera = false;
    }
	if(action==1){
		switch(key){
			 case 305: //3
				std::cout << "Exploration mode : PATH" << std::endl;
				//_explorationMode = PATH;
				flagAnimatePictorialCamera = !flagAnimatePictorialCamera;
				break;
			case 90: //Z=> change control point under modification
                _selectedControlPoint++;
                if(_selectedControlPoint> 3*int((_cameraPathControls.size()-3)/9))
                    _selectedControlPoint=0;
                std::cout<<"Changing control point nB"<<_selectedControlPoint<<std::endl;
				std::cout<<"Control list :"<<std::endl<<"Key Z : change point to set"<<std::endl;
				std::cout<<"Key U : position X + 0.1"<<std::endl;
				std::cout<<"Key J : position X - 0.1"<<std::endl;
				std::cout<<"Key I : position Y + 0.1"<<std::endl;
				std::cout<<"Key K : position Y - 0.1"<<std::endl;
				std::cout<<"Key O : position Z + 0.1"<<std::endl;
				std::cout<<"Key L : position Z - 0.1"<<std::endl;
				std::cout<<"Key + : add bezier curve"<<std::endl;

				break;
			case 85: //U=> X+
                //bezier_4_points(_selectedControlPoint,0.1f,0.0f,0.0f);
				N_MultipleBezier_controlled(_selectedControlPoint,0.1f,0.0f,0.0f);
				break;
			case 74: //J=> X-
                //bezier_4_points(_selectedControlPoint,-0.1f,0.0f,0.0f);
				N_MultipleBezier_controlled(_selectedControlPoint,-0.1f,0.0f,0.0f);
				break;
			case 73: //I=> Y+
                //bezier_4_points(_selectedControlPoint,0.0f,0.1f,0.0f);
				N_MultipleBezier_controlled(_selectedControlPoint,0.0f,0.1f,0.0f);
				break;
			case 75: //K=> Y-
                //bezier_4_points(_selectedControlPoint,0.0f,-0.1f,0.0f);
				N_MultipleBezier_controlled(_selectedControlPoint,0.0f,-0.1f,0.0f);
				break;
			case 79: //O=> Z+
                //bezier_4_points(_selectedControlPoint,0.0f,0.0f,0.1f);
				N_MultipleBezier_controlled(_selectedControlPoint,0.0f,0.0f,0.1f);
				break;
			case 76: //L=> Z-
                //bezier_4_points(_selectedControlPoint,0.0f,0.0f,-0.1f);
				N_MultipleBezier_controlled(_selectedControlPoint,0.0f,0.0f,-0.1f);
				break;
			case 307: //5
				InitSubdivision();
				break;
			case 308: //6
				bezier_4_points(0,0,0,0);
				break;
			case 309:
				animatePictorialCamera2(0); //test purpose
				break;
//			case 294://ENTER => drop control point
//				if(_explorationMode == FLYING){
//					KeyENTER = true;
//				}
//				break;
            case 294://+
				Add_Bcurve();
				break;
			case 314://-
				if(_cameraPathControls.size()>12){
					if(((_cameraPathControls.size()/3)-4)<_selectedControlPoint){
						_selectedControlPoint=0;
					}
					Remove_Bcurve();

				}
				break;
		}
	}
}

max_stars_repo_name: mdeff/terrain

max_issues_repo_name: mdeff/terrain

max_forks_repo_name: mdeff/terrain


text:
#include <math.h>
#include <vector>
#include <limits>
using namespace std;

#include <armadillo>
using namespace arma;

#include "optimize_state_reconstructor.h"
#include "rate_model.h"
#include "state_reconstructor.h"

#include <gsl/gsl_multimin.h>
#include <gsl/gsl_vector.h>

OptimizeStateReconstructor::OptimizeStateReconstructor(RateModel * _rm,StateReconstructor * _sr, mat * _free_mask, int _nfree):
    rm(_rm),sr(_sr),free_variables(_free_mask),nfree_variables(_nfree),maxiterations(10000),stoppingprecision(0.001) {}

double OptimizeStateReconstructor::GetLikelihoodWithOptimized(const gsl_vector * variables) {
    for (unsigned int i=0; i < free_variables->n_rows; i++) {
        for (unsigned int j=0; j < free_variables->n_cols; j++) {
            if (i != j) {
                rm->set_Q_cell(i,j,gsl_vector_get(variables,(*free_variables)(i, j)));
                if (rm->get_Q()(i, j) < 0 || rm->get_Q()(i, j) >= 1000) {
                    return 100000000;
                }
            }
        }
    }
    double like;
    rm->set_Q_diag();
    like = sr->eval_likelihood();
    //cout << like << endl;
    if (like < 0 || like == std::numeric_limits<double>::infinity()) {
        like = 100000000;
    }
    return like;
}

double OptimizeStateReconstructor::GetLikelihoodWithOptimized_gsl(const gsl_vector * variables, void *obj) {
    double temp;
    temp= ((OptimizeStateReconstructor*)obj)->GetLikelihoodWithOptimized(variables);
    return temp;
}

/*
 * USES THE SIMPLEX ALGORITHM
 *
 */
mat OptimizeStateReconstructor::optimize() {
    const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex2;
    gsl_multimin_fminimizer *s = NULL;
    gsl_vector *ss, *x;
    size_t np = nfree_variables;
    size_t iter = 0, i;
    int status;
    double size;
    /* Initial vertex size vector */
    ss = gsl_vector_alloc (np);
    /* Set all step sizes to .01 */ //Note that it was originally 1
    gsl_vector_set_all (ss, .2);
    /* Starting point */
    //cout<<"Now in OPtimizaRateWithGivenTipVariance in OptimizationFn"<<endl;
    x = gsl_vector_alloc (np);
    for (unsigned int i=0; i < np; i++) {
        gsl_vector_set (x, i, 0.1);
    }
    OptimizeStateReconstructor *pt;
    pt=(this);
    double (*F)(const gsl_vector *, void *);
    F = &OptimizeStateReconstructor::GetLikelihoodWithOptimized_gsl;
    /* Initialize method and iterate */
    gsl_multimin_function minex_func;
    minex_func.f =* F;
    minex_func.params = pt;
    minex_func.n = np;
    s = gsl_multimin_fminimizer_alloc (T, np);
    gsl_multimin_fminimizer_set (s, &minex_func, x, ss);
    do {
        //cout<<"Now on iteration "<<iter<<endl;
        iter++;
        status = gsl_multimin_fminimizer_iterate(s);
        if (status != 0) { //0 Means it's a success
        //    printf ("error: %s\n", gsl_strerror (status));
            break;
        }
        size = gsl_multimin_fminimizer_size (s);
        //status = gsl_multimin_test_size (size, 1e-2);
        status = gsl_multimin_test_size (size, stoppingprecision); //since we want more precision
        if (status == GSL_SUCCESS) {
        //    printf ("converged to minimum at\n");
        }
        //printf ("%5d ", iter);
        for (i = 0; i < np; i++) {
        //    printf ("%10.3e ", gsl_vector_get (s->x, i));
        }
        //printf ("f() = %7.3f size = %.3f\n", s->fval, size);
    }
    while (status == GSL_CONTINUE && iter < maxiterations);
    mat results (free_variables->n_rows, free_variables->n_cols); results.fill(0);
    for (unsigned int i=0; i < results.n_rows; i++) {
        for (unsigned int j=0; j < results.n_cols; j++) {
            if (i != j) {
                results(i, j) = (gsl_vector_get(s->x, (*free_variables)(i, j)));
            }
        }
    }
    gsl_vector_free(x);
    gsl_vector_free(ss);
    gsl_multimin_fminimizer_free (s);
    return results;
}


text:
// Distributed under the BSD License, see accompanying LICENSE.txt
// (C) Copyright 2010 John-John Tedro et al.
#include "image_base.hpp"

#include <cstring>

#include <boost/numeric/conversion/cast.hpp>

// image_base
void image_base::fill(color &q)
{
  for (pos_t x = 0; x < get_width(); x++) {
    for (pos_t y = 0; y < get_height(); y++) {
      set_pixel(x, y, q);
    }
  }
}

void image_base::clear() {
  boost::shared_array<color> line(new color[get_width()]);
  ::memset(line.get(), 0x0, get_width() * sizeof(color));

  for (pos_t y = 0; y < get_height(); y++)
    set_line(y, 0, get_width(), line.get());
}

void image_base::composite(int x, int y, image_operations_ptr opers)
{
  std::vector<image_operation>::size_type i = 0;

  align(x, y, opers->max_x, opers->max_y);

  while (i++ < opers->operations.size())
  {
    image_operation op = opers->operations[i];
    blend_pixel(x + op.x, y + op.y, op.c);
  }
}

void image_base::draw_line(pos_t x1, pos_t y1, pos_t x2, pos_t y2, color &c)
{
    int sx, sy;
    int dx = (x1<x2) ? x2-x1 : x1-x2;
    int dy = (y1<y2) ? y2-y1 : y1-y2;
    sx = (x1<x2) ? 1 : -1 ;
    sy = (y1<y2) ? 1 : -1 ;
    int err = dx-dy;

    do
    {
        this->set_pixel(x1, y1, c);
        int e2 = 2*err;
        if(e2 > -dy)
        {
            err -= dy;
            x1 += sx;
        }
        if(e2 < dx)
        {
            err += dx;
            y1 += sy;
        }
    } while(x1 != x2 || y1 != y2);
}

void image_base::safe_blend_pixel(pos_t x, pos_t y, color &c)
{
  if (x >= width) return;
  if (y >= height) return;
  blend_pixel(x, y, c);
}

void image_base::resize(image_ptr target) {
  if (target->get_width() > get_width()) {
    // scale up
    unsigned int factor = target->get_width() / get_width();
    boost::shared_array<color> line(new color[get_width()]);

    unsigned int scanline = 0;
    bool first = false;

    for (pos_t y = 0; y < target->get_height(); y++) {
      pos_t y_p = (y / factor);

      if (y_p != scanline || !first) {
        get_line(y_p, pos_t(0), get_width(), line.get());
        first = true;
        scanline = y_p;
      }

      for (pos_t x = 0; x < target->get_width(); x++) {
        pos_t x_p = (x / factor);
        target->set_pixel(x, y, line[x_p]);
      }
    }
  }
  else {
    // scale down
    unsigned int factor = get_width() / target->get_width();

    boost::shared_array<color> line(new color[get_width()]);

    for (pos_t y = 0; y < target->get_height(); y++) {
      pos_t y_p = ((y * factor) + factor / 2);
      get_line(y_p, pos_t(0), get_width(), line.get());

      for (pos_t x = 0; x < target->get_width(); x++) {
        pos_t x_p = ((x * factor) + factor / 2);
        target->set_pixel(x, y, line[x_p]);
      }
    }
  }
}


text:
//
// Copyright © 2019 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "PeriodicCounterSelectionCommandHandler.hpp"
#include "ProfilingUtils.hpp"

#include <boost/numeric/conversion/cast.hpp>

namespace armnn
{

namespace profiling
{

using namespace std;
using boost::numeric_cast;

void PeriodicCounterSelectionCommandHandler::ParseData(const Packet& packet, CaptureData& captureData)
{
    std::vector<uint16_t> counterIds;
    uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
    uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
    uint32_t offset = 0;

    if (packet.GetLength() > 0)
    {
        if (packet.GetLength() >= 4)
        {
            captureData.SetCapturePeriod(ReadUint32(reinterpret_cast<const unsigned char*>(packet.GetData()), offset));

            unsigned int counters = (packet.GetLength() - 4) / 2;

            if (counters > 0)
            {
                counterIds.reserve(counters);
                offset += sizeOfUint32;
                for(unsigned int pos = 0; pos < counters; ++pos)
                {
                    counterIds.emplace_back(ReadUint16(reinterpret_cast<const unsigned char*>(packet.GetData()),
                                            offset));
                    offset += sizeOfUint16;
                }
            }

            captureData.SetCounterIds(counterIds);
        }
    }
}

void PeriodicCounterSelectionCommandHandler::operator()(const Packet& packet)
{
    CaptureData captureData;

    ParseData(packet, captureData);

    vector<uint16_t> counterIds = captureData.GetCounterIds();

    m_CaptureDataHolder.SetCaptureData(captureData.GetCapturePeriod(), counterIds);

    m_CaptureThread.Start();

    // Write packet to Counter Stream Buffer
    m_SendCounterPacket.SendPeriodicCounterSelectionPacket(captureData.GetCapturePeriod(), captureData.GetCounterIds());
}

} // namespace profiling

} // namespace armnn

max_stars_repo_name: tom-gall/armnn

max_issues_repo_name: tom-gall/armnn

max_forks_repo_name: tom-gall/armnn