00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef LDM_DATA_ACCESS_H
00024 #define LDM_DATA_ACCESS_H
00025
00026 #include <Inventor/SbLinear.h>
00027 #include <Inventor/SbBox.h>
00028 #include <LDM/SoLDMTileID.h>
00029
00030 #include <Inventor/STL/vector>
00031 #include <Inventor/STL/map>
00032
00033 #include <Inventor/devices/SoBufferObject.h>
00034 #include <Inventor/devices/SoCpuBufferObject.h>
00035
00036 #include <LDM/nodes/SoLDMResourceParameters.h>
00037
00038 #ifdef _MSC_VER
00039 #pragma warning( push )
00040 #pragma warning(disable:4251)
00041 #endif
00042
00043 class SoDataSet;
00044 class SoLDMMediator;
00045 class SoPerfCounter;
00046 class SoPerfCounterManager;
00047 class SoCpuBufferObject;
00048 class SbThread;
00049 class SbThreadBarrier;
00050 class SoLDMAlgorithms;
00051
00052
00247 class SoLDMDataAccess {
00248
00249 public:
00250
00256 SoLDMDataAccess();
00257
00261 virtual ~SoLDMDataAccess();
00262
00266 void setDataSet(SoDataSet* v);
00267
00271 inline SoDataSet* getDataSet();
00272
00274 enum ErrorValue
00275 {
00277 CORRECT,
00279 INCORRECT_RESOLUTION,
00281 INCORRECT_SUBVOLUME,
00283 INCORRECT_LINE,
00285 INCORRECT_POLYLINE,
00287 INCORRECT_PLANE,
00289 INCORRECT_POSITION,
00291 INCORRECT_COORDINATE,
00293 MEMORY_FULL,
00295 REQUEST_NOT_COMPLETED,
00297 REQUEST_NOT_FOUND,
00303 INCORRECT_BUFFER
00304 };
00305
00311 enum GetDataMode
00312 {
00314 CACHE,
00317 DIRECT,
00320 DIRECT_AND_PREFETCH
00321 };
00322
00332 struct DataInfoBox
00333 {
00337 ErrorValue errorFlag;
00339 int64_t bufferSize;
00343 SbVec3i32 bufferDimension;
00344 };
00345
00355 struct DataInfoTrace
00356 {
00360 ErrorValue errorFlag;
00362 int64_t bufferSize;
00365 int bufferDimension;
00366 };
00367
00377 struct DataInfoLine
00378 {
00382 ErrorValue errorFlag;
00384 int64_t bufferSize;
00386 int bufferDimension;
00388 SbVec3i32 lineCoord[2];
00389 };
00400 class DataInfoPolyLine
00401 {
00402 public:
00403
00405 DataInfoPolyLine(int numPoint);
00406 virtual ~DataInfoPolyLine();
00407
00409 DataInfoPolyLine(const DataInfoPolyLine& A);
00410 void operator=(const DataInfoPolyLine& A);
00411
00415 ErrorValue errorFlag;
00416
00418 int64_t bufferSize;
00419
00421 int numSegment;
00422
00425 int* segmentBufferDim;
00426
00429 int* segmentBufferSize;
00430
00436 SbVec3i32** segmentCoord;
00437
00438 private:
00439 private:
00440
00441 DataInfoPolyLine();
00442 void free();
00443 void commonConstructor();
00444 void allocate(int numPoint);
00445 };
00446
00456 struct DataInfoPlane
00457 {
00461 ErrorValue errorFlag;
00463 int64_t bufferSize;
00465 SbVec2i32 bufferDimension;
00470 int numPolygonPoints;
00472 SbVec3i32 polygonCoord[6];
00474 SbVec3i32 quadCoord[4];
00478 int uAxis;
00482 int vAxis;
00483 };
00484
00491 struct DataInfo
00492 {
00496 ErrorValue errorFlag;
00498 SoLDMTileID tileID;
00500 SbVec3i32 tileDimension;
00503 SbBox3i32 tilePosition;
00505 int64_t bufferSize;
00510 void* tileData;
00511 };
00512
00525 DataInfoBox getData(int resolution, const SbBox3i32& subVolume0, SoBufferObject* bufferObj = NULL);
00526
00539 DataInfoPlane getData(int resolution, const SbBox3i32& subVolume0, const SbPlane& plane,
00540 SoBufferObject* bufferObj = NULL);
00541
00554 DataInfoLine getData(int resolution, const SbBox3i32& subVolume0, const SbLine& line , SoBufferObject* bufferObj = NULL) ;
00555
00570 DataInfoTrace getData( int resolution, const SbBox3i32& subVolume, const SbVec2i32 coord,
00571 SoBufferObject* bufferObj = NULL);
00572
00585 void getData(DataInfoPolyLine& infoPolyline, int resolution, const SbBox3i32& subVolume, int numPoints,
00586 const SbVec3i32* polyline, SoBufferObject* bufferObj = NULL);
00587
00642 DataInfo getData(int resolution, const SbVec3i32& dataPosition);
00643
00649 void releaseData(SoLDMTileID tileID);
00650
00659 SbBool isTileUniform(int resolution, const SbVec3i32& dataPosition, double& uniformValue);
00660
00662
00664
00686 int requestData(int resolution, const SbBox3i32& box, SoBufferObject* bufferObj);
00687
00691 void getRequestedData(int requestId, DataInfoBox& infoBox);
00692
00712 int requestData(int resolution, const SbBox3i32& subVolume, const SbPlane& plane, SoBufferObject* bufferObj);
00713
00717 void getRequestedData(int requestId, DataInfoPlane& infoPlane);
00718
00738 int requestData(int resolution, const SbBox3i32& subVolume0, const SbLine& line , SoBufferObject* bufferObj);
00739
00743 void getRequestedData(int requestId, DataInfoLine& infoLine);
00744
00765 int requestData(int resolution, const SbBox3i32& subVolume, int numPoints, const SbVec3i32* polyline, SoBufferObject* bufferObj);
00766
00770 void getRequestedData(int requestId, DataInfoPolyLine& infoPolyline);
00771
00794 int requestData(int resolution, const SbBox3i32& subVolume0, const SbVec2i32& coord, SoBufferObject* bufferObj);
00795
00799 void getRequestedData(int requestId, DataInfoTrace& infoTrace);
00800
00807 virtual void endRequest(int requestId);
00808
00812 SbVec3f voxelToXYZ(const SbVec3i32 &dataPosition);
00813
00817 SbVec3i32 XYZToVoxel(const SbVec3f &dataPosition);
00818
00824 bool setGetDataMode (const GetDataMode getDataMode);
00825
00829 GetDataMode getGetDataMode();
00830
00831
00832 #if 1 SoDEPRECATED
00838 DataInfoBox getData(int resolution, const SbBox3i32& subVolume, void* buffer);
00839 SoDEPRECATED
00844 DataInfoPlane getData(int resolution, const SbBox3i32& subVolume, const SbPlane& plane,
00845 void* buffer);
00846 SoDEPRECATED
00851 DataInfoLine getData(int resolution, const SbBox3i32& subVolume, const SbLine& line , void* buffer);
00852 SoDEPRECATED
00857 DataInfoTrace getData(int resolution, const SbBox3i32& subVolume, const SbVec2i32 coord,
00858 void* buffer );
00859 SoDEPRECATED
00864 void getData(DataInfoPolyLine& infoPolyline, int resolution, const SbBox3i32& subVolume, int numPoints,
00865 const SbVec3i32* polyline, void* buffer);
00866 SoDEPRECATED
00871 int requestData(int resolution, const SbBox3i32& subVolume, void* buffer);
00872 SoDEPRECATED
00877 int requestData(int resolution, const SbBox3i32& subVolume, const SbPlane& plane, void* buffer);
00878 SoDEPRECATED
00883 int requestData(int resolution, const SbBox3i32& subVolume, const SbLine& line, void* buffer);
00884 SoDEPRECATED
00889 int requestData(int resolution, const SbBox3i32& subVolume, int numPoints, const SbVec3i32* polyline, void* buffer);
00890 SoDEPRECATED
00895 int requestData(int resolution, const SbBox3i32& subVolume, const SbVec2i32& coord, void* buffer);
00896
00897 #endif
00899 private:
00900
00901 void initPerfCounters();
00902 void resetPerfCounters();
00903 bool intersectWith(const SbBox3i32 &, const SbBox3i32 &, SbBox3i32 &);
00904 int intersectWith(const SbBox3i32 &, const SbPlane &, int, SbVec3i32 [], SbVec3i32 []) const;
00905 bool intersectWith(const SbBox3i32 &, const SbLine &, SbVec3i32 []) const;
00906 int getZaxis(const SbVec3f &) const;
00907 void copyData(unsigned short dataSetId, SoLDMTileID tile, SoBufferObject* tileBufferObj, int tileIndex);
00908 void copyData(unsigned short dataSetId, SoLDMTileID tile, SoBufferObject* tileBufferObj, int tileIndex, int requestId, bool canRelease=true);
00909 void requestNotCompleted(int requestId);
00910 std::vector<SoLDMTileID> getRequestedTiles(int requestId);
00911 SoDataSet* getRequestingDataSet(int requestId);
00912 bool isTileToPrefecth(const SoLDMTileID &tileId);
00913 void removeTileFromPrefetch(const SoLDMTileID &tileId);
00914 SbThreadBarrier* getDataAccessBarrier();
00915 SbThreadBarrier* getReaderThreadBarrier();
00916
00917
00918 enum CopyWhat{
00919 COPY_BOX,
00920 COPY_PLANE,
00921 COPY_LINE,
00922 COPY_SINGLE,
00923 COPY_TRACE
00924 };
00925
00926 CopyWhat getTypeOfCopy () { return m_copyInfo.copyWhat;};
00927
00928 #if 1 SoDEPRECATED
00931 DataInfo getData(int resolution, const SbVec3s& dataPosition)
00932 {
00933 SbVec3i32 dataPositionI32( dataPosition[0], dataPosition[1], dataPosition[2] );
00934 return getData( resolution, dataPositionI32 );
00935 };
00936 SoDEPRECATED
00938 DataInfoBox getData(int resolution, const SbBox3s& subVolume, void* buffer);
00939 SoDEPRECATED
00941 DataInfoPlane getData( int resolution, const SbBox3s& subVolume, const SbPlane& plane, void* buffer);
00942 SoDEPRECATED
00944 DataInfoLine getData( int resolution, const SbBox3s& subVolume, const SbLine& line , void* buffer);
00945 SoDEPRECATED
00947 void getData(DataInfoPolyLine& infoPolyline, int resolution,
00948 const SbBox3s& subVolume, int numPoints, const SbVec3s* polyline, void* buffer);
00949
00950 #endif
00952 private:
00953 int clipSegment(const SbVec3i32 &, const SbVec3i32 &, SbVec3i32 *, int, const SbVec3i32 &, const SbVec3i32 &) const;
00954
00955
00956 void checkBox(int& _max, int& _min, int volBox);
00957 SbBox3i32 getBox(SbVec3i32 p1, SbVec3i32 p2, SbBox3i32 volBox);
00958
00959
00960
00961 struct CopyInfo {
00962 public:
00963 CopyInfo()
00964 { reset(); }
00965
00966
00967 void reset()
00968 {
00969 destBufferObj = NULL;
00970 userCpuObj = NULL;
00971 vmin = SbVec3i32(0,0,0);
00972 vmax = SbVec3i32(0,0,0);
00973 tileDim = -1;
00974 dataSize = -1;
00975 subVolume.makeEmpty();
00976 resolution = -1;
00977 copyWhat = COPY_SINGLE;
00978 qmin = SbVec2i32(0,0);
00979 qmax = SbVec2i32(0,0);
00980 normal = SbVec3f(0,0,0);
00981 distance = -1;
00982 Zaxis = -1;
00983 voxelsToCopy = NULL;
00984
00985
00986 tiles.clear();
00987 tilesFromDisk.clear();
00988 tilesToPrefetch.clear();
00989 tilesInfos.clear();
00990 tilesIndex.clear();
00991 ldmDataAccess = NULL;
00992 dataSet = NULL;
00993 loadPolicy = LDMResourceParameter::NEVER;
00994 requestSize = -1;
00995 requestId = -1;
00996 shouldCallback = false;
00997 dataInfoPolyline = NULL;
00998 dataInfoLine = NULL;
00999 dataInfoPlane = NULL;
01000 dataInfoBox = NULL;
01001 dataInfoTrace = NULL;
01002 }
01003
01004 public:
01005 SoRef<SoBufferObject> destBufferObj;
01006 SoRef<SoCpuBufferObject> userCpuObj;
01007 SbVec3i32 vmin;
01008 SbVec3i32 vmax;
01009 int tileDim;
01010 int dataSize;
01011 SbBox3i32 subVolume;
01012 int resolution;
01013 CopyWhat copyWhat;
01014 SbVec2i32 qmin;
01015 SbVec2i32 qmax;
01016 SbVec3f normal;
01017 float distance;
01018 SbPlane plane;
01019 int Zaxis;
01020 SbVec2i32 bufDim;
01021 std::vector< std::vector<SbVec2i32> >* voxelsToCopy;
01022
01023 std::vector<SoLDMTileID> tiles;
01024 std::vector<SoLDMTileID> tilesFromDisk;
01025 std::vector<SoLDMTileID> tilesToPrefetch;
01026 std::map<int64_t, SbVec3i32> tilesInfos;
01027 std::map<int64_t, int> tilesIndex;
01028 SoLDMDataAccess* ldmDataAccess;
01029 SoDataSet* dataSet;
01030 LDMResourceParameter::LoadPolicy loadPolicy;
01031 int64_t requestSize;
01032 int requestId;
01033 bool shouldCallback;
01034 DataInfoPolyLine* dataInfoPolyline;
01035 DataInfoLine* dataInfoLine;
01036 DataInfoPlane* dataInfoPlane;
01037 DataInfoBox* dataInfoBox;
01038 DataInfoTrace* dataInfoTrace;
01039 } m_copyInfo;
01040
01041
01042 private:
01043 enum threadType
01044 { NONE = 0,
01045 PLANE,
01046 BOX,
01047 TRACES,
01048 LINE,
01049 POLYLINE
01050 };
01051
01055 SoLDMDataAccess& operator=(const SoLDMDataAccess& ldmDa);
01056 SoLDMDataAccess(const SoLDMDataAccess& );
01057
01058 SoDataSet *m_dataSet;
01059 SoLDMMediator *m_mediator;
01060
01061 std::map<int, CopyInfo> m_requests;
01062
01063
01064 bool getRequestInfo(int resolution, const SbBox3i32& subVolume0, SoBufferObject *bufferObj, DataInfoBox& info, CopyInfo& copyInfo);
01065
01066 bool getRequestInfo(int resolution, const SbBox3i32& subVolume0, const SbPlane& plane, SoBufferObject *bufferObj,
01067 SoLDMDataAccess::DataInfoPlane& info, SoLDMDataAccess::CopyInfo& copyInfo);
01068
01069 bool getRequestInfo(int resolution, const SbBox3i32& subVolume0, const SbLine& line, SoBufferObject *bufferObj,
01070 SoLDMDataAccess::DataInfoLine& info, SoLDMDataAccess::CopyInfo& copyInfo);
01071
01072 bool getRequestInfo(int resolution, const SbBox3i32& subVolume0, const SbVec2i32 coord, SoBufferObject *bufferObj,
01073 SoLDMDataAccess::DataInfoTrace& info, SoLDMDataAccess::CopyInfo& copyInfo);
01074
01075
01076 void copyData(unsigned short , SoLDMTileID tile, SoBufferObject* tileBufferObj, int tileIndex, SoLDMDataAccess::CopyInfo& copyInfo);
01077
01078 static void *readTraceFromDiskThreadRoutine(void* userData);
01079 static void *readSliceFromDiskThreadRoutine(void* userData);
01080 static void *readTilesFromDiskThreadRoutine(void* userData);
01081 void readSliceInTile(SoLDMDataAccess* dataAccess, int fileID, SbBox3i32& tilePos, int& sliceNumber, SoBufferObject* destBuffer );
01082 void buildPlane(SoLDMDataAccess* dataAccess, int firstAxis, int secondAxis, int sliceNumber);
01083 bool createThreadReadDisk(SoLDMDataAccess::threadType threadType);
01084 void updateTilesLists(SoLDMDataAccess::CopyInfo& copyInfo);
01085 void checkGetDataMode (const SoLDMDataAccess::CopyWhat copyWhat);
01086
01087 enum RequestState {
01088 NOT_FOUND,
01089 OK
01090 };
01091
01092 RequestState queryRequestState(int requestId);
01093
01094 int m_nextRequestId;
01095 int getNextRequestId();
01096
01097 SoPerfCounterManager* m_perf;
01098
01099
01100 SoPerfCounter* m_time_copy_data_trace;
01101 SoPerfCounter* m_time_copy_data_vol;
01102 SoPerfCounter* m_time_copy_data_plane;
01103
01104
01105 SoPerfCounter* m_time_tile_select_trace;
01106 SoPerfCounter* m_time_tile_select_vol;
01107 SoPerfCounter* m_time_tile_select_plane;
01108
01109 SoLDMAlgorithms* m_algorithms;
01110
01111 GetDataMode m_getDataMode;
01112
01113 SbThreadBarrier *m_readerBarrier;
01114 SbThreadBarrier *m_diskReadThreadBarrier;
01115 SbThread *m_diskReadThread;
01116 bool m_threadMustStop;
01117 threadType m_threadType;
01118
01119 static void checkBuffer(SoBufferObject* buffer);
01120
01121 private:
01122 CopyInfo& getCopyInfo();
01123 bool threadMustStop();
01124
01125 };
01126
01127
01128 inline SoDataSet*
01129 SoLDMDataAccess::getDataSet()
01130 {
01131 return m_dataSet;
01132 }
01133
01134 inline void
01135 SoLDMDataAccess::endRequest(int)
01136 {
01137 SoDebugError::post("SoLDMDataAccess::endRequest", "An asynchronous access has been made using first class SoLDMDataAccess. Please implement your own, overloading endRequest method.");
01138 }
01139
01140 inline SoLDMDataAccess::GetDataMode
01141 SoLDMDataAccess::getGetDataMode()
01142 {
01143 return m_getDataMode;
01144 }
01145
01146 inline SoLDMDataAccess::CopyInfo&
01147 SoLDMDataAccess::getCopyInfo()
01148 {
01149 return m_copyInfo;
01150 }
01151
01152 inline bool
01153 SoLDMDataAccess::threadMustStop()
01154 {
01155 return m_threadMustStop;
01156 }
01157
01158 inline SbThreadBarrier*
01159 SoLDMDataAccess::getDataAccessBarrier()
01160 {
01161 return m_readerBarrier;
01162 }
01163
01164 inline SbThreadBarrier*
01165 SoLDMDataAccess::getReaderThreadBarrier()
01166 {
01167 return m_diskReadThreadBarrier;
01168 }
01169
01170
01171 inline SoLDMDataAccess&
01172 SoLDMDataAccess::operator=(const SoLDMDataAccess& )
01173 {
01174 assert(0);
01175 return *this;
01176 }
01177
01178 #ifdef _MSC_VER
01179 #pragma warning( pop )
01180 #endif
01181
01182 #endif
01183
01184
01185