-
Notifications
You must be signed in to change notification settings - Fork 3
/
pbj.hpp
816 lines (753 loc) · 26.3 KB
/
pbj.hpp
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
/* PBJ Message Base Class
* pbj.hpp
*
* Copyright (c) 2009, Daniel Reiter Horn
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Sirikata nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _PBJ_PBJ_HPP_
#define _PBJ_PBJ_HPP_
// NOTE: You should have a ProjectPBJ.hpp in your include path. This defines
// the types you want PBJ to use for the project.
// FIXME currently we're still using things directly from the Sirikata namespace.
#include "sirikata/core/ProjectPBJ.hpp"
#include <google/protobuf/message.h>
#include <cmath>
namespace PBJ {
class HasFields {
public:
bool evaluateInput(bool inp)const {
return inp;
}
bool evaluateOutput(bool outp)const {
return true;
}
};
template <class HasAllPBJFieldsAble> class Message {
::google::protobuf::Message * _mMessage;
protected:
Message(::google::protobuf::Message *msg){
_mMessage=msg;
}
void setMessageRepresentation(::google::protobuf::Message * msg) {
_mMessage=msg;
}
public:
void CopyFrom(const Message&from) {
_mMessage->CopyFrom(*from._mMessage);
}
void MergeFrom(const Message&from) {
_mMessage->MergeFrom(*from._mMessage);
}
void Clear() {
_mMessage->Clear();
}
void DiscardUnknownFields() {
_mMessage->DiscardUnknownFields();
}
bool InputOK() const{
return static_cast<const HasAllPBJFieldsAble*>(this)->HasAllPBJFieldsAble::_HasAllPBJFields();
}
bool OutputOK() const{
assert(static_cast<const HasAllPBJFieldsAble*>(this)->HasAllPBJFieldsAble::_HasAllPBJFields());
return true;
}
bool IsInitialized() const {
return _mMessage->IsInitialized()&&static_cast<const HasAllPBJFieldsAble*>(this)->HasAllPBJFieldsAble::_HasAllPBJFields();
}
bool ParseFromCodedStream(::google::protobuf::io::CodedInputStream* input){
return _mMessage->ParseFromCodedStream(input)&&InputOK();
}
// Like ParseFromCodedStream(), but accepts messages that are missing
// required fields.
bool ParsePartialFromCodedStream(::google::protobuf::io::CodedInputStream* input){
return _mMessage->ParsePartialFromCodedStream(input);
}
// Read a protocol buffer from the given zero-copy input stream. If
// successful, the entire input will be consumed.
bool ParseFromZeroCopyStream(::google::protobuf::io::ZeroCopyInputStream* input){
return _mMessage->ParseFromZeroCopyStream(input)&&InputOK();
}
// Like ParseFromZeroCopyStream(), but accepts messages that are missing
// required fields.
bool ParsePartialFromZeroCopyStream(::google::protobuf::io::ZeroCopyInputStream* input){
return _mMessage->ParsePartialFromZeroCopyStream(input);
}
// Parse a protocol buffer contained in a std::string.
bool ParseFromString(const std::string& data){
return _mMessage->ParseFromString(data)&&InputOK();
}
// Like ParseFromString(), but accepts messages that are missing
// required fields.
bool ParsePartialFromString(const std::string& data){
return _mMessage->ParsePartialFromString(data);
}
// Parse a protocol buffer contained in an array of bytes.
bool ParseFromArray(const void* data, int size){
return _mMessage->ParseFromArray(data,size)&&InputOK();
}
// Like ParseFromArray(), but accepts messages that are missing
// required fields.
bool ParsePartialFromArray(const void* data, int size){
return _mMessage->ParsePartialFromArray(data,size);
}
// Parse a protocol buffer from a file descriptor. If successful, the entire
// input will be consumed.
bool ParseFromFileDescriptor(int file_descriptor){
return _mMessage->ParseFromFileDescriptor(file_descriptor)&&InputOK();
}
// Like ParseFromFileDescriptor(), but accepts messages that are missing
// required fields.
bool ParsePartialFromFileDescriptor(int file_descriptor){
return _mMessage->ParsePartialFromFileDescriptor(file_descriptor);
}
// Parse a protocol buffer from a C++ istream. If successful, the entire
// input will be consumed.
bool ParseFromIstream(std::istream* input){
return _mMessage->ParseFromIstream(input)&&InputOK();
}
// Like ParseFromIstream(), but accepts messages that are missing
// required fields.
bool ParsePartialFromIstream(std::istream* input){
return _mMessage->ParsePartialFromIstream(input);
}
// Reads a protocol buffer from the stream and merges it into this
// Message. Singular fields read from the input overwrite what is
// already in the Message and repeated fields are appended to those
// already present.
//
// It is the responsibility of the caller to call input->LastTagWas()
// (for groups) or input->ConsumedEntireMessage() (for non-groups) after
// this returns to verify that the message's end was delimited correctly.
//
// ParsefromCodedStream() is implemented as Clear() followed by
// MergeFromCodedStream().
bool MergeFromCodedStream(::google::protobuf::io::CodedInputStream* input){
return _mMessage->MergeFromCodedStream(input)&&InputOK();
}
// Like MergeFromCodedStream(), but succeeds even if required fields are
// missing in the input.
//
// MergeFromCodedStream() is just implemented as MergePartialFromCodedStream()
// followed by IsInitialized().
bool MergePartialFromCodedStream(::google::protobuf::io::CodedInputStream* input){
return _mMessage->MergePartialFromCodedStream(input);
}
// Serialization ---------------------------------------------------
// Methods for serializing in protocol buffer format. Most of these
// are just simple wrappers around ByteSize() and SerializeWithCachedSizes().
// Write a protocol buffer of this message to the given output. Returns
// false on a write error. If the message is missing required fields,
// this may GOOGLE_CHECK-fail.
bool SerializeToCodedStream(::google::protobuf::io::CodedOutputStream* output) const{
return _mMessage->SerializeToCodedStream(output)&&OutputOK();
}
// Like SerializeToCodedStream(), but allows missing required fields.
bool SerializePartialToCodedStream(::google::protobuf::io::CodedOutputStream* output) const{
return _mMessage->SerializePartialToCodedStream(output);
}
// Write the message to the given zero-copy output stream. All required
// fields must be set.
bool SerializeToZeroCopyStream(::google::protobuf::io::ZeroCopyOutputStream* output) const{
return _mMessage->SerializeToZeroCopyStream(output)&&OutputOK();
}
// Like SerializeToZeroCopyStream(), but allows missing required fields.
bool SerializePartialToZeroCopyStream(::google::protobuf::io::ZeroCopyOutputStream* output) const{
return _mMessage->SerializePartialToZeroCopyStream(output);
}
// Serialize the message and store it in the given std::string. All required
// fields must be set.
bool SerializeToString(std::string* output) const{
return _mMessage->SerializeToString(output)&&OutputOK();
}
// Like SerializeToString(), but allows missing required fields.
bool SerializePartialToString(std::string* output) const{
return _mMessage->SerializePartialToString(output);
}
// Serialize the message and store it in the given byte array. All required
// fields must be set.
bool SerializeToArray(void* data, int size) const{
return _mMessage->SerializeToArray(data,size)&&OutputOK();
}
// Like SerializeToArray(), but allows missing required fields.
bool SerializePartialToArray(void* data, int size) const{
return _mMessage->SerializePartialToArray(data,size);
}
// Serialize the message and write it to the given file descriptor. All
// required fields must be set.
bool SerializeToFileDescriptor(int file_descriptor) const{
return _mMessage->SerializeToFileDescriptor(file_descriptor)&&OutputOK();
}
// Like SerializeToFileDescriptor(), but allows missing required fields.
bool SerializePartialToFileDescriptor(int file_descriptor) const{
return _mMessage->SerializePartialToFileDescriptor(file_descriptor);
}
// Serialize the message and write it to the given C++ ostream. All
// required fields must be set.
bool SerializeToOstream(std::ostream* output) const{
return _mMessage->SerializeToOstream(output)&&OutputOK();
}
// Like SerializeToOstream(), but allows missing required fields.
bool SerializePartialToOstream(std::ostream* output) const{
return _mMessage->SerializePartialToOstream(output);
}
// Like SerializeToString(), but appends to the data to the std::string's existing
// contents. All required fields must be set.
bool AppendToString(std::string* output) const{
return _mMessage->AppendToString(output)&&OutputOK();
}
// Like AppendToString(), but allows missing required fields.
bool AppendPartialToString(std::string* output) const{
return _mMessage->AppendPartialToString(output)&&OutputOK();
}
// Computes the serialized size of the message. This recursively calls
// ByteSize() on all embedded messages. If a subclass does not override
// this, it MUST override SetCachedSize().
int ByteSize() const{
return _mMessage->ByteSize();
}
// Serializes the message without recomputing the size. The message must
// not have changed since the last call to ByteSize(); if it has, the results
// are undefined.
bool SerializeWithCachedSizes(::google::protobuf::io::CodedOutputStream* output) const{
return _mMessage->SerializeWithCachedSizes(output)&&OutputOK();
}
// Returns the result of the last call to ByteSize(). An embedded message's
// size is needed both to serialize it (because embedded messages are
// length-delimited) and to compute the outer message's size. Caching
// the size avoids computing it multiple times.
//
// ByteSize() does not automatically use the cached size when available
// because this would require invalidating it every time the message was
// modified, which would be too hard and expensive. (E.g. if a deeply-nested
// sub-message is changed, all of its parents' cached sizes would need to be
// invalidated, which is too much work for an otherwise inlined setter
// method.)
int GetCachedSize() const {
return _mMessage->GetCachedSize();
}
};
template <class T> class RefClass : public T{
public:
RefClass(const T&t):T(*const_cast<T*>(&t)){}
};
typedef Sirikata::SHA256 SHA256;
typedef Sirikata::UUID UUID;
typedef Sirikata::uint64 uint64;
typedef Sirikata::uint32 uint32;
typedef Sirikata::uint16 uint16;
typedef Sirikata::uint8 uint8;
typedef Sirikata::int64 int64;
typedef Sirikata::int32 int32;
typedef Sirikata::int16 int16;
typedef Sirikata::int8 int8;
typedef Sirikata::Time Time;
typedef Sirikata::Duration Duration;
typedef Sirikata::Vector2<float> Vector2f;
typedef Sirikata::Vector2<double> Vector2d;
typedef Sirikata::Vector3f Vector3f;
typedef Sirikata::Vector3d Vector3d;
typedef Sirikata::Vector4f Vector4f;
typedef Sirikata::Vector4d Vector4d;
typedef Sirikata::Quaternion Quaternion;
typedef Sirikata::BoundingBox3f3f BoundingBox3f3f;
typedef Sirikata::BoundingBox3d3f BoundingBox3d3f;
typedef Sirikata::BoundingSphere3f BoundingSphere3f;
typedef Sirikata::BoundingSphere3d BoundingSphere3d;
typedef Sirikata::SolidAngle SolidAngle;
typedef Sirikata::Array<float,2> Array2f;
typedef Sirikata::Array<double,2> Array2d;
typedef Sirikata::Array<float,3> Array3f;
typedef Sirikata::Array<double,3> Array3d;
typedef Sirikata::Array<float,4> Array4f;
typedef Sirikata::Array<double,4> Array4d;
typedef Sirikata::Array<float,6> Array6f;
typedef Sirikata::Array<double,6> Array6d;
class angle {};
class normal :public Vector3f{public:normal(const Vector3f&v):Vector3f(v){}};
class quaternion{};
class utf8string:public ::std::string{public:utf8string(const std::string&s):std::string(s){}};
class bytes:public ::std::string{public:bytes(const std::string&s):std::string(s){}};
}
template <typename convertTo> class _PBJConstruct {
public:
convertTo operator()(const convertTo&ct) {
return ct;
}
};
template <> class _PBJConstruct<PBJ::angle> {
public:
float operator()(const float &ang) {
return ang;
}
};
template <> class _PBJConstruct<PBJ::SolidAngle> {
public:
float operator()(const PBJ::SolidAngle &ang) {
return ang.asFloat();
}
};
template <> class _PBJConstruct<PBJ::Time> {
public:
PBJ::int64 operator()(const PBJ::Time &time) {
return (time-PBJ::Time::null()).toMicro();
}
};
template <> class _PBJConstruct<PBJ::Duration> {
public:
PBJ::int64 operator()(const PBJ::Duration &time) {
return time.toMicro();
}
};
template <> class _PBJConstruct<PBJ::UUID> {
public:
std::string operator()(const PBJ::UUID&ct) {
return std::string((const char*)ct.getArray().begin(),PBJ::UUID::static_size);
}
};
template <> class _PBJConstruct<PBJ::SHA256> {
public:
std::string operator()(const PBJ::SHA256&ct) {
return std::string((const char*)ct.rawData().begin(),(size_t)PBJ::SHA256::static_size);
}
};
template <> class _PBJConstruct<PBJ::utf8string> {
public:
const std::string& operator()(const std::string&ct) {
return ct;
}
};
template <> class _PBJConstruct<PBJ::bytes> {
public:
const std::string& operator()(const std::string&ct) {
return ct;
}
};
template <> class _PBJConstruct<PBJ::Vector2f> {
public:
typedef PBJ::Array2f ArrayType;
PBJ::Array2f operator()(const PBJ::Vector2f&ct) {
float data[2]={ct.x,ct.y};
return PBJ::Array2f::construct(data);
}
};
template <> class _PBJConstruct<PBJ::Vector2d> {
public:
typedef PBJ::Array2d ArrayType;
PBJ::Array2d operator()(const PBJ::Vector2d&ct) {
double data[2]={ct.x,ct.y};
return PBJ::Array2d::construct(data);
}
};
template <> class _PBJConstruct<PBJ::normal> {
public:
typedef PBJ::Array2f ArrayType;
PBJ::Array2f operator()(const PBJ::Vector3f&n) {
PBJ::Vector3f ct=n/n.length();
float data[2]={ct.x+(ct.z<0.0f?3.0f:0.0f),ct.y};
return PBJ::Array2f::construct(data);
}
};
template <> class _PBJConstruct<PBJ::Vector3f> {
public:
typedef PBJ::Array3f ArrayType;
PBJ::Array3f operator()(const PBJ::Vector3f&ct) {
float data[3]={ct.x,ct.y,ct.z};
return PBJ::Array3f::construct(data);
}
};
template <> class _PBJConstruct<PBJ::Vector3d> {
public:
typedef PBJ::Array3d ArrayType;
PBJ::Array3d operator()(const PBJ::Vector3d&ct) {
double data[3]={ct.x,ct.y,ct.z};
return PBJ::Array3d::construct(data);
}
};
template <> class _PBJConstruct<PBJ::Vector4f> {
public:
typedef PBJ::Array4f ArrayType;
PBJ::Array4f operator()(const PBJ::Vector4f&ct) {
float data[4]={ct.x,ct.y,ct.z,ct.w};
return PBJ::Array4f::construct(data);
}
};
template <> class _PBJConstruct<PBJ::Vector4d> {
public:
typedef PBJ::Array4d ArrayType;
PBJ::Array4d operator()(const PBJ::Vector4d&ct) {
double data[4]={ct.x,ct.y,ct.z,ct.w};
return PBJ::Array4d::construct(data);
}
};
template <> class _PBJConstruct<PBJ::BoundingSphere3f> {
public:
typedef PBJ::Array4f ArrayType;
PBJ::Array4f operator()(const PBJ::BoundingSphere3f&ct) {
float data[4]={ct.center().x,ct.center().y,ct.center().z,ct.radius()};
return PBJ::Array4f::construct(data);
}
};
template <> class _PBJConstruct<PBJ::BoundingSphere3d> {
public:
typedef PBJ::Array4d ArrayType;
PBJ::Array4d operator()(const PBJ::BoundingSphere3d&ct) {
double data[4]={ct.center().x,ct.center().y,ct.center().z,ct.radius()};
return PBJ::Array4d::construct(data);
}
};
template <> class _PBJConstruct<PBJ::Quaternion> {
public:
typedef PBJ::Array3f ArrayType;
PBJ::Array3f operator()(const PBJ::Quaternion&q) {
PBJ::Quaternion ct=q/q.length();
float data[3]={ct.x+(ct.w<0.0f?3.0f:0.0f),ct.y,ct.z};
return PBJ::Array3f::construct(data);
}
};
template <> class _PBJConstruct<PBJ::BoundingBox3f3f> {
public:
typedef PBJ::Array6f ArrayType;
PBJ::Array6f operator()(const PBJ::BoundingBox3f3f&ct) {
float data[6]={ct.center().x,ct.center().y,ct.center().z,ct.across().x,ct.across().y,ct.across().z};
return PBJ::Array6f::construct(data);
}
};
template <> class _PBJConstruct<PBJ::BoundingBox3d3f> {
public:
typedef PBJ::Array6d ArrayType;
PBJ::Array6d operator()(const PBJ::BoundingBox3d3f&ct) {
double data[6]={ct.center().x,ct.center().y,ct.center().z,ct.across().x,ct.across().y,ct.across().z};
return PBJ::Array6d::construct(data);
}
};
#ifdef _WIN32
template <class Type> inline bool _PBJIsFinite(Type val) {
return Type::error_not_supported;
}
template <> inline bool _PBJIsFinite<float>(float val) {
PBJ::uint32 ieeeval = (PBJ::uint32&)val;
PBJ::uint32 infmask = 0x7f800000;
return (ieeeval & infmask) != infmask; // check less than "infinity"
}
template <> inline bool _PBJIsFinite<double>(double val) {
PBJ::uint64 ieeeval = (PBJ::uint64&)val;
PBJ::uint64 infmask = (((PBJ::uint64)0x7ff00000)<<32);
return (ieeeval & infmask) != infmask; // check less than "infinity"
}
#else
template <class Type> inline bool _PBJIsFinite(Type val) {
return std::isfinite(val);
}
#endif
template <typename convertFrom, typename convertTo> class _PBJCastMessage {
public:
template <typename Type> const Type& operator()(const Type&ct) {
return ct;
}
const convertTo& operator()() {
static convertFrom retval;
return retval;;
}
};
template <typename convertTo> class _PBJCast {
public:
template <typename Type> const Type& operator()(const Type&ct) {
return ct;
}
convertTo operator()() {
return (convertTo)0;
}
};
template <> class _PBJCast<PBJ::utf8string> {
public:
const std::string& operator()(const std::string& s) {
return s;
}
const std::string operator()() {
return std::string();
}
};
template <> class _PBJCast<PBJ::bytes> {
public:
const std::string& operator()(const std::string& s) {
return s;
}
const std::string operator()() {
return std::string();
}
};
template <> class _PBJCast<PBJ::normal> {
public:
PBJ::Vector3f operator()() {
return PBJ::Vector3f(0,0,1);
}
PBJ::Vector3f operator()(float x, float y) {
if (!_PBJIsFinite(x) || !_PBJIsFinite(y)) {
return PBJ::Vector3f(0,0,0);
}
float neg=(x>1.5f||y>1.5f)?-1.0f:1.0f;
if (x>1.5f)
x-=3.0f;
if (y>1.5f)
y-=3.0f;
return PBJ::Vector3f(x,y,neg*sqrt(1-x*x-y*y));
}
};
template <> class _PBJCast<PBJ::UUID> {
public:
PBJ::UUID operator()(const std::string bytes) {
try {
return PBJ::UUID(bytes,PBJ::UUID::BinaryString());
}catch (std::invalid_argument&) {
static const char nilcstr[16]={0};
static const std::string nilstr(nilcstr,16);
return PBJ::UUID(nilstr,PBJ::UUID::BinaryString());
}
}
PBJ::UUID operator()() {
return PBJ::UUID::null();
}
};
template <> class _PBJCast<PBJ::SHA256> {
public:
PBJ::SHA256 operator()(const std::string bytes) {
return PBJ::SHA256::convertFromBinary(bytes.data());
}
PBJ::SHA256 operator()() {
return PBJ::SHA256::null();
}
};
template <> class _PBJCast<PBJ::angle> {
public:
float operator()(const float ang) {
if (!_PBJIsFinite(ang)) {
return operator()();
}
return ang;
}
float operator()() {
return 0;
}
};
template <> class _PBJCast<PBJ::SolidAngle> {
public:
PBJ::SolidAngle operator()(const float ang) {
if (!_PBJIsFinite(ang)) {
return operator()();
}
return PBJ::SolidAngle(ang);
}
PBJ::SolidAngle operator()() {
return PBJ::SolidAngle(0.f);
}
};
template <> class _PBJCast<PBJ::Time> {
public:
PBJ::Time operator()(const PBJ::int64 &time) {
return PBJ::Time::microseconds(time);
}
PBJ::Time operator()() {
return PBJ::Time::null();
}
};
template <> class _PBJCast<PBJ::Duration> {
public:
PBJ::Duration operator()(const PBJ::int64 &time) {
return PBJ::Duration::microseconds(time);
}
PBJ::Duration operator()() {
return PBJ::Duration::seconds(0);
}
};
template <> class _PBJCast<PBJ::Quaternion> {
public:
PBJ::Quaternion operator()(float x, float y,float z) {
if (!_PBJIsFinite(x) || !_PBJIsFinite(y) || !_PBJIsFinite(z)) {
return operator()();
}
float neg=(x>1.5f||y>1.5f||z>1.5f)?-1.0f:1.0f;
if (x>1.5f) x-=3.0f;
if (y>1.5f) y-=3.0f;
if (z>1.5f) z-=3.0f;
return PBJ::Quaternion(x,y,z,neg*sqrt(1-x*x-y*y-z*z),PBJ::Quaternion::XYZW());
}
PBJ::Quaternion operator()() {
return PBJ::Quaternion::identity();
}
};
template <> class _PBJCast<PBJ::Vector2f> {
public:
PBJ::Vector2f operator()(float x, float y) {
return PBJ::Vector2f(x,y);
}
PBJ::Vector2f operator()() {
return PBJ::Vector2f(0,0);
}
};
template <> class _PBJCast<PBJ::Vector2d> {
public:
PBJ::Vector2d operator()(double x, double y) {
return PBJ::Vector2d(x,y);
}
PBJ::Vector2d operator()() {
return PBJ::Vector2d(0,0);
}
};
template <> class _PBJCast<PBJ::Vector3f> {
public:
PBJ::Vector3f operator()(float x, float y, float z) {
return PBJ::Vector3f(x,y,z);
}
PBJ::Vector3f operator()() {
return PBJ::Vector3f(0,0,0);
}
};
template <> class _PBJCast<PBJ::Vector3d> {
public:
PBJ::Vector3d operator()(double x, double y, double z) {
return PBJ::Vector3d(x,y,z);
}
PBJ::Vector3d operator()() {
return PBJ::Vector3d(0,0,0);
}
};
template <> class _PBJCast<PBJ::Vector4f> {
public:
PBJ::Vector4f operator()(float x, float y, float z, float w) {
return PBJ::Vector4f(x,y,z,w);
}
PBJ::Vector4f operator()() {
return PBJ::Vector4f(0,0,0,0);
}
};
template <> class _PBJCast<PBJ::Vector4d> {
public:
PBJ::Vector4d operator()(double x, double y, double z, double w) {
return PBJ::Vector4d(x,y,z,w);
}
PBJ::Vector4d operator()() {
return PBJ::Vector4d(0,0,0,0);
}
};
template <> class _PBJCast<PBJ::BoundingSphere3f> {
public:
PBJ::BoundingSphere3f operator()(float x, float y, float z, float r) {
if (!_PBJIsFinite(x) || !_PBJIsFinite(y) || !_PBJIsFinite(z)) {
x=y=z=0;
}
if (r<0) {
r=0;
}
return PBJ::BoundingSphere3f(PBJ::Vector3f(x,y,z),r);
}
PBJ::BoundingSphere3f operator()() {
return PBJ::BoundingSphere3f(PBJ::Vector3f(0,0,0),0);
}
};
template <> class _PBJCast<PBJ::BoundingSphere3d> {
public:
PBJ::BoundingSphere3d operator()(double x, double y, double z, float r) {
if (!_PBJIsFinite(x) || !_PBJIsFinite(y) || !_PBJIsFinite(z)) {
x=y=z=0;
}
if (r<0) {
r=0;
}
return PBJ::BoundingSphere3d(PBJ::Vector3d(x,y,z),r);
}
PBJ::BoundingSphere3d operator()() {
return PBJ::BoundingSphere3d(PBJ::Vector3d(0,0,0),0);
}
};
template <> class _PBJCast<PBJ::BoundingBox3f3f> {
public:
PBJ::BoundingBox3f3f operator()(float x, float y, float z, float w, float h, float d) {
if (!_PBJIsFinite(x) || !_PBJIsFinite(y) || !_PBJIsFinite(z)) {
x=y=z=0;
}
if (w<0) w=0;
if (h<0) h=0;
if (d<0) d=0;
return PBJ::BoundingBox3f3f(PBJ::Vector3f(x,y,z),PBJ::Vector3f(x+w,y+h,z+d));
}
PBJ::BoundingBox3f3f operator()() {
return PBJ::BoundingBox3f3f(PBJ::Vector3f(0,0,0),PBJ::Vector3f(0,0,0));
}
};
template <> class _PBJCast<PBJ::BoundingBox3d3f> {
public:
PBJ::BoundingBox3d3f operator()(double x, double y, double z, double w, double h, double d) {
if (!_PBJIsFinite(x) || !_PBJIsFinite(y) || !_PBJIsFinite(z)) {
x=y=z=0;
}
if (w<0) w=0;
if (h<0) h=0;
if (d<0) d=0;
return PBJ::BoundingBox3d3f(PBJ::Vector3d(x,y,z),PBJ::Vector3d(x+w,y+h,z+d));
}
PBJ::BoundingBox3d3f operator()() {
return PBJ::BoundingBox3d3f(PBJ::Vector3d(0,0,0),0);
}
};
template <typename Type> class _PBJValidate {
public:
template <typename Input> bool operator()(const Input&ct) {
return true;
}
};
template <> class _PBJValidate<PBJ::utf8string> {
public:
bool operator()(const std::string&ct) {
return true;
}
};
template <> class _PBJValidate<PBJ::angle> {
public:
bool operator()(const float&ct) {
return ct>=0&&ct<=2*3.1415926536;
}
};
template <> class _PBJValidate<PBJ::SolidAngle> {
public:
bool operator()(const float&ct) {
return ct>=0&&ct<=4*3.1415926536;
}
};
template <typename Type> class _PBJValidateFlags {
public:
bool operator() (Type input, Type allFlagsOn) {
return (input|allFlagsOn)==allFlagsOn;
}
};
template <typename Type> class _PBJCastFlags {
public:
Type operator() (Type input, Type allFlagsOn) {
return input&allFlagsOn;
}
};
#endif