Read the position Actor in PhysX - physx

I have a question about PhysX SDK 2.8.1
I'm an actor:
NxActorDesc actorDesc;
NxBodyDesc bodyDesc;
NxSphereShapeDesc sphereDesc;
sphereDesc.radius = 1.5f;
actorDesc.shapes.pushBack(&sphereDesc);
actorDesc.body = &bodyDesc;
actorDesc.density = 10;
actorDesc.globalPose.t = NxVec3(0.0f, 25.0f, 0.0f);
NxActor *dynamicActor = gsc->createActor(actorDesc);
I want the console print out the current position of the actor. How to do it? This below doesn't work:
for (int i = 0; i <= 10; i++) {
//Step PhysX simulation
if (gsc)
StepPhysX();
NxMat34 pose = dynamicActor->getGlobalPose();
cout <<pose.t << endl;
}
Specifically depends on my reading the position Y.

std::cout can't take an NxVec3 (which your post.t is).
If you want to print out the global position of your dynamicActor,
you need to print out X, Y, Z components of your NxVec3 variable separately.
NxVec3 trans = dynamicActor->getGlobalPose().t; // or you could use "dynamicActor->getGlobalPosition()"
std::cout << trans.x << ", " << trans.y << ", " << trans.z << std::endl;

Related

How to repeat tensor in libtorch

I am using libtorch to inference, I have read data from txt file to vector and convert to tensor, I want to repeat a tensor three times then change it to 3D,
I tried this
std::vector<std::vector<float>> feature_data(255, std::vector<float>(221));
ifstream f_data("../data.txt"); //
if (! f_data) {
cout << "Error, file couldn't be opened" << endl;
return 1;
}
for(int i=0;i<255;i++)
{
for(int j=0;j<221;j++)
{
if ( !f_data )
{
std::cout << "read error" << std::endl;
break;
}
f_data >> feature_data[i][j];
}
}
auto data_options = torch::TensorOptions().dtype(at::kFloat);
auto feature_tensor = torch::zeros({255,221}, data_options);
for (int i = 0; i < 255; i++)
feature_tensor.slice(0, i,i+1) = torch::from_blob(feature_data[i].data(), {221},
data_options);
// begin to repeat three times
auto tensor_clone = feature_tensor.clone();
auto one_time_clone = torch::cat({feature_tensor, tensor_clone}, 0);
auto two_times_clone = torch::cat({one_time_clone, tensor_clone}, 0);
auto transformed_asr = two_times_clone.view({3, 255, 221});
it looks troublesome and I am not sure if it is right, is there an easy way?
...
for (int i = 0; i < 255; i++)
feature_tensor.slice(0, i,i+1) = torch::from_blob(feature_data[i].data(), {221},
data_options);
auto tensor_clone = feature_tensor.repeat({3, 1});
...

Calling a Method Multiple Times (C++)

so, i have a main method
int main () {
int x = 0;
inc(x);
inc(x);
inc(x);
std::cout << x << std::endl;
}
I'm trying to get my output to be '3' but can't figure out why everytime inc(x) is called x resets to 0.
my inc method:
int inc(int x){
++x;
std::cout << "x = " << x << std::endl;
return x;
}
my output:
x = 1
x = 1
x = 1
0
Why does x reset after every call to inc(x) and how can i fix this without editing my main function
Instead of
inc(x);
I think you need
x = inc(x);
You may slap your head now.
You're passing "x" to inc() by value, so changes made in inc() won't be visible in main().
If you want x to be changed you need to pass it by reference and not as a value. Also inc() would need to return void for that to make sense. here is an example.
// adding & before x passes the reference to x
void inc(int &x){
++x;
std::cout << "x = " << x << std::endl;
}
int main() {
int x = 0;
inc(x);
inc(x);
inc(x);
std::cout << x << std::endl;
}
this prints
x = 1
x = 2
x = 3
3

Multiple for loop in C++

