BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Arr_geometry_traits/Arr_plane_3.h
Go to the documentation of this file.
00001 // Copyright (c) 2006  Tel-Aviv University (Israel).
00002 // All rights reserved.
00003 //
00004 // This file is part of CGAL (www.cgal.org); you may redistribute it under
00005 // the terms of the Q Public License version 1.0.
00006 // See the file LICENSE.QPL distributed with CGAL.
00007 //
00008 // Licensees holding a valid commercial license may use this file in
00009 // accordance with the commercial license agreement provided with the software.
00010 //
00011 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 //
00014 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Arrangement_on_surface_2/include/CGAL/Arr_geometry_traits/Arr_plane_3.h $
00015 // $Id: Arr_plane_3.h 42535 2008-03-20 16:46:25Z spion $
00016 // 
00017 //
00018 // Author(s)     : Efi Fogel          <efif@post.tau.ac.il>
00019 
00020 #ifndef CGAL_ARR_PLANE_3_h
00021 #define CGAL_ARR_PLANE_3_h
00022 
00031 #include <CGAL/basic.h>
00032 #include <iostream>
00033 
00034 #include <CGAL/Kernel/solve.h>
00035 
00036 CGAL_BEGIN_NAMESPACE
00037 
00039 template <class Kernel_>
00040 class Arr_plane_3 {
00041 public:
00042   typedef Kernel_                       Kernel;
00043   typedef Arr_plane_3<Kernel>           Self;
00044   typedef typename Kernel::Point_2      Point_2;
00045   typedef typename Kernel::Point_3      Point_3;
00046   typedef typename Kernel::Vector_3     Vector_3;
00047   typedef typename Kernel::Direction_3  Direction_3;
00048   typedef typename Kernel::Line_3       Line_3;
00049   typedef typename Kernel::FT           FT;
00050 
00051 private:
00053   FT m_a;
00054 
00056   FT m_b;
00057 
00059   FT m_c;
00060   
00061 public:
00063   Arr_plane_3() : m_a(0), m_b(0), m_c(0) {}
00064 
00066   Arr_plane_3(int a, int b, int c) : m_a(a), m_b(b), m_c(c) {}
00067 
00069   Arr_plane_3(typename Kernel::Plane_3 p)
00070   {
00071     CGAL_precondition_code(Kernel kernel;);
00072     CGAL_precondition_code(typename Kernel::Point_3 orig = kernel.construct_point_3_object()(ORIGIN););
00073     CGAL_precondition(kernel.has_on_3_object()(p, orig));
00074 
00075     m_a = p.a(); m_b = p.b(); m_c = p.c() ;
00076   }
00077 
00079   Arr_plane_3(const Point_3 & p, const Point_3 & r)
00080   {
00081     FT prx = r.x() - p.x();
00082     FT pry = r.y() - p.y();
00083     FT prz = r.z() - p.z();
00084     m_a = r.y() * prz - pry * r.z();
00085     m_b = r.z() * prx - prz * r.x();
00086     m_c = r.x() * pry - prx * r.y();
00087   }
00088 
00090   const FT & a() const { return m_a; }
00091 
00093   const FT & b() const { return m_b; }
00094 
00096   const FT & c() const { return m_c; }
00097   
00102   FT operator[](unsigned int i) const
00103   {
00104     CGAL_assertion(i < 3);
00105     return (i == 0) ? m_a : ((i == 1) ? m_b : m_c);
00106   }
00107 
00108   bool equal(Self plane) const
00109   {
00110     return ((a() == plane.a()) &&
00111             (b() == plane.b()) &&
00112             (c() == plane.c()));
00113   }
00114 
00116   operator typename Kernel::Plane_3 () const
00117   {
00118     Kernel kernel;
00119     return kernel.construct_plane_3_object() (m_a, m_b, m_c, 0);
00120   }
00121 
00128   Point_2 to_2d(const Point_3 & p) const
00129   {
00130     Kernel kernel;
00131     typename Kernel::Plane_3 base_plane(m_a, m_b, m_c, 0);
00132     Vector_3 v1 = kernel.construct_base_vector_3_object()(base_plane, 1);    
00133     Vector_3 v2 = kernel.construct_base_vector_3_object()(base_plane, 2);    
00134     Vector_3 v3 = kernel.construct_orthogonal_vector_3_object()(base_plane);
00135 
00136     FT denom = v2[1]*v3[0]*v1[2] - v2[0]*v3[1]*v1[2] + v3[2]*v2[0]*v1[1] +
00137                v2[2]*v3[1]*v1[0] - v3[0]*v2[2]*v1[1] - v2[1]*v3[2]*v1[0];
00138     FT x = - (v2[1]*v3[2]*p[0] - v2[1]*v3[0]*p[2] + v3[0]*v2[2]*p[1] +
00139               v2[0]*v3[1]*p[2] - v3[2]*v2[0]*p[1] - v2[2]*v3[1]*p[0]) / denom;
00140     FT y = (v1[1]*v3[2]*p[0] - v1[1]*v3[0]*p[2] - v3[1]*p[0]*v1[2] +
00141             v3[1]*v1[0]*p[2] + p[1]*v3[0]*v1[2] - p[1]*v3[2]*v1[0]) / denom;
00142     
00143     return Point_2(x, y);
00144   }
00145 
00154   Point_3 to_3d(const Point_2 & p_2, unsigned int i) const
00155   {
00156     CGAL_assertion(i < 3);
00157 
00158 #if 0
00159     std::cout << "(a, b, c): " << a() << "," << b() << "," << c()
00160               << std::endl;
00161 #endif
00162     
00163     if (i == 0) {
00164       CGAL_assertion(m_a != 0);
00165       FT y = p_2.x();
00166       FT z = p_2.y();
00167       FT x = -(m_b * y + m_c * z) / m_a;
00168       Point_3 p_3(x, y, z);
00169       return p_3;
00170     }
00171 
00172     if (i == 1) {
00173       CGAL_assertion(m_b != 0);
00174       FT z = p_2.x();
00175       FT x = p_2.y();
00176       FT y = -(m_a * x + m_c * z) / m_b;
00177       Point_3 p_3(x, y, z);
00178       return p_3;
00179     }
00180 
00181     // if (i == 2) return Base::to_3d(p_2);
00182     CGAL_assertion(m_c != 0);
00183     FT x = p_2.x();
00184     FT y = p_2.y();
00185     FT z = -(m_a * x + m_b * y) / m_c;
00186     Point_3 p_3(x, y, z);
00187     return p_3;
00188   }
00189 
00195   Oriented_side oriented_side(const Point_3 & p) const
00196   {
00197     return CGAL_NTS sign(m_a*p.x() + m_b*p.y() + m_c*p.z());
00198   }
00199 };
00200 
00207 template <class Kernel>
00208 CGAL::Object intersect(const Arr_plane_3<Kernel> & plane1,
00209                        const Arr_plane_3<Kernel> & plane2)
00210 {
00211   typedef typename Kernel::Point_3      Point_3;
00212   typedef typename Kernel::Direction_3  Direction_3;
00213   typedef typename Kernel::Line_3       Line_3;
00214   typedef typename Kernel::FT           FT;
00215 
00216   // We know that the plane goes throgh the origin
00217   const FT & a1 = plane1.a();
00218   const FT & b1 = plane1.b();
00219   const FT & c1 = plane1.c();
00220 
00221   const FT & a2 = plane2.a();
00222   const FT & b2 = plane2.b();
00223   const FT & c2 = plane2.c();
00224 
00225   FT det = a1*b2 - a2*b1;
00226   if (det != 0) {
00227     Point_3 is_pt = Point_3(0, 0, 0, det);
00228     Direction_3 is_dir = Direction_3(b1*c2 - c1*b2, a2*c1 - a1*c2, det);
00229     return make_object(Line_3(is_pt, is_dir));
00230   }
00231 
00232   det = a1*c2 - a2*c1;
00233   if (det != 0) {
00234     Point_3 is_pt = Point_3(0, 0, 0, det);
00235     Direction_3 is_dir = Direction_3(c1*b2 - b1*c2, det, a2*b1 - a1*b2);
00236     return make_object(Line_3(is_pt, is_dir));
00237   }
00238   det = b1*c2 - c1*b2;
00239   if (det != 0) {
00240     Point_3 is_pt = Point_3(0, 0, 0, det);
00241     Direction_3 is_dir = Direction_3(det, c1*a2 - a1*c2, a1*b2 - b1*a2);
00242     return make_object(Line_3(is_pt, is_dir));
00243   }
00244 
00245   // degenerate case
00246   return make_object(plane1);
00247 }
00248 
00256 template <class Kernel>
00257 typename Kernel::Point_2
00258 construct_projected_xy_point(const Arr_plane_3<Kernel> & plane,
00259                              const typename Kernel::Point_3 & p)
00260 {
00261   return plane.to_2d(p);
00262 }
00263 
00269 template <class Kernel>
00270 inline std::ostream & operator<<(std::ostream & os,
00271                                  const Arr_plane_3<Kernel> & plane)
00272 {
00273   os << plane[0] << ", " << plane[1] << ", " << plane[2];
00274   return os;
00275 }  
00276 
00277 CGAL_END_NAMESPACE
00278 
00279 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines