/*============================================================================= Project: SharpImage Module: siPlane.cs Language: C# Author: Dan Mueller Date: $Date: 2007-07-06 10:57:00 +1000 (Fri, 06 Jul 2007) $ Revision: $Revision: 2 $ Copyright (c) Queensland University of Technology (QUT) 2007. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =============================================================================*/ using System; using System.Collections.Generic; using System.Text; using itk; namespace SharpImage.Rendering { public class siPlane { private itkPoint[] m_Points; private itkVector m_Normal; /// /// Constructor. /// /// The points describing the plane. Only the first 3 points are considered. public siPlane(itkPoint[] points) { this.m_Points = new itkPoint[3]; for (int i = 0; i < 3; i++) this.m_Points[i] = points[i]; this.ComputeNormalFromPoints(); } /// /// Get the plane surface normal. /// public itkVector Normal { get { return this.m_Normal; } } /// /// Get the points defining the plane. /// public itkPoint[] Points { get { return this.m_Points; } } /// /// Offsets the plane by the given amount. /// /// public void Offset(double offset) { // Offset each point for (int i = 0; i < 3; i++) this.m_Points[i] -= this.m_Normal * offset; } /// /// Calculates if the line described by p0 and p1 intersect with the plane. /// If (result = true) then intersection is the point of intersection. /// NOTE: This code was adapted from Simian (Joe Michael Kniss [jmk@cs.utah.edu]) /// /// /// /// /// public bool Intersect(itkPoint p0, itkPoint p1, out itkPoint intersection) { itkVector sn = this.m_Normal; // The surface normal (sn) itkPoint sp = this.m_Points[0]; // A point on the surface intersection = new itkPoint(3U); // Determine if an intersection occured itkVector diff1 = p1 - p0; itkVector diff2 = sp - p0; double dot1 = itkVector.Dot(sn, diff1); double dot2 = itkVector.Dot(sn, diff2); double result = dot2 / dot1; if ((result >= 0.0) && (result <= 1.0)) { // An intersection did occur, compute the location for (int i = 0; i < intersection.Length; i++) intersection[i] = p0[i] + result * (p1[i] - p0[i]); return true; } else { // An intersection did NOT occur return false; } } /// /// Calculates the plane surface normal from three of the points. /// private void ComputeNormalFromPoints() { this.m_Normal = new itkVector(0.0, 0.0, 1.0); itkVector diff1 = this.m_Points[1] - this.m_Points[0]; itkVector diff2 = this.m_Points[2] - this.m_Points[0]; this.m_Normal = itkVector.Cross(diff1, diff2); this.m_Normal.Normalize(); } } }