iDevGames Forums
memcpy(stuct array pointer struct array point) - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: memcpy(stuct array pointer struct array point) (/thread-4980.html)

Pages: 1 2


memcpy(stuct array pointer struct array point) - unknown - Sep 28, 2005 07:59 AM

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


memcpy(stuct array pointer struct array point) - ThemsAllTook - Sep 28, 2005 08:32 AM

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


memcpy(stuct array pointer struct array point) - unknown - Sep 28, 2005 09:05 AM

Thanks very much that was really helpful. I didnt know you had to malloc before memcpy,
That code works perfectly.


memcpy(stuct array pointer struct array point) - TomorrowPlusX - Sep 28, 2005 09:08 AM

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


memcpy(stuct array pointer struct array point) - unknown - Sep 28, 2005 09:31 AM

v, e, f : vertices, edges, faces
[vef]n, number of [vef]

Whats wrong with that?
Rolleyes


memcpy(stuct array pointer struct array point) - akb825 - Sep 28, 2005 09:33 AM

Single-letter variables (except maybe i, j, and k for loops) are not very good to use. Just spell it out.


memcpy(stuct array pointer struct array point) - unknown - Sep 28, 2005 09:38 AM

But id have to write vertex every time, and ive got a struct with that name already.


memcpy(stuct array pointer struct array point) - Zekaric - Sep 28, 2005 10:04 AM

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.


memcpy(stuct array pointer struct array point) - unknown - Sep 28, 2005 10:14 AM

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])


memcpy(stuct array pointer struct array point) - Zekaric - Sep 28, 2005 11:22 AM

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


memcpy(stuct array pointer struct array point) - unknown - Sep 28, 2005 11:27 AM

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.


memcpy(stuct array pointer struct array point) - Malarkey - Sep 28, 2005 11:34 AM

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


memcpy(stuct array pointer struct array point) - unknown - Sep 28, 2005 12:35 PM

code-completion?
Thats probably somthing I wouldnt be able to live without, if I had it..


memcpy(stuct array pointer struct array point) - akb825 - Sep 28, 2005 12:52 PM

You can enable it in XCode. (in the preferences somewhere) Only works if you're working in a project, though.


memcpy(stuct array pointer struct array point) - Malarkey - Sep 28, 2005 01:10 PM

Just to warn you, though, XCode's code completion isn't all together that great but it's usually functional.