proj home

Files   Classes   Functions   Hierarchy  

vrmlconvert.cpp

Go to the documentation of this file.
00001 #include <graphmisc.h>
00002 #include <vrmlconvert.h>
00003 
00004 // Presently diffuseColor is only attribute supported.
00005 boolc vrmlconvert::equalattributes
00006 (
00007   vrmlshape const & s, 
00008   vrmlshaperaw const & raw 
00009 ) const
00010 {
00011   if (s.diffuseColor[0] != (float) raw.diffuseColor[0] )
00012     return false;
00013 
00014   if (s.diffuseColor[1] != (float) raw.diffuseColor[1] )
00015     return false;
00016 
00017   if (s.diffuseColor[2] != (float) raw.diffuseColor[2] )
00018     return false;
00019 
00020   return true;
00021 }
00022 
00023 
00024 void vrmlconvert::writeattributes
00025 (
00026   vrmlshape & s, 
00027   vrmlshaperaw const & raw 
00028 ) const
00029 {
00030   s.diffuseColor[0] = raw.diffuseColor[0];
00031   s.diffuseColor[1] = raw.diffuseColor[1];
00032   s.diffuseColor[2] = raw.diffuseColor[2];
00033 }
00034 
00035 
00036 boolc vrmlconvert::validcoord
00037 ( 
00038   vector<uint> & coord1,
00039   vector<int> const & coord2,
00040   int const N
00041 ) const 
00042 {
00043   assert(!coord2.empty());
00044   if (coord2.empty())
00045     return false;
00046 
00047   uintc imax = coord2.size();
00048   for (uint i=0; i<imax; ++i)
00049   {
00050 
00051     if ( ((i+1)%4)==0 )
00052     {
00053       assert(coord2[i]==-1);
00054       if (coord2[i]!=-1)
00055         return false;
00056     }
00057     else
00058     {
00059       // 0 <= coord2[i] < N
00060 
00061       assert( coord2[i]<N );
00062       if (coord2[i]>=N)
00063         return false;
00064 
00065       assert(coord2[i]>=0);
00066       if (coord2[i]<0)
00067         return false;
00068 
00069       coord1.push_back( coord2[i] );
00070     }
00071   }
00072 
00073   assert( (coord1.size()%3)==0 );
00074   if (! ((coord1.size()%3)==0) )
00075     return false;
00076 
00077   return true;
00078 }
00079 
00080 
00081 void vrmlconvert::writepointsandnormals
00082 (
00083   vrmlshape & s, 
00084   vrmlshaperaw const & raw 
00085 ) const
00086 {
00087   assert(raw.point.size()==raw.normal.size());
00088 
00089   uintc N = raw.point.size();
00090   if (N!=raw.normal.size())
00091     return;
00092 
00093   assert(N!=0);
00094   if (N==0)
00095     return;
00096 
00097   assert((N%3)==0);
00098   if ((N%3)!=0)
00099     return;
00100 
00101   vector<uint> coord;
00102   if (! validcoord(coord,raw.coordIndex,N/3) )
00103     return;
00104 
00105   uintc imax = coord.size();
00106   uint index;
00107   for (uint i=0; i<imax; ++i)
00108   {
00109     index = coord[i]*3;
00110     s.point.push_back( raw.point[index] );
00111     s.point.push_back( raw.point[index+1] );
00112     s.point.push_back( raw.point[index+2] );
00113     s.normal.push_back( raw.normal[index] );
00114     s.normal.push_back( raw.normal[index+1] );
00115     s.normal.push_back( raw.normal[index+2] );
00116   }
00117 }
00118 
00119 boolc vrmlconvert::hastriangles
00120 (
00121   vector< vrmlshaperaw > const & v 
00122 ) const
00123 {
00124 cout << SHOW(v.size()) << endl;
00125   if (v.empty())
00126     return false;
00127 
00128   // Determine if there are shapes to write. 
00129   uintc imax = v.size();
00130   for (uint i=0; i<imax; ++i)
00131   {
00132     if(v[i].istriangles)
00133       return true;
00134   }
00135 
00136   return false;
00137 }
00138 
00139 boolc vrmlconvert::eval
00140 (
00141   vector< vrmlshape > & s,
00142   vrmlshapeparse const & p
00143 ) const
00144 {
00145   vector< vrmlshaperaw > const & v(p.vshp);
00146 
00147   if(!hastriangles(v))
00148     return false;
00149 
00150   // Iterate writing shapes.
00151   vrmlshape * curr = 0;
00152 
00153   uintc imax = v.size();
00154   for (uint i=0; i<imax; ++i)
00155   {
00156     if (!v[i].istriangles)
00157       continue;
00158 
00159     if (s.empty())
00160     {
00161       s.push_back(vrmlshape());
00162       curr = & s.back();
00163       writepointsandnormals(*curr,v[i]); 
00164       writeattributes(*curr,v[i]);
00165     }
00166     else
00167     {
00168       curr = & s.back();
00169       if (equalattributes(*curr,v[i]))
00170       {
00171         writepointsandnormals(*curr,v[i]); 
00172       }
00173       else
00174       {
00175         s.push_back(vrmlshape());
00176         writepointsandnormals(*curr,v[i]); 
00177         writeattributes(*curr,v[i]);
00178       }
00179     }
00180   }
00181 
00182   return true;
00183 }
00184 
00185 
00186 
00187 
00188 

Generated on Fri Mar 4 00:49:32 2011 for Chelton Evans Source by  doxygen 1.5.8