_Surface.cc

Go to the documentation of this file.
00001 #include <esg/geometry/_Surface.h>
00002 #include <esg/mesh/PolygonMesh.h>
00003 
00004 using namespace esg;
00005 
00006 Mesh* _Surface::_mesh(int) const
00007 {
00008     return NULL;  // unspecified
00009 }
00010 
00011 void _Surface::_duplicate_attributes(const SceneGraphObject& src)
00012 {
00013     _duplicate_attributes((Geometry&)src);
00014 }
00015 
00016 void _Surface::_duplicate_attributes(const Geometry& src)
00017 {
00018     // we do not want to call the Polygon::duplicate_attributes()
00019     // because it clones all attributes (but we are using shared arrays)
00020     // Therefore jump over this method and call
00021     // the Geometry::_duplicate_attributes() instead.
00022     Geometry::_duplicate_attributes(src);
00023 
00024     // Now we must clone all attributes (including those defined
00025     // in Polygon class) except the shared arrays. They are referenced.
00026     _nVert = ((_Surface&)src)._nVert;
00027     _vertices = ((_Surface&)src)._vertices;
00028     _normals  = ((_Surface&)src)._normals;
00029     _normal.set(((_Surface&)src)._normal);
00030     _normalFixed = ((_Surface&)src)._normalFixed;
00031     _fxyz = ((_Surface&)src)._fxyz;
00032     _edgeProjection = ((_Surface&)src)._edgeProjection;
00033 
00034     _vindices = new unsigned [_nVert];
00035     for (register unsigned i = 0; i < _nVert; i++)
00036         _vindices[i] = ((_Surface&)src)._vindices[i];
00037 
00038     if (((_Surface&)src).haveVertNormals()) {
00039         _nindices = new unsigned [_nVert];
00040         for (register unsigned i = 0; i < _nVert; i++)
00041             _nindices[i] = ((_Surface&)src)._nindices[i];
00042     } else
00043         _nindices = NULL;
00044 
00045     if (((_Surface&)src).haveVertUVCoords()) {
00046         _uvindices = new unsigned [_nVert];
00047         for (register unsigned i = 0; i < _nVert; i++)
00048             _uvindices[i] = ((_Surface&)src)._uvindices[i];
00049     } else
00050         _uvindices = NULL;
00051     
00052     if (((_Surface&)src)._normals) {
00053         _normals = new Vector3 [_nVert];
00054         for (unsigned i = 0; i < _nVert; i++)
00055             _normals[i].set(((_Surface&)src)._normals[i]);
00056     } else
00057         _normals = NULL;
00058 
00059     if (((_Surface&)src)._uvCoords) {
00060         _uvCoords = new Vector2 [_nVert];
00061         for (unsigned i = 0; i < _nVert; i++)
00062             _uvCoords[i].set(((_Surface&)src)._uvCoords[i]);
00063     } else
00064         _uvCoords = NULL;
00065 
00066     _tangible = true;
00067     _proj = NULL;
00068 }
00069 
00070 Vertex3 _Surface::_get_vertex(unsigned index) const 
00071 {
00072     return _vertices[_vindices[index]];
00073 }
00074 
00075 Vertex3 _Surface::_get_vert_normal(unsigned index) const 
00076 {
00077     return _normals[_nindices[index]];
00078 }
00079 
00080 Vertex2 _Surface::_get_vert_uv_coord(unsigned index) const 
00081 {
00082     return _uvCoords[_uvindices[index]];
00083 }
00084 
00085 void _Surface::_rotateX(float a)
00086 {
00087     Matrix3 trMat;
00088     trMat.rotX(a);
00089     trMat.transform(_normal);
00090     _set_edge_projection();
00091     if (_proj) { delete [] _proj; _proj = NULL; }
00092     _fxyz = - _normal.dot(getVertex(0));
00093 }
00094 
00095 void _Surface::_rotateY(float a)
00096 {
00097     Matrix3 trMat;
00098     trMat.rotY(a);
00099     trMat.transform(_normal);
00100     _set_edge_projection();
00101     if (_proj) { delete [] _proj; _proj = NULL; }
00102     _fxyz = - _normal.dot(getVertex(0));
00103 }
00104 
00105 void _Surface::_rotateZ(float a)
00106 {
00107     Matrix3 trMat;
00108     trMat.rotZ(a);
00109     trMat.transform(_normal);
00110     _set_edge_projection();
00111     if (_proj) { delete [] _proj; _proj = NULL; }
00112     _fxyz = - _normal.dot(getVertex(0));
00113 }
00114 
00115 void _Surface::_rotate(float a, const Vector3& axis)
00116 {
00117     Matrix4 trMat;
00118     Matrix3 rotMat;
00119     trMat.rotationGL(a, axis);
00120     trMat.get(rotMat);
00121     rotMat.transform(_normal);
00122     _set_edge_projection();
00123     if (_proj) { delete [] _proj; _proj = NULL; }
00124     _fxyz = - _normal.dot(getVertex(0));
00125 }
00126 
00127 void _Surface::_rotate(const Matrix3& rotMat)
00128 {
00129     rotMat.transform(_normal);
00130     _set_edge_projection();
00131     if (_proj) { delete [] _proj; _proj = NULL; }
00132     _fxyz = - _normal.dot(getVertex(0));
00133 }
00134 
00135 void _Surface::_translate(float x, float y, float z)
00136 {
00137     _fxyz = - _normal.dot(getVertex(0));
00138 }
00139 
00140 void _Surface::_transform(const Matrix4& trMat)
00141 {
00142     Matrix3 rotMat;
00143     trMat.get(rotMat);
00144     rotMat.transform(_normal);
00145     _set_edge_projection();
00146     if (_proj) { delete [] _proj; _proj = NULL; }
00147     _fxyz = - _normal.dot(getVertex(0));
00148 }
00149 
00150 void _Surface::_scale(float s)
00151 {
00152     if (_proj) { delete [] _proj; _proj = NULL; }
00153     _fxyz *= s;
00154 }
00155 
00156 _Surface::_Surface()
00157 {
00158     _vindices  = NULL;
00159     _nindices  = NULL;
00160     _uvindices = NULL;
00161 }
00162 
00163 
00164 //------ public ------
00165 
00166 _Surface::_Surface(Vector3*             v,
00167                    Vector3*             n,
00168                    Vector2*             uv,
00169                    unsigned             num,
00170                    const unsigned*      vind,
00171                    const unsigned*      nind,
00172                    const unsigned*      uvind,
00173                    const Vector3&       norm,
00174                    bool                 fixedn)
00175 {
00176   _nVert          = num;
00177   _vertices       = v;
00178   _normals        = n;
00179   _uvCoords       = uv;
00180   _vindices       = new unsigned [_nVert];
00181   for (unsigned i = 0; i < _nVert; i++)  _vindices[i] = vind[i];
00182   _normal.set(norm);
00183   _normalFixed    = fixedn;
00184   _fxyz           = - _normal.dot(getVertex(0));
00185   _proj           = NULL;
00186   
00187   if (n && nind) {
00188       _nindices = new unsigned [_nVert];
00189       for (unsigned i = 0; i < _nVert; i++) _nindices[i] = nind[i];
00190   } else 
00191       _nindices = NULL;
00192 
00193   if (uv && uvind) {
00194       _uvindices = new unsigned [_nVert];
00195       for (unsigned i = 0; i < _nVert; i++) _uvindices[i] = uvind[i];
00196   } else 
00197       _uvindices = NULL;
00198 
00199   // find projection plane
00200   _set_edge_projection();
00201   _tangible = true;
00202 }
00203 
00204 _Surface::_Surface(Vector3*             v,
00205                    Vector3*             n,
00206                    Vector2*             uv,
00207                    unsigned             num,
00208                    const unsigned*      vind,
00209                    const unsigned*      nind,
00210                    const unsigned*      uvind)
00211 {
00212   _nVert          = num;
00213   _vertices       = v;
00214   _normals        = n;
00215   _uvCoords       = uv;
00216   _vindices       = new unsigned [_nVert];
00217   for (unsigned i = 0; i < _nVert; i++)  _vindices[i] = vind[i];
00218   _normal.set(_get_facet_normal());
00219   _normal.normalize();
00220   _normalFixed    = false;
00221   _fxyz           = - _normal.dot(getVertex(0));
00222   _proj           = NULL;
00223 
00224   if (n && nind) {
00225       _nindices = new unsigned [_nVert];
00226       for (unsigned i = 0; i < _nVert; i++) _nindices[i] = nind[i];
00227   } else 
00228       _nindices = NULL;
00229 
00230   if (uv && uvind) {
00231       _uvindices = new unsigned [_nVert];
00232       for (unsigned i = 0; i < _nVert; i++) _uvindices[i] = uvind[i];
00233   } else 
00234       _uvindices = NULL;
00235 
00236   // find projection plane
00237   _set_edge_projection();
00238 
00239   _tangible = true;
00240 }
00241 
00242 _Surface::~_Surface()
00243 {
00244     if (_vindices)  delete [] _vindices;
00245     if (_nindices)  delete [] _nindices;
00246     if (_uvindices) delete [] _uvindices;
00247 }
00248 
00249 SceneGraphObject* _Surface::clone() const
00250 {
00251   _Surface* pRet = new _Surface();
00252   pRet->_duplicate_attributes((SceneGraphObject&) *this);
00253   return (SceneGraphObject*) pRet;
00254 }
00255 
00256 Geometry* _Surface::clone(const Matrix4* pTrMat) const
00257 {
00258   _Surface* pRet = new _Surface();
00259   pRet->_duplicate_attributes((Geometry&) *this);
00260   if (pTrMat) pRet->_transform(*pTrMat);
00261   return pRet;
00262 }
00263 
00264 void _Surface::dump(const char* intent, const char* tab)
00265 {
00266     printf("%s geometry PolygonOfSurface {\n", intent);
00267     if (_normalFixed) 
00268         printf("%s %s normal %f %f %f\n", intent, tab, _normal.x, _normal.y, _normal.z);
00269     if (_vindices) {
00270         printf("%s %s vertexIndices", intent, tab);
00271         for (register unsigned i = 0; i < _nVert; i++) printf(" %d", _vindices[i]);
00272         printf("\n");
00273     }
00274     if (haveVertNormals()) {
00275         printf("%s %s vertexNormalIndices", intent, tab);
00276         for (register unsigned i = 0; i < _nVert; i++)
00277             for (register unsigned i = 0; i < _nVert; i++) printf(" %d", _nindices[i]);
00278         printf("\n");
00279     }
00280     if (haveVertUVCoords()) {
00281         printf("%s %s uvCoordIndices", intent, tab);
00282         for (register unsigned i = 0; i < _nVert; i++) printf(" %d", _uvindices[i]);
00283         printf("\n");
00284     }
00285     printf("%s }\n", intent);
00286 }
00287 
00288 Vertex3 _Surface::getVertex(unsigned index) const 
00289 {
00290     return ((index < _nVert && _vertices && _vindices)
00291             ? _get_vertex(index)
00292             :Vertex3(0,0,0));
00293 }
00294 
00295 Vertex3 _Surface::getVertNormal(unsigned index) const 
00296 {
00297     return ((index < _nVert && _normals && _nindices)
00298             ? _get_vert_normal(index)
00299             : Vertex3(0,0,0));
00300 }
00301 
00302 Vertex2 _Surface::getVertUVCoord(unsigned index) const 
00303 {
00304     return ((index < _nVert && _uvCoords && _uvindices)
00305             ? _get_vert_uv_coord(index)
00306             : Vertex2(0,0));
00307 }
00308 
00309 bool _Surface::haveVertNormals() const
00310 {
00311     return (_normals && _nindices);
00312 }
00313 
00314 bool _Surface::haveVertUVCoords() const
00315 {
00316     return (_uvCoords && _uvindices);
00317 }
00318 
00319 
00320 
00321 
00322 void _Surface::__debug()
00323 {
00324     //fprintf(stderr,"_Surface vertices:\n");
00325     for (unsigned i = 0; i < _nVert; i++)
00326         fprintf(stderr,"%f %f %f\n",
00327                 _get_vertex(i).x,_get_vertex(i).y,_get_vertex(i).z);
00328     //fprintf(stderr,"_Surface normal: %f %f %f\n",_normal.x,_normal.y,_normal.z);
00329 }
00330 
00331 
00332 

Generated on Wed Jun 28 12:24:29 2006 for esg by  doxygen 1.4.6