C++ 3d points: classes or typedefs?

C++ 3d points: classes or typedefs?

Post by Russell Glasse » Tue, 04 Aug 1998 04:00:00



As part of my self-introduction to OpenGL, I am writing myself a library
filled with useful classes to keep track of object hierarchies, position and
orientation, etc.  This is to be written in VC++ 5.
        I would like to define a class GLpoint that will have x, y, and z components;
and also it will contain overloaded operators for common math operations.
This seems like a good idea to me, however I am bothered by something I read
in the OpenGL guide for Windows that said that the 3dv and 3fv functions
(using vectors, i.e., 3-element arrays) always operate more quickly than their
3d and 3f counterparts.
        In the last system I used, points were represented only as a typedef
translating to vectors.  If I use a class with separate x, y, and z
components, I will have to use the 3d and 3f routines.  Will I be likely to
inflict and inexcusable performance hit on my system by doing this?  Would you
recommend using classes in the way I have suggested or not?

--
"The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself.  Therefore all
progress depends on the unreasonable man."
                -- George Bernard Shaw

Russell can be heckled at
        http://www.willynet.com/rglasser

 
 
 

C++ 3d points: classes or typedefs?

Post by Brandon Ehl » Tue, 04 Aug 1998 04:00:00


Depending on how what compiler settings you have and what platform you use, you can
setup up a class with the 3 coords    x, y, z   in a row  and the use the reference to
the x
as the pointer that you pass to the 3dv and 3fv functions.

Brandon Ehle


> As part of my self-introduction to OpenGL, I am writing myself a library
> filled with useful classes to keep track of object hierarchies, position and
> orientation, etc.  This is to be written in VC++ 5.
>         I would like to define a class GLpoint that will have x, y, and z components;
> and also it will contain overloaded operators for common math operations.
> This seems like a good idea to me, however I am bothered by something I read
> in the OpenGL guide for Windows that said that the 3dv and 3fv functions
> (using vectors, i.e., 3-element arrays) always operate more quickly than their
> 3d and 3f counterparts.
>         In the last system I used, points were represented only as a typedef
> translating to vectors.  If I use a class with separate x, y, and z
> components, I will have to use the 3d and 3f routines.  Will I be likely to
> inflict and inexcusable performance hit on my system by doing this?  Would you
> recommend using classes in the way I have suggested or not?

> --
> "The reasonable man adapts himself to the world; the unreasonable one
> persists in trying to adapt the world to himself.  Therefore all
> progress depends on the unreasonable man."
>                 -- George Bernard Shaw

> Russell can be heckled at
>         http://www.willynet.com/rglasser


 
 
 

C++ 3d points: classes or typedefs?

Post by btu.. » Tue, 04 Aug 1998 04:00:00



> As part of my self-introduction to OpenGL, I am writing myself a library
> filled with useful classes to keep track of object hierarchies, position and
> orientation, etc.  This is to be written in VC++ 5.
>         I would like to define a class GLpoint that will have x, y, and z components;
> and also it will contain overloaded operators for common math operations.
> This seems like a good idea to me, however I am bothered by something I read
> in the OpenGL guide for Windows that said that the 3dv and 3fv functions
> (using vectors, i.e., 3-element arrays) always operate more quickly than their
> 3d and 3f counterparts.
>         In the last system I used, points were represented only as a typedef
> translating to vectors.  If I use a class with separate x, y, and z
> components, I will have to use the 3d and 3f routines.  Will I be likely to
> inflict and inexcusable performance hit on my system by doing this?  Would you
> recommend using classes in the way I have suggested or not?

you could always use a union inside a class:

union point
{
    double point[3];
    double x;
    double y;
    double z;

Quote:}

x,y and z are stored in the same memory as point.
(x=point[0], y=point[1], z=point[2])
 
 
 

C++ 3d points: classes or typedefs?

Post by btu.. » Tue, 04 Aug 1998 04:00:00




> > As part of my self-introduction to OpenGL, I am writing
myself a library
> > filled with useful classes to keep track of object

hierarchies, position and
Quote:> > orientation, etc.  This is to be written in VC++ 5.
> >         I would like to define a class GLpoint that will

have x, y, and z components;
Quote:> > and also it will contain overloaded operators for common
math operations.
> > This seems like a good idea to me, however I am bothered
by something I read
> > in the OpenGL guide for Windows that said that the 3dv
and 3fv functions
> > (using vectors, i.e., 3-element arrays) always operate

more quickly than their
Quote:> > 3d and 3f counterparts.
> >         In the last system I used, points were

represented only as a typedef
Quote:> > translating to vectors.  If I use a class with separate
x, y, and z
> > components, I will have to use the 3d and 3f routines.
Will I be likely to
> > inflict and inexcusable performance hit on my system by

