f



point classes, design of class relation

I would like to work with points in space:
Point2D : (x,y)
Point3D : (x,y,z)
Pos2D : (x,y,yaw)
Pos6D : (x,y,z,roll,pitch,yaw)

what would be nice is that I can pass a Pos6D to a function that takes
a Point3D as argument. What would be the best way of doing that?

Possibilities I can see are:

1) Point2D is base class of Pos2D which is base class of Pos6D. That's
what I was doing untill I introduced the Point3D class. If Point3D
inherits from Point2D and If Pos6D inherits from both Point3D and
Pos2D then there is ambiguity. Can I solve that?

2) make this classes independent and define constructors and cast
operator to convert between them?

Thanks
Brice

0
4/3/2008 10:51:13 AM
comp.lang.c 30656 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

2 Replies
286 Views

Similar Articles

[PageSpeed] 5

Brice Rebsamen writes:
> 1) Point2D is base class of Pos2D which is base class of Pos6D.

C does not have classes, let alone base classes.  Try comp.lang.c++.
Though C does allow a hack:  If two structs start with the same
members, you can cast a pointer to one of the structs to a pointer
to the other and access the common members that way.  Usually
it's better to avoid such hacks, however.

> 2) make this classes independent and define constructors and cast
> operator to convert between them?

Nor does C have constructors.

-- 
Hallvard
0
h.b.furuseth (450)
4/3/2008 11:01:47 AM
Brice Rebsamen said:

> I would like to work with points in space:
> Point2D : (x,y)
> Point3D : (x,y,z)
> Pos2D : (x,y,yaw)
> Pos6D : (x,y,z,roll,pitch,yaw)
> 
> what would be nice is that I can pass a Pos6D to a function that takes
> a Point3D as argument. What would be the best way of doing that?
> 
> Possibilities I can see are:
> 
> 1) Point2D is base class of Pos2D which is base class of Pos6D.

One problem with this is that a 6D position is not really a special kind of 
2D point, is it? Nor do you add much value to an (x, y, z) co-ordinate by 
rolling, pitching, or yawing it, because points are infinitesimally small 
in all directions, so you wouldn't be able to tell what you'd done anyway.

Solid objects are made up of surfaces (and sometimes holes in those 
surfaces), with the surfaces (and, by omission, the holes!) being defined 
by faces, which are either described by edges (delimited by vertices) or 
by parametric equations. It is only solid objects (or "solid" objects, 
e.g. clouds!) that you can meaningfully yaw, pitch, or roll.

So I think your whole design needs a re-think while there's still time.

If you're after a 3D graphics engine, I would forget 2D completely, and use 
an hierarchy that looks something like this:

struct Point_
{
  double x;
  double y;
  double z;
};

typedef struct Point_ Point;

That's about it, really.

Leaving parametric equations aside for the moment, I would then continue 
something like this:

struct Edge_
{
  Point a;
  Point b;
};

typedef struct Edge_ Edge;

struct Face_
{
  size_t NumEdges;
  Edge *e;
};

typedef struct Face_ Face;

....and so on, building primitive objects out of faces, and composites out 
of primitives - and keeping track of which edge, if any, two faces have in 
common. At no stage are these "is-a" relationships. They are all "has-a" 
relationships, so there's no obvious reason for an hierarchical structure.

At the primitive object stage, record the current scale, orientation 
(pitch, yaw, roll), position ***relative to the composite object of which 
it is a part***.

At the composite object stage, record the current scale, orientation 
(pitch, yaw, roll), position relative to the origin of the virtual world.

Then all you need to know is where your camera is pointing, and you're 
laughing - Shrek IV in 10,000,000,000 astoundingly complicated stages.

Just to reiterate the point: you will have noticed the complete absence of 
a class hierarchy in this design. That's for two reasons - 1) I really 
don't think a class hierarchy is going to be a winner for you here (see 
above), and 2) whilst it's *possible* to do OOP in C, it's really, really 
icky and you almost certainly don't want to go there. Not if you want to 
*finish* this project at some point, anyway.

-- 
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
0
rjh (10791)
4/3/2008 11:43:53 AM
Reply: