memcpy(stuct array pointer struct array point)
Im super lost here,
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..
Code:
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..
Sir, e^iπ + 1 = 0, hence God exists; reply!
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
Code:
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
Thanks very much that was really helpful. I didnt know you had to malloc before memcpy,
That code works perfectly.
That code works perfectly.
Sir, e^iπ + 1 = 0, hence God exists; reply!
ThemsAllTook Wrote: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.
v, e, f : vertices, edges, faces
[vef]n, number of [vef]
Whats wrong with that?
[vef]n, number of [vef]
Whats wrong with that?
Sir, e^iπ + 1 = 0, hence God exists; reply!
Single-letter variables (except maybe i, j, and k for loops) are not very good to use. Just spell it out.
But id have to write vertex every time, and ive got a struct with that name already.
Sir, e^iπ + 1 = 0, hence God exists; reply!
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.
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.
Quote:hack it and cast it off as a pointer to 3 floats if you need it that wayHow 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.
Quote:two triangles stuck back to back all that wellWhy you would want to slice two back too back triangles I have no idea.

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])
Sir, e^iπ + 1 = 0, hence God exists; reply!
unknown Wrote: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...
Code:
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);Quote:Why you would want to slice two back too back triangles I have no idea.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.Quote:But yeah, define the three vertices, then define two faces, one clockwise one counterclockwise.I didn't exactly follow all that, but then I probably don't need to.
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])
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.
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.
Sir, e^iπ + 1 = 0, hence God exists; reply!
unknown Wrote:But id have to write vertex every time, and ive got a struct with that name already.
That's why they invented code-completion.
The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
code-completion?
Thats probably somthing I wouldnt be able to live without, if I had it..
Thats probably somthing I wouldnt be able to live without, if I had it..
Sir, e^iπ + 1 = 0, hence God exists; reply!
You can enable it in XCode. (in the preferences somewhere) Only works if you're working in a project, though.
Just to warn you, though, XCode's code completion isn't all together that great but it's usually functional.
The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| mutable array - fill with objects - draw to view | burrows111 | 2 | 2,264 |
Apr 21, 2010 02:32 PM Last Post: burrows111 |
|
| Malloc() Struct with NSString Inside? | Graphic Ace | 3 | 3,783 |
Jan 26, 2010 05:32 PM Last Post: cmiller |
|
| Finding the closest point to (x1,y1) in array of [x1,y1,z1,x2... | mikey | 17 | 7,879 |
Aug 28, 2009 08:12 AM Last Post: mikey |
|
| memory pointer tricks? | Toontingy | 2 | 3,671 |
Mar 31, 2009 02:35 AM Last Post: Ingemar |
|
| pointer cleanup questions | kendric | 7 | 3,848 |
Mar 29, 2009 07:48 PM Last Post: kendric |
|

