Reference documentation for deal.II version Git 1ad260a 2018-02-22 16:12:58 +0100
point.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2018 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 
103 template <int dim, typename Number = double>
104 class Point : public Tensor<1,dim,Number>
105 {
106 public:
111  Point ();
112 
116  explicit Point (const Tensor<1,dim,Number> &);
117 
124  explicit Point (const Number x);
125 
133  Point (const Number x,
134  const Number y);
135 
143  Point (const Number x,
144  const Number y,
145  const Number z);
146 
152  static Point<dim,Number> unit_vector(const unsigned int i);
153 
157  Number operator () (const unsigned int index) const;
158 
162  Number &operator () (const unsigned int index);
163 
164  /*
165  * @name Addition and subtraction of points.
166  * @{
167  */
168 
173 
182 
190 
195 
200  /*
201  * @name Multiplication and scaling of points. Dot products. Norms.
202  * @{
203  */
204 
210  template <typename OtherNumber>
212  operator * (const OtherNumber) const;
213 
217  template <typename OtherNumber>
219  operator / (const OtherNumber) const;
220 
224  Number operator * (const Tensor<1,dim,Number> &p) const;
225 
237 
244 
250 
259  template <class Archive>
260  void serialize(Archive &ar, const unsigned int version);
261 };
262 
263 /*------------------------------- Inline functions: Point ---------------------------*/
264 
265 #ifndef DOXYGEN
266 
267 // At least clang-3.7 requires us to have a user-defined constructor
268 // and we can't use 'Point<dim,Number>::Point () = default' here.
269 template <int dim, typename Number>
270 inline
272 {}
273 
274 
275 
276 template <int dim, typename Number>
277 inline
279  :
280  Tensor<1,dim,Number>(t)
281 {}
282 
283 
284 
285 template <int dim, typename Number>
286 inline
287 Point<dim,Number>::Point (const Number x)
288 {
289  Assert (dim==1,
290  ExcMessage ("You can only initialize Point<1> objects using the constructor "
291  "that takes only one argument. Point<dim> objects with dim!=1 "
292  "require initialization with the constructor that takes 'dim' "
293  "arguments."));
294 
295  // we can only get here if we pass the assertion. use the switch anyway so
296  // as to avoid compiler warnings about uninitialized elements or writing
297  // beyond the end of the 'values' array
298  switch (dim)
299  {
300  case 1:
301  this->values[0] = x;
302  break;
303 
304  default:
305  ;
306  }
307 }
308 
309 
310 
311 template <int dim, typename Number>
312 inline
313 Point<dim,Number>::Point (const Number x,
314  const Number y)
315 {
316  Assert (dim==2,
317  ExcMessage ("You can only initialize Point<2> objects using the constructor "
318  "that takes two arguments. Point<dim> objects with dim!=2 "
319  "require initialization with the constructor that takes 'dim' "
320  "arguments."));
321  // we can only get here if we pass the assertion. use the indirection anyway
322  // so as to avoid compiler warnings about uninitialized elements or writing
323  // beyond the end of the 'values' array
324  constexpr unsigned int y_index = (dim<2)?0:1;
325  this->values[0] = x;
326  this->values[y_index] = y;
327 }
328 
329 
330 
331 template <int dim, typename Number>
332 inline
333 Point<dim,Number>::Point (const Number x,
334  const Number y,
335  const Number z)
336 {
337  Assert (dim==3,
338  ExcMessage ("You can only initialize Point<3> objects using the constructor "
339  "that takes three arguments. Point<dim> objects with dim!=3 "
340  "require initialization with the constructor that takes 'dim' "
341  "arguments."));
342 
343  // we can only get here if we pass the assertion. use the indirection anyway
344  // so as to avoid compiler warnings about uninitialized elements or writing
345  // beyond the end of the 'values' array
346  constexpr unsigned int y_index = (dim<2)?0:1;
347  constexpr unsigned int z_index = (dim<3)?0:2;
348  this->values[0] = x;
349  this->values[y_index] = y;
350  this->values[z_index] = z;
351 }
352 
353 
354 template <int dim, typename Number>
355 inline
357 Point<dim,Number>::unit_vector(unsigned int i)
358 {
360  p[i] = 1.;
361  return p;
362 }
363 
364 
365 template <int dim, typename Number>
366 inline
367 Number
368 Point<dim,Number>::operator () (const unsigned int index) const
369 {
370  AssertIndexRange((int) index, dim);
371  return this->values[index];
372 }
373 
374 
375 
376 template <int dim, typename Number>
377 inline
378 Number &
379 Point<dim,Number>::operator () (const unsigned int index)
380 {
381  AssertIndexRange((int) index, dim);
382  return this->values[index];
383 }
384 
385 
386 
387 template <int dim, typename Number>
388 inline
391 {
392  Point<dim,Number> tmp = *this;
393  tmp += p;
394  return tmp;
395 }
396 
397 
398 
399 template <int dim, typename Number>
400 inline
403 {
404  return (Tensor<1,dim,Number>(*this) -= p);
405 }
406 
407 
408 
409 template <int dim, typename Number>
410 inline
413 {
414  Point<dim,Number> tmp = *this;
415  tmp -= p;
416  return tmp;
417 }
418 
419 
420 
421 template <int dim, typename Number>
422 inline
425 {
426  Point<dim,Number> result;
427  for (unsigned int i=0; i<dim; ++i)
428  result.values[i] = -this->values[i];
429  return result;
430 }
431 
432 
433 
434 template <int dim, typename Number>
435 template <typename OtherNumber>
436 inline
438 Point<dim,Number>::operator * (const OtherNumber factor) const
439 {
441  for (unsigned int i=0; i<dim; ++i)
442  tmp[i] = this->operator[](i) * factor;
443  return tmp;
444 }
445 
446 
447 
448 template <int dim, typename Number>
449 template <typename OtherNumber>
450 inline
452 Point<dim,Number>::operator / (const OtherNumber factor) const
453 {
455  for (unsigned int i=0; i<dim; ++i)
456  tmp[i] = this->operator[](i) / factor;
457  return tmp;
458 }
459 
460 
461 
462 template <int dim, typename Number>
463 inline
464 Number
466 {
467  Number res = Number();
468  for (unsigned int i=0; i<dim; ++i)
469  res += this->operator[](i) * p[i];
470  return res;
471 }
472 
473 
474 template <int dim, typename Number>
475 inline
478 {
479  return this->norm_square();
480 }
481 
482 
483 
484 template <int dim, typename Number>
485 inline
488 {
489  return std::sqrt(distance_square(p));
490 }
491 
492 
493 
494 template <int dim, typename Number>
495 inline
498 {
500  for (unsigned int i=0; i<dim; ++i)
501  {
502  const Number diff=static_cast<Number>(this->values[i])-p(i);
504  }
505 
506  return sum;
507 }
508 
509 
510 
511 template <int dim, typename Number>
512 template <class Archive>
513 inline
514 void
515 Point<dim,Number>::serialize(Archive &ar, const unsigned int)
516 {
517  // forward to serialization
518  // function in the base class
519  ar &static_cast<Tensor<1,dim,Number> &>(*this);
520 }
521 
522 #endif // DOXYGEN
523 
524 
525 /*------------------------------- Global functions: Point ---------------------------*/
526 
527 
534 template <int dim, typename Number, typename OtherNumber>
535 inline
537 operator * (const OtherNumber factor,
538  const Point<dim,Number> &p)
539 {
540  return p * factor;
541 }
542 
543 
544 
550 template <int dim, typename Number>
551 inline
552 std::ostream &operator << (std::ostream &out,
553  const Point<dim,Number> &p)
554 {
555  for (unsigned int i=0; i<dim-1; ++i)
556  out << p[i] << ' ';
557  out << p[dim-1];
558 
559  return out;
560 }
561 
562 
563 
568 template <int dim, typename Number>
569 inline
570 std::istream &operator >> (std::istream &in,
572 {
573  for (unsigned int i=0; i<dim; ++i)
574  in >> p[i];
575 
576  return in;
577 }
578 
579 
580 #ifndef DOXYGEN
581 
587 template <typename Number>
588 inline
589 std::ostream &operator << (std::ostream &out,
590  const Point<1,Number> &p)
591 {
592  out << p[0];
593 
594  return out;
595 }
596 
597 #endif // DOXYGEN
598 DEAL_II_NAMESPACE_CLOSE
599 
600 #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:345
#define AssertIndexRange(index, range)
Definition: exceptions.h:1237
numbers::NumberTraits< Number >::real_type distance(const Point< dim, Number > &p) const
Definition: point.h:104
static::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:349
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:53
std::istream & operator>>(std::istream &in, Point< dim, Number > &p)
Definition: point.h:570
Point< dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator*(const OtherNumber) const