~ubuntu-branches/ubuntu/wily/ifrit/wily

« back to all changes in this revision

Viewing changes to core/ifileloader.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2009-09-13 14:53:24 UTC
  • mfrom: (1.1.11 upstream) (2.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090913145324-084ivqvf29arkc1b
Tags: 3.3.2-1
* New upstream release.
* Bump Standards-Version to 3.8.3.  No changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*=========================================================================
2
 
 
3
 
  Program:   Ionization FRont Interactive Tool (IFRIT)
4
 
  Language:  C++
5
 
 
6
 
 
7
 
Copyright (c) 2002-2006 Nick Gnedin 
8
 
All rights reserved.
9
 
 
10
 
This file may be distributed and/or modified under the terms of the
11
 
GNU General Public License version 2 as published by the Free Software
12
 
Foundation and appearing in the file LICENSE.GPL included in the
13
 
packaging of this file.
14
 
 
15
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
16
 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
 
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
19
 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
 
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
 
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
 
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
 
 
26
 
=========================================================================*/
 
1
/*=========================================================================
 
2
 
 
3
  Program:   Ionization FRont Interactive Tool (IFRIT)
 
4
  Language:  C++
 
5
 
 
6
 
 
7
Copyright (c) 2002-2006 Nick Gnedin 
 
8
All rights reserved.
 
9
 
 
10
This file may be distributed and/or modified under the terms of the
 
11
GNU General Public License version 2 as published by the Free Software
 
12
Foundation and appearing in the file LICENSE.GPL included in the
 
13
packaging of this file.
 
14
 
 
15
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
 
16
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
17
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
18
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
 
19
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
20
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
21
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
22
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
23
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
24
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
25
 
 
26
=========================================================================*/
27
27
 
28
28
 
29
29
#include "ifileloader.h"
55
55
{ \
56
56
        iFileLoader_Private::SwapBytes(p); \
57
57
} \
58
 
void iFileLoader::SwapBytesRange(_type_ *p, long count) \
 
58
void iFileLoader::SwapBytesRange(_type_ *p, vtkIdType count) \
59
59
{ \
60
60
        iFileLoader_Private::SwapBytesRange(p,count); \
61
61
}
62
62
 
63
63
#define IDATASUBJECT_DEFINE_READ_BLOCK_FUNCTIONS(_type_) \
64
 
bool iFileLoader::ReadBlock(iFile& F, _type_ *p, long len, float updateStart, float updateDuration) \
 
64
bool iFileLoader::ReadBlock(iFile& F, _type_ *p, vtkIdType len, float updateStart, float updateDuration) \
65
65
{ \
66
66
        return iFileLoader_Private::ReadBlockToArray(F,p,len,updateStart,updateDuration,mIsBigEndian!=iSystem::IsBigEndianMachine(),mObserver); \
67
67
} \
69
69
{ \
70
70
        return ReadingBuffer(&d,1UL,iFileLoader_Private::TypeIndex(&d),sizeof(_type_)); \
71
71
} \
72
 
iFileLoader::ReadingBuffer iFileLoader::Buffer(_type_ *d, long l) const \
 
72
iFileLoader::ReadingBuffer iFileLoader::Buffer(_type_ *d, vtkIdType l) const \
73
73
{ \
74
74
        return ReadingBuffer(d,l,iFileLoader_Private::TypeIndex(d),sizeof(_type_)); \
75
75
}
98
98
        }
99
99
 
100
100
        
101
 
        void Swap4BytesRange(char *data, long count)
 
101
        void Swap4BytesRange(char *data, vtkIdType count)
102
102
        {
103
103
                char *pos;
104
 
                long i;
 
104
                vtkIdType i;
105
105
 
106
106
                pos = data;
107
107
                for(i = 0; i<count; i++)
112
112
        }
113
113
 
114
114
 
115
 
        void Swap8BytesRange(char *data, long count)
 
115
        void Swap8BytesRange(char *data, vtkIdType count)