doing this?  Would you

Quote:> > recommend using classes in the way I have suggested or
not?

> you could always use a union inside a class:

> union point
> {
>     double point[3];
>     double x;
>     double y;
>     double z;
> }

> x,y and z are stored in the same memory as point.
> (x=point[0], y=point[1], z=point[2])

  corection to my yesterdays post:

  it's

  union point
  {
       double point[3];
       struct
       {
           double x;
           double y;
           double z;
       }
  };

  sorry.

 
 
 

C++ 3d points: classes or typedefs?

Post by APOSTOLY Guillaum » Wed, 05 Aug 1998 04:00:00


Hum, structure is what I used for my first try with Glide (3dfx's private
API), but I moved after to arrays only and saw a great performance
improvement because this is more "cache efficient". I'm starting to with
OpenGL and will certainly make a big use of the Arrays stuffs. To help you,
I've got a good trick that make code easier to read with arrays, just make
some #define for the "offsets" in the arrays, like cX 0 cY1 cZ2 so that you
can call something like MyVertexArray[cX] (wich is as readable as
MyVertexArray->cX).
Hope this helps 8)


Quote:>As part of my self-introduction to OpenGL, I am writing myself a library
>filled with useful classes to keep track of object hierarchies, position
and
>orientation, etc.  This is to be written in VC++ 5.
> I would like to define a class GLpoint that will have x, y, and z
components;
>and also it will contain overloaded operators for common math operations.
>This seems like a good idea to me, however I am bothered by something I
read
>in the OpenGL guide for Windows that said that the 3dv and 3fv functions
>(using vectors, i.e., 3-element arrays) always operate more quickly than
their
>3d and 3f counterparts.
> In the last system I used, points were represented only as a typedef
>translating to vectors.  If I use a class with separate x, y, and z
>components, I will have to use the 3d and 3f routines.  Will I be likely to
>inflict and inexcusable performance hit on my system by doing this?  Would
you
>recommend using classes in the way I have suggested or not?

>--
>"The reasonable man adapts himself to the world; the unreasonable one
>persists in trying to adapt the world to himself.  Therefore all
>progress depends on the unreasonable man."
>                -- George Bernard Shaw

>Russell can be heckled at
>        http://www.willynet.com/rglasser

 
 
 

C++ 3d points: classes or typedefs?

Post by Nico Schmi » Wed, 05 Aug 1998 04:00:00



>> As part of my self-introduction to OpenGL, I am writing myself a library
>> filled with useful classes to keep track of object hierarchies, position and
>> orientation, etc.  This is to be written in VC++ 5.
>> I would like to define a class GLpoint that will have x, y, and z components;
>> and also it will contain overloaded operators for common math operations.
>> This seems like a good idea to me, however I am bothered by something I read
>> in the OpenGL guide for Windows that said that the 3dv and 3fv functions
>> (using vectors, i.e., 3-element arrays) always operate more quickly than their
>> 3d and 3f counterparts.
>>         In the last system I used, points were represented only as a typedef
>> translating to vectors.  If I use a class with separate x, y, and z
>> components, I will have to use the 3d and 3f routines.  Will I be likely to
>> inflict and inexcusable performance hit on my system by doing this?  Would you
>> recommend using classes in the way I have suggested or not?

>you could always use a union inside a class:

>union point
>{
>    double point[3];
>    double x;
>    double y;
>    double z;
>}

>x,y and z are stored in the same memory as point.
>(x=point[0], y=point[1], z=point[2])

Actually that does not work because all x, y and z are stored in the same
memory. Maybe you could do a class providing a cast operator to const double* :

class Point3D {
  public:
    double& x;
    double& y;
    double& z;

    Point3D() : x(point[0]), y(point[1]), z(point[2]) {
              //^^^^^^ member reference initializers
    }

    operator const double*() {
        return point;
    }
  private:
    double point[3];

Quote:};

Then you could use the following syntax :

 Point3D color;

 color.x = 1.0;
 color.y = .5;
 color.z = .2;

 glColor3dv(color);

Greetings
Nico

--

Sekr. PTZ 4, Pascalstr. 8-9       Phone : +49-30-39006342
D-10587 Berlin                    Fax   : +49-30-3930246    

 
 
 

C++ 3d points: classes or typedefs?

Post by Cass Everit » Wed, 05 Aug 1998 04:00:00



>         I would like to define a class GLpoint that will have x, y, and z components;
> and also it will contain overloaded operators for common math operations.
> This seems like a good idea to me, however I am bothered by something I read
> in the OpenGL guide for Windows that said that the 3dv and 3fv functions
> (using vectors, i.e., 3-element arrays) always operate more quickly than their
> 3d and 3f counterparts.

  Since you're using C++, you can write a class (call it vec) that has lots
