-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.h
217 lines (183 loc) · 5.57 KB
/
point.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* (c) 30.07.2014 Martin Hünniger
*
* This file is part of DelDec.
*
* DelDec is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DelDec is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef FC_POINT_H
#define FC_POINT_H
#include "configure.h"
#include <vector>
#include <algorithm>
#include <numeric>
#include <initializer_list>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include <istream>
#include <fstream>
#include <strstream>
#include <sstream>
#include <iterator>
#include <boost/serialization/vector.hpp>
#ifdef USE_MPI
#include <boost/mpi.hpp>
#endif
namespace NAMESPACE {
template<int dim=3, typename Float=double>
class Point
{
public: // types
typedef typename std::vector<Float>::const_iterator Const_iterator;
typedef typename std::vector<Float>::iterator Iterator;
typedef typename std::vector<Float>::size_type size_type;
public: // construction and destruction
/**
* @brief Point Constructs a d-dimensional point with zero coordinates
*/
Point()
: c(dim)
, flag(false)
{}
/**
* @brief Point constructs the point from a initializer list
* @param list the initializer list
*/
Point(const std::initializer_list<Float> list)
: c(dim)
, flag(false)
{
ASSERT(list.size() == dim);
std::copy(list.begin(), list.end(), c.begin());
}
/**
* @brief Point Constructs a d-dimensional point with Cartesian center coordinates [first,first+d).
* @param d the dimension of the point
* @param first Iterator to the first data element used to initialize the point.
*/
template<typename InputIterator>
Point(const InputIterator first )
: c(first,first+dim)
, flag(false)
{}
Point(Point && other)
: c(std::move(other.c))
, flag(std::move(other.flag))
, squared_norm_value(std::move(other.squared_norm_value))
{}
Point(const Point& other)
: c(other.begin(),other.end())
, flag(other.flag)
, squared_norm_value(other.squared_norm_value)
{}
Point& operator=(Point other)
{
std::swap(c,other.c);
std::swap(flag,other.flag);
std::swap(squared_norm_value,other.squared_norm_value);
return *this;
}
public: // access
/**
* @brief operator [] returns a const-reference to the i-th coordinate.
* @param i the index of the coordinate to return
* @return the value at the i-th coordinate
*/
const Float& operator[]( const size_type i ) const
{
ASSERT( 0 <= i && i < c.size() );
return c[i];
}
Float& operator[]( const size_type i )
{
ASSERT( 0 <= i && i < c.size() );
return c[i];
}
const Float* data() const { return c.data(); }
Float* pointer() const { return const_cast<Float*>( &c.at(0) ); }
Const_iterator begin() const { return c.begin(); }
Const_iterator end() const { return c.end(); }
Iterator begin() { return c.begin(); }
Iterator end() { return c.end(); }
size_type size() const { return c.size(); }
/**
* @brief perturb Perturbs the point in a ball with radius max
* remember to initialize the random generator!!
* @param max
*/
void perturb( const Float max )
{
typename std::vector<Float>::iterator it;
for( it = c.begin(); it != c.end(); ++it ) {
*it += static_cast<Float>( 2*max*rand()/RAND_MAX-max );
}
flag = false;
}
const Float squared_norm() const
{
if(!flag) {
squared_norm_value = std::inner_product(c.begin(),c.end(),c.begin(),Float(0.0));
flag = true;
}
return squared_norm_value;
}
private: // Serialization
friend class boost::serialization::access;
template<class Archive>
void serialize( Archive &ar, const unsigned int )
{
ar & BOOST_SERIALIZATION_NVP( c );
}
friend std::ifstream& operator>>(std::ifstream& ifs, Point& point) {
std::string str;
std::getline(ifs, str);
std::stringstream ss(str);
std::copy(std::istream_iterator<Float>(ss),
std::istream_iterator<Float>(),
point.begin());
return ifs;
}
friend std::ostream& operator<<(std::ostream& os, const Point& point ) {
os << "[ ";
for(auto it=point.c.begin();it!=point.c.end();++it) {
os << *it << " ";
}
os << "]";
return os;
}
friend std::ofstream& operator<<(std::ofstream& ofs, const Point& point ) {
for(auto it=point.c.begin();it!=point.c.end();++it) {
ofs << *it << " ";
}
return ofs;
}
private: // member fields
std::vector<Float> c; // Cartesian center coordinates.
mutable Float squared_norm_value;
mutable bool flag;
};
} // namespace FC_NAMESPACE
#ifdef USE_MPI
namespace boost {
namespace mpi {
template<>
template<int dim, typename Float>
struct is_mpi_datatype<NAMESPACE::Point<dim,Float> > : mpl::true_ { };
}
}
#endif
#endif