polyhedron.cpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002     
00003     polyhedron.cpp -- See declarations in polyhedron.hpp.
00004 
00005     This file is a part of the Arageli library.
00006 
00007     Copyright (C) Sergey S. Lyalin, 2006
00008     University of Nizhni Novgorod, Russia, 2005--2006
00009 
00010 *****************************************************************************/
00011 
00018 #include "config.hpp"
00019 
00020 #if !defined(ARAGELI_INCLUDE_CPP_WITH_EXPORT_TEMPLATE) || \
00021     defined(ARAGELI_INCLUDE_CPP_WITH_EXPORT_TEMPLATE_polyhedron)
00022 
00023 // REFERENCE ADDITIONAL HEADERS HERE
00024 
00025 #include "frwrddecl.hpp"
00026 #include "polyhedron.hpp"
00027 #include "vector.hpp"
00028 #include "matrix.hpp"
00029 
00030 
00031 namespace Arageli
00032 {
00033 
00034 
00035 template <typename T, typename M, typename C, typename CFG>
00036 template <typename D>
00037 polyhedron<T, M, C, CFG>::polyhedron (const D& dim, const fromspace_t&)
00038 {
00039     cone_m.inequaton_matrix().assign_eye(1, dim + 1);   // one inequation x[0] >= 0
00040     cone_m.equation_matrix().assign(0, dim + 1);
00041     set_normal_implicit();
00042 }
00043 
00044 
00045 template <typename T, typename M, typename C, typename CFG>
00046 template <typename M1>
00047 polyhedron<T, M, C, CFG>::polyhedron (const M1& ineqmat, const fromineq_t&)
00048 :   cone_m(ineqmat, fromineq)
00049 {
00050     ARAGELI_ASSERT_0(cone_m.inequation_matrix().ncols() > 0);
00051 
00052     // Add one additional inequation to cut all points with x[0] < 0.
00053     // May be it's superfluous, but the determination of superfluity is expensive.
00054     
00055     cone_m.inequation_matrix().insert_row
00056     (
00057         cone_m.inequation_matrix().nrows(),
00058         Arageli::null<inequation_element_type>()
00059     );
00060     
00061     cone_m.inequation_matrix(cone_m.inequation_matrix().nrows(), 0) =
00062         unit<inequation_element_type>();
00063 }
00064 
00065     
00066 template <typename T, typename M, typename C, typename CFG>
00067 template <typename M1>
00068 polyhedron<T, M, C, CFG>::polyhedron (const M1& vert, const fromivert_t&)
00069 {
00070     cone_m.generatrix_matrix() = vert;
00071     cone_m.generatrix_matrix().insert_col
00072         (0, vector<T, false>(vert.nrows(), unit<T>(), fromval));
00073     cone_m.basis_matrix().resize(0, cone_m.generatrix_matrix().ncols());
00074     output_aligned(std::cout << "\ncone_m.generatrix_matrix() = \n", cone_m.generatrix_matrix());
00075 }
00076 
00077 
00078 template <typename T, typename M, typename C, typename CFG>
00079 template <typename M1>
00080 polyhedron<T, M, C, CFG>::polyhedron (const M1& vert, const fromvert_t&)
00081 {
00082     // WARNING! This is too expensive!
00083     // TODO: Make it faster!
00084 
00085     vector<T, false> b;
00086     b.reserve(vert.nrows());
00087     cone_m.generatrix_matrix().resize(0, vert.ncols());
00088 
00089     for(size_type i = 0; i < vert.nrows(); ++i)
00090     {
00091         T lcmval = unit<T>();
00092         for(size_type j = 0; j < vert.ncols(); ++j)
00093             lcmval = lcm(lcmval, vert(i, j).denominator());
00094 
00095         cone_m.generatrix_matrix().insert_row
00096             (cone_m.generatrix_matrix().nrows(), lcmval*vert.copy_row(i));
00097         b.push_back(lcmval);
00098     }
00099 
00100     cone_m.generatrix_matrix().insert_col(0, b);
00101     cone_m.basis_matrix().resize(0, cone_m.generatrix_matrix().ncols());
00102 }
00103 
00104 
00105 template <typename T, typename M, typename C, typename CFG>
00106 sideset polyhedron<T, M, C, CFG>::sides () const
00107 {
00108     matrix<T> a = cone_m.inequation_matrix(), f, q, e;
00109     skeleton(a, f, q, e);
00110 
00111     //output_aligned(std::cout << "\nq = \n", q);
00112 
00113     // we are intrested only in q matrix
00114 
00115     vector<vector<std::size_t> > res;
00116 
00117     for(std::size_t i = 0; i < q.ncols(); ++i)
00118     {
00119         vector<std::size_t> curres;
00120 
00121         for(std::size_t j = 0; j < q.nrows(); ++j)
00122             if(is_null(q(j, i)))curres.push_back(j);
00123 
00124         ARAGELI_ASSERT_1(!curres.is_empty());
00125         
00126         res.push_back(curres);
00127     }
00128 
00129     return sideset(res);
00130 }
00131 
00132 
00133 
00134 
00135 template
00136 <
00137     typename Out, 
00138     typename T,
00139     typename R,
00140     typename M,
00141     typename CFG
00142 >
00143 void output_vrml (Out& out, const polyhedron<T, R, M, CFG>& p)
00144 {
00145     ARAGELI_ASSERT_0(p.space_dim() == 3);
00146 
00147     typedef polyhedron<T, R, M, CFG> Polyhedra;
00148     typedef sideset Side_set;
00149     typedef typename Side_set::vertex_indices_set Vertex_set;
00150     
00151     // VRML header
00152     out <<
00153         "#VRML V2.0 utf8\n"
00154         "#This file is automatically generated by Arageli library.\n"
00155         "\n"
00156         "Shape { appearance Appearance { material Material { diffuseColor 1 1 1 } }"
00157         "geometry IndexedFaceSet { solid TRUE coord Coordinate { point [\n";
00158 
00159     output_aligned(out, p.template vertices<matrix<double> >(), "", ",", " ");
00160 
00161     out << "] } coordIndex [\n";
00162 
00163     Side_set sides = p.sides();
00164 
00165     for(typename Side_set::const_iterator i = sides.begin(); i != sides.end(); ++i)
00166     {
00167         Vertex_set vi = *i;
00168         for
00169         (
00170             typename Vertex_set::const_iterator j = vi.begin();
00171             j != vi.end(); ++j
00172         )
00173             out << *j << ", ";
00174         out << "-1,\n";
00175     }
00176 
00177     out << "] } }\n";
00178 }
00179 
00180 
00181 //template
00182 //<
00183 //  typename Out,
00184 //  typename P,
00185 //  typename X1, typename Y1, typename X2, typename Y2,
00186 //  typename Viewdir,
00187 //  typename Colormap
00188 //>
00189 //void output_polytope_pstricks_3d
00190 //(
00191 //  Out& out,
00192 //  const P& p,
00193 //  double x1,
00194 //  double y1,
00195 //  double x2,
00196 //  double y2,
00197 //  double linewidth,
00198 //  const Viewdir& viewdir,
00199 //  const Colormap& colormap
00200 //)
00201 //{
00202 //  
00203 //
00204 //}
00205 
00206 
00207 }
00208 
00209 
00210 #else   // #if !defined(ARAGELI_INCLUDE_CPP_WITH_EXPORT_TEMPLATE) || ...
00211 
00212 
00213 namespace Arageli
00214 {
00215 
00216 // PLACE ALL NOT TEMPLATE NOT INLINE IMPLEMENTATIONS HERE
00217 
00218 }
00219 
00220 
00221 #endif  // #if !defined(ARAGELI_INCLUDE_CPP_WITH_EXPORT_TEMPLATE) || ...

Generated on Thu Aug 31 17:38:08 2006 for Arageli by  doxygen 1.4.7