of handy operator overloading and cross/dot product methods, then provide a
friend function (call it glVertex(const vec &) ) to call the GL.
  Something like this:

  class vec  {
    public:
     ...
    friend void glVertex(const vec &);
     ...
    private:
     float v[3];
   };

  inline void glVertex(const vec & vector) { glVertex3fv(vector.v); }

  This should be just as efficient as doing it by hand, plus it has
all the advantages of being a class.

                                                Hope this helps -
                                                        Cass

-------------------------------------------------------
Cass W. Everitt         Mississippi State University

Phone: 601/325-1451     P.O. Box 9627
Fax:   601/325-7692     Mississippi State, MS 39762
WWW:   http://www.erc.msstate.edu/~cass

 
 
 

C++ 3d points: classes or typedefs?

Post by Kevi » Wed, 05 Aug 1998 04:00:00


I have found one of the most important attributes is encapsulation.  So if
you encapsulate the data representation within your point class it doesn't
matter how it is stored.  You can store the data as a vector or 3 distinct
values and since it is encapsulated you are free to change the
representation as you wish.  I keep it as a vector and overload the
operartor "operator const float*() const" for speed purposes to pass the
vector directly to OpenGL's api functions.
 
 
 

C++ 3d points: classes or typedefs?

Post by Ciaran Will » Wed, 05 Aug 1998 04:00:00



> As part of my self-introduction to OpenGL, I am writing myself a library
> filled with useful classes to keep track of object hierarchies, position and
> orientation, etc.  This is to be written in VC++ 5.
>         I would like to define a class GLpoint that will have x, y, and z components;
> and also it will contain overloaded operators for common math operations.
> This seems like a good idea to me, however I am bothered by something I read
> in the OpenGL guide for Windows that said that the 3dv and 3fv functions
> (using vectors, i.e., 3-element arrays) always operate more quickly than their
> 3d and 3f counterparts.

In my 3D point classes, I store the componenets in an array, and have a
method which returns the base of the array, like:

class Point {
  private:
    float data[3];
  public:
    const float* array() const {
        return data;
    }

Quote:}

So I can pass the point to GL as
glVertex3fv(p.array());

A decent C++ compiler should inline all this, making it very efficient.

If you still want to be able to access your components in the
'conventional' sense, you can add a subscript operator to your point
class, like:

class Point {
  // ...

  float& operator [] (int i) {
    return data[i];
  }
  const float& operator [] (int i) const {
    return data[i];
  }

Quote:}

... allowing const and non-const access to your X, Y, and Z:

Point p;
p[0] = 1;  // Assign X coord of p;

const Point q;
float y;
y = q[1];

You can #define X, Y and Z as 0, 1 and 2 if you like (although I don't,
as it can cause problems elsewhere).

Ciaran.

 
 
 

C++ 3d points: classes or typedefs?

Post by Reinhard Heuin » Wed, 05 Aug 1998 04:00:00


after overloading a lot of methods with classes and all the other
overhead i actually use only
double Point[3];
to define points or vectors so works fast in every situation and i use simply
matrix and math stuff to rotate and transform points.

Reinhard

Russell Glasser schrieb:

Quote:> As part of my self-introduction to OpenGL, I am writing myself a library
> filled with useful classes to keep track of object hierarchies, position and
> orientation, etc.  This is to be written in VC++ 5.
>         I would like to define a class GLpoint that will have x, y, and z components;
> and also it will contain overloaded operators for common math operations.
> This seems like a good idea to me, however I am bothered by something I read
> in the OpenGL guide for Windows that said that the 3dv and 3fv functions
> (using vectors, i.e., 3-element arrays) always operate more quickly than their
> 3d and 3f counterparts.
>         In the last system I used, points were represented only as a typedef
> translating to vectors.  If I use a class with separate x, y, and z
> components, I will have to use the 3d and 3f routines.  Will I be likely to
> inflict and inexcusable performance hit on my system by doing this?  Would you
> recommend using classes in the way I have suggested or not?

> --
> "The reasonable man adapts himself to the world; the unreasonable one
> persists in trying to adapt the world to himself.  Therefore all
> progress depends on the unreasonable man."
>                 -- George Bernard Shaw

> Russell can be heckled at
>         http://www.willynet.com/rglasser

 
 
 

C++ 3d points: classes or typedefs?

Post by Peter Dolbe » Thu, 06 Aug 1998 04:00:00




