1

I would like to make namespace Solids which does nothing more than stores informations about vertexes and edge / face connectivity for several common solids such as platonic solids.

So I did this header file Solids.h:

#ifndef  Solids_h
#define  Solids_h

#include "Vec3.h" // this is my class of 3D vector, e.g. class Vec3d{double x,y,z;}

namespace Solids{

    struct Tetrahedron{
        const static int    nVerts = 4;
        const static int    nEdges = 6;
        const static int    nTris  = 4;
        constexpr static Vec3d verts [nVerts]    = { {-1.0d,-1.0d,-1.0d}, {+1.0d,+1.0d,-1.0d}, {-1.0d,+1.0d,+1.0d}, {+1.0d,-1.0d,+1.0d} };
        constexpr static int   edges [nEdges][2] = { {0,1},{0,2},{0,3}, {1,2},{1,3},{2,3} };
        constexpr static int   tris  [nTris ][3] = { {0,1,2},{0,1,3},{0,2,3},{1,2,3} };
    } tetrahedron;

};

#endif

Then in my program which includes Solids.h I want to plot it like this:

void drawTriangles( int nlinks, const int * links, const Vec3d * points ){
    int n2 = nlinks*3;
    glBegin( GL_TRIANGLES );
    for( int i=0; i<n2; i+=3 ){
        Vec3f a,b,c,normal;
        convert( points[links[i  ]], a ); // this just converts double to float verion of Vec3
        convert( points[links[i+1]], b );
        convert( points[links[i+2]], c );
        normal.set_cross( a-b, b-c );
        normal.normalize( );
        glNormal3f( normal.x, normal.y, normal.z );
        glVertex3f( a.x, a.y, a.z );
        glVertex3f( b.x, b.y, b.z );
        glVertex3f( c.x, c.y, c.z );
    }
    glEnd();
};

// ommited some code there
int main(){
   drawTriangles( Solids::tetrahedron.nTris, (int*)(&Solids::tetrahedron.tris[0][0]), Solids::tetrahedron.verts );
}

however when I do, I get undefined reference to Solids::Tetrahedron::verts. Which is probably connected with this undefined-reference-to-static-const-int issue.

So I guess the solution should be something like initialize it outside of namespace{} and outside of struc{} declaration ?

Like:

Solids::Tetrahedron::tris [Solids::Tetrahedron::nTris ][3] = { {0,1,2},{0,1,3},{0,2,3},{1,2,3} };

or:

Solids::Tetrahedron.tris [Solids::Tetrahedron.nTris ][3] = { {0,1,2},{0,1,3},{0,2,3},{1,2,3} };

Isn't there any more elegant solution ?

Community
  • 1
  • 1
Prokop Hapala
  • 2,424
  • 2
  • 30
  • 59

1 Answers1

0

Instead of using constexpr data members, you can use constexpr static member functions.

struct Tetrahedron{
    constexpr static int    nVerts = 4;
    constexpr static int    nEdges = 6;
    constexpr static int    nTris  = 4;
    constexpr static Vec3d[nverts] verts() { return { {-1.0d,-1.0d,-1.0d}, {+1.0d,+1.0d,-1.0d}, {-1.0d,+1.0d,+1.0d}, {+1.0d,-1.0d,+1.0d} }; }
    constexpr static int[nEdges][2] edges() { return { {0,1},{0,2},{0,3}, {1,2},{1,3},{2,3} }; }
    constexpr static int[nTris ][3] tris() { return { {0,1,2},{0,1,3},{0,2,3},{1,2,3} }; }
} tetrahedron;
Rumburak
  • 3,416
  • 16
  • 27