int main() {
arma::mat rawdata;
rawdata.load(data.c_str());
arma::mat X;
X=rawdata(arma::span(0, numExp-1), arma::span(0, numFeat-1));
int m = numExp;
int n = numFeat;
arma::mat mu(nCl,n);
mu.randu();

//bring inital mu into correct range
for (int i=0; i<n; i++) {
  mu.col(i) = X.col(i).min() + (X.col(i).max()-X.col(i).min())*mu.col(i);
}

arma::mat cinit(nCl,1);
cinit.zeros();
arma::mat cloop;
arma::mat c(m,1);  //vector which assign each example to a cluster
int numC;
for (int k=0; k<numIter; k++) {
  //E-Step:
  c.zeros();
  for (int i=0; i<m; i++) {
    cloop = cinit;
    for (int j=0; j<nCl; j++) {
      cloop.row(j) = (X.row(i)-mu.row(j))*trans(X.row(i)-mu.row(j));
    }
    c.row(i) = cloop.index_min();
  }
  //M-Step:
  mu.zeros();
  for (int j=0; j<nCl; j++) {
    numC=0;
    for (int i=0; i<m; i++) {
      if (c(i,0)==j) {
        numC += 1;
        mu.row(j) += X.row(i);
      }
    }
    if(numC==0) {mu.row(j).zeros();} //no training examples were assigned to this cluster (maybe too many clusters?)
    mu.row(j) = mu.row(j)/numC;
  }
}

  //output data files to be used for plotting
  std::ofstream of_X("X_kMeans.out");
  std::ofstream of_c("c_kMeans.out");
  std::ofstream of_mu("mu_kMeans.out");
  for (int i=0;i<X.n_rows;i++) {
    of_X << X.row(i) << std::endl;
    of_c << c.row(i) << std::endl;
  }
  for (int i=0;i<mu.n_rows;i++) {
    of_mu << mu.row(i) << std::endl;
  }

}
void ObjectEditorWidget::saveCurrent()
{
	if( !noObjectSelected->isVisible() )
	{
		bool needsRefresh = false;
		QString nam = nameEditor->text();
		std::string nameOfObject = nam.toStdString();
		if( nam.compare( originalName ) != 0 )
		{
			remove( ( "Assets/objs/" + originalName + ".rpobj" ).toStdString().c_str() );
			needsRefresh = true;
		}

		std::ofstream of_c(	"Assets/objs/" + nameOfObject + ".rpobj", std::ios_base::binary);
		std::cout << "Saving" << std::endl;
	
		//of_c << nameOfObject;
		int count = outlets->rowCount();
		of_c.write( reinterpret_cast<char*>(&count), sizeof(count) );
		for( int i = 0; i < outlets->rowCount(); i++ )
		{
			std::string outName = outlets->getName( i ).toStdString();
			int outNameLength = outName.length();
			of_c.write( reinterpret_cast<char*>(&outNameLength), sizeof(outNameLength) );
			of_c << outName;

			float num = outlets->getValue( i );
			of_c.write( reinterpret_cast<char*>( &num ), sizeof(num) );
		}

		for( int i = 0; i < codeEditTabs->count(); i++ )
		{
			bool enabled = codeEditTabs->widget( i )->findChild<QCheckBox*>()->isChecked();
			of_c.write( reinterpret_cast<char*>(&enabled), sizeof(enabled) );

			std::string code = codeEditTabs->widget( i )->findChild<QTextEdit*>()->toPlainText().toStdString();
			int codeLength = code.length();

			of_c.write( reinterpret_cast<char*>(&codeLength), sizeof(codeLength) );
			of_c << code;
		}

		of_c.close();
	
		originalName = nam;
		hasChanged = false;
		if( needsRefresh )
		{
			objectList->refreshList();
			objectList->seekObject( nam );
		}

	}
}
Exemple #3
0
void SceneModel::save( std::string path )
{
	std::ofstream of_c( path, std::ios_base::binary);
	
	int count = structures.size();
	of_c.write( reinterpret_cast<char*>(&count), sizeof(count) );
	for( int i = 0; i < count; i++ )
	{
		std::string structName = structures[i]->st.name;
		int structNameLength = structName.length();
		of_c.write( reinterpret_cast<char*>(&structNameLength), sizeof(structNameLength) );
		of_c << structName;

		int outletCount = structures[i]->st.numOutlets;
		of_c.write( reinterpret_cast<char*>(&outletCount), sizeof(outletCount) );

		//Outlets
		auto useDefaults = structures[i]->outletList->findChildren<QCheckBox*>();
		auto lineEdits = structures[i]->outletList->findChildren<QLineEdit*>();
		for( int j = 0; j < outletCount; j++ )
		{
			bool useDefault = useDefaults.at( j )->isChecked();
			of_c.write( reinterpret_cast<char*>(&useDefault), sizeof(useDefault) );

			if( !useDefault )
			{
				std::string subscript = lineEdits.at( j )->text().toStdString();
				int subscriptLength = subscript.length();

				of_c.write( reinterpret_cast<char*>(&subscriptLength), sizeof(subscriptLength) );
				of_c << subscript;
			}
		}
	}

	int variableCount = variables.size();
	of_c.write( reinterpret_cast<char*>(&variableCount), sizeof(variableCount) );
	for( int i = 0; i < variableCount; i++ )
	{
		int variableNameLength = variables[i].name.length();
		of_c.write( reinterpret_cast<char*>(&variableNameLength), sizeof(variableNameLength) );
		of_c << variables[i].name;

		int variablePartCount = variables[i].numUsed;
		of_c.write( reinterpret_cast<char*>(&variablePartCount), sizeof(variablePartCount) );
		for( int j = 0; j < variablePartCount; j++ )
		{
			int variablePartNameLength = std::string( variables[i].valueNames[j] ).length();
			of_c.write( reinterpret_cast<char*>(&variablePartNameLength), sizeof(variablePartNameLength) );
			of_c << variables[i].valueNames[j];

			float variablePartDefault = variables[i].defaultValues[j];
			of_c.write( reinterpret_cast<char*>(&variablePartDefault), sizeof(variablePartDefault) );
		}
	}

	for( int i = 0; i < variableCount; i++ )
	{
		int numKeyPoints = variables[i].animation.size();
		of_c.write( reinterpret_cast<char*>(&numKeyPoints), sizeof(numKeyPoints) );

		for( int j = 0; j < numKeyPoints; j++ )
		{
			float time = variables[i].animation[j].time;
			of_c.write( reinterpret_cast<char*>(&time), sizeof(time) );

			for( int k = 0; k < variables[i].numUsed; k++ )
			{
				float val = variables[i].animation[j].values[k];
				of_c.write( reinterpret_cast<char*>(&val), sizeof(val) );
			}
		}
	}


	of_c.close();

	this->path = path;
	this->autoSave = true;
}
Exemple #4
0
void SceneModel::visualize( NewRayMarchWidget * widg, bool propogateErrors )
{
	std::string file;
	ScriptParser * parser = ScriptParser::makeRegular();

	InSceneJointWidget * topJoint = new InSceneJointWidget( structures[0] );
	for( unsigned int structureIndex = 1; structureIndex < structures.size(); structureIndex++ )
	{
		topJoint = new InSceneJointWidget( topJoint, new InSceneJointWidget( structures[structureIndex] ) );
	}

	try
	{
		std::ifstream t("Assets/clparts/clheader.cl");
		std::string header((std::istreambuf_iterator<char>(t)),
						 std::istreambuf_iterator<char>());
		t.close();

		file += header;


		std::string partsString = "";
		std::string sceneCalcString = "";

		//Get the string representing all of the parts as well as their tree structure
		traverseJointRelationships( topJoint, &partsString, &sceneCalcString, parser );
		file += partsString;

		//Create all of the cases for the normal switch statement
		std::string normalCases = "";
		for( unsigned int structureIndex = 0; structureIndex < structures.size(); structureIndex++ )
		{
			normalCases += "\t\tcase " + std::to_string( structureIndex ) + ": result = normalFunction_" + std::to_string( structureIndex ) + "( origin, direction, distTraveled, sceneVars ); break;\n";
		}

		//Draw everything together
		std::ifstream inj("Assets/clparts/clcalcinjection.cl");
		std::string calcInjection = std::string((std::istreambuf_iterator<char>(inj)),
						std::istreambuf_iterator<char>());
		inj.close();
		stringReplaceAll( &calcInjection, "$$DISTCALCINJECTION$$", sceneCalcString );
		stringReplaceAll( &calcInjection, "$$NORMALINJECTION$$", normalCases );
		file += calcInjection;


		//Put the core kernel in
		std::ifstream ft("Assets/clparts/clcombinedfooter.cl");
		std::string footer = std::string((std::istreambuf_iterator<char>(ft)),
						std::istreambuf_iterator<char>());
		ft.close();
		file += footer;

		std::cout << file << std::endl;


		std::ofstream of_c("Assets/temp/testAll.cl", std::ios_base::binary);
		of_c << file;
		of_c.close();
		
		widg->load( "Assets/temp/testAll.cl" );
	}
	catch( char * except )
	{
		if( propogateErrors )
			throw except;
	}
}
ObjectEditorWidget::ObjectEditorWidget()
{
	this->setLayout( new QHBoxLayout() );

	QWidget * objectListContainer = new QWidget;
	objectListContainer->setLayout( new QVBoxLayout );

	objectListContainer->layout()->addWidget( new QLabel( "Objects" ) );

	objectList = new ObjectListWidget();
	objectList->refreshList();

	connect( objectList->selectionModel(), &QItemSelectionModel::selectionChanged, [this](){ this->changingSelection(); } );


	objectListContainer->setFixedWidth( 150 );

	objectListContainer->layout()->addWidget( objectList );

	QPushButton * newObjButt = new QPushButton( "New" );
	connect( newObjButt, &QPushButton::clicked, [this](){
		bool ok;
		QString s = QInputDialog::getText(this, tr("New Object"), tr("Name:"), QLineEdit::Normal, tr( "NewObject" ), &ok);
		if (ok && !s.isEmpty())
		{
			StructureObject obj;
			obj.name = s.toStdString();
			display( obj );
			saveCurrent();
			objectList->refreshList();
			hasChanged = false;
			objectList->seekObject( s );
		}
	} );
	objectListContainer->layout()->addWidget( newObjButt );

	QPushButton * deleteObjButt = new QPushButton( "Delete" );
	connect( deleteObjButt, &QPushButton::clicked, [this](){
		if( objectList->currentRow() != -1 )
		{
			QMessageBox msgBox;
			msgBox.setWindowTitle("Deleting Object");
			msgBox.setText("Are you sure you want to delete the object \"" + objectList->item( objectList->currentRow() )->text() + "\"?");
			msgBox.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
			msgBox.setDefaultButton(QMessageBox::No);
			int result = msgBox.exec();
			if( result == QMessageBox::Yes )
			{
				remove( ( "Assets/objs/" + ( objectList->item( objectList->currentRow() )->text() + ".rpobj" ).toStdString() ).c_str() );
				editingIndex = -1;
				objectList->selectionModel()->reset();
				objectList->refreshList();				
				hide();
			}
		}
	} );
	objectListContainer->layout()->addWidget( deleteObjButt );

	this->layout()->addWidget( objectListContainer ); //Existing objects widget

	noObjectSelected = new QWidget();
	noObjectSelected->setLayout( new QVBoxLayout() );
	noObjectSelected->layout()->setAlignment( Qt::AlignHCenter );
	noObjectSelected->layout()->addWidget( new QLabel( "No object has been selected for editing" ) );
	QFont nf("Tahoma");
	nf.setPixelSize( 24 );
	noObjectSelected->setFont( nf );
	this->layout()->addWidget( noObjectSelected );

	currentObjectContainer = new QSplitter( Qt::Vertical );
	//currentObjectContainer->setLayout( new QVBoxLayout );

	QWidget * currentObjectEditorContainer = new QWidget;
	currentObjectEditorContainer->setLayout( new QVBoxLayout );

	nameEditor = new QLineEdit;
	QFont lf("Tahoma");
	lf.setPixelSize( 24 );
	nameEditor->setFont(lf);
	nameEditor->setMaximumWidth( 320 );
	connect( nameEditor, &QLineEdit::textEdited, [this](){ hasChanged = true; } );
	currentObjectEditorContainer->layout()->addWidget( nameEditor );

	codeEditTabs = new QTabWidget();

	const char * signatures[] = { "subsetRayMarch( x, y, z, out r, out g, out b ) : boolean", "distanceRayMarch( x, y, z, out r, out g, out b ) : number", "rayTrace( x, y, z, dx, dy, dy, out dist, out r, out g, out b ) : boolean" };

	for( int i = 0; i < 3; i++ )
	{
		QWidget * de = new QWidget;
		de->setLayout( new QVBoxLayout );

	
		QCheckBox * check = new QCheckBox;
		check->setChecked( false );
		check->setText( "Enabled" );
		de->layout()->addWidget( check );

	
		de->layout()->addWidget( new QLabel( signatures[i] ) );

		CodeEditWidget * edit = new CodeEditWidget();
		de->layout()->addWidget( edit );
		edit->setEnabled( false );
		connect( edit, &QTextEdit::textChanged, [this](){ hasChanged = true; } );

		connect( check, &QCheckBox::stateChanged, [edit, check](){ edit->setEnabled( check->isChecked() ); } );
		connect( check, &QCheckBox::clicked, [this](){ hasChanged = true; } );

		codeEditTabs->addTab( de, tr("Test"));
	}

	codeEditTabs->setTabText(0, "Subset Ray Marching" );
	codeEditTabs->setTabText(1, "Distance Estimated Ray Marching" );
	codeEditTabs->setTabText(2, "Absolute Ray Tracing" );

	//QObject::connect(
	//connect( check, SIGNAL( stateChanged( int ) ), [edit](int state){ edit->setEnabled( state == 0 ); } );


	currentObjectEditorContainer->layout()->addWidget( codeEditTabs ); 

	QSplitter * horizontalBottomBar = new QSplitter( Qt::Horizontal );
	//horizontalBottomBar->setLayout( new QHBoxLayout );

	currentObjectContainer->addWidget( currentObjectEditorContainer );

	
	horizontalBottomBar->setFixedHeight( 256 );

	QWidget * previewVerticalContainer = new QWidget;
	previewVerticalContainer->setLayout( new QVBoxLayout );

	QLabel * prevLabel = new QLabel( "Preview" );
	prevLabel->setFixedHeight( 24 );
	previewVerticalContainer->layout()->addWidget( prevLabel ); 
	NewRayMarchWidget * march = new NewRayMarchWidget( 256, 256 );
		NewRayMarchCL * under = new NewRayMarchCL();
		under->initialize();
		march->gpu = under;
	
	previewVerticalContainer->layout()->addWidget( march ); //preview
	march->init();

	//previewVerticalContainer->setFixedWidth( 256 );

	QWidget * previewButtonBar = new QWidget;
	previewButtonBar->setFixedHeight( 38 );
	previewButtonBar->setLayout( new QHBoxLayout );
	previewButtonBar->layout()->setAlignment( Qt::AlignHCenter );

	//Making the parser
	ScriptParser * parser = ScriptParser::makeRegular();

	QPushButton * compButt = new QPushButton( "Compile Selected" );
	connect( compButt, &QPushButton::clicked, [this,parser,march](){
		console->log( "Parsing started" );
		Block * script = parser->parse( this->codeEditTabs->currentWidget()->findChild<QTextEdit*>()->toPlainText().toStdString() );
		console->log( "Parsing ended" );
		console->log( "Compiling started" );

		std::string outlets = "";

		try
		{
			for( int i = 0; i < this->outlets->rowCount(); i++ )
			{
				outlets += ( "frac " + this->outlets->getName( i ) + " = " + std::to_string( this->outlets->getValue( i ) ).c_str() + ";" ).toStdString();
				script->variablesInScope[ this->outlets->getName( i ).toStdString() ] = VariablePacket( Value() );
			}

			if( ( this->codeEditTabs->currentIndex() ) == 0 )
			{
				script->variablesInScope[ "x" ] = VariablePacket( Value() );
				script->variablesInScope[ "y" ] = VariablePacket( Value() );
				script->variablesInScope[ "z" ] = VariablePacket( Value() );
				script->variablesInScope[ "r" ] = VariablePacket( Value(), true );
				script->variablesInScope[ "g" ] = VariablePacket( Value(), true );
				script->variablesInScope[ "b" ] = VariablePacket( Value(), true );

				std::ifstream if_a("Assets/clparts/clheader.cl", std::ios_base::binary);
				std::ifstream if_b("Assets/clparts/clfootergutsSUBSET.cl", std::ios_base::binary);
				std::ofstream of_c("Assets/temp/testAll.cl", std::ios_base::binary);

				std::string output = parser->textOf( script, 0, true );

				of_c << if_a.rdbuf() << "bool subsetFunction( frac x, frac y, frac z, frac * r, frac * g, frac * b ){ " << outlets << output << "}" << if_b.rdbuf();
				of_c.close();

				march->load( "Assets/temp/testAll.cl" );
			}
			else
			if( ( this->codeEditTabs->currentIndex() ) == 1 )
			{
				script->variablesInScope[ "x" ] = VariablePacket( Value() );
				script->variablesInScope[ "y" ] = VariablePacket( Value() );
				script->variablesInScope[ "z" ] = VariablePacket( Value() );
				script->variablesInScope[ "r" ] = VariablePacket( Value(), true );
				script->variablesInScope[ "g" ] = VariablePacket( Value(), true );
				script->variablesInScope[ "b" ] = VariablePacket( Value(), true );

				std::ifstream if_a("Assets/clparts/clheader.cl", std::ios_base::binary);
				std::ifstream if_b("Assets/clparts/clfooterguts.cl", std::ios_base::binary);
				std::ofstream of_c("Assets/temp/testAll.cl", std::ios_base::binary);

				std::string output = parser->textOf( script, 0, true );

				of_c << if_a.rdbuf() << "frac distanceFunction( frac x, frac y, frac z, frac * r, frac * g, frac * b, __global const float* sceneVarData ){ " << outlets << output << "}" << if_b.rdbuf();
				of_c.close();

				march->load( "Assets/temp/testAll.cl" );
			}
			else
			if( ( this->codeEditTabs->currentIndex() ) == 2 )
			{
				script->variablesInScope[ "x" ] = VariablePacket( Value() );
				script->variablesInScope[ "y" ] = VariablePacket( Value() );
				script->variablesInScope[ "z" ] = VariablePacket( Value() );
				script->variablesInScope[ "dx" ] = VariablePacket( Value() );
				script->variablesInScope[ "dy" ] = VariablePacket( Value() );
				script->variablesInScope[ "dz" ] = VariablePacket( Value() );
				script->variablesInScope[ "r" ] = VariablePacket( Value(), true );
				script->variablesInScope[ "g" ] = VariablePacket( Value(), true );
				script->variablesInScope[ "b" ] = VariablePacket( Value(), true );
				script->variablesInScope[ "dist" ] = VariablePacket( Value(), true );

				std::ifstream if_a("Assets/clparts/clheader.cl", std::ios_base::binary);
				std::ifstream if_b("Assets/clparts/clfootergutsTRACE.cl", std::ios_base::binary);
				std::ofstream of_c("Assets/temp/testAll.cl", std::ios_base::binary);

				std::string output = parser->textOf( script, 0, true );

				of_c << if_a.rdbuf() << "bool traceFunction( frac x, frac y, frac z, frac dx, frac dy, frac dz, frac * r, frac * g, frac * b, frac * dist ){ " << outlets << output << "}" << if_b.rdbuf();
				of_c.close();

				march->load( "Assets/temp/testAll.cl" );
			}
		}
		catch( char * error )
		{
			console->log( ( "UNEXPECTED COMPILATION ERROR: " + std::string( error ) ).c_str(), ConsoleWidget::MESSAGE_TYPE::MSG_DIREERROR );
		}
		console->log( "Compiling ended" );
	} );
	previewButtonBar->layout()->addWidget( compButt );
	QPushButton * viewButt = new QPushButton( "Reset View" );
	connect( viewButt, &QPushButton::clicked, [march](){ march->reset(); } );
	previewButtonBar->layout()->addWidget( viewButt );

	previewVerticalContainer->layout()->addWidget( previewButtonBar ); //preview

	horizontalBottomBar->addWidget( previewVerticalContainer );

	QWidget * consoleVerticalContainer = new QWidget;
	consoleVerticalContainer->setLayout( new QVBoxLayout );
	consoleVerticalContainer->setMinimumWidth( 256 );

	consoleVerticalContainer->layout()->addWidget( new QLabel( "Console" ) ); 

	console = new ConsoleWidget;

	consoleVerticalContainer->layout()->addWidget( console ); 

	horizontalBottomBar->addWidget( consoleVerticalContainer );

	currentObjectContainer->addWidget( horizontalBottomBar );

	
	this->layout()->addWidget( currentObjectContainer );//Script edit widget
	currentObjectContainer->setVisible( false );

	outletContainer = new QWidget;
	outletContainer->setLayout( new QVBoxLayout );

	outletContainer->layout()->addWidget( new QLabel( "Outlets" ) );

	outlets = new ValueTableWidget();

	outletContainer->setFixedWidth( 150 );

	outletContainer->layout()->addWidget( outlets );

	QPushButton * addButt = new QPushButton( "Add" );
	connect( addButt, &QPushButton::clicked, [this](){
		outlets->addFullRow();
		hasChanged = true;
	} );
	outletContainer->layout()->addWidget( addButt );

	QPushButton * deleteButt = new QPushButton( "Delete" );
	connect( deleteButt, &QPushButton::clicked, [this](){
		if( outlets->currentRow() != -1 )
		{
			QMessageBox msgBox;
			msgBox.setWindowTitle("Deleting outlet");
			msgBox.setText("Are you sure you want to delete the outlet \"" + outlets->getName( outlets->currentRow() ) + "\"?");
			msgBox.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
			msgBox.setDefaultButton(QMessageBox::No);
			int result = msgBox.exec();
			if( result == QMessageBox::Yes )
			{
				outlets->removeRow( outlets->currentRow() );
				hasChanged = true;
			}
		}
	} );
	outletContainer->layout()->addWidget( deleteButt );

	this->layout()->addWidget( outletContainer ); //Outlet widget
	hasChanged = false;
	editingIndex = -1;
	hide();
}