//------------------------------------------------------------------------------
// Lamp : Open source game middleware
// Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//------------------------------------------------------------------------------

/** @file
 * 平面交差実装
 * @author Junpee
 */

#include "LampBasic.h"
#include "Geometry/Intersection/PlaneIntersection.h"

namespace Lamp{

//------------------------------------------------------------------------------
// 点
//------------------------------------------------------------------------------
// 点交差
bool PlaneIntersection::intersect(
	const Plane& plane, const Vector3& point, float range){
	Assert(false);
	return false;
}
//------------------------------------------------------------------------------
// 平面
//------------------------------------------------------------------------------
// 平面交差
bool PlaneIntersection::intersect(const Plane& plane0, const Plane& plane1){
	// 面が平行でなければ交差している
	Vector3 cross = plane0.getNormal().crossProduct(plane1.getNormal());
	return (cross.getSquaredLength() > Math::epsilon);
}
//------------------------------------------------------------------------------
// レイ
//------------------------------------------------------------------------------
// レイ交差
bool PlaneIntersection::intersect(const Plane& plane, const Ray& ray){
	const Vector3& normal = plane.getNormal();
	float denominator = ray.getDirection().dotProduct(normal);
	float distance = plane.getDistance(ray.getOrigin());
	if(Math::abs(denominator) <= Math::epsilon){
		// 面に平行なレイは原点が面に接していれば交差
		return (Math::abs(distance) <= Math::epsilon);
	}
	float t = ((-distance) / denominator);
	// tが0以上なら交差
	return (t >= 0.f);
}
//------------------------------------------------------------------------------
// セグメント
//------------------------------------------------------------------------------
// セグメント交差
bool PlaneIntersection::intersect(const Plane& plane, const Segment& segment){
	const Vector3& normal = plane.getNormal();
	float denominator = segment.getDirection().dotProduct(normal);
	float distance = plane.getDistance(segment.getOrigin());
	if(Math::abs(denominator) <= Math::epsilon){
		// 面に平行なセグメントは原点が面に接していれば交差
		return (Math::abs(distance) <= Math::epsilon);
	}
	float t = ((-distance) / denominator);
	// tが0以上1以下なら交差
	return ((t >= 0.f) && (t <= 1.f));
}
//------------------------------------------------------------------------------
// 球
//------------------------------------------------------------------------------
// 球交差
bool PlaneIntersection::intersect(const Plane& plane, const Sphere& sphere){
	// 面と球の距離が球半径以内なら交差
	float distance = plane.getDistance(sphere.getCenter());
	return (Math::abs(distance) <= sphere.getRadius());
}
//------------------------------------------------------------------------------
// 三角
//------------------------------------------------------------------------------
// 三角交差
bool PlaneIntersection::intersect(const Plane& plane, const Triangle& triangle){
	Assert(false);
	return false;
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
//------------------------------------------------------------------------------
