CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
PointCoordinates.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (c) 2009-2015 C.B. Barber. All rights reserved.
4 ** $Id: //main/2015/qhull/src/libqhullcpp/PointCoordinates.cpp#1 $$Change: 1981 $
5 ** $DateTime: 2015/09/28 20:26:32 $$Author: bbarber $
6 **
7 ****************************************************************************/
8 
9 #include "QhullError.h"
10 #include "QhullPoint.h"
11 #include "PointCoordinates.h"
12 
13 #include <iterator>
14 #include <iostream>
15 
16 using std::istream;
17 using std::string;
18 using std::ws;
19 
20 #ifdef _MSC_VER // Microsoft Visual C++ -- warning level 4
21 #pragma warning( disable : 4996) // function was declared deprecated(strcpy, localtime, etc.)
22 #endif
23 
24 namespace orgQhull {
25 
26 #
27 
28 #
29 
30 PointCoordinates::
31 PointCoordinates()
32 : QhullPoints()
33 , point_coordinates()
34 , describe_points()
35 {
36 }
37 
38 PointCoordinates::
39 PointCoordinates(const std::string &aComment)
40 : QhullPoints()
41 , point_coordinates()
42 , describe_points(aComment)
43 {
44 }
45 
46 PointCoordinates::
47 PointCoordinates(int pointDimension, const std::string &aComment)
48 : QhullPoints()
49 , point_coordinates()
50 , describe_points(aComment)
51 {
52  setDimension(pointDimension);
53 }
54 
56 PointCoordinates::
57 PointCoordinates(const Qhull &q)
58 : QhullPoints(q)
59 , point_coordinates()
60 , describe_points()
61 {
62 }
63 
64 PointCoordinates::
65 PointCoordinates(const Qhull &q, const std::string &aComment)
66 : QhullPoints(q)
67 , point_coordinates()
68 , describe_points(aComment)
69 {
70 }
71 
72 PointCoordinates::
73 PointCoordinates(const Qhull &q, int pointDimension, const std::string &aComment)
74 : QhullPoints(q)
75 , point_coordinates()
76 , describe_points(aComment)
77 {
78  setDimension(pointDimension);
79 }
80 
81 PointCoordinates::
82 PointCoordinates(const Qhull &q, int pointDimension, const std::string &aComment, countT coordinatesCount, const coordT *c)
83 : QhullPoints(q)
84 , point_coordinates()
85 , describe_points(aComment)
86 {
87  setDimension(pointDimension);
88  append(coordinatesCount, c);
89 }
90 
91 PointCoordinates::
92 PointCoordinates(QhullQh *qqh)
93 : QhullPoints(qqh)
94 , point_coordinates()
95 , describe_points()
96 {
97 }
98 
99 PointCoordinates::
100 PointCoordinates(QhullQh *qqh, const std::string &aComment)
101 : QhullPoints(qqh)
102 , point_coordinates()
103 , describe_points(aComment)
104 {
105 }
106 
107 PointCoordinates::
108 PointCoordinates(QhullQh *qqh, int pointDimension, const std::string &aComment)
109 : QhullPoints(qqh)
110 , point_coordinates()
111 , describe_points(aComment)
112 {
113  setDimension(pointDimension);
114 }
115 
116 PointCoordinates::
117 PointCoordinates(QhullQh *qqh, int pointDimension, const std::string &aComment, countT coordinatesCount, const coordT *c)
118 : QhullPoints(qqh)
119 , point_coordinates()
120 , describe_points(aComment)
121 {
122  setDimension(pointDimension);
123  append(coordinatesCount, c);
124 }
125 
126 PointCoordinates::
127 PointCoordinates(const PointCoordinates &other)
128 : QhullPoints(other)
129 , point_coordinates(other.point_coordinates)
130 , describe_points(other.describe_points)
131 {
132  makeValid(); // Update point_first and point_end
133 }
134 
135 PointCoordinates & PointCoordinates::
136 operator=(const PointCoordinates &other)
137 {
138  QhullPoints::operator=(other);
139  point_coordinates= other.point_coordinates;
140  describe_points= other.describe_points;
141  makeValid(); // Update point_first and point_end
142  return *this;
143 }//operator=
144 
145 PointCoordinates::
146 ~PointCoordinates()
147 { }
148 
149 #
150 
151 void PointCoordinates::
152 checkValid() const
153 {
154  if(getCoordinates().data()!=data()
155  || getCoordinates().count()!=coordinateCount()){
156  throw QhullError(10060, "Qhull error: first point (%x) is not PointCoordinates.data() or count (%d) is not PointCoordinates.count (%d)", coordinateCount(), getCoordinates().count(), 0.0, data());
157  }
158 }//checkValid
159 
160 void PointCoordinates::
161 setDimension(int i)
162 {
163  if(i<0){
164  throw QhullError(10062, "Qhull error: can not set PointCoordinates dimension to %d", i);
165  }
166  int currentDimension=QhullPoints::dimension();
167  if(currentDimension!=0 && i!=currentDimension){
168  throw QhullError(10063, "Qhull error: can not change PointCoordinates dimension (from %d to %d)", currentDimension, i);
169  }
170  QhullPoints::setDimension(i);
171 }//setDimension
172 
173 #
174 
175 Coordinates::ConstIterator PointCoordinates::
176 beginCoordinates(countT pointIndex) const
177 {
178  return point_coordinates.begin()+indexOffset(pointIndex);
179 }
180 
181 Coordinates::Iterator PointCoordinates::
182 beginCoordinates(countT pointIndex)
183 {
184  return point_coordinates.begin()+indexOffset(pointIndex);
185 }
186 
187 #
188 
189 void PointCoordinates::
190 append(countT coordinatesCount, const coordT *c)
191 {
192  if(coordinatesCount<=0){
193  return;
194  }
195  if(includesCoordinates(c)){
196  throw QhullError(10065, "Qhull error: can not append a subset of PointCoordinates to itself. The coordinates for point %d may move.", indexOf(c, QhullError::NOthrow));
197  }
198  reserveCoordinates(coordinatesCount);
199  std::copy(c, c+coordinatesCount, std::back_inserter(point_coordinates));
200  makeValid();
201 }//append coordT
202 
203 void PointCoordinates::
204 append(const PointCoordinates &other)
205 {
206  setDimension(other.dimension());
207  append(other.coordinateCount(), other.data());
208 }//append PointCoordinates
209 
210 void PointCoordinates::
211 append(const QhullPoint &p)
212 {
213  setDimension(p.dimension());
214  append(p.dimension(), p.coordinates());
215 }//append QhullPoint
216 
217 void PointCoordinates::
218 appendComment(const std::string &s){
219  if(char c= s[0] && describe_points.empty()){
220  if(c=='-' || isdigit(c)){
221  throw QhullError(10028, "Qhull argument error: comments can not start with a number or minus, %s", 0, 0, 0.0, s.c_str());
222  }
223  }
224  describe_points += s;
225 }//appendComment
226 
229 void PointCoordinates::
230 appendPoints(istream &in)
231 {
232  int inDimension;
233  countT inCount;
234  in >> ws >> inDimension >> ws;
235  if(!in.good()){
236  in.clear();
237  string remainder;
238  getline(in, remainder);
239  throw QhullError(10005, "Qhull error: input did not start with dimension or count -- %s", 0, 0, 0, remainder.c_str());
240  }
241  char c= (char)in.peek();
242  if(c!='-' && !isdigit(c)){ // Comments start with a non-digit
243  getline(in, describe_points);
244  in >> ws;
245  }
246  in >> inCount >> ws;
247  if(!in.good()){
248  in.clear();
249  string remainder;
250  getline(in, remainder);
251  throw QhullError(10009, "Qhull error: input did not start with dimension and count -- %d %s", inDimension, 0, 0, remainder.c_str());
252  }
253  c= (char)in.peek();
254  if(c!='-' && !isdigit(c)){ // Comments start with a non-digit
255  getline(in, describe_points);
256  in >> ws;
257  }
258  if(inCount<inDimension){ // Count may precede dimension
259  std::swap(inCount, inDimension);
260  }
261  setDimension(inDimension);
262  reserveCoordinates(inCount*inDimension);
263  countT coordinatesCount= 0;
264  while(!in.eof()){
265  realT p;
266  in >> p >> ws;
267  if(in.fail()){
268  in.clear();
269  string remainder;
270  getline(in, remainder);
271  throw QhullError(10008, "Qhull error: failed to read coordinate %d of point %d\n %s", coordinatesCount % inDimension, coordinatesCount/inDimension, 0, remainder.c_str());
272  }else{
273  point_coordinates.push_back(p);
274  coordinatesCount++;
275  }
276  }
277  if(coordinatesCount != inCount*inDimension){
278  if(coordinatesCount%inDimension==0){
279  throw QhullError(10006, "Qhull error: expected %d %d-d PointCoordinates but read %i PointCoordinates", int(inCount), inDimension, 0.0, int(coordinatesCount/inDimension));
280  }else{
281  throw QhullError(10012, "Qhull error: expected %d %d-d PointCoordinates but read %i PointCoordinates plus %f extra coordinates", inCount, inDimension, float(coordinatesCount%inDimension), coordinatesCount/inDimension);
282  }
283  }
284  makeValid();
285 }//appendPoints istream
286 
287 PointCoordinates PointCoordinates::
288 operator+(const PointCoordinates &other) const
289 {
290  PointCoordinates pc= *this;
291  pc << other;
292  return pc;
293 }//operator+
294 
295 void PointCoordinates::
296 reserveCoordinates(countT newCoordinates)
297 {
298  // vector::reserve is not const
299  point_coordinates.reserve((countT)point_coordinates.size()+newCoordinates); // WARN64
300  makeValid();
301 }//reserveCoordinates
302 
303 #
304 
305 countT PointCoordinates::
306 indexOffset(countT i) const {
307  countT n= i*dimension();
308  countT coordinatesCount= point_coordinates.count();
309  if(i<0 || n>coordinatesCount){
310  throw QhullError(10061, "Qhull error: point_coordinates is too short (%d) for point %d", coordinatesCount, i);
311  }
312  return n;
313 }
314 
315 }//namespace orgQhull
316 
317 #
318 
319 using std::endl;
320 using std::ostream;
321 
322 using orgQhull::Coordinates;
323 using orgQhull::PointCoordinates;
324 
325 ostream&
326 operator<<(ostream &os, const PointCoordinates &p)
327 {
328  p.checkValid();
329  countT count= p.count();
330  int dimension= p.dimension();
331  string comment= p.comment();
332  if(comment.empty()){
333  os << dimension << endl;
334  }else{
335  os << dimension << " " << comment << endl;
336  }
337  os << count << endl;
338  Coordinates::ConstIterator c= p.beginCoordinates();
339  for(countT i=0; i<count; i++){
340  for(int j=0; j<dimension; j++){
341  os << *c++ << " ";
342  }
343  os << endl;
344  }
345  return os;
346 }//operator<<
347 
std::ostream & operator<<(std::ostream &sout, const SelectiveDynamics &sel)
Write SelectiveDynamics options.
Definition: VaspIO.hh:63
QhullRidge – Qhull's ridge structure, ridgeT, as a C++ class.
Definition: Coordinates.cpp:20
GenericCluster< CoordType > operator+(const GenericCluster< CoordType > &LHS, const Coordinate &RHS)
create translated cluster
void swap(ConfigDoF &A, ConfigDoF &B)
Definition: ConfigDoF.cc:195