View Full Version : memcpy(stuct array pointer struct array point)
unknown
2005.09.28, 11:59 AM
Im super lost here,
typedef struct {
float r[3];
} vertex;
typedef struct {
int v[2];
int f[2];
int cut;
} edge;
typedef struct {
int v[3];
int e[3];
int f[3];
float N[3];
} triangle;
typedef struct {
unsigned int vn, en, fn;
vertex *v;
edge *e;
triangle *f;
} model;
is my definition for a 3D model, but I cant make an appropriate copy method to duplicate a model,
Do I have to memcpy ever array, or can I memcpy arrays of structs?
I dont know Ive been stuck for ages, please help someone..
:\
ThemsAllTook
2005.09.28, 12:32 PM
Assuming vn, en, and fn are the vertex, edge, and triangle counts (I'll spare you the usual variable name rant... For the moment, at least):
model copyModel(model sourceModel) {
model destinationModel;
destinationModel.vn = sourceModel.vn;
destinationModel.en = sourceModel.en;
destinationModel.fn = sourceModel.fn;
destinationModel.v = (vertex *) malloc(sizeof(vertex) * destinationModel.vn);
destinationModel.e = (edge *) malloc(sizeof(edge) * destinationModel.en);
destinationModel.f = (triangle *) malloc(sizeof(triangle) * destinationModel.fn);
memcpy(destinationModel.v, sourceModel.v, (sizeof(vertex) * sourceModel.vn));
memcpy(destinationModel.e, sourceModel.e, (sizeof(edge) * sourceModel.en));
memcpy(destinationModel.f, sourceModel.f, (sizeof(triangle) * sourceModel.fn));
return destinationModel;
}
- Alex Diener
unknown
2005.09.28, 01:05 PM
Thanks very much that was really helpful. I didnt know you had to malloc before memcpy,
That code works perfectly.
TomorrowPlusX
2005.09.28, 01:08 PM
Assuming vn, en, and fn are the vertex, edge, and triangle counts (I'll spare you the usual variable name rant... For the moment, at least):
- Alex Diener
Don't space him, don't spare anyone. Those variable names are basically worthless, and will, in 6 months, confuse him as much as it would somebody who isn't familiar with the code.
Not a jab against you, Unknown. Just sayin. ;)
unknown
2005.09.28, 01:31 PM
v, e, f : vertices, edges, faces
[vef]n, number of [vef]
Whats wrong with that?
:rolleyes:
akb825
2005.09.28, 01:33 PM
Single-letter variables (except maybe i, j, and k for loops) are not very good to use. Just spell it out.
unknown
2005.09.28, 01:38 PM
But id have to write vertex every time, and ive got a struct with that name already.
Zekaric
2005.09.28, 02:04 PM
If you have a terminology and you stick with it then there isn't much of a problem. Personally I hate ijk for loop variables because ijk has meaning in mathematics. Similary why I don't generally use xyz or uvw either. However I use all the time abc (efg if needed) for unimportant loop varaibles. Not everything needs to be spelt out.
However... If you find somewhere down the line that you need to change a variable name from a single or dual character to something more verbose, search and replace will be a bit nastier to do.
Typically I'd use...
vlist, elist, flist,
vcount, ecount, fcount
vindex, eindex (or eidx), findex,
for example. But this is just part of style and preference which is a pretty personal choice apparently.
Also just to nitpick as nitpicking is being done already...
vertex is just an array of 3 floats. I'd actually use float x,y,z; instead of the array of floats. It's more explicit and you could always hack it and cast it off as a pointer to 3 floats if you need it that way. Similarly for the normal in the face structure should be of type vertex instead of another array of 3 floats.
One note of nitpick more is that the model will not account for the case of two triangles stuck back to back all that well. Or I could be wrong about the your mental model. In this case the face->f[] would really only have one other face index in that array instead of 3 unique indicies which I'm assuming is expected. But you may have already put conditions on the model from that other post you made not too long ago.
unknown
2005.09.28, 02:14 PM
hack it and cast it off as a pointer to 3 floats if you need it that way
How could you do that, the reason im using a array of 3 floats, is because I had to do nasty stuff with the same code written out three times, using abc then, bca, then cab.
two triangles stuck back to back all that well
Why you would want to slice two back too back triangles I have no idea. :p
But yeah, define the three vertices, then define two faces, one clockwise one counterclockwise.
all three face->f would be the other side, because face->f is the adjacent side (when f[0] is the adjacent side across face->e[0] and that opposite face-v[0])
Zekaric
2005.09.28, 03:22 PM
How could you do that, the reason im using a array of 3 floats, is because I had to do nasty stuff with the same code written out three times, using abc then, bca, then cab.Whoa! Really? That really makes me wonder why you need to rearrange the coordinates. Well if you find that this way is more flexible and easier to use then stick with it.
Type casting...typedef struct {
float x, y, z;
} Point;
...
void func(float *farray) {
farray[0] = 0.;
farray[1] = 0.;
farray[2] = 0.;
}
...
Point p;
func((float *) &p);The compiler may warn but it may also be smart enough to know that all that is stored in Point is essentially 3 floats. Although I wouldn't deliberately design this way. func would probably not be one of my functions but maybe some other library where it will expect arrays of floats instead of an array of some Point/Vertex/Vector structure but under the hood in memory they are the same.Why you would want to slice two back too back triangles I have no idea. :p I'm just being and idiot. ;) But then, a user of your product (assuming it'll be hacked on if it gets released.) may be an idiot and create a plane with a triangle wing where the top side is mated with the bottom side (assuming back face culling is used and that this trick might be of use.) Yes you won't see much of anything in a profile but this idiot may not care. Also if such a wing was connected to a body of the craft that wasn't as stupidly made the 2 faces per edge fails and 3 adjacent faces per face fail.
But yeah, define the three vertices, then define two faces, one clockwise one counterclockwise.
all three face->f would be the other side, because face->f is the adjacent side (when f[0] is the adjacent side across face->e[0] and that opposite face-v[0]) I didn't exactly follow all that, but then I probably don't need to. :)
unknown
2005.09.28, 03:27 PM
idiot, no.
It would work for that though, because It generates all the edges, and for duplicate edges, instead of adding it twice it just says, this face is adjacent to that one.
So in that case the triangles will just be adjacent to each other, three times.
yeah, thats just some specifics of my model class, it works how you define triangles for sine law
a/sinA=b/sinB=c/sinC
the a, b, c are opposite A, B, C, and the adjacent triangles are ordered two, so Its a good system.
Malarkey
2005.09.28, 03:34 PM
But id have to write vertex every time, and ive got a struct with that name already.
That's why they invented code-completion. ;)
unknown
2005.09.28, 04:35 PM
code-completion?
Thats probably somthing I wouldnt be able to live without, if I had it..
akb825
2005.09.28, 04:52 PM
You can enable it in XCode. (in the preferences somewhere) Only works if you're working in a project, though.
Malarkey
2005.09.28, 05:10 PM
Just to warn you, though, XCode's code completion isn't all together that great but it's usually functional.
Zekaric
2005.09.28, 05:54 PM
Ok, phew! I turned on that code completion thing and it made editing painful. I turned it off because of this.
Are there any tricks that make it work better or an option to assign a hot key to activate for only just this once sort of thing?
Malarkey
2005.09.28, 06:40 PM
Ok, phew! I turned on that code completion thing and it made editing painful. I turned it off because of this.
Are there any tricks that make it work better or an option to assign a hot key to activate for only just this once sort of thing?
Is it coming automatically up for you or something? Generally, I have it set to indicate when it could possibly complete something and then I just choose to use it. Check your Xcode prefs. Oh, and the hot key (which I think is the default) is Cmd-.
Zekaric
2005.09.29, 12:58 PM
Yes. Every time, even just cursoring around it'll try to keep up. I'll look at the options more closely then. Thanks for heads up.
If you have a terminology and you stick with it then there isn't much of a problem. Personally I hate ijk for loop variables because ijk has meaning in mathematics. Similary why I don't generally use xyz or uvw either. However I use all the time abc (efg if needed) for unimportant loop varaibles. Not everything needs to be spelt out.
...
I, j, and k are used in mathematics, and their prime use is...iterators :) for sums, products, series, and whatnot. They are also used to denote the base vectors in 3D, and complex numbers' imaginary parts, but that is just niche uses.
X, y, z, are general variables, u, v, w are general vector variables, and a, b, c generally refer to constants.
So, make sure you choose to stick with the right terminology.
Zekaric
2005.09.29, 05:51 PM
Not so niche when you are dealing with quarternions which seem to be somewhat of a fad at the moment in 3D graphics. This is why I keep them out of being iterators. Similarly for xyz which are cartesian names and uvw being texture space names. All related to 3D in someway.
There are arguments in any way you defend your position.
ThemsAllTook
2005.09.29, 06:53 PM
The reason I dislike i, j, k, etc. for iterators is that they don't give any hint about what they're iterating over. The convention I use for iterator variables is objectIndex. For example:
int numberOfFileNames;
char ** fileNameList;
int fileNameIndex, charIndex, length;
for (fileNameIndex = 0; fileNameIndex < numberOfFileNames; fileNameIndex++) {
for (charIndex = strlen(fileNames[fileNameIndex]); charIndex >= 0; charIndex--) {
if (fileNames[fileNameIndex][charIndex] == '.') {
fileNames[fileNameIndex][charIndex] = '\x00';
break;
}
}
}
fileNameIndex and charIndex make the meaning of the variables immediately clear. If I'd named them i and j, and there was a lot of complicated code in the body of the inner loop, I would likely have to refer back to the top of the loop at least a few times to figure out which variable is which. The more nested loops with nonsensical iterator names, the worse it gets. I despise having to artificially attach a meaning to an identifier that isn't self-explanatory; my brain just doesn't work like that.
- Alex Diener
TomorrowPlusX
2005.09.29, 07:13 PM
Not so niche when you are dealing with quarternions which seem to be somewhat of a fad at the moment in 3D graphics.
Fad?! They're useful!
I don't use them a whole heck of a lot, but when I want to rotate something around an arbitrary axis they come in handy.
unknown
2005.09.29, 07:16 PM
uvw being texture space names
yeah, thats true, they are commonly used as texture name variables. But there variables so they can be used for anything..
I use whatever variable name I think is suitable, I dont think its good practice to rigidly define certain things such as i is for looping, or j is for one loop inside another. It confuses people and gives them extra things to try and remember when they are learning.
I think to a small degree you will always learn programming style from who teaches you, or what book you learn from.
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.