include/deal.II/base/point.h

00001 //---------------------------------------------------------------------------
00002 //    @f$Id: point.h 25492 2012-05-05 15:11:20Z kormann @f$
00003 //
00004 //    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2010, 2012 by the deal.II authors
00005 //
00006 //    This file is subject to QPL and may not be  distributed
00007 //    without copyright and license information. Please refer
00008 //    to the file deal.II/doc/license.html for the  text  and
00009 //    further information on this license.
00010 //
00011 //---------------------------------------------------------------------------
00012 #ifndef __deal2__point_h
00013 #define __deal2__point_h
00014 
00015 
00016 #include <deal.II/base/config.h>
00017 #include <deal.II/base/exceptions.h>
00018 #include <deal.II/base/tensor_base.h>
00019 #include <cmath>
00020 
00021 DEAL_II_NAMESPACE_OPEN
00022 
00050 template <int dim, typename Number>
00051 class Point : public Tensor<1,dim,Number>
00052 {
00053   public:
00058     Point ();
00059 
00065     explicit Point (const bool initialize);
00066 
00072     Point (const Tensor<1,dim,Number> &);
00073 
00081     explicit Point (const Number x);
00082 
00090     Point (const Number x, const Number y);
00091 
00099     Point (const Number x, const Number y, const Number z);
00100 
00105     static Point<dim,Number> unit_vector(const unsigned int i);
00106 
00111     Number   operator () (const unsigned int index) const;
00112 
00117     Number & operator () (const unsigned int index);
00118 
00119 /*
00120  * Plus and minus operators are re-implemented from Tensor<1,dim>
00121  * to avoid additional casting.
00122  */
00123 
00130     Point<dim,Number>   operator + (const Tensor<1,dim,Number>&) const;
00131 
00138     Point<dim,Number>   operator - (const Tensor<1,dim,Number>&) const;
00139 
00143     Point<dim,Number>   operator - () const;
00144 
00154     Point<dim,Number>   operator * (const Number) const;
00155 
00160     Number       operator * (const Tensor<1,dim,Number> &) const;
00161 
00168     Point<dim,Number>   operator / (const Number) const;
00169 
00175     Number              square () const;
00176 
00184     Number distance (const Point<dim,Number> &p) const;
00185 
00190     template <class Archive>
00191     void serialize(Archive & ar, const unsigned int version);
00192 };
00193 
00194 /*------------------------------- Inline functions: Point ---------------------------*/
00195 
00196 #ifndef DOXYGEN
00197 
00198 template <int dim, typename Number>
00199 inline
00200 Point<dim,Number>::Point ()
00201 {}
00202 
00203 
00204 
00205 template <int dim, typename Number>
00206 inline
00207 Point<dim,Number>::Point (const bool initialize)
00208                           :
00209                           Tensor<1,dim,Number>(initialize)
00210 {}
00211 
00212 
00213 
00214 template <int dim, typename Number>
00215 inline
00216 Point<dim,Number>::Point (const Tensor<1,dim,Number> &t)
00217                           :
00218                           Tensor<1,dim,Number>(t)
00219 {}
00220 
00221 
00222 
00223 template <int dim, typename Number>
00224 inline
00225 Point<dim,Number>::Point (const Number x)
00226 {
00227   Assert (dim==1, StandardExceptions::ExcInvalidConstructorCall());
00228   this->values[0] = x;
00229 }
00230 
00231 
00232 
00233 template <int dim, typename Number>
00234 inline
00235 Point<dim,Number>::Point (const Number x, const Number y)
00236 {
00237   Assert (dim==2, StandardExceptions::ExcInvalidConstructorCall());
00238   this->values[0] = x;
00239   this->values[1] = y;
00240 }
00241 
00242 
00243 
00244 template <int dim, typename Number>
00245 inline
00246 Point<dim,Number>::Point (const Number x, const Number y, const Number z)
00247 {
00248   Assert (dim==3, StandardExceptions::ExcInvalidConstructorCall());
00249   this->values[0] = x;
00250   this->values[1] = y;
00251   this->values[2] = z;
00252 }
00253 
00254 
00255 template <int dim, typename Number>
00256 inline
00257 Point<dim,Number>
00258 Point<dim,Number>::unit_vector(unsigned int i)
00259 {
00260   Point<dim,Number> p;
00261   p[i] = 1.;
00262   return p;
00263 }
00264 
00265 
00266 template <int dim, typename Number>
00267 inline
00268 Number
00269 Point<dim,Number>::operator () (const unsigned int index) const
00270 {
00271   AssertIndexRange((int) index, dim);
00272   return this->values[index];
00273 }
00274 
00275 
00276 
00277 template <int dim, typename Number>
00278 inline
00279 Number &
00280 Point<dim,Number>::operator () (const unsigned int index)
00281 {
00282   AssertIndexRange((int) index, dim);
00283   return this->values[index];
00284 }
00285 
00286 
00287 
00288 template <int dim, typename Number>
00289 inline
00290 Point<dim,Number>
00291 Point<dim,Number>::operator + (const Tensor<1,dim,Number> &p) const
00292 {
00293   return (Point<dim,Number>(*this) += p);
00294 }
00295 
00296 
00297 
00298 template <int dim, typename Number>
00299 inline
00300 Point<dim,Number>
00301 Point<dim,Number>::operator - (const Tensor<1,dim,Number> &p) const
00302 {
00303   return (Point<dim,Number>(*this) -= p);
00304 }
00305 
00306 
00307 
00308 template <int dim, typename Number>
00309 inline
00310 Point<dim,Number>
00311 Point<dim,Number>::operator - () const
00312 {
00313   Point<dim,Number> result;
00314   for (unsigned int i=0; i<dim; ++i)
00315     result.values[i] = -this->values[i];
00316   return result;
00317 }
00318 
00319 
00320 
00321 template <int dim, typename Number>
00322 inline
00323 Point<dim,Number>
00324 Point<dim,Number>::operator * (const Number factor) const
00325 {
00326   return (Point<dim,Number>(*this) *= factor);
00327 }
00328 
00329 
00330 
00331 template <int dim, typename Number>
00332 inline
00333 Number
00334 Point<dim,Number>::operator * (const Tensor<1,dim,Number> &p) const
00335 {
00336                                    // simply pass down
00337   return Tensor<1,dim,Number>::operator * (p);
00338 }
00339 
00340 
00341 template <int dim, typename Number>
00342 inline
00343 Number
00344 Point<dim,Number>::square () const
00345 {
00346   Number q = Number();
00347   for (unsigned int i=0; i<dim; ++i)
00348     q += this->values[i] * this->values[i];
00349   return q;
00350 }
00351 
00352 
00353 
00354 template <int dim, typename Number>
00355 inline
00356 Number
00357 Point<dim,Number>::distance (const Point<dim,Number> &p) const
00358 {
00359   Number sum=0;
00360   for (unsigned int i=0; i<dim; ++i)
00361     {
00362       const double diff=this->values[i]-p(i);
00363       sum += diff*diff;
00364     }
00365 
00366   return std::sqrt(sum);
00367 }
00368 
00369 
00370 
00371 template <int dim, typename Number>
00372 inline
00373 Point<dim,Number> Point<dim,Number>::operator / (const Number factor) const
00374 {
00375   return (Point<dim,Number>(*this) /= factor);
00376 }
00377 
00378 
00379 
00380 template <int dim, typename Number>
00381 template <class Archive>
00382 inline
00383 void
00384 Point<dim,Number>::serialize(Archive & ar, const unsigned int)
00385 {
00386                                      // forward to serialization
00387                                      // function in the base class
00388   ar &  static_cast<Tensor<1,dim,Number> &>(*this);
00389 }
00390 
00391 #endif // DOXYGEN
00392 
00393 
00394 /*------------------------------- Global functions: Point ---------------------------*/
00395 
00396 
00401 template <int dim, typename Number>
00402 inline
00403 Point<dim,Number> operator * (const Number             factor,
00404                               const Point<dim,Number> &p)
00405 {
00406   return p*factor;
00407 }
00408 
00409 
00410 
00415 template <int dim>
00416 inline
00417 Point<dim,double> operator * (const double             factor,
00418                               const Point<dim,double> &p)
00419 {
00420   return p*factor;
00421 }
00422 
00423 
00424 
00430 template <int dim, typename Number>
00431 inline
00432 std::ostream & operator << (std::ostream            &out,
00433                             const Point<dim,Number> &p)
00434 {
00435   for (unsigned int i=0; i<dim-1; ++i)
00436     out << p[i] << ' ';
00437   out << p[dim-1];
00438 
00439   return out;
00440 }
00441 
00442 
00443 
00449 template <int dim, typename Number>
00450 inline
00451 std::istream & operator >> (std::istream      &in,
00452                             Point<dim,Number> &p)
00453 {
00454   for (unsigned int i=0; i<dim; ++i)
00455     in >> p[i];
00456 
00457   return in;
00458 }
00459 
00460 
00461 #ifndef DOXYGEN
00462 
00468 template <typename Number>
00469 inline
00470 std::ostream & operator << (std::ostream &out,
00471                             const Point<1,Number> &p)
00472 {
00473   out << p[0];
00474 
00475   return out;
00476 }
00477 
00478 #endif // DOXYGEN
00479 DEAL_II_NAMESPACE_CLOSE
00480 
00481 #endif
00482 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

deal.II documentation generated on Wed May 23 2012 06:07:31 by doxygen 1.7.3