>> As part of my self-introduction to OpenGL, I am writing myself a library
>> filled with useful classes to keep track of object hierarchies, position
and
>> orientation, etc.  This is to be written in VC++ 5.
>>         I would like to define a class GLpoint that will have x, y, and z
components;
>> and also it will contain overloaded operators for common math operations.
>> This seems like a good idea to me, however I am bothered by something I
read
>> in the OpenGL guide for Windows that said that the 3dv and 3fv functions
>> (using vectors, i.e., 3-element arrays) always operate more quickly than
their
>> 3d and 3f counterparts.
>>         In the last system I used, points were represented only as a
typedef
>> translating to vectors.  If I use a class with separate x, y, and z
>> components, I will have to use the 3d and 3f routines.  Will I be likely
to
>> inflict and inexcusable performance hit on my system by doing this?
Would you
>> recommend using classes in the way I have suggested or not?

>you could always use a union inside a class:

>union point
>{
>    double point[3];
>    double x;
>    double y;
>    double z;
>}

>x,y and z are stored in the same memory as point.
>(x=point[0], y=point[1], z=point[2])

As has been previously mentioned in another thread, this will create a union
where all 4 items share the same memory space. In C++, I use the typedef

typedef struct tagPoint3D
{
    union
     {
        struct
         {
             double x;
             double y;
             double z;
          };
          struct {double xyz[3];};
     };

Quote:} Point3D;

Defining

Point3D p;

enables you to access the x co-ord as p.x or p.xyz[0], but you can use the
array form as an argument to the gl...3dv routines. I normally derive futher
classes from this struct, but it saves having to write accessor functions.

Pete

----------------------------------------------
Peter C. Dolbey

Rapra Technology Ltd.
http://www.rapra.net
----------------------------------------------

 
 
 

C++ 3d points: classes or typedefs?

Post by Rainer Bau » Fri, 07 Aug 1998 04:00:00



Quote:>Actually that does not work because all x, y and z are stored in the same
>memory. Maybe you could do a class providing a cast operator to const double* :

>class Point3D {
>  public:
>    double& x;
>    double& y;
>    double& z;

>    Point3D() : x(point[0]), y(point[1]), z(point[2]) {
>              //^^^^^^ member reference initializers
>    }

>    operator const double*() {
>        return point;
>    }
>  private:
>    double point[3];
>};

This does not work, because on some systems references must be
initalized where they are declared.
------------------------------------------------------------

Holometric Technologies     Gartenstr.133      D-73430 Aalen
------------------------------------------------------------
 
 
 

C++ 3d points: classes or typedefs?

Post by Nico Schmi » Fri, 07 Aug 1998 04:00:00




>>Actually that does not work because all x, y and z are stored in the same
>>memory. Maybe you could do a class providing a cast operator to const double* :

>>class Point3D {
>>  public:
>>    double& x;
>>    double& y;
>>    double& z;

>>    Point3D() : x(point[0]), y(point[1]), z(point[2]) {
>>              //^^^^^^ member reference initializers
>>    }

>>    operator const double*() {
>>        return point;
>>    }
>>  private:
>>    double point[3];
>>};

>This does not work, because on some systems references must be
>initalized where they are declared.
>------------------------------------------------------------

>Holometric Technologies     Gartenstr.133      D-73430 Aalen
>------------------------------------------------------------

According to the C++ ARM (r.12.6.2) this is totally correct C++.
So every compiler that conforms to the standard should do it.

Regards
Nico

--

Sekr. PTZ 4, Pascalstr. 8-9       Phone : +49-30-39006342
D-10587 Berlin                    Fax   : +49-30-3930246    

 
 
 

C++ 3d points: classes or typedefs?

Post by mike » Fri, 07 Aug 1998 04:00:00


struct POINT3D
{
 double x;
 double y;
 double z
Quote:}

...

POINT3D P3D;

 
 
 

1. Wanted: 3D Camera class for C++ and OpenGL

Has anyone written a C++ class that encapsulates
a Camera which can be used with OpenGL?

What I have in mind is something like this:

class Camera
{
        Vector3D location;
        Vector3D lookat;
        Vector3D lookup;

        PanZoom();
        Pan();
        Zoom();
        Locate();
        Orient();

Where each of the member functions generates the
appropriate set of OpenGL commands to accomplish the
given task.

Free source code would be fanstastic, pointers to
books or articles would be nice, and any and all
help would be appriecitated.

Jay Rush

2. Software for drawing cyclic networks

3. Softimage F|S

4. ILOG Vision: C++ 3D classes on top of OpenGL

5. Book Announcement -- PECG Second Edition by Rogers

6. class-A, class-B and class-C surfaces

7. Stranahan Film Screening

8. Find closest point of 3D Point in 3D Triangle

9. Efficiently finding nearest 3D point in a 3D points cloud

10. Closest point on a 3d-Line to a point in 3d-space

11. Tiff C++ Class

12. C++ Class Library for 2d Graphs