This is the source code:
#include <Windows.h>
#include <string>
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
#define d_open fstream::in | fstream::out | fstream::app | fstream::ate
#define Space " "
#define d_qq "\""
struct point3D
{
float X;
float Y;
float Z;
};
struct ObjectStruct
{
point3D T_LT;
point3D T_LB;
point3D T_RT;
point3D T_RB;
point3D B_LT;
point3D B_LB;
point3D B_RT;
point3D B_RB;
};
void CreateObject(ObjectStruct myobject)
{
fstream outmap;
outmap.open("prefab.map", d_open);
float * X = new float[6];
float * Y = new float[6];
float * Z = new float[6];
float * X2 = new float[6];
float * Y2 = new float[6];
float * Z2 = new float[6];
float * X3 = new float[6];
float * Y3 = new float[6];
float * Z3 = new float[6];
X[0] = myobject.T_LT.X;
Y[0] = myobject.T_LT.Y;
Z[0] = myobject.T_LT.Z;
X2[0] = myobject.T_RT.X;
Y2[0] = myobject.T_RT.Y;
Z2[0] = myobject.T_RT.Z;
X3[0] = myobject.T_RB.X;
Y3[0] = myobject.T_RB.Y;
Z3[0] = myobject.T_RB.Z;
X[1] = myobject.B_LB.X;
Y[1] = myobject.B_LB.Y;
Z[1] = myobject.B_LB.Z;
X2[1] = myobject.B_RB.X;
Y2[1] = myobject.B_RB.Y;
Z2[1] = myobject.B_RB.Z;
X3[1] = myobject.B_RT.X;
Y3[1] = myobject.B_RT.Y;
Z3[1] = myobject.B_RT.Z;
X[2] = myobject.T_LT.X;
Y[2] = myobject.T_LT.Y;
Z[2] = myobject.T_LT.Z;
X2[2] = myobject.T_LB.X;
Y2[2] = myobject.T_LB.Y;
Z2[2] = myobject.T_LB.Z;
X3[2] = myobject.B_LB.X;
Y3[2] = myobject.B_LB.Y;
Z3[2] = myobject.B_LB.Z;
X[3] = myobject.B_RT.X;
Y[3] = myobject.B_RT.Y;
Z[3] = myobject.B_RT.Z;
X2[3] = myobject.B_RB.X;
Y2[3] = myobject.B_RB.Y;
Z2[3] = myobject.B_RB.Z;
X3[3] = myobject.T_RB.X;
Y3[3] = myobject.T_RB.Y;
Z3[3] = myobject.T_RB.Z;
X[4] = myobject.T_RT.X;
Y[4] = myobject.T_RT.Y;
Z[4] = myobject.T_RT.Z;
X2[4] = myobject.T_LT.X;
Y2[4] = myobject.T_LT.Y;
Z2[4] = myobject.T_LT.Z;
X3[4] = myobject.B_LT.X;
Y3[4] = myobject.B_LT.Y;
Z3[4] = myobject.B_LT.Z;
X[5] = myobject.B_RB.X;
Y[5] = myobject.B_RB.Y;
Z[5] = myobject.B_RB.Z;
X2[5] = myobject.B_LB.X;
Y2[5] = myobject.B_LB.Y;
Z2[5] = myobject.B_LB.Z;
X3[5] = myobject.T_LB.X;
Y3[5] = myobject.T_LB.Y;
Z3[5] = myobject.T_LB.Z;
outmap
<< "{" << endl
<< "( " << X[0] << Space << Y[0] << Space << Z[0] << " ) "
<< "( " << X2[0] << Space << Y2[0] << Space << Z2[0] << " ) "
<< "( " << X3[0] << Space << Y3[0] << Space << Z3[0] << " ) "
<< "YELLOW"
<< " [ 1 0 0 0 ] [ 0 -1 0 0 ] 0 1 1 " << endl
<< "( " << X[1] << Space << Y[1] << Space << Z[1] << " ) "
<< "( " << X2[1] << Space << Y2[1] << Space << Z2[1] << " ) "
<< "( " << X3[1] << Space << Y3[1] << Space << Z3[1] << " ) "
<< "YELLOW"
<< " [ 1 0 0 0 ] [ 0 -1 0 0 ] 0 1 1 " << endl
<< "( " << X[2] << Space << Y[2] << Space << Z[2] << " ) "
<< "( " << X2[2] << Space << Y2[2] << Space << Z2[2] << " ) "
<< "( " << X3[2] << Space << Y3[2] << Space << Z3[2] << " ) "
<< "YELLOW"
<< " [ 0 1 0 0 ] [ 0 0 -1 0 ] 0 1 1 " << endl
<< "( " << X[3] << Space << Y[3] << Space << Z[3] << " ) "
<< "( " << X2[3] << Space << Y2[3] << Space << Z2[3] << " ) "
<< "( " << X3[3] << Space << Y3[3] << Space << Z3[3] << " ) "
<< "YELLOW"
<< " [ 0 1 0 0 ] [ 0 0 -1 0 ] 0 1 1 " << endl
<< "( " << X[4] << Space << Y[4] << Space << Z[4] << " ) "
<< "( " << X2[4] << Space << Y2[4] << Space << Z2[4] << " ) "
<< "( " << X3[4] << Space << Y3[4] << Space << Z3[4] << " ) "
<< "YELLOW"
<< " [ 1 0 0 0 ] [ 0 0 -1 0 ] 0 1 1 " << endl
<< "( " << X[5] << Space << Y[5] << Space << Z[5] << " ) "
<< "( " << X2[5] << Space << Y2[5] << Space << Z2[5] << " ) "
<< "( " << X3[5] << Space << Y3[5] << Space << Z3[5] << " ) "
<< "YELLOW"
<< " [ 1 0 0 0 ] [ 0 0 -1 0 ] 0 1 1 " << endl
<< "}" << endl;
delete [] X;
delete [] Y;
delete [] Z;
delete [] X2;
delete [] Y2;
delete [] Z2;
delete [] X3;
delete [] Y3;
delete [] Z3;
outmap.flush();
//
outmap.close();
}
int main(int argc, char ** argv)
{
//HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTitle("Prefab Creator by Raichu (xgm.ru,d3scene.ru)");
Sleep(200);
SetConsoleTitle("PrefabTool by Raichu v0.1");
int select = 0;
cout << "Available shapes: 1-Ring, 2-Cube, 3-Triangle" << endl;
cin >> select;
if (select == 1)
{
cout << "Enter quantity of objects:" << endl;
int quality;
cin >> quality;
if (quality > 200 || quality < 10)
{
cout << "Error... Restart..." << endl;
return main(argc, argv);
}
cout << "Enter Ring radius:(example 20.0)" << endl;
float WhSz;
cin >> WhSz;
float inangle = 360 / quality;
//system("cls");
float X1_, X2_, Y1_, Y2_;
float X1, X2, Y1, Y2;
float a = 0;
X1_ = WhSz * cos(a);
Y1_ = WhSz * sin(a);
X2_ = (WhSz + 4) * cos(a);
Y2_ = (WhSz + 4) * sin(a);
a = inangle;
for (; a <= 360; )
{
X1 = WhSz * cos(a);
Y1 = WhSz * sin(a);
X2 = (WhSz + 4) * cos(a);
Y2 = (WhSz + 4) * sin(a);
#pragma region TEST
//cout << "X:" << X1 << " Y:" << Y1 << endl;
/* COORD xy;
xy.X = X1;
xy.Y = Y1;
SetConsoleCursorPosition(console, xy);
cout << "+" << endl;
xy.X = X2;
xy.Y = Y2;
SetConsoleCursorPosition(console, xy);
cout << "?" << endl;*/
#pragma endregion
ObjectStruct NewObject;
/* down */
NewObject.B_LB.Z = NewObject.B_LT.Z = NewObject.B_RB.Z = NewObject.B_RT.Z = -5;
/* top */
NewObject.T_LB.Z = NewObject.T_LT.Z = NewObject.T_RB.Z = NewObject.T_RT.Z = 5;
NewObject.T_LT.X = NewObject.B_LT.X = X2_;
NewObject.T_LT.Y = NewObject.B_LT.Y = Y2_;
NewObject.T_LB.X = NewObject.B_LB.X = X1_;
NewObject.T_LB.Y = NewObject.B_LB.Y = Y1_;
NewObject.T_RT.X = NewObject.B_RT.X = X2;
NewObject.T_RT.Y = NewObject.B_RT.Y = Y2;
NewObject.T_RB.X = NewObject.B_RB.X = X1;
NewObject.T_RB.Y = NewObject.B_RB.Y = Y1;
CreateObject(NewObject);
X1_ = X1;
Y1_ = Y1;
X2_ = X2;
Y2_ = Y2;
//a += inangle/3; // - Create SUN
a += inangle;
}
}
system("pause");
return 0;
}
problem code:
if (select == 1)
{
cout << "Enter quantity of objects:" << endl;
int quality;
cin >> quality;
if (quality > 200 || quality < 10)
{
cout << "Error... Restart..." << endl;
return main(argc, argv);
}
cout << "Enter Ring radius:(example 20.0)" << endl;
float WhSz;
cin >> WhSz;
float inangle = 360 / quality;
//system("cls");
float X1_, X2_, Y1_, Y2_;
float X1, X2, Y1, Y2;
float a = 0;
X1_ = WhSz * cos(a);
Y1_ = WhSz * sin(a);
X2_ = (WhSz + 4) * cos(a);
Y2_ = (WhSz + 4) * sin(a);
a = inangle;
for (; a <= 360; )
{
X1 = WhSz * cos(a);
Y1 = WhSz * sin(a);
X2 = (WhSz + 4) * cos(a);
Y2 = (WhSz + 4) * sin(a);
#pragma region TEST
//cout << "X:" << X1 << " Y:" << Y1 << endl;
/* COORD xy;
xy.X = X1;
xy.Y = Y1;
SetConsoleCursorPosition(console, xy);
cout << "+" << endl;
xy.X = X2;
xy.Y = Y2;
SetConsoleCursorPosition(console, xy);
cout << "?" << endl;*/
#pragma endregion
ObjectStruct NewObject;
/* down */
NewObject.B_LB.Z = NewObject.B_LT.Z = NewObject.B_RB.Z = NewObject.B_RT.Z = -5;
/* top */
NewObject.T_LB.Z = NewObject.T_LT.Z = NewObject.T_RB.Z = NewObject.T_RT.Z = 5;
NewObject.T_LT.X = NewObject.B_LT.X = X2_;
NewObject.T_LT.Y = NewObject.B_LT.Y = Y2_;
NewObject.T_LB.X = NewObject.B_LB.X = X1_;
NewObject.T_LB.Y = NewObject.B_LB.Y = Y1_;
NewObject.T_RT.X = NewObject.B_RT.X = X2;
NewObject.T_RT.Y = NewObject.B_RT.Y = Y2;
NewObject.T_RB.X = NewObject.B_RB.X = X1;
NewObject.T_RB.Y = NewObject.B_RB.Y = Y1;
CreateObject(NewObject);
X1_ = X1;
Y1_ = Y1;
X2_ = X2;
Y2_ = Y2;
//a += inangle/3; // - Create SUN
a += inangle;
}
}
I want to create a ring using objects. It turns 3 turns (3 layers) instead of one. How to fix it? It seems the problem in degrees, but I can not understand.
(sorry for that that I use a translator Google Translate)
sin and cos take radians as input, not degrees. You'll need to convert your angles to radians before passing them into sin and cos:
X1 = WhSz * cos(a * 3.141592654 / 180.0);
Related
I'm trying to implement Runga Kutta method for solving the Lorenz differential equation system.
At some point, the program always stops at a breakpoint which I can't see, and then throws an error about heap corruption.
Something must be wrong with the way I use the pointers, but I have no idea what's not working well.
Here's my code:// Runge_Kutta.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <vector>
#include <time.h>
#include <math.h>
#include <fstream>
#include <chrono>
using namespace std;
const int dimension=3;
double* F = new double(3);
double p1 = 10;
double p2 = 28;
double p3 = 8.0 / 3.0;
double* K1 = new double(3);
double* K2 = new double(3);
double* K3 = new double(3);
double* K4 = new double(3);
double* X = new double(3);
double* t = new double(3);
double a = 0;
double b = 100;
const int N = 1000;
//double * x=new double[N];
double dt = (b - a) / N;
void f(double *F,double *x, double *t)
{
double dx1 = p1 * (x[1] - x[0]);
double dx2 = x[0] * (p2 - x[2]) - x[1];
double dx3 = x[0] * x[1] - p3 * x[2];
//cout << "dx-ek: " << dx1 << " " << dx2 << " " << dx3 << "\n";
F[0] = dx1;
F[1] = dx2;
F[2] = dx3;
}
double* RK4(double* x, double *t, double dt)
{
f(K1, x, t);
//cout << "K1: " << K1[0] << " " << K1[1] << " " << K1[2] << "\n";
for (int i = 0; i < dimension; i++)
{
X[i] = x[i] + dt * K1[i] * 0.5;
}
f(K2, X, t);
//cout << "K2: " << K2[0] << " " << K2[1] << " " << K2[2] << "\n";
for (int i = 0; i < dimension; i++)
{
X[i] = x[i] + dt * K2[i] * 0.5;
}
f(K3, X, t);
//cout << "K3: " << K3[0] << " " << K3[1] << " " << K3[2] << "\n";
for (int i = 0; i < dimension; i++)
{
X[i] = x[i] + dt * K2[i] * 0.5;
}
f(K4, X, t);
//cout << "K4: " << K4[0] << " " << K4[1] << " " << K4[2] << "\n";
for (int i = 0; i < dimension; i++)
{
//cout << "Ennek nem kéne 0-nak lennie: " << K1[i] * 0.1666666 + K2[i] * 0.33333333 + K3[i] * 0.3333333 + K4[i] * 0.1666666 << '\n';
x[i] = x[i] + dt * (K1[i] * 0.1666666 + K2[i] * 0.33333333 + K3[i] * 0.3333333 + K4[i] * 0.1666666);
//cout << "Return előtt előtt: " << x[0] << " " << x[1] << " " << x[2] << "\n";
}
//cout << "Return előtt: " << x[0] << " " << x[1] << " " << x[2] << "\n";
return x;
}
int main()
{
cout << "Let's start!";
std::ofstream myfile;
myfile.open("solution.csv");
double* x = new double(3);
/*
for(int z=0;z<dimension;z++)
{
//cout << "WHat the heck?";
x[z]= 0;
}
*/
x[0] = -5.0;
x[1] = 0.0;
x[2] = 0.0;
auto start=std::chrono::high_resolution_clock::now();
for (int j = 0; j < N; j++)
{
RK4(x, t, dt);
//cout << j << "\n";
//cout << "Return után "<< x[0] << " " <<x[1]<<" "<<x[2]<<" it: "<<j << "\n";
//cout << "\n";
for (int u = 0; u < dimension; u++)
{
myfile << x[u];
if (u <= dimension - 2) myfile << ",";
else myfile << "\n";
}
}
auto elapsed = std::chrono::high_resolution_clock::now()-start;
long long microseconds = std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
cout << "Simulation time: " << microseconds << " microsec" << endl << endl;
myfile.close();```
I have a little calculation.
Right now I calculate p_o and the value I get is correct, but somehow p_TR, where it gets the value from, is shown wrong.
The values I get are:
p_o= 0.3067666187328126 (right)
p_TR= 6.94807050163253e-310 (wrong)
Like you can see close to the end of the code, it says:
p_o = p_TR
I compile the code on Opensuse in the shell withe the g++ command.
I read that floating point numbers vary in decimal points, but this is not the same I would say.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main ()
{
//Header der angezeigt wird bei Pragrammstart
cout << "----------------------------------------------------------------------\n";
cout << "**********************************************************************\n";
cout << "----------------------------------------------------------------------\n";
cout << "===>Exakter\n";
cout << "======>Riemann\n";
cout << "=========>Loeser" << endl;
cout.flush();
//physikalische Groessen
const float k = 1.4; //isentropen Exponent
const float M = 28.9; //Molare Masse [g/kmol] des Fluids
const float R = 8314.51; //universelle Gaskonstante [J/(kmol*K)]
cout << "\nStoff-/Physikalische Groessen\n" << "k=" << k << "; M=" << M << "; R=" << R << endl;
//Anfangswerte Links
const float rho_L = 1; //Dichte [kg/m^3]
const float u_L = 0; //Geschwindigkeit [m/s]
const float p_L = 1; //Druck [Pa]
const double T_L = (p_L*M)/(R*rho_L); //Temperatur[K]
const double RmT_L = p_L/rho_L; //Zwischen Groesse fuer a_L
const double a_L = pow((k*(R/M)*T_L),(0.5)); //Schallgeschwindigkeit [m/s]
cout << "\nWerte auf der rechten Seite:\n" << "rho_L=" << rho_L << "; u_L=" << u_L << "; p_L=" << p_L << "; T_L=" << T_L << "; a_L=" << a_L << "; RmT_L=" << RmT_L << endl;
//Anfangswerte Rechts
const float rho_R = 0.125;
const int u_R = 0;
const float p_R = 0.1;
const double T_R = (p_R*M)/(R*rho_R);
const double RmT_R = p_R/rho_R; //Zwischen Groesse fuer a_R
const double a_R = pow((k*(R/M)*T_R),(0.5));
cout << "\nWerte auf der linken Seite:\n" << "rho_R=" << rho_R << "; u_R=" << u_R << "; p_R=" << p_R << "; T_R=" << T_R << "; a_R=" << a_R << "; RmT_R=" << RmT_R << endl;
//Allgemeine Anfangswerte
const float du = u_R-u_L; //Geschwindigkeitsdifferenz [m/s]
const double du_krit = ((2*a_L)/(k-1)) + ((2*a_R)/(k-1)); //kritische Geschwindigkeitsdifferenz [m/s] (positive Druck Bedingung)
const double TOL = 1e-6; //Toleranz fuer p*
cout << "\nWeitere Groessen:\n" << "du=" << du << "; du_krit=" << du_krit << "; TOL=" << TOL << endl;
cout.flush();
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Variablen Iteration p*
//Berechnung des Schätzwertes
double p_o;
double p_TR;
if (p_L>0 & p_R>0)
{
const double p_TR = pow(((a_L + a_R - 0.5*(k-1)*du)/((a_L/(pow(p_L,((k-1)/(2*k)))))+(a_R/(pow(p_R,((k-1)/(2*k))))))),((2*k)/(k-1))); //Schaetzwert fuer p0[Pa] als annahme das man 2 Verduennungswellen hat
//const double p_o = max(TOL,p_TR); //erster Schaetzwert , genommen ist Druck fuer 2 Verduennungen
p_o = p_TR;
cout << "\nkein Vakuum am Anfang ===> p_TR" << endl;
}
else
{
const double p_o = 0.5*(p_L+p_R);
cout << "\n Vakuum am Anfang ===> arithmitische Mittel fuer p_o" << endl;
}
//Variablen fuer Iteration
int n = 1;
double p_n = p_o; //p fuer Iterationen
float CHA = 1;
//Hilfsvariablen zum einfacheren rechnen
const double AL = 2/((k+1)*rho_L);
const double AR = 2/((k+1)*rho_R);
const double BL = ((k-1)*p_L)/(k+1);
const double BR = ((k-1)*p_R)/(k+1);
cout << "\np_TR=" << std::setprecision(16)<< p_TR;
cout << "\nWerte fuer Iteration:" << "\np_o=" << std::setprecision(16) << p_o << "\nAL=" << AL << "; BL=" << BL << "\nAR=" << AR << "; BR=" << BR << endl;
cout.flush();
Because you are declaring p_TR two times in different scopes, so you basically have two different variables in two different scopes having the same name p_TR.
One is instanced in the main scope, the other is instanced in your if/else block.
Same problem with p_o in the else block.
Just remove the "const double" inside your if/else block, it should do the trick.
double p_o;
double p_TR;
if (p_L>0 & p_R>0)
{
p_TR = pow(((a_L + a_R - 0.5*(k-1)*du)/((a_L/(pow(p_L,((k-1)/(2*k)))))+(a_R/(pow(p_R,((k-1)/(2*k))))))),((2*k)/(k-1))); //Schaetzwert fuer p0[Pa] als annahme das man 2 Verduennungswellen hat
//const double p_o = max(TOL,p_TR); //erster Schaetzwert , genommen ist Druck fuer 2 Verduennungen
p_o = p_TR;
cout << "\nkein Vakuum am Anfang ===> p_TR" << endl;
}
else
{
p_o = 0.5*(p_L+p_R);
cout << "\n Vakuum am Anfang ===> arithmitische Mittel fuer p_o" << endl;
}
In the following snippet, when I print the posUVZ values, they are non-zero but after I pass them to ProjectDepthToCamera(wxhDepth, posUVZ, pos3D) all the pos3D values happen to be zero. Any though on why is this happening and how to fix it?
/***
Reads the depth data from the sensor and fills in the matrix
***/
void SR300Camera::fillInZCoords()
{
PXCImage::ImageData depthImage;
PXCImage *depthMap = sample->depth;
depthMap->AcquireAccess(PXCImage::ACCESS_READ, &depthImage);
PXCImage::ImageInfo imgInfo = depthMap->QueryInfo();
int depth_stride = depthImage.pitches[0] / sizeof(pxcU16);
Projection * projection = device->CreateProjection();
unsigned int wxhDepth = depth_width * depth_height;
// create the array of depth coordinates + depth value (posUVZ) within the defined ROI
PXCPoint3DF32* posUVZ = new PXCPoint3DF32[wxhDepth];
pxcU16 *dpixels = (pxcU16*)depthImage.planes[0];
unsigned int dpitch = depthImage.pitches[0] / sizeof(pxcU16); /* aligned width */
for (unsigned int yy = 0, k = 0; yy < depth_height; yy++)
{
for (unsigned int xx = 0; xx < depth_width; xx++, k++)
{
posUVZ[k].x = (pxcF32)xx;
posUVZ[k].y = (pxcF32)yy;
posUVZ[k].z = (pxcF32)dpixels[yy * dpitch + xx];
// cout << "xx is " << posUVZ[k].x << endl;
// cout << "yy is " << posUVZ[k].y << endl;
// cout << "zz is " << posUVZ[k].z<< endl;
}
}
// convert the array of depth coordinates + depth value (posUVZ) into the world coordinates (pos3D) in mm
PXCPoint3DF32* pos3D = new PXCPoint3DF32[wxhDepth];
projection->ProjectDepthToCamera(wxhDepth, posUVZ, pos3D);
/*
if (projection->ProjectDepthToCamera(wxhDepth, posUVZ, pos3D) < PXC_STATUS_NO_ERROR)
{
delete[] posUVZ;
delete[] pos3D;
cout << "projection unsucessful";
return;
}
*/
for (unsigned int yy = 0, k = 0; yy < depth_height; yy++)
{
for (unsigned int xx = 0; xx < depth_width; xx++, k++)
{
cout << "xx is " << pos3D[k].x*1000.0 << endl;
cout << "yy is " << pos3D[k].y*1000.0 << endl;
cout << "zz is " << pos3D[k].z*1000.0 << endl;
xyzBuffer.push_back(cv::Point3f(pos3D[k].x, pos3D[k].y, pos3D[k].z));
}
}
/*
for (int idx = 0; idx < wxhDepth; idx++) {
cout << "x is " << pos3D[idx].x*1000.0 << endl;
cout << "y is " << pos3D[idx].y*1000.0 << endl;
cout << "z is " << pos3D[idx].z*1000.0 << endl;
xyzBuffer.push_back(cv::Point3f(pos3D[idx].x, pos3D[idx].y, pos3D[idx].z));
}
*/
//xyzMap = cv::Mat(xyzMap.size(), xyzMap.type, &pos3D);
xyzMap = cv::Mat(xyzBuffer);
cout << "xyzMap = " << endl << " " << xyzMap << endl << endl;
projection->Release();
delete[] posUVZ;
delete[] pos3D;
};
Here's the correct answer to get the xyzMap from the Depth image UVmap:
PXCImage::ImageData depthImage;
depthMap->AcquireAccess(PXCImage::ACCESS_READ, &depthImage);
PXCImage::ImageInfo imgInfo = depthMap->QueryInfo();
depth_width = imgInfo.width;
depth_height = imgInfo.height;
num_pixels = depth_width * depth_height;
PXCProjection * projection = device->CreateProjection();
PXCPoint3DF32 *pos3D = new PXCPoint3DF32[num_pixels];
sts = projection->QueryVertices(depthMap, &pos3D[0]);
if (sts < Status::STATUS_NO_ERROR) {
wprintf_s(L"Projection was unsuccessful! \n");
sm->Close();
}
GsVec curve_eval::eval_lagrange(float t, float numberofsegmentsn, const GsArray<GsVec>& ctrlpnts) //f(t) = sum of p.i * B.i(t)
{
//float interval = 1 / numberofsegmentsn; //so if 4, then 0.25
float interval = 1 / ctrlpnts.size();
//remember that for interval that is put in above, it's based on numbers of ctrlpnts
//for lagrange, let t
GsVec ft(0.0f, 0.0f, 0.0f);
int sizeofctrlpnts = ctrlpnts.size();
float result = 0;
std::cout << "interval = " << interval << " \\ number of segments = " << numberofsegmentsn << " \\ ctrlpnts.size() = " << ctrlpnts.size() << "\n";
float tt = 0;
float ti[50] = { 0 };
float tj[50] = { 0 }; //only this might be used
for (int x = 0; x < ctrlpnts.size(); x++) //changed from 'numberofsegmentsn'
{
tj[x] = tt;//
std::cout << "tt in tj[" << x << "]= " << tt << "\n";
tt = tt + interval;
}
float tb = 1;
tt = 1;
int i = 0;
for (int i = 0; i < ctrlpnts.size(); i ++)
{
tt = 1;
tb = 1;
for (int j = 0; j < ctrlpnts.size(); j++) //
{
if (i != j)
{
std::cout << "Before cal: i = " << i << " :: j = " << j << " :: tt = " << tt << " :: tb = " << tb << " :: t = " << t << " :: tj[i" << j << "] = " << tj[j] << " :: tj[j" << i << "] = " << tj[i] << "\n";
tt = (t - tj[j]) * tt;
tb = (tj[i] - tj[j])* tb;
std::cout << "After cal: tt = " << tt << " :: tb = " << tb << "\n";
}
//t gotta change
}
result = tt / tb;
ft = ft+(ctrlpnts[i]*result);
}
return ft;
Above is my written algorithm for Lagrange function for opengl.
Following link is the screenshot of the formula that i had to impliment, http://imgur.com/gkuaxVm.
I have been tweaking it for awhile, and i can't seem to find what is wrong with it.
I want to save a triangulation_2D to the next format :
v x1 y1
v x2 y2
...
f v11 v12 v13
f v21 v22 v23
...
where v11 v12 ... are indexes of the points already saved, here what I'm using : (CDT is a Triangulation_2 and _Point is a point_2)
std::vector<_Point> meshpoints;
int find_index(_Point p)
{
bool trouv = false;
unsigned i = 0;
while(i < meshpoints.size() && !trouv)
{
if(p.x() == meshpoints.at(i).x() && p.y() == meshpoints.at(i).y()) trouv = true;
else i++;
}
if(trouv) return i;
return -1;
}
void save_triangulation(CDT triangulation, std::string link = "unkown.txt")
{
std::cout << "Saving IVGE . . ." ;
std::ofstream triangulation_file(link);
for(CDT::Vertex_iterator vertex = triangulation.vertices_begin() ; vertex != triangulation.vertices_end() ; vertex++)
meshpoints.push_back(vertex->point());
for(unsigned i = 0 ; i < meshpoints.size() ; i++)
triangulation_file << "v " << std::setprecision(15) << meshpoints.at(i) << std::endl;
for(CDT::Face_iterator c = triangulation.faces_begin(); c != triangulation.faces_end(); c++)
{
triangulation_file << "f " << find_indice(c->vertex(0)->point()) << " " << find_indice(c->vertex(1)->point()) << " " << find_indice(c->vertex(2)->point()) <<std::endl;
}
std::cout << " Done\n" ;
}
my question is : is there any method to optimize find_index, cause I have a triangulation of more then 2M points.
I changed the format of out put file, here is the new format:
v x1 y1
v x2 y2
...
f v11 v12 v13 a11 a12 a13
f v21 v22 v23 a21 a22 a23
...
where a12 ... are triangle neighbors. the function becomes :
std::ofstream Ivge_file(link);
unsigned Vertx_index = 0;
Ivge_file << std::setprecision(15);
for(CDT::Vertex_iterator vertex = IVGE.vertices_begin() ; vertex != IVGE.vertices_end() ; vertex++)
{
Triangulation::Vertex_handle vertx_h = vertex;
vertx_h->info() = Vertx_index;
Vertx_index++;
Ivge_file << "v " << vertex->point() << std::endl;
}
int id=0;
for(CDT::Face_iterator c = IVGE.faces_begin(); c != IVGE.faces_end(); c++,id++)
c->info()._Id = id;
for(CDT::Face_iterator c = IVGE.faces_begin(); c != IVGE.faces_end(); c++)
Ivge_file << "f " << c->vertex(0)->info() << " " << c->vertex(1)->info() << " " << c->vertex(2)->info() << " " << c->neighbor(0)->info()._Id << " " << c->neighbor(1)->info()._Id << " " << c->neighbor(2)->info()._Id << std::endl;
I changed the format of out put file, here is the new format:
v x1 y1
v x2 y2
...
f v11 v12 v13 a11 a12 a13
f v21 v22 v23 a21 a22 a23
...
where a12 ... are triangle neighbors. the function becomes :
std::ofstream Ivge_file(link);
unsigned Vertx_index = 0;
Ivge_file << std::setprecision(15);
for(CDT::Vertex_iterator vertex = IVGE.vertices_begin() ; vertex != IVGE.vertices_end() ; vertex++)
{
Triangulation::Vertex_handle vertx_h = vertex;
vertx_h->info() = Vertx_index;
Vertx_index++;
Ivge_file << "v " << vertex->point() << std::endl;
}
int id=0;
for(CDT::Face_iterator c = IVGE.faces_begin(); c != IVGE.faces_end(); c++,id++)
c->info()._Id = id;
for(CDT::Face_iterator c = IVGE.faces_begin(); c != IVGE.faces_end(); c++)
Ivge_file << "f " << c->vertex(0)->info() << " " << c->vertex(1)->info() << " " << c->vertex(2)->info() << " " << c->neighbor(0)->info()._Id << " " << c->neighbor(1)->info()._Id << " " << c->neighbor(2)->info()._Id << std::endl;