PolyEvaluator.cpp

Go to the documentation of this file.
00001 // Copyright (C) 2005 Dave Griffiths
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 
00017 #include "PolyEvaluator.h"
00018 #include "PolyPrimitive.h"
00019 #include "Geometry.h"
00020 
00021 using namespace Fluxus;
00022 
00023 PolyEvaluator::PolyEvaluator(const PolyPrimitive *prim) :
00024 m_Prim(prim)
00025 {
00026     assert(m_Prim!=NULL);
00027 }
00028 
00029 PolyEvaluator::~PolyEvaluator()
00030 {
00031 }
00032 
00034 
00035 Evaluator::Point PolyEvaluator::ClosestPoint(const dVector &position)
00036 {
00037     return Point();
00038 }
00039 
00041 
00042 bool PolyEvaluator::IntersectLine(const dVector &start, const dVector &end, vector<Point> &points)
00043 {
00044     switch (m_Prim->GetType())
00045     {
00046         case PolyPrimitive::TRISTRIP: return IntersectTriStrip(start,end,points); break;
00047         case PolyPrimitive::QUADS: return IntersectQuads(start,end,points); break;
00048         case PolyPrimitive::TRILIST: return IntersectTriList(start,end,points); break;
00049         case PolyPrimitive::TRIFAN: return IntersectTriFan(start,end,points); break;
00050         case PolyPrimitive::POLYGON: return IntersectPolygon(start,end,points); break;
00051     };
00052     
00053     return false;
00054 }
00055 
00056 bool PolyEvaluator::IntersectTriStrip(const dVector &start, const dVector &end, vector<Point> &points)
00057 {
00058     return false;
00059 }
00060 
00061 bool PolyEvaluator::IntersectQuads(const dVector &start, const dVector &end, vector<Point> &points)
00062 {
00063     dVector bary;
00064     bool found=false;
00065     unsigned int i=0;
00066     while(i<m_Prim->Size())
00067     {
00068         unsigned int i1=i++;
00069         unsigned int i2=i++;
00070         unsigned int i4=i++;
00071         unsigned int i3=i++;
00072         
00073         if (IntersectLineTriangle(start,end,m_Prim->GetData<dVector>("p",i1),
00074                                         m_Prim->GetData<dVector>("p",i2),
00075                                         m_Prim->GetData<dVector>("p",i3),bary))
00076         {
00077             points.push_back(InterpolatePData(bary,i1,i2,i3));
00078             found=true;
00079         }
00080         else if (IntersectLineTriangle(start,end,m_Prim->GetData<dVector>("p",i2),
00081                                                  m_Prim->GetData<dVector>("p",i3),
00082                                                  m_Prim->GetData<dVector>("p",i4),bary))
00083         {
00084             points.push_back(InterpolatePData(bary,i2,i3,i4));
00085             found=true;
00086         }
00087     }
00088 
00089     return found;
00090 }
00091 
00092 bool PolyEvaluator::IntersectTriList(const dVector &start, const dVector &end, vector<Point> &points)
00093 {   
00094     dVector bary;
00095     bool found=false;
00096     unsigned int i=0;
00097     while(i<m_Prim->Size())
00098     {
00099         unsigned int i1=i++;
00100         unsigned int i2=i++;
00101         unsigned int i3=i++;
00102         
00103         if (IntersectLineTriangle(start,end,m_Prim->GetData<dVector>("p",i1),
00104                                             m_Prim->GetData<dVector>("p",i2),
00105                                             m_Prim->GetData<dVector>("p",i3),bary))
00106         {
00107             points.push_back(InterpolatePData(bary,i1,i2,i3));
00108             found=true;
00109         }
00110     }
00111 
00112     return found;
00113 }
00114 
00115 bool PolyEvaluator::IntersectTriFan(const dVector &start, const dVector &end, vector<Point> &points)
00116 {
00117     return false;
00118 }
00119 
00120 bool PolyEvaluator::IntersectPolygon(const dVector &start, const dVector &end, vector<Point> &points)
00121 {
00122     return false;
00123 }
00124 
00126 
00127 // todo - extend to arbitrary numbers of elements, and put in Evaluator
00128 Evaluator::Point PolyEvaluator::InterpolatePData(dVector bary, unsigned int i1, unsigned int i2, unsigned int i3)
00129 {
00130     Evaluator::Point point;
00131     vector<string> names;
00132     m_Prim->GetDataNames(names);
00133 
00134     for (vector<string>::iterator i=names.begin(); i!=names.end(); ++i)
00135     {
00136         char type=0;
00137         unsigned int size=0;
00138         m_Prim->GetDataInfo(*i, type, size);
00139         
00140         Blend *blend;
00141         
00142         switch(type)
00143         {
00144             case 'f': blend = new TypedBlend<float>('f',m_Prim->GetData<float>(*i,i1)*bary.x +
00145                                                     m_Prim->GetData<float>(*i,i2)*bary.y +
00146                                                     m_Prim->GetData<float>(*i,i3)*bary.z); break;
00147             case 'v': blend = new TypedBlend<dVector>('v',m_Prim->GetData<dVector>(*i,i1)*bary.x +
00148                                                   m_Prim->GetData<dVector>(*i,i2)*bary.y +
00149                                                   m_Prim->GetData<dVector>(*i,i3)*bary.z); break;
00150             case 'c': blend = new TypedBlend<dColour>('c',m_Prim->GetData<dColour>(*i,i1)*bary.x +
00151                                                   m_Prim->GetData<dColour>(*i,i2)*bary.y +
00152                                                   m_Prim->GetData<dColour>(*i,i3)*bary.z); break;
00153             case 'm': blend = new TypedBlend<dMatrix>('m',m_Prim->GetData<dMatrix>(*i,i1)*bary.x +
00154                                                   m_Prim->GetData<dMatrix>(*i,i2)*bary.y +
00155                                                   m_Prim->GetData<dMatrix>(*i,i3)*bary.z); break;
00156             default: 
00157                 cerr<<"unknown pdata type in PolyEvaluator::InterpolatePData: "<<type<<endl; 
00158                 assert(0); 
00159                 break;
00160         };
00161         
00162         blend->m_Name=*i;
00163         
00164         point.m_Blends.push_back(blend);
00165     }
00166     
00167     return point;
00168 } 
00169 

Generated on Wed Sep 17 21:16:30 2008 for The Fluxus Renderer (libfluxus) by  doxygen 1.5.1