libspatialindex API Reference  (git-trunk)
MovingPoint.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 <cmath>
30 #include <limits>
31 
33 
34 using namespace SpatialIndex;
35 
37 = default;
38 
39 MovingPoint::MovingPoint( const double* pCoords,
40  const double* pVCoords,
41  const IInterval& ti,
42  uint32_t dimension)
43 {
44  initialize( pCoords,
45  pVCoords,
46  ti.getLowerBound(),
47  ti.getUpperBound(),
48  dimension);
49 }
50 
51 MovingPoint::MovingPoint( const double* pCoords,
52  const double* pVCoords,
53  double tStart,
54  double tEnd,
55  uint32_t dimension)
56 {
57  initialize(pCoords, pVCoords, tStart, tEnd, dimension);
58 }
59 
61  const Point& vp,
62  const IInterval& ti)
63 {
64  if (p.m_dimension != vp.m_dimension)
65  throw Tools::IllegalArgumentException("MovingPoint: Points have different number of dimensions.");
66 
67  initialize( p.m_pCoords,
68  vp.m_pCoords,
69  ti.getLowerBound(),
70  ti.getUpperBound(),
71  p.m_dimension);
72 }
73 
74 MovingPoint::MovingPoint(const Point& p, const Point& vp, double tStart, double tEnd)
75 {
76  if (p.m_dimension != vp.m_dimension)
77  throw Tools::IllegalArgumentException("MovingPoint: Points have different number of dimensions.");
78 
79  initialize( p.m_pCoords,
80  vp.m_pCoords,
81  tStart,
82  tEnd,
83  p.m_dimension);
84 }
85 
87 {
89  m_endTime = p.m_endTime;
90  m_pCoords = nullptr;
91 
93 
94  try
95  {
96  m_pCoords = new double[m_dimension];
97  m_pVCoords = new double[m_dimension];
98  }
99  catch (...)
100  {
101  delete[] m_pCoords;
102  throw;
103  }
104 
105  memcpy(m_pCoords, p.m_pCoords, m_dimension * sizeof(double));
106  memcpy(m_pVCoords, p.m_pVCoords, m_dimension * sizeof(double));
107 }
108 
110 {
111  delete[] m_pVCoords;
112 }
113 
114 void MovingPoint::initialize(
115  const double* pCoords, const double* pVCoords,
116  double tStart, double tEnd, uint32_t dimension)
117 {
118  m_dimension = dimension;
119  m_startTime = tStart;
120  m_endTime = tEnd;
121  m_pCoords = nullptr;
122 
123  if (m_endTime <= m_startTime)
124  throw Tools::IllegalArgumentException("MovingPoint: Cannot support degenerate time intervals.");
125 
126  try
127  {
128  m_pCoords = new double[m_dimension];
129  m_pVCoords = new double[m_dimension];
130  }
131  catch (...)
132  {
133  delete[] m_pCoords;
134  throw;
135  }
136 
137  // first store the point coordinates, than the point velocities.
138  memcpy(m_pCoords, pCoords, m_dimension * sizeof(double));
139  memcpy(m_pVCoords, pVCoords, m_dimension * sizeof(double));
140 }
141 
143 {
144  if (this != &p)
145  {
147  memcpy(m_pCoords, p.m_pCoords, m_dimension * sizeof(double));
148  memcpy(m_pVCoords, p.m_pVCoords, m_dimension * sizeof(double));
149 
151  m_endTime = p.m_endTime;
152  }
153 
154  return *this;
155 }
156 
158 {
159  if (
160  m_startTime < p.m_startTime - std::numeric_limits<double>::epsilon() ||
161  m_startTime > p.m_startTime + std::numeric_limits<double>::epsilon() ||
162  m_endTime < p.m_endTime - std::numeric_limits<double>::epsilon() ||
163  m_endTime > p.m_endTime + std::numeric_limits<double>::epsilon())
164  return false;
165 
166  for (uint32_t cDim = 0; cDim < 2 * m_dimension; ++cDim)
167  {
168  if (
169  m_pCoords[cDim] < p.m_pCoords[cDim] - std::numeric_limits<double>::epsilon() ||
170  m_pCoords[cDim] > p.m_pCoords[cDim] + std::numeric_limits<double>::epsilon() ||
171  m_pVCoords[cDim] < p.m_pVCoords[cDim] - std::numeric_limits<double>::epsilon() ||
172  m_pVCoords[cDim] > p.m_pVCoords[cDim] + std::numeric_limits<double>::epsilon())
173  return false;
174  }
175 
176  return true;
177 }
178 
179 
180 double MovingPoint::getCoord(uint32_t d, double t) const
181 {
183 
184  if (t >= m_endTime) return m_pCoords[d] + m_pVCoords[d] * (m_endTime - m_startTime);
185  else if (t <= m_startTime) return m_pCoords[d];
186  else return m_pCoords[d] + m_pVCoords[d] * (t - m_startTime);
187 }
188 
189 double MovingPoint::getProjectedCoord(uint32_t d, double t) const
190 {
192 
193  return m_pCoords[d] + m_pVCoords[d] * (t - m_startTime);
194 }
195 
196 double MovingPoint::getVCoord(uint32_t d) const
197 {
199 
200  return m_pVCoords[d];
201 }
202 
203 void MovingPoint::getPointAtTime(double t, Point& out) const
204 {
206  for (uint32_t cDim = 0; cDim < m_dimension; ++cDim)
207  {
208  out.m_pCoords[cDim] = getCoord(cDim, t);
209  }
210 }
211 
212 //
213 // IObject interface
214 //
216 {
217  return new MovingPoint(*this);
218 }
219 
220 //
221 // ISerializable interface
222 //
224 {
225  return (sizeof(uint32_t) + 2 * sizeof(double) + 2 * m_dimension * sizeof(double));
226 }
227 
228 void MovingPoint::loadFromByteArray(const uint8_t* ptr)
229 {
230  uint32_t dimension;
231  memcpy(&dimension, ptr, sizeof(uint32_t));
232  ptr += sizeof(uint32_t);
233  memcpy(&m_startTime, ptr, sizeof(double));
234  ptr += sizeof(double);
235  memcpy(&m_endTime, ptr, sizeof(double));
236  ptr += sizeof(double);
237 
238  makeDimension(dimension);
239  memcpy(m_pCoords, ptr, m_dimension * sizeof(double));
240  ptr += m_dimension * sizeof(double);
241  memcpy(m_pVCoords, ptr, m_dimension * sizeof(double));
242  //ptr += m_dimension * sizeof(double);
243 }
244 
245 void MovingPoint::storeToByteArray(uint8_t** data, uint32_t& len)
246 {
247  len = getByteArraySize();
248  *data = new uint8_t[len];
249  uint8_t* ptr = *data;
250 
251  memcpy(ptr, &m_dimension, sizeof(uint32_t));
252  ptr += sizeof(uint32_t);
253  memcpy(ptr, &m_startTime, sizeof(double));
254  ptr += sizeof(double);
255  memcpy(ptr, &m_endTime, sizeof(double));
256  ptr += sizeof(double);
257  memcpy(ptr, m_pCoords, m_dimension * sizeof(double));
258  ptr += m_dimension * sizeof(double);
259  memcpy(ptr, m_pVCoords, m_dimension * sizeof(double));
260  //ptr += m_dimension * sizeof(double);
261 }
262 
263 //
264 // IEvolvingShape interface
265 //
266 void MovingPoint::getVMBR(Region& out) const
267 {
269  memcpy(out.m_pLow, m_pVCoords, m_dimension * sizeof(double));
270  memcpy(out.m_pHigh, m_pVCoords, m_dimension * sizeof(double));
271 }
272 
273 void MovingPoint::getMBRAtTime(double t, Region& out) const
274 {
276  for (uint32_t cDim = 0; cDim < m_dimension; ++cDim)
277  {
278  out.m_pLow[cDim] = getCoord(cDim, t);
279  out.m_pHigh[cDim] = getCoord(cDim, t);
280  }
281 }
282 
283 void MovingPoint::makeInfinite(uint32_t dimension)
284 {
285  makeDimension(dimension);
286  for (uint32_t cIndex = 0; cIndex < m_dimension; ++cIndex)
287  {
288  m_pCoords[cIndex] = std::numeric_limits<double>::max();
289  m_pVCoords[cIndex] = -std::numeric_limits<double>::max();
290  }
291 
292  m_startTime = std::numeric_limits<double>::max();
293  m_endTime = -std::numeric_limits<double>::max();
294 }
295 
296 void MovingPoint::makeDimension(uint32_t dimension)
297 {
298  if (m_dimension != dimension)
299  {
300  delete[] m_pCoords;
301  delete[] m_pVCoords;
302  m_pCoords = nullptr; m_pVCoords = nullptr;
303 
304  m_dimension = dimension;
305  m_pCoords = new double[m_dimension];
306  m_pVCoords = new double[m_dimension];
307  }
308 }
309 
310 std::ostream& SpatialIndex::operator<<(std::ostream& os, const MovingPoint& pt)
311 {
312  uint32_t i;
313 
314  os << "Coords: ";
315  for (i = 0; i < pt.m_dimension; ++i)
316  {
317  os << pt.m_pCoords[i] << " ";
318  }
319 
320  os << "VCoords: ";
321  for (i = 0; i < pt.m_dimension; ++i)
322  {
323  os << pt.m_pVCoords[i] << " ";
324  }
325 
326  os << ", Start: " << pt.m_startTime << ", End: " << pt.m_endTime;
327 
328  return os;
329 }
virtual double getCoord(uint32_t index, double t) const
Definition: MovingPoint.cc:180
virtual double getVCoord(uint32_t index) const
Definition: MovingPoint.cc:196
void makeDimension(uint32_t dimension) override
Definition: MovingPoint.cc:296
MovingPoint * clone() override
Definition: MovingPoint.cc:215
void getMBRAtTime(double t, Region &out) const override
Definition: MovingPoint.cc:273
virtual MovingPoint & operator=(const MovingPoint &p)
Definition: MovingPoint.cc:142
virtual double getProjectedCoord(uint32_t index, double t) const
Definition: MovingPoint.cc:189
virtual void getPointAtTime(double t, Point &out) const
Definition: MovingPoint.cc:203
virtual bool operator==(const MovingPoint &p) const
Definition: MovingPoint.cc:157
void getVMBR(Region &out) const override
Definition: MovingPoint.cc:266
void loadFromByteArray(const uint8_t *data) override
Definition: MovingPoint.cc:228
void makeInfinite(uint32_t dimension) override
Definition: MovingPoint.cc:283
uint32_t getByteArraySize() override
Definition: MovingPoint.cc:223
void storeToByteArray(uint8_t **data, uint32_t &len) override
Definition: MovingPoint.cc:245
double * m_pCoords
Definition: Point.h:78
uint32_t m_dimension
Definition: Point.h:77
virtual void makeDimension(uint32_t dimension)
Definition: Point.cc:243
double * m_pHigh
Definition: Region.h:99
virtual void makeDimension(uint32_t dimension)
Definition: Region.cc:561
double * m_pLow
Definition: Region.h:98
SIDX_DLL std::ostream & operator<<(std::ostream &os, const Ball &ball)
Definition: Ball.cc:215