Updates for Plane
This commit is contained in:
parent
392871a1ee
commit
590f10a756
2 changed files with 42 additions and 46 deletions
|
@ -10,22 +10,28 @@
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
#include "basics.h"
|
|
||||||
#include "object.h"
|
|
||||||
#include "objectPlane.hh"
|
#include "objectPlane.hh"
|
||||||
|
|
||||||
|
|
||||||
|
using charles::basics::Ray;
|
||||||
|
using charles::basics::Vector4;
|
||||||
|
|
||||||
|
|
||||||
namespace charles {
|
namespace charles {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* charles::Plane::Plane --
|
* charles::Plane::Plane --
|
||||||
*/
|
*/
|
||||||
Plane::Plane()
|
Plane::Plane()
|
||||||
: mNormal(Vector3::Y),
|
: mNormal(0, 1, 0),
|
||||||
mDistance(0.0)
|
mDistance(0.0)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
|
||||||
const Vector3&
|
/*
|
||||||
|
* charles::Plane::GetNormal --
|
||||||
|
*/
|
||||||
|
const Vector4&
|
||||||
Plane::GetNormal()
|
Plane::GetNormal()
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
|
@ -33,13 +39,19 @@ Plane::GetNormal()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* charles::Plane::SetNormal --
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
Plane::SetNormal(const Vector3& normal)
|
Plane::SetNormal(const Vector4& normal)
|
||||||
{
|
{
|
||||||
mNormal = normal.normalized();
|
mNormal = basics::Normalized(normal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* charles::Plane::GetDistance --
|
||||||
|
*/
|
||||||
Double
|
Double
|
||||||
Plane::GetDistance()
|
Plane::GetDistance()
|
||||||
const
|
const
|
||||||
|
@ -48,6 +60,9 @@ Plane::GetDistance()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* charles::Plane::SetDistance --
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
Plane::SetDistance(Double distance)
|
Plane::SetDistance(Double distance)
|
||||||
{
|
{
|
||||||
|
@ -59,7 +74,7 @@ Plane::SetDistance(Double distance)
|
||||||
* charles::Plane::DoesIntersect --
|
* charles::Plane::DoesIntersect --
|
||||||
*/
|
*/
|
||||||
bool
|
bool
|
||||||
Plane::DoesIntersect(const Ray &ray,
|
Plane::DoIntersect(const Ray& ray,
|
||||||
TVector& t,
|
TVector& t,
|
||||||
Stats& stats)
|
Stats& stats)
|
||||||
const
|
const
|
||||||
|
@ -94,14 +109,14 @@ Plane::DoesIntersect(const Ray &ray,
|
||||||
stats.planeIntersectionTests++;
|
stats.planeIntersectionTests++;
|
||||||
|
|
||||||
/* The denominator for the t equation above. */
|
/* The denominator for the t equation above. */
|
||||||
Double vd = mNormal.dot(ray.direction);
|
Double vd = mNormal.Dot(ray.direction);
|
||||||
if (NearZero(vd)) {
|
if (NearZero(vd)) {
|
||||||
/* The ray is parallel to the plane. */
|
/* The ray is parallel to the plane. */
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The numerator of the equation for t above. */
|
/* The numerator of the equation for t above. */
|
||||||
Double vo = -(mNormal.dot(ray.origin) + mDistance);
|
Double vo = -(mNormal.Dot(ray.origin) + mDistance);
|
||||||
|
|
||||||
Double t0 = vo / vd;
|
Double t0 = vo / vd;
|
||||||
if (t0 < 0.0) {
|
if (t0 < 0.0) {
|
||||||
|
@ -119,33 +134,14 @@ Plane::DoesIntersect(const Ray &ray,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* charles::Plane::point_is_on_surface --
|
|
||||||
*/
|
|
||||||
bool
|
|
||||||
Plane::point_is_on_surface(const Vector3 &p)
|
|
||||||
const
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
* Plug point p into the equation for a plane:
|
|
||||||
*
|
|
||||||
* A * x + B * y + C * z + D = 0
|
|
||||||
*
|
|
||||||
* where (A, B, C) are the coordinates of the normal vector, and D is the
|
|
||||||
* distance along that vector from the origin.
|
|
||||||
*/
|
|
||||||
return NearZero(mNormal.dot(p) + mDistance);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#pragma clang diagnostic push
|
#pragma clang diagnostic push
|
||||||
#pragma clang diagnostic ignored "-Wunused-parameter"
|
#pragma clang diagnostic ignored "-Wunused-parameter"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* charles::Plane::compute_normal --
|
* charles::Plane::DoNormal --
|
||||||
*/
|
*/
|
||||||
Vector3
|
Vector4
|
||||||
Plane::compute_normal(const Vector3 &p)
|
Plane::DoNormal(const Vector4& p)
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
return mNormal;
|
return mNormal;
|
||||||
|
@ -154,6 +150,9 @@ Plane::compute_normal(const Vector3 &p)
|
||||||
#pragma clang diagnostic pop
|
#pragma clang diagnostic pop
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* charles::Plane::Write --
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
Plane::Write(std::ostream& ost)
|
Plane::Write(std::ostream& ost)
|
||||||
const
|
const
|
||||||
|
|
|
@ -8,9 +8,9 @@
|
||||||
#ifndef __OBJECT_PLANE_H__
|
#ifndef __OBJECT_PLANE_H__
|
||||||
#define __OBJECT_PLANE_H__
|
#define __OBJECT_PLANE_H__
|
||||||
|
|
||||||
#include "basics.h"
|
#include "object.hh"
|
||||||
#include "object.h"
|
#include "basics/basics.hh"
|
||||||
#include "types.hh"
|
|
||||||
|
|
||||||
namespace charles {
|
namespace charles {
|
||||||
|
|
||||||
|
@ -21,25 +21,22 @@ public:
|
||||||
/** Default constructor. Creates a plane with a normal along the Y axis. */
|
/** Default constructor. Creates a plane with a normal along the Y axis. */
|
||||||
Plane();
|
Plane();
|
||||||
|
|
||||||
const Vector3& GetNormal() const;
|
const basics::Vector4& GetNormal() const;
|
||||||
void SetNormal(const Vector3& normal);
|
void SetNormal(const basics::Vector4& normal);
|
||||||
|
|
||||||
Double GetDistance() const;
|
Double GetDistance() const;
|
||||||
void SetDistance(Double distance);
|
void SetDistance(Double distance);
|
||||||
|
|
||||||
/**
|
|
||||||
* @see charles::Object::DoesIntersect
|
|
||||||
*/
|
|
||||||
bool DoesIntersect(const Ray &ray, TVector& t, Stats& stats) const;
|
|
||||||
bool point_is_on_surface(const Vector3 &p) const;
|
|
||||||
Vector3 compute_normal(const Vector3 &p) const;
|
|
||||||
|
|
||||||
/** @see charles::Object::Write */
|
/** @see charles::Object::Write */
|
||||||
void Write(std::ostream& ost) const;
|
void Write(std::ostream& ost) const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool DoIntersect(const basics::Ray& ray, TVector& t, Stats& stats) const;
|
||||||
|
basics::Vector4 DoNormal(const basics::Vector4& p) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/** A normal vector, which specified the orientation of the plane. */
|
/** A normal vector, which specified the orientation of the plane. */
|
||||||
Vector3 mNormal;
|
basics::Vector4 mNormal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The distance from the origin along the normal vector that this plane is
|
* The distance from the origin along the normal vector that this plane is
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue