ShapeSet.cpp

Go to the documentation of this file.
00001 /**<!-------------------------------------------------------------------->
00002    @file   ShapeSet.cpp
00003    @author Travis Fischer (fisch0920@gmail.com)
00004    @author Matthew Jacobs (jacobs.mh@gmail.com)
00005    @date   Fall 2008
00006    
00007    @brief
00008       Represents a collection of shapes which all have the same transformation
00009    <!-------------------------------------------------------------------->**/
00010 
00011 #include "ShapeSet.h"
00012 #include <kdTreeAccel.h>
00013 #include <NaiveSpatialAccel.h>
00014 #include <ResourceManager.h>
00015 #include <GL/gl.h>
00016 #include <QtCore>
00017 
00018 ShapeSet::~ShapeSet() {
00019    if (m_ownMemory) {
00020       FOREACH(PrimitiveListIter, m_primitives, iter) {
00021          Shape *shape = *iter;
00022          ASSERT(shape);
00023          
00024          safeDelete(shape);
00025       }
00026    }
00027    
00028    safeDelete(m_spatialAccel);
00029 }
00030 
00031 void ShapeSet::init() {
00032    init(true);
00033 }
00034 
00035 void ShapeSet::init(bool initChildren) {
00036    if (NULL == m_spatialAccel) {
00037       //ResourceManager::log.info << "ShapeSet::init " << 
00038       //   (initChildren ? "initChildren=true" : "initChildren=false") << 
00039       //   ", " << m_primitives.size() << endl;
00040       
00041       AABB aabb;
00042       
00043       if (initChildren) {
00044          ASSERT(m_material);
00045          FOREACH(PrimitiveListIter, m_primitives, iter) {
00046             Shape *s = *iter;
00047             ASSERT(s);
00048             
00049             ASSERT(s->getMaterial());
00050             //s->setMaterial(m_material);
00051             
00052             s->init();
00053             ASSERT(s->getAABB().isValid());
00054          }
00055       }
00056       
00057       const std::string &accelType = getValue<std::string>("spatialAccel", 
00058                                                            "kdTree");
00059       
00060       if (accelType == "kdTree") {
00061          m_spatialAccel = new kdTreeAccel();
00062       } else {
00063          ASSERT(accelType == "naive");
00064          
00065          m_spatialAccel = new NaiveSpatialAccel();
00066       }
00067       
00068       m_spatialAccel->setGeometry(reinterpret_cast<IntersectableList*>(&m_primitives));
00069       m_spatialAccel->inherit(*this);
00070       m_spatialAccel->init();
00071       
00072       m_objSpaceAABB = m_spatialAccel->getAABB();
00073       
00074       // transform AABB into parent coordinate system
00075       Transformable::init();
00076       
00077       //ResourceManager::log.info << "ShapeSet: " << m_aabb << endl;
00078    }
00079 }
00080 
00081 void ShapeSet::preview() {
00082    Transformable::preview();
00083    
00084    bool enableAccelPreview = 
00085       ResourceManager::getValue<bool>("enableAccelPreview", true);
00086    
00087    if (enableAccelPreview && m_spatialAccel)
00088       m_spatialAccel->preview();
00089    else m_aabb.preview();
00090    
00091    FOREACH(PrimitiveListIter, m_primitives, iter) {
00092       Shape *shape = *iter;
00093       ASSERT(shape);
00094       
00095       glPushMatrix();
00096       shape->preview();
00097       glPopMatrix();
00098    }
00099 }
00100 
00101 real_t ShapeSet::getIntersection(const Ray &ray, SurfacePoint &pt) {
00102    ASSERT(m_spatialAccel);
00103    
00104    // TODO: creating a new Ray here is rather inefficient...
00105    Point3  P;
00106    Vector3 D;
00107    _transformRayWorldToObj(ray, P, D);
00108    
00109    return m_spatialAccel->getIntersection(Ray(P, D), pt);
00110 }
00111 
00112 bool ShapeSet::intersects(const Ray &ray, real_t tMax) {
00113    ASSERT(m_spatialAccel);
00114    
00115    // TODO: creating a new Ray here is rather inefficient...
00116    Point3  P;
00117    Vector3 D;
00118    _transformRayWorldToObj(ray, P, D);
00119    
00120    return m_spatialAccel->intersects(Ray(P, D), tMax);
00121 }
00122 
00123 real_t ShapeSet::_getSurfaceArea() {
00124    real_t area = 0;
00125    
00126    FOREACH(PrimitiveListIter, m_primitives, iter) {
00127       Shape *shape = *iter;
00128       ASSERT(shape);
00129       
00130       area += shape->getSurfaceArea();
00131    }
00132    
00133    return area;
00134 }
00135 
00136 void ShapeSet::_getUV(SurfacePoint &pt) const {
00137    ASSERT(0 && "Shouldn't be here\n");
00138 }
00139 
00140 void ShapeSet::_getGeometricNormal(SurfacePoint &pt) const {
00141    ASSERT(0 && "Shouldn't be here\n");
00142 }
00143 

Generated on 28 Feb 2009 for Milton by doxygen 1.5.6