Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

octree.h

Go to the documentation of this file.
00001 /*
00002     toxic - A Global Illumination Renderer
00003     Copyright (C) 2003-2004 Francois Beaune
00004     Contact: http://toxicengine.sourceforge.net/
00005 
00006     This file is part of toxic.
00007 
00008     toxic is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     toxic is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with toxic; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #ifndef TOXIC_RENDERER_OCTREE_H
00024 #define TOXIC_RENDERER_OCTREE_H
00025 
00026 #include "common/math/aabb3.h"
00027 #include "common/math/matrix4.h"
00028 #include "common/math/octree.h"
00029 #include "common/math/point2.h"
00030 #include "common/math/point3.h"
00031 #include "common/math/vector3.h"
00032 #include "common/math/real.h"
00033 #include "common/misc/minmax.h"
00034 #include "globals.h"
00035 #include "hit.h"
00036 #include "iboundedobject.h"
00037 
00038 #include <vector>
00039 
00040 namespace toxic {
00041 
00042     class Context;
00043     class Framebuffer;
00044     class ICamera;
00045     class Ray;
00046     class Scene;
00047 
00048     const int DEFAULT_OCTREE_POP_THRESHOLD = 8;
00049     const int DEFAULT_OCTREE_MAX_SUBDIV = 6;    // at most 8^6 = 262,144 cells
00050 
00051     class Octree : public IBoundedObject {
00052     public:
00053         //! m is the object space to world space transformation matrix.
00054         Octree(
00055             const sheep::Matrix4 &m,
00056             int pop_threshold = DEFAULT_OCTREE_POP_THRESHOLD,
00057             int max_subdiv = DEFAULT_OCTREE_MAX_SUBDIV,
00058             IntersectionMask intersection_mask = INTERSECT_ALL_RAYS);
00059 
00060         //! An octree deletes its children when it is destructed.
00061         virtual ~Octree();
00062 
00063         void Insert(IBoundedObject *object);
00064 
00065         //! This method must be called only once, after the object is completely
00066         //! configured, and before it is used for the first time.
00067         virtual void Finalize(const Context &context);
00068 
00069         //! This method returns the number of objects contained into this object.
00070         //! For primitives, it returns 1. For composite objects (like the Collection
00071         //! object), it returns the number of child objects.
00072         virtual int GetObjectCount() const;
00073 
00074         //! Returns the Axis Aligned Bounding Box of this object. The AABB is
00075         //! expressed in world space.
00076         virtual const sheep::AABB3 &GetAABB() const;
00077 
00078         //! Computes the surface area of this object.
00079         virtual sheep::Real ComputeSurfaceArea() const;
00080 
00081         //! Given a point in the unit square (the set of points in [0..1]^2),
00082         //! this method computes the corresponding surface point and normal
00083         //! (both are expressed in world space).
00084         virtual void EvaluateSurface(
00085             const sheep::Point2 &input,
00086             sheep::Point3 *point,
00087             sheep::Vector3 *geometric_normal
00088         ) const;
00089 
00090         //! This method returns true if the ray intersect the object, or false otherwise.
00091         //! If 'hit' is not null, intersection data are reported through the 'hit' parameter
00092         //! (only if an intersection has been found; if no intersection has been found, the
00093         //! 'hit' parameter is left unchanged). The ray is expressed in world space.
00094         virtual bool Intersect(
00095             const Context &context,
00096             const Ray &ray,
00097             Hit *hit = 0
00098         ) const;
00099 
00100         virtual void Annotate(
00101             const Context &context,
00102             const ICamera *camera,
00103             Framebuffer *framebuffer
00104         ) const;
00105 
00106     private:
00107         typedef std::vector<IBoundedObject *> object_vector;
00108         typedef object_vector::const_iterator object_vector_const_it;
00109 
00110         typedef sheep::OctreeNode<const IBoundedObject *> octree_node;
00111         typedef sheep::Octree<const IBoundedObject *> octree;
00112 
00113         struct object_voxel_intersector {
00114             bool Intersect(const IBoundedObject *object, const sheep::AABB3 &voxel) const;
00115         };
00116 
00117         //! This visitor is invoked by the Octree::Intersect() method when the hit
00118         //! parameter is 0 (null). It is aimed at testing whether the ray touches
00119         //! an object in the octree or not. It does not search for the nearest
00120         //! object and it does not compute the intersection distance.
00121         struct octree_test_visitor {
00122             const Context &m_context;
00123             const Ray &m_ray;
00124             bool m_found;
00125 
00126             octree_test_visitor(const Context &context, const Ray &ray);
00127 
00128             bool Trace(const octree_node *node, sheep::Real t0, sheep::Real t1);
00129         };
00130 
00131         //! This visitor is invoked by the Octree::Intersect() method when the hit
00132         //! parameter is not null. It does search for the nearest intersected
00133         //! object and it does report the intersection distance.
00134         struct octree_full_visitor {
00135             const Context &m_context;
00136             const Ray &m_ray;
00137             Hit m_nearest;
00138 
00139             octree_full_visitor(const Context &context, const Ray &ray);
00140 
00141             bool Trace(const octree_node *node, sheep::Real t0, sheep::Real t1);
00142         };
00143 
00144         object_vector m_objects;
00145         int m_object_count; //!< Number of child objects. Computed by the Finalize() method.
00146 
00147         //! Octree parameters.
00148         int m_pop_threshold;
00149         int m_max_subdiv;
00150 
00151         octree *m_octree;       //!< Used only if the octree contains enough objects.
00152 
00153         sheep::AABB3 m_aabb;    //!< Bounding box of the object, expressed in world space.
00154     };
00155 
00156 #include "octree.inl"
00157 
00158 }
00159 
00160 #endif  // !TOXIC_RENDERER_OCTREE_H

Generated on Tue May 11 01:31:51 2004 for toxic by doxygen 1.3.6