I am trying to write current time and some data (distance & amplitude) from a sensor in a text file. Number of data are very large (sensor rotate at 50Hz frequency and number of data could be 5000 per scan). Now I want write current time first and then all the data in a single line like this,
11:23:17 (time)
distance1 amplitude1; distance2 amplitude2; ... distance5000 amplitude5000;
11:23:18
distance1 amplitude1; distance2 amplitude2; ... distance5000 amplitude5000;
.
.
.
11:27:00
distance1 amplitude1; distance2 amplitude2; ... distance5000 amplitude5000;
"So my question is how to get this?"
I am able to write only distance and data like below
for(int t=0; t<distances.size(); t++)
{
pfsave << distances[t] <<"\t" << amplitudes[t]<<";";
}
pfsave<<endl;
Note: data type of distances & amplitudes are
vector<uint32_t> distacnes;
vector<uint32_t> amplitudes;
You can write the current time like this, before the beginning of your for loop:
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
pfsave << std::put_time(&tm, "%H:%M:%S") << std::endl;
Then your for loop without endl, to write a single line:
for(int t=0; t<distances.size(); t++)
{
pfsave << distances[t] <<"\t" << amplitudes[t]<<";";
}
And finally add endl, to finish the line:
pfsave << endl;
EDIT: following your comment
bool canContinue = true; // Condition used to stop the loop when needded
while(canContinue)
{
// Read data from your scanning device
distances = ... ;
amplitudes = ... ;
// Write output file
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
pfsave << std::put_time(&tm, "%H:%M:%S") << std::endl;
for(int t=0; t<distances.size(); t++)
pfsave << distances[t] <<"\t" << amplitudes[t]<<";";
pfsave << endl;
// Update of canContinue
canContinue = ... ;
}

Linear interpolation is not working as expected

I have a map that represents the values of a coefficient Y for a given range of temperatures. I'm trying to get the coeff_Y whenever the input key designTempfalls anywhere between the upper and lower limits of keys. I was able to get the three cases: a) when the value of the input designTemp is below the first key then coeff_Y is the first value, b) if the value of the input designTemp is beyond the last key then coeff_Y is the last value and c) if designTemp matches a key then the coeff_Y becomes the corresponding value. The case if the key falls anywhere within the key range is not working. The code showing the failed attempt of interpolation is shown below. Please note that I'm not a programmer, I'm a piping engineer just trying to write my own programs and trying to become proficient at coding with C++. Also, if there is any better solution please show so.
`cout << "\n Enter design temp. in degF: ";
float designTemp;
cin.clear(); cin.ignore(10000, '\n'); cin >> designTemp;
map<float, float> ferriticsteels_Y = { {900, 0.4}, {950, 0.5}, {1000, 0.7} };
if (ferriticsteels_Y.find(designTemp) != ferriticsteels_Y.end())
{
float coeff_Y = ferriticsteels_Y[designTemp];
cout << "\n Y: " << coeff_Y << endl;
}
if (designTemp < ferriticsteels_Y.begin()->first)
{
float coeff_Y = ferriticsteels_Y.begin()->second;
cout << "\n Y: " << coeff_Y << endl;
}
if (designTemp > ferriticsteels_Y.rbegin()->first)
{
float coeff_Y = ferriticsteels_Y.rbegin()->second;
cout << "\n Y: " << coeff_Y << endl;
}
auto lower = ferriticsteels_Y.lower_bound(designTemp) == ferriticsteels_Y.begin() ? ferriticsteels_Y.begin() : --(ferriticsteels_Y.lower_bound(designTemp));
auto upper = ferriticsteels_Y.upper_bound(designTemp);
float coeff_Y = lower->second + (upper->second - lower->second) * float(designTemp - lower->first)/fabs(upper->first - lower->first);
time_t rawtime_end;
struct tm * timeinfo_end;
time(&rawtime_end);
timeinfo_end = localtime(&rawtime_end);
cout << "\n" << asctime(timeinfo_end);
cout << "\nEnter any character and hit enter to exit: ";
char ans;
//cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cin >> ans;...giving error at 'max()'
cin.clear(); cin.ignore(10000, '\n'); cin >> ans;
return 0;}`
It works. I just was making stupid mistake. It only required to revise the nesting of the if-statements and to add a cout for looking the interpolated value at the last else. Below is the code which works as expected:
#include "../../std_lib_facilities.h"
#include <Windows.h>
#include <map>
int main()
{
SetConsoleTitle(TEXT("PipeTran™_v0.1"));
system("CLS");
system("color F1");
time_t rawtime_start;
struct tm * timeinfo_start;
time(&rawtime_start);
timeinfo_start = localtime(&rawtime_start);
printf(asctime(timeinfo_start));
cout << "\n Enter design temp. in degF: ";
float designTemp;
cin >> designTemp;
map<float, float> ferriticsteels_Y = { { 900, 0.4 },{ 950, 0.5 },{ 1000, 0.7 } };
if (ferriticsteels_Y.find(designTemp) != ferriticsteels_Y.end()) {
float coeff_Y = ferriticsteels_Y[designTemp];
cout << "\n Y: " << coeff_Y << endl;
}
else if (designTemp < ferriticsteels_Y.begin()->first) {
float coeff_Y = ferriticsteels_Y.begin()->second;
cout << "\n Y: " << coeff_Y << endl;
}
else if (designTemp > ferriticsteels_Y.rbegin()->first) {
float coeff_Y = ferriticsteels_Y.rbegin()->second;
cout << "\n Y: " << coeff_Y << endl;
}
else {
auto lower = ferriticsteels_Y.lower_bound(designTemp) == ferriticsteels_Y.begin() ? ferriticsteels_Y.begin() : --(ferriticsteels_Y.lower_bound(designTemp));
auto upper = ferriticsteels_Y.upper_bound(designTemp);
float coeff_Y = lower->second + (upper->second - lower->second) * float(designTemp - lower->first) / fabs(upper->first - lower->first);
cout << "\n Y: " << coeff_Y << endl;
}
time_t rawtime_end;
struct tm * timeinfo_end;
time(&rawtime_end);
timeinfo_end = localtime(&rawtime_end);
cout << "\n" << asctime(timeinfo_end);
cout << "\nEnter any character and hit enter to exit: ";
char ans;
cin.clear(); cin.ignore(10000, '\n'); cin >> ans;
return 0;
}

