Reference documentation for deal.II version 8.4.2
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 
238  template <class Archive>
239  void serialize(Archive &ar, const unsigned int version);
240 };
241 
242 /*------------------------------- Inline functions: Point ---------------------------*/
243 
244 #ifndef DOXYGEN
245 
246 template <int dim, typename Number>
247 inline
249 {}
250 
251 
252 
253 template <int dim, typename Number>
254 inline
256  :
258 {}
259 
260 
261 
262 template <int dim, typename Number>
263 inline
264 Point<dim,Number>::Point (const Number x)
265 {
266  switch (dim)
267  {
268  case 1:
269  this->values[0] = x;
270  default:
271  Assert (dim==1, StandardExceptions::ExcInvalidConstructorCall());
272  }
273 }
274 
275 
276 
277 template <int dim, typename Number>
278 inline
279 Point<dim,Number>::Point (const Number x, const Number y)
280 {
281  switch (dim)
282  {
283  case 2:
284  this->values[0] = x;
285  this->values[1] = y;
286  default:
287  Assert (dim==2, StandardExceptions::ExcInvalidConstructorCall());
288  }
289 }
290 
291 
292 
293 template <int dim, typename Number>
294 inline
295 Point<dim,Number>::Point (const Number x, const Number y, const Number z)
296 {
297  switch (dim)
298  {
299  case 3:
300  this->values[0] = x;
301  this->values[1] = y;
302  this->values[2] = z;
303  default:
304  Assert (dim==3, StandardExceptions::ExcInvalidConstructorCall());
305  }
306 }
307 
308 
309 template <int dim, typename Number>
310 inline
312 Point<dim,Number>::unit_vector(unsigned int i)
313 {
315  p[i] = 1.;
316  return p;
317 }
318 
319 
320 template <int dim, typename Number>
321 inline
322 Number
323 Point<dim,Number>::operator () (const unsigned int index) const
324 {
325  AssertIndexRange((int) index, dim);
326  return this->values[index];
327 }
328 
329 
330 
331 template <int dim, typename Number>
332 inline
333 Number &
334 Point<dim,Number>::operator () (const unsigned int index)
335 {
336  AssertIndexRange((int) index, dim);
337  return this->values[index];
338 }
339 
340 
341 
342 template <int dim, typename Number>
343 inline
346 {
347  Point<dim,Number> tmp = *this;
348  tmp += p;
349  return tmp;
350 }
351 
352 
353 
354 template <int dim, typename Number>
355 inline
358 {
359  return (Tensor<1,dim,Number>(*this) -= p);
360 }
361 
362 
363 
364 template <int dim, typename Number>
365 inline
368 {
369  Point<dim,Number> tmp = *this;
370  tmp -= p;
371  return tmp;
372 }
373 
374 
375 
376 template <int dim, typename Number>
377 inline
380 {
381  Point<dim,Number> result;
382  for (unsigned int i=0; i<dim; ++i)
383  result.values[i] = -this->values[i];
384  return result;
385 }
386 
387 
388 
389 template <int dim, typename Number>
390 template<typename OtherNumber>
391 inline
393 Point<dim,Number>::operator * (const OtherNumber factor) const
394 {
396  for (unsigned int i=0; i<dim; ++i)
397  tmp[i] = this->operator[](i) * factor;
398  return tmp;
399 }
400 
401 
402 
403 template <int dim, typename Number>
404 template<typename OtherNumber>
405 inline
407 Point<dim,Number>::operator / (const OtherNumber factor) const
408 {
410  for (unsigned int i=0; i<dim; ++i)
411  tmp[i] = this->operator[](i) / factor;
412  return tmp;
413 }
414 
415 
416 
417 template <int dim, typename Number>
418 inline
419 Number
421 {
422  Number res = Number();
423  for (unsigned int i=0; i<dim; ++i)
424  res += this->operator[](i) * p[i];
425  return res;
426 }
427 
428 
429 template <int dim, typename Number>
430 inline
433 {
434  return this->norm_square();
435 }
436 
437 
438 
439 template <int dim, typename Number>
440 inline
443 {
444  Number sum = Number();
445  for (unsigned int i=0; i<dim; ++i)
446  {
447  const Number diff=this->values[i]-p(i);
449  }
450 
451  return std::sqrt(sum);
452 }
453 
454 
455 
456 template <int dim, typename Number>
457 template <class Archive>
458 inline
459 void
460 Point<dim,Number>::serialize(Archive &ar, const unsigned int)
461 {
462  // forward to serialization
463  // function in the base class
464  ar &static_cast<Tensor<1,dim,Number> &>(*this);
465 }
466 
467 #endif // DOXYGEN
468 
469 
470 /*------------------------------- Global functions: Point ---------------------------*/
471 
472 
479 template <int dim, typename Number, typename OtherNumber>
480 inline
482 operator * (const OtherNumber factor,
483  const Point<dim,Number> &p)
484 {
485  return p * factor;
486 }
487 
488 
489 
495 template <int dim, typename Number>
496 inline
497 std::ostream &operator << (std::ostream &out,
498  const Point<dim,Number> &p)
499 {
500  for (unsigned int i=0; i<dim-1; ++i)
501  out << p[i] << ' ';
502  out << p[dim-1];
503 
504  return out;
505 }
506 
507 
508 
514 template <int dim, typename Number>
515 inline
516 std::istream &operator >> (std::istream &in,
518 {
519  for (unsigned int i=0; i<dim; ++i)
520  in >> p[i];
521 
522  return in;
523 }
524 
525 
526 #ifndef DOXYGEN
527 
533 template <typename Number>
534 inline
535 std::ostream &operator << (std::ostream &out,
536  const Point<1,Number> &p)
537 {
538  out << p[0];
539 
540  return out;
541 }
542 
543 #endif // DOXYGEN
544 DEAL_II_NAMESPACE_CLOSE
545 
546 #endif
Tensor< rank, dim, Number > sum(const Tensor< rank, dim, Number > &local, const MPI_Comm &mpi_communicator)
Definition: mpi.h:650
Point< dim, Number > operator-() const
Point< dim, Number > operator+(const Tensor< 1, dim, Number > &) const
#define AssertIndexRange(index, range)
Definition: exceptions.h:1081
Number operator()(const unsigned int index) const
std::ostream & operator<<(std::ostream &out, const Point< dim, Number > &p)
Definition: point.h:497
Definition: point.h:89
static real_type abs_square(const number &x)
Definition: numbers.h:313
#define Assert(cond, exc)
Definition: exceptions.h:294
static Point< dim, Number > unit_vector(const unsigned int i)
void serialize(Archive &ar, const unsigned int version)
numbers::NumberTraits< Number >::real_type norm_square() const
Definition: tensor.h:1056
numbers::NumberTraits< Number >::real_type distance(const Point< dim, Number > &p) const
Tensor< rank_-1, dim, Number > values[(dim !=0) ? dim :1]
Definition: tensor.h:562
Definition: mpi.h:48
numbers::NumberTraits< Number >::real_type square() const
Point< dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator*(const OtherNumber) const
std::istream & operator>>(std::istream &in, Point< dim, Number > &p)
Definition: point.h:516
Point< dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator/(const OtherNumber) const