include/cinder/Plane.h
Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2011, The Cinder Project, All rights reserved.
00003  This code is intended for use with the Cinder C++ library: http://libcinder.org
00004  
00005  Portions of this code (C) Paul Houx
00006  All rights reserved.
00007 
00008  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
00009  the following conditions are met:
00010 
00011     * Redistributions of source code must retain the above copyright notice, this list of conditions and
00012     the following disclaimer.
00013     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
00014     the following disclaimer in the documentation and/or other materials provided with the distribution.
00015 
00016  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00017  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00018  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00019  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00020  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00021  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00022  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00023  POSSIBILITY OF SUCH DAMAGE.
00024 */
00025 
00026 #pragma once
00027 
00028 #include "cinder/Vector.h"
00029 #include <iostream>
00030 
00031 namespace cinder {
00032 
00033 template<typename T>
00034 class Plane
00035 {
00036   public:
00037     Plane() {}
00038     Plane( const Vec3<T> &v1, const Vec3<T> &v2, const Vec3<T> &v3 );
00039     Plane( const Vec3<T> &point, const Vec3<T> &normal );
00040     Plane( T a, T b, T c, T d );
00041 
00043     void    set( const Vec3<T> &v1, const Vec3<T> &v2, const Vec3<T> &v3 );
00045     void    set( const Vec3<T> &point, const Vec3<T> &normal );
00047     void    set( T a, T b, T c, T d );
00048 
00049     Vec3<T>         getPoint() const { return mNormal * mDistance; };
00050     const Vec3<T>&  getNormal() const { return mNormal; };
00051     float           getDistance() const { return mDistance; }
00052     float           distance( const Vec3f &p ) const { return (mNormal.dot(p) - mDistance); };
00053 
00054     Vec3<T>         reflectPoint( const Vec3<T> &p ) const { return mNormal * distance( p ) * -2 + p; }
00055     Vec3<T>         reflectVector( const Vec3<T> &v ) const { return mNormal * mNormal.dot(v) * 2 - v; }
00056 
00057     Vec3<T>     mNormal;
00058     T           mDistance;
00059 };
00060 
00061 typedef Plane<float>    Planef;
00062 typedef Plane<double>   Planed;
00063 
00064 template<typename T>
00065 std::ostream& operator<<( std::ostream &o, const Plane<T> &p )
00066 {
00067     return o << "(" << p.mNormal << ", " << p.mDistance << ")";
00068 }
00069 
00070 
00071 class PlaneExc : public std::exception {
00072  public:
00073     virtual const char* what() const throw() { return "Invalid parameters specified"; }
00074 };
00075 
00076 } // namespace cinder