Assign a point to the class member - c++

i trying to implement the following link http://in.mathworks.com/help/vision/examples/motion-based-multiple-object-tracking.html in opencv and c++.
I have created a class say ex:
class assign
{
vector <int> id;
vector <int> area;
vector<Point> centroid;
};
After this i have created an object
assign id;
Now i want to assign the centroid value and other values too. what i tried is
id.centroid (p);
where p is a "point" But i'm getting error for this. I don't know where i'm going wrong.

centroid is a private member of class assign. If you want to access it directly, you should make it public
class assign
{
public:
vector<Point> centroid;
//...
};
And if you want to add a Point into centroid, you should
id.centroid.push_back(p);

The main answer is already given by songyuanyao. What I want to add is a possible solution which allows you to use the member variables like you already tried it.
If you want to get and set the member centroid with id.centroid(p) you could go with the following class declaration:
class Assign
{
public:
vector<Point> centroid();
void centroid(vector<Point> c);
private:
vector<Point> m_centroid;
};
The definition might then look like this:
// getter
vector<Point> Assign::centroid() {
return m_centroid;
}
// setter
void Assign::centroid(vector<Point> c) {
m_centroid = c;
}
Now if you use id.centroid(p) to set the member the overloaded setter will be called and will set the variable. If you call p = id.centroid() (empty parameter list) the overloaded getter will be called and will return the current m_centroid.

To add to the previous answers; if you want to expand on your class this can be done for you during construction of your object.
class Assign {
private:
std::vector<int> m_vIds;
std::vector<int> m_vAreas;
std::vector<Vec2> m_vCentroids;
public:
Assign(); // Default Constructor Same As What You Have But Not Declared.
Assign( int* pIds, int* pAreas, int* pCentroids ); // Create By Using Pointers
// Create By Passing In Either Pre Filled Vectors Or Even An Empty
// Vectors To Be Filled Out Later. Passes By Reference. This Will
// Also Set The Variables That Are Passed In From The Caller.
Assign( std::vector<int>& vIds, std::vector<int>& vAreas, std::vector<Vec2>& vCentroids );
// Since You Are Using Vectors Within This Class It Is Also Good To
// Have A Destructor To Clear These Out Once The Object Is Done And
// Ready To Be Destroyed Or Removed From Memory
~Assign();
};
// The Destructor Would Look Like This
Assign::~Asign() {
if ( !m_vIds.empty() ) {
m_vIds.clear();
}
if ( !m_vAreas.empty() ) {
m_vAreas.clear();
}
if ( !m_vCentroids.empty() ) {
m_vCentroids.empty();
}
} // ~Assign
// NOTE: I used Vec2 instead of point due to my use of programming
// 2D & 3D Graphics Rendering Engines; Most Graphics APIs and Libraries
// along with Most Math Libraries Will Not Have A Point Class; Most Will
// Use Vec2 or Vec3 - Vector2 or Vector3 & Vector4 Since in terms of
// memory they are exactly the same thing. It is up to you to know which
// objects are points or locations, and which are vectors as in forces,
// velocities, accelerations, directions, normals etc. The only major
// difference between a discrete Point Class or Structure versus a Vector
// Class is that the Vector Class usually has operations defined with it
// to do vector mathematics such as addition, subtraction, multiplication by
// value, multiplication by vector, division by value, division by vector,
// cross & dot product, comparisons, testing if vector is 0, setting it to
// be a normal vector, returning the magnitude or length and a few others.
// The general point class or object is usually just data values or
// simply coordinates without operations.

Related

C++: armadillo matrices inside struct