OpenCV perspectiveTransform broken function

Im trying to use perspectiveTransform but I keep getting error. I tried to follow the solution from this thread http://answers.opencv.org/question/18252/opencv-assertion-failed-for-perspective-transform/
_players[i].getCoordinates() is of type Point
_homography_matrix is a 3 x 3 Mat
Mat temp_Mat = Mat::zeros(2, 1, CV_32FC2);
for (int i = 0; i < _players.size(); i++)
{
cout << Mat(_players[i].get_Coordinates()) << endl;
perspectiveTransform(Mat(_players[i].get_Coordinates()), temp_Mat, _homography_matrix);
}
Also, how do I convert temp_Mat into type Point ?
OpenCV Error: Assertion failed (scn + 1 == m.cols) in cv::perspectiveTransform
Basically you just need to correct from
Mat(_players[i].get_Coordinates()) ...
to
Mat2f(_players[i].get_Coordinates()) ...
In the first case you are creating a 2x1, 1 channel float matrix, in the second case (correct) you create a 1x1, 2 channel float matrix.
You also don't need to initialize temp_Mat.
You can also use template Mat_ to better control the types of your Mats. E.g. creating a Mat of type CV_32FC2 is equivalent to create a Mat2f.
This sample code will show you also how to convert back and forth between Mat and Point:
#include <opencv2\opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
// Some random points
vector<Point2f> pts = {Point2f(1,2), Point2f(5,10)};
// Some random transform matrix
Mat1f m(3,3, float(0.1));
for (int i = 0; i < pts.size(); ++i)
{
cout << "Point: " << pts[i] << endl;
Mat2f dst;
perspectiveTransform(Mat2f(pts[i]), dst, m);
cout << "Dst mat: " << dst << endl;
Point2f p(dst(0));
cout << "Dst point: " << p << endl;
}
return 0;
}

Resources