MAV'RIC
/home/travis/build/lis-epfl/MAVRIC_Library/util/raytracing.hpp
00001 /*******************************************************************************
00002  * Copyright (c) 2009-2016, MAV'RIC Development Team
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  * this list of conditions and the following disclaimer.
00010  *
00011  * 2. Redistributions in binary form must reproduce the above copyright notice,
00012  * this list of conditions and the following disclaimer in the documentation
00013  * and/or other materials provided with the distribution.
00014  *
00015  * 3. Neither the name of the copyright holder nor the names of its contributors
00016  * may be used to endorse or promote products derived from this software without
00017  * specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  ******************************************************************************/
00031 
00032 /*******************************************************************************
00033  * \file raytracing.hpp
00034  *
00035  * \author MAV'RIC Team
00036  * \author Julien Lecoeur
00037  *
00038  * \brief Raytracing. Implements rays and intersection with objects (plane, sphere, cylinder... )
00039  *
00040  ******************************************************************************/
00041 
00042 
00043 #ifndef RAYTRACING_HPP_
00044 #define RAYTRACING_HPP_
00045 
00046 #include "util/matrix.hpp"
00047 #include <vector>
00048 
00049 namespace raytracing
00050 {
00051 
00052 
00053 //##################################################################################################
00054 // Ray class
00055 //##################################################################################################
00059 class Ray
00060 {
00061 public:
00068     Ray(Vector3f origin = Vector3f{0.0f, 0.0f, 0.0f}, Vector3f direction = Vector3f{1.0f, 0.0f, 0.0f});
00069 
00073     const Vector3f& origin(void) const;
00074 
00078     bool set_origin(Vector3f origin);
00079 
00083     const Vector3f& direction(void) const;
00084 
00088     bool set_direction(Vector3f origin);
00089 
00090 private:
00091     Vector3f origin_;           
00092     Vector3f direction_;        
00093 };
00094 
00095 
00096 //##################################################################################################
00097 // Intersection class
00098 //##################################################################################################
00102 class Intersection
00103 {
00104 public:
00112     Intersection(Vector3f point = Vector3f{0.0f, 0.0f, 0.0f}, Vector3f normal = Vector3f{1.0f, 0.0f, 0.0f}, float distance = 0.0f);
00113 
00117     const Vector3f& point(void) const;
00118 
00122     bool set_point(Vector3f point);
00123 
00127     const Vector3f& normal(void) const;
00128 
00132     bool set_normal(Vector3f normal);
00133 
00137     float distance(void) const;
00138 
00142     bool set_distance(float distance);
00143 
00144 private:
00145       Vector3f point_;     
00146       Vector3f normal_;    
00147       float distance_;     
00148 };
00149 
00150 
00151 //##################################################################################################
00152 // Object interface
00153 //##################################################################################################
00157 class Object
00158 {
00159 public:
00168     virtual bool intersect(const Ray& ray, Intersection& intersection) = 0;
00169 };
00170 
00171 //##################################################################################################
00172 // World class
00173 //##################################################################################################
00177 class World
00178 {
00179 public:
00183     World(void);
00184 
00192     bool add_object(Object* obj);
00193 
00203     bool intersect(const Ray& ray, Intersection& intersection, Object* object);
00204 
00205 private:
00206     std::array<Object*, 20>  objects_;
00207     uint32_t                 object_count_;
00208 };
00209 
00210 
00211 //##################################################################################################
00212 // Plane class
00213 //##################################################################################################
00217 class Plane: public Object
00218 {
00219 public:
00226     Plane(Vector3f center = Vector3f{0.0f, 0.0f, 0.0f}, Vector3f normal = Vector3f{1.0f, 0.0f, 0.0f});
00227 
00231     const Vector3f& center(void) const;
00232 
00236     bool set_center(Vector3f center);
00237 
00241     const Vector3f& normal(void) const;
00242 
00246     bool set_normal(Vector3f normal);
00247 
00256     bool intersect(const Ray& ray, Intersection& intersection);
00257 
00258 private:
00259     Vector3f center_;       
00260     Vector3f normal_;       
00261 };
00262 
00263 
00264 //##################################################################################################
00265 // Sphere class
00266 //##################################################################################################
00270 class Sphere: public Object
00271 {
00272 public:
00279     Sphere(Vector3f center = Vector3f{0.0f, 0.0f, 0.0f}, float radius = 1.0f);
00280 
00284     const Vector3f& center(void) const;
00285 
00289     bool set_center(Vector3f center);
00290 
00294     float radius(void) const;
00295 
00299     bool set_radius(float radius);
00300 
00309     bool intersect(const Ray& ray, Intersection& intersection);
00310 
00311 private:
00312     Vector3f center_;       
00313     float radius_;          
00314 };
00315 
00316 
00317 //##################################################################################################
00318 // Cylinder class
00319 //##################################################################################################
00323 class Cylinder: public Object
00324 {
00325 public:
00333     Cylinder(Vector3f center = Vector3f{0.0f, 0.0f, 0.0f}, Vector3f axis = Vector3f{0.0f, 0.0f, 1.0f}, float radius = 1.0f);
00334 
00338     const Vector3f& center(void) const;
00339 
00343     bool set_center(Vector3f center);
00344 
00348     const Vector3f& axis(void) const;
00349 
00353     bool set_axis(Vector3f axis);
00354 
00358     float radius(void) const;
00359 
00363     bool set_radius(float radius);
00364 
00373     bool intersect(const Ray& ray, Intersection& intersection);
00374 
00375 private:
00376     Vector3f center_;       
00377     Vector3f axis_;         
00378     float radius_;          
00379     // float height_;          ///< Height of cylinder
00380 };
00381 
00382 }
00383 
00384 #endif /* RAYTRACING_HPP_ */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines