I need to pass structure to the function Constraint::AddFixedOrientationAxis, however when I check the passed data their values are completely wrong. I have tried to use different datatypes but without any luck.
typedef struct{
size_t idx;
size_t axis_idx;
double axis_vector_1;
double axis_vector_2;
double axis_vector_3;
}AddFixedOrientationAxisData;
double Constraint::AddFixedOrientationAxis(const std::vector<double> &x, std::vector<double> &grad, void *data)
{
Eigen::VectorXd fixed_rot(3);
AddFixedOrientationAxisData *d = reinterpret_cast<AddFixedOrientationAxisData*>(data);
auto idx = d->idx;
auto axis_idx = d->axis_idx; // 0->x, 1->y, 2->z
fixed_rot << d->axis_vector_1, d->axis_vector_2, d->axis_vector_3;
std::cout << "idx: " << idx << std::endl;
std::cout << "axis: " << axis_idx << std::endl;
std::cout << "fixed_rot: " << fixed_rot << std::endl;
}
In the main, I call use it the same way as the tutorial is:
AddFixedOrientationAxisData fixed_orient_constraint_data;
fixed_orient_constraint_data.idx = 0;
fixed_orient_constraint_data.axis_idx = 0;
fixed_orient_constraint_data.axis_vector_1 = FK_q(0,0);
fixed_orient_constraint_data.axis_vector_2 = FK_q(1,0);
fixed_orient_constraint_data.axis_vector_3 = FK_q(2,0);
opt.add_equality_constraint(Constraint::AddFixedOrientationAxis, &fixed_orient_constraint_data);
The terminal output is:
idx: 93901286131024
axis: 93901286131080
fixed_rot:
4.63934e-310
-0.54938 //interestingly, this is the correct value
0.00838157 //interestingly, this is the correct value
As #{Some programmer dude} told me in the comments, the problem was that the variable was not alive when the function was called.
Related
I am trying to use the PtInRect function in Win32, which takes a POINT object. I have x and y coordinates, but I don't know how I create a POINT from those. I tried looking it up, but could only find this documentation, which does not contain the information I want.
Thank you for your help.
Problem:
You're creating trouble for yourself because of attempting to dynamically allocate the POINT object with malloc. BTW you do not always need dynamic allocation and to initialize any struct you do not need to use malloc as you pointed in the comments, you can just create the object directly.
Solution:
As tkausl pointed in the comments, POINT is created in the same way as any other struct.
Some examples on how creating a POINT:
int main()
{
int someX = 100;
int someY = 100;
RECT testRect1 = {0,0,200,200};
RECT testRect2 = {150,150,350,350};
POINT p1{someX,someY};
POINT p2 = {someX,someY};
POINT p3;
p3.x = someX;
p3.y = someY;
std::cout << (bool) PtInRect(&testRect1,p1) << std::endl;
std::cout << (bool) PtInRect(&testRect1,p2) << std::endl;
std::cout << (bool) PtInRect(&testRect1,p3) << std::endl;
std::cout << (bool) PtInRect(&testRect1,{someX,someY}) << std::endl;
std::cout << (bool) PtInRect(&testRect2,p1) << std::endl;
std::cout << (bool) PtInRect(&testRect2,p2) << std::endl;
std::cout << (bool) PtInRect(&testRect2,p3) << std::endl;
std::cout << (bool) PtInRect(&testRect2,{someX,someY}) << std::endl;
}
I am building a 2d game and I am storing all my enemy objects in an array. Right now I am trying to implement a quadtree. Currently I am just trying to build the quadtree and am not concerned with collisions. The code that pushes items to the quadtree is the following :
for (std::vector<Enemy>::iterator i=m_enemies.begin(); i != m_enemies.end(); ++i) {
std::cout << &(*i) << "Address of the object" << std::endl;
m_quad.Insert(&(*i));
}
The code for the Insert is the following :
void Quad::Insert(sf::RectangleShape* l_gameObject){
std::cout << &l_gameObject << "dsa1" << std::endl;
std::cout << "called insert " << m_objects.size() << std::endl;
m_objects.push_back(l_gameObject);
if (m_level < m_maxLevel) {
if (m_objects.size() > 3) {
std::cout<< "creating subregions " << m_objects.size() << std::endl;
m_subRegions.push_back(Quad(m_x,m_y,m_width/2.f, m_height/2, m_level + 1, m_maxLevel-1));
m_subRegions.push_back(Quad(m_x+m_width/2.f,m_y,m_width/2.f,m_height/2.f, m_level + 1, m_maxLevel-1));
m_subRegions.push_back(Quad(m_x+m_width/2.f, m_y + m_height/2.f, m_width/2.f, m_height/2.f, m_level + 1, m_maxLevel-1));
m_subRegions.push_back(Quad(m_x, m_y + m_height/2.f, m_width/2.f, m_height/2.f, m_level + 1, m_maxLevel-1));
std::vector<int> temp;
for (int i=0; i < m_objects.size(); i++){
for (int j=0; j< m_subRegions.size(); j++) {
if (m_subRegions[j].Contains(m_objects[i])) {
m_subRegions[j].Insert(m_objects[i]);
temp.push_back(i);
break;
}
}
}
for (int i = temp.size(); i > -1; i--){
m_objects.erase(m_objects.begin() + temp[i]);
}
}
}
}
When I print the address that I am passing to the Insert function and the one I have in the function I see that they are different. In fact the on in is always the same and the one I pass is always different as it should be. Could anyone clarify why that is the case ?
EDIT : Thanks to gsamaras for pointing out that I was printing the address of the parameter.
Followup question
When I use the methods of the object I am addressing in the first for loop I get the correct results, but when I do the same thing in the Insert function I get 0. Why is that ?
You are printing the address of the address.
Change this:
std::cout << &l_gameObject << "dsa1" << std::endl;
to this:
std::cout << l_gameObject << "dsa1" << std::endl;
in order to print the same thing as outside your of your function.
Inside Insert, you're printing the address of the parameter.
Outside Insert, you're printing the parameter's value.
You want
std::cout << l_gameObject << "dsa1" << std::endl;
since l_gameObject is the address you're passing in.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Hopefully I am missing something super obvious with this:
Trying to set a value of an asset (class) with an asset property modifier (class) using some vectors as containers. This seems to work during the function call, but when following the set call with a get call to confirm, it is as if the set call did not happen. See code and output below:
The asset.h file:
namespace assets {
class Asset
{
friend class AssetPropertyModifier;
std::vector<properties::AssetPropertyDouble> asset_property_double_;
enum property_id_ { ZERO, USHORT, FLOAT, INT, UINT, DOUBLE, VEC3, STRING,
BOOL};
public:
void AddProperty(std::string property_name, double property_value);
};//class Asset
}//namespace assets
AddProperty is working just fine so I am omitting that and the .cpp as not relevant. Just does a push_back into the vector to add the value, along with some indexing tracking.
In the asset property header:
namepsace properties {
struct AssetPropertyDouble
{
private:
friend class AssetPropertyModifier;
double real_number_value_;
public:
AssetPropertyDouble(double d) : real_number_value_(d) {}
};//struct AssetPropertyDouble
}//namespace proeprties
Here is the APM header:
namespace assets {
class Asset;
class AssetPropertyModifier
{
int LookUpVectorIndex(Asset a, int type , int index);
int LookUpNameIndex(Asset a, std::string s);
public:
double GetPropertyValue(Asset a, std::string property_name,
double not_used_just_an_overloader);
void SetPropertyValue(Asset a, std::string property_name,
double new_double_value);
};//class AssetPropertyModifier
}//namespace assets
In the APM .cpp file:
namespace assets {
//LookUpNameIndex definition
//LookUpVectorIndex defintion
double AssetPropertyModifier::GetPropertyValue(Asset a,
std::string property_name,
double not_used_just_an_overloader)
{
//Using Verbose output to debug:
std::cout << "------------GET-------------------" << std::endl;
int name_index = LookUpNameIndex(a, property_name);
std::cout << "names vector index is " << name_index << std::endl;
int double_index = LookUpVectorIndex(a, a.DOUBLE, name_index);
std::cout << "double property vector index is " << double_index <<
std::endl;
double property_value =
a.asset_property_double_.at(double_index).real_number_value_;
std::cout << property_name << " are equal to " << property_value <<
std::endl;
return property_value;
}
void AssetPropertyModifier::SetPropertyValue(Asset a,
std::string property_name,
double new_double_value)
{
//Using Verbose output to debug:
std::cout << "------------SET-------------------" << std::endl;
int name_index = LookUpNameIndex(a, property_name);
std::cout << "names vector index is " << name_index << std::endl;
int double_index = LookUpVectorIndex(a, a.DOUBLE, name_index);
std::cout << "double vector index is " << double_index << std::endl;
a.asset_property_double_.at(double_index).real_number_value_ =
new_double_value;
std::cout << property_name << " was changed to " <<
a.asset_property_double_.at(double_index).real_number_value_
<< std::endl;
}//SetPropertyValue
}//namespace assets
Then when I use this prototype, I get the following to happen:
from main.cpp example:
std::string some_name = "Property Name";
double some_initial_value = 5.5;
double some_new_value = 4.4;
assets::AssetPropertyModifier apm;
assets::Asset my_asset;
main {
my_asset.AddProperty(some_name, some_initial_value);
std::cout << "Current value = " << apm.GetPropertyValue(my_asset,
some_name , _DOUBLE_)
<< std::endl;
apm.SetPropertyValue(my_asset, some_name , some_new_value );
std::cout << "Current value = " << apm.GetPropertyValue(my_asset,
some_name , _DOUBLE_)
<< std::endl;
}
This is the resulting output:
------------GET-------------------
names vector index is 0
double property vector index is 0
Property Name are equal to 5.5
Current value = 5.5
------------SET-------------------
names vector index is 0
double vector index is 0
Property Name was changed to 4.4
------------GET-------------------
names vector index is 0
double property vector index is 0
Property Name are equal to 5.5
Current value = 5.5
As you can see above the GET returned 5.5 and the SET Claims it updated to 4.4 but then the following GET shows 5.5 still.
One other thing I can provide to help troubleshoot this is that if I set the property to be public so that I can access the Asset properties directly from main for sake of testing, it updates correctly:
//hard SET with public access to the vector and the property struct value
my_asset.asset_property_double_.at(0).real_number_value_ = 3.3;
//hard GET with public access:
std::cout << my_asset.asset_property_double_.at(0).real_number_value_ <<
std::endl;
//GET through APM but with public values still enabled:
std::cout << "Current value = " << apm.GetPropertyValue(my_asset, some_name,
_DOUBLE_)
<< std::endl;
That output is:
3.3
------------GET-------------------
names vector index is 0
double property vector index is 0
Property Name are equal to 3.3
Current value = 3.3
Does anyone know why the SET function is not sticking above? Thanks in advance for any help!
You are passing a copy of your asset rather than the reference to the asset, so you are actually only updating the copy, not the actual. You will need to pass by reference instead:
void AssetPropertyModifier::SetPropertyValue(Asset& a, //<----added '&'
std::string property_name,
double new_double_value)
{
//Using Verbose output to debug:
std::cout << "------------SET-------------------" << std::endl;
int name_index = LookUpNameIndex(a, property_name);
std::cout << "names vector index is " << name_index << std::endl;
int double_index = LookUpVectorIndex(a, a.DOUBLE, name_index);
std::cout << "double vector index is " << double_index << std::endl;
a.asset_property_double_.at(double_index).real_number_value_ =
new_double_value;
std::cout << property_name << " was changed to " <<
a.asset_property_double_.at(double_index).real_number_value_
<< std::endl;
}//SetPropertyValue
Am i doing this right, I want a map with a Integer as key, and struct as value. What is the easiest way to, say I want the object at 1. How do I retrieve the value of isIncluded? The last two lines in the code, I tried doing it, but then I realized I donĀ“t really know what is the way to retrieving values of structs in a numbered Map array.
Do I need to call cells.get(1) and assign that to a new temporarely struct to get its values?
/** set ups cells map. with initial state of all cells and their info*/
void setExcludedCells (int dimension)
{
// Sets initial state for cells
cellInfo getCellInfo;
getCellInfo.isIncluded = false;
getCellInfo.north = 0;
getCellInfo.south = 0;
getCellInfo.west = 0;
getCellInfo.east = 0;
for (int i = 1; i <= (pow(dimension, 2)); i++)
{
cells.put(i, getCellInfo);
}
cout << "Cells map initialized. Set [" << + cells.size() << "] cells to excluded: " << endl;
cells.get(getCellInfo.isIncluded);
cells.get(1);
}
the Map, is declared as an private instance variable like this:
struct cellInfo {
bool isIncluded;
int north; // If value is 0, that direction is not applicable (border of grid).
int south;
int west;
int east;
};
Map<int, cellInfo> cells; // Keeps track over included /excluded cells
From the documentation for Map, it appears that .get() returns a ValueType.
You would use it thus:
// Display item #1
std::cout << cells.get(1).isIncluded << "\n";
std::cout << cells.get(1).north << "\n";
Or, since the lookup is relatively expensive, you could copy it to a local variable:
// Display item #1 via initialized local variable
cellInfo ci = cells.get(1);
std::cout << ci.isIncluded << " " << ci.north << "\n";
// Display item #2 via assigned-to local variable
ci = cells.get(2);
std::cout << ci.isIncluded << " " << ci.north << "\n";
My best advice is to use the standard library's std::map data structure instead:
// Expensive way with multiple lookups:
std::cout << cells[1].isIncluded << " " << cells[1].north << "\n";
// Cheap way with one lookup and no copies
const cellinfo& ci(maps[1]);
std::cout << ci.isIncluded << " " << ci.north << "\n";
The following code prints '2' four times. Why does it never print '1'? Can someone explain me exactly what is happening here?
#include <iostream>
int main () {
union IntegersUnion {
int a;
int b;
};
IntegersUnion q;
q.a = 1;
q.b = 2;
std::cout << "(*(&q.a)) = " << (*(&q.a)) << std::endl;
std::cout << "(*(&q.b)) = " << (*(&q.b)) << std::endl;
std::cout << "(*(&(q.a))) = " << (*(&(q.a))) << std::endl;
std::cout << "(*(&(q.b))) = " << (*(&(q.b))) << std::endl;
return 0;
}
A union shares the memory between its members. By doing:
q.a = 1;
q.b = 2;
the second assignment overwrites the a.
union uses the same memory for all of its members.
So, when you assign q.b = 2;, q.a will be 2, too.
Every item in the union refers to the same location.
The most common use of union is something like this:
struct {
int dataTypeID;
union {
char char_here;
int number_here;
}
} incoming_data;
In this example, incoming_data is data imported from a file, where dataTypeID tells you what kind of data it is. (There are many file formats which optimize space in this fashion.)