Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   Related Pages  

sglTriStruct.hpp

00001 /*****************************************************************************
00002  * SGL: A Scene Graph Library
00003  *
00004  * Copyright (C) 1997-2001  Scott McMillan   All Rights Reserved.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the Free
00018  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  *****************************************************************************
00020  *     File: sglTriStruct.hpp
00021  *   Author: Scott McMillan
00022  *  Created: 17 July 1997
00023  *  Summary: triStruct objects
00024  *****************************************************************************/
00025 
00026 #ifndef __SGL_TRI_STRUCT_HPP
00027 #define __SGL_TRI_STRUCT_HPP
00028 
00029 #include <sgldb.h>
00030 #include <list>
00031 #include <sglVertStruct.hpp>
00032 #include <sglOStream.hpp>
00033 
00034 //============================================================================
00035 
00036 template <class T>
00037 class sglTriStruct;
00038 
00039 template <class T>
00040 class sglTriList : public list< sglTriStruct<T>* > {};
00041 
00042 
00043 //============================================================================
00045 template <class T>
00046 class SGLDB_DLL_API sglTriStruct
00047 {
00048 public:
00050    int index;
00051 
00053    T v[3];
00054 
00056    bool used;
00057 
00062    unsigned int adjcount;
00063 
00064 public:
00065    typedef typename sglTriList<T>::iterator list_iterator;
00066 
00068    list_iterator adj[3];
00069 
00071    bool isDegenerate() { return ((v[0].vi == v[1].vi) ||
00072                                  (v[0].vi == v[2].vi) ||
00073                                  (v[1].vi == v[2].vi)); }
00074 
00076    bool isMember(T *vert) { return (*vert == v[0] || 
00077                                     *vert == v[1] || 
00078                                     *vert == v[2]); }
00079 
00081    T* findSameLocation(T *vert) { if (vert->vi == v[0].vi) return &v[0];
00082                                   if (vert->vi == v[1].vi) return &v[1];
00083                                   if (vert->vi == v[2].vi) return &v[2];
00084                                   return NULL; }
00085 
00087    list_iterator minadj();
00088 
00090    static T* notCommon(sglTriStruct *tri, sglTriStruct *tri2);
00091 
00093    unsigned int hash(unsigned int hashsize);
00094 
00096    bool isEqual(sglTriStruct *tri2);
00097 };
00098 
00099 //----------------------------------------------------------------------------
00100 template <class T>
00101 inline ostream& operator<<(ostream& ostrm, const sglTriStruct<T>& tri)
00102 {
00103    ostrm << endl << "sglTriStruct address " << hex << &tri << dec << endl;
00104    ostrm << "   index: " << tri.index << endl;
00105    ostrm << "   vertex0 address: " << hex << &(tri.v[0])
00106          << dec << tri.v[0] << endl;
00107    ostrm << "   vertex1 address: " << hex << &(tri.v[1])
00108          << dec << tri.v[1] << endl;
00109    ostrm << "   vertex2 address: " << hex << &(tri.v[2])
00110          << dec << tri.v[2] << endl;
00111    ostrm << "   used: " << tri.used << endl;
00112    ostrm << "   adjcount: " << tri.adjcount << endl;
00113    ostrm << "   adjtris: " << hex;
00114    if (tri.adj[0] != 0)
00115       ostrm << *(tri.adj[0]);
00116    else
00117       ostrm << "null";
00118    ostrm << " ";
00119    if (tri.adj[1] != 0)
00120       ostrm << *(tri.adj[1]);
00121    else
00122       ostrm << "null";
00123    ostrm << " ";
00124    if (tri.adj[2] != 0)
00125       ostrm << *(tri.adj[2]);
00126    else
00127       ostrm << "null";
00128    ostrm << endl;
00129    return ostrm;
00130 }
00131 
00132 //----------------------------------------------------------------------------
00133 template <class T>
00134 inline T* sglTriStruct<T>::notCommon(sglTriStruct<T> *tri, 
00135                               sglTriStruct<T> *tri2)
00136 {
00137    for (unsigned int i = 0; i < 3; i++)
00138    {
00139       if (!tri2->isMember(&(tri->v[i])))
00140          return &(tri->v[i]);
00141    }
00142    return NULL;
00143 }
00144 
00145 
00146 //----------------------------------------------------------------------------
00147 template <class T>
00148 inline sglTriStruct<T>::list_iterator sglTriStruct<T>::minadj()
00149 {
00150    unsigned int min0, min1;
00151 
00152    switch (adjcount)
00153    {
00154       case 0:
00155          return 0;
00156       case 1:
00157          if (adj[0] != 0)
00158             return adj[0];
00159          else if (adj[1] != 0)
00160             return adj[1];
00161          else
00162             return adj[2];
00163       case 2:
00164          if (adj[0] != 0)
00165          {
00166             min0 = 0;
00167             if (adj[1] != 0)
00168                min1 = 1;
00169             else
00170                min1 = 2;
00171          }
00172          else
00173          {
00174             min0 = 1;
00175             min1 = 2;
00176          }
00177          if ((*(adj[min0]))->adjcount <= (*(adj[min1]))->adjcount)
00178             return adj[min0];
00179          else
00180             return adj[min1];
00181       case 3:
00182       default:
00183          if ((*(adj[0]))->adjcount <= (*(adj[1]))->adjcount)
00184             min0 = 0;
00185          else
00186             min0 = 1;
00187          min1 = 2;
00188          if ((*(adj[min0]))->adjcount <= (*(adj[min1]))->adjcount)
00189             return adj[min0];
00190          else
00191             return adj[min1];
00192    }
00193 }
00194 
00195 //----------------------------------------------------------------------------
00196 template <class T>
00197 inline unsigned int sglTriStruct<T>::hash(unsigned int hashsize)
00198 {
00199    unsigned long val = v[0].hash() * v[1].hash() * v[2].hash();
00200    val = val & 0x7fffffff;
00201    return val % hashsize;
00202 }
00203 
00204 //----------------------------------------------------------------------------
00205 template <class T>
00206 inline bool sglTriStruct<T>::isEqual(sglTriStruct<T> *tri2)
00207 {
00208    unsigned int i,j, cnt;
00209 
00210    cnt=0;
00211    for (i=0; i<3; i++)
00212    {
00213       for (j=0; j<3; j++)
00214       {
00215          if (v[i] == tri2->v[j]) cnt++;
00216       }
00217    }
00218 
00219    if (cnt != 3) return FALSE;
00220 
00221    return TRUE;
00222 }
00223 
00224 
00225 #endif

Generated at Mon Jul 1 18:00:06 2002 for SGL by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001