Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkUIDGenerator.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include <mitkLogMacros.h>
18 #include <mitkUIDGenerator.h>
19 
20 #include <cstdlib>
21 #include <iostream>
22 #include <math.h>
23 #include <sstream>
24 #include <stdexcept>
25 
26 mitk::UIDGenerator::UIDGenerator(const char *prefix, unsigned int lengthOfRandomPart)
27  : m_Prefix(prefix),
28  m_LengthOfRandomPart(lengthOfRandomPart),
29  m_RandomGenerator(itk::Statistics::MersenneTwisterRandomVariateGenerator::New())
30 {
31  if (lengthOfRandomPart < 5)
32  {
33  MITK_ERROR << "To few digits requested (" << lengthOfRandomPart << " digits)";
34  throw std::invalid_argument("To few digits requested");
35  }
36 
37  m_RandomGenerator->Initialize();
38 }
39 
41 {
42  std::ostringstream s;
43  s << m_Prefix;
44  time_t tt = time(nullptr);
45  tm *t = gmtime(&tt);
46 
47  if (t)
48  {
49  s << t->tm_year + 1900;
50 
51  if (t->tm_mon < 9)
52  s << "0"; // add a 0 for months 1 to 9
53  s << t->tm_mon + 1;
54 
55  if (t->tm_mday < 10)
56  s << "0"; // add a 0 for days 1 to 9
57  s << t->tm_mday;
58 
59  if (t->tm_hour < 10)
60  s << "0"; // add a 0 for hours 1 to 9
61  s << t->tm_hour;
62 
63  if (t->tm_min < 10)
64  s << "0"; // add a 0 for minutes 1 to 9
65  s << t->tm_min;
66 
67  if (t->tm_sec < 10)
68  s << "0"; // add a 0 for seconds 1 to 9
69  s << t->tm_sec;
70 
71  std::ostringstream rs;
72  rs << (long int)(pow(10.0, double(m_LengthOfRandomPart)) / double(RAND_MAX) *
73  double(m_RandomGenerator->GetUniformVariate(0, RAND_MAX)));
74 
75  for (size_t i = rs.str().length(); i < m_LengthOfRandomPart; ++i) // add zeros for non available digits
76  {
77  s << "0";
78  }
79 
80  s << rs.str();
81  }
82 
83  return s.str();
84 }
#define MITK_ERROR
Definition: mitkLogMacros.h:24
UIDGenerator(const char *prefix="UID_", unsigned int lengthOfRandomPart=8)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.