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);
Related
I am experimenting with a simple vertex class.
class Vertex
{
public:
std::vector<float> coords;
//other functionality here - largely irrelevant
};
And lets say we create a Vertex object as below:
Vertex v0(1.f, 5.f, 7.f);
I am wondering if there is anyway to assign a name to each element of a vector?
Let's say that each std::vector will only ever have a size of 3. I know I can access an element or index of the vector in a way such as v0.coords[0] through to v0.coords[2];
However, I am wondering if there is a way in which I could assign a name to each element of the vector, ie:
v0.coords.x == v0.coords[0];
v0.coords.y == v0.coords[1];
v0.coords.z == v0.coords[2];
So that if I was to access the vector, I could access via a name rather than an index.
Is such a thing possible? If so, how do I go about creating such aliasing?
I am wondering if there is anyway to assign a name to each element of a vector?
No, there is not. At least, not the way you want.
I suppose you could use macros, eg:
#define coords_x coords[0]
#define coords_y coords[1]
#define coords_x coords[2]
Now you can use v0.coords_x, v0.coords_y, and v0.coords_z as needed.
Or, you can use getter methods, eg:
class Vertex
{
public:
vector<float> coords;
//other functionality here - largely irrelevant
float& x(){ return coords[0]; }
float& y(){ return coords[1]; }
float& z(){ return coords[2]; }
};
Now you can use v0.x(), v0.y(), and v0.z() as needed.
But really, in this situation, there is just good no reason to use a vector at all. It is simply the wrong tool for the job. Use a struct instead, eg:
struct Coords
{
float x;
float y;
float z;
};
class Vertex
{
public:
Coords coords;
//other functionality here - largely irrelevant
};
Alternatively:
class Vertex
{
public:
struct
{
float x;
float y;
float z;
} coords;
//other functionality here - largely irrelevant
};
Now you can use v0.coords.x, v0.coords.y, and v0.coords.z as needed.
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))
);
}
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.
I'm making a monopoly game, and I have two arrays of Vectors for coordinates for my 2d array of characters. Below is my board.h where the array is kept in the board class
class Board {
...
Vector propList[40];
Vector coordList[40];
...
public:
...
};
I am getting an error running my program in bash when trying to create the executable, displayed below (there are 2 identical errors for each array)
board.cc:15:8: error: constructor for 'Board' must explicitly initialize the member 'propList' which does not have a default constructor
Board::Board() {
^
./board.h:17:12: note: member is declared here
Vector propList[40];
I have all 40 elements initialized in my board constructor as displayed below
propList[0] = Vector(-1, -1);
propList[1] = Vector(73, 51);
...
propList[39] = Vector(81, 46);
coordList[0] = Vector(81, 54);
coordList[1] = Vector(73, 54);
...
I also tried the following
Vector v = (-1, 1);
propList[0] = v;
...
and receive the same error. Does anybody know what is going on and how to fix it?
edit: I forgot to add my vector code. I had to create a constructor since I can't use C++11 initialization on my computer.
vector.cc
#include "vector.h"
Vector::Vector(int x, int y) : x(x), y(y) {}
vector.h
struct Vector {
Vector(int x, int y);
int x;
int y;
};
In your constructor, these are assignments, not initialization. Constructors have an initialization list specifically for this purpose. Unfortunately, you can't initialize arrays like this.
Here's the initialization list for a simple int member:
class MyClass
{
int myField;
MyClass() :
m_myField(1)// initialization
{
// right here, myField is 1.
myField = 2;// assignment.
}
};
This initializes myField to 1. Then assigns 2, like you're doing in your constructor.
You can't initialize arrays like this, so I would recommend a workaround: A std::vector of Vectors. Yea that's confusing because both are called "vector", but they mean different things. std::vector is a storage container.
std::vector<Vector> propList;
and in your constructor, add Vector objects to the propList
Board::Board()
{
propList.push_back(Vector(-1, -1));
propList.push_back(Vector(73, 51));
...
}
Another solution is to add a default constructor to Vector. But that's a bit dirty compared to using std::vector<>.
In C++11 it may be possible using aggregate initialization in a constructor initialization list. No, it's not possible, period.
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]; }
};