libspatialindex API Reference  (git-trunk)
TimePoint.cc
Go to the documentation of this file.
1 /******************************************************************************
2  * Project: libspatialindex - A C++ library for spatial indexing
3  * Author: Marios Hadjieleftheriou, mhadji@gmail.com
4  ******************************************************************************
5  * Copyright (c) 2002, Marios Hadjieleftheriou
6  *
7  * All rights reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26 ******************************************************************************/
27 
28 #include <cstring>
29 #include <limits>
30 
32 
33 using namespace SpatialIndex;
34 
36  : Point(), m_startTime(-std::numeric_limits<double>::max()), m_endTime(std::numeric_limits<double>::max())
37 {
38 }
39 
40 TimePoint::TimePoint(const double* pCoords, const IInterval& ti, uint32_t dimension)
41  : Point(pCoords, dimension), m_startTime(ti.getLowerBound()), m_endTime(ti.getUpperBound())
42 {
43 }
44 
45 TimePoint::TimePoint(const double* pCoords, double tStart, double tEnd, uint32_t dimension)
46  : Point(pCoords, dimension), m_startTime(tStart), m_endTime(tEnd)
47 {
48 }
49 
50 TimePoint::TimePoint(const Point& p, const IInterval& ti)
51  : Point(p), m_startTime(ti.getLowerBound()), m_endTime(ti.getUpperBound())
52 {
53 }
54 
55 TimePoint::TimePoint(const Point& p, double tStart, double tEnd)
56  : Point(p), m_startTime(tStart), m_endTime(tEnd)
57 {
58 }
59 
61  : m_startTime(p.m_startTime), m_endTime(p.m_endTime)
62 {
64 
65  m_pCoords = new double[m_dimension];
66  memcpy(m_pCoords, p.m_pCoords, m_dimension * sizeof(double));
67 }
68 
70 = default;
71 
73 {
74  if (this != &p)
75  {
77  memcpy(m_pCoords, p.m_pCoords, m_dimension * sizeof(double));
79  m_endTime = p.m_endTime;
80  }
81 
82  return *this;
83 }
84 
85 bool TimePoint::operator==(const TimePoint& p) const
86 {
87  if (
88  m_startTime < p.m_startTime - std::numeric_limits<double>::epsilon() ||
89  m_startTime > p.m_startTime + std::numeric_limits<double>::epsilon() ||
90  m_endTime < p.m_endTime - std::numeric_limits<double>::epsilon() ||
91  m_endTime > p.m_endTime + std::numeric_limits<double>::epsilon())
92  return false;
93 
94  for (uint32_t cDim = 0; cDim < m_dimension; ++cDim)
95  {
96  if (
97  m_pCoords[cDim] < p.m_pCoords[cDim] - std::numeric_limits<double>::epsilon() ||
98  m_pCoords[cDim] > p.m_pCoords[cDim] + std::numeric_limits<double>::epsilon())
99  return false;
100  }
101 
102  return true;
103 }
104 
105 //
106 // IObject interface
107 //
109 {
110  return new TimePoint(*this);
111 }
112 
113 //
114 // ISerializable interface
115 //
117 {
118  return (sizeof(uint32_t) + 2 * sizeof(double) + m_dimension * sizeof(double));
119 }
120 
121 void TimePoint::loadFromByteArray(const uint8_t* ptr)
122 {
123  uint32_t dimension;
124  memcpy(&dimension, ptr, sizeof(uint32_t));
125  ptr += sizeof(uint32_t);
126  memcpy(&m_startTime, ptr, sizeof(double));
127  ptr += sizeof(double);
128  memcpy(&m_endTime, ptr, sizeof(double));
129  ptr += sizeof(double);
130 
131  makeDimension(dimension);
132  memcpy(m_pCoords, ptr, m_dimension * sizeof(double));
133  //ptr += m_dimension * sizeof(double);
134 }
135 
136 void TimePoint::storeToByteArray(uint8_t** data, uint32_t& len)
137 {
138  len = getByteArraySize();
139  *data = new uint8_t[len];
140  uint8_t* ptr = *data;
141 
142  memcpy(ptr, &m_dimension, sizeof(uint32_t));
143  ptr += sizeof(uint32_t);
144  memcpy(ptr, &m_startTime, sizeof(double));
145  ptr += sizeof(double);
146  memcpy(ptr, &m_endTime, sizeof(double));
147  ptr += sizeof(double);
148  memcpy(ptr, m_pCoords, m_dimension * sizeof(double));
149  //ptr += m_dimension * sizeof(double);
150 }
151 
152 //
153 // ITimeShape interface
154 //
156 {
157  const TimeRegion* pr = dynamic_cast<const TimeRegion*>(&in);
158  if (pr != nullptr) return pr->containsPointInTime(*this);
159 
160  throw Tools::IllegalStateException("intersectsShapeInTime: Not implemented yet!");
161 }
162 
163 bool TimePoint::intersectsShapeInTime(const IInterval&, const ITimeShape&) const
164 {
165  throw Tools::IllegalStateException("intersectsShapeInTime: Not implemented yet!");
166 }
167 
169 {
170  return false;
171 }
172 
173 bool TimePoint::containsShapeInTime(const IInterval&, const ITimeShape&) const
174 {
175  return false;
176 }
177 
179 {
180  throw Tools::IllegalStateException("touchesShapeInTime: Not implemented yet!");
181 }
182 
183 bool TimePoint::touchesShapeInTime(const IInterval&, const ITimeShape&) const
184 {
185  throw Tools::IllegalStateException("touchesShapeInTime: Not implemented yet!");
186 }
187 
189 {
190  return 0.0;
191 }
192 
193 double TimePoint::getAreaInTime(const IInterval&) const
194 {
195  return 0.0;
196 }
197 
199 {
200  return 0.0;
201 }
202 
203 double TimePoint::getIntersectingAreaInTime(const IInterval&, const ITimeShape&) const
204 {
205  return 0.0;
206 }
207 
208 //
209 // IInterval interface
210 //
212 {
213  if (this != &i)
214  {
216  m_endTime = i.getUpperBound();
217  }
218 
219  return *this;
220 }
221 
223 {
224  return m_startTime;
225 }
226 
228 {
229  return m_endTime;
230 }
231 
232 void TimePoint::setBounds(double l, double h)
233 {
234  assert(l <= h);
235 
236  m_startTime = l;
237  m_endTime = h;
238 }
239 
240 bool TimePoint::intersectsInterval(const IInterval& ti) const
241 {
242  return intersectsInterval(ti.getIntervalType(), ti.getLowerBound(), ti.getUpperBound());
243 }
244 
245 bool TimePoint::intersectsInterval(Tools::IntervalType, const double start, const double end) const
246 {
247  //if (m_startTime != start &&
248  // (m_startTime >= end || m_endTime <= start)) return false;
249  if (m_startTime >= end || m_endTime <= start) return false;
250 
251  return true;
252 }
253 
254 bool TimePoint::containsInterval(const IInterval& ti) const
255 {
256  if (m_startTime <= ti.getLowerBound() && m_endTime >= ti.getUpperBound()) return true;
257  return false;
258 }
259 
261 {
262  return Tools::IT_RIGHTOPEN;
263 }
264 
265 void TimePoint::makeInfinite(uint32_t dimension)
266 {
267  makeDimension(dimension);
268  for (uint32_t cIndex = 0; cIndex < m_dimension; ++cIndex)
269  {
270  m_pCoords[cIndex] = std::numeric_limits<double>::max();
271  }
272 
273  m_startTime = std::numeric_limits<double>::max();
274  m_endTime = -std::numeric_limits<double>::max();
275 }
276 
277 void TimePoint::makeDimension(uint32_t dimension)
278 {
279  if (m_dimension != dimension)
280  {
281  m_dimension = dimension;
282 
283  delete[] m_pCoords;
284  m_pCoords = nullptr;
285 
286  m_pCoords = new double[m_dimension];
287  }
288 }
289 
290 std::ostream& SpatialIndex::operator<<(std::ostream& os, const TimePoint& pt)
291 {
292  uint32_t i;
293 
294  for (i = 0; i < pt.m_dimension; ++i)
295  {
296  os << pt.m_pCoords[i] << " ";
297  }
298 
299  os << ", Start: " << pt.m_startTime << ", End: " << pt.m_endTime;
300 
301  return os;
302 }
double * m_pCoords
Definition: Point.h:78
uint32_t m_dimension
Definition: Point.h:77
bool containsShapeInTime(const ITimeShape &in) const override
Definition: TimePoint.cc:168
bool touchesShapeInTime(const ITimeShape &in) const override
Definition: TimePoint.cc:178
void makeInfinite(uint32_t dimension) override
Definition: TimePoint.cc:265
double getUpperBound() const override
Definition: TimePoint.cc:227
void loadFromByteArray(const uint8_t *data) override
Definition: TimePoint.cc:121
bool intersectsShapeInTime(const ITimeShape &in) const override
Definition: TimePoint.cc:155
Tools::IntervalType getIntervalType() const override
Definition: TimePoint.cc:260
void makeDimension(uint32_t dimension) override
Definition: TimePoint.cc:277
bool intersectsInterval(const Tools::IInterval &ti) const override
double getAreaInTime() const override
Definition: TimePoint.cc:188
virtual bool operator==(const TimePoint &p) const
Definition: TimePoint.cc:85
double getIntersectingAreaInTime(const ITimeShape &r) const override
Definition: TimePoint.cc:198
void setBounds(double, double) override
Definition: TimePoint.cc:232
TimePoint * clone() override
Definition: TimePoint.cc:108
double getLowerBound() const override
Definition: TimePoint.cc:222
void storeToByteArray(uint8_t **data, uint32_t &len) override
Definition: TimePoint.cc:136
virtual TimePoint & operator=(const TimePoint &p)
Definition: TimePoint.cc:72
uint32_t getByteArraySize() override
Definition: TimePoint.cc:116
bool containsInterval(const Tools::IInterval &ti) const override
Definition: TimePoint.cc:254
virtual bool containsPointInTime(const TimePoint &in) const
Definition: TimeRegion.cc:164
virtual double getLowerBound() const =0
virtual double getUpperBound() const =0
SIDX_DLL std::ostream & operator<<(std::ostream &os, const Ball &ball)
Definition: Ball.cc:215
IntervalType
Definition: Tools.h:81
@ IT_RIGHTOPEN
Definition: Tools.h:82