116
116
        {
117
117
                char *pos;
118
 
                long i;
 
118
                vtkIdType i;
119
119
 
120
120
                pos = data;
121
121
                for(i = 0; i<count; i++)
150
150
 
151
151
 
152
152
        template<class T>
153
 
        inline void SwapBytesRange(T *p, long count)
 
153
        inline void SwapBytesRange(T *p, vtkIdType count)
154
154
        {
155
155
                switch(sizeof(T))
156
156
                {
175
175
        //
176
176
        //  Check validity of numbers
177
177
        //
178
 
        bool IsNumberValid(int v){ return true; }
179
 
        bool IsNumberValid(long v){ return true; }
180
 
        bool IsNumberValid(float v){ return (-iMath::_FloatMax<=v && v<=iMath::_FloatMax); }
181
 
        bool IsNumberValid(double v){ return (-iMath::_DoubleMax<=v && v<=iMath::_DoubleMax); }
 
178
        bool IsNumberValid(vtkTypeInt32 v){ return true; }
 
179
        bool IsNumberValid(vtkTypeInt64 v){ return true; }
 
180
        bool IsNumberValid(vtkTypeFloat32 v){ return (-iMath::_FloatMax<=v && v<=iMath::_FloatMax); }
 
181
        bool IsNumberValid(vtkTypeFloat64 v){ return (-iMath::_DoubleMax<=v && v<=iMath::_DoubleMax); }
182
182
 
183
183
 
184
184
        //
185
185
        //  Type indices
186
186
        //
187
 
        const int _Int = 1;
188
 
        const int _Long = 2;
189
 
        const int _Float = 3;
190
 
        const int _Double = 4;
 
187
        const int _Int32 = 1;
 
188
        const int _Int64 = 2;
 
189
        const int _Float32 = 3;
 
190
        const int _Float64 = 4;
191
191
 
192
 
        int TypeIndex(int *){ return _Int; }
193
 
        int TypeIndex(long *){ return _Long; }
194
 
        int TypeIndex(float *){ return _Float; }
195
 
        int TypeIndex(double *){ return _Double; }
 
192
        int TypeIndex(vtkTypeInt32 *){ return _Int32; }
 
193
        int TypeIndex(vtkTypeInt64 *){ return _Int64; }
 
194
        int TypeIndex(vtkTypeFloat32 *){ return _Float32; }
 
195
        int TypeIndex(vtkTypeFloat64 *){ return _Float64; }
196
196
 
197
197
 
198
198
        //
199
199
        //  Read blocks of data from a file
200
200
        //
201
201
        template<class T>
202
 
        bool ReadBlockToArray(iFile& F, T *p, long len, float updateStart, float updateDuration, bool swapbytes, iProgressEventObserver *observer)
 
202
        bool ReadBlockToArray(iFile& F, T *p, vtkIdType len, float updateStart, float updateDuration, bool swapbytes, iProgressEventObserver *observer)
203
203
        {
204
 
                const long nline = 16384L;
205
 
                long nread, l, nstep = (len+nline-1)/nline;
 
204
                const vtkIdType nline = 16384L;
 
205
                vtkIdType nread, l, nstep = (len+nline-1)/nline;
206
206
                float ustep = updateDuration/nstep;
207
207
 
208
208
                for(l=0; l<nstep; l++) 
232
232
        {
233
233
                switch(b.Type)
234
234
                {
235
 
                case _Int:
236
 
                        {
237
 
                                int *d = (int *)b.Data;
238
 
                                return iFileLoader_Private::ReadBlockToArray(F,d,b.Length,updateStart,updateDuration,swapbytes,observer);
239
 
                        }
240
 
                case _Long:
241
 
                        {
242
 
                                long *d = (long *)b.Data;
243
 
                                return iFileLoader_Private::ReadBlockToArray(F,d,b.Length,updateStart,updateDuration,swapbytes,observer);
244
 
                        }
245
 
                case _Float:
246
 
                        {
247
 
                                float *d = (float *)b.Data;
248
 
                                return iFileLoader_Private::ReadBlockToArray(F,d,b.Length,updateStart,updateDuration,swapbytes,observer);
249
 
                        }
250
 
                case _Double:
251
 
                        {
252
 
                                double *d = (double *)b.Data;
 
235
                case _Int32:
 
236
                        {
 
237
                                vtkTypeInt32 *d = (vtkTypeInt32 *)b.Data;
 
238
                                return iFileLoader_Private::ReadBlockToArray(F,d,b.Length,updateStart,updateDuration,swapbytes,observer);
 
239
                        }
 
240
                case _Int64:
 
241
                        {
 
242
                                vtkTypeInt64 *d = (vtkTypeInt64 *)b.Data;
 
243
                                return iFileLoader_Private::ReadBlockToArray(F,d,b.Length,updateStart,updateDuration,swapbytes,observer);
 
244
                        }
 
245
                case _Float32:
 
246
                        {
 
247
                                vtkTypeFloat32 *d = (vtkTypeFloat32 *)b.Data;
 
248
                                return iFileLoader_Private::ReadBlockToArray(F,d,b.Length,updateStart,updateDuration,swapbytes,observer);
 
249
                        }
 
250
                case _Float64:
 
251
                        {
 
252
                                vtkTypeFloat64 *d = (vtkTypeFloat64 *)b.Data;
253
253
                                return iFileLoader_Private::ReadBlockToArray(F,d,b.Length,updateStart,updateDuration,swapbytes,observer);
254
254
                        }
255
255
                default:
324
324
        mShift[0] = mShift[1] = mShift[2] = 0.0f;
325
325
 
326
326
        mPeriodic[0] = mPeriodic[1] = mPeriodic[2] = false;
327
 
        mBoundaryConditions = _BoundaryConditionsWall;
 
327
        mBoundaryConditions = BoundaryConditions::Wall;
328
328
 
329
329
        //
330
330
        //  Event observers are driven directly (otherwise we would have to create at least 16 new events, which is
394
394
 
395
395
void iFileLoader::SetBoundaryConditions(int s)
396
396
{
397
 
        if(s>=0 && s<__NumBoundaryConditions)
 
397
        if(BoundaryConditions::IsValid(s))
398
398
        {
399
399
                mBoundaryConditions = s;
400
400
        }
605
605
void iFileLoader::ShiftData(double dr[3])
606
606
{
607
607
        int i;
608
 
        double dxmax, dx[3];
 
608
        double dx[3];
609
609
 
610
610
        mObserver->Started(iProgressEventObserver::_Shifting);
611
611
        mObserver->SetProgress(0.0);
612
612
 
613
 
        dxmax = 0.0;
614
 
        for(i=0; i<3; i++)
615
 
        {
616
 
                dx[i] = dr[i] - mShift[i];
617
 
                if(fabs(dx[i]) > dxmax) dxmax = fabs(dx[i]);
618
 
        }
619
 
 
620
 
        if(dxmax > 1.0e-30)
621
 
        {
622
 
                for(i=0; i<this->NumStreams(); i++) if(mStreams[i]->ReleasedData != 0)
623
 
                {
624
 
                        this->ShiftDataBody(mStreams[i]->ReleasedData,dx);
625
 
                }
626
 
                for(i=0; i<3; i++) mShift[i] = dr[i];
627
 
        }
 
613
        for(i=0; i<3; i++) dx[i] = dr[i] - mShift[i];
 
614
 
 
615
        for(i=0; i<this->NumStreams(); i++) if(mStreams[i]->ReleasedData != 0)
 
616
        {
 
617
                this->ShiftDataBody(mStreams[i]->ReleasedData,dx);
 
618
        }
 
619
 
 
620
        for(i=0; i<3; i++) mShift[i] = dr[i];
628
621
 
629
622
        mObserver->Finished();
630
623
}
653
646
//
654
647
//  Data reading helpers
655
648
//
656
 
bool iFileLoader::SkipFortranRecord(iFile& F, long len)
 
649
bool iFileLoader::SkipFortranRecord(iFile& F, vtkIdType len)
657
650
{
658
 
        long lrec1, lrec2;
 
651
        vtkIdType lrec1, lrec2;
659
652
 
660
653
        if(!this->ReadFortranHeaderFooter(F,lrec1)) return false;
661
654
        if(!F.SkipBlock(len)) return false;
668
661
 
669
662
bool iFileLoader::ReadFortranRecord(iFile& F, const ReadingBuffer &b, float updateStart, float updateDuration, bool autoswap)
670
663
{
671
 
        long lrec1, lrec2;
 
664
        vtkIdType lrec1, lrec2;
672
665
 
673
666
        if(!this->ReadFortranHeaderFooter(F,lrec1)) return false;
674
667
 
684
677
 
685
678
bool iFileLoader::ReadFortranRecord(iFile& F, const ReadingBuffer &b1, const ReadingBuffer &b2, float updateStart, float updateDuration, bool autoswap)
686
679
{
687
 
        long lrec1, lrec2;
688
 
        long len = b1.Length*b1.Size + b2.Length*b2.Size;
 
680
        vtkIdType lrec1, lrec2;
 
681
        vtkIdType len = b1.Length*b1.Size + b2.Length*b2.Size;
689
682
        float f1 = float(b1.Length*b1.Size)/len;
690
683
        float f2 = float(b2.Length*b2.Size)/len;
691
684
 
705
698
 
706
699
bool iFileLoader::ReadFortranRecord(iFile& F, const ReadingBuffer &b1, const ReadingBuffer &b2, const ReadingBuffer &b3, float updateStart, float updateDuration, bool autoswap)
707
700
{
708
 
        long lrec1, lrec2;
709
 
        long len = b1.Length*b1.Size + b2.Length*b2.Size + b3.Length*b3.Size;
 
701
        vtkIdType lrec1, lrec2;
 
702
        vtkIdType len = b1.Length*b1.Size + b2.Length*b2.Size + b3.Length*b3.Size;
710
703
        float f1 = float(b1.Length*b1.Size)/len;
711
704
        float f2 = float(b2.Length*b2.Size)/len;
712
705
        float f3 = float(b3.Length*b3.Size)/len;
729
722
        
730
723
bool iFileLoader::ReadFortranRecord(iFile& F, int nbufs, const ReadingBuffer *b, float updateStart, float updateDuration, bool autoswap) // read any number of buffers
731
724
{
732
 
        long lrec1, lrec2;
 
725
        vtkIdType lrec1, lrec2;
733
726
 
734
727
        if(b==0 || nbufs<=0 || !this->ReadFortranHeaderFooter(F,lrec1)) return false;
735
728
 
736
729
        int i;
737
 
        long len = 0L;
 
730
        vtkIdType len = 0L;
738
731
        for(i=0; i<nbufs; i++)
739
732
        {
740
733
                len += b[i].Length*b[i].Size;
749
742
}
750
743
 
751
744
 
752
 
bool iFileLoader::ReadFortranHeaderFooter(iFile& F, long &lrec)
 
745
bool iFileLoader::ReadFortranHeaderFooter(iFile& F, vtkIdType &lrec)
753
746
{
754
747
        bool ret = false;
755
748
 
756
 
        if(mFortranHeaderFooterLength == sizeof(long))
 
749
        if(mFortranHeaderFooterLength == 8)
757
750
        {
758
 
                ret = F.ReadBlock(&lrec,sizeof(long));
759
 
                if(ret && mIsBigEndian!=iSystem::IsBigEndianMachine()) this->SwapBytes(lrec);
 
751
                vtkTypeInt64 ltmp;
 
752
                ret = F.ReadBlock(&ltmp,8);
 
753
                if(ret)
 
754
                {
 
755
                        if(mIsBigEndian != iSystem::IsBigEndianMachine()) this->SwapBytes(ltmp);
 
756
                        if(sizeof(vtkIdType) < 8)
 
757
                        {
 
758
                                IERROR_LOW("VTK is compiled with 32-bit Ids. The record length of this file is 64-bits, it cannot be read with this VTK installation. To be able to read this file you will have to recompile VTK with the advanced option VTK_USE_64BIT_IDS set to ON and then recompile IFrIT.");
 
759
                                return false;
 
760
                        }
 
761
                        lrec = ltmp;
 
762
                }
760
763
        }
761
 
        else if(mFortranHeaderFooterLength == sizeof(int))
 
764
        else if(mFortranHeaderFooterLength == 4)
762
765
        {
763
 
                int ltmp;
764
 
                ret = F.ReadBlock(&ltmp,sizeof(int));
 
766
                vtkTypeInt32 ltmp;
 
767
                ret = F.ReadBlock(&ltmp,4);
765
768
                if(ret)
766
769
                {
767
770
                        if(mIsBigEndian != iSystem::IsBigEndianMachine()) this->SwapBytes(ltmp);
768
 
                        lrec = (long)ltmp;
 
771
                        lrec = ltmp;
769
772
                }
770
773
        }
771
774
        else
777
780
}
778
781
 
779
782
 
780
 
bool iFileLoader::DetectFortranFileStructure(iFile &F, long len)
 
783
bool iFileLoader::DetectFortranFileStructure(iFile &F, vtkIdType len)
781
784
{
782
785
        //
783
 
        //  Try int-sized records
 
786
        //  Try 4-byte records
784
787
        //
785
 
        if(AnalyzeFirstRecordForType<int>(F,len,mIsBigEndian))
 
788
        if(AnalyzeFirstRecordForType<vtkTypeInt32>(F,len,mIsBigEndian))
786
789
        {
787
 
                mFortranHeaderFooterLength = sizeof(int);
 
790
                mFortranHeaderFooterLength = sizeof(vtkTypeInt32);
788
791
                return true;
789
792
        }
790
793
        //
791
 
        //  Try long-sized records
 
794
        //  Try 8-byte records
792
795
        //
793
 
        if(AnalyzeFirstRecordForType<long>(F,len,mIsBigEndian))
 
796
        if(AnalyzeFirstRecordForType<vtkTypeInt64>(F,len,mIsBigEndian))
794
797
        {
795
 
                mFortranHeaderFooterLength = sizeof(long);
 
798
                mFortranHeaderFooterLength = sizeof(vtkTypeInt64);
796
799
                return true;
797
800
        }
798
801
        //
862
865
//
863
866
//  Overloads
864
867
//
865
 
IDATASUBJECT_DEFINE_SWAP_BYTES_FUNCTIONS(int);
866
 
IDATASUBJECT_DEFINE_SWAP_BYTES_FUNCTIONS(long);
867
 
IDATASUBJECT_DEFINE_SWAP_BYTES_FUNCTIONS(float);
868
 
IDATASUBJECT_DEFINE_SWAP_BYTES_FUNCTIONS(double);
869
 
//IDATASUBJECT_DEFINE_SWAP_BYTES_FUNCTIONS(unsigned int);
870
 
//IDATASUBJECT_DEFINE_SWAP_BYTES_FUNCTIONS(unsigned long);
 
868
IDATASUBJECT_DEFINE_SWAP_BYTES_FUNCTIONS(vtkTypeInt32);
 
869
IDATASUBJECT_DEFINE_SWAP_BYTES_FUNCTIONS(vtkTypeInt64);
 
870
IDATASUBJECT_DEFINE_SWAP_BYTES_FUNCTIONS(vtkTypeFloat32);
 
871
IDATASUBJECT_DEFINE_SWAP_BYTES_FUNCTIONS(vtkTypeFloat64);
871
872
 
872
 
IDATASUBJECT_DEFINE_READ_BLOCK_FUNCTIONS(int);
873
 
IDATASUBJECT_DEFINE_READ_BLOCK_FUNCTIONS(long);
874
 
IDATASUBJECT_DEFINE_READ_BLOCK_FUNCTIONS(float);
875
 
IDATASUBJECT_DEFINE_READ_BLOCK_FUNCTIONS(double);
876
 
//IDATASUBJECT_DEFINE_READ_BLOCK_FUNCTIONS(unsigned int);
877
 
//IDATASUBJECT_DEFINE_READ_BLOCK_FUNCTIONS(unsigned long);
 
873
IDATASUBJECT_DEFINE_READ_BLOCK_FUNCTIONS(vtkTypeInt32);
 
874
IDATASUBJECT_DEFINE_READ_BLOCK_FUNCTIONS(vtkTypeInt64);
 
875
IDATASUBJECT_DEFINE_READ_BLOCK_FUNCTIONS(vtkTypeFloat32);
 
876
IDATASUBJECT_DEFINE_READ_BLOCK_FUNCTIONS(vtkTypeFloat64);
878
877
 
879
878
 
880
879
//