forked from patrickdemond/OVis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovUtilities.h.in
317 lines (272 loc) · 8.93 KB
/
ovUtilities.h.in
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*=========================================================================
Program: ovis (OrlandoVision)
Module: ovUtilities.h
Language: C++
Author: Patrick Emond <[email protected]>
=========================================================================*/
//
// .SECTION Description
// A utilities header to include typedefs, macros, global functions, etc.
//
#ifndef __vtkUtilities_h
#define __vtkUtilities_h
#define OVIS_VERSION_MAJOR @OVIS_VERSION_MAJOR@
#define OVIS_VERSION_MINOR @OVIS_VERSION_MINOR@
#define OVIS_VERSION_PATCH @OVIS_VERSION_PATCH@
#define OVIS_RESOURCES_DIR "@OVIS_RESOURCES_DIR@"
// for atoi
#include <stdio.h>
#include <stdlib.h>
// STL headers and typedefs
#include <vtkstd/map>
#include <vtkstd/stack>
#include <vtkstd/string>
#include <vtkstd/vector>
// For the stemming algorithm
#include "stemming/english_stem.h"
typedef vtkstd::string ovString;
typedef vtkstd::stack< ovString > ovStringStack;
typedef vtkstd::vector< int > ovIntVector;
typedef vtkstd::vector< ovString > ovStringVector;
struct ovSearchTerm
{
ovString term;
bool stemming;
bool notLogic;
bool andLogic;
ovSearchTerm()
{
this->Clear();
}
void Clear()
{
this->term = "";
this->stemming = false;
this->notLogic = false;
this->andLogic = true;
}
void copy( const ovSearchTerm &rhs )
{
this->term = rhs.term;
this->stemming = rhs.stemming;
this->notLogic = rhs.notLogic;
this->andLogic = rhs.andLogic;
}
};
typedef vtkstd::vector< ovSearchTerm* > ovSearchTermVector;
//-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-
struct safe_delete
{
template <typename T>
void operator()( T *&p ) { if( p ) { delete p; p = 0; } };
};
//-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-
inline bool ovIsOppositeColorWhite( double r, double g, double b, double a = 1.0 ) { return r + g + b <= 0.66; }
inline bool ovIsOppositeColorWhite( double *rgba ) { return ovIsOppositeColorWhite( rgba[0], rgba[1], rgba[2] ); }
//-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-
struct ovDate
{
int year;
int month;
int day;
ovDate() : year( 0 ), month( 0 ), day( 0 ) {}
ovDate( int y, int m, int d ) : year( y ), month( m ), day( d ) { this->Validate(); }
ovDate( int date ) { this->SetDate( date ); }
ovDate( const char *date ) { this->SetDate( date ); }
static int DaysInMonth( int year, int month )
{
if( 1 == month || 3 == month || 5 == month || 7 == month ||
8 == month || 10 == month || 12 == month ) return 31;
if( 4 == month || 6 == month || 9 == month || 11 == month ) return 30;
// The only month left is February, the "our calendar doesn't actually work
// so let's use this month to try and fix it" month. We need to determine
// whether or not the given year is a leap year. The algorithm to determine
// whether this is a leap year taken from wikipedia:
// http://en.wikipedia.org/wiki/Leap_year
bool leapYear = 0 == year % 400 ? true
: 0 == year % 100 ? false
: 0 == year % 4 ? true
: false;
return leapYear ? 29 : 28;
}
void SetDate( const char *date )
{
this->year = 0;
this->month = 0;
this->day = 0;
ovString dateStr = ovString( date );
size_t first = dateStr.find( '-' );
if( vtkstd::string::npos != first )
{
this->year = atoi( dateStr.substr( 0, first ).c_str() );
size_t second = dateStr.find( '-', first + 1 );
if( vtkstd::string::npos != second )
{
this->month = atoi( dateStr.substr( first + 1, second - first - 1 ).c_str() );
this->day = atoi( dateStr.substr( second + 1, dateStr.length() - second - 1 ).c_str() );
}
}
this->Validate();
}
void SetDate( int date )
{
this->day = date % 100;
this->month = ( ( date - this->day ) % 10000 ) / 100;
this->year = ( date - this->month - this->day ) / 10000;
this->Validate();
}
bool IsSet() { return 0 < this->year; } const
int ToInt() { return 10000 * this->year + 100 * month + day; } const
void ToString( ovString &dateString ) const
{
char buffer[64];
sprintf( buffer,
this->year && this->month && this->day ? "%d-%02d-%02d CE" :
this->year && this->month ? "%d-%02d CE" :
this->year ? "%d CE" : "",
this->year, this->month, this->day );
dateString = buffer;
}
void Validate()
{
// range checking
if( 0 > this->year ) this->year = 0;
if( 0 > this->month || 12 < this->month ) this->month = 0;
// not necessary to bother with exact number of days per month
if( 0 > this->day || ovDate::DaysInMonth( this->year, this->month ) < this->day ) this->day = 0;
}
bool operator == ( const ovDate &rhs ) const
{
return this->year == rhs.year &&
this->month == rhs.month &&
this->day == rhs.day;
}
bool operator < ( const ovDate &rhs ) const
{
return this->year < rhs.year ||
( this->year == rhs.year &&
this->month < rhs.month ) ||
( this->year == rhs.year &&
this->month == rhs.month &&
this->day < rhs.day );
}
bool operator <= ( const ovDate &rhs ) const { return *this == rhs || *this < rhs; }
bool operator > ( const ovDate &rhs ) const { return rhs < *this; }
bool operator >= ( const ovDate &rhs ) const { return rhs <= *this; }
bool operator != ( const ovDate &rhs ) const { return !( *this == rhs ); }
};
//-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-
struct ovDatePeriod
{
ovString name;
ovDate start;
ovDate end;
ovDatePeriod( ovString n, ovDate s, ovDate e ) : name( n ), start( s ), end( e ) {}
};
typedef vtkstd::vector< ovDatePeriod* > ovDatePeriodVector;
//-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-
struct ovTag
{
ovString parent;
ovString name;
ovString title;
bool active;
bool expanded;
double color[4];
ovTag() : parent( "" ), name( "" ), title( "" ), active( false )
{ this->expanded = false; }
ovTag( ovString p, ovString n, ovString t, int a = 1 ) :
parent( p ), name( n ), title( t ), active( a )
{ this->expanded = false; }
void DeepCopy( const ovTag* tag )
{
this->parent = tag->parent;
this->name = tag->name;
this->title = tag->title;
this->active = tag->active;
this->expanded = tag->expanded;
for( int i = 0; i < 4; i++ ) this->color[i] = tag->color[i];
}
bool operator == ( const ovTag &rhs ) const
{
return this->parent == rhs.parent &&
this->name == rhs.name &&
this->title == rhs.title &&
this->active == rhs.active &&
this->expanded == rhs.expanded &&
this->color[0] == rhs.color[0] &&
this->color[1] == rhs.color[1] &&
this->color[2] == rhs.color[2] &&
this->color[3] == rhs.color[3];
}
bool operator != ( const ovTag &rhs ) const { return !( (*this) == rhs ); }
};
typedef vtkstd::vector< ovTag* > ovTagVector;
//-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-
inline void wstrToStr( const std::wstring &wstr, ovString &str )
{
str.clear();
for( size_t i = 0; i < wstr.size(); i++ )
{
wchar_t w = wstr[i];
if( w <= 0x7f )
{
str.push_back( (char)w );
}
else if( w <= 0x7ff )
{
str.push_back( 0xc0 | ( ( w >> 6 ) & 0x1f ) );
str.push_back( 0x80 | ( w & 0x3f ) );
}
else if( w <= 0xffff )
{
str.push_back( 0xe0 | ( ( w >> 12 ) & 0x0f ) );
str.push_back( 0x80 | ( ( w >> 6 ) & 0x3f ) );
str.push_back( 0x80 | ( w & 0x3f ) );
}
else if( w <= 0x10ffff )
{
str.push_back( 0xf0 | ( ( w >> 18 ) & 0x07 ) );
str.push_back( 0x80 | ( ( w >> 12 ) & 0x3f ) );
str.push_back( 0x80 | ( ( w >> 6 ) & 0x3f ) );
str.push_back( 0x80 | ( w & 0x3f ) );
}
else
{
str.push_back( '?' );
}
}
}
//-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-+#+-
inline ovString stemWords( ovString words )
{
ovString output, stem;
vtkstd::wstring wstr;
const char *deliminators = " `~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?";
stemming::english_stem<> StemEnglish;
char *buffer = new char[ words.size() + 1 ];
std::copy( words.begin(), words.end(), buffer );
buffer[ words.size() ] = '\0';
const char *ptr = strtok( buffer, deliminators );
bool first = true;
while( NULL != ptr )
{
// convert token to a wstring
size_t len = strlen( ptr );
wchar_t* wbuffer = new wchar_t[ len + 1 ];
vtkstd::wmemset( wbuffer, 0, len + 1 );
std::mbstowcs( wbuffer, ptr, len );
wstr = wbuffer;
// find the stem and convert to UTF-8
StemEnglish( wstr );
wstrToStr( wstr, stem );
if( !first ) output += " ";
else first = false;
output += stem;
// clean up and move to the next word
delete [] wbuffer;
ptr = strtok( NULL, deliminators );
}
return output;
}
#endif // __vtkUtilities_h