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
00822 SbVec3f voxelToXYZ(const SbVec3i32 &dataPosition);
00823
00839 SbBox3f voxelToXYZ( const SbBox3i32& boxIJK );
00840
00854 SbVec3i32 XYZToVoxel(const SbVec3f &dataPosition);
00855
00871 SbBox3i32 XYZToVoxel( const SbBox3f& boxXYZ );
00872
00878 bool setGetDataMode (const GetDataMode getDataMode);
00879
00883 GetDataMode getGetDataMode();
00884
00885
00886 #if 1 SoDEPRECATED
00892 DataInfoBox getData(int resolution, const SbBox3i32& subVolume, void* buffer);
00893 SoDEPRECATED
00898 DataInfoPlane getData(int resolution, const SbBox3i32& subVolume, const SbPlane& plane,
00899 void* buffer);
00900 SoDEPRECATED
00905 DataInfoLine getData(int resolution, const SbBox3i32& subVolume, const SbLine& line , void* buffer);
00906 SoDEPRECATED
00911 DataInfoTrace getData(int resolution, const SbBox3i32& subVolume, const SbVec2i32 coord,
00912 void* buffer );
00913 SoDEPRECATED
00918 void getData(DataInfoPolyLine& infoPolyline, int resolution, const SbBox3i32& subVolume, int numPoints,
00919 const SbVec3i32* polyline, void* buffer);
00920 SoDEPRECATED
00925 int requestData(int resolution, const SbBox3i32& subVolume, void* buffer);
00926 SoDEPRECATED
00931 int requestData(int resolution, const SbBox3i32& subVolume, const SbPlane& plane, void* buffer);
00932 SoDEPRECATED
00937 int requestData(int resolution, const SbBox3i32& subVolume, const SbLine& line, void* buffer);
00938 SoDEPRECATED
00943 int requestData(int resolution, const SbBox3i32& subVolume, int numPoints, const SbVec3i32* polyline, void* buffer);
00944 SoDEPRECATED
00949 int requestData(int resolution, const SbBox3i32& subVolume, const SbVec2i32& coord, void* buffer);
00950
00951 #endif
00953 private:
00954
00955 void initPerfCounters();
00956 void resetPerfCounters();
00957 bool intersectWith(const SbBox3i32 &, const SbBox3i32 &, SbBox3i32 &);
00958 int intersectWith(const SbBox3i32 &, const SbPlane &, int, SbVec3i32 [], SbVec3i32 []) const;
00959 bool intersectWith(const SbBox3i32 &, const SbLine &, SbVec3i32 []) const;
00960 int getZaxis(const SbVec3f &) const;
00961 void copyData(unsigned short dataSetId, SoLDMTileID tile, SoBufferObject* tileBufferObj, int tileIndex);
00962 void copyData(unsigned short dataSetId, SoLDMTileID tile, SoBufferObject* tileBufferObj, int tileIndex, int requestId, bool canRelease=true);
00963 void requestNotCompleted(int requestId);
00964 std::vector<SoLDMTileID> getRequestedTiles(int requestId);
00965 SoDataSet* getRequestingDataSet(int requestId);
00966 bool isTileToPrefecth(const SoLDMTileID &tileId);
00967 void removeTileFromPrefetch(const SoLDMTileID &tileId);
00968 SbThreadBarrier* getDataAccessBarrier();
00969 SbThreadBarrier* getReaderThreadBarrier();
00970
00971
00972 enum CopyWhat{
00973 COPY_BOX,
00974 COPY_PLANE,
00975 COPY_LINE,
00976 COPY_SINGLE,
00977 COPY_TRACE
00978 };
00979
00980 CopyWhat getTypeOfCopy () { return m_copyInfo.copyWhat;};
00981
00982 private:
00983 int clipSegment(const SbVec3i32 &, const SbVec3i32 &, SbVec3i32 *, int, const SbVec3i32 &, const SbVec3i32 &) const;
00984
00985
00986 void checkBox(int& _max, int& _min, int volBox);
00987 SbBox3i32 getBox(SbVec3i32 p1, SbVec3i32 p2, SbBox3i32 volBox);
00988
00989
00990
00991 struct CopyInfo {
00992 public:
00993 CopyInfo()
00994 { reset(); }
00995
00996
00997 void reset()
00998 {
00999 destBufferObj = NULL;
01000 userCpuObj = NULL;
01001 vmin = SbVec3i32(0,0,0);
01002 vmax = SbVec3i32(0,0,0);
01003 tileDim = -1;
01004 dataSize = -1;
01005 subVolume.makeEmpty();
01006 resolution = -1;
01007 copyWhat = COPY_SINGLE;
01008 qmin = SbVec2i32(0,0);
01009 qmax = SbVec2i32(0,0);
01010 normal = SbVec3f(0,0,0);
01011 distance = -1;
01012 Zaxis = -1;
01013 voxelsToCopy = NULL;
01014
01015
01016 tiles.clear();
01017 tilesFromDisk.clear();
01018 tilesToPrefetch.clear();
01019 tilesInfos.clear();
01020 tilesIndex.clear();
01021 ldmDataAccess = NULL;
01022 dataSet = NULL;
01023 loadPolicy = SoLDMResourceParameters::NEVER;
01024 requestSize = -1;
01025 requestId = -1;
01026 shouldCallback = false;
01027 dataInfoPolyline = NULL;
01028 dataInfoLine = NULL;
01029 dataInfoPlane = NULL;
01030 dataInfoBox = NULL;
01031 dataInfoTrace = NULL;
01032 }
01033
01034 public:
01035 SoRef<SoBufferObject> destBufferObj;
01036 SoRef<SoCpuBufferObject> userCpuObj;
01037 SbVec3i32 vmin;
01038 SbVec3i32 vmax;
01039 int tileDim;
01040 int dataSize;
01041 SbBox3i32 subVolume;
01042 int resolution;
01043 CopyWhat copyWhat;
01044 SbVec2i32 qmin;
01045 SbVec2i32 qmax;
01046 SbVec3f normal;
01047 float distance;
01048 SbPlane plane;
01049 int Zaxis;
01050 SbVec2i32 bufDim;
01051 std::vector< std::vector<SbVec2i32> >* voxelsToCopy;
01052
01053 std::vector<SoLDMTileID> tiles;
01054 std::vector<SoLDMTileID> tilesFromDisk;
01055 std::vector<SoLDMTileID> tilesToPrefetch;
01056 std::map<int64_t, SbVec3i32> tilesInfos;
01057 std::map<int64_t, int> tilesIndex;
01058 SoLDMDataAccess* ldmDataAccess;
01059 SoDataSet* dataSet;
01060 SoLDMResourceParameters::LoadPolicy loadPolicy;
01061 int64_t requestSize;
01062 int requestId;
01063 bool shouldCallback;
01064 DataInfoPolyLine* dataInfoPolyline;
01065 DataInfoLine* dataInfoLine;
01066 DataInfoPlane* dataInfoPlane;
01067 DataInfoBox* dataInfoBox;
01068 DataInfoTrace* dataInfoTrace;
01069 } m_copyInfo;
01070
01071
01072 private:
01073 enum threadType
01074 { NONE = 0,
01075 PLANE,
01076 BOX,
01077 TRACES,
01078 LINE,
01079 POLYLINE
01080 };
01081
01085 SoLDMDataAccess& operator=(const SoLDMDataAccess& ldmDa);
01086 SoLDMDataAccess(const SoLDMDataAccess& );
01087
01088 SoDataSet *m_dataSet;
01089 SoLDMMediator *m_mediator;
01090
01091 std::map<int, CopyInfo> m_requests;
01092
01093
01094 bool getRequestInfo(int resolution, const SbBox3i32& subVolume0, SoBufferObject *bufferObj, DataInfoBox& info, CopyInfo& copyInfo);
01095
01096 bool getRequestInfo(int resolution, const SbBox3i32& subVolume0, const SbPlane& plane, SoBufferObject *bufferObj,
01097 SoLDMDataAccess::DataInfoPlane& info, SoLDMDataAccess::CopyInfo& copyInfo);
01098
01099 bool getRequestInfo(int resolution, const SbBox3i32& subVolume0, const SbLine& line, SoBufferObject *bufferObj,
01100 SoLDMDataAccess::DataInfoLine& info, SoLDMDataAccess::CopyInfo& copyInfo);
01101
01102 bool getRequestInfo(int resolution, const SbBox3i32& subVolume0, const SbVec2i32 coord, SoBufferObject *bufferObj,
01103 SoLDMDataAccess::DataInfoTrace& info, SoLDMDataAccess::CopyInfo& copyInfo);
01104
01105
01106 void copyData(unsigned short , SoLDMTileID tile, SoBufferObject* tileBufferObj, int tileIndex, SoLDMDataAccess::CopyInfo& copyInfo);
01107
01108 static void *readTraceFromDiskThreadRoutine(void* userData);
01109 static void *readSliceFromDiskThreadRoutine(void* userData);
01110 static void *readTilesFromDiskThreadRoutine(void* userData);
01111 void readSliceInTile(SoLDMDataAccess* dataAccess, int fileID, SbBox3i32& tilePos, int& sliceNumber, SoBufferObject* destBuffer );
01112 void buildPlane(SoLDMDataAccess* dataAccess, int firstAxis, int secondAxis, int sliceNumber);
01113 bool createThreadReadDisk(SoLDMDataAccess::threadType threadType);
01114 void updateTilesLists(SoLDMDataAccess::CopyInfo& copyInfo);
01115 void checkGetDataMode (const SoLDMDataAccess::CopyWhat copyWhat);
01116
01117 enum RequestState {
01118 NOT_FOUND,
01119 OK
01120 };
01121
01122 RequestState queryRequestState(int requestId);
01123
01124 int m_nextRequestId;
01125 int getNextRequestId();
01126
01127 SoPerfCounterManager* m_perf;
01128
01129
01130 SoPerfCounter* m_time_copy_data_trace;
01131 SoPerfCounter* m_time_copy_data_vol;
01132 SoPerfCounter* m_time_copy_data_plane;
01133
01134
01135 SoPerfCounter* m_time_tile_select_trace;
01136 SoPerfCounter* m_time_tile_select_vol;
01137 SoPerfCounter* m_time_tile_select_plane;
01138
01139 SoLDMAlgorithms* m_algorithms;
01140
01141 GetDataMode m_getDataMode;
01142
01143 SbThreadBarrier *m_readerBarrier;
01144 SbThreadBarrier *m_diskReadThreadBarrier;
01145 SbThread *m_diskReadThread;
01146 bool m_threadMustStop;
01147 threadType m_threadType;
01148
01149 static void checkBuffer(SoBufferObject* buffer);
01150
01151 private:
01152 CopyInfo& getCopyInfo();
01153 bool threadMustStop();
01154
01155 };
01156
01157
01158 inline SoDataSet*
01159 SoLDMDataAccess::getDataSet()
01160 {
01161 return m_dataSet;
01162 }
01163
01164 inline void
01165 SoLDMDataAccess::endRequest(int)
01166 {
01167 SoDebugError::post("SoLDMDataAccess::endRequest", "An asynchronous access has been made using first class SoLDMDataAccess. Please implement your own, overloading endRequest method.");
01168 }
01169
01170 inline SoLDMDataAccess::GetDataMode
01171 SoLDMDataAccess::getGetDataMode()
01172 {
01173 return m_getDataMode;
01174 }
01175
01176 inline SoLDMDataAccess::CopyInfo&
01177 SoLDMDataAccess::getCopyInfo()
01178 {
01179 return m_copyInfo;
01180 }
01181
01182 inline bool
01183 SoLDMDataAccess::threadMustStop()
01184 {
01185 return m_threadMustStop;
01186 }
01187
01188 inline SbThreadBarrier*
01189 SoLDMDataAccess::getDataAccessBarrier()
01190 {
01191 return m_readerBarrier;
01192 }
01193
01194 inline SbThreadBarrier*
01195 SoLDMDataAccess::getReaderThreadBarrier()
01196 {
01197 return m_diskReadThreadBarrier;
01198 }
01199
01200
01201 inline SoLDMDataAccess&
01202 SoLDMDataAccess::operator=(const SoLDMDataAccess& )
01203 {
01204 assert(0);
01205 return *this;
01206 }
01207
01208 #ifdef _MSC_VER
01209 #pragma warning( pop )
01210 #endif
01211
01212 #endif
01213
01214
01215