Reference documentation for deal.II version Git 05e4468 2017-09-21 10:18:23 +0200
point.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2016 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_point_h
17 #define dealii_point_h
18 
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/exceptions.h>
22 #include <deal.II/base/tensor.h>
23 #include <cmath>
24 
25 DEAL_II_NAMESPACE_OPEN
26 
88 template <int dim, typename Number = double>
89 class Point : public Tensor<1,dim,Number>
90 {
91 public:
96  Point ();
97 
101  explicit Point (const Tensor<1,dim,Number> &);
102 
109  explicit Point (const Number x);
110 
118  Point (const Number x,
119  const Number y);
120 
128  Point (const Number x,
129  const Number y,
130  const Number z);
131 
137  static Point<dim,Number> unit_vector(const unsigned int i);
138 
142  Number operator () (const unsigned int index) const;
143 
147  Number &operator () (const unsigned int index);
148 
149  /*
150  * @name Addition and subtraction of points.
151  * @{
152  */
153 
158 
167 
175 
180 
185  /*
186  * @name Multiplication and scaling of points. Dot products. Norms.
187  * @{
188  */
189 
195  template <typename OtherNumber>
197  operator * (const OtherNumber) const;
198 
202  template <typename OtherNumber>
204  operator / (const OtherNumber) const;
205 
209  Number operator * (const Tensor<1,dim,Number> &p) const;
210 
222 
229 
235 
244  template <class Archive>
245  void serialize(Archive &ar, const unsigned int version);
246 };
247 
248 /*------------------------------- Inline functions: Point ---------------------------*/
249 
250 #ifndef DOXYGEN
251 
252 // At least clang-3.7 requires us to have a user-defined constructor
253 // and we can't use 'Point<dim,Number>::Point () = default' here.
254 template <int dim, typename Number>
255 inline
257 {}
258 
259 
260 
261 template <int dim, typename Number>
262 inline
264  :
265  Tensor<1,dim,Number>(t)
266 {}
267 
268 
269 
270 template <int dim, typename Number>
271 inline
272 Point<dim,Number>::Point (const Number x)
273 {
274  Assert (dim==1,
275  ExcMessage ("You can only initialize Point<1> objects using the constructor "
276  "that takes only one argument. Point<dim> objects with dim!=1 "
277  "require initialization with the constructor that takes 'dim' "
278  "arguments."));
279 
280  // we can only get here if we pass the assertion. use the switch anyway so
281  // as to avoid compiler warnings about uninitialized elements or writing
282  // beyond the end of the 'values' array
283  switch (dim)
284  {
285  case 1:
286  this->values[0] = x;
287  break;
288 
289  default:
290  ;
291  }
292 }
293 
294 
295 
296 template <int dim, typename Number>
297 inline
298 Point<dim,Number>::Point (const Number x,
299  const Number y)
300 {
301  Assert (dim==2,
302  ExcMessage ("You can only initialize Point<2> objects using the constructor "
303  "that takes two arguments. Point<dim> objects with dim!=2 "
304  "require initialization with the constructor that takes 'dim' "
305  "arguments."));
306  // we can only get here if we pass the assertion. use the switch anyway so
307  // as to avoid compiler warnings about uninitialized elements or writing
308  // beyond the end of the 'values' array
309  switch (dim)
310  {
311  case 2:
312  this->values[0] = x;
313  this->values[1] = y;
314  break;
315 
316  default:
317  ;
318  }
319 }
320 
321 
322 
323 template <int dim, typename Number>
324 inline
325 Point<dim,Number>::Point (const Number x,
326  const Number y,
327  const Number z)
328 {
329  Assert (dim==3,
330  ExcMessage ("You can only initialize Point<3> objects using the constructor "
331  "that takes three arguments. Point<dim> objects with dim!=3 "
332  "require initialization with the constructor that takes 'dim' "
333  "arguments."));
334 
335  // we can only get here if we pass the assertion. use the switch anyway so
336  // as to avoid compiler warnings about uninitialized elements or writing
337  // beyond the end of the 'values' array
338  switch (dim)
339  {
340  case 3:
341  this->values[0] = x;
342  this->values[1] = y;
343  this->values[2] = z;
344  break;
345 
346  default:
347  ;
348  }
349 }
350 
351 
352 template <int dim, typename Number>
353 inline
355 Point<dim,Number>::unit_vector(unsigned int i)
356 {
358  p[i] = 1.;
359  return p;
360 }
361 
362 
363 template <int dim, typename Number>
364 inline
365 Number
366 Point<dim,Number>::operator () (const unsigned int index) const
367 {
368  AssertIndexRange((int) index, dim);
369  return this->values[index];
370 }
371 
372 
373 
374 template <int dim, typename Number>
375 inline
376 Number &
377 Point<dim,Number>::operator () (const unsigned int index)
378 {
379  AssertIndexRange((int) index, dim);
380  return this->values[index];
381 }
382 
383 
384 
385 template <int dim, typename Number>
386 inline
389 {
390  Point<dim,Number> tmp = *this;
391  tmp += p;
392  return tmp;
393 }
394 
395 
396 
397 template <int dim, typename Number>
398 inline
401 {
402  return (Tensor<1,dim,Number>(*this) -= p);
403 }
404 
405 
406 
407 template <int dim, typename Number>
408 inline
411 {
412  Point<dim,Number> tmp = *this;
413  tmp -= p;
414  return tmp;
415 }
416 
417 
418 
419 template <int dim, typename Number>
420 inline
423 {
424  Point<dim,Number> result;
425  for (unsigned int i=0; i<dim; ++i)
426  result.values[i] = -this->values[i];
427  return result;
428 }
429 
430 
431 
432 template <int dim, typename Number>
433 template <typename OtherNumber>
434 inline
436 Point<dim,Number>::operator * (const OtherNumber factor) const
437 {
439  for (unsigned int i=0; i<dim; ++i)
440  tmp[i] = this->operator[](i) * factor;
441  return tmp;
442 }
443 
444 
445 
446 template <int dim, typename Number>
447 template <typename OtherNumber>
448 inline
450 Point<dim,Number>::operator / (const OtherNumber factor) const
451 {
453  for (unsigned int i=0; i<dim; ++i)
454  tmp[i] = this->operator[](i) / factor;
455  return tmp;
456 }
457 
458 
459 
460 template <int dim, typename Number>
461 inline
462 Number
464 {
465  Number res = Number();
466  for (unsigned int i=0; i<dim; ++i)
467  res += this->operator[](i) * p[i];
468  return res;
469 }
470 
471 
472 template <int dim, typename Number>
473 inline
476 {
477  return this->norm_square();
478 }
479 
480 
481 
482 template <int dim, typename Number>
483 inline
486 {
487  return std::sqrt(distance_square(p));
488 }
489 
490 
491 
492 template <int dim, typename Number>
493 inline
496 {
498  for (unsigned int i=0; i<dim; ++i)
499  {
500  const Number diff=static_cast<Number>(this->values[i])-p(i);
502  }
503 
504  return sum;
505 }
506 
507 
508 
509 template <int dim, typename Number>
510 template <class Archive>
511 inline
512 void
513 Point<dim,Number>::serialize(Archive &ar, const unsigned int)
514 {
515  // forward to serialization
516  // function in the base class
517  ar &static_cast<Tensor<1,dim,Number> &>(*this);
518 }
519 
520 #endif // DOXYGEN
521 
522 
523 /*------------------------------- Global functions: Point ---------------------------*/
524 
525 
532 template <int dim, typename Number, typename OtherNumber>
533 inline
535 operator * (const OtherNumber factor,
536  const Point<dim,Number> &p)
537 {
538  return p * factor;
539 }
540 
541 
542 
548 template <int dim, typename Number>
549 inline
550 std::ostream &operator << (std::ostream &out,
551  const Point<dim,Number> &p)
552 {
553  for (unsigned int i=0; i<dim-1; ++i)
554  out << p[i] << ' ';
555  out << p[dim-1];
556 
557  return out;
558 }
559 
560 
561 
566 template <int dim, typename Number>
567 inline
568 std::istream &operator >> (std::istream &in,
570 {
571  for (unsigned int i=0; i<dim; ++i)
572  in >> p[i];
573 
574  return in;
575 }
576 
577 
578 #ifndef DOXYGEN
579 
585 template <typename Number>
586 inline
587 std::ostream &operator << (std::ostream &out,
588  const Point<1,Number> &p)
589 {
590  out << p[0];
591 
592  return out;
593 }
594 
595 #endif // DOXYGEN
596 DEAL_II_NAMESPACE_CLOSE
597 
598 #endif
Number operator()(const unsigned int index) const
Point< dim, Number > operator-() const
static DEAL_II_CUDA_HOST_DEV real_type abs_square(const number &x)
Definition: numbers.h:342
#define AssertIndexRange(index, range)
Definition: exceptions.h:1199
numbers::NumberTraits< Number >::real_type distance(const Point< dim, Number > &p) const
Definition: point.h:89
static::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:337
numbers::NumberTraits< Number >::real_type distance_square(const Point< dim, Number > &p) const
static Point< dim, Number > unit_vector(const unsigned int i)
Point< dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator/(const OtherNumber) const
void serialize(Archive &ar, const unsigned int version)
Point< dim, Number > operator+(const Tensor< 1, dim, Number > &) const
SymmetricTensor< rank, dim, Number > sum(const SymmetricTensor< rank, dim, Number > &local, const MPI_Comm &mpi_communicator)
numbers::NumberTraits< Number >::real_type square() const
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:163
Definition: mpi.h:51
std::istream & operator>>(std::istream &in, Point< dim, Number > &p)
Definition: point.h:568
Point< dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator*(const OtherNumber) const