I am using the Armadillo library in C++, I work with a group of particles having each of them its own position and velocity in space. This is why I considered to create an array of Particle where Particle is defined as a 3x2 matrix (first column=3d position, second column=3d velocity). I tried this:
struct Particle{
arma::mat state(3,2);
};
but doesn't work, it tells me "expected a type specifier".
I simply want to initialize a 3x2 matrix (possibly with zeros) every time i create a Particle object.
I tried also this:
struct Particella {
arma::mat::fixed<3,2> state;
};
which works (even if i don't know how to initialize it) but I don't know why the first statement doesn't.
The first code is trying to call a constructor where you declare the variable, which afaik with parentheses is illegal. member initialization reference
With c++11 you could do
struct Particle {
// There might be a better way to do this but armadillo's doc is currently down.
arma::mat state{arma::mat(3, 2)};
};
If not available you could try initializing the mat inside Particle's initializer list like this
struct Particle {
Particle() : state(arma::mat(3, 2)) {}
private:
arma::mat state;
};
Or in a more C-like way..
struct Particle {
arma::mat state;
};
Particle p;
p.state = arma::mat(3, 2);

invalid use of non-static data member for a matrix building

class Itembuilder
{
private:
int numOfX;
int numOfY;
int numOfZ;
int numOfSpc;
int itemMatrix [numOfZ][numOfY][numOfX];
public:
void build (Space spc, Item item)
{
numOfX = item.getX()/spc.getX(); //number of space requirement for X origin
numOfY = item.getY()/spc.getY(); //number of space requirement for Y origin
numOfZ = item.getZ()/spc.getZ(); //number of space requirement for Z origin
for (int layer=1; layer<=numOfZ; layer++) // stating layers of item through Z origin
{
for (int orgY=1; orgY<=numOfY; orgY++) // stating origin Y of a layer
{
for (int orgX=1; orgX<=numOfX; orgX++) // stating origin X
{
itemMatrix[layer][orgY][orgX]=0;
}
}
}
}
};
Hi, I'm very new to coding in C++. I'm trying to build 3D item for allocating in a domain. First, I got "item.get" and "spc.get" variables from other classes. When trying to state the units as 0 with itemMatrix, I got error about non-static condition of private variables. How would I state space units with matrix?
Please correct my codes with proper one
Thanks
The problem is here:
int itemMatrix[numOfZ][numOfY][numOfX];
C++ does not allow you to use values of member variables in declaring other members.
The process of creating a 3D matrix from arrays is a lot simpler if you use nested vectors:
std::vector<std::vector<std::vector<int>>> itemMatrix;
Then you can initialize it in the constructor as follows:
Itembuilder(int numOfX, int numOfY, int numOfZ)
: itemMatrix(numOfX, std::vector<std::vector<int>>(numOfY, std::vector<int>(numOfZ))) {
}
Is there any other way to initialize vector instead of constructor?
The vector needs to be initialized in the constructor in order to make the object consistent upon construction. However, it does not mean that you don't have an option to re-assign the vector once the constructor has finished. If you later need to change the matrix, for example, to change its size, you can re-assign the vector:
void changeSize(int numOfX, int numOfY, int numOfZ) {
itemMatrix = std::vector<std::vector<std::vector<int>>>(
numOfX
, std::vector<std::vector<int>>(numOfY, std::vector<int>(numOfZ))
);
}

Difference between the dot operator and a get method

I have a vector class
class Vector {
public:
double x, y, z;
Vector add(Vector v) {
return Vector(x+v.x, y+v.y, z+v.z);
}
};
and I have a class Ray
class Ray {
public:
Vector origin, direction;
Vector getOrigin() { return origin; }
};
In some method, I do:
Vector x = ray.origin().add(normal); // doesn't work
Vector y = ray.getRayOrigin().add(normal); // works
error message: Type vector doesn't provide a call operator
Why can't I just access the vector directly?
Because origin is not a function. Remove the parentheses when you access it.
Xīcò has the correct solution, but not the right symptom.
origin doesn't have to be a function. The Vector class could overload operator() and be called as if it were a function, and that's the message the compiler is trying to get across.
ray.origin allows anyone to do anything to ray's origin member including things that could be harmful to ray. Very uncool. The purpose of setters and getters is to regulate access to member variables. The goal is self defence.
OP's getOrigin method doesn't return origin. It returns a copy of origin. A malicious cretin can do anything they want to the copy without breaking ray. This is most often the right way unless the object returned is intended to be modified or prohibitively expensive to copy. In that modification case, lock down the returned object with private data, and getters and setters of it's own. In the expensive copy case, declare the return value to be const to reduce the possibility of damage.
A good setter will vet all input to the origin member before allowing the change to take place. If the caller tries to feed in values that are inconsistent with origin, ray can slap it down.
Directly accessing origin through . allows ray no defence whatsoever. It also prevents ray from changing the implementation of origin without also chancing origin's users.
Whether these are concerns with a pair of simple classes like Vector and Ray, is a matter of coding style and necessity, but locking down data access to the minimum necessary is generally a good habit to get into and a must when developing complicated software.
class Vector {
public:
double x, y, z;
Vector add(Vector v) {
return Vector(x+v.x, y+v.y, z+v.z);
}
};
class Ray {
public:
Vector origin, direction;
Vector getOrigin() { return origin; }
Vector& getOrigin2() { return origin; }
};
int main() {
Ray ray;
Vector v1 = ray.origin; // returns origin member
Vector v2 = ray.getOrigin(); // returns a copy of origin member
Vector v3 = ray.getOrigin2(); // same as v1, returns origin member
}

VBO of std::vector<MyClass*> and correct strides

I'm wondering how to get the maximum data locality and performance for the following problem without data copy.
I've a std::vector< MyClass* > where MyClass is something like
class MyClass
{
public:
MyClass(int n,double px,double py,double pz)
{
someField=n;
x=px;
y=py;
z=pz;
anotherField=100;
anotherUnusefulField=-10.0;
}
int someField;
int anotherField;
double x;
double y;
double z;
double anotherUnusefulField;
};
std::vector<MyClass*> myClassVector;
// add some values and set x,y,z
for (std::vector<MyClass*>::iterator iter = myClassVector.begin(); iter!=myClassVector.end();++iter)
{
MyClass *tmp = *iter;
tmp->x+=1.0;
tmp->y+=2.0;
tmp->z+=3.0;
}
I'm iterating frequently on these data and I also would like to enforce data locality. The data contained in the pointer to MyClass should be sent to a OpenGL vertex array, where the vertices are ONLY determined by x,y,z variables. As you may imagine is difficult to correctly set the strides, so I'm here to ask if there are other (portable) solution to this problem.
(p.s. I've already read the post VBOs with std::vector but my case is basically different because I have pointers and I also have other variables inside the class.)
I have pointers
Those pointers are useless to OpenGL, as they're in client address space. Also OpenGL doesn't dereference second level pointers.
and I also have other variables inside the class.
Well, then don't do this. If you passed those class instances to OpenGL you'd copy a lot of useless data. I recommend you just store a index into a tightly packed std::vector or array in your class members, and a reference to the vector/array itself. You can use getter/setter/referencer member functions to abstract away the access to the vector, i.e.
class …
{
// …
std::vector<v_t> *v;
size_t index_v;
x_t getX() const { return (*v)[index_v]; }
x_t setX(x_t x) { return (*v)[index_v] = x;}
x_t &x() { return (*v)[index_v]; }
};

Values from vector differ from the original value

I am confused with C++ vector and ask for help.
I declare a class CBoundaryPoint:
class CBoundaryPoint:
{
public:
double m_param;
int m_index;
}
And then I define a vector:
vector<CBoundaryPoint> vBoundPoints;
CBoundaryPoint bp;
double param;
// other codes
bp.m_param = param;
vBoundPoints.push_back( bp );
It surprises me that for every element in vBoundPoints, the value of m_param is totally different from the given value param. I just don't know why.
For Example:
param = 0.3356;
bp.m_param = param; // so bp.param equals to 0.3356;
vBoundPoints.push_back( bp ); // while (*(vBoundPoints.end()-1)).m_param = -6.22774385622041925e+066; same case to other elements
So what happened and why? I'm using VS2010.
You probably get garbage when you resize the vector or create a vector of a certain size using the size_type constructor. You get default-constructed objects in your vector, and these contain primmitive types. Since you have no user defined default constructor, the values are essentially random, or "garbage".
You can fix this by adding a default constructor to your class:
class CBoundaryPoint:
{
public:
CBoundaryPoint : m_param(), m_index() {} // initializes members to 0. and 0
double m_param;
int m_index;
}