memcpy(stuct array pointer struct array point)

Sage
Posts: 1,403
Joined: 2005.07
Post: #1
Im super lost here,
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..
Annoyed

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
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):

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
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #3
Thanks very much that was really helpful. I didnt know you had to malloc before memcpy,
That code works perfectly.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #4
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. Wink
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #5
v, e, f : vertices, edges, faces
[vef]n, number of [vef]

Whats wrong with that?
Rolleyes

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #6
Single-letter variables (except maybe i, j, and k for loops) are not very good to use. Just spell it out.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #7
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!
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #8
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.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #9
Quote: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.

Quote:two triangles stuck back to back all that well
Why you would want to slice two back too back triangles I have no idea. Rasp
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!
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #10
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);
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.
Quote:Why you would want to slice two back too back triangles I have no idea. Rasp
I'm just being and idiot. Wink 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.
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. Smile
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #11
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.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #12
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. Wink

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #13
code-completion?
Thats probably somthing I wouldnt be able to live without, if I had it..

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #14
You can enable it in XCode. (in the preferences somewhere) Only works if you're working in a project, though.
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #15
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).
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  mutable array - fill with objects - draw to view burrows111 2 2,560 Apr 21, 2010 02:32 PM
Last Post: burrows111
  Malloc() Struct with NSString Inside? Graphic Ace 3 4,465 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 9,028 Aug 28, 2009 08:12 AM
Last Post: mikey
  memory pointer tricks? Toontingy 2 4,002 Mar 31, 2009 02:35 AM
Last Post: Ingemar
  pointer cleanup questions kendric 7 4,252 Mar 29, 2009 07:48 PM
Last Post: kendric