politopix  4.1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Point_Rn.cpp
Go to the documentation of this file.
1 // politopix allows to make computations on polytopes such as finding vertices, intersecting, Minkowski sums, ...
2 // Copyright (C) 2011-2015 : Delos Vincent
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
19 // I2M (UMR CNRS 5295 / University of Bordeaux)
20 
21 #include <iostream>
22 #include <sstream>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string>
26 #include <cmath>
27 #include "Point_Rn.h"
28 #include "Rn.h"
29 
30 Point_Rn::Point_Rn(unsigned int n) {
31  _coordinates.resize(n);
32 }
33 
34 Point_Rn::Point_Rn(unsigned int n, double u) {
35  _coordinates.resize(n, u);
36 }
37 
38 Point_Rn::Point_Rn(double u1, double u2, double u3) {
39  _coordinates.resize(3);
40  _coordinates[0] = u1;
41  _coordinates[1] = u2;
42  _coordinates[2] = u3;
43 }
44 
46 }
47 
49  double norm = norm_2(_coordinates);
50  _coordinates = _coordinates / norm;
51  return norm;
52 }
53 
54 double Point_Rn::distanceFrom(const Point_Rn& P) {
55  vector<double> distV = _coordinates;
56  distV -= P._coordinates;
57  double dist = norm_2(distV);
58  return dist;
59 }
60 
61 void Point_Rn::setCoordinate(unsigned int i, double val) throw (std::out_of_range) {
62  if (i < _coordinates.size())
63  _coordinates[i] = val;
64  else {
65  std::string errorMessage = Point_Rn::concatStrings(i, val, "Point_Rn::setCoordinate");
66  throw std::out_of_range(errorMessage);
67  }
68 }
69 
70 double Point_Rn::getCoordinate(unsigned int i) const throw (std::out_of_range) {
71  if (i < _coordinates.size())
72  return _coordinates[i];
73  else {
74  std::string errorMessage = Point_Rn::concatStrings(i, "Point_Rn::getCoordinate");
75  throw std::out_of_range(errorMessage);
76  }
77 }
78 
79 std::string Point_Rn::concatStrings(int i, const std::string& functionName) {
80  std::ostringstream stream_;
81  stream_ << "index=";
82  stream_ << i;
83  std::string valString = stream_.str();
84 
85  std::string errorMessage(functionName);
86  errorMessage += " ";
87  errorMessage += valString;
88  errorMessage += " ";
89 
90  return errorMessage;
91 }
92 
93 std::string Point_Rn::concatStrings(int i, double val, const std::string& functionName) {
94  std::ostringstream stream_;
95  stream_ << "index=";
96  stream_ << i;
97  stream_ << ", value=";
98  stream_ << val;
99  std::string valString = stream_.str();
100 
101  std::string errorMessage(functionName);
102  errorMessage += " ";
103  errorMessage += valString;
104  errorMessage += " ";
105 
106  return errorMessage;
107 }
108 
109 void Point_Rn::load(std::istream &this_istream) {
110  double val;
111  for (unsigned int coord_count=0; coord_count<_coordinates.size(); coord_count++) {
112  this_istream >> val;
113  setCoordinate(coord_count, val);
114  }
115 }
116 
117 void Point_Rn::save(std::ostream &this_ostream) const {
118  for (unsigned int coord_count=0; coord_count<_coordinates.size(); coord_count++) {
119  this_ostream << getCoordinate(coord_count) << " ";
120  }
121  //this_ostream << std::endl;
122 }
123 
void load(std::istream &this_istream)
Definition: Point_Rn.cpp:109
Creation of a n-coordinate geometric point designed to be shared by its neighbour faces...
Definition: Point_Rn.h:34
double getCoordinate(unsigned int i) const
Definition: Point_Rn.cpp:70
double normalize()
Definition: Point_Rn.cpp:48
double distanceFrom(const Point_Rn &)
Definition: Point_Rn.cpp:54
virtual ~Point_Rn()
Definition: Point_Rn.cpp:45
vector< double > _coordinates
Definition: Point_Rn.h:80
Point_Rn(unsigned int n)
Create a n-coordinates point.
Definition: Point_Rn.cpp:30
static std::string concatStrings(int i, const std::string &functionName)
Useful function to provide error message to the exception mechanism.
Definition: Point_Rn.cpp:79
void save(std::ostream &this_ostream) const
Definition: Point_Rn.cpp:117
void setCoordinate(unsigned int i, double val)
Definition: Point_Rn.